0
votes

In the user space programm I am allocating some memory via mmap as the following function call:

void *memory;
int fd;
fd = open(filepath, O_RDWR);
if (fd < 0)
   return errno;

memory = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED, fd, 0);
if (memory == MAP_FAILED)
      return -1;

//syscall() goes here

In the kernel space in my system call I am trying to copy data to the memory mapped region as follows:

copy_to_user(memory,src,4096);

EDIT: added error checking code to the post for clarification The copy_to_user() call is repeatedly failing in this case, whereas if I would have done a memory = malloc() it was succeeding always.

Am I getting some permission flags wrong in this case for mmap ?

1
what is the error ? Do you use perror() ? We can't help you with only what you actually write.Stargateur
copy_to_user() in kernel space does not sets errno which can be predicted via perror()bawejakunal

1 Answers

0
votes

Does the open succeed? What about mmap? Is the target file big enough? Can you write to the file through the mapping in userspace?

Also, the repeated 4096 is a strong hit your code is wrong. Userspace should pass the expected size instead.