Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp1098849rdg; Fri, 11 Aug 2023 09:37:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IENzmRJj5X1VupcPkdLdivFEbe0CeXvPi6SdhuQw8i8PakkF4oQTXsQW0D2kG4vGbiR/vLs X-Received: by 2002:a05:6a20:12c3:b0:140:3cc7:b3a6 with SMTP id v3-20020a056a2012c300b001403cc7b3a6mr7620122pzg.4.1691771843599; Fri, 11 Aug 2023 09:37:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691771843; cv=none; d=google.com; s=arc-20160816; b=ZkI0WdJ0gQ8lafo9Iddnf2rbXz/OaK5ZArDVm2secRqY1PbsVzeXc4weA3tp7/ikfv kX486E+KaBhkh8zP4U9YJx5cGEatb8qrKYOz1f+6nFhDrQ7BwG4r8fkBF5taTs5u2h9+ jAwl1X6f5dZfnreLlWyZPWRgBVa8GOxNy6WfNDhn4OWbXxov59ePSVBnfoALOyJ5EIhw 0JNNr683Dr7HrzjGHp9AiCT/aeBaBlwG7Nux0f1zzHmGawTa0tDnRtHIWp/svMD2ASKa aU4bredYPxbQ9fpL3mQOkDl2R+lVC3xCprBYhJ8aEQuflR2QAGV3bJBMbfA57nLICTNh XSvw== 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=vnCMJDHvrSrLNSZkcpfQR1Q0wFl30okMiQEpgzdx9ds=; fh=BvTsPvZmwNzIqPb2bL1VMvtoAQxT1jAJEBJAY1B8Ad0=; b=PtWxiV2A6tO0wC+rFtPCK3J1zxjcQSKwRl5cXTSHCfHaLz16O48WkezCh/w1+htO1r yFkSpPP1oMhVkbW+gO4OsLegOHLm5snX7VuW1Z20XJXW+dLvmSVno+neg1JD9LPgWLzY h0c5DqssN4WQ5ALSbjeQCUCH1mdt6Htd+uQrI725/vx1vRnVRRNrHFNb76vuoYEKjR17 Te0WMc4sO92eS9fpCXMN3dJTexJEPiOf2Z+sEe74Yh5n6Ns9J3Dp1KJNDg90LaQZNiIV LQsYG8yGwanrcGiLRSM8+J1ZiQBCMQ0WO77W1LjUonQ/VrztWQfJXi2k04Qj1wBaKiCX /sZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=USTOcRLn; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv185-20020a632ec2000000b005640118d203si3506625pgb.193.2023.08.11.09.37.10; Fri, 11 Aug 2023 09:37:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=@infradead.org header.s=casper.20170209 header.b=USTOcRLn; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232046AbjHKQPw (ORCPT + 99 others); Fri, 11 Aug 2023 12:15:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235157AbjHKQPq (ORCPT ); Fri, 11 Aug 2023 12:15:46 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C582271E; Fri, 11 Aug 2023 09:15:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=vnCMJDHvrSrLNSZkcpfQR1Q0wFl30okMiQEpgzdx9ds=; b=USTOcRLnPKT38Z7UkKIopQ+evQ +U6mA6hybkvUu3vUq2cTEdn6mIS9VGPYYF5fvVdHBxkigAjcbpMI6X8tJgNnCvtGtRkqMAaalNA7E 7nCFXGIl+jVu2RnbwmlODeDHQRBqw7M+VjNfBXRCqj4M8gbC+p4HY7E0t51umne9y9jiTyfLCm3S8 +hekBh7O12rkJsxZ6bsasm1Ctz+kl1ZP539DmEfEStQ8zAATDW6HRh0rbDT04l8moQE/EtKCqX56b k4sk6igMuKis+SGUSMjhkbcpM1DOf4xXwvVlQ7FkQQZQN4y8eXOedNeQHu8xkEi5NvkJ24x43q1hz 2x+TJIsQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qUUnT-0027kY-2q; Fri, 11 Aug 2023 16:15:31 +0000 From: "Matthew Wilcox (Oracle)" To: akpm@linux-foundation.org Cc: "Matthew Wilcox (Oracle)" , Hui Zhu , linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org Subject: [PATCH 2/3] buffer: Hoist GFP flags from grow_dev_page() to __getblk_gfp() Date: Fri, 11 Aug 2023 17:15:27 +0100 Message-Id: <20230811161528.506437-3-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230811161528.506437-1-willy@infradead.org> References: <20230811161528.506437-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,URIBL_BLOCKED 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-ext4@vger.kernel.org grow_dev_page() is only called by grow_buffers(). grow_buffers() is only called by __getblk_slow() and __getblk_slow() is only called from __getblk_gfp(), so it is safe to move the GFP flags setting all the way up. With that done, add a new bdev_getblk() entry point that leaves the GFP flags the way the caller specified them. Signed-off-by: Matthew Wilcox (Oracle) --- fs/buffer.c | 60 ++++++++++++++++++++++++------------- include/linux/buffer_head.h | 2 ++ 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index 7326acc29541..122b7d16befb 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -1048,20 +1048,11 @@ grow_dev_page(struct block_device *bdev, sector_t block, struct buffer_head *bh; sector_t end_block; int ret = 0; - gfp_t gfp_mask; - - gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp; - - /* - * XXX: __getblk_slow() can not really deal with failure and - * will endlessly loop on improvised global reclaim. Prefer - * looping in the allocator rather than here, at least that - * code knows what it's doing. - */ - gfp_mask |= __GFP_NOFAIL; folio = __filemap_get_folio(inode->i_mapping, index, - FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp_mask); + FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp); + if (IS_ERR(folio)) + return PTR_ERR(folio); bh = folio_buffers(folio); if (bh) { @@ -1074,7 +1065,9 @@ grow_dev_page(struct block_device *bdev, sector_t block, goto failed; } - bh = folio_alloc_buffers(folio, size, gfp_mask); + bh = folio_alloc_buffers(folio, size, gfp); + if (!bh) + goto failed; /* * Link the folio to the buffers and initialise them. Take the @@ -1426,24 +1419,49 @@ __find_get_block(struct block_device *bdev, sector_t block, unsigned size) } EXPORT_SYMBOL(__find_get_block); +/** + * bdev_getblk - Get a buffer_head in a block device's buffer cache. + * @bdev: The block device. + * @block: The block number. + * @size: The size of buffer_heads for this @bdev. + * @gfp: The memory allocation flags to use. + * + * In contrast to __getblk_gfp(), the @gfp flags must be all of the flags; + * they are not augmented with the mapping's GFP flags. + * + * Return: The buffer head, or NULL if memory could not be allocated. + */ +struct buffer_head *bdev_getblk(struct block_device *bdev, sector_t block, + unsigned size, gfp_t gfp) +{ + struct buffer_head *bh = __find_get_block(bdev, block, size); + + might_alloc(gfp); + if (bh) + return bh; + + return __getblk_slow(bdev, block, size, gfp); +} +EXPORT_SYMBOL(bdev_getblk); + /* * __getblk_gfp() will locate (and, if necessary, create) the buffer_head * which corresponds to the passed block_device, block and size. The * returned buffer has its reference count incremented. - * - * __getblk_gfp() will lock up the machine if grow_dev_page's - * try_to_free_buffers() attempt is failing. FIXME, perhaps? */ struct buffer_head * __getblk_gfp(struct block_device *bdev, sector_t block, unsigned size, gfp_t gfp) { - struct buffer_head *bh = __find_get_block(bdev, block, size); + gfp |= mapping_gfp_constraint(bdev->bd_inode->i_mapping, ~__GFP_FS); - might_sleep(); - if (bh == NULL) - bh = __getblk_slow(bdev, block, size, gfp); - return bh; + /* + * Prefer looping in the allocator rather than here, at least that + * code knows what it's doing. + */ + gfp |= __GFP_NOFAIL; + + return bdev_getblk(bdev, block, size, gfp); } EXPORT_SYMBOL(__getblk_gfp); diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h index d17efb8b7976..01110db9213c 100644 --- a/include/linux/buffer_head.h +++ b/include/linux/buffer_head.h @@ -233,6 +233,8 @@ void __wait_on_buffer(struct buffer_head *); wait_queue_head_t *bh_waitq_head(struct buffer_head *bh); struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block, unsigned size); +struct buffer_head *bdev_getblk(struct block_device *bdev, sector_t block, + unsigned size, gfp_t gfp); struct buffer_head *__getblk_gfp(struct block_device *bdev, sector_t block, unsigned size, gfp_t gfp); void __brelse(struct buffer_head *); -- 2.40.1