Skip to contentSkip to conversation

locking user space memory in kernel

DISCUSSION Manfred Spraul 3 participants 8 messages Mar 21, 2004 – Mar 22, 2004

lore
Share & tools
lore
#0 lore raw

Hi Eli,

I think just get_user_pages() should be sufficient: the pages won't be swapped out. You don't need to set VM_LOCKED in vma->vm_flags to prevent the swap out. In the worst case, the pte is cleared a that will cause a soft page fault, but the physical address won't change. Multiple get_user_pages() calls on overlapping regions are ok, the page count is an atomic_t, at least 24-bit large.

--
Manfred

#1 lore raw
Quoted 7 lines · Arjan
Quoted 2 lines · Eli Cohen

Hi,
I need to be able to lock memory allocated in user space and passed to my driver, in order to pass it to a dma controller that can maintain a translation table for each process. The obvious thing is to use

the linux way is to do it the other way around, provide a device that userspace then can mmap......

That's definitively the preferred method, but unfortunately there are existing apis that are the other way around. I think the main MPI transfer functions must read/write to arbitrary addresses, I'm sure there are other examples.

--
Manfred

#2 lore raw
In reply to Manfred Spraul

Manfred> I think just get_user_pages() should be sufficient: the Manfred> pages won't be swapped out. You don't need to set Manfred> VM_LOCKED in vma->vm_flags to prevent the swap out. In Manfred> the worst case, the pte is cleared a that will cause a Manfred> soft page fault, but the physical address won't Manfred> change. Multiple get_user_pages() calls on overlapping Manfred> regions are ok, the page count is an atomic_t, at least Manfred> 24-bit large.

There is one case that we ran into where the physical address can
change: if a process does a fork() and then triggers COW.

- Roland

#3 lore raw
Quoted 9 lines · Roland Dreier

Manfred> I think just get_user_pages() should be sufficient: the Manfred> pages won't be swapped out. You don't need to set Manfred> VM_LOCKED in vma->vm_flags to prevent the swap out. In Manfred> the worst case, the pte is cleared a that will cause a Manfred> soft page fault, but the physical address won't Manfred> change. Multiple get_user_pages() calls on overlapping Manfred> regions are ok, the page count is an atomic_t, at least Manfred> 24-bit large.

Quoted 3 lines

There is one case that we ran into where the physical address can
change: if a process does a fork() and then triggers COW.

You are right.
What should happen if there are registered transfers during fork()? Copy the pages during the fork() syscall?

--
Manfred

#4 lore raw

Manfred> I think just get_user_pages() should be sufficient: the Manfred> pages won't be swapped out. You don't need to set Manfred> VM_LOCKED in vma->vm_flags to prevent the swap out. In Manfred> the worst case, the pte is cleared a that will cause a Manfred> soft page fault, but the physical address won't Manfred> change. Multiple get_user_pages() calls on overlapping Manfred> regions are ok, the page count is an atomic_t, at least Manfred> 24-bit large.

Roland> There is one case that we ran into where the physical Roland> address can change: if a process does a fork() and then Roland> triggers COW.

Manfred> You are right. What should happen if there are Manfred> registered transfers during fork()? Copy the pages Manfred> during the fork() syscall?

The current Mellanox InfiniBand driver goes to some trouble to mark the memory being registered with VM_DONTCOPY. This means the vmas don't get copied into the child of a fork(), so the COW doesn't happen. However, this certainly leads to some quirks in semantics. In particular, an application using fork() has to be careful that registered memory doesn't share a page with something the child process wants to use.

I don't think copying all the registered memory on fork() is feasible, because it's going to kill performance (especially since exec() is likely to immediately follow the fork() in the child). Also, there may not be enough memory around to copy everything.

Out of curiousity, what happens if I fork with pending AIO in the current kernel?

- Roland

#5 lore raw
Quoted 7 lines · Roland Dreier

I don't think copying all the registered memory on fork() is feasible, because it's going to kill performance (especially since exec() is likely to immediately follow the fork() in the child). Also, there may not be enough memory around to copy everything.

Suppose a new vma flag is introduced, VM_NOCOW and an API to apply this flag on a range of addreses, splitting or unifying vmas as necessary. A driver which registers memory with hardware would call this function. When fork takes place, the ptes of the parent belonging to such vmas will not be changed to read only thus they will not undergo COW. The kernel will copy the first and last pages of theses vmas to the child. All the pages in between will be marked read only and will undergo COW when written to. One problem would be that that child can read pages of the parent after the parent modifies them but that could be avoided if the address space of the child does not inherit the range of the moddle pages. ??? Eli

#6 lore raw
In reply to Roland Dreier
Quoted 7 lines · Roland Dreier

I don't think copying all the registered memory on fork() is feasible, because it's going to kill performance (especially since exec() is likely to immediately follow the fork() in the child). Also, there may not be enough memory around to copy everything.

Suppose a new vma flag is introduced, VM_NOCOW and an API to apply this flag on a range of addreses, splitting or unifying vmas as necessary. A driver which registers memory with hardware would call this function. When fork takes place, the ptes of the parent belonging to such vmas will not be changed to read only thus they will not undergo COW. The kernel will copy the first and last pages of theses vmas to the child. All the pages in between will be marked read only and will undergo COW when written to. One problem would be that that child can read pages of the parent after the parent modifies them but that could be avoided if the address space of the child does not inherit the range of the middle pages. ??? Eli

#7 lore raw
Quoted 10 lines · Eli Cohen
Quoted 7 lines · Roland Dreier

I don't think copying all the registered memory on fork() is feasible, because it's going to kill performance (especially since exec() is likely to immediately follow the fork() in the child). Also, there may not be enough memory around to copy everything.

Suppose a new vma flag is introduced, VM_NOCOW and an API to apply this flag on a range of addreses, splitting or unifying vmas as necessary.

Something like that. But it should be hidden within a suitable abstraction. get_user_pages and then put_page is not stateful enough. Actually it's fundamentally broken for platform that need cache flush calls. create_page_mapping/free_page_mapping, or something like that.

And I still think that the initial implementation should copy the affected pages within fork() - it might be slow, but at least it's simple and correct. _If_ it's too slow, then it can be fixed later.

--
Manfred