From patchwork Mon Jan 29 14:34:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13535774 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40A51C47DB3 for ; Mon, 29 Jan 2024 14:36:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C98746B00B5; Mon, 29 Jan 2024 09:36:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C20966B00B6; Mon, 29 Jan 2024 09:36:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC1C76B00B7; Mon, 29 Jan 2024 09:36:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9A6856B00B5 for ; Mon, 29 Jan 2024 09:36:07 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 31ADB120A6D for ; Mon, 29 Jan 2024 14:36:07 +0000 (UTC) X-FDA: 81732598374.01.3257AA3 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf28.hostedemail.com (Postfix) with ESMTP id 64981C000B for ; Mon, 29 Jan 2024 14:36:05 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=Bw1EYdds; spf=none (imf28.hostedemail.com: domain of BATV+95c35c30fd22f84c25d9+7463+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+95c35c30fd22f84c25d9+7463+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706538965; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=300P4uyDHmiPq/TQ4ACZID3NFloQxMDWfXSAQlSY+eU=; b=oBMQZ+iII1AGThrMcAS1KxB/KQ73kkuXZzAQpHYi7XOBT1wTRiPzuA+clI0bo0DWx3mmwR khWfKZ7pZKGFpnBWTWBQa7sy7SdY0uWZAALCabZf9gLvAJRyjIbary561aD0rDrEdB3drr NnBEmNqBZkLvUWMUS4AVGg0fgf5ls44= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=Bw1EYdds; spf=none (imf28.hostedemail.com: domain of BATV+95c35c30fd22f84c25d9+7463+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+95c35c30fd22f84c25d9+7463+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706538965; a=rsa-sha256; cv=none; b=51XSABkcSPe8mZHO1xjkel8lfFUevIEhtG+5Rrr6jJ2+qKTeLn60arLUEEUsj6hH/WgaO2 Hw2MCzyrWA/VWWMr7/uLi8dnnq8i7GGCrpcOoEreZWns+ayquSZeYA840ODKzVUQyxTR4B ydE+r0NAJJH1xueEoMTHh5pVlXXBPfE= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=300P4uyDHmiPq/TQ4ACZID3NFloQxMDWfXSAQlSY+eU=; b=Bw1EYddsW8/Mygrlz56D6ktsTH /m6UmApFgB8SnEq4aDqghmvIxzT+pIr0N18gYQFFT/NAXEi1ZgBtaOpVnZe8eBwMvTLmhm1R4mcHD 7rK+602m/uzTrrRR7rk3rqah1NXeT/Y8e/HHX0Hf/4FBBG7vpHoqVEm4nRNv9GAiVmwggUE8a0PJs QIcO8eFMf6KcNrVQxEOwJ5K3fHX2TUpGnS2z78zFBYFjzkSlM6A3nYgFH/ig8ECCxwLdxeP9k3ory zWK6JWf0ROZlBSV1EVwsBOIHfq2LGPY3caSJqiUyBKg3zhohaepJef8sqe6I3HPZrzKVQaQyx2+c/ 0na/UZPA==; Received: from [2001:4bb8:182:6550:c70:4a89:bc61:3] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1rUSjx-0000000D6Qk-4BXT; Mon, 29 Jan 2024 14:36:02 +0000 From: Christoph Hellwig To: Chandan Babu R , "Darrick J. Wong" , Hugh Dickins , Andrew Morton Cc: linux-xfs@vger.kernel.org, linux-mm@kvack.org, Kent Overstreet Subject: [PATCH 16/20] xfs: add file_{get,put}_folio Date: Mon, 29 Jan 2024 15:34:58 +0100 Message-Id: <20240129143502.189370-17-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240129143502.189370-1-hch@lst.de> References: <20240129143502.189370-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html X-Rspamd-Queue-Id: 64981C000B X-Rspam-User: X-Stat-Signature: 9gc8sm1if4w4x4ou13rzen9mqy3u4znc X-Rspamd-Server: rspam01 X-HE-Tag: 1706538965-812400 X-HE-Meta: U2FsdGVkX1+Zx/vsu5oxo8aJrkmFk/3HW1wifGOYuu5FqEgYGw30ExJeVY7VyfXW6NXjaLVoSKfeBgq5ElCl902lEh8A8zxwcbGPyQGVtvozriauO797PweVuYx6Y1pvuJ8EbFw3MPlWilJH6uc/JzQtfhcgiadD82ofY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jZgrrLojwOqNIYjlao3A7ryFgvXMtA14L+VUT2xFm25tgH17L0K+xCUIbXpPoUY+HQZbY0VOV710caH/HJpkFswIlUKJBMNJstZMVcSBGwNVTQD4JC24RA8Tg03OYMXbA01vPArGAr7+tcVE9b6WNganuOPgoWT33kk94FU79H+R2tfthMgpoheEePpFdEQqU2IoEfuuHHkw8ka9K3DhRkDVKYmVxPac2L6qcUWXNOW3kgFRqI2GmsjVDPgL8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Darrick J. Wong" Add helper similar to file_{get,set}_page, but which deal with folios and don't allocate new folio unless explicitly asked to, which map to shmem_get_folio instead of calling into the aops. Signed-off-by: Darrick J. Wong Signed-off-by: Christoph Hellwig Reviewed-by: Kent Overstreet --- fs/xfs/scrub/trace.h | 2 ++ fs/xfs/scrub/xfile.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/xfile.h | 7 +++++ 3 files changed, 83 insertions(+) diff --git a/fs/xfs/scrub/trace.h b/fs/xfs/scrub/trace.h index 0327cab606b070..c61fa7a95ef522 100644 --- a/fs/xfs/scrub/trace.h +++ b/fs/xfs/scrub/trace.h @@ -908,6 +908,8 @@ DEFINE_XFILE_EVENT(xfile_store); DEFINE_XFILE_EVENT(xfile_seek_data); DEFINE_XFILE_EVENT(xfile_get_page); DEFINE_XFILE_EVENT(xfile_put_page); +DEFINE_XFILE_EVENT(xfile_get_folio); +DEFINE_XFILE_EVENT(xfile_put_folio); TRACE_EVENT(xfarray_create, TP_PROTO(struct xfarray *xfa, unsigned long long required_capacity), diff --git a/fs/xfs/scrub/xfile.c b/fs/xfs/scrub/xfile.c index c524fe9980fea2..2d59407909de35 100644 --- a/fs/xfs/scrub/xfile.c +++ b/fs/xfs/scrub/xfile.c @@ -340,3 +340,77 @@ xfile_put_page( return -EIO; return 0; } + +/* + * Grab the (locked) folio for a memory object. The object cannot span a folio + * boundary. Returns the locked folio if successful, NULL if there was no + * folio or it didn't cover the range requested, or an ERR_PTR on failure. + */ +struct folio * +xfile_get_folio( + struct xfile *xf, + loff_t pos, + size_t len, + unsigned int flags) +{ + struct inode *inode = file_inode(xf->file); + struct folio *folio = NULL; + unsigned int pflags; + int error; + + if (inode->i_sb->s_maxbytes - pos < len) + return ERR_PTR(-ENOMEM); + + trace_xfile_get_folio(xf, pos, len); + + /* + * Increase the file size first so that shmem_get_folio(..., SGP_CACHE), + * actually allocates a folio instead of erroring out. + */ + if ((flags & XFILE_ALLOC) && pos + len > i_size_read(inode)) + i_size_write(inode, pos + len); + + pflags = memalloc_nofs_save(); + error = shmem_get_folio(inode, pos >> PAGE_SHIFT, &folio, + (flags & XFILE_ALLOC) ? SGP_CACHE : SGP_READ); + memalloc_nofs_restore(pflags); + if (error) + return ERR_PTR(error); + + if (!folio) + return NULL; + + if (len > folio_size(folio) - offset_in_folio(folio, pos)) { + folio_unlock(folio); + folio_put(folio); + return NULL; + } + + if (filemap_check_wb_err(inode->i_mapping, 0)) { + folio_unlock(folio); + folio_put(folio); + return ERR_PTR(-EIO); + } + + /* + * Mark the folio dirty so that it won't be reclaimed once we drop the + * (potentially last) reference in xfile_put_folio. + */ + if (flags & XFILE_ALLOC) + folio_set_dirty(folio); + return folio; +} + +/* + * Release the (locked) folio for a memory object. + */ +void +xfile_put_folio( + struct xfile *xf, + struct folio *folio) +{ + trace_xfile_put_folio(xf, folio_pos(folio), folio_size(folio)); + + folio_unlock(folio); + folio_put(folio); +} diff --git a/fs/xfs/scrub/xfile.h b/fs/xfs/scrub/xfile.h index 465b10f492b66d..afb75e9fbaf265 100644 --- a/fs/xfs/scrub/xfile.h +++ b/fs/xfs/scrub/xfile.h @@ -39,4 +39,11 @@ int xfile_get_page(struct xfile *xf, loff_t offset, unsigned int len, struct xfile_page *xbuf); int xfile_put_page(struct xfile *xf, struct xfile_page *xbuf); +#define XFILE_MAX_FOLIO_SIZE (PAGE_SIZE << MAX_PAGECACHE_ORDER) + +#define XFILE_ALLOC (1 << 0) /* allocate folio if not present */ +struct folio *xfile_get_folio(struct xfile *xf, loff_t offset, size_t len, + unsigned int flags); +void xfile_put_folio(struct xfile *xf, struct folio *folio); + #endif /* __XFS_SCRUB_XFILE_H__ */