From patchwork Sun Aug 6 01:26:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Kent X-Patchwork-Id: 131554 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp706483vqr; Sat, 5 Aug 2023 18:30:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFxgqXTl/2PhanRFGzTL01Q4r1Na5SSu211qZcqTFcpD1CnGVlEdPhlzD9tw9U5oUDxL/KU X-Received: by 2002:a17:906:7494:b0:988:71c8:9f3a with SMTP id e20-20020a170906749400b0098871c89f3amr3923086ejl.16.1691285418037; Sat, 05 Aug 2023 18:30:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691285418; cv=none; d=google.com; s=arc-20160816; b=XbbJQdhEVBu+UQ97z37aVhDkoq6vEi+04Hq/uP4sDJOlQFQrDXpbyRhcda7TPfbaJB nScsGIl/FS6V5ixHIiI9sbbA/qyf27/V09iqltwkdmiaAvgw+WsJ6OiQos/NtskMwZbS TlQQbviL8Zd8Od+o9KkGQIj6cAME/jjzYtb8B0tsSe0vfql3QMDeWW0qMyR5j0uPlNFB ij4x5Yi8oAtoFCBK++DWehiqsaqqsQviIg6U/8xUEOLnbLJZmmY/w5uvRTuY8D5QxJk1 xEd/6XfGOZeAyPnCobHllJBw/+h+4mxv6Rj2twc5q/LmFt4/YWVBq3FohN8jSqPNDgw2 b+Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:message-id:date:cc:to:from:subject; bh=2AmBp018Ux91tfF0zA/Lw1gWw8feN1Rk9+eFTZK+xjs=; fh=a1LCQBhRO+Uppg3kc4a4aXvSTobRqgD3F5fJBKq1kQ8=; b=SULrCGRwOBF58IvJ1yvRRymECZfVovU69Kcl75dLp5PrSwPqtbpOCF+mTAq/DcZmj5 wKPczIHhByTji9uZ3EP4sTNoOirO2b0Wu9OVoYlVSqENuulJw0l5f/f9p+An+yXgA9YT OKbN/9bP2g9JFNyBjP9bewX0wMIaiXCKKhHtrfDzBsOjNgO59jLjVw9B/TU0Zul1G8y3 HJCtqi/56qiIcemifkT8yJmLdk2BvuuQSbLvZ03qn/YD5/bol3FgzmcLRWALNdVACc9p MD/9XFZi91L9Qj+eg/OwyNChYn41FqyTbsUGaEVXxzq0/ssE6jAYkdrmy2hQMivbIp2H pLAA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qq22-20020a17090720d600b00992bfd1ffc2si3857452ejb.981.2023.08.05.18.29.44; Sat, 05 Aug 2023 18:30:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229514AbjHFB1C (ORCPT + 99 others); Sat, 5 Aug 2023 21:27:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbjHFB1A (ORCPT ); Sat, 5 Aug 2023 21:27:00 -0400 Received: from smtp01.aussiebb.com.au (smtp01.aussiebb.com.au [121.200.0.92]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FEAC2109 for ; Sat, 5 Aug 2023 18:26:58 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp01.aussiebb.com.au (Postfix) with ESMTP id 78E4110030F for ; Sun, 6 Aug 2023 11:26:52 +1000 (AEST) X-Virus-Scanned: Debian amavisd-new at smtp01.aussiebb.com.au Received: from smtp01.aussiebb.com.au ([127.0.0.1]) by localhost (smtp01.aussiebb.com.au [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3lmYV9Gsp3gr for ; Sun, 6 Aug 2023 11:26:52 +1000 (AEST) Received: by smtp01.aussiebb.com.au (Postfix, from userid 116) id 6E7C01003BD; Sun, 6 Aug 2023 11:26:52 +1000 (AEST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 Received: from donald.themaw.net (2403-580e-4b40-0-7968-2232-4db8-a45e.ip6.aussiebb.net [IPv6:2403:580e:4b40:0:7968:2232:4db8:a45e]) by smtp01.aussiebb.com.au (Postfix) with ESMTP id E6F9C10028C; Sun, 6 Aug 2023 11:26:49 +1000 (AEST) Subject: [PATCH] kernfs: fix missing kernfs_iattr_rwsem locking From: Ian Kent To: Greg Kroah-Hartman , Tejun Heo Cc: Kernel Mailing List , Imran Khan , Miklos Szeredi , Arnd Bergmann , Anders Roxell , Minchan Kim , Eric Sandeen Date: Sun, 06 Aug 2023 09:26:49 +0800 Message-ID: <169128520941.68052.15749253469930138901.stgit@donald.themaw.net> User-Agent: StGit/1.5 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773441298494053943 X-GMAIL-MSGID: 1773441298494053943 When the kernfs_iattr_rwsem was introduced a case was missed. The update of the kernfs directory node child count was also protected by the kernfs_rwsem and needs to be included in the change so that the child count (and so the inode n_link attribute) does not change while holding the rwsem for read. Fixes: 9caf696142 (kernfs: Introduce separate rwsem to protect inode attributes) Signed-off-by: Ian Kent Reviewed-By: Imran Khan Acked-by: Miklos Szeredi Cc: Anders Roxell Cc: Arnd Bergmann Cc: Minchan Kim Cc: Eric Sandeen --- fs/kernfs/dir.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 5a1a4af9d3d2..bf243015834e 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -383,9 +383,11 @@ static int kernfs_link_sibling(struct kernfs_node *kn) rb_insert_color(&kn->rb, &kn->parent->dir.children); /* successfully added, account subdir number */ + down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); if (kernfs_type(kn) == KERNFS_DIR) kn->parent->dir.subdirs++; kernfs_inc_rev(kn->parent); + up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); return 0; } @@ -408,9 +410,11 @@ static bool kernfs_unlink_sibling(struct kernfs_node *kn) if (RB_EMPTY_NODE(&kn->rb)) return false; + down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); if (kernfs_type(kn) == KERNFS_DIR) kn->parent->dir.subdirs--; kernfs_inc_rev(kn->parent); + up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); rb_erase(&kn->rb, &kn->parent->dir.children); RB_CLEAR_NODE(&kn->rb);