Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1190214ybn; Wed, 2 Oct 2019 12:11:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqyxNjCOkoWnFg7Xm3Pk1OSeF5MTsEo5e0hb1rrzg1hCGHTWG2WD/hnyk6xWuz1ErXfXbyD6 X-Received: by 2002:a50:bb22:: with SMTP id y31mr5776531ede.58.1570043475930; Wed, 02 Oct 2019 12:11:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570043475; cv=none; d=google.com; s=arc-20160816; b=I9VEn8T0yFRGq6RbAxvWg0v7twk66q1y+usXYUbEl5QvowDEaXu/9501wGByRs9SkW pUw8hOc4Ez+6PfeNKqeye+22Rk17SYl7Z5EWzay+2JDAUYKkFzxJ9eCLtrIPxL/CAueo wUhlB3nf7S/b+Ntne5lyF4RzFDyDnRee5laYoWhT7lWcnpzKLI8755j6cfgDVdkFBaOl pmWv5x6UAzpqG1KpHKKNz0qW8U9YuJO8Xv+Wx2QyGpjroV3/ATcor9ZY7P4EZG64nc2c to3jb9+LCWrtvLiWgy+4uI6+3z3Klwpm7Nf31ioxb+SZDggqBIoPupesWW0OQrGhn+fx W4+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=jYCnEq8hEhftuJK69HwVuyVbPnJC4qoU8+TguzvT1ts=; b=iyV+I2PBPE4349IgFaFUP4Y3/363MdxxoV4tl5rH/EmKVAzWsb39739SKKY6lwsX3h YPmJXoGkMwqqbfNX9dDVbIjg0OtlB4tbEmDFr0qVcsKhmal/1UJdhxApwS1rI4Chf38Q M+cdw5GZp0qgXOgMxngFo/dm4Tto/6ibwruZnznI/pWcvvRiWbpXmb2eROKWOVUwRlFV PtyCGuYXcsH+UON9q90IgfGe6YFf58HlY3NDP3MPRzwcJaF/W+g5tq32KYS/FY6Kib6e cFOdO9XvsxsobokcqM3E++nHXL9Ht5blRNb5VGzsLKbKBGXA7iQsu3vOjFeOBD01Gvr5 /OBw== 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 l48si17164edc.276.2019.10.02.12.10.51; Wed, 02 Oct 2019 12:11:15 -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; 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 S1729621AbfJBTJ2 (ORCPT + 99 others); Wed, 2 Oct 2019 15:09:28 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:36066 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729364AbfJBTIT (ORCPT ); Wed, 2 Oct 2019 15:08:19 -0400 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iFjyx-00036K-Qc; Wed, 02 Oct 2019 20:08:15 +0100 Received: from ben by deadeye with local (Exim 4.92.1) (envelope-from ) id 1iFjyp-0003eZ-2W; Wed, 02 Oct 2019 20:08:07 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, Denis Kirjanov , "Joseph Qi" , "Changwei Ge" , "Joel Becker" , "Wengang Wang" , "Junxiao Bi" , "Mark Fasheh" , "Jun Piao" , "Daniel Sobe" , "Linus Torvalds" , "Gang He" Date: Wed, 02 Oct 2019 20:06:51 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 59/87] fs/ocfs2: fix race in ocfs2_dentry_attach_lock() In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.75-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Wengang Wang commit be99ca2716972a712cde46092c54dee5e6192bf8 upstream. ocfs2_dentry_attach_lock() can be executed in parallel threads against the same dentry. Make that race safe. The race is like this: thread A thread B (A1) enter ocfs2_dentry_attach_lock, seeing dentry->d_fsdata is NULL, and no alias found by ocfs2_find_local_alias, so kmalloc a new ocfs2_dentry_lock structure to local variable "dl", dl1 ..... (B1) enter ocfs2_dentry_attach_lock, seeing dentry->d_fsdata is NULL, and no alias found by ocfs2_find_local_alias so kmalloc a new ocfs2_dentry_lock structure to local variable "dl", dl2. ...... (A2) set dentry->d_fsdata with dl1, call ocfs2_dentry_lock() and increase dl1->dl_lockres.l_ro_holders to 1 on success. ...... (B2) set dentry->d_fsdata with dl2 call ocfs2_dentry_lock() and increase dl2->dl_lockres.l_ro_holders to 1 on success. ...... (A3) call ocfs2_dentry_unlock() and decrease dl2->dl_lockres.l_ro_holders to 0 on success. .... (B3) call ocfs2_dentry_unlock(), decreasing dl2->dl_lockres.l_ro_holders, but see it's zero now, panic Link: http://lkml.kernel.org/r/20190529174636.22364-1-wen.gang.wang@oracle.com Signed-off-by: Wengang Wang Reported-by: Daniel Sobe Tested-by: Daniel Sobe Reviewed-by: Changwei Ge Reviewed-by: Joseph Qi Cc: Mark Fasheh Cc: Joel Becker Cc: Junxiao Bi Cc: Gang He Cc: Jun Piao Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Ben Hutchings --- fs/ocfs2/dcache.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) --- a/fs/ocfs2/dcache.c +++ b/fs/ocfs2/dcache.c @@ -310,6 +310,18 @@ int ocfs2_dentry_attach_lock(struct dent out_attach: spin_lock(&dentry_attach_lock); + if (unlikely(dentry->d_fsdata && !alias)) { + /* d_fsdata is set by a racing thread which is doing + * the same thing as this thread is doing. Leave the racing + * thread going ahead and we return here. + */ + spin_unlock(&dentry_attach_lock); + iput(dl->dl_inode); + ocfs2_lock_res_free(&dl->dl_lockres); + kfree(dl); + return 0; + } + dentry->d_fsdata = dl; dl->dl_count++; spin_unlock(&dentry_attach_lock);