如何使用mmap在内核模块中静态分配内存?

时间:2021-10-25 21:23:25

I need to allocate memory statically in kernel module using mmap in device driver to perform following operations - 1. write in kernel and read in userspace 2. write in userspace and read in kernel

我需要在设备驱动程序中使用mmap在内核模块中静态分配内存以执行以下操作:1。在内核中写入并在用户空间中读取2.在用户空间中写入并在内核中读取

I am able to do by dynamic memory allocation as given in following link - [http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-8.html ]

我可以通过以下链接中给出的动态内存分配来做 - [http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-8.html]

Just used device driver instead of debugfs. How I can do it statically?

刚使用设备驱动程序而不是debugfs。我怎么能静态地做到这一点?

1 个解决方案

#1


0  

TL;DR

You cannot statically map memory.

您无法静态映射内存。

Long Version

The term "static" is used a bit ambiguously to describe memory allocations, so I'll clarify here:

术语“静态”有点含糊不清地用来描述内存分配,所以我在这里澄清一下:

  1. As described in the GNU C Library documentation, static allocation is when you use the keyword "static" or create a global variable. The space for these variables is allocated "when your program is started" and "is never freed."

    如GNU C Library文档中所述,静态分配是指使用关键字“static”或创建全局变量。这些变量的空间在“程序启动时”和“永不释放”时分配。

  2. The term "static allocation" is also used to describe what is more properly called "automatic allocation." These are your local variables and function parameters, which are allocated when code execution enters the variable's scope, and they are freed when that scope is exited. (People often describe this type of allocation as "static" because they're contrasting it with "dynamic" memory allocation, such as with malloc - but "automatic" is the proper term.)

    术语“静态分配”也用于描述更恰当地称为“自动分配”的内容。这些是您的局部变量和函数参数,它们在代码执行进入变量的作用域时分配,并在退出该作用域时释放它们。 (人们经常将这种类型的分配描述为“静态”,因为它们将它与“动态”内存分配形成对比,例如使用malloc - 但“自动”是正确的术语。)

I'm not sure which of these meanings of "static" you intended, but it doesn't really matter. You can't make the mapping happen at the same time that these allocations happen.

我不确定你想要哪种“静态”含义,但这并不重要。您无法在这些分配发生的同时进行映射。

The process of allocating memory for mapping is different from normal allocations - you can allocate a single byte for a char (disregarding architectural alignment requirements), but the size of memory-mapped spaces must be a multiple of page size. Working with mapped memory is like dealing with a file - you explicitly read from and write to it, in contrast with normal variables, where you assign values to them.

为映射分配内存的过程与正常分配不同 - 您可以为char分配单个字节(忽略架构对齐要求),但内存映射空间的大小必须是页面大小的倍数。使用映射内存就像处理文件一样 - 您明确地读取和写入文件,与普通变量相反,您可以在其中为其分配值。

Integral data types and mapped memory are fundamentally different, and don't have the same capabilities or usage.

集成数据类型和映射内存根本不同,并且没有相同的功能或用法。

#1


0  

TL;DR

You cannot statically map memory.

您无法静态映射内存。

Long Version

The term "static" is used a bit ambiguously to describe memory allocations, so I'll clarify here:

术语“静态”有点含糊不清地用来描述内存分配,所以我在这里澄清一下:

  1. As described in the GNU C Library documentation, static allocation is when you use the keyword "static" or create a global variable. The space for these variables is allocated "when your program is started" and "is never freed."

    如GNU C Library文档中所述,静态分配是指使用关键字“static”或创建全局变量。这些变量的空间在“程序启动时”和“永不释放”时分配。

  2. The term "static allocation" is also used to describe what is more properly called "automatic allocation." These are your local variables and function parameters, which are allocated when code execution enters the variable's scope, and they are freed when that scope is exited. (People often describe this type of allocation as "static" because they're contrasting it with "dynamic" memory allocation, such as with malloc - but "automatic" is the proper term.)

    术语“静态分配”也用于描述更恰当地称为“自动分配”的内容。这些是您的局部变量和函数参数,它们在代码执行进入变量的作用域时分配,并在退出该作用域时释放它们。 (人们经常将这种类型的分配描述为“静态”,因为它们将它与“动态”内存分配形成对比,例如使用malloc - 但“自动”是正确的术语。)

I'm not sure which of these meanings of "static" you intended, but it doesn't really matter. You can't make the mapping happen at the same time that these allocations happen.

我不确定你想要哪种“静态”含义,但这并不重要。您无法在这些分配发生的同时进行映射。

The process of allocating memory for mapping is different from normal allocations - you can allocate a single byte for a char (disregarding architectural alignment requirements), but the size of memory-mapped spaces must be a multiple of page size. Working with mapped memory is like dealing with a file - you explicitly read from and write to it, in contrast with normal variables, where you assign values to them.

为映射分配内存的过程与正常分配不同 - 您可以为char分配单个字节(忽略架构对齐要求),但内存映射空间的大小必须是页面大小的倍数。使用映射内存就像处理文件一样 - 您明确地读取和写入文件,与普通变量相反,您可以在其中为其分配值。

Integral data types and mapped memory are fundamentally different, and don't have the same capabilities or usage.

集成数据类型和映射内存根本不同,并且没有相同的功能或用法。