Received: by 2002:a05:6358:795:b0:dc:4c66:fc3e with SMTP id n21csp44199rwj; Fri, 28 Oct 2022 22:16:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6I/zMlGfHN6SjZFiv38vM6Mch0SaY3+aZQu5OiZCsCNJSCqEQLUyzVw6L4qlhWE0gPGV/V X-Received: by 2002:a17:907:2c59:b0:7a9:1134:db02 with SMTP id hf25-20020a1709072c5900b007a91134db02mr2516469ejc.508.1667020616280; Fri, 28 Oct 2022 22:16:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667020616; cv=none; d=google.com; s=arc-20160816; b=IsH1NvdrGTzYeiT3m0wb2iU1nDUmCBVnKZykJhcaqgdccR1eTO7cuHK1csfYm8C4EB 71jJcZy6VoU+p5hrn7FS6hvRK5Rr4yNIlJXeH+ST/YW2Z87U4NhSLzQP/6x1vFy4Sk7M /rEkj3BiyZ7PgMkL0h6XzgcGiXB36CdYJ4qB9QwTPiwmIeMdLV4IA6oxmucjc57iHAN9 Cvb4Yd55yn5ok0ier2pvC80t90QvSzuefo0VzSfBb4a/yT4gXyT/uTwyehH16O5vxnPL Gd/7bU1sILFfPknkeMSF4n1/vlCOVuGXl88IuhVtoa4wOVkj/6a16rQMZHjOAXBuMjUS +I5w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=euh2my+Sy+qXwfAid3AtEb0nPeWmqr+fHHInSeKPPc0=; b=kVZsB2G/Hyj/MaEW4CvKFvqP3JWTnlQr1Bn752BbolazxjQL1NrvW5yDnUaGoJJZNa TNc/Qu6l25DLMm+cfIqlqf+RmEryPadzz8xyRm2YNddgM+t1Nwi4bhCPyfVwOBlb76fl /U+x86imvKo4cdiiZWE0htcy+e9nlQnGxxVzgUL6yXp7vwpW1n5Q+xvhtw3Psw2F+Eh9 1OAWKOzgdr8/G5Nfh6HNcFcaoC+qICWKBe9ke4zyiDtMcil6J9YmYRNtvoU7gMx1idpF wAqH1sy+QMIqg5quGbky81lmYu6J1EtX2m5kFrD8x2HSpEKGWjYztPwJZ5Vxt7+vxZB5 hP5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=EUB36euj; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w9-20020a056402268900b004617facf124si855875edd.253.2022.10.28.22.16.31; Fri, 28 Oct 2022 22:16:56 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=EUB36euj; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229743AbiJ2EtU (ORCPT + 99 others); Sat, 29 Oct 2022 00:49:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbiJ2EtS (ORCPT ); Sat, 29 Oct 2022 00:49:18 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5F2D1AFA84; Fri, 28 Oct 2022 21:49:17 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id 17so2159735pfv.4; Fri, 28 Oct 2022 21:49:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=euh2my+Sy+qXwfAid3AtEb0nPeWmqr+fHHInSeKPPc0=; b=EUB36euj9XgdqD9PfFhr6RPcN0XLaM9nL58cjW4OidgmsavA6U7bO4E3/PbC7zIXpY b1XYr0B2wA7Qjh9wBE0w3xb5+/STwpPfWoxDXM/Vmak3RC31Un265kFVreu2+qUWPyd4 GCRBgPjqT12EzMLX1DRZzY5Munjeg7hJCseU7/S2QDgvxQ/RZ5iHGnf+B+y9ZBv1RKa7 +p/gNUWmM3wlS7hcO6LRVybjqmVq1gFt9P7RbKEIkkvcj95RGdFQoB8ey+wTpFGjuEtO 81O5QknK0M34Mr3Z2eSWThmDTSK0CSL0AgknRtmMGkQ/ucTHtluPySLXjnhgRZHb4MBr ucxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=euh2my+Sy+qXwfAid3AtEb0nPeWmqr+fHHInSeKPPc0=; b=WL/aaIXxd42wxTGA2MegfhD3i9xx1rZAqgbTrqvhbbr9cSvAoMbsprWNzDhwEkXMHi wzW4sDN6S/S4eUvhhTIFILDlGCG1671we4icdnnDWY4KVAKpnA/QOG9TP+vYJ9LJstCv b+kfTxAjtyn+jMyaOWNz0PL56v1YKAXn6uM+ycXk3cSPPCZ8NNOf5UZMHhrUpTxCmO+M FdE4nl6piItZF/IMDkYUwGkmhM7hXypAciPwl4dHGBc4LS0BOOVkdSxgs04RQ4owLlFa L2NFhqJFaBSuK3YTQ2sUCUNWXD08wvWfSbyOwUHLT/iqNxz1+GN3197ZvHGJTu9v+hyw xYoQ== X-Gm-Message-State: ACrzQf3qaNFfa16MsAcItBeGLqaEmIQKAY0+R+0y+CwUk/ssgUxvhDxY VCaU54r0GCKaWmErdHK7M0S0LakL9p4BKQ== X-Received: by 2002:a63:f214:0:b0:461:8862:331e with SMTP id v20-20020a63f214000000b004618862331emr2600393pgh.386.1667018956989; Fri, 28 Oct 2022 21:49:16 -0700 (PDT) Received: from carrot.. (i220-108-44-211.s42.a014.ap.plala.or.jp. [220.108.44.211]) by smtp.gmail.com with ESMTPSA id f1-20020a170902f38100b00176ab6a0d5fsm324652ple.54.2022.10.28.21.49.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Oct 2022 21:49:16 -0700 (PDT) From: Ryusuke Konishi To: Andrew Morton Cc: linux-nilfs , syzbot , syzkaller-bugs@googlegroups.com, LKML Subject: [PATCH] nilfs2: fix deadlock in nilfs_count_free_blocks() Date: Sat, 29 Oct 2022 13:49:12 +0900 Message-Id: <20221029044912.9139-1-konishi.ryusuke@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <000000000000fbf46f05eb3645d0@google.com> References: <000000000000fbf46f05eb3645d0@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A semaphore deadlock can occur if nilfs_get_block() detects metadata corruption while locating data blocks and a superblock writeback occurs at the same time: task 1 task 2 ------ ------ * A file operation * nilfs_truncate() nilfs_get_block() down_read(rwsem A) <-- nilfs_bmap_lookup_contig() ... generic_shutdown_super() nilfs_put_super() * Prepare to write superblock * down_write(rwsem B) <-- nilfs_cleanup_super() * Detect b-tree corruption * nilfs_set_log_cursor() nilfs_bmap_convert_error() nilfs_count_free_blocks() __nilfs_error() down_read(rwsem A) <-- nilfs_set_error() down_write(rwsem B) <-- *** DEADLOCK *** Here, nilfs_get_block() readlocks rwsem A (= NILFS_MDT(dat_inode)->mi_sem) and then calls nilfs_bmap_lookup_contig(), but if it fails due to metadata corruption, __nilfs_error() is called from nilfs_bmap_convert_error() inside the lock section. Since __nilfs_error() calls nilfs_set_error() unless the filesystem is read-only and nilfs_set_error() attempts to writelock rwsem B (= nilfs->ns_sem) to write back superblock exclusively, hierarchical lock acquisition occurs in the order rwsem A -> rwsem B. Now, if another task starts updating the superblock, it may writelock rwsem B during the lock sequence above, and can deadlock trying to readlock rwsem A in nilfs_count_free_blocks(). However, there is actually no need to take rwsem A in nilfs_count_free_blocks() because it, within the lock section, only reads a single integer data on a shared struct with nilfs_sufile_get_ncleansegs(). This has been the case after commit aa474a220180 ("nilfs2: add local variable to cache the number of clean segments"), that is, even before this bug was introduced. So, this resolves the deadlock problem by just not taking the semaphore in nilfs_count_free_blocks(). Signed-off-by: Ryusuke Konishi Reported-by: syzbot+45d6ce7b7ad7ef455d03@syzkaller.appspotmail.com Fixes: e828949e5b42 ("nilfs2: call nilfs_error inside bmap routines") Tested-by: Ryusuke Konishi Cc: stable@vger.kernel.org # v2.6.38+ --- fs/nilfs2/the_nilfs.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/fs/nilfs2/the_nilfs.c b/fs/nilfs2/the_nilfs.c index 3b4a079c9617..c8b89b4f94e0 100644 --- a/fs/nilfs2/the_nilfs.c +++ b/fs/nilfs2/the_nilfs.c @@ -690,9 +690,7 @@ int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks) { unsigned long ncleansegs; - down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); - up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment; return 0; } -- 2.34.1