How to get linux kernel page size programmatically

前端 未结 7 1434
悲哀的现实
悲哀的现实 2021-01-31 07:56

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 tha

相关标签:
7条回答
  • 2021-01-31 08:41

    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.

    0 讨论(0)
  • 2021-01-31 08:44

    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!

    0 讨论(0)
  • 2021-01-31 08:46

    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.

    0 讨论(0)
  • 2021-01-31 08:48

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

    getconf PAGESIZE
    
    0 讨论(0)
  • 2021-01-31 08:58

    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
    
    0 讨论(0)
  • 2021-01-31 09:01

    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.

    0 讨论(0)
提交回复
热议问题