Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp2122763lqa; Tue, 30 Apr 2024 08:41:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVLBixTvFayFAmJyfgI8q6S9cPqFIuQ8IfwMRuyto4GrUfQ0ybi/aBEMGfwdtPWFCcnhB096zk2KmApot8ODsWjBQPCiqnl9knMNiiP3Q== X-Google-Smtp-Source: AGHT+IHchDmIg+v+GO9GdQNRKJwi+MwY/n0yFCqiPIgSUo2v8DrnDVHJz3i/JvoI8sSeiacVtb0o X-Received: by 2002:a05:6808:90f:b0:3c8:2ae6:4b1b with SMTP id w15-20020a056808090f00b003c82ae64b1bmr3123147oih.35.1714491717080; Tue, 30 Apr 2024 08:41:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714491717; cv=pass; d=google.com; s=arc-20160816; b=fIxJhL9cB+ZkY8/tAPcofojj7uEw2G3NXCDlJ2MSu8PL2d3ABXBU1ED1PJiaEh2ns3 DEMM+p8hsbuapDgf6lxmcWjY6RN/U0WREMDvHnpnWvbSDHtF1/wvbKK/NwM+XAEqJ7x5 N2SLyogyENP8wvQg8ryXJ5lskBZVClnS8zW4B416o86uNdLAXgMKHJUbYaPQZc+67kOg LTSbetdcwWzPFE9JfuCRcJgOZAuldtfH29Hm0jBycCHzJpTx3fNxt/ewU89zUD/zZgTb eyLiETUcsyB9h7aermTC9bXsYdLu4bqlITUG+33oh8GkDbnTTcBmyplNAmNBziq9a1tE kpVw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ALyXA1N3h50IKuqnW6AM7h0jqplMyLOr0C50l7ESkJs=; fh=QJNYBTQD7gHjGFgyGcC38D9XNG0MWXBCVwjcNXx14EU=; b=WjHIfhuSxp+Z/hTvZTGgAdCKS8UTsHX9p5nYXZQy9hkI4LF42H65CqOrMe6LgjnPdU S4UvulxuxTO5Vag/rOABKU0P23bWoRY0ZYWSnLayR5atZF3r4o+/hbAfuqNj8a/8D8wE Vb4APXPXcNFrmqDTF3FafjeJzmk74UT44FwKKe2cVtjqG06xja84nsNCDQKX7Z5hhd6N quRHXO6d3Bl3YNq36876aCrrFK/DzEPtj61auESV9TXfshV0E+iSQCJ0fLDIviLYowON pjz6Oh7x/+XhXswRlQ3kI/MIR/xHzj2LCJ+tX4NX7f2GlF0TicgblvNnIQV+TwH3/pna BlHw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=fXOdTypW; arc=pass (i=1 spf=pass spfdomain=ionos.com dkim=pass dkdomain=ionos.com dmarc=pass fromdomain=ionos.com); spf=pass (google.com: domain of linux-kernel+bounces-164277-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-164277-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 12-20020a05621420ec00b006a0d6e50d95si3338860qvk.117.2024.04.30.08.41.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 08:41:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-164277-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=fXOdTypW; arc=pass (i=1 spf=pass spfdomain=ionos.com dkim=pass dkdomain=ionos.com dmarc=pass fromdomain=ionos.com); spf=pass (google.com: domain of linux-kernel+bounces-164277-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-164277-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 491911C23B57 for ; Tue, 30 Apr 2024 15:32:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0AF0517BB25; Tue, 30 Apr 2024 15:30:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ionos.com header.i=@ionos.com header.b="fXOdTypW" Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8481F175579 for ; Tue, 30 Apr 2024 15:29:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714490999; cv=none; b=pngOWPHHikUJ+TANMQIgebKbS48o7y6wb0ZQ6rA17JBAl/OzYE4tm2NINfCLefKO/PfQAiYSWIepm8o4xicyJiPKnxUzTyBc6c2TnzfMgS04lm7lzYOfB4zE8+v2Rw8R77uju/apj98dGnG9f6QLQ05goAKfnGp00yxKMQbNzYU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714490999; c=relaxed/simple; bh=QeOThma+C+xVBPv/VbtaPx4QYbN7GGFCVewQ4gi5YFs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bvMNCjLQF5NvGF3lU5dEjUBbLyevRiKw+9fFY7jiMyhRo6MkbLcFBXrSsX3/MzrrKMTZISz1F+zvUVWIIgQrX05QFtVl2vAxRFdszE5aUOc/Oj/83isgDG5+M4EJbJWXLJOeLDUKGpJGDMtKq9BzDUcVTRMhbFz6KD97IYFlLLQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ionos.com; spf=pass smtp.mailfrom=ionos.com; dkim=pass (2048-bit key) header.d=ionos.com header.i=@ionos.com header.b=fXOdTypW; arc=none smtp.client-ip=209.85.208.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ionos.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ionos.com Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5709cb80b03so6096906a12.2 for ; Tue, 30 Apr 2024 08:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1714490995; x=1715095795; darn=vger.kernel.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=ALyXA1N3h50IKuqnW6AM7h0jqplMyLOr0C50l7ESkJs=; b=fXOdTypWZxurfiOPVBXlulquMiCaHjqEwRVfWpNp9RvDM8m2m1NdAHfje94SvyHqr+ OvW+5TyuCJPjQ3BiP4w+lYSm7Op2AB5wSd20r1JGmJ54Y72jmDNCwdNhs7L5u093tsV1 d3VCczL/KW1o9hFweXeLiKWxKgAviTzF/FyqwBHfpcCVVxjOVURb87Cn5YrmuR+BgeGT Xbos+XCuYxvVDOEh4PP48kQl8/8WtcV2/lPb6zQqGw4FKhLYebpxuihbLK/eyf6uL5Gp wh57Rx+DSfKnRQOZxzc8x2sNbCz5pgrqA5/oHha5mNTe9GtGqo8yKRbifhVC/L0xXam+ qqig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714490995; x=1715095795; 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=ALyXA1N3h50IKuqnW6AM7h0jqplMyLOr0C50l7ESkJs=; b=GWq41O2rVN6JWddUB8oL93gVLZPo9ZiA8NjhDvaoyt04fMDEhKBqIYOD+oCBQ5mwww i7UEBFYscf7KQoypXcDDOIQIVImf19qOk4npWmDGSdR3uu6w8pSYxFE1bBWLitb9TRTS ot+WC0GEtByluIr9NdkVp7+5SuYpGFw5e55K2WGCHCxhmdRipK15fIF2J+TXxoJsW0Js noRCjB2g35AKjv4laAlt7UClSjH8Sr4zQIJOzCsZFCRJGGYPcewIZckbiHGRiNwz/zK+ vVps9U5ZWU2dtbpFRdOtNwhyhuAe5wUrq8P1szDxw/gNvhfUypxNzO3WzjS9F0Yx8OBv HARA== X-Forwarded-Encrypted: i=1; AJvYcCXpRQAVkl3yZQHhtxEXqUt0BBjjGE9D4rxgwJA/c/FIWu8LDSqxtsfpvrYU4FKGOhedPLOC9l/qAEJkv7TfZduugOZ9LmjXafkdg+kg X-Gm-Message-State: AOJu0YznE+GVEEZRxv3dM6WPAsIRXxO4bCt+07LtVU3LghHB3OM0O3Nx khFefWZduQvsHENC9PEW2zHY96XVeIi/7n+IqPxvQDtG9x/iNu5GLIaN6SXDmu0= X-Received: by 2002:a17:906:3555:b0:a58:e9ef:5e47 with SMTP id s21-20020a170906355500b00a58e9ef5e47mr33246eja.63.1714490994716; Tue, 30 Apr 2024 08:29:54 -0700 (PDT) Received: from raven.intern.cm-ag (p200300dc6f06e100023064fffe740809.dip0.t-ipconnect.de. [2003:dc:6f06:e100:230:64ff:fe74:809]) by smtp.gmail.com with ESMTPSA id a10-20020a1709066d4a00b00a52567ca1b6sm15278137ejt.94.2024.04.30.08.29.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 08:29:54 -0700 (PDT) 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, david@redhat.com.au, Max Kellermann Subject: [PATCH v5 10/15] linux/mm.h: move usage count functions to mm/folio_usage.h Date: Tue, 30 Apr 2024 17:29:26 +0200 Message-Id: <20240430152931.1137975-11-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240430152931.1137975-1-max.kellermann@ionos.com> References: <20240430152931.1137975-1-max.kellermann@ionos.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 9539ba12b99d..035e56e203df 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 @@ -1074,51 +1074,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 { @@ -1275,8 +1230,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); @@ -1365,129 +1318,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_folio_refs(folio, 1)) - 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..1cf11ca1f5ab --- /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_folio_refs(folio, 1)) + return; + folio_put(folio); +} + +#endif /* _LINUX_MM_FOLIO_USAGE_H */ -- 2.39.2