3

See also this answer: https://stackoverflow.com/a/10770582/1284631

I need something similar, but without having to allocate a buffer: the buffer is large, in theory, but the user space program only needs to access some parts of it, so a limited number of pages.

The question is:

What would be the body of the my_vm_ops.fault() method and what page to return through vmf->page? (it needs to allocate the needed page, but not from a pre-existing buffer)

Community
  • 1
  • 1
user1284631
  • 4,446
  • 36
  • 61
  • If you don't allocate the buffer, where does it come from? – CL. Jun 29 '13 at 10:46
  • @CL. Well, the idea would be to allocate just the pages that are asked for. Do you suggest that for each page fault I should create a buffer with vmalloc_user() of exactly one PAGE_SIZE and, then, return that page to the vmf->page field? Basically, the my_fault() function should be like: {void *my_buf = vmalloc_user(PAGE_SIZE); vmf->page=vmalloc_to_page(my_buf); get_page(vmf->page);} ? What to do then with vmf->pgoff? Does it still has any meaning? PS Thank you. – user1284631 Jun 29 '13 at 10:57
  • @CL. I think this is the answer, indeed: static int my_fault(struct vm_area_struct *vma, struct vm_fault *vmf){void *my_buf = vmalloc_user(PAGE_SIZE); vmf->page=vmalloc_to_page(my_buf); vmf->page=get_page(vmf->page);}. Could you confirm this, pls? return 0; } – user1284631 Jun 29 '13 at 14:50
  • What kind of memory do you need? Why can't the application allocate it? – CL. Jun 29 '13 at 19:12
  • @CL. It is an embedded Linux: no swap, and only 1GB of RAM. I need to fake (virtualize) memory/registers spread over 256 MB of RAM, that I simply have no room for. Is there any method to get_page() a page only if was not previously allocated? – user1284631 Jun 29 '13 at 20:53

1 Answers1

0

What you want to do is already possible by calling mmap with MAP_ANONYMOUS.

Alternatively, call mmap on /dev/zero.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • Thank you, but I cannot modify the user-space program. – user1284631 Jun 30 '13 at 06:26
  • But can you make a symbolic link to `/dev/zero`? – CL. Jun 30 '13 at 09:25
  • I am able to create a symbolic link to /dev/zero, indeed. How should I further proceed? – user1284631 Jun 30 '13 at 18:13
  • The program is looking for some /dev/uiox device, that I am able to make it pointing towards /dev/zero instead. However, if the program mmap() the file (that is /dev/zero), it won't need as much memory to allocate? Does mmap() only allocate pages on demand? And, if yes, how could I emulate the same behavior inside my vmf->fault() method? – user1284631 Jun 30 '13 at 19:06