From patchwork Sat Nov 30 02:02:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 11267815 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 93D3B13A4 for ; Sat, 30 Nov 2019 01:41:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6FFA0215E5 for ; Sat, 30 Nov 2019 01:41:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727277AbfK3BlS (ORCPT ); Fri, 29 Nov 2019 20:41:18 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:39668 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727230AbfK3BlR (ORCPT ); Fri, 29 Nov 2019 20:41:17 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 6C92941332E05518ED29; Sat, 30 Nov 2019 09:41:15 +0800 (CST) Received: from huawei.com (10.175.124.28) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.439.0; Sat, 30 Nov 2019 09:41:07 +0800 From: yu kuai To: , , , , , , , , CC: , , , , , , Subject: [PATCH V2 1/3] dcache: add a new enum type for 'dentry_d_lock_class' Date: Sat, 30 Nov 2019 10:02:23 +0800 Message-ID: <20191130020225.20239-2-yukuai3@huawei.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20191130020225.20239-1-yukuai3@huawei.com> References: <20191130020225.20239-1-yukuai3@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.28] X-CFilter-Loop: Reflected Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 'dentry_d_lock_class' can be used for spin_lock_nested in case lockdep confused about two different dentry take the 'd_lock'. However, a single 'DENTRY_D_LOCK_NESTED' may not be enough if more than two dentry are involed. So, add in 'DENTRY_D_LOCK_NESTED_TWICE'. Signed-off-by: yu kuai --- include/linux/dcache.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 10090f11ab95..830405b401ce 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -129,7 +129,16 @@ struct dentry { enum dentry_d_lock_class { DENTRY_D_LOCK_NORMAL, /* implicitly used by plain spin_lock() APIs. */ - DENTRY_D_LOCK_NESTED + /* + * used by spin_lock_nested when two d_lock need to be taken + * at the same time + */ + DENTRY_D_LOCK_NESTED, + /* + * used by spin_lock_nested when three d_lock need to be taken + * at the same time + */ + DENTRY_D_LOCK_NESTED_TWICE }; struct dentry_operations { From patchwork Sat Nov 30 02:02:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 11267813 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 688F4195A for ; Sat, 30 Nov 2019 01:41:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 45733215E5 for ; Sat, 30 Nov 2019 01:41:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727289AbfK3BlS (ORCPT ); Fri, 29 Nov 2019 20:41:18 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:39606 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727227AbfK3BlR (ORCPT ); Fri, 29 Nov 2019 20:41:17 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 5E6CC6908ADF057AE9F2; Sat, 30 Nov 2019 09:41:15 +0800 (CST) Received: from huawei.com (10.175.124.28) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.439.0; Sat, 30 Nov 2019 09:41:08 +0800 From: yu kuai To: , , , , , , , , CC: , , , , , , Subject: [PATCH 2/3] fs/libfs.c: use 'spin_lock_nested' when taking 'd_lock' for dentry in simple_empty Date: Sat, 30 Nov 2019 10:02:24 +0800 Message-ID: <20191130020225.20239-3-yukuai3@huawei.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20191130020225.20239-1-yukuai3@huawei.com> References: <20191130020225.20239-1-yukuai3@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.28] X-CFilter-Loop: Reflected Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org simple_emtpty currently use 'spin_lock_nested' for 'child' to avoid confusion for lockdep. However, it's not used for 'dentry'. In that case, there will be a problem if the caller called 'simple_empty' with a parent's 'd_lock' held: spin_lock(&dentry->d_parent->d_lock) call simple_empty(dentry) spin_lock(&dentry->d_lock) --> lockdep will report this spin_lock_nested(child->d_lock, spin_lock_nested) spin_unlock(child_lock) spin_unlock($dentry->d_lock) return from simple_empty spin_unlock(&dentry->d_patrent->d_lock) So, use 'DENTRY_D_LOCK_NESTED' for 'dentry' and 'DENTRY_D_LOCK_NESTED_TWICE' for child. Signed-off-by: yu kuai --- fs/libfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 1463b038ffc4..54a37444f7f9 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -336,9 +336,9 @@ int simple_empty(struct dentry *dentry) struct dentry *child; int ret = 0; - spin_lock(&dentry->d_lock); + spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); list_for_each_entry(child, &dentry->d_subdirs, d_child) { - spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); + spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED_TWICE); if (simple_positive(child)) { spin_unlock(&child->d_lock); goto out; From patchwork Sat Nov 30 02:02:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 11267809 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 14691112B for ; Sat, 30 Nov 2019 01:41:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EDFF02464D for ; Sat, 30 Nov 2019 01:41:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727237AbfK3BlR (ORCPT ); Fri, 29 Nov 2019 20:41:17 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:39660 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727231AbfK3BlR (ORCPT ); Fri, 29 Nov 2019 20:41:17 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 658C6E2D7CCA17DD39D2; Sat, 30 Nov 2019 09:41:15 +0800 (CST) Received: from huawei.com (10.175.124.28) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.439.0; Sat, 30 Nov 2019 09:41:09 +0800 From: yu kuai To: , , , , , , , , CC: , , , , , , Subject: [PATCH 3/3] debugfs: fix potential infinite loop in debugfs_remove_recursive Date: Sat, 30 Nov 2019 10:02:25 +0800 Message-ID: <20191130020225.20239-4-yukuai3@huawei.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20191130020225.20239-1-yukuai3@huawei.com> References: <20191130020225.20239-1-yukuai3@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.28] X-CFilter-Loop: Reflected Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org debugfs_remove_recursive uses list_empty to judge weather a dentry has any subdentry or not. This can lead to infinite loop when any subdir is in use. The problem was discoverd by the following steps in the console. 1. use debugfs_create_dir to create a dir and multiple subdirs(insmod); 2. cd to the subdir with depth not less than 2; 3. call debugfs_remove_recursive(rmmod). After removing the subdir, the infinite loop is triggered because debugfs_remove_recursive uses list_empty to judge if the current dir doesn't have any subdentry. However list_empty can't skip the subdentry that is not simple_positive(simple_positive() will return false). Fix the problem by using simple_empty instead of list_empty. Fixes: 776164c1faac ('debugfs: debugfs_remove_recursive() must not rely on list_empty(d_subdirs)') Reported-by: chenxiang66@hisilicon.com Signed-off-by: yu kuai --- fs/debugfs/inode.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c index 7b975dbb2bb4..d5ef7a0a4410 100644 --- a/fs/debugfs/inode.c +++ b/fs/debugfs/inode.c @@ -773,8 +773,11 @@ void debugfs_remove_recursive(struct dentry *dentry) if (!simple_positive(child)) continue; - /* perhaps simple_empty(child) makes more sense */ - if (!list_empty(&child->d_subdirs)) { + /* + * use simple_empty to prevent infinite loop when any + * subdentry of child is in use + */ + if (!simple_empty(child)) { spin_unlock(&parent->d_lock); inode_unlock(d_inode(parent)); parent = child;