omkar1707
omkar1707

Reputation: 75

How to statically allocate memory in kernel module using mmap?

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

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 ]

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

Upvotes: 0

Views: 1090

Answers (1)

skrrgwasme
skrrgwasme

Reputation: 9633

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."

  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.)

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.

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

Upvotes: 0

Related Questions