From patchwork Thu Feb 22 11:50:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Kellermann X-Patchwork-Id: 13567170 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 967D9C54788 for ; Thu, 22 Feb 2024 11:51:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A836F6B0082; Thu, 22 Feb 2024 06:51:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E7FB6B0085; Thu, 22 Feb 2024 06:51:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7740C6B0087; Thu, 22 Feb 2024 06:51:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5C61F6B0082 for ; Thu, 22 Feb 2024 06:51:14 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2F2D940DB3 for ; Thu, 22 Feb 2024 11:51:14 +0000 (UTC) X-FDA: 81819274068.09.1A70315 Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) by imf21.hostedemail.com (Postfix) with ESMTP id 54D2C1C0008 for ; Thu, 22 Feb 2024 11:51:12 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=ionos.com header.s=google header.b="OUtdzP/4"; dmarc=pass (policy=quarantine) header.from=ionos.com; spf=pass (imf21.hostedemail.com: domain of max.kellermann@ionos.com designates 209.85.167.52 as permitted sender) smtp.mailfrom=max.kellermann@ionos.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708602672; 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=1kLoKUxPzCv4au/TRObsd9V3UzJrpdaiZFCw0biA2LU=; b=jhol3un4/3ctzOztF5o2azrl25Ef0L1A8GYwUKf+KRoXVp71bywGM6lcK34MtP+AmksXLm kYS6jovjUF+xGnzFSXRNoZlCg0AsZ3I0qb+0hkU0hbmehKRu0J7p2nd5H6yPWrzIAWDP41 VF5wYQcsqnbO/Y0tLjwRNQLJ3nrw1sk= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=ionos.com header.s=google header.b="OUtdzP/4"; dmarc=pass (policy=quarantine) header.from=ionos.com; spf=pass (imf21.hostedemail.com: domain of max.kellermann@ionos.com designates 209.85.167.52 as permitted sender) smtp.mailfrom=max.kellermann@ionos.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708602672; a=rsa-sha256; cv=none; b=mmBUPegAaRpPKzo+ZhytgLlWFa9hHrDeVOwGuIW1lgFsy7EbAlSFstbUKe9bey1X7xBPBD uvRGZKQljFj1UlrDcT5B+4hnGOKx761KrEr1cBJsRYNQHW9UfDbwPtd9iJrXlZO3s6XPzh wIjO83ohPY0gg75vrLnWMRgvCpzw8fs= Received: by mail-lf1-f52.google.com with SMTP id 2adb3069b0e04-512cc3ea7a5so3271986e87.3 for ; Thu, 22 Feb 2024 03:51:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1708602670; x=1709207470; 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=1kLoKUxPzCv4au/TRObsd9V3UzJrpdaiZFCw0biA2LU=; b=OUtdzP/4rIwofysifeIvkjmEpWa31nYNhu/qt7gEDMbdULU1Cj/tlf6H1b9uP0UMZ7 hp7IFtfqbMq16wjVqeG2Ah5C/FTXp89oh31VWSZXYzGi/IrMUeDfo/PyF9oZLJbknMTj JLj75PThlP4gYDSxYfcVX54zVOavwRYd/Ae7pLifAhHlFQramMjs4YEO5WCA6w/D9ZJP FRguuBQOV82PYcaeBYnyOQRJidKPuyoQYg/1Gqtpsb0eSis1f7q4am97L/kmFqG6G2gB PqgVJhe/Z6vUyIXhbENIEQj8kuUq7I9fSppPKZMbfQ5qXaOxZTxJDhFhGUHP82GSdGaP QfXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708602670; x=1709207470; 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=1kLoKUxPzCv4au/TRObsd9V3UzJrpdaiZFCw0biA2LU=; b=bmmGQ7SQyaPtVAmEw139yrs7dcKd6Pc8/PWu4LcwdtRPj88Xb0FWIZcbAYKyKOVJLo l1ORtCJg+EY5YJqK67DGBnEJ6v/7k+zWu9XCWBzRisg075kZrI1LqJJXgAghV66wIY3I 2cFu2U81Fm17hvDlQBB+5nTtDWtNXtZHafVHM/REuJ+UuMDBRkWZwf7h2BKzSl5bCwe5 qXzuStYu88yYPIMGyE+ilU8MODcrqLOV65iCHSn/hxBx8RJro4nIeCo0et4x0z+hG9YJ uqwZRUNkMJM0049EqZ+xXcarNo9fIjDSGQ2qymJIAQyI/1rCIss5XOedzLiB2VF3vnUn Tw5w== X-Forwarded-Encrypted: i=1; AJvYcCU3U2Rzel2C7jc9if72WKhB7K0uD+0upuaVjjO/PwSwqtKAGiv/6xIOzFQjqhxp0krWW5lCFN9/UKQwbxb8KeZidGE= X-Gm-Message-State: AOJu0Yy85ysCU97/G07sPnEiPUQQOFiTnEIPA520DOAqNluttyVADX4c OXBwtQ0T5MI7p4g59TEe4N5i1qVX/38XSySP/560E9rKESyB4zTqlOmbJOSrPJ0= X-Google-Smtp-Source: AGHT+IG3KNwxLKO2NUP+qJu3NUWgqMuYFxpFclvxLFDRnLhVWht5yaKDXfsrMiBDlo2H+wUtOyr1eA== X-Received: by 2002:a19:ac03:0:b0:512:beb3:7070 with SMTP id g3-20020a19ac03000000b00512beb37070mr5896813lfc.59.1708602670390; Thu, 22 Feb 2024 03:51:10 -0800 (PST) Received: from raven.intern.cm-ag (p200300dc6f010900023064fffe740809.dip0.t-ipconnect.de. [2003:dc:6f01:900:230:64ff:fe74:809]) by smtp.gmail.com with ESMTPSA id co18-20020a0560000a1200b0033d4c3b0beesm15044920wrb.19.2024.02.22.03.51.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 03:51:09 -0800 (PST) From: Max Kellermann To: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: willy@infradead.org, Max Kellermann Subject: [PATCH v2 09/14] linux/mm.h: move usage count functions to mm/folio_usage.h Date: Thu, 22 Feb 2024 12:50:50 +0100 Message-Id: <20240222115055.1172877-10-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240222115055.1172877-1-max.kellermann@ionos.com> References: <20240222115055.1172877-1-max.kellermann@ionos.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 54D2C1C0008 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: ni31sdm4uzufrpcgfbn6wobgqre7jxix X-HE-Tag: 1708602672-103800 X-HE-Meta: U2FsdGVkX1/pAZYQwypqV+GgA1Tnq+yeWUC29tOKHj9nr79+iSaOOI68dJismYQLYJSiMrlApCSBPG474HFhrk5L0A2u4116Epr9uMtlPS8ySb5GnBp4aGLgQeeXjfGZteVdNReiE5wUzXxoe1ibIHnzeMg+LEXcC99s2HROmttxmgeRTZunddoJPz88jIeGQN+fBhE5AjKvlhJN+hCuhSwjmbNnCL39yjvrnycxIEcY8o18UnEl/2sAdwBDezjjK8eitm08QhgAqJS8bIxhszEh9qzoQc5Fc9G+lyMCH2iTSQOWw3O97X9pjK0DY0Zq27DoIjJBhA46WIC9m+UOjKTbet2cLn8nLIV8rM7mBN91cJtbzetJAryANoejHzrETfu83yPd5cTY+mllVzgFx7jRszCvnAftJ0y4t/3OpecMEM90TdWFHiDOWdWLXZqzBKWz/dSLKrM2XdLyaXfNMObV74Oc+/JUchoUAU7/zmLK3eoyMvmcYiGsEQDH/oaeNvTnXkeeINoi5jPs+z6L2av78gX3bDO1WvCuD+Vfs0a13YKlIAjkEETvxzC5eFSybbtYXsU76tYmZuZAw6aQsab60A3SfJNta6S4IpyJNckSTyTyoxtG5KBnxs6OfeA2ivBI3Q1n8UZF22KvcDBptpLPdFUDLLaQcXXeJL7DnpHVCD09SEU97O68bcoo0NqmXdVENIUjtEZu738Lb9LitA0pm7EURxWTyDkcBWUHEfIfIeU040JIQw2BoTZ3ynifymEw9bPt3faAr1NjcCmv+BXb1rfHol1ww1KUrMCrCiFuPTAtYkBg4LPHD+v8JvZoGWvKQMrki3GdG1nsMma0TRk4FAFEbzgbLIwAKniqsJ3CkEjcgBhf8/LcVJQc5mxxQRfc9rd01qHQUovWp7sFzoCGkmyi6euXwxB7dhM++PX+/FfYlb5HrGnZ5DJUmaLfAnLrZgn9+ULNbmxFY6Z /IA4CQOJ 6jEnyCDMVRRgyrWAgJqqjbOZbIxEtKnZe/AwI+pPA//3oNYN7yiG99EQVq2qCpFyH70E9crtJ60Mif8XEg90GX41pbcZ9WPQKpsJ4H5g9HugITl2FXBFx815iFANn+qyCpWz65nx9fosc8ZEYRXfrfJ6wBKrrVfEVTCqZ 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 | 168 +------------------------------ include/linux/mm/folio_usage.h | 177 +++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 167 deletions(-) create mode 100644 include/linux/mm/folio_usage.h diff --git a/include/linux/mm.h b/include/linux/mm.h index 7a302da7de24..0dc38017e134 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -1058,51 +1059,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 { @@ -1258,8 +1214,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); @@ -1350,126 +1304,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/folio_usage.h b/include/linux/mm/folio_usage.h new file mode 100644 index 000000000000..2700e2ec4d78 --- /dev/null +++ b/include/linux/mm/folio_usage.h @@ -0,0 +1,177 @@ +/* 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 + +/* + * 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_FOLIO_USAGE_H */