memory mapping /dev/kmem on X86_64

Jeff Haran Jeff.Haran at
Wed Dec 18 20:05:48 EST 2013

Dear Fellow Kernelnewbies,

I wrote an application that calls mmap64() on /dev/kmem to memory map all of kernel virtual memory in this kernel address range from Documentation/x86/x86_64/mm.txt:

ffff880000000000 - ffffc7ffffffffff (=64 TB) direct mapping of all phys. Memory

I limit the top of the range to what's in the kernel variable high_memory, which I obtained for use in user space by printk()ing it from a simple kernel module that I wrote for that purpose. Sloppy, I know, but this is prototype code at this point.

First thing I found was that the mmap64() call failed because my kernel (RHEL6) didn't have CONFIG_DEVKMEM defined so /dev/kmem didn't even exist. So I rebuilt the kernel with CONFIG_DEVKMEM configured. Now /dev/kmem exists, but the mmap64() call still fails due to CONFIG_STRICT_DEVMEN being configured, the purpose of which seems to be to cripple /dev/kmem and /dev/mem so that only the first MB of RAM and ioremapped registers can be accessed as part of a security thing.

So I am now contemplating turning CONFIG_STRICT_DEVMEN off in my configuration and rebuilding the kernel, but I am concerned that it's still going to fail because CONFIG_X86_PAT is configured and looking at the code it appears that so long as CONFIG_X86_PAT is configured mmap access to RAM via these devices is going to be prohibited either by the range_is_allowed() function in drivers/char/mem.c (which kicks in when CONFIG_STRICT_DEVMEN is enabled) or the identical range_is_allowed() function in arch/x86/pat.c (which kicks in when CONFIG_STRICT_DEVMEM is not configured). In the latter case it appears the mem driver's call to phys_mem_access_prot_allowed() via the weak alias in mem.c will be replaced by the strong one in pat.c, which calls its range_is_allowed() function.

Am I reading this right, that if I keep CONFIG_X86_PAT enabled I will not be able to mmap kernel memory regardless of my setting of CONFIG_STRICT_DEVMEM?

If so, can somebody explain why this needs to be like this when CONFIG_X86_PAT is enabled?

There's comments in the source about avoiding "cache aliasing" in pat.c, but I don't understand what that means.

What are the downsides of disabling CONFIG_X86_PAT?


Jeff Haran

-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Kernelnewbies mailing list