From patchwork Fri Mar 1 16:44:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pankaj Raghav (Samsung)" X-Patchwork-Id: 13578740 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 D096BC5478C for ; Fri, 1 Mar 2024 16:45:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 645C96B0083; Fri, 1 Mar 2024 11:45:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5CDC76B0085; Fri, 1 Mar 2024 11:45:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42A7B6B0087; Fri, 1 Mar 2024 11:45:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2A1726B0083 for ; Fri, 1 Mar 2024 11:45:01 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EB86912033A for ; Fri, 1 Mar 2024 16:45:00 +0000 (UTC) X-FDA: 81849044760.10.C91FB55 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by imf16.hostedemail.com (Postfix) with ESMTP id 4EBE8180017 for ; Fri, 1 Mar 2024 16:44:59 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=bL8wu3wN; spf=pass (imf16.hostedemail.com: domain of me@pankajraghav.com designates 80.241.56.172 as permitted sender) smtp.mailfrom=me@pankajraghav.com; dmarc=pass (policy=quarantine) header.from=pankajraghav.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709311499; 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=1cELEozXOYCd1Af2ZXfddDuv3Odm1CWuSBqyYDa15DQ=; b=3lqkQ6fab48S4+OGcMbwJ+Azr+XOg4K9RoHb6tXXZkZgUBX7XP85v6z2qV0QJ/TLUDXeDA 6qIRB/AgSyK6tXsGiPLUclmpudQJrwIUsvPuBbgZNK8ulNi28b0OCWOh/oSKu/KYt0LDxv 2FcHwexvIGDLfMyZliC7MLhsLCr0epw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709311499; a=rsa-sha256; cv=none; b=i6t0+uPE1qbGb814mLTajSFFuYvsT10YqTQW4lzo8zPSjLjpEwBcbjWl+fGfKFTCc/AxML DJp0hNAaN30AT+8zzGuPltFaR2WKG8j7sNfqdk/FiE0WKOxiSpmj3g+ekfcdej2iGReZIj xbAzJnsk/9beQuQ2rzuJlsqy/hEz0xA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=bL8wu3wN; spf=pass (imf16.hostedemail.com: domain of me@pankajraghav.com designates 80.241.56.172 as permitted sender) smtp.mailfrom=me@pankajraghav.com; dmarc=pass (policy=quarantine) header.from=pankajraghav.com Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4TmYpv5Blmz9tW4; Fri, 1 Mar 2024 17:44:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1709311495; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1cELEozXOYCd1Af2ZXfddDuv3Odm1CWuSBqyYDa15DQ=; b=bL8wu3wNKf1pmc8SzUQPy23UfUZ0BwEQ8+fDKjHUH8N+j2+VBUy1OSkewMRkC5mKZ/EWvp rQuqv1J8e6Vp3ePcwqreNd8Np8dNup0i92iIQy96VkqcjsyAawLjiJLYav4gsGGdCUNmcA 2lGI++nSQTqWERFoQA7n7WWLsY9t5UfQJiaX3aM0PHMZ1pJ+ckJtG93Tf9a5Cm/n06wnT9 r0yuN/PlrhcScbNCXn5iUOCg5Nt2ipmOpo8lK8BVQhqzgMJWClHr3TjBA7+wIS08h+1l3a s22J69b1C9/Y8dEvw0GLItEqLOMZw1U5cE+ygJdpzWGZ+pPT5Kpw6pzwE1ZHZA== From: "Pankaj Raghav (Samsung)" To: linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org Cc: djwong@kernel.org, mcgrof@kernel.org, linux-mm@kvack.org, hare@suse.de, david@fromorbit.com, akpm@linux-foundation.org, gost.dev@samsung.com, linux-kernel@vger.kernel.org, chandan.babu@oracle.com, willy@infradead.org, Pankaj Raghav Subject: [PATCH v2 02/13] fs: Allow fine-grained control of folio sizes Date: Fri, 1 Mar 2024 17:44:33 +0100 Message-ID: <20240301164444.3799288-3-kernel@pankajraghav.com> In-Reply-To: <20240301164444.3799288-1-kernel@pankajraghav.com> References: <20240301164444.3799288-1-kernel@pankajraghav.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4EBE8180017 X-Rspam-User: X-Stat-Signature: 7ndhj4zw5et4ufpph8wre9ry8d4epcyg X-Rspamd-Server: rspam03 X-HE-Tag: 1709311499-352479 X-HE-Meta: U2FsdGVkX1+T5QCAcTAeLXQqo9u1NDyHd2gfl+LENTv7JzWk3NFtRDIeEskPfXPBpXB38O75TVoL5Zib/QX20kpLRTYqTFDIjsNqEEWDppKYT0kGU8zixPV3Q8NWdP+HWBkuupVcrhQrlvnlJuAntx4/EkV9qihUCN9bnCyM/VO7PV+qdBmBJGhk2SecsEKUG1MAnqlhpEQpyfaijyIK5gy9iC2ge2ji3q2bhFeYIHYdhNOqvHbntGq8XwqBKXRrqbFVjo0wd3yeAzaS3eFxJD2/blRZlBoKN48DzVfky0LVOIIbt7uqy2B7jGd+FwMvWkfNHIdAm+FKeUz+dmyjscnFHXX5goONbfFS6G2K7p4E6xGNL3PHxaQb02F8/VCFqihHQg4lDNmiRo/M+PggLTCQMy0I0Pzs/djghOyHqRgomoG8GVMA7XsUjPZ4tsA+NIc1bDwHnhy6k16rIlGpgHg+1bec0tBecSia9kzH6y9yI9BRk+sf/6+YOvt7rixtjjBIIxso7HQxyPCSDrGdtZZ5n7vyL1ZFR0TJGNaHd9yKORhJLMzPyXIoONztWNe8L04ijOaQnIzBVH5IeEC0ORFGtRdNO5+5sR14WULeU9S5QIrUAUYjStDVgrYaPReY/S+K/sk95nNQVQf2q56xrs5lQI6rFa7zDjfMZC5kCpgilpgZ5TJYhOqfNajCn9ua6Q0gnaWjjI6v7IT2hAMAaUYrbGclOe+VjqODk1b8ewoZ//ecj9bF500WVIp4fwsGYBWRG1bbuVIm+UZj5M8hsSbd39Wr2HoeGtmvxxdZoCvfZPn7fr8VHy3S1WD1ttqVdFaRQhzyih4ac/vTmZ+EHq3YYFWx9l1gSVTHd5ESoa/ls1H+cLozX24di4K6i5ih4R26UZ91bkJxOWdINwWXseEdPWheXQFTXJD0gAucpFwWKcKjzaorH8YtpsnfrNq5XR+e8AB2B8T73nIzOSh ViiszYjC JhtoejBH40aRW9tX0uMfuyq81pSOyy46MJwpVCXT/AcUf/AmvIUWr1HELMIcXbU9sRSLF010ccJQxAWgDDXlfPGEZxBvegU/I9Wnv 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: "Matthew Wilcox (Oracle)" Some filesystems want to be able to ensure that folios that are added to the page cache are at least a certain size. Add mapping_set_folio_min_order() to allow this level of control. Signed-off-by: Matthew Wilcox (Oracle) Co-developed-by: Pankaj Raghav Signed-off-by: Pankaj Raghav Signed-off-by: Luis Chamberlain Reviewed-by: Hannes Reinecke --- include/linux/pagemap.h | 100 ++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 20 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 2df35e65557d..fc8eb9c94e9c 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -202,13 +202,18 @@ enum mapping_flags { AS_EXITING = 4, /* final truncate in progress */ /* writeback related tags are not used */ AS_NO_WRITEBACK_TAGS = 5, - AS_LARGE_FOLIO_SUPPORT = 6, - AS_RELEASE_ALWAYS, /* Call ->release_folio(), even if no private data */ - AS_STABLE_WRITES, /* must wait for writeback before modifying + AS_RELEASE_ALWAYS = 6, /* Call ->release_folio(), even if no private data */ + AS_STABLE_WRITES = 7, /* must wait for writeback before modifying folio contents */ - AS_UNMOVABLE, /* The mapping cannot be moved, ever */ + AS_FOLIO_ORDER_MIN = 8, + AS_FOLIO_ORDER_MAX = 13, /* Bit 8-17 are used for FOLIO_ORDER */ + AS_UNMOVABLE = 18, /* The mapping cannot be moved, ever */ }; +#define AS_FOLIO_ORDER_MIN_MASK 0x00001f00 +#define AS_FOLIO_ORDER_MAX_MASK 0x0003e000 +#define AS_FOLIO_ORDER_MASK (AS_FOLIO_ORDER_MIN_MASK | AS_FOLIO_ORDER_MAX_MASK) + /** * mapping_set_error - record a writeback error in the address_space * @mapping: the mapping in which an error should be set @@ -344,9 +349,47 @@ static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask) m->gfp_mask = mask; } +/* + * There are some parts of the kernel which assume that PMD entries + * are exactly HPAGE_PMD_ORDER. Those should be fixed, but until then, + * limit the maximum allocation order to PMD size. I'm not aware of any + * assumptions about maximum order if THP are disabled, but 8 seems like + * a good order (that's 1MB if you're using 4kB pages) + */ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +#define MAX_PAGECACHE_ORDER HPAGE_PMD_ORDER +#else +#define MAX_PAGECACHE_ORDER 8 +#endif + +/* + * mapping_set_folio_min_order() - Set the minimum folio order + * @mapping: The address_space. + * @min: Minimum folio order (between 0-MAX_PAGECACHE_ORDER inclusive). + * + * The filesystem should call this function in its inode constructor to + * indicate which base size of folio the VFS can use to cache the contents + * of the file. This should only be used if the filesystem needs special + * handling of folio sizes (ie there is something the core cannot know). + * Do not tune it based on, eg, i_size. + * + * Context: This should not be called while the inode is active as it + * is non-atomic. + */ +static inline void mapping_set_folio_min_order(struct address_space *mapping, + unsigned int min) +{ + if (min > MAX_PAGECACHE_ORDER) + min = MAX_PAGECACHE_ORDER; + + mapping->flags = (mapping->flags & ~AS_FOLIO_ORDER_MASK) | + (min << AS_FOLIO_ORDER_MIN) | + (MAX_PAGECACHE_ORDER << AS_FOLIO_ORDER_MAX); +} + /** * mapping_set_large_folios() - Indicate the file supports large folios. - * @mapping: The file. + * @mapping: The address_space. * * The filesystem should call this function in its inode constructor to * indicate that the VFS can use large folios to cache the contents of @@ -357,7 +400,37 @@ static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask) */ static inline void mapping_set_large_folios(struct address_space *mapping) { - __set_bit(AS_LARGE_FOLIO_SUPPORT, &mapping->flags); + mapping_set_folio_min_order(mapping, 0); +} + +static inline unsigned int mapping_max_folio_order(struct address_space *mapping) +{ + return (mapping->flags & AS_FOLIO_ORDER_MAX_MASK) >> AS_FOLIO_ORDER_MAX; +} + +static inline unsigned int mapping_min_folio_order(struct address_space *mapping) +{ + return (mapping->flags & AS_FOLIO_ORDER_MIN_MASK) >> AS_FOLIO_ORDER_MIN; +} + +static inline unsigned long mapping_min_folio_nrpages(struct address_space *mapping) +{ + return 1UL << mapping_min_folio_order(mapping); +} + +/** + * mapping_align_start_index() - Align starting index based on the min + * folio order of the page cache. + * @mapping: The address_space. + * + * Ensure the index used is aligned to the minimum folio order when adding + * new folios to the page cache by rounding down to the nearest minimum + * folio number of pages. + */ +static inline pgoff_t mapping_align_start_index(struct address_space *mapping, + pgoff_t index) +{ + return round_down(index, mapping_min_folio_nrpages(mapping)); } /* @@ -367,7 +440,7 @@ static inline void mapping_set_large_folios(struct address_space *mapping) static inline bool mapping_large_folio_support(struct address_space *mapping) { return IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && - test_bit(AS_LARGE_FOLIO_SUPPORT, &mapping->flags); + (mapping_max_folio_order(mapping) > 0); } static inline int filemap_nr_thps(struct address_space *mapping) @@ -528,19 +601,6 @@ static inline void *detach_page_private(struct page *page) return folio_detach_private(page_folio(page)); } -/* - * There are some parts of the kernel which assume that PMD entries - * are exactly HPAGE_PMD_ORDER. Those should be fixed, but until then, - * limit the maximum allocation order to PMD size. I'm not aware of any - * assumptions about maximum order if THP are disabled, but 8 seems like - * a good order (that's 1MB if you're using 4kB pages) - */ -#ifdef CONFIG_TRANSPARENT_HUGEPAGE -#define MAX_PAGECACHE_ORDER HPAGE_PMD_ORDER -#else -#define MAX_PAGECACHE_ORDER 8 -#endif - #ifdef CONFIG_NUMA struct folio *filemap_alloc_folio(gfp_t gfp, unsigned int order); #else