From patchwork Thu Feb 15 14:55:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Kellermann X-Patchwork-Id: 13558589 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 17E76C48BEB for ; Thu, 15 Feb 2024 14:56:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DC6C8D0027; Thu, 15 Feb 2024 09:56:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 166C78D0029; Thu, 15 Feb 2024 09:56:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED18C8D0027; Thu, 15 Feb 2024 09:56:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CF7218D0029 for ; Thu, 15 Feb 2024 09:56:22 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 93B2DC060F for ; Thu, 15 Feb 2024 14:56:22 +0000 (UTC) X-FDA: 81794339004.22.7FB563B Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by imf27.hostedemail.com (Postfix) with ESMTP id AE7CD4001D for ; Thu, 15 Feb 2024 14:56:20 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=ionos.com header.s=google header.b=E1lV30+T; spf=pass (imf27.hostedemail.com: domain of max.kellermann@ionos.com designates 209.85.218.48 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=1708008980; 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=pAAfduKncyIlO7bGb0H7B4KC/vDoZlMcbrTq+YqBvns=; b=Ej9vRO6yHDplmnMM49ZHOB7rfWlrgdQ5wfg8EIc8mXMic+hKJHVafv8ZYvAb2l7c0mUfkk 7JkJW6zso7/doc2gam7qjV1oZHF4Ycv4SF8q26cMVCc00ThB+ymBfwkGXY+GHFaavs/Jet PmIbmz8S32GuELB/3SJCbv1328C25J8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=ionos.com header.s=google header.b=E1lV30+T; spf=pass (imf27.hostedemail.com: domain of max.kellermann@ionos.com designates 209.85.218.48 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=1708008980; a=rsa-sha256; cv=none; b=eAO4NC/0NBGuBTnKK7OocpBMueh/fPuHaTOqaDi4kmLPmMjcRDnaDxUGAdK5CmdxS8cQZP X3DmGwfpc9cAeQyFxesNIVqCaV+4X3xaJDug7PjGv+DiV9Zws/3xzzY27JFrTOlOevyang rOTIQV0ARypHU1OKHpwsQjZUpwyw/0w= Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-a2d7e2e7fe0so184995266b.1 for ; Thu, 15 Feb 2024 06:56:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1708008979; x=1708613779; 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=pAAfduKncyIlO7bGb0H7B4KC/vDoZlMcbrTq+YqBvns=; b=E1lV30+TlJnMPsudGKKZDLgec6+laGjTRU63f+BxdNehwDHjTcMjjulLdGYDlEFYLJ Ew24i8lNb+tslXE7vSYi8Pj8bBOwzIfVQzu5WUi1+dhNHmxiJopu7miDQTJAcWfuyRBJ wR8ard7X4rAo2hwH058OyRs2p4gps6p3f4Phf/LqIBnN5D1LcQQvvBKAdy3qu7bXcJFv d8f4AOsToapXKQSpNlxUqnhE/0CUh+IOjRS8eeTiSztstoycrhuVlhpbhLTlJ5ls7TXG qQ4b8uHpClJG1fQdUnqMk9NTWvsMihsLccRubGUDZnEyW2nrGIkVcGteHpgAJgp65eJo 2A8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708008979; x=1708613779; 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=pAAfduKncyIlO7bGb0H7B4KC/vDoZlMcbrTq+YqBvns=; b=hW9Kx69qU3GoLIT96ruvuhuJqDVUeojXGaKWoerHyGrpGHZWa8huA5HznCqTtgKEO6 E/7uKy0U3j4pKbSE9iTAwtfRZ4tXr+luD93hklcHKw3M1Ha4iA3ETDIogO4B0mqRTcS9 DqXC+34nHGu6C7A33cRdfG2kljv5X0xtT9mx7zMTtxRWBZUJ13XQAr1oU7vrGWjxTnoz nmfy61t4bze4eBvItLClrKBTrBCOWlYVgmA6DgSvWT1mUFmkRE6ls466eAhpUKGZaVlR TfrBHRNkz5tJ5wScKNL9GCQ0lnbrlI+DeVSIapcNfVSwd6QAm/8seuawO8JHGqFjzSpp R8rg== X-Forwarded-Encrypted: i=1; AJvYcCWUrb8n2x8x3oRH0i0eR7u8QYyM2Z7BkIRoxsMUZP8KFLCH7gK5qkCKACs3DE6tz78zzOUta4veJ6wTDgsF8F+6fgE= X-Gm-Message-State: AOJu0YwnzeKYwX7hoMrW6whwo36msvQj3uacQkgVemGCLA3rOLOQ1Y8K mxRe/6hguAbTfFxp0Osb9rdyLP8YEEZT2iIzoyGeGXcKRsEXPLMs4odP5gvgJmXip0cqdXBy8ud T X-Google-Smtp-Source: AGHT+IGigQj7ZTNu10ittsrAOmrJ2ArDckgllGOZzKGW4cyupgYSflwee8ZSNNYlHtvPBt42rJjGYg== X-Received: by 2002:a17:906:3e11:b0:a3c:2c8e:9030 with SMTP id k17-20020a1709063e1100b00a3c2c8e9030mr4707897eji.6.1708008979244; Thu, 15 Feb 2024 06:56:19 -0800 (PST) Received: from raven.intern.cm-ag (p200300dc6f267100023064fffe740809.dip0.t-ipconnect.de. [2003:dc:6f26:7100:230:64ff:fe74:809]) by smtp.gmail.com with ESMTPSA id s18-20020a170906169200b00a3d1897ab68sm631175ejd.113.2024.02.15.06.56.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 06:56:19 -0800 (PST) From: Max Kellermann To: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Max Kellermann Subject: [PATCH v1 09/14] linux/mm.h: move usage count functions to mm/page_usage.h Date: Thu, 15 Feb 2024 15:55:57 +0100 Message-Id: <20240215145602.1371274-10-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240215145602.1371274-1-max.kellermann@ionos.com> References: <20240215145602.1371274-1-max.kellermann@ionos.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AE7CD4001D X-Rspam-User: X-Stat-Signature: rab6pf6jfg5zea9caoyp58b54c8qgsir X-Rspamd-Server: rspam01 X-HE-Tag: 1708008980-202860 X-HE-Meta: U2FsdGVkX1+RczGMSPYsyWZZ/KWF8gNdl6nSI8Iw9Igwk+S/24Vc4fcWi3HcLpKsL5sETGPp4eAaynAs3LmyjU4bY46x42UMgw2BN5ZhjBxL2zUADOOUcJbHo2LP1mtoF/BfvrwlLrrJC++clTZlMGhoyurkGYONlzYZ+R4Bzc+MrsU7Olr+opy5OM2uL68tYfwHxdtz+lsxYEjyljm5vlO9l7o8DxnkX4b2IsTTr7kEee7bM+1qneZv2+BLsrqY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3DFv4NRPgZrz76bnX7WTfxTF5TRkCmuodSKoIGZnNw1k6j/nIfIxHT4ruRVbo6BltZvjwNxeRZg5ePutqMjuX//I= 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 in linux/mm.h. Signed-off-by: Max Kellermann --- include/linux/mm.h | 168 +------------------------------- include/linux/mm/page_usage.h | 177 ++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 167 deletions(-) create mode 100644 include/linux/mm/page_usage.h diff --git a/include/linux/mm.h b/include/linux/mm.h index cb2dd71e2940..26d506ed855f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -1053,51 +1054,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 { @@ -1253,8 +1209,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); @@ -1345,126 +1299,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); -} - -/* - * 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. - * @nr: How many folios there are. - * - * Like folio_put(), but for an array 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. - * - * 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 **folios, unsigned int nr) -{ - release_pages(folios, nr); -} - -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/page_usage.h b/include/linux/mm/page_usage.h new file mode 100644 index 000000000000..53015e28b5df --- /dev/null +++ b/include/linux/mm/page_usage.h @@ -0,0 +1,177 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MM_PAGE_USAGE_H +#define _LINUX_MM_PAGE_USAGE_H + +#include // for put_devmap_managed_page() +#include // for VM_BUG_ON_PAGE() +#include // for struct folio +#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; +} + +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); +} + +/* + * 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. + * @nr: How many folios there are. + * + * Like folio_put(), but for an array 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. + * + * 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 **folios, unsigned int nr) +{ + release_pages(folios, nr); +} + +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_PAGE_USAGE_H */