Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2341476imm; Mon, 28 May 2018 06:22:57 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpbwJs66lAvqKyaMKM0MYKll1WcEpMA3yxKE5wE4jytJsSRumeP10zQrvVTkOcAVAImkHf5 X-Received: by 2002:a17:902:b789:: with SMTP id e9-v6mr13672621pls.102.1527513776966; Mon, 28 May 2018 06:22:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527513776; cv=none; d=google.com; s=arc-20160816; b=hLzQFJZBx8q4cjM0wtvPgh5NonLt8c9/LmKeSTWLw1RlbHov8ccnF0e0nmHussgqYo rXaFaOpeLJOZ/kBgWSHxJP1lVgbVluvQsDAnb/xeLxxL+jWPQ3zj4dSnlJT6C4OQz9eI GB+6UTzdEFxFUN4NTVPLCy4vLyqi9YXyuAC6W2vE57LI0F5M6W+JCy/KVjWqbXAMyz7X FN09Bu4ExnqHRpPj/9qkVyO7L0YzamvG9RFKt6Y2KFQrJLVJ60j1IoSCQUP9ojRI54JV 5eF/atq9KlMP8LCSfjgyq5SFcBulJCvOr4Zi0WC9vmxPsnrAYNeOs68aTRrl32ENjgo4 FJKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=zRrY2p54ZYiVkVW8e7B5XpnCbmt+ugdPJ4SVb+Zejqo=; b=y0/ADlckg2lM/qVcQpmeVk0pF62rBUYzXGD/WoDs7h8FbKsaJiZGgq38ZRn1S9CB46 t4C+SNIuHSGro0f0iOF5VD90wSnqJOE6dt0FrmB69W1Mb20etbA9TJCftlXuxfp9B63o nMFIly3p076e+2QarBEbUmPSNhqXBfNpLWI9VHVw9Jbh7FQmWDXrtTCDNuUbSOqtI9Zk tPf9IoN2k0+sKDf74oE4tEMJFCB7Dwxc/n5UdFBeGZ7qY4MDC3ZIG1Y08CIsm3Bb/xuQ d50Q64PJZjBAUz5Unp1MS8i2MIG1CIPsY0eMr5+azzn0LpWh791tBgoPVq2EBlgTPIDz LBhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AGMcDGwR; 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 e15-v6si23795563pgr.400.2018.05.28.06.22.42; Mon, 28 May 2018 06:22:56 -0700 (PDT) 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=@kernel.org header.s=default header.b=AGMcDGwR; 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 S1165330AbeE1NVF (ORCPT + 99 others); Mon, 28 May 2018 09:21:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:39000 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1163486AbeE1KtN (ORCPT ); Mon, 28 May 2018 06:49:13 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0717F204EE; Mon, 28 May 2018 10:49:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527504553; bh=V447FgYB09pi0cJLRufXEgTvVnaDMUSSLDOh6MwrZkY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AGMcDGwR753DyJXeg04EitctdsXe0sxZoLgBrR5xAIJfKIrUZHrzwF+im+HLhvDUF evwpKcH8022nSZWevImVtK4rNSwStRagiQoJ0biDnfm4hlh1ZHsT+rC5NM68X6hedT D9WFp9keWlaoahm9GPyrdZh8cWZJevGGieh9GlsA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Zijlstra , Al Viro , Naresh Madhusudana , Matthew Wilcox , Will Deacon , Sasha Levin Subject: [PATCH 4.14 138/496] fs: dcache: Avoid livelock between d_alloc_parallel and __d_add Date: Mon, 28 May 2018 11:58:43 +0200 Message-Id: <20180528100325.748441454@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100319.498712256@linuxfoundation.org> References: <20180528100319.498712256@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Will Deacon [ Upstream commit 015555fd4d2930bc0c86952c46ad88b3392f66e4 ] 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 Signed-off-by: Al Viro Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/dcache.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) --- a/fs/dcache.c +++ b/fs/dcache.c @@ -2482,7 +2482,7 @@ struct dentry *d_alloc_parallel(struct d 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)) { @@ -2503,6 +2503,12 @@ retry: 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);