Skip to content
Advertisement

Should I release data after the use of get_user_pages_fast?

I’m using the get_user_pages_fast, which I allocate a memory buffer in the user and create a pages in the kernel space.

Should I free the struct pages** after the use this memory? or call to specific release function?

Thanks!

Advertisement

Answer

From documentation on get_user_pages() (which has similar functionality, but with more parameters, and needs a semaphore held):

Each page returned must be released with a put_page call when it is finished with. vmas will only remain valid while mmap_sem is held.


As a side node, if you were to be freeing something, it’d be a struct pages * being passed (freeing a struct pages), not struct pages **, since a pointer to struct pages * is passed to be used as a return value.

However, you typically shouldn’t be assuming that you should free arbitrary things in the kernel without knowing that you’re supposed to. In general, the kernel provides functions to create and destroy whatever objects you’re working with.

Often, when you’re given a pointer, there’s a lot more going on behind the scenes. There could be semaphores, counts of references, etc. That may also be a pointer to a “real” used object in the kernel, not just some structure made for you, so freeing it could rip the rug out form under other pieces of code.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement