Skip to content

fix: add VRAM fallback for non-amdgpu drivers - #957

Closed
GongHeng2017 wants to merge 1 commit into
linuxdeepin:develop/eaglefrom
GongHeng2017:agent/agent/76ea10653a2a
Closed

GongHeng2017 wants to merge 1 commit into
linuxdeepin:develop/eaglefrom
GongHeng2017:agent/agent/76ea10653a2a

Conversation

@GongHeng2017

@GongHeng2017 GongHeng2017 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

DeviceGpu::setHwinfoInfo() has three VRAM detection paths, all of which fail with the radeon driver. The primary path reads /sys/<SysFS_ID>/gpu-info, which is an amdgpu-only sysfs file — radeon does not expose it, so file.open() silently fails and m_GraphicsMemory stays empty. The lshw path maps the memory field to m_MemAddress (address range, not size), and the dmesg path requires a VRAM keyword that radeon driver dmesg output does not contain.

Fix Approach

Added a fallback block in setHwinfoInfo() after the gpu-info read path, triggered only when m_GraphicsMemory is empty. It first tries the DRM sysfs interface (/sys/class/drm/card*/device/mem_info_visible_vram_size), matching the GPU by PCI BDF from m_SysPath. If that fails, it falls back to parsing the Memory Size field from hwinfo output. This is consistent with the analysis report's recommended fix (section 10).

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Fallback only triggers when m_GraphicsMemory is empty; amdgpu path is completely unchanged
  • New logic performs read-only sysfs file reads and mapInfo lookups with proper error handling
  • Historical insight: setHwinfoInfo() has prior VRAM-fixing commits (NVIDIA gpu-info path, JJW GPU special case); this change follows the same pattern without reverting any historical fix

Business Impact Scope

  • GPU VRAM display: radeon-driver GPUs (e.g., Turks Radeon HD 7600) will now show VRAM size in device manager; amdgpu GPUs unaffected
  • Other device types: CPU, storage, network, and other GPU info fields are not affected

Verification Suggestion

  1. Test GPU VRAM display on a machine with amdgpu driver — verify no regression (VRAM still shows)
  2. Test GPU VRAM display on a machine with radeon driver — verify VRAM now displays (e.g., 2GB for Radeon HD 7600)
  3. Test on a machine without DRM sysfs interface — verify hwinfo fallback works

根因分析

DeviceGpu::setHwinfoInfo() 有三条显存检测路径,在 radeon 驱动下全部失效。主路径读取 /sys/<SysFS_ID>/gpu-info,该 sysfs 文件为 amdgpu 专有,radeon 驱动不暴露,file.open() 静默失败,m_GraphicsMemory 保持为空。lshw 路径将 memory 字段映射到 m_MemAddress(地址范围,非大小),dmesg 路径要求 VRAM 关键字但 radeon 驱动 dmesg 输出不含此关键字。

修复方案

在 setHwinfoInfo() 的 gpu-info 读取路径之后新增 fallback 块,仅在 m_GraphicsMemory 为空时触发。优先尝试 DRM sysfs 接口(/sys/class/drm/card*/device/mem_info_visible_vram_size),通过 m_SysPath 中的 PCI BDF 匹配当前 GPU。若失败,则从 hwinfo 输出的 Memory Size 字段解析显存大小。与分析报告第十节方案一致。

改动安全评估

代码安全评估

  • 风险等级: 低
  • fallback 仅在 m_GraphicsMemory 为空时触发,amdgpu 路径完全不变
  • 新增逻辑为只读 sysfs 文件读取和 mapInfo 查找,均有错误处理
  • 历史洞察:setHwinfoInfo() 有历史显存修复提交(NVIDIA gpu-info 路径、JJW 显卡特殊处理),本次改动遵循相同模式,不回退任何历史修复

业务影响范围

  • GPU 显存显示:radeon 驱动 GPU(如 Turks Radeon HD 7600)将在设备管理器中显示显存大小;amdgpu GPU 不受影响
  • 其他设备类型:CPU、存储、网络及其他 GPU 信息字段不受影响

验证建议

  1. 在 amdgpu 驱动机器上测试 GPU 显存显示——验证无回归
  2. 在 radeon 驱动机器上测试 GPU 显存显示——验证显存正常显示(如 Radeon HD 7600 显示 2GB)
  3. 在无 DRM sysfs 接口的机器上测试——验证 hwinfo fallback 生效

Summary by Sourcery

Add fallback VRAM detection so GPUs without amdgpu-specific sysfs data display their memory size correctly.

Bug Fixes:

  • Restore VRAM size detection for GPUs using non-amdgpu drivers such as radeon.
  • Preserve existing VRAM detection while adding hwinfo-based recovery when DRM sysfs data is unavailable.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GongHeng2017

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR preserves the existing amdgpu VRAM detection path while adding radeon/non-amdgpu recovery: it scans DRM sysfs entries for the GPU matching the device PCI BDF, reads the visible VRAM byte count, formats it for display, and finally uses hwinfo's Memory Size field when DRM data is unavailable.

Flow diagram for GPU VRAM fallback detection

flowchart TD
    A[setHwinfoInfo] --> B{m_GraphicsMemory is empty?}
    B -->|No| Z[Keep existing VRAM value]
    B -->|Yes| C[Extract PCI BDF from m_SysPath]
    C --> D[Scan DRM card device symlinks]
    D --> E{PCI BDF matches?}
    E -->|Yes| F[Read mem_info_visible_vram_size]
    E -->|No| D
    F --> G{Valid VRAM byte count?}
    G -->|Yes| H[Format VRAM as GB or MB]
    G -->|No| I{Memory Size in mapInfo?}
    I -->|Yes| J[Use hwinfo Memory Size]
    I -->|No| K[Leave VRAM empty]
    H --> L[Display graphics memory]
    J --> L
Loading

File-Level Changes

Change Details Files
Add a layered VRAM fallback for GPUs whose driver does not provide the amdgpu-specific gpu-info sysfs file.
  • Trigger fallback only when the existing VRAM value is empty.
  • Derive the GPU PCI BDF from the device sysfs path and match it against DRM card symlinks.
  • Read and format mem_info_visible_vram_size as MB or GB when available.
  • Fall back to the hwinfo Memory Size field if DRM VRAM discovery fails.
deepin-devicemanager/src/DeviceManager/DeviceGpu.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

1. Root cause: gpu-info sysfs file is amdgpu-only, radeon driver
   does not expose it, m_GraphicsMemory stays empty
2. Fix: add fallback in setHwinfoInfo() - try DRM sysfs
   mem_info_visible_vram_size, then hwinfo "Memory Size" field
3. Impact: radeon GPU VRAM now displays; amdgpu path unchanged

Log: Fixed GPU VRAM not displayed with radeon driver

Influence:
1. Test GPU VRAM display with amdgpu driver (no regression)
2. Test GPU VRAM display with radeon driver
3. Test GPU VRAM display on machines without DRM sysfs interface

fix: 添加非amdgpu驱动显存检测的fallback

1. 根因:gpu-info sysfs文件为amdgpu专有,radeon驱动不暴露,
   m_GraphicsMemory保持为空
2. 方案:在setHwinfoInfo()中添加fallback,优先尝试DRM sysfs
   mem_info_visible_vram_size,其次从hwinfo输出解析
3. 影响:radeon驱动GPU显存可正常显示,amdgpu路径不受影响

Log: 修复radeon驱动下显卡显存不显示的问题

Influence:
1. 测试amdgpu驱动下GPU显存显示(无回归)
2. 测试radeon驱动下GPU显存显示
3. 测试无DRM sysfs接口机器的GPU显存显示

Bug: #380847
PMS: BUG-380847
@GongHeng2017
GongHeng2017 force-pushed the agent/agent/76ea10653a2a branch from a6d63d6 to 01e6eca Compare October 28, 2026 12:50
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 93 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交为DeviceGpu::setHwinfoInfo添加了VRAM回退获取逻辑,用于在amdgpu驱动不适用时(如radeon驱动)通过DRM sysfs接口和hwinfo输出获取显存大小。代码逻辑清晰,注释完善,无安全漏洞。存在少量代码质量问题(魔法数字、已废弃宏),但不影响功能正确性。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ["统一VRAM大小的输出格式,例如统一使用'XGB'或'X MB'格式,避免不同来源产生不一致的显示"]


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['将1048576替换为命名常量(如static const quint64 BYTES_PER_MB = 1024 * 1024)', '将foreach替换为for (const QString &card : cards)', "使用QString::number(curSize, 'f', 1)限制GB值小数位数为1位"]


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['将硬编码的sysfs路径提取为static const QString或宏定义,便于维护']


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例

// 建议改进示例:使用命名常量、range-for、限制精度
if (m_GraphicsMemory.isEmpty()) {
    static const quint64 BYTES_PER_MB = 1024 * 1024;
    static const double MB_PER_GB = 1024.0;
    QString pciBdf = m_SysPath.section('/', -1);
    if (!pciBdf.isEmpty()) {
        QDir drmDir("/sys/class/drm");
        QStringList cards = drmDir.entryList(QStringList() << "card[0-9]*",
                                              QDir::Dirs | QDir::NoDotAndDotDot);
        for (const QString &card : cards) {
            if (card.contains('-'))
                continue;
            QString devicePath = QFile::symLinkTarget(
                QString("/sys/class/drm/%1/device").arg(card));
            if (!devicePath.contains(pciBdf))
                continue;
            QFile memFile(QString("/sys/class/drm/%1/device/mem_info_visible_vram_size")
                              .arg(card));
            if (memFile.open(QIODevice::ReadOnly)) {
                QString memStr = memFile.readAll().trimmed();
                memFile.close();
                bool ok;
                quint64 vramBytes = memStr.toULongLong(&ok);
                if (ok && vramBytes >= BYTES_PER_MB) {
                    quint64 vramMB = vramBytes / BYTES_PER_MB;
                    auto curSize = vramMB / MB_PER_GB;
                    if (curSize > 1) {
                        m_GraphicsMemory = QString::number(curSize, 'f', 1) + "GB";
                    } else {
                        m_GraphicsMemory = QString::number(vramMB) + "MB";
                    }
                    break;
                }
            }
        }
    }
    if (m_GraphicsMemory.isEmpty() && mapInfo.contains("Memory Size")) {
        m_GraphicsMemory = mapInfo["Memory Size"].trimmed();
    }
}

本报告由 AI 代码审查工具自动生成

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants