From patchwork Mon Oct 14 20:36:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13835448 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 96B1DD18144 for ; Mon, 14 Oct 2024 20:37:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C2866B0095; Mon, 14 Oct 2024 16:37:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 371276B0098; Mon, 14 Oct 2024 16:37:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14D536B0096; Mon, 14 Oct 2024 16:37:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E9C7C6B0093 for ; Mon, 14 Oct 2024 16:37:01 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9300BA129E for ; Mon, 14 Oct 2024 20:36:46 +0000 (UTC) X-FDA: 82673366874.30.9C2C6C8 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 4280D40014 for ; Mon, 14 Oct 2024 20:36:50 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q1zsZ6dh; spf=pass (imf07.hostedemail.com: domain of 36oANZwYKCGERTQDMAFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=36oANZwYKCGERTQDMAFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728938173; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KDELffEPLAPDpRQ8WZJTecgr0QxXWhGmdEuRSVQA138=; b=SqSddFL/nt/5rK+bMLYx8J1Ds2wcDuWs6y8nuE48pnTWpTQ74y1H2c51IDYTOAnQ46c0NQ 9xjGA8CqXI60LByxT10JqIwpcdFES0Al7bYuWiubpZe7/PCcsobeIG7aYGYHCiKYTWaYIF Chtv+MGFNddXG1CtmGSVqH4KjlhNqKk= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q1zsZ6dh; spf=pass (imf07.hostedemail.com: domain of 36oANZwYKCGERTQDMAFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=36oANZwYKCGERTQDMAFNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728938173; a=rsa-sha256; cv=none; b=GHS1UImNolyonOE0LtL0bueC1G1EieyIz1ALfT1gLZ0noUHrBo17j25NlMx5xNwDLpR5Ea e3WZjIrTYrKlGHlHLzR4GSzUwfPjLxwSdde5QHROWdpbmArPnreFcWH5K2fhVjuayV41rm M9LqcFKXtlKgy2Mm8sTru4KUIj0QOgY= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6dbbeee08f0so77201917b3.0 for ; Mon, 14 Oct 2024 13:36:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728938219; x=1729543019; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KDELffEPLAPDpRQ8WZJTecgr0QxXWhGmdEuRSVQA138=; b=q1zsZ6dhVXl56yCAsjnsjwylb9zxyAESJ5sDXOJ+Fz9HkNSaLWm7R/e3anJMn0e/V5 NQ0yvn77pyf1F8AFx/zMP8UVvPYPPhaKmcOi1b7TnezTpgOPpkaLQwLDo9roDxqDb+Kr eEE7k7rrVFDS7Ukt2V9Jyw6mbLDIvBATcRQhb/7hJi8feXKEbA2yZp4ZUOxYz74QXKf0 g7mA5vB3wpH1G1VJJ4nmttFm6RJXlymOS5QeTG49gpUxOJ+axOKPh9YvDtQz0W1ts8c9 ZNR/DBxehXfuPsF6xFL8dZTZ/SJ1fpL+bGvImtqGHFeurHGWHVDqdOAl+vIAx3Qyu4dg DrTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728938219; x=1729543019; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KDELffEPLAPDpRQ8WZJTecgr0QxXWhGmdEuRSVQA138=; b=WyuyrhaENMaSvqmszgdYpH9T8KXCnzdTQwFhZvFSR+EYUEObWyLJVdbzsTInT/0ox8 2LJ5nP5FG42eegCVd+1DPHuDCp5MvXGZMrxwlLXntbeRjHhDl43SwnE/CW2rk/d9Zz4+ 7kUDlrq/VznV1Prupr8UHIX2XkuGYxkrHBQwlZ3n0NV5Cxqffqzt4uWJtPZlPWk9xN4y FRXJ8niBFjxixMzRg3AkIKg7mtZnNV/cvsshMAmZJneT6TG1keT4jxcMAQj3fEaMCmUt cI/HgqqmHgE0IqGc71YApOwe0UaR3NLN3jefH2Ze/9s3pjVIZzTYPtwf7qqiLIVyKGj5 /XOw== X-Forwarded-Encrypted: i=1; AJvYcCX3ZYqOHUVXG2V5ZN5UTxNz+KrKvDN9sHJh/U6FT42BsnUHzOXGgTLXsyz9tE3asw40t/ug8V3ruw==@kvack.org X-Gm-Message-State: AOJu0YzO1O80jwTmv2g23HzpkapcGkTM+6n39qIndrY/fJ8OpavCYG2O z3ODNbX+XukeKFqvWk9Fb3s9dPjhhVXeZD2c5LpQzwvucN/3x1Olh05nXwrmVHhvvbdMamaIG/A 1gw== X-Google-Smtp-Source: AGHT+IFebmDDzFfa9NO21TdKX40eCmjiTPo05JzyoY7+ZX8h6EIqKu1BLOx2oszwApr2jiSzy+GImo1RYfc= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:915:bdd7:e08a:7997]) (user=surenb job=sendgmr) by 2002:a5b:40a:0:b0:e29:9c5:5fcb with SMTP id 3f1490d57ef6-e290bb22b20mr207938276.4.1728938218676; Mon, 14 Oct 2024 13:36:58 -0700 (PDT) Date: Mon, 14 Oct 2024 13:36:45 -0700 In-Reply-To: <20241014203646.1952505-1-surenb@google.com> Mime-Version: 1.0 References: <20241014203646.1952505-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241014203646.1952505-5-surenb@google.com> Subject: [PATCH v3 4/5] alloc_tag: introduce pgalloc_tag_ref to abstract page tag references From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, corbet@lwn.net, arnd@arndb.de, mcgrof@kernel.org, rppt@kernel.org, paulmck@kernel.org, thuth@redhat.com, tglx@linutronix.de, bp@alien8.de, xiongwei.song@windriver.com, ardb@kernel.org, david@redhat.com, vbabka@suse.cz, mhocko@suse.com, hannes@cmpxchg.org, roman.gushchin@linux.dev, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, pasha.tatashin@soleen.com, souravpanda@google.com, keescook@chromium.org, dennis@kernel.org, jhubbard@nvidia.com, yuzhao@google.com, vvvvvv@google.com, rostedt@goodmis.org, iamjoonsoo.kim@lge.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspam-User: X-Stat-Signature: 1k9wq5sk158xwic1yx73n1m35ek7dxu3 X-Rspamd-Queue-Id: 4280D40014 X-Rspamd-Server: rspam11 X-HE-Tag: 1728938210-686277 X-HE-Meta: U2FsdGVkX1/rEvFAFm9tjqkdGbh6gKmSCvdhmVxFznwRudeZ9Pnd8Sh3n8kQGjPWvpJSPbqib7kJdhjmRfwIlmsEbf9LpDdDoBzbB3q65k/nxKXtywWrv9vHxy7KyfHl3Ib5aDrFyvur9TJD6OJsZ/MRMZAOIkhE1Tg1A+mj0neFiFhkn1ygcdxTmH+SrRGCO1Nfi220mqQiXdKxr1CZf1tJGt1Prvrg7i1jK/AMnKxYfH0BbU2WXq4qcOgljjxYzqweE+8EWGzTPphXZouQ9JRfPr4EGAyglAYZLrgW68gbaHUnWbK7EjFg84aBfpHJYNj33W78TSQrXaHuFwwUNGnMLtCSj7Kdm2KffYptQ1PGaRJlSX+dUmm+P9fxiacqzVNWF1Bf43l4jMwtrENtJWYUikL/8m35lZVKO2SyiGp/id+U5i8OtmuN+yznNqERI4VbXLgQ/WSh7rmCV9wbbr7D29J7daVj+h/o7Pqx976ICMJSS2AQyMyvSu8u2kR86Ex15WyUlTWOayX/Qc07PqfPXPC+TPxTp+fF89Os3hzqPMQ+Qmhrwbp3Bno2Bshhh6UtEUKVeLTdJqoWsFzOfJImNOfi5F51Tl67GwalQeHkzjmyeS3t4VJ/U7y40F+pX/JBqTlOWctyWP3C7qtr1W/7IUwGR9Ofde3pkJAJHUjbasq7GFLMmZlTqS0M+5iWtmFML/2ah3tdCNzq+zQu3qBp+EHbj3X5un3AQeoBsAEPTLXpQPykDfyL0+n5+b6YCxy/gHPcLvYYkhWfzUDO6E6rr65svvsVTg0Y0Vyp2MCyVYaz4X71/IfDQ7V4r9HHaABdLSfUxgwEJi0lyaYLwMVh774Qn4W+wQ67gUwlUkdZSRrtTYLXZ0J7TQUenT3q9CuT61KBnO+9yrZEvah86o263sGQXiG1CE2zJj85mv65ozRFkCLJYincTw9lFElGpeNHia4MfU3EfeJLHLb hBxBqz6g JhuC0i6HBrCwSSqtWwwmTJ7IF4Fv44307LBHdcjEGnJgjv5IJMyr71/B6K53+38xJsqRQjsfvTBbGLu769koSL4aeM71lq5mbc3+SVpnHVFhKZrNnRuZwxhSAmlJqNOrs3VWhVWXszKC3UoB9YW1+J0BvK5jGSL6MXwMqB+lne6c8rtss4vJJKmcXMIB/8xJ0UmSrmwPQ8Dlo1mdizX7jdRcnqoEDdxzgOqQiTtbqggPZxsA98Qq8Mbtg7Jhlp5xQ8vghlFH5pW/qzbYQ4jEt2jtUUi30peFGYYiZ62Tdzt417GxpC/lm8HNsmhN1h8K8NCS11yU2HBk2TU6D0Ariwpnv76YF9n60Lt960Y2gf+MQEzH04dCg+NiNubJdrs3orqOfcwFPPPEtd4zZLdDhCLaYbpHO2u128K+mAi+x6g708v59/TBgo/1n+4qb0zYuZQFCYsItvQ8yiP1cZL5H5E6IAIqe/wZgx8zNk0+WfgaljBTgEi1rCUwV64GXqyORoGC6g9q+pD1HC91hr9tF0rQ5seiwEe9pKv/aBIQlcge1cZrT3Sw1d7RP38/knX/5EIl30m7h9DRN/YiTwm2SQiPeWg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000015, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: To simplify later changes to page tag references, introduce new pgalloc_tag_ref and pgtag_ref_handle types. This allows easy replacement of page_ext as a storage of page allocation tags. Signed-off-by: Suren Baghdasaryan --- include/linux/mm.h | 25 +++++---- include/linux/pgalloc_tag.h | 108 ++++++++++++++++++++++++------------ lib/alloc_tag.c | 3 +- 3 files changed, 88 insertions(+), 48 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 5cd22303fbc0..8efb4a6a1a70 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4180,37 +4180,38 @@ static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new return; for (i = nr_pages; i < (1 << old_order); i += nr_pages) { - union codetag_ref *ref = get_page_tag_ref(folio_page(folio, i)); + union pgtag_ref_handle handle; + union codetag_ref ref; - if (ref) { + if (get_page_tag_ref(folio_page(folio, i), &ref, &handle)) { /* Set new reference to point to the original tag */ - alloc_tag_ref_set(ref, tag); - put_page_tag_ref(ref); + alloc_tag_ref_set(&ref, tag); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) { + union pgtag_ref_handle handle; + union codetag_ref ref; struct alloc_tag *tag; - union codetag_ref *ref; tag = pgalloc_tag_get(&old->page); if (!tag) return; - ref = get_page_tag_ref(&new->page); - if (!ref) + if (!get_page_tag_ref(&new->page, &ref, &handle)) return; /* Clear the old ref to the original allocation tag. */ clear_page_tag_ref(&old->page); /* Decrement the counters of the tag on get_new_folio. */ - alloc_tag_sub(ref, folio_nr_pages(new)); - - __alloc_tag_ref_set(ref, tag); - - put_page_tag_ref(ref); + alloc_tag_sub(&ref, folio_nr_pages(new)); + __alloc_tag_ref_set(&ref, tag); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } #else /* !CONFIG_MEM_ALLOC_PROFILING */ static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 59a3deb792a8..bc65710ee1f9 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -9,48 +9,83 @@ #ifdef CONFIG_MEM_ALLOC_PROFILING +typedef union codetag_ref pgalloc_tag_ref; + +static inline void read_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) +{ + ref->ct = pgref->ct; +} + +static inline void write_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) +{ + pgref->ct = ref->ct; +} + +union pgtag_ref_handle { + pgalloc_tag_ref *pgref; /* reference in page extension */ +}; + #include extern struct page_ext_operations page_alloc_tagging_ops; -static inline union codetag_ref *codetag_ref_from_page_ext(struct page_ext *page_ext) +static inline pgalloc_tag_ref *pgref_from_page_ext(struct page_ext *page_ext) { - return (union codetag_ref *)page_ext_data(page_ext, &page_alloc_tagging_ops); + return (pgalloc_tag_ref *)page_ext_data(page_ext, &page_alloc_tagging_ops); } -static inline struct page_ext *page_ext_from_codetag_ref(union codetag_ref *ref) +static inline struct page_ext *page_ext_from_pgref(pgalloc_tag_ref *pgref) { - return (void *)ref - page_alloc_tagging_ops.offset; + return (void *)pgref - page_alloc_tagging_ops.offset; } /* Should be called only if mem_alloc_profiling_enabled() */ -static inline union codetag_ref *get_page_tag_ref(struct page *page) +static inline bool get_page_tag_ref(struct page *page, union codetag_ref *ref, + union pgtag_ref_handle *handle) { - if (page) { - struct page_ext *page_ext = page_ext_get(page); + struct page_ext *page_ext; + pgalloc_tag_ref *pgref; - if (page_ext) - return codetag_ref_from_page_ext(page_ext); - } - return NULL; + if (!page) + return false; + + page_ext = page_ext_get(page); + if (!page_ext) + return false; + + pgref = pgref_from_page_ext(page_ext); + read_pgref(pgref, ref); + handle->pgref = pgref; + return true; +} + +static inline void put_page_tag_ref(union pgtag_ref_handle handle) +{ + if (WARN_ON(!handle.pgref)) + return; + + page_ext_put(page_ext_from_pgref(handle.pgref)); } -static inline void put_page_tag_ref(union codetag_ref *ref) +static inline void update_page_tag_ref(union pgtag_ref_handle handle, + union codetag_ref *ref) { - if (WARN_ON(!ref)) + if (WARN_ON(!handle.pgref || !ref)) return; - page_ext_put(page_ext_from_codetag_ref(ref)); + write_pgref(handle.pgref, ref); } static inline void clear_page_tag_ref(struct page *page) { if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref = get_page_tag_ref(page); + union pgtag_ref_handle handle; + union codetag_ref ref; - if (ref) { - set_codetag_empty(ref); - put_page_tag_ref(ref); + if (get_page_tag_ref(page, &ref, &handle)) { + set_codetag_empty(&ref); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -59,11 +94,13 @@ static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, unsigned int nr) { if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref = get_page_tag_ref(page); + union pgtag_ref_handle handle; + union codetag_ref ref; - if (ref) { - alloc_tag_add(ref, task->alloc_tag, PAGE_SIZE * nr); - put_page_tag_ref(ref); + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -71,11 +108,13 @@ static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) { if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref = get_page_tag_ref(page); + union pgtag_ref_handle handle; + union codetag_ref ref; - if (ref) { - alloc_tag_sub(ref, PAGE_SIZE * nr); - put_page_tag_ref(ref); + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_sub(&ref, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -85,13 +124,14 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) struct alloc_tag *tag = NULL; if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref = get_page_tag_ref(page); - - alloc_tag_sub_check(ref); - if (ref) { - if (ref->ct) - tag = ct_to_alloc_tag(ref->ct); - put_page_tag_ref(ref); + union pgtag_ref_handle handle; + union codetag_ref ref; + + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_sub_check(&ref); + if (ref.ct) + tag = ct_to_alloc_tag(ref.ct); + put_page_tag_ref(handle); } } @@ -106,8 +146,6 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) #else /* CONFIG_MEM_ALLOC_PROFILING */ -static inline union codetag_ref *get_page_tag_ref(struct page *page) { return NULL; } -static inline void put_page_tag_ref(union codetag_ref *ref) {} static inline void clear_page_tag_ref(struct page *page) {} static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, unsigned int nr) {} diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 648f32d52b8d..2ef762acc203 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -474,7 +475,7 @@ static __init void init_page_alloc_tagging(void) } struct page_ext_operations page_alloc_tagging_ops = { - .size = sizeof(union codetag_ref), + .size = sizeof(pgalloc_tag_ref), .need = need_page_alloc_tagging, .init = init_page_alloc_tagging, };