Unpinned futexes v2 - part 1: indexing changes
Subsystems (2)
PATCH 1/2 Unpinned futexes v2 - part 1: indexing changes
Folks listed in To & Cc, please take a look. Barring bugs and fundamental disagreements, this should be the final version.
(Linus may have something to say about the occurence of VM_MAYSHARE. Otherwise there's nothing contentious).
Thanks,
-- Jamie
Patch: futex-fixes-2.6.0-test4-11jl
This patch is against test-2.6.0-test4. It changes the way futexes are indexed, so that they don't pin pages. It also fixes some bugs with private mappings and COW pages.
Currently, all futexes look up the page at the userspace address and pin it, using the pair (page,offset) as an index into a table of waiting futexes. Any page with a futex waiting on it remains pinned in RAM, which is a problem when many futexes are used, especially with FUTEX_FD.
Another problem is that the page is not always the correct one, if it can be changed later by a COW (copy on write) operation. This can happen when waiting on a futex without writing to it after fork(), exec() or mmap(), if the page is then written to before attempting to wake a futex at the same adress.
There are two symptoms of the COW problem: 1. The wrong process can receive wakeups; 2. A process can fail to receive required wakeups.
This patch fixes both by changing the indexing so that VM_SHARED mappings use the triple (inode,offset,index), and private mappings use the pair (mm,virtual_address).
The former correctly handles all shared mappings, including tmpfs and therefore all kinds of shared memory (IPC shm, /dev/shm and MAP_ANON|MAP_SHARED). This works because every mapping which is VM_SHARED has an associated non-zero vma->vm_file, and hence inode. (This is ensured in do_mmap_pgoff, where it calls shmem_zero_setup).
The latter handles all private mappings, both files and anonymous. It isn't affected by COW, because it doesn't care about the actual pages, just the virtual address.
The only obvious problem is that mremap() can move a private mapping without informing futexes waiting on that mapping. However, mremap() was already broken with futexes, because it doesn't update the vcache, so this just changes an existing bug.
(The next patch in this series fixes mremap()).
The patch has a few bonuses:
1. It removes the vcache implementation, as only futexes were using it, and they don't any more.
2. Removing the vcache should make COW page faults a bit faster.
3. Futex operations no longer take the page table lock, walk the page table, fault in pages that aren't mapped in the page table, or do a vcache hash lookup - they are mostly a simple offset calculation with one hash for the futex table. So they should be noticably faster.
Special thanks to Hugh Dickins, Andrew Morton and Rusty Russell for insightful feedback. All suggestions are included.
Enjoy,
-- Jamie
include/linux/mm.h +1 -0
@@ -110,6 +110,7 @@#define VM_RESERVED 0x00080000/* Don't unmap it from swap_out */ #define VM_ACCOUNT 0x00100000/* Is a VM accounted object */ #define VM_HUGETLB 0x00400000/* Huge TLB Page VM */ +#define VM_NONLINEAR 0x00800000/* Is non-linear (remap_file_pages) */ #ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */ #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
include/linux/vcache.h +0 -26
@@ -1,26 +0,0 @@ -/* -* virtual => physical mapping cache support. -*/ -#ifndef _LINUX_VCACHE_H -#define _LINUX_VCACHE_H - -typedef struct vcache_s { -unsigned long address; -struct mm_struct *mm; -struct list_head hash_entry; -void (*callback)(struct vcache_s *data,struct page *new_page); -}vcache_t ; - -extern spinlock_t vcache_lock; - -extern void __attach_vcache (vcache_t *vcache, -unsigned long address, -struct mm_struct *mm, -void (*callback)(struct vcache_s *data,struct page *new_page)); - -extern void __detach_vcache (vcache_t *vcache); - -extern void invalidate_vcache (unsigned long address,struct mm_struct *mm, -struct page *new_page); - -#endif
kernel/futex.c +223 -204
648 lines not on this page · raw message
mm/fremap.c +23 -6
@@ -140,9 +140,10 @@return err;#endif -down_read (&mm->mmap_sem); - +/* We need down_write() to change vma->vm_flags. */ +down_write (&mm->mmap_sem); vma =find_vma (mm, start); +/* * Make sure the vma is shared, that it supports prefaulting, * and that the remapped range is valid and fully within
@@ -151,11 +152,27 @@if (vma && (vma->vm_flags &VM_SHARED ) && vma->vm_ops && vma->vm_ops->populate && end > start && start >= vma->vm_start && - end <= vma->vm_end) - err = vma->vm_ops->populate (vma, start, size, vma->vm_page_prot, - pgoff, flags &MAP_NONBLOCK ); + end <= vma->vm_end) { + +/* Must set VM_NONLINEAR before any pages are populated. */ +if (pgoff != ((start - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff) + vma->vm_flags |=VM_NONLINEAR ; + +/* ->populate can take a long time, so downgrade the lock. */ +downgrade_write (&mm->mmap_sem); + err = vma->vm_ops->populate (vma, start, size, + vma->vm_page_prot, + pgoff, flags &MAP_NONBLOCK ); -up_read (&mm->mmap_sem); +/* +* We can't clear VM_NONLINEAR because we'd have to do +* it after ->populate completes, and that would prevent +* downgrading the lock. (Locks can't be upgraded). +*/ +up_read (&mm->mmap_sem); + }else { +up_write (&mm->mmap_sem); + }return err; }
mm/Makefile +1 -1
@@ -9,6 +9,6 @@ obj-y := bootmem.o filemap.o mempool.o oom_kill.o fadvise.o \ page_alloc.o page-writeback.o pdflush.o readahead.o \ - slab.o swap.o truncate.o vcache.o vmscan.o $(mmu-y) + slab.o swap.o truncate.o vmscan.o $(mmu-y) obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o
mm/memory.c +0 -2
@@ -43,7 +43,6 @@#include <linux/swap.h> #include <linux/highmem.h> #include <linux/pagemap.h> -#include <linux/vcache.h> #include <linux/rmap-locking.h> #include <asm/pgalloc.h>
@@ -960,7 +959,6 @@static inline void break_cow (struct vm_area_struct * vma,struct page * new_page,unsigned long address,pte_t *page_table) { -invalidate_vcache (address, vma->vm_mm, new_page);flush_cache_page (vma, address);establish_pte (vma, address, page_table,pte_mkwrite (pte_mkdirty (mk_pte (new_page, vma->vm_page_prot)))); }
mm/vcache.c +0 -90
90 lines not on this page · raw message
Ok, applied. However:
Quoted 2 lines · Jamie Lokier
(The next patch in this series fixes
mremap()).
I don't think this one is worth it. If the user unmaps or changes the mapping from under the futex, I just think that is "user error". And the same way it is totally undefined what happens if one thread re-organizes the VM space while another thread may be doing some other operation (read() or similar), we just don't care.
Linus
Quoted 7 lines · Linus Torvalds
Quoted 1 line
(The next patch in this series fixes
mremap()).I don't think this one is worth it. If the user unmaps or changes the mapping from under the futex, I just think that is "user error". And the same way it is totally undefined what happens if one thread re-organizes the VM space while another thread may be doing some other operation (
read()or similar), we just don't care.
I agree for synchronous futexes, the inherent race conditions do make it "user error".
For async futexes (FUTEX_FD), I think it has a well-defined and potentially useful meaning to move futexes on remapping, if that's what the program wants to do.
It's well-defined for shared mappings, and then it's even useful (think database with locks distributed in an 8GB file). So it's a little odd for it to fail for private mappings.
I'm not wedded to the idea. Just so long as it's mentioned in "man futex" under "don't do this".
-- Jamie