kmem_cache_alloc() in Windows

Hi Experts,

Am porting a linux device driver to windows Kmdf bus driver. In the linux they did allocated an object from cache by using kmem_cache_alloc() .

When I gone through google, i can get some idea about this kmem_cache_alloc(),it is the method in which we can keep multiple copies of the frequently requiring structure allocated in advance & when it is wanted ,it can be used . So that it returns the address of the block already allocated struct.

I need to implement this exact kmem_cache_alloc() in Windows.

How can I achieve this allocation?

Thanks in Advance,
Janaki

> I need to implement this exact kmem_cache_alloc() in Windows.

ExAllocateFromNPagedLookasideList


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

Note that if you want to keep your structures preinitialized, ExAllocateFromNPagedLookasideList does NOT give the guarantee.

>Am porting a linux device driver to windows Kmdf bus driver. In the linux they did allocated an object from cache by using kmem_cache_alloc().

MmAllocateContiguousMemorySpecifyCache can be used to allocate a block of cached memory. The allocation is page-aligned. This means that two consecutive allocations will consume at least two pages.

“Drivers must not access memory beyond the requested allocation size. For example, developers should not assume that their drivers can safely use memory between the end of their requested allocation and the next page boundary.”

So, my understanding is that you can obtain a block of cached memory that can serve as a heap or pool of cached memory but the heap mechanism (objects/blocks allocator, destructor …) must be implemented by you.

Or you can use ExAllocatePoolWithTag to let the OS manage caching and forget about kmem_cache_alloc.

Read the documentation to obtain more informations.

https://msdn.microsoft.com/en-us/library/windows/hardware/ff554464(v=vs.85).aspx

Good luck.

Maxim S. Shatskih wrote:

> I need to implement this exact kmem_cache_alloc() in Windows.
ExAllocateFromNPagedLookasideList

We don’t have “upvotes” in the forum, but THIS is the correct answer to
Janaki’s wquestion.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Yes, it’s true. Forget about MmAllocateContiguousMemorySpecifyCache.

> MmAllocateContiguousMemorySpecifyCache

…should never be used anywhere outside of the DMA adapter’s common buffers implementations.

In all other contexts, the physical contiguity of the pages is not important.

The allocation is page-aligned.

Pool allocations larger than a page are also page-aligned.

kmem_cache_alloc.

The word “cache” in kmem_cache_alloc() has the different meaning as in MmAllocateContiguousMemorySpecifyCache.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

>The word “cache” in kmem_cache_alloc() has the different meaning as in
MmAllocateContiguousMemorySpecifyCache.

Yes and I even concluded that a “cached heap” was a feature of Unix (Solaris) that was missing in Windows. But as you pointed out, kmem_cache_alloc does not allocate cached memory.

Thank you.

IIRC, “kcalloc() equivalent” thread somehow evolved into something reminiscent of our epic discussions that we had back in 2007-2008, with Alberto and Mr.Kyler as active participants - it somehow grew to 175 posts, and, IIRC, I managed to clash with a good half of its participants. I just wonder if “kmem_cache_alloc() thread” may have the same potential…

Anton Bassov

> The word “cache” in kmem_cache_alloc() has the different meaning as in >MmAllocateContiguousMemorySpecifyCache.

Well, Abdel’s logic here is that MmAllocateContiguousMemorySpecifyCache() is roughly kmalloc()'s
equivalent (as you must know, memory that kmalloc() allocates is physically contiguous), on top of which kmem_cache_alloc() is already built. However, as you have properly pointed out, so precise simulation of Linux specifics would be simply absurd in this context…

Anton Bassov

>Windows. But as you pointed out, kmem_cache_alloc does not allocate cached memory.

Not correct. It allocates cached (i.e. usual) memory. Cached in the CPU cache.

Nevertheless, what is called “cache” in kmem_cache_alloc() is called “lookaside” in Windows.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

Thanks for your Inputs. I will make use of the function and get back the response to you .