agent.smith
agent.smith

Reputation: 9436

system calls failing for iomapped memory in linux.

I am mapping some io memory to vmalloced area of in the driver. I am also sharing this area with user processes using flag (PAGE_SHARED in ioremap_page_range).

Now I am able to access this shared memory into user space. I can write and read to this memory. However, if I pass this memory as a buffer to the system calls like recv or send then the calls fails with bad memory (Memory not mapped into user process).

However, I am sure my buffer does not have any issues. So, there seems to be some conflict in the way I am sharing memory and error check for system calls.

driver code:

shared_buf = __get_vm_area(size, VM_IOREMAP, VMALLOCS_START, VMALLOC_END); 
ioremap_page_range(shared_buf->addr, size, phy_addr_of_io, PAGE_SHARED);      

After that I do an ioctl call and pass this shared_buf->addr to the user space application.
I write and read by using this addr. Then I do

ret = recv(sockfd, shared_buf->area, 0) and I get an error "bad addr".   

Instead of that if I try

ret = recv(sockfd, local_buf, size, 0);  
memcpy(shared_buf->addr, local_buf, size); Then it goes without issues.   

(Disclaimer: I do use shared_buf->area in the threads which has not done IOCTL. However it is the same process.)

Can anyone see a mistake?

Upvotes: 4

Views: 215

Answers (1)

bdonlan
bdonlan

Reputation: 231253

System calls all validate that the pointer passed in is in the user portion of address space. vmalloc space is not in this user portion; hence, you cannot use it for system calls. More importantly, don't give user processes direct access to memory in the vmalloc address space. That's just asking for trouble. And probably horribly insecure - can other processes access that too? Write an mmapable file instead.

Upvotes: 3

Related Questions