Received: by 10.223.176.5 with SMTP id f5csp67500wra; Mon, 5 Feb 2018 16:50:33 -0800 (PST) X-Google-Smtp-Source: AH8x227qC7lbpQXE12H2Ut+wxdGI/b3mGJ2eoaXGZYWYiCXb/SX1ky7EnPL8DP1XXvv0SHr0fHq9 X-Received: by 2002:a17:902:8501:: with SMTP id bj1-v6mr613019plb.5.1517878233460; Mon, 05 Feb 2018 16:50:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517878233; cv=none; d=google.com; s=arc-20160816; b=YbnWrMsz49g2ex0dES5Mw6JPqG+TpeEj6VnJ3+beyOBquw2DVnA759bNedtbb1xcx/ rEirnNJL0LFQ0IQ0G2zWhQYv0nFMb7AMU/s26HJklCiLLKzMwdYOa2Uv9QZ3+wN+L5mb OISpuFvxb35gkAM0BePZL6JZRV6s8ZitVQAfOZoURxvRgA+3pqCiJdiliwSgJwdsIipg ZtFmGAW9GdXUAk6q+KKGUYfHupeDXVPJnp25o38o2zuwJDvnpf4XSYuL/8ZL/SpE9I8c /kbokgRZFNaZNnrsSRLdSmx/OL178x2EO4d98RGOC/bLHg8uezXksxhHjp/ZJ2XYgz8P FL+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=/wqFZQnNfN6UhvHSxQ6OXX5rv9JJm7tiKFCN/SWGElo=; b=khkKGNm8q0qK+909BAKfJg31bICjGAUuWYcRHpRDnp0YbcdhLzVizuk2EqpY98ehCq 2MHDm2YzFnmfo89RdrOMy4mHHiSIo5bQd03fkbANh43ZwMNFZMbPQG0aBdSK22jhxQG9 LBIf+YxRbsJclykgG1ZvWKAn4R/yqvyJ9dKSD58J0en1GzVq+M9hP47Y6qZwdWmcbH2n xtp3DcvAQ91ejWHJFKyUxzU2+cSj5Zitf316GCMvauQe6mBSv9dC+5TbOH7FoI3clSs2 tahr9C3T7jfMECc9xDHWje2CareTvH9o5ZMzk6bkAZdHlbLKpaQkdaf/0GnQDmUl0jyY zoWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ol+N8qQe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c3-v6si1191521pld.174.2018.02.05.16.50.19; Mon, 05 Feb 2018 16:50:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ol+N8qQe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752141AbeBFAt3 (ORCPT + 99 others); Mon, 5 Feb 2018 19:49:29 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:38868 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751514AbeBFAtV (ORCPT ); Mon, 5 Feb 2018 19:49:21 -0500 Received: by mail-pg0-f66.google.com with SMTP id l18so222918pgc.5 for ; Mon, 05 Feb 2018 16:49:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=/wqFZQnNfN6UhvHSxQ6OXX5rv9JJm7tiKFCN/SWGElo=; b=Ol+N8qQeALt3b2Iyc6OgAA11JBFqiPWdJmDA3wWbAhJTn+RVvDSBvObvFDhnDtOyRP FLnkTCBzWv44y3elfRCDYFeS4pUdTp+e2lT6uTZJL1LtL+p0LstncXYFJtIGN+rD/xrd sobqkCHbTaKhox70tlBA0FMARBSEuDvIWQaycoXNMezDKmN7vg2eaFHK8C7mOvVHNqK0 O4cuZxng7h0iJDSTzpkcGkddA5Z2NByoAejhzS6KoZB93U0F7KKLwWgwiOrv72VM/tWX IcFuJmJlb3E3o8EyWKeuzmqOtDnFVORfyI7qJuTEbZOhbyMHdv9Us5nlme0xHpQpVJ9v VYXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=/wqFZQnNfN6UhvHSxQ6OXX5rv9JJm7tiKFCN/SWGElo=; b=MswFlxCzDv2D4nc/PF8F1rNvYaZ1J+ognVTDA3fdMTkq967PgNTmQMPH48olNZAley RnPUQ909I8QXJ4OPxe7b4P/vn26WmL42nWUhuh3fevSSZXxSJKZpx3s/CvTZzMX88boC 8NuE+BwHpzjpiWGOaSYdAIxuuPLlvU5y8yuFUrRXgD1ZMsjw8SBs1dzxXUzb2KDg/R6f cuNGPLSEgeaNB+jnloD0DHxcbo3Ild7vrUSMyr3xvHtVG5rvWnUVPAacIfGSAFq1Vycp TdE/d0V/yMsHBJOI/lSJ406LoZfDBowRrYwI1t4kS3BUPeGBayZYhKzz9aDw3dPjTNWs SEHQ== X-Gm-Message-State: APf1xPAYjDGqrYntq3ZgC11ShFPEW1ZPJBd6IrOKwkqvgOP/3EX2BGcH rbsX0HrHoJHwPUpQmPmLHySWbefVSxI= X-Received: by 10.98.73.70 with SMTP id w67mr596170pfa.61.1517878160219; Mon, 05 Feb 2018 16:49:20 -0800 (PST) Received: from joelaf.mtv.corp.google.com ([172.22.121.121]) by smtp.gmail.com with ESMTPSA id i14sm14798698pgv.40.2018.02.05.16.49.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Feb 2018 16:49:19 -0800 (PST) From: Joel Fernandes To: linux-kernel@vger.kernel.org Cc: Joel Fernandes , Peter Zilstra , mhocko@kernel.org, minchan@kernel.org, linux-mm@kvack.org Subject: [PATCH RFC] ashmem: Fix lockdep RECLAIM_FS false positive Date: Mon, 5 Feb 2018 16:49:03 -0800 Message-Id: <20180206004903.224390-1-joelaf@google.com> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org During invocation of ashmem shrinker under memory pressure, ashmem calls into VFS code via vfs_fallocate. We however make sure we don't enter it if the allocation was GFP_FS to prevent looping into filesystem code. However lockdep doesn't know this and prints a lockdep splat as below. This patch fixes the issue by releasing the reclaim_fs lock after checking for GFP_FS but before calling into the VFS path, and reacquiring it after so that lockdep can continue reporting any reclaim issues later. [ 2115.359650] -(1)[106:kswapd0]================================= [ 2115.359665] -(1)[106:kswapd0][ INFO: inconsistent lock state ] [ 2115.359684] -(1)[106:kswapd0]4.9.60+ #2 Tainted: G W O [ 2115.359699] -(1)[106:kswapd0]--------------------------------- [ 2115.359715] -(1)[106:kswapd0]inconsistent {RECLAIM_FS-ON-W} -> {IN-RECLAIM_FS-W} usage. [ 2115.359732] -(1)[106:kswapd0]kswapd0/106 [HC0[0]:SC0[0]:HE1:SE1] takes: [ 2115.359748] (&sb->s_type->i_mutex_key#9){++++?+}, at: [] shmem_fallocate+0x104/0x880 [ 2115.359809] -(1)[106:kswapd0]{RECLAIM_FS-ON-W} state was registered at: [ 2115.359828] -(1)[106:kswapd0] mark_lock+0x2a4/0x10c0 [ 2115.359845] -(1)[106:kswapd0] mark_held_locks+0xc0/0x128 [ 2115.359862] -(1)[106:kswapd0] lockdep_trace_alloc+0x284/0x368 [ 2115.359881] -(1)[106:kswapd0] kmem_cache_alloc+0x3c/0x368 [ 2115.359900] -(1)[106:kswapd0] __d_alloc+0x3c/0x7a8 [ 2115.359917] -(1)[106:kswapd0] d_alloc+0x34/0x140 [ 2115.359934] -(1)[106:kswapd0] d_alloc_parallel+0xfc/0x1480 [ 2115.359953] -(1)[106:kswapd0] lookup_open+0x3c4/0x12a8 [ 2115.359971] -(1)[106:kswapd0] path_openat+0xb40/0x1dc0 [ 2115.359987] -(1)[106:kswapd0] do_filp_open+0x170/0x258 [ 2115.360006] -(1)[106:kswapd0] do_sys_open+0x1b8/0x2f0 [ 2115.360023] -(1)[106:kswapd0] SyS_openat+0x10/0x18 [ 2115.360041] -(1)[106:kswapd0] el0_svc_naked+0x24/0x28 [ 2115.360056] -(1)[106:kswapd0]irq event stamp: 2437365 [ 2115.360079] -(1)[106:kswapd0]hardirqs last enabled at (2437365): [] mutex_trylock+0x224/0x460 [ 2115.360098] -(1)[106:kswapd0]hardirqs last disabled at (2437364): [] mutex_trylock+0xe8/0x460 [ 2115.360116] -(1)[106:kswapd0]softirqs last enabled at (2436534): [] __do_softirq+0xc38/0x1190 [ 2115.360138] -(1)[106:kswapd0]softirqs last disabled at (2436515): [] irq_exit+0x1ac/0x228 [ 2115.360153] -(1)[106:kswapd0]\x0aother info that might help us debug this: [ 2115.360169] -(1)[106:kswapd0] Possible unsafe locking scenario:\x0a [ 2115.360184] -(1)[106:kswapd0] CPU0 [ 2115.360198] -(1)[106:kswapd0] ---- [ 2115.360211] -(1)[106:kswapd0] lock(&sb->s_type->i_mutex_key#9); [ 2115.360252] -(1)[106:kswapd0] [ 2115.360265] -(1)[106:kswapd0] lock(&sb->s_type->i_mutex_key#9); [ 2115.360304] -(1)[106:kswapd0]\x0a *** DEADLOCK ***\x0a [ 2115.360322] -(1)[106:kswapd0] #0: (shrinker_rwsem){++++..}, at: [] shrink_slab.part.15.constprop.28+0xb0/0xe10 [ 2115.360382] -(1)[106:kswapd0] #1: (ashmem_mutex){+.+.+.}, at: [] ashmem_shrink_scan+0x80/0x308 [ 2115.360439] -(1)[106:kswapd0]\x0astack backtrace: [ 2115.360462] -(1)[106:kswapd0]CPU: 1 PID: 106 Comm: kswapd0 Tainted: G W O 4.9.60+ #2 [ 2115.360478] -(1)[106:kswapd0]Hardware name: MT6765 (DT) [ 2115.360494] -(1)[106:kswapd0]Call trace: [ 2115.360515] -(1)[106:kswapd0][] dump_backtrace+0x0/0x400 [ 2115.360533] -(1)[106:kswapd0][] show_stack+0x14/0x20 [ 2115.360555] -(1)[106:kswapd0][] dump_stack+0xb0/0xe8 [ 2115.360576] -(1)[106:kswapd0][] print_usage_bug.part.24+0x548/0x568 [ 2115.360595] -(1)[106:kswapd0][] mark_lock+0x494/0x10c0 [ 2115.360613] -(1)[106:kswapd0][] __lock_acquire+0xc94/0x58e8 [ 2115.360630] -(1)[106:kswapd0][] lock_acquire+0x1d0/0x708 [ 2115.360650] -(1)[106:kswapd0][] down_write+0x48/0xd0 [ 2115.360669] -(1)[106:kswapd0][] shmem_fallocate+0x104/0x880 [ 2115.360688] -(1)[106:kswapd0][] ashmem_shrink_scan+0x1a4/0x308 [ 2115.360709] -(1)[106:kswapd0][] shrink_slab.part.15.constprop.28+0x330/0xe10 [ 2115.360729] -(1)[106:kswapd0][] shrink_node+0x1b4/0x588 [ 2115.360747] -(1)[106:kswapd0][] kswapd+0x774/0x1640 [ 2115.360767] -(1)[106:kswapd0][] kthread+0x28c/0x310 [ 2115.360786] -(1)[106:kswapd0][] ret_from_fork+0x10/0x50 Cc: Peter Zilstra Cc: mhocko@kernel.org Cc: minchan@kernel.org Cc: linux-mm@kvack.org Signed-off-by: Joel Fernandes --- drivers/staging/android/ashmem.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/staging/android/ashmem.c b/drivers/staging/android/ashmem.c index 372ce9913e6d..7e060f32aaa8 100644 --- a/drivers/staging/android/ashmem.c +++ b/drivers/staging/android/ashmem.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "ashmem.h" #define ASHMEM_NAME_PREFIX "dev/ashmem/" @@ -446,8 +447,17 @@ ashmem_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) if (!(sc->gfp_mask & __GFP_FS)) return SHRINK_STOP; - if (!mutex_trylock(&ashmem_mutex)) + /* + * Release reclaim-fs marking since we've already checked GFP_FS, This + * will prevent lockdep's reclaim recursion deadlock false positives. + * We'll renable it before returning from this function. + */ + fs_reclaim_release(sc->gfp_mask); + + if (!mutex_trylock(&ashmem_mutex)) { + fs_reclaim_acquire(sc->gfp_mask); return -1; + } list_for_each_entry_safe(range, next, &ashmem_lru_list, lru) { loff_t start = range->pgstart * PAGE_SIZE; @@ -464,6 +474,8 @@ ashmem_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) break; } mutex_unlock(&ashmem_mutex); + + fs_reclaim_acquire(sc->gfp_mask); return freed; } -- 2.16.0.rc1.238.g530d649a79-goog