From patchwork Tue Mar 5 08:59:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Kellermann X-Patchwork-Id: 13581863 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 CE80EC54E41 for ; Tue, 5 Mar 2024 09:00:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D1646B009E; Tue, 5 Mar 2024 03:59:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 93F83940007; Tue, 5 Mar 2024 03:59:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A4ED6B00A0; Tue, 5 Mar 2024 03:59:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4D9A46B009D for ; Tue, 5 Mar 2024 03:59:49 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CA796120DBF for ; Tue, 5 Mar 2024 08:59:48 +0000 (UTC) X-FDA: 81862387656.22.47A07BC Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) by imf28.hostedemail.com (Postfix) with ESMTP id DDE22C000C for ; Tue, 5 Mar 2024 08:59:46 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=ionos.com header.s=google header.b=RZDczbC9; spf=pass (imf28.hostedemail.com: domain of max.kellermann@ionos.com designates 209.85.167.49 as permitted sender) smtp.mailfrom=max.kellermann@ionos.com; dmarc=pass (policy=quarantine) header.from=ionos.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709629187; 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=cowoIpweWt3L2bjBHT1kUeTpunMOhbiTNwgqFnKsuAs=; b=UmdN7ByoORBw54yMpAYeCSiu+ltZqqHSssYLZ9eSDNe6Sej1KfxplBbFzHO5S8odD2gUdj Wn8FWiNsmZJLJPc28v3sdVZ8g3X4XEX2ZURn51qLQfbWS6UuLpG7/YSY/YDr12ojQSEcQB youmGuML7SXwCJyIKENihmoWbNRPfvk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=ionos.com header.s=google header.b=RZDczbC9; spf=pass (imf28.hostedemail.com: domain of max.kellermann@ionos.com designates 209.85.167.49 as permitted sender) smtp.mailfrom=max.kellermann@ionos.com; dmarc=pass (policy=quarantine) header.from=ionos.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709629187; a=rsa-sha256; cv=none; b=7ArU5jHt2shdAzaiJHLjPxcsxXtrvuLtvnGZVs7hkE94gHNSvQISssX2vxFxKbWwYaTqb4 YBm3x9xDR1j4fM/0Ug4VaK5jTaRWywHi+qpfZbw92DKAFq79+bOZZL6E2pb67aeAhhWcks 83YZ7vEGjsuIhTLyRBxde+2DRojmI1U= Received: by mail-lf1-f49.google.com with SMTP id 2adb3069b0e04-512b3b04995so4020012e87.3 for ; Tue, 05 Mar 2024 00:59:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1709629185; x=1710233985; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cowoIpweWt3L2bjBHT1kUeTpunMOhbiTNwgqFnKsuAs=; b=RZDczbC9CJ+eSQLFIyXKAAHdC2Xahpe1xWXCm9y4On+Uarbq9bpb/yd1WS8omTyOKn bJuLq+hCBoy5rDv7Lsd5KkOhwJ9k48uJvuivLVwgLjUC+MZiUv1dmJu/Jubbea8nAEAd 1fG/f7T+KC9H7g4vO3m9LwMEEyM0QJDVH42IafGZrcRonzOvNMPbyDKkdjGm7laDmmhl YzQVF4McoqvFlcT7NnLFO0dYrDjMqN9itjZnrrlvj3beKp74POaHcVto8IEiZNcBxAIH /i6vDmO11HAiaf/pX4r6T/QKPtL7KhxP1RNl1PRPjcIE17Q2CDb9W983IsjJIZOJgpuG wsfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709629185; x=1710233985; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cowoIpweWt3L2bjBHT1kUeTpunMOhbiTNwgqFnKsuAs=; b=NiIwf4L0hUotYLjekBAA6bYM8BdEJ1SfCEl7eRNYgABl3HqrUkjyhJGGHwlH228UOw uboXf9ViRadfdFl4V1TvM6/fwdaN4Px38DuyGwVGaz9cTQU8gHZfS+XLe7jrsQxL0K2o 3IPkZKlrYgFxfZ8baxicIAlgWPR9jR03igYPnsXvdn1MJfMVtxRrAJnmKsOc+wCjIIhd 4bXSAOoEO/g36mLm0a7idh7Mfu8j14T737sMGCHQA+e+NIrl+WsdnV7NrCfszX7ApDMh bd3vi0VA0PMya8pJRom9T/MBMwFolYAdl+grbc5TCe/yZ2QlKhqvA52olCC6euf88d8L Jznw== X-Forwarded-Encrypted: i=1; AJvYcCWA4HqAiouFJRx89A4SbuJ58fgYtNJKSmgtwmY3Xt6Fxp7eslC5TcwmtcY5ixyxztNAIleTkNeG7KWgUMjiHqF0Fco= X-Gm-Message-State: AOJu0YzNYoveV645UfWRJPMpPoJvfuQpTexezMA5MoDOu9uAnBdLCMyv 2H7fToVcGKgT6nQ8mFGMutckVAv4eH+8a6cxC6+sBIepkCi+udSydxUYxGurzdI= X-Google-Smtp-Source: AGHT+IGKngCTmPmeb81IUjSVUV+bTdylSFgdpzvBNEX+ZfYgXiwTu8YQIgQN2zwEsxNYEM8vJK3Vuw== X-Received: by 2002:a05:6512:1152:b0:513:5203:e255 with SMTP id m18-20020a056512115200b005135203e255mr793839lfg.7.1709629184681; Tue, 05 Mar 2024 00:59:44 -0800 (PST) Received: from raven.blarg.de (p200300dc6f010900023064fffe740809.dip0.t-ipconnect.de. [2003:dc:6f01:900:230:64ff:fe74:809]) by smtp.gmail.com with ESMTPSA id gs4-20020a170906f18400b00a449d12cdc5sm4453005ejb.119.2024.03.05.00.59.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 00:59:44 -0800 (PST) From: Max Kellermann To: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: willy@infradead.org, sfr@canb.auug.org.au, Max Kellermann Subject: [PATCH v3 09/14] linux/mm.h: move usage count functions to mm/folio_usage.h Date: Tue, 5 Mar 2024 09:59:14 +0100 Message-Id: <20240305085919.1601395-10-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240305085919.1601395-1-max.kellermann@ionos.com> References: <20240305085919.1601395-1-max.kellermann@ionos.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: DDE22C000C X-Rspam-User: X-Stat-Signature: yxddjo1t96uoi97meej3f8b4tphtm77f X-Rspamd-Server: rspam01 X-HE-Tag: 1709629186-289838 X-HE-Meta: U2FsdGVkX1/0i7MdlCXbUOIB6mW9GGzjNd1JvAWAngq7FRdgadf645tKBINeDz95ko2uoqfuugQhGnv+dYYE2EHByCAtzB/r70r/NyMOe+MRj0+FpTTuYvLiqEqjCsSUmYE4BKCX8J1FtMBrrmYIrY9LFEKoABShht1KY/SmYorjhenTqbrtZo1OXvl3Z22e9oBl0cQJWQSL0GMVCAtpQhwm09iCUA3WNABJygKbTd0NxxaspMrzDfMTiLXfD1lvMlaSjdDQ16oVdgxOjJJ+I7Giyoj2RkIQjBTx2U2tM1kmG8spJw82CqqlDvE2jx8qsfKj1rZpT/9v2cY6EtB/Gea667py0EOYQ5QI5I9lxOjNorD4Nm8uHR9M375a+OlBHSG6Ey646izp9g8q0O5brBXDOWjnH46GbvFgAOK/8K+miBi2cCdZ86Fzhbdwxiy8EqtGDxnv+mrUCSmJwtDqUI2Ov6G5lazKxVV7/sSu6+yfRIXVQs+tJymamOVhXKzMwHdiGFQ49jWfLDFw5xkeyKkwvCQCGRanoRc3Ga361/xzSKCiaXTR9T6GqTMCS17+3lzNF4+/szwvLjauGa5UJhu1D5O59gHjPIQbUKt1WOZB+4OcOBp3AOEn4DRAGjzndId+QHwRh+Z4/H1DjcBNl96NcsDyvuEtH7RhsP5VJGnzDJJGi4tsjsIl4hYnfcavOBJ0AGpptl0URXdTyWynkQ6lallOho7+DZJGotkdqx0gOZuXNuSW7ZAcwrjwDAybLpM8BhPhVY/jhV8t4xHKEb5VRNI3bTdw2A38+UGsIyJ9Cf0zwaoJpL07jrSQvld4O0xMISFGPv1pUm507FBcGavqhfS1PQ2gX0bgAzdNvVrfdPDlxaJaz5agT16xNEXpMdgyNzEAKYACQ7YKEjBB2rWDA3LoluGuxFG7P5nsqctg7KB6G+AjkNBr889V1LuYGZcLIb3YSM87tueRh6M xwASDKBk R+1EOtxD9tpxlT5jicKdn/wyRxneq7dUu9yl3CfvpVsc3IxfKn4zkQgCuev5H26GNlCagDm52pSBUUxASz0QPplU/ojzr3Bgl8tNxq8VgZtEAzXLMnS5P7oc2LAWzAB+4cBK8W6+q8v9ULxkG/1BTWJgNijpMPw96Zc2a 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: Prepare to reduce dependencies on linux/mm.h. This new header contains wrappers for the low-level functions from page_ref.h. By having those higher-level functions in a separate header, we can avoid their additional dependencies in the page_ref.h. Having these in a separate header will allow eliminating the dependency on linux/mm.h from these headers: - linux/skbuff.h - linux/swap.h Signed-off-by: Max Kellermann --- include/linux/mm.h | 172 +------------------------------ include/linux/mm/folio_usage.h | 182 +++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+), 171 deletions(-) create mode 100644 include/linux/mm/folio_usage.h diff --git a/include/linux/mm.h b/include/linux/mm.h index 61f1312a626e..e8a914e7bebd 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2,9 +2,9 @@ #ifndef _LINUX_MM_H #define _LINUX_MM_H -#include #include #include +#include #include #include #include @@ -1073,51 +1073,6 @@ struct inode; #include -/* - * Methods to modify the page usage count. - * - * What counts for a page usage: - * - cache mapping (page->mapping) - * - private data (page->private) - * - page mapped in a task's page tables, each mapping - * is counted separately - * - * Also, many kernel routines increase the page count before a critical - * routine so they can be sure the page doesn't go away from under them. - */ - -/* - * Drop a ref, return true if the refcount fell to zero (the page has no users) - */ -static inline int put_page_testzero(struct page *page) -{ - VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); - return page_ref_dec_and_test(page); -} - -static inline int folio_put_testzero(struct folio *folio) -{ - return put_page_testzero(&folio->page); -} - -/* - * Try to grab a ref unless the page has a refcount of zero, return false if - * that is the case. - * This can be called when MMU is off so it must not access - * any of the virtual mappings. - */ -static inline bool get_page_unless_zero(struct page *page) -{ - return page_ref_add_unless(page, 1, 0); -} - -static inline struct folio *folio_get_nontail_page(struct page *page) -{ - if (unlikely(!get_page_unless_zero(page))) - return NULL; - return (struct folio *)page; -} - extern int page_is_ram(unsigned long pfn); enum { @@ -1266,8 +1221,6 @@ static inline struct folio *virt_to_folio(const void *x) return page_folio(page); } -void __folio_put(struct folio *folio); - void put_pages_list(struct list_head *pages); void split_page(struct page *page, unsigned int order); @@ -1358,129 +1311,6 @@ vm_fault_t finish_fault(struct vm_fault *vmf); * back into memory. */ -/* 127: arbitrary random number, small enough to assemble well */ -#define folio_ref_zero_or_close_to_overflow(folio) \ - ((unsigned int) folio_ref_count(folio) + 127u <= 127u) - -/** - * folio_get - Increment the reference count on a folio. - * @folio: The folio. - * - * Context: May be called in any context, as long as you know that - * you have a refcount on the folio. If you do not already have one, - * folio_try_get() may be the right interface for you to use. - */ -static inline void folio_get(struct folio *folio) -{ - VM_BUG_ON_FOLIO(folio_ref_zero_or_close_to_overflow(folio), folio); - folio_ref_inc(folio); -} - -static inline void get_page(struct page *page) -{ - folio_get(page_folio(page)); -} - -static inline __must_check bool try_get_page(struct page *page) -{ - page = compound_head(page); - if (WARN_ON_ONCE(page_ref_count(page) <= 0)) - return false; - page_ref_inc(page); - return true; -} - -/** - * folio_put - Decrement the reference count on a folio. - * @folio: The folio. - * - * If the folio's reference count reaches zero, the memory will be - * released back to the page allocator and may be used by another - * allocation immediately. Do not access the memory or the struct folio - * after calling folio_put() unless you can be sure that it wasn't the - * last reference. - * - * Context: May be called in process or interrupt context, but not in NMI - * context. May be called while holding a spinlock. - */ -static inline void folio_put(struct folio *folio) -{ - if (folio_put_testzero(folio)) - __folio_put(folio); -} - -/** - * folio_put_refs - Reduce the reference count on a folio. - * @folio: The folio. - * @refs: The amount to subtract from the folio's reference count. - * - * If the folio's reference count reaches zero, the memory will be - * released back to the page allocator and may be used by another - * allocation immediately. Do not access the memory or the struct folio - * after calling folio_put_refs() unless you can be sure that these weren't - * the last references. - * - * Context: May be called in process or interrupt context, but not in NMI - * context. May be called while holding a spinlock. - */ -static inline void folio_put_refs(struct folio *folio, int refs) -{ - if (folio_ref_sub_and_test(folio, refs)) - __folio_put(folio); -} - -void folios_put_refs(struct folio_batch *folios, unsigned int *refs); - -/* - * union release_pages_arg - an array of pages or folios - * - * release_pages() releases a simple array of multiple pages, and - * accepts various different forms of said page array: either - * a regular old boring array of pages, an array of folios, or - * an array of encoded page pointers. - * - * The transparent union syntax for this kind of "any of these - * argument types" is all kinds of ugly, so look away. - */ -typedef union { - struct page **pages; - struct folio **folios; - struct encoded_page **encoded_pages; -} release_pages_arg __attribute__ ((__transparent_union__)); - -void release_pages(release_pages_arg, int nr); - -/** - * folios_put - Decrement the reference count on an array of folios. - * @folios: The folios. - * - * Like folio_put(), but for a batch of folios. This is more efficient - * than writing the loop yourself as it will optimise the locks which need - * to be taken if the folios are freed. The folios batch is returned - * empty and ready to be reused for another batch; there is no need to - * reinitialise it. - * - * Context: May be called in process or interrupt context, but not in NMI - * context. May be called while holding a spinlock. - */ -static inline void folios_put(struct folio_batch *folios) -{ - folios_put_refs(folios, NULL); -} - -static inline void put_page(struct page *page) -{ - struct folio *folio = page_folio(page); - - /* - * For some devmap managed pages we need to catch refcount transition - * from 2 to 1: - */ - if (put_devmap_managed_page(&folio->page)) - return; - folio_put(folio); -} - /* * GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload * the page's refcount so that two separate items are tracked: the original page diff --git a/include/linux/mm/folio_usage.h b/include/linux/mm/folio_usage.h new file mode 100644 index 000000000000..4a7e9cd74909 --- /dev/null +++ b/include/linux/mm/folio_usage.h @@ -0,0 +1,182 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MM_FOLIO_USAGE_H +#define _LINUX_MM_FOLIO_USAGE_H + +#include // for put_devmap_managed_page() +#include // for VM_BUG_ON_PAGE() +#include // for struct folio +#include + +struct folio_batch; + +/* + * Methods to modify the page usage count. + * + * What counts for a page usage: + * - cache mapping (page->mapping) + * - private data (page->private) + * - page mapped in a task's page tables, each mapping + * is counted separately + * + * Also, many kernel routines increase the page count before a critical + * routine so they can be sure the page doesn't go away from under them. + */ + +/* + * Drop a ref, return true if the refcount fell to zero (the page has no users) + */ +static inline int put_page_testzero(struct page *page) +{ + VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); + return page_ref_dec_and_test(page); +} + +static inline int folio_put_testzero(struct folio *folio) +{ + return put_page_testzero(&folio->page); +} + +/* + * Try to grab a ref unless the page has a refcount of zero, return false if + * that is the case. + * This can be called when MMU is off so it must not access + * any of the virtual mappings. + */ +static inline bool get_page_unless_zero(struct page *page) +{ + return page_ref_add_unless(page, 1, 0); +} + +static inline struct folio *folio_get_nontail_page(struct page *page) +{ + if (unlikely(!get_page_unless_zero(page))) + return NULL; + return (struct folio *)page; +} + +void __folio_put(struct folio *folio); + +/* 127: arbitrary random number, small enough to assemble well */ +#define folio_ref_zero_or_close_to_overflow(folio) \ + ((unsigned int) folio_ref_count(folio) + 127u <= 127u) + +/** + * folio_get - Increment the reference count on a folio. + * @folio: The folio. + * + * Context: May be called in any context, as long as you know that + * you have a refcount on the folio. If you do not already have one, + * folio_try_get() may be the right interface for you to use. + */ +static inline void folio_get(struct folio *folio) +{ + VM_BUG_ON_FOLIO(folio_ref_zero_or_close_to_overflow(folio), folio); + folio_ref_inc(folio); +} + +static inline void get_page(struct page *page) +{ + folio_get(page_folio(page)); +} + +static inline __must_check bool try_get_page(struct page *page) +{ + page = compound_head(page); + if (WARN_ON_ONCE(page_ref_count(page) <= 0)) + return false; + page_ref_inc(page); + return true; +} + +/** + * folio_put - Decrement the reference count on a folio. + * @folio: The folio. + * + * If the folio's reference count reaches zero, the memory will be + * released back to the page allocator and may be used by another + * allocation immediately. Do not access the memory or the struct folio + * after calling folio_put() unless you can be sure that it wasn't the + * last reference. + * + * Context: May be called in process or interrupt context, but not in NMI + * context. May be called while holding a spinlock. + */ +static inline void folio_put(struct folio *folio) +{ + if (folio_put_testzero(folio)) + __folio_put(folio); +} + +/** + * folio_put_refs - Reduce the reference count on a folio. + * @folio: The folio. + * @refs: The amount to subtract from the folio's reference count. + * + * If the folio's reference count reaches zero, the memory will be + * released back to the page allocator and may be used by another + * allocation immediately. Do not access the memory or the struct folio + * after calling folio_put_refs() unless you can be sure that these weren't + * the last references. + * + * Context: May be called in process or interrupt context, but not in NMI + * context. May be called while holding a spinlock. + */ +static inline void folio_put_refs(struct folio *folio, int refs) +{ + if (folio_ref_sub_and_test(folio, refs)) + __folio_put(folio); +} + +void folios_put_refs(struct folio_batch *folios, unsigned int *refs); + +/* + * union release_pages_arg - an array of pages or folios + * + * release_pages() releases a simple array of multiple pages, and + * accepts various different forms of said page array: either + * a regular old boring array of pages, an array of folios, or + * an array of encoded page pointers. + * + * The transparent union syntax for this kind of "any of these + * argument types" is all kinds of ugly, so look away. + */ +typedef union { + struct page **pages; + struct folio **folios; + struct encoded_page **encoded_pages; +} release_pages_arg __attribute__ ((__transparent_union__)); + +void release_pages(release_pages_arg, int nr); + +/** + * folios_put - Decrement the reference count on an array of folios. + * @folios: The folios. + * + * Like folio_put(), but for a batch of folios. This is more efficient + * than writing the loop yourself as it will optimise the locks which need + * to be taken if the folios are freed. The folios batch is returned + * empty and ready to be reused for another batch; there is no need to + * reinitialise it. + * + * Context: May be called in process or interrupt context, but not in NMI + * context. May be called while holding a spinlock. + */ +static inline void folios_put(struct folio_batch *folios) +{ + folios_put_refs(folios, NULL); +} + +static inline void put_page(struct page *page) +{ + struct folio *folio = page_folio(page); + + /* + * For some devmap managed pages we need to catch refcount transition + * from 2 to 1: + */ + if (put_devmap_managed_page(&folio->page)) + return; + folio_put(folio); +} + +#endif /* _LINUX_MM_FOLIO_USAGE_H */