How to get linux kernel page size programmatically

走远了吗. 提交于 2019-12-20 09:28:18

问题


I am working on a Linux module for IA64. My current problem is that the driver uses the PAGE_SIZE and PAGE_SHIFT macros for dma page allocation. The problem I am having is that the machine compiling the driver is not the ones that needed to run the driver. So, if the PAGE_SIZE on the compiling machine is 2^14K and the destination machine is 2^16K then the driver fails.

I don't want to turn this question into a 'best practice' issue about compiling modules on machines which are not the ones running the modules. I understand the issues about that. What I found is that people mostly uses getpagesize() or sysconf(_SC_PAGE_SIZE). These two options are out of the ia64 kernel headers so I can't use them. Is there another way that I could get the runtime PAGE_SIZE?

Options I am looking at:

  • Reading some file in /proc?
  • syscall?
  • Other function that let me calculate the PAGE_SIZE by inference (e.g ORDER, getpageshift, etc)?
  • Other?

回答1:


Try using the getconf utility, which will allow you to retrieve the page size easily.

getconf PAGESIZE



回答2:


One approximate method is to read /proc/meminfo and check Mapped size ( on mine its 52544 kB as of now ) and then check nr_mapped in /proc/vmstat ( on mine its 131136 as of now ). Finally PAGE_SIZE = Mapped/nr_mapped. Sometimes this gives you an accurate value ( as in the current example I've quoted ) and sometimes its approximate but very close. Hope this helps!




回答3:


If you are trying to build a kernel module, you will need to have at least the kernel headers that are configured for the kernel the module will run on. Those will define the page size macros you need. If you don't have the correctly configured headers, the kernel will refuse to load your module.

And there is nothing wrong with compiling a module on one machine to run on another, even if it's a different architecture. You just need to build against the correct kernel source.




回答4:


One way to find the page size is to obtain it from smaps for a process.

For example:

cd /proc/1
grep -i pagesize smaps

KernelPageSize:        4 kB
MMUPageSize:           4 kB



回答5:


This is what I finally did:

  • Re-work my current module to take a new module parameter called page_shift and used that to calculate the PAGE_SIZE (PAGE_SIZE = 1 << PAGE_SHIFT)
  • Created a module loader wrapper which gets the current system PAGE_SHIFT using getconf API from libc. This wrapper gets the current system page shift and pass it as a module parameter.

Right now the module is being loaded on different architectures with different PAGE_SIZE without any problems.




回答6:


I fear that it is impossible to do as page size is something defined as part of the kernel. page size knowledge is required in case of toolchain also which you use to compile the kernel module.

So atleast with current kernel architecture, it is impossible to do so.




回答7:


You could just run a test, just mmap a file with different offsets and see which fail. Might be annoying in a kernel module though, but maybe there is some other test like that you could use.



来源:https://stackoverflow.com/questions/4888067/how-to-get-linux-kernel-page-size-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!