我正在尝试使用 vmalloc()
为内核模块分配大内存。
我无法在具有 64GB 内存的 64 位 Linux (3.10.0-514.2.2.el7.x86_64) 上分配超过 2GB 的内存。
这些是相关的代码部分:
...
static int logical_block_size = 512;
module_param(logical_block_size, int, 0);
static int nsectors = 1024; /* How big the drive is */
module_param(nsectors, int, 0);
...
/*
* The internal representation of our device.
*/
static struct sbd_device {
unsigned long size;
spinlock_t lock;
u8 *data;
struct gendisk *gd;
} Device;
...
static int __init sbd_init(void) {
/*
* Set up our internal device.
*/
Device.size = nsectors * logical_block_size;
spin_lock_init(&Device.lock);
Device.data = vmalloc(Device.size);
...
可通过vmalloc
分配的内存大小是否有限制?有没有其他方法可以为内核模块分配大量内存?
最佳答案
您引用这里的代码:Simple Block Driver在评论中,这是回答您的问题所必需的。
原因是,您正在尝试分配 16 艾字节 的数据。
sbd_init()
中的这个计算是原因:
Device.size = nsectors * logical_block_size;
Device.size 是unsigned long
,而模块参数nsectors
和logical_block_size
是整数。
现在,当您将 locgical_block_size 设置为 1024
并将 nsectors 设置为 2097152
(总计 2GB 空间)时,计算将以有符号整数,因此结果是:
1024 * 2097152 = -2147483648
当它隐式转换为 unsigned long
(通过分配给 Device.size)时,结果为 18446744071562067968
,然后传递给 vmalloc( )
,(可能)略微超过物理内存和 vmalloc 保留区域,在 linux x86_64 上为 32TB。
解决方案是以unsigned long
的形式执行计算:
Device.size = (unsigned long) nsectors * logical_block_size;
然后它应该按预期工作。
关于c - 使用 vmalloc 为内核模块分配大量内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44259749/