tmp_suning_uos_patched/mm
Tejun Heo 5b990546e3 mempool: fix and document synchronization and memory barrier usage
mempool_alloc/free() use undocumented smp_mb()'s.  The code is slightly
broken and misleading.

The lockless part is in mempool_free().  It wants to determine whether the
item being freed needs to be returned to the pool or backing allocator
without grabbing pool->lock.  Two things need to be guaranteed for correct
operation.

1. pool->curr_nr + #allocated should never dip below pool->min_nr.
2. Waiters shouldn't be left dangling.

For #1, The only necessary condition is that curr_nr visible at free is
from after the allocation of the element being freed (details in the
comment).  For most cases, this is true without any barrier but there can
be fringe cases where the allocated pointer is passed to the freeing task
without going through memory barriers.  To cover this case, wmb is
necessary before returning from allocation and rmb is necessary before
reading curr_nr.  IOW,

	ALLOCATING TASK			FREEING TASK

	update pool state after alloc;
	wmb();
	pass pointer to freeing task;
					read pointer;
					rmb();
					read pool state to free;

The current code doesn't have wmb after pool update during allocation and
may theoretically, on machines where unlock doesn't behave as full wmb,
lead to pool depletion and deadlock.  smp_wmb() needs to be added after
successful allocation from reserved elements and smp_mb() in
mempool_free() can be replaced with smp_rmb().

For #2, the waiter needs to add itself to waitqueue and then check the
wait condition and the waker needs to update the wait condition and then
wake up.  Because waitqueue operations always go through full spinlock
synchronization, there is no need for extra memory barriers.

Furthermore, mempool_alloc() is already holding pool->lock when it decides
that it needs to wait.  There is no reason to do unlock - add waitqueue -
test condition again.  It can simply add itself to waitqueue while holding
pool->lock and then unlock and sleep.

This patch adds smp_wmb() after successful allocation from reserved pool,
replaces smp_mb() in mempool_free() with smp_rmb() and extend pool->lock
over waitqueue addition.  More importantly, it explains what memory
barriers do and how the lockless testing is correct.

-v2: Oleg pointed out that unlock doesn't imply wmb.  Added explicit
     smp_wmb() after successful allocation from reserved pool and
     updated comments accordingly.

Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: David Howells <dhowells@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-01-10 16:30:45 -08:00
..
backing-dev.c
bootmem.c mm: bootmem: try harder to free pages in bulk 2012-01-10 16:30:45 -08:00
bounce.c
cleancache.c
compaction.c mm: compaction: push isolate search base of compact control one pfn ahead 2012-01-10 16:30:44 -08:00
debug-pagealloc.c
dmapool.c
fadvise.c fadvise: only initiate writeback for specified range with FADV_DONTNEED 2012-01-10 16:30:43 -08:00
failslab.c
filemap_xip.c
filemap.c mm: filemap: pass __GFP_WRITE from grab_cache_page_write_begin() 2012-01-10 16:30:43 -08:00
fremap.c
highmem.c
huge_memory.c
hugetlb.c mm: hugetlb: fix pgoff computation when unmapping page from vma 2012-01-10 16:30:45 -08:00
hwpoison-inject.c
init-mm.c
internal.h
Kconfig
Kconfig.debug mm: more intensive memory corruption debugging 2012-01-10 16:30:42 -08:00
kmemcheck.c
kmemleak-test.c
kmemleak.c
ksm.c
maccess.c
madvise.c
Makefile
memblock.c
memcontrol.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2012-01-09 14:46:52 -08:00
memory_hotplug.c
memory-failure.c
memory.c
mempolicy.c mm/mempolicy.c: mpol_equal(): use bool 2012-01-10 16:30:45 -08:00
mempool.c mempool: fix and document synchronization and memory barrier usage 2012-01-10 16:30:45 -08:00
migrate.c mm/migrate.c: cleanup comment for migration_entry_wait() 2012-01-10 16:30:45 -08:00
mincore.c
mlock.c
mm_init.c
mmap.c mm: simplify find_vma_prev() 2012-01-10 16:30:44 -08:00
mmu_context.c
mmu_notifier.c
mmzone.c
mprotect.c
mremap.c mremap: enforce rmap src/dst vma ordering in case of vma_merge() succeeding in copy_vma() 2012-01-10 16:30:44 -08:00
msync.c
nobootmem.c
nommu.c
oom_kill.c tracepoint: add tracepoints for debugging oom_score_adj 2012-01-10 16:30:44 -08:00
page_alloc.c mm: page_alloc: generalize order handling in __free_pages_bootmem() 2012-01-10 16:30:44 -08:00
page_cgroup.c
page_io.c
page_isolation.c
page-writeback.c mm: try to distribute dirty pages fairly across zones 2012-01-10 16:30:43 -08:00
pagewalk.c
percpu-km.c
percpu-vm.c
percpu.c
pgtable-generic.c
prio_tree.c
process_vm_access.c
quicklist.c
readahead.c
rmap.c mremap: enforce rmap src/dst vma ordering in case of vma_merge() succeeding in copy_vma() 2012-01-10 16:30:44 -08:00
shmem.c
slab.c tracing/mm: Move include of trace/events/kmem.h out of header into slab.c 2012-01-09 14:19:33 -08:00
slob.c
slub.c slub: min order when debug_guardpage_minorder > 0 2012-01-10 16:30:43 -08:00
sparse-vmemmap.c
sparse.c
swap_state.c
swap.c mm: add free_hot_cold_page_list() helper 2012-01-10 16:30:41 -08:00
swapfile.c mm: avoid livelock on !__GFP_FS allocations 2012-01-10 16:30:42 -08:00
thrash.c
truncate.c
util.c
vmalloc.c
vmscan.c mm/vmscan.c: consider swap space when deciding whether to continue reclaim 2012-01-10 16:30:45 -08:00
vmstat.c