You need to enable JavaScript to run this app. 内核模块分配物理连续页问题及DRAM行大小查询方法咨询
优惠活动
大模型
产品
解决方案
定价
更多

内核模块分配物理连续页问题及DRAM行大小查询方法咨询

问题解答

alloc_pages_exact分配10MB失败问题

  • alloc_pages_exact用于分配物理连续内存,分配成功率高度依赖系统当前的内存碎片化程度。系统运行一段时间后,内存会被分散分配释放,很难找到10MB大小的连续物理块,而1MB的连续块相对容易获取,就会出现你观测到的现象。
  • 代码中while(pages == 0);的死循环没有任何意义,物理内存的碎片化状态不会因为等待自动改善,该逻辑只会导致内核线程永久挂死。
  • 针对Rowhammer实验需要连续物理内存的场景,推荐两种解决方案:
    1. 在系统启动时通过启动参数mem=预留出一段内存,实验时直接映射预留内存使用,不会出现分配失败问题
    2. 调整分配标识,尝试用GFP_KERNEL | __GFP_NOWARN | __GFP_RETRY_MAYFAIL触发内存规整,提升大块连续内存分配成功率
  • 额外代码问题:你代码中自定义的page_size变量实际为DRAM行大小,和内核默认的4KB系统页大小概念冲突,建议重命名为dram_row_size避免逻辑混淆,alloc_pages_exact的分配大小只需要满足系统页大小对齐即可,不需要和DRAM行大小绑定。另外原代码存在指针偏移错误,地址计算时没有乘行大小,会导致访问越界。

DRAM行大小查询方法

不需要查阅内存数据手册,有两个通用方法可以直接从系统读取精确值:

  • 安装cpuid工具后执行命令:cpuid | grep -i dram_row,直接返回内存芯片的行大小
  • 执行命令:dmidecode -t memory,读取内存SPD信息中的行列参数,即可计算出精确的DRAM行大小
  • 注意:DRAM行大小、系统页大小、CPU缓存行大小是三个完全独立的参数,没有必然对应关系,不要混用三者的数值。

修正后参考代码

static int __init hello_entry(void){
    unsigned long i = 0;
    unsigned long j = 0;
    unsigned long counter;
    int fault = 0;
    char default_value = 0xff;
    
    unsigned long size_of_memory_to_test_bytes = 10 * 1024 * 1024;//10MB chunk
    unsigned long dram_row_size = 8 * 1024;//8kB DRAM row size. From data sheet for elpida 4GB ddr3 ram
    unsigned long number_of_rows = size_of_memory_to_test_bytes / dram_row_size;
    unsigned long number_of_hammer_access = 200000;
    
    printk("Number Of Bytes to allocate: %ld\n", size_of_memory_to_test_bytes);
    char* pages = (char *)alloc_pages_exact(size_of_memory_to_test_bytes, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
    
    if (!pages) {
        printk(KERN_ERR "Failed to allocate continuous memory, exit\n");
        return -ENOMEM;
    }
    
    printk("Starting Address: %px", pages);

    char *hammerRow0, *hammerRow1, *victimRow;
    char victimRow_data, read_midvalue;

    printk(KERN_ALERT "Memory Test Entry!\n");
    printk(KERN_ALERT "DRAM Row Size: %ld\n", dram_row_size);
    printk(KERN_ALERT "Cache Line Size: %d\n", cache_line_size());

    //The design consists of two hammer rows which will be repeatedly accessed one after another
    //There will be a victim row in between the two hammer rows, kind of like a victim row sandwiched between two hammer rows
    //This will server two purposes:
    //1. Greater probability of inducing bitflip in the victim row due to repeated access of two adjacent rows.
    //2. Clearing out the row buffer of DRAM chip so that it is guaranteed that the wordline will be accessed to access the data
    printk(KERN_INFO "Rowhammer Test Starts\n");
    for(counter = 0; counter < number_of_rows - 2; counter++){
        //Doing the rowhammer test on every page of dram
        hammerRow0 = pages + counter * dram_row_size;
        victimRow = hammerRow0 + dram_row_size;
        hammerRow1 = victimRow + dram_row_size;

        //Storing initial data into rows to check them later for errors
        for(i = 0; i < dram_row_size / sizeof(char); i++){
            //Setting all bits of dram 1 to maximize the possibility of charge leak
            hammerRow0[i] = default_value;
            victimRow[i] = default_value;
            hammerRow1[i] = default_value;
        }

        //Starting Hammer Test
        fault = 0;
        for(j = 0; j < number_of_hammer_access; j++){
            //hammerRow0 access
            clflush_cache_range(hammerRow0, sizeof(char));
            read_midvalue = hammerRow0[0]; //Reading the first value present in the page
            
            //hammerRow1 access
            clflush_cache_range(hammerRow1, sizeof(char));
            read_midvalue = hammerRow1[0]; //Reading the first value present in the page
        }

        //Checking the victim row for errors
        for(i = 0; i < dram_row_size / sizeof(char); i++){
            victimRow_data = victimRow[i];
            if(victimRow_data != default_value){
                printk(KERN_ALERT "victimRow Fault (After Hammering) at index %ld: Present Value: %x \t Expected Value: %x:\tRow numer %ld\n", i, victimRow_data, 0xff, counter);
                fault = 1;
            }
        }

        if(fault == 1){
            printk(KERN_ALERT "Rowhammer Test Failed\n");
            free_pages_exact(pages, size_of_memory_to_test_bytes);
            return 0;
        }
    }
    printk(KERN_ALERT "Rowhammer Test Passed\n");
    free_pages_exact(pages, size_of_memory_to_test_bytes);
    return 0;
}

static void __exit hello_exit(void){
    printk(KERN_INFO "Memory Test Exit!\n");
}

module_init(hello_entry);
module_exit(hello_exit);

//Licensing Info
MODULE_LICENSE("GPL");
MODULE_AUTHOR("jonvonton");
MODULE_DESCRIPTION("Rowhammer Testing");

内容的提问来源于stack exchange,提问作者jonvonton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 08:36:04