0

I am allocating memory to the buffer in kernel space. I want to pin the pages and get the pinned kernel pages. But I did not find any APIs to do that. There is one API called get_kernel_page(unsigned long start, int write, struct ** pages) which pins a single page in kernel space. If I want to pin more than one page (Ex: 25 pages) in kernel space then there is no API to do that. My questions are,

  1. Does anyone know how to pin pages in kernel space by just sending starting address of the buffer and number of pages to be pinned?
  2. If the allocated memory in kernel space is physically non-contiguous and virtually contiguous, then can we pin the pages?

NOTE: There is an API called get_user_pages_fast(unsigned long start, int nr_pages ,int write, struct ** pages) which pins the pages. But here the memory should be allocated in user space. I want similar API to pin the memory which is allocated in kernel space

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Well there is [`get_kernel_pages()`](https://elixir.bootlin.com/linux/v4.20/source/mm/swap.c#L149), though not as straightforward as `get_user_pages()`... – Marco Bonelli Aug 26 '22 at 11:55
  • @MarcoBonelli Thanks....What's your thoughts on my second question? – Vinay Hasyagar Aug 26 '22 at 12:49
  • 2
    "I am allocating memory to the buffer in kernel space." - Which function do you use for that allocation? If you use `kmalloc`, the the result is already pinned: all in-kernel memory is always mapped. – Tsyvarev Aug 26 '22 at 17:10
  • @Tsyvarev Thanks for your reply. I am not sure which is the function that used to allocate memory. The memory is allocated in videobuf2 framework of linux subsystem and the pointer is returned in the driver. If the memory is already pinned then how can I get the pinned pages (address of the pinned pages) ? – Vinay Hasyagar Aug 29 '22 at 04:29
  • 1
    There is `kmap_to_page` function, which probably is intended to extract page corresponding to the kernel address. I am unsure, whether that page is intended to be "transferred" to the user: as far as I know, the memory allocated by `kmalloc` should be used in the kernel space only. – Tsyvarev Aug 29 '22 at 06:59
  • @Tsyvarev thank you for your reply. Yeah I went through the kmap_to_page(). It pins or returns only one page at a time. If the buffer size is 25625 bytes and page size is 4096 bytes then it constitutes totally 25 pages. If you want to get all the 25 pages, you need to move the base address of the buffer. Is there any extra calculation needed for this? – Vinay Hasyagar Aug 29 '22 at 15:02
  • `kmap_to_page` only works on the addresses returned by `kmap` (and similar functions). – Ian Abbott Aug 30 '22 at 15:04

0 Answers0