Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3473143rwa; Tue, 23 Aug 2022 05:27:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR7YOJcGL053tlzmsqb5pi+I7BhoAg1MQNwNxiqZdd0368gSPVTL+G8OE92swVNDh8VgEf89 X-Received: by 2002:a63:f0a:0:b0:42a:fc7b:543e with SMTP id e10-20020a630f0a000000b0042afc7b543emr1129707pgl.499.1661257664793; Tue, 23 Aug 2022 05:27:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661257664; cv=none; d=google.com; s=arc-20160816; b=BY0OAgH6ODoExSQ5UQfJLuiDEXvQg+TIYzAvutpWwe9VhMTVuarLqaQVdNb0oWOM60 etRrROtqcjqMxtHZ2f4I3roopfC00ZdLE5iARDynLStV8VAlNGjpTQg0SiCct910f/IW AURfMoXQxHaz4SwLb4HnLTuf3ewtA7mmZ4tt1YXiiG8pTAXaPLdy8/AZq08sHo7VIMyt 7Z7p/fsPQg3s4jN/6NlLeG9Yw25HghNYV6BaPJ3oHhKxlJMByuev+nYdpZACf8f/d8tu J2S+AupCWwrA85P2oLkPn24eCHe03DUpz2CvqoAIGcE0G4LIf06E+pF1U6zZs7/6xDsW uQCw== 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=6NwYi6BfIryNucy1WeaYdEpCEdWUIodWhO7x0oCB5t4=; b=cgGQRAx+RazMYNxHDuItYvgCHX2k71DkMgUkGUuXGIebY3oWfeX9baAr3kmYlGgsJl QoAl5nHH3ybwxpHuoGUR0jocNBBHNJnOYMgD/ZRbfHSPGFxvrOwt01hnKU4RtyxeW1hY Aw5HdTy1gs3o5DLYASQ9CjO2eFVogFPS67xnR+3awHG+nzcoqyJgYxLk0hAusHr1FHGg IhB5xWfAxlxCnXIBO7zlOJtc80411HJZVClNhizhexlUM2ho1gWi+DEb8VphTPbd6peJ lL+KjwAlBMrfyeA6MKo+dHPExr/sdeCjvnqAZhvSgz8StjCX48aSIa8kcdqKEcJRQ0R+ WZlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XHu6N09U; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t16-20020a17090aba9000b001fafc10a6ccsi10713608pjr.187.2022.08.23.05.27.32; Tue, 23 Aug 2022 05:27:44 -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=@linuxfoundation.org header.s=korg header.b=XHu6N09U; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354900AbiHWK3w (ORCPT + 99 others); Tue, 23 Aug 2022 06:29:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352736AbiHWKN1 (ORCPT ); Tue, 23 Aug 2022 06:13:27 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D94407284E; Tue, 23 Aug 2022 01:59:35 -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 ams.source.kernel.org (Postfix) with ESMTPS id 80CFFB81C1C; Tue, 23 Aug 2022 08:59:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBFDEC433D6; Tue, 23 Aug 2022 08:59:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661245173; bh=barHYbJ38GWSZZ4f3neFi2+4tV/8a0RE6MuxMx1Rpw8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XHu6N09U9iFhEXRnCHxRWDoIk1aikbagRvGygbv6ODyzhMCoRMN7uKHChSDUwThwR dSZwZxhBn33jgpeXUG+H55WqqU8l4QQa4l3N3iR8Wg5ZbW/tOjjX0GrDCIHsKN1zWL E1qJgqPaPgg/gj2h6pNp47+41L/Fon8458cvAWYU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Brian Foster , "Darrick J. Wong" , Dave Chinner , Leah Rumancik Subject: [PATCH 5.15 239/244] xfs: remove infinite loop when reserving free block pool Date: Tue, 23 Aug 2022 10:26:38 +0200 Message-Id: <20220823080107.580775752@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080059.091088642@linuxfoundation.org> References: <20220823080059.091088642@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: "Darrick J. Wong" [ Upstream commit 15f04fdc75aaaa1cccb0b8b3af1be290e118a7bc ] Infinite loops in kernel code are scary. Calls to xfs_reserve_blocks should be rare (people should just use the defaults!) so we really don't need to try so hard. Simplify the logic here by removing the infinite loop. Cc: Brian Foster Signed-off-by: Darrick J. Wong Reviewed-by: Dave Chinner Signed-off-by: Leah Rumancik Acked-by: Darrick J. Wong Signed-off-by: Greg Kroah-Hartman --- fs/xfs/xfs_fsops.c | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) --- a/fs/xfs/xfs_fsops.c +++ b/fs/xfs/xfs_fsops.c @@ -430,46 +430,36 @@ xfs_reserve_blocks( * If the request is larger than the current reservation, reserve the * blocks before we update the reserve counters. Sample m_fdblocks and * perform a partial reservation if the request exceeds free space. + * + * The code below estimates how many blocks it can request from + * fdblocks to stash in the reserve pool. This is a classic TOCTOU + * race since fdblocks updates are not always coordinated via + * m_sb_lock. */ - error = -ENOSPC; - do { - free = percpu_counter_sum(&mp->m_fdblocks) - + free = percpu_counter_sum(&mp->m_fdblocks) - xfs_fdblocks_unavailable(mp); - if (free <= 0) - break; - - delta = request - mp->m_resblks; - lcounter = free - delta; - if (lcounter < 0) - /* We can't satisfy the request, just get what we can */ - fdblks_delta = free; - else - fdblks_delta = delta; - + delta = request - mp->m_resblks; + if (delta > 0 && free > 0) { /* * We'll either succeed in getting space from the free block - * count or we'll get an ENOSPC. If we get a ENOSPC, it means - * things changed while we were calculating fdblks_delta and so - * we should try again to see if there is anything left to - * reserve. - * - * Don't set the reserved flag here - we don't want to reserve - * the extra reserve blocks from the reserve..... + * count or we'll get an ENOSPC. Don't set the reserved flag + * here - we don't want to reserve the extra reserve blocks + * from the reserve. */ + fdblks_delta = min(free, delta); spin_unlock(&mp->m_sb_lock); error = xfs_mod_fdblocks(mp, -fdblks_delta, 0); spin_lock(&mp->m_sb_lock); - } while (error == -ENOSPC); - /* - * Update the reserve counters if blocks have been successfully - * allocated. - */ - if (!error && fdblks_delta) { - mp->m_resblks += fdblks_delta; - mp->m_resblks_avail += fdblks_delta; + /* + * Update the reserve counters if blocks have been successfully + * allocated. + */ + if (!error) { + mp->m_resblks += fdblks_delta; + mp->m_resblks_avail += fdblks_delta; + } } - out: if (outval) { outval->resblks = mp->m_resblks;