# git rev-parse -q --verify ea07b862ac8ef9b8c8358517d2e39f847dda6659^{commit} ea07b862ac8ef9b8c8358517d2e39f847dda6659 already have revision, skipping fetch # git checkout -q -f -B kisskb ea07b862ac8ef9b8c8358517d2e39f847dda6659 # git clean -qxdf # git log -1 commit ea07b862ac8ef9b8c8358517d2e39f847dda6659 Author: Johannes Weiner Date: Fri Jan 6 19:21:43 2017 -0500 mm: workingset: fix use-after-free in shadow node shrinker Several people report seeing warnings about inconsistent radix tree nodes followed by crashes in the workingset code, which all looked like use-after-free access from the shadow node shrinker. Dave Jones managed to reproduce the issue with a debug patch applied, which confirmed that the radix tree shrinking indeed frees shadow nodes while they are still linked to the shadow LRU: WARNING: CPU: 2 PID: 53 at lib/radix-tree.c:643 delete_node+0x1e4/0x200 CPU: 2 PID: 53 Comm: kswapd0 Not tainted 4.10.0-rc2-think+ #3 Call Trace: delete_node+0x1e4/0x200 __radix_tree_delete_node+0xd/0x10 shadow_lru_isolate+0xe6/0x220 __list_lru_walk_one.isra.4+0x9b/0x190 list_lru_walk_one+0x23/0x30 scan_shadow_nodes+0x2e/0x40 shrink_slab.part.44+0x23d/0x5d0 shrink_node+0x22c/0x330 kswapd+0x392/0x8f0 This is the WARN_ON_ONCE(!list_empty(&node->private_list)) placed in the inlined radix_tree_shrink(). The problem is with 14b468791fa9 ("mm: workingset: move shadow entry tracking to radix tree exceptional tracking"), which passes an update callback into the radix tree to link and unlink shadow leaf nodes when tree entries change, but forgot to pass the callback when reclaiming a shadow node. While the reclaimed shadow node itself is unlinked by the shrinker, its deletion from the tree can cause the left-most leaf node in the tree to be shrunk. If that happens to be a shadow node as well, we don't unlink it from the LRU as we should. Consider this tree, where the s are shadow entries: root->rnode | [0 n] | | [s ] [sssss] Now the shadow node shrinker reclaims the rightmost leaf node through the shadow node LRU: root->rnode | [0 ] | [s ] Because the parent of the deleted node is the first level below the root and has only one child in the left-most slot, the intermediate level is shrunk and the node containing the single shadow is put in its place: root->rnode | [s ] The shrinker again sees a single left-most slot in a first level node and thus decides to store the shadow in root->rnode directly and free the node - which is a leaf node on the shadow node LRU. root->rnode | s Without the update callback, the freed node remains on the shadow LRU, where it causes later shrinker runs to crash. Pass the node updater callback into __radix_tree_delete_node() in case the deletion causes the left-most branch in the tree to collapse too. Also add warnings when linked nodes are freed right away, rather than wait for the use-after-free when the list is scanned much later. Fixes: 14b468791fa9 ("mm: workingset: move shadow entry tracking to radix tree exceptional tracking") Reported-by: Dave Chinner Reported-by: Hugh Dickins Reported-by: Andrea Arcangeli Reported-and-tested-by: Dave Jones Signed-off-by: Johannes Weiner Cc: Christoph Hellwig Cc: Chris Leech Cc: Lee Duncan Cc: Jan Kara Cc: Kirill A. Shutemov Cc: Matthew Wilcox Cc: Andrew Morton Signed-off-by: Linus Torvalds # < /opt/cross/kisskb/gcc-4.6.3-nolibc/um-x86_64-linux/bin/x86_64-linux-gcc --version # < git log --format=%s --max-count=1 ea07b862ac8ef9b8c8358517d2e39f847dda6659 # < make -j 48 ARCH=um O=/home/kisskb/slave/build/linus_um-defconfig_um-x86_64 CROSS_COMPILE=/opt/cross/kisskb/gcc-4.6.3-nolibc/um-x86_64-linux/bin/x86_64-linux- SUBARCH=x86_64 defconfig kernel/time/Kconfig:155:warning: range is invalid # make -j 48 ARCH=um O=/home/kisskb/slave/build/linus_um-defconfig_um-x86_64 CROSS_COMPILE=/opt/cross/kisskb/gcc-4.6.3-nolibc/um-x86_64-linux/bin/x86_64-linux- SUBARCH=x86_64 make[1]: Entering directory '/home/kisskb/slave/build/linus_um-defconfig_um-x86_64' GEN ./Makefile scripts/kconfig/conf --silentoldconfig arch/x86/um/Kconfig kernel/time/Kconfig:155:warning: range is invalid CHK include/generated/uapi/linux/version.h UPD include/generated/uapi/linux/version.h HOSTCC scripts/basic/bin2c WRAP arch/um/include/generated/asm/barrier.h WRAP arch/um/include/generated/asm/bug.h WRAP arch/um/include/generated/asm/clkdev.h WRAP arch/um/include/generated/asm/cputime.h WRAP arch/um/include/generated/asm/current.h WRAP arch/um/include/generated/asm/delay.h WRAP arch/um/include/generated/asm/device.h WRAP arch/um/include/generated/asm/emergency-restart.h WRAP arch/um/include/generated/asm/exec.h WRAP arch/um/include/generated/asm/ftrace.h WRAP arch/um/include/generated/asm/futex.h WRAP arch/um/include/generated/asm/hardirq.h WRAP arch/um/include/generated/asm/hw_irq.h WRAP arch/um/include/generated/asm/io.h WRAP arch/um/include/generated/asm/irq_regs.h WRAP arch/um/include/generated/asm/irq_work.h WRAP arch/um/include/generated/asm/kdebug.h WRAP arch/um/include/generated/asm/mcs_spinlock.h WRAP arch/um/include/generated/asm/mm-arch-hooks.h WRAP arch/um/include/generated/asm/param.h WRAP arch/um/include/generated/asm/pci.h WRAP arch/um/include/generated/asm/percpu.h WRAP arch/um/include/generated/asm/preempt.h WRAP arch/um/include/generated/asm/switch_to.h WRAP arch/um/include/generated/asm/topology.h WRAP arch/um/include/generated/asm/trace_clock.h WRAP arch/um/include/generated/asm/word-at-a-time.h WRAP arch/um/include/generated/asm/xor.h CHK include/config/kernel.release GEN ./Makefile HOSTCC scripts/kallsyms CC scripts/mod/empty.o HOSTCC scripts/mod/mk_elfconfig CC scripts/mod/devicetable-offsets.s GEN scripts/mod/devicetable-offsets.h UPD include/config/kernel.release MKELF scripts/mod/elfconfig.h HOSTCC scripts/mod/modpost.o HOSTCC scripts/mod/file2alias.o HOSTCC scripts/mod/sumversion.o SYSTBL arch/x86/entry/syscalls/../../include/generated/asm/syscalls_32.h SYSHDR arch/x86/entry/syscalls/../../include/generated/asm/unistd_32_ia32.h SYSHDR arch/x86/entry/syscalls/../../include/generated/asm/unistd_64_x32.h SYSTBL arch/x86/entry/syscalls/../../include/generated/asm/syscalls_64.h SYSHDR arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_32.h SYSHDR arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_64.h SYSHDR arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_x32.h HOSTLD scripts/mod/modpost Using /home/kisskb/slave/src as source for kernel CHK include/generated/utsrelease.h UPD include/generated/utsrelease.h HOSTCC scripts/unifdef CC arch/x86/um/user-offsets.s In file included from /usr/include/features.h:374:0, from /usr/include/stdio.h:27, from /home/kisskb/slave/src/arch/x86/um/user-offsets.c:1: /opt/cross/gcc-4.6.3-nolibc/x86_64-linux/lib/gcc/x86_64-linux/4.6.3/../../../../x86_64-linux/include/sys/cdefs.h:385:27: fatal error: bits/wordsize.h: No such file or directory compilation terminated. /home/kisskb/slave/src/scripts/Makefile.build:154: recipe for target 'arch/x86/um/user-offsets.s' failed make[2]: *** [arch/x86/um/user-offsets.s] Error 1 arch/um/Makefile:150: recipe for target 'arch/x86/um/user-offsets.s' failed make[1]: *** [arch/x86/um/user-offsets.s] Error 2 make[1]: Leaving directory '/home/kisskb/slave/build/linus_um-defconfig_um-x86_64' Makefile:150: recipe for target 'sub-make' failed make: *** [sub-make] Error 2 Command 'make -j 48 ARCH=um O=/home/kisskb/slave/build/linus_um-defconfig_um-x86_64 CROSS_COMPILE=/opt/cross/kisskb/gcc-4.6.3-nolibc/um-x86_64-linux/bin/x86_64-linux- SUBARCH=x86_64 ' returned non-zero exit status 2 # rm -rf /home/kisskb/slave/build/linus_um-defconfig_um-x86_64 # Build took: 0:00:04.958949