4

How to share memory between user space and kernel which is not backed by a file ? If mmap is done using MAP_ANON, how to share it with kernel space?

I have a data structure

struct data {
int x;
char* y;
};

This data structure is updated by user process and kernel. If I use mmap, I do not have a file which is backed. How do I do it. Also I cannot use copy_from_user as the structure is updated by kernel and user process periodically. Please let know

  • If you control the kernel code that accesses such data structures. you can, for example, update that code to create a character device or a file in debugfs and use that file to back the mmapped memory. See also: http://stackoverflow.com/questions/11501527/ – Eugene Jun 09 '14 at 07:56
  • Note that if you want to pass strings between the user-space and the kernel space (as `char * y` in that structure suggests), you also need to store the string itself (not only the pointer to it) somewhere in the memory shared between the user space and the kernel space. Or, rely on copy_to_user/copy_from_user for it. – Eugene Jun 09 '14 at 08:00
  • 1
    Possible duplicate of [How to mmap a Linux kernel buffer to user space?](https://stackoverflow.com/questions/10760479/how-to-mmap-a-linux-kernel-buffer-to-user-space) – Ciro Santilli OurBigBook.com Jan 01 '18 at 18:55

2 Answers2

0

I can suggest you something on similar line, but i am not sure if this is what you need: i have developed this on PowerPC.

  1. limit your memory that kernel can see (say 2MB less than earlier) i.e. if original memory was 1GB, new memory is 1022MB, with the help of mem=1022M in bootargs

  2. now create this structure at 1022MB by ioremap(start address=0x3FE00000 , size=2MB) in kernel.

    struct data *data_kernel = ioremap(...)

  3. use/update the same in user space by mmap (start address=0x3FE0 0000, size 2MB)

    struct data *data_user = mmap(...)

ashish
  • 361
  • 3
  • 8
0

Why not using a UIO Driver ? The uio driver export a /dev/uioX file used by mmap to share memory between kernel space and user space.

The main goal of uio is to share physical memory with user application, to rapidly develop a userspace driver. But you can also share kernel allocated memory with it if your configure your uio_mem structure memtype with UIO_MEM_LOGICAL.

FabienM
  • 3,421
  • 23
  • 45