fix(grub): Replace DeepinGfxmodeNotSupported mechanism with EDID hash caching#1043
Conversation
There was a problem hiding this comment.
Sorry @electricface, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: electricface The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
hash caching - Deprecate DeepinGfxmodeNotSupported key: Removed from the parameter initialization list, allowedKeys, and finishGfxmodeDetect. It is now unconditionally deleted. - Add GetConnectedEdidsHash function (grub_common): Iterates through connected monitors' EDIDs and calculates an MD5 hash to detect changes in monitor connection status. - Add grub_gfx/cache.go: Defines DetectCache (containing EdidsHash + MaxGfxmode) and provides loadDetectCache / saveDetectCache to persist detection results. - Integrate EDID hash comparison in detectChange: Compares the EDID hash against the cache; marks detectCacheChanged=true only when the hash or maxGfxmode changes. - Update needDetect: Replaces the original maxNotSupported parameter with detectCacheChanged. The logic is now: config parsing failed OR config mismatch OR (current resolution is not max && cache has changed). - Enhance logging: Logs GRUB params, EDID hash, needDetect conditions, and the final result to facilitate debugging. --- fix(grub): 用EDID哈希缓存替代DeepinGfxmodeNotSupported机制 - 废弃 DeepinGfxmodeNotSupported 键:从参数初始化列表、allowedKeys 及 finishGfxmodeDetect 中移除,改为无条件 delete - 新增 GetConnectedEdidsHash 函数(grub_common),遍历已连接显示器 的 EDID 并计算 MD5 哈希,用于感知显示器连接状态变化 - 新增 grub_gfx/cache.go:定义 DetectCache(EdidsHash + MaxGfxmode), 提供 loadDetectCache / saveDetectCache,持久化检测结果 - 在 detectChange 中引入 EDID 哈希与缓存比较,仅在哈希或 maxGfxmode 变化时才标记 detectCacheChanged=true - needDetect 以 detectCacheChanged 替换原 maxNotSupported 参数, 逻辑改为:配置解析失败 || 配置与当前不一致 || (当前非最大分辨率 && 缓存已变化) - 加强日志记录:打印 grub params、EDID 哈希、needDetect 各条件 及最终结果,便于调试 Influence: GRUB主题 PMS: BUG-277733, BUG-291909
d367cbb to
ad86888
Compare
deepin pr auto review代码审查意见整体评价这次提交引入了一个新的缓存机制来优化GRUB图形模式检测过程,通过使用EDID哈希值来避免重复检测。整体代码结构清晰,但存在一些安全性和性能方面的改进空间。 具体问题与建议1. 安全性问题1.1 MD5哈希算法安全性不足在 h := md5.New()问题:MD5已被证明存在碰撞攻击风险,不适合用于安全敏感场景。虽然这里用于缓存键值可能不会直接导致安全问题,但建议使用更安全的哈希算法。 建议:改用SHA-256或SHA-3等更安全的哈希算法: import "crypto/sha256"
// 在GetConnectedEdidsHash函数中替换
h := sha256.New()1.2 文件权限设置在 return os.WriteFile(detectCacheFile, data, 0644)问题:0644权限意味着所有用户都可读,如果缓存文件包含敏感信息(如系统配置信息),可能会有信息泄露风险。 建议:考虑使用更严格的权限,如0600: return os.WriteFile(detectCacheFile, data, 0600)2. 性能问题2.1 EDID数据排序效率在 sort.Slice(edids, func(i, j int) bool {
return bytes.Compare(edids[i], edids[j]) < 0
})问题:对于大量EDID数据, 建议:可以考虑使用更高效的比较方式,或者如果EDID数据量不大,可以保持现状。 2.2 缓存文件I/O操作在 data, err := os.ReadFile(detectCacheFile)
// ...
return os.WriteFile(detectCacheFile, data, 0644)建议:考虑添加缓存文件的内存缓存,减少磁盘I/O操作。 3. 代码质量问题3.1 错误处理在 edidsHash, err := grub_common.GetConnectedEdidsHash()
if err != nil {
logger.Warning("failed to get connected edids hash:", err)
}问题:获取EDID哈希失败后,代码仍然继续执行,可能导致缓存不一致。 建议:考虑在获取EDID哈希失败时,直接返回或使用默认值: edidsHash, err := grub_common.GetConnectedEdidsHash()
if err != nil {
logger.Warning("failed to get connected edids hash:", err)
return // 或者使用默认值
}3.2 日志记录在 logger.Debugf("needDetect: cfgGfxmodeErr != nil: %v (cfgGfxmodeErr: %v)", condCfgGfxmodeErr, cfgGfxmodeErr)
logger.Debugf("needDetect: cfgGfxmode != currentGfxmode: %v (cfgGfxmode: %v, currentGfxmode: %v)", condCfgNeqCurrent, cfgGfxmode, currentGfxmode)
logger.Debugf("needDetect: currentGfxmode != maxGfxmode && detectCacheStale: %v (currentGfxmode: %v, maxGfxmode: %v, detectCacheStale: %v)", condCurrentNeqMaxAndCacheStale, currentGfxmode, maxGfxmode, detectCacheStale)
logger.Debug("needDetect result:", result)建议:考虑将详细的调试日志封装到一个函数中,减少主流程中的日志代码: func logNeedDetectDetails(condCfgGfxmodeErr, condCfgNeqCurrent, condCurrentNeqMaxAndCacheStale bool, cfgGfxmodeErr error, cfgGfxmode, currentGfxmode, maxGfxmode grub_common.Gfxmode) {
logger.Debugf("needDetect: cfgGfxmodeErr != nil: %v (cfgGfxmodeErr: %v)", condCfgGfxmodeErr, cfgGfxmodeErr)
logger.Debugf("needDetect: cfgGfxmode != currentGfxmode: %v (cfgGfxmode: %v, currentGfxmode: %v)", condCfgNeqCurrent, cfgGfxmode, currentGfxmode)
logger.Debugf("needDetect: currentGfxmode != maxGfxmode && detectCacheStale: %v (currentGfxmode: %v, maxGfxmode: %v, detectCacheStale: %v)", condCurrentNeqMaxAndCacheStale, currentGfxmode, maxGfxmode, detectCacheStale)
}3.3 注释和文档
建议:为所有公共函数添加文档注释,说明其用途、参数和返回值。 4. 逻辑问题4.1 缓存验证逻辑在 detectCache, err := loadDetectCache()
logger.Debugf("loaded detect cache: %+v", detectCache)
if err == nil && detectCache.equal(edidsHash, maxGfxmode) {
detectCacheStale = false
}问题:即使缓存加载失败(err != nil),代码仍然会继续执行,可能导致不必要的检测。 建议:考虑更明确的缓存验证逻辑: detectCache, err := loadDetectCache()
if err != nil {
logger.Debugf("failed to load detect cache: %v", err)
detectCacheStale = true
} else if detectCache.equal(edidsHash, maxGfxmode) {
detectCacheStale = false
logger.Debug("detect cache is valid")
} else {
detectCacheStale = true
logger.Debug("detect cache is stale")
}总结这次提交引入的缓存机制是一个很好的优化,可以减少不必要的图形模式检测。主要需要改进的是:
这些改进将提高代码的安全性、可维护性和健壮性。 |
|
/forcemerge |
initialization list, allowedKeys, and finishGfxmodeDetect. It is
now unconditionally deleted.
through connected monitors' EDIDs and calculates an MD5 hash to
detect changes in monitor connection status.
persist detection results.
EDID hash against the cache; marks detectCacheChanged=true only
when the hash or maxGfxmode changes.
with detectCacheChanged. The logic is now: config parsing failed
OR config mismatch OR (current resolution is not max && cache
has changed).
conditions, and the final result to facilitate debugging.
fix(grub): 用EDID哈希缓存替代DeepinGfxmodeNotSupported机制
及 finishGfxmodeDetect 中移除,改为无条件 delete
的 EDID 并计算 MD5 哈希,用于感知显示器连接状态变化
提供 loadDetectCache / saveDetectCache,持久化检测结果
maxGfxmode 变化时才标记 detectCacheChanged=true
逻辑改为:配置解析失败 || 配置与当前不一致 ||
(当前非最大分辨率 && 缓存已变化)
及最终结果,便于调试
Influence: GRUB主题
PMS: BUG-277733, BUG-291909