Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp926987pxb; Wed, 6 Apr 2022 04:31:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxsjsfSUbGLXGbc3t4WV+ZEAOoVSFXA9zH4wQwaFP/qn95jO5ftAZz39zejWqYOLXiE5KMI X-Received: by 2002:a17:90a:888:b0:1ca:a9ac:c866 with SMTP id v8-20020a17090a088800b001caa9acc866mr9221225pjc.203.1649244668800; Wed, 06 Apr 2022 04:31:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649244668; cv=none; d=google.com; s=arc-20160816; b=I+/7+zI3V1Z67KHMsjg7ruwZ78Y9uQyAKwGPNxA96GGKE3LqWZnmrwimCElUB2jevh CSu9iErSObuouKcFr4aLL7BciyZttvpc1n4QO+J+kscSCqhVe8S2zWAZYl8hVWTV0CE5 xZKZ1o5ssErUY7d47OAUxS9+/yQci3COD1X5Z5x5UyP9/OSr+vvq4KD8Kz+42zKQw/bF GmfXNiHMqgef6736KaVh0DSpI7zMrVk30h6Ol5rL0NRME/k5Vl3YXxeN1WAcaadbl6M8 biwbo/DMDwvn6A3uKmsSRW4FGIVEFryJUxxX2+LNsw/ZqMYhnQFYLbJKq7R0xk8BLKh6 Q1oQ== 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:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=8Itta5D3j95UjxKivBijY3KS4NWBsAMO8xuDbdmZQxM=; b=uIF7KzWq3KC3zi/+zFPVoXmfTQwuZePn1ms7ildRt6FkhQrfePnCoiCofAvxRvzy4g H+k1ut1cxUGpnbLsYpkYEllxeu+GQHg9X+yeG2qLZGvAFijZvbDka/Rt2A+09zkcfScA lOjBN1b3vWHnKUUflXT5nq/bUWyob3xNxn1adaze/Dp+8PlPPJVI/R4wOOfv5q59omsn DUoF1dSuH7/hv/ulW2cO2DxxluPMFQ3JRHBcjeyNciruTcBuMzMWj8ZCM6RioQwiDvjY gZIP3AchGHQN6PHv+uPR527QctYEp7cQINibokvrGkQXqwCABVOyH3eMQRkzKhxf7yu4 aG3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gG70Y1gR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id i6-20020a6561a6000000b003822513f7adsi14036430pgv.445.2022.04.06.04.31.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 04:31:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gG70Y1gR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 499DD606649; Wed, 6 Apr 2022 02:53:08 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383484AbiDEUln (ORCPT + 99 others); Tue, 5 Apr 2022 16:41:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344808AbiDEKkN (ORCPT ); Tue, 5 Apr 2022 06:40:13 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 571622CC87; Tue, 5 Apr 2022 03:25:23 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2F0E66141D; Tue, 5 Apr 2022 10:25:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43ABFC385A0; Tue, 5 Apr 2022 10:25:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649154322; bh=0ACTeB0AQnU4xF7As+Q/1bWFh20ZLPazzO9cB2wIEMs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gG70Y1gRw1AudqirUc71lVcx5EZIC+BzknP9dYUStDAIYPlwjbn0JCe3SY27QwuFY RE3ZfDIOmKDt0hl+fLzUpShKUACyYYYy14M8Mxbl/f22MYbzFRehGEwfu6xE98yg+v YdkFUQrsHbPWlcH94r8DOaZuOu/3VDYcjOlLPbSo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhihao Cheng , Richard Weinberger Subject: [PATCH 5.10 539/599] ubifs: Fix deadlock in concurrent rename whiteout and inode writeback Date: Tue, 5 Apr 2022 09:33:53 +0200 Message-Id: <20220405070314.882018104@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070258.802373272@linuxfoundation.org> References: <20220405070258.802373272@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Zhihao Cheng commit afd427048047e8efdedab30e8888044e2be5aa9c upstream. Following hung tasks: [ 77.028764] task:kworker/u8:4 state:D stack: 0 pid: 132 [ 77.028820] Call Trace: [ 77.029027] schedule+0x8c/0x1b0 [ 77.029067] mutex_lock+0x50/0x60 [ 77.029074] ubifs_write_inode+0x68/0x1f0 [ubifs] [ 77.029117] __writeback_single_inode+0x43c/0x570 [ 77.029128] writeback_sb_inodes+0x259/0x740 [ 77.029148] wb_writeback+0x107/0x4d0 [ 77.029163] wb_workfn+0x162/0x7b0 [ 92.390442] task:aa state:D stack: 0 pid: 1506 [ 92.390448] Call Trace: [ 92.390458] schedule+0x8c/0x1b0 [ 92.390461] wb_wait_for_completion+0x82/0xd0 [ 92.390469] __writeback_inodes_sb_nr+0xb2/0x110 [ 92.390472] writeback_inodes_sb_nr+0x14/0x20 [ 92.390476] ubifs_budget_space+0x705/0xdd0 [ubifs] [ 92.390503] do_rename.cold+0x7f/0x187 [ubifs] [ 92.390549] ubifs_rename+0x8b/0x180 [ubifs] [ 92.390571] vfs_rename+0xdb2/0x1170 [ 92.390580] do_renameat2+0x554/0x770 , are caused by concurrent rename whiteout and inode writeback processes: rename_whiteout(Thread 1) wb_workfn(Thread2) ubifs_rename do_rename lock_4_inodes (Hold ui_mutex) ubifs_budget_space make_free_space shrink_liability __writeback_inodes_sb_nr bdi_split_work_to_wbs (Queue new wb work) wb_do_writeback(wb work) __writeback_single_inode ubifs_write_inode LOCK(ui_mutex) ↑ wb_wait_for_completion (Wait wb work) <-- deadlock! Reproducer (Detail program in [Link]): 1. SYS_renameat2("/mp/dir/file", "/mp/dir/whiteout", RENAME_WHITEOUT) 2. Consume out of space before kernel(mdelay) doing budget for whiteout Fix it by doing whiteout space budget before locking ubifs inodes. BTW, it also fixes wrong goto tag 'out_release' in whiteout budget error handling path(It should at least recover dir i_size and unlock 4 ubifs inodes). Fixes: 9e0a1fff8db56ea ("ubifs: Implement RENAME_WHITEOUT") Link: https://bugzilla.kernel.org/show_bug.cgi?id=214733 Signed-off-by: Zhihao Cheng Signed-off-by: Richard Weinberger Signed-off-by: Greg Kroah-Hartman --- fs/ubifs/dir.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) --- a/fs/ubifs/dir.c +++ b/fs/ubifs/dir.c @@ -1322,6 +1322,7 @@ static int do_rename(struct inode *old_d if (flags & RENAME_WHITEOUT) { union ubifs_dev_desc *dev = NULL; + struct ubifs_budget_req wht_req; dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); if (!dev) { @@ -1343,6 +1344,20 @@ static int do_rename(struct inode *old_d whiteout_ui->data = dev; whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0)); ubifs_assert(c, !whiteout_ui->dirty); + + memset(&wht_req, 0, sizeof(struct ubifs_budget_req)); + wht_req.dirtied_ino = 1; + wht_req.dirtied_ino_d = ALIGN(whiteout_ui->data_len, 8); + /* + * To avoid deadlock between space budget (holds ui_mutex and + * waits wb work) and writeback work(waits ui_mutex), do space + * budget before ubifs inodes locked. + */ + err = ubifs_budget_space(c, &wht_req); + if (err) { + iput(whiteout); + goto out_release; + } } lock_4_inodes(old_dir, new_dir, new_inode, whiteout); @@ -1417,16 +1432,6 @@ static int do_rename(struct inode *old_d } if (whiteout) { - struct ubifs_budget_req wht_req = { .dirtied_ino = 1, - .dirtied_ino_d = \ - ALIGN(ubifs_inode(whiteout)->data_len, 8) }; - - err = ubifs_budget_space(c, &wht_req); - if (err) { - iput(whiteout); - goto out_release; - } - inc_nlink(whiteout); mark_inode_dirty(whiteout);