From patchwork Tue Mar 6 19:05:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 130830 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp4295510lja; Tue, 6 Mar 2018 11:05:11 -0800 (PST) X-Google-Smtp-Source: AG47ELtTO4Sf4xi01PvhVSrtSfgAGFW6iYtjJkjrjq/Kc/G1cX3hg8jEA98cR/C9Ys2eLS+7ii6B X-Received: by 10.98.232.6 with SMTP id c6mr19715979pfi.242.1520363110885; Tue, 06 Mar 2018 11:05:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520363110; cv=none; d=google.com; s=arc-20160816; b=M4lZsAuyMQxSlzcyEATxTjae8Qn/gZOhsSkl1g9VXdnYXMPO9K1eIRFWgobZ+B0wy1 p5tnliI5bDb0LdsDTbXE+y4H2mdZuf7HSIvW0+dzYtzhKd8kN1TiFqg/lRmbLOL/O2iz UzypEwoqirTmp+SerGqF8MMbczexA6daWxRcPNb6xkOvsejx7o5yfkcUQ8I6CTJn2B83 dDov9IOckKUVslvJWYaUkITyqO3bHqSgESaaqEmCKosWAq4EEEKl68Swteu7umw7FNXR KTwuPvKiW7pg9QZ0/49fA1gX8TPf3Yv5FhC7s8OytMh/78NC8zLYXJqq93KiTYEqEqHV wdLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=Xts7ebPL04mtUoh4JMHxdtFpRuJhoVp3OIau7gyvM5Y=; b=erwXuA8zATQKKbNJHfjljYSCgkqozgJe61qmLcCl6DvIReciu8EmcwVspy1S8rezhQ 8UKbUvgBMPt7FFNaPuu3P7R73XFllBFnkZsQe7j5Mq8mBqTpoAMO89VUrPET5AjNAJTB Dkf5P15WczyCS7+kXKVGbNAsK7Grrllw0nw8cyzazLUgk2SG+8liAh+Nm669nNjUqD7G B0TQ5HDhe2XEHPXfEVS3CDadHv1n/8lioe3moJuLXib1cJBq2mHw9DEg8hKwxODTByaA ngVCg+1WnL/YU7TGTMlpvQEm/2Y02rMkzX/U63zD9HL8qJgsY13QcEgWEPjLPlI50rOp IGlQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d9si8232931pga.789.2018.03.06.11.05.10; Tue, 06 Mar 2018 11:05:10 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933136AbeCFTFH (ORCPT + 28 others); Tue, 6 Mar 2018 14:05:07 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:43120 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753308AbeCFTFE (ORCPT ); Tue, 6 Mar 2018 14:05:04 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AE0FE1596; Tue, 6 Mar 2018 11:05:03 -0800 (PST) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 802843F58C; Tue, 6 Mar 2018 11:05:03 -0800 (PST) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id B8A341AE0E8F; Tue, 6 Mar 2018 19:05:07 +0000 (GMT) From: Will Deacon To: linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk, torvalds@linux-foundation.org Cc: peterz@infradead.org, willy@infradead.org, linux-kernel@vger.kernel.org, Will Deacon Subject: [RESEND PATCHv2 1/2] fs: dcache: Avoid livelock between d_alloc_parallel and __d_add Date: Tue, 6 Mar 2018 19:05:05 +0000 Message-Id: <1520363106-4687-2-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1520363106-4687-1-git-send-email-will.deacon@arm.com> References: <1520363106-4687-1-git-send-email-will.deacon@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If d_alloc_parallel runs concurrently with __d_add, it is possible for d_alloc_parallel to continuously retry whilst i_dir_seq has been incremented to an odd value by __d_add: CPU0: __d_add n = start_dir_add(dir); cmpxchg(&dir->i_dir_seq, n, n + 1) == n CPU1: d_alloc_parallel retry: seq = smp_load_acquire(&parent->d_inode->i_dir_seq) & ~1; hlist_bl_lock(b); bit_spin_lock(0, (unsigned long *)b); // Always succeeds CPU0: __d_lookup_done(dentry) hlist_bl_lock bit_spin_lock(0, (unsigned long *)b); // Never succeeds CPU1: if (unlikely(parent->d_inode->i_dir_seq != seq)) { hlist_bl_unlock(b); goto retry; } Since the simple bit_spin_lock used to implement hlist_bl_lock does not provide any fairness guarantees, then CPU1 can starve CPU0 of the lock and prevent it from reaching end_dir_add(dir), therefore CPU1 cannot exit its retry loop because the sequence number always has the bottom bit set. This patch resolves the livelock by not taking hlist_bl_lock in d_alloc_parallel if the sequence counter is odd, since any subsequent masked comparison with i_dir_seq will fail anyway. Cc: Peter Zijlstra Cc: Al Viro Reported-by: Naresh Madhusudana Acked-by: Peter Zijlstra (Intel) Reviewed-by: Matthew Wilcox Signed-off-by: Will Deacon --- fs/dcache.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) -- 2.1.4 diff --git a/fs/dcache.c b/fs/dcache.c index 7c38f39958bc..b243deec298c 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -2474,7 +2474,7 @@ struct dentry *d_alloc_parallel(struct dentry *parent, retry: rcu_read_lock(); - seq = smp_load_acquire(&parent->d_inode->i_dir_seq) & ~1; + seq = smp_load_acquire(&parent->d_inode->i_dir_seq); r_seq = read_seqbegin(&rename_lock); dentry = __d_lookup_rcu(parent, name, &d_seq); if (unlikely(dentry)) { @@ -2495,6 +2495,12 @@ struct dentry *d_alloc_parallel(struct dentry *parent, rcu_read_unlock(); goto retry; } + + if (unlikely(seq & 1)) { + rcu_read_unlock(); + goto retry; + } + hlist_bl_lock(b); if (unlikely(parent->d_inode->i_dir_seq != seq)) { hlist_bl_unlock(b); From patchwork Tue Mar 6 19:05:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 130832 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp4295911lja; Tue, 6 Mar 2018 11:05:36 -0800 (PST) X-Google-Smtp-Source: AG47ELsXpU3U7ea4U3GupU8VzoyvAS/bmKa0aF4zbCL+s3ALLFB6cngWV4eSnKs7lP6eF+v+IpvE X-Received: by 2002:a17:902:850a:: with SMTP id bj10-v6mr18030001plb.5.1520363136160; Tue, 06 Mar 2018 11:05:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520363136; cv=none; d=google.com; s=arc-20160816; b=oRWWNZwwUvUkRu5pLnaJfk9j+EFFBke53STtxUDIGwBra/b6GQ1DQ8/brhtHVwRATX uHb5yiu82632ys/4qkj8O5c4+6CumYMh+g/QoheQXmEgvIVg7hoQ+g0qn6Ad0JRwjPMp bFFNt+v7v4ZgFHYak/Fe1OytFLWsBkOpRVyqAsTggjbQqThcozPASwLcaEXPhcW71Ahb aWEJYEw4fMwvx2XalQ91ESXKZdGx+OzReUrGAdvDBNHBxzGUEeIjeip39ArR4k/CoNxT KOkp+694U41gg8uEcNwSjk1nAQb+qG1pTFlGH2GBmJ2A9ZNyn5bjOse38mOiIpgSGb9N 42cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=a1V6BwQO4moREB4V8DdZsoaaYJ3c4dnM7bhD6UB+FKQ=; b=jOArirjMjioWDXEcJLWZIIE/unFNDA+4JeRhTlKad3nVIeEMcm1RWMowIJOwi96ItY 57Ffe1WIWKSVrq/iCckJtCfaZSXCAuq3ZlHrLoGY8GFKkxkJXleh/pxbJS4KXylNXiNl 5bo5JoN1hsK3hK/yreu7r6RU/h2P0EXEpxhNSH/1YV2wMToYc0++tAxSeeqcgndCurWc mh4DV5rz8ho1XatHLnvPfd8BvdzMuEZxyMqqaEO14VaOgGbAVUipwEhIyOQ8pbEkxHKs w/sytAPIsp4FxW9dRJBH0sweSWw6gl5nrPmVeo54n0BNx6/nw84w1b+yx2foj7tM3k4y etyg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k15si12335584pfi.174.2018.03.06.11.05.35; Tue, 06 Mar 2018 11:05:36 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754043AbeCFTFF (ORCPT + 28 others); Tue, 6 Mar 2018 14:05:05 -0500 Received: from foss.arm.com ([217.140.101.70]:43130 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750838AbeCFTFE (ORCPT ); Tue, 6 Mar 2018 14:05:04 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BB6C415BE; Tue, 6 Mar 2018 11:05:03 -0800 (PST) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8D6563F5C9; Tue, 6 Mar 2018 11:05:03 -0800 (PST) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id C8EA91AE536F; Tue, 6 Mar 2018 19:05:07 +0000 (GMT) From: Will Deacon To: linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk, torvalds@linux-foundation.org Cc: peterz@infradead.org, willy@infradead.org, linux-kernel@vger.kernel.org, Will Deacon Subject: [RESEND PATCHv2 2/2] fs: dcache: Use READ_ONCE when accessing i_dir_seq Date: Tue, 6 Mar 2018 19:05:06 +0000 Message-Id: <1520363106-4687-3-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1520363106-4687-1-git-send-email-will.deacon@arm.com> References: <1520363106-4687-1-git-send-email-will.deacon@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org i_dir_seq is subject to concurrent modification by a cmpxchg or store-release operation, so ensure that the relaxed access in d_alloc_parallel uses READ_ONCE. Reported-by: Peter Zijlstra Signed-off-by: Will Deacon --- fs/dcache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.1.4 diff --git a/fs/dcache.c b/fs/dcache.c index b243deec298c..5716ab04e1db 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -2502,7 +2502,7 @@ struct dentry *d_alloc_parallel(struct dentry *parent, } hlist_bl_lock(b); - if (unlikely(parent->d_inode->i_dir_seq != seq)) { + if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) { hlist_bl_unlock(b); rcu_read_unlock(); goto retry;