From nobody Tue Feb 10 02:59:35 2026 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 D99FB3E47B for ; Mon, 19 Aug 2024 15:15:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724080523; cv=none; b=LxWgHLpGlMPk9X2UHU4XEoQcnhSSP7SVDN142d20SFOqe7pB3rKRacEyk1wO4gRB0Afkk/FVDwFGKGdB2MhwIniBeT5BKFI+YtRJQMXKcV7JbG0Q8hiZtTXmCgGgTqN3oM70gYTUgExvW9bPuIa3rEPmkM4VEOoqQe52n29hFSY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724080523; c=relaxed/simple; bh=Ko22kfl3vqLMU/DlMYsrr92EuhgeRLadRizUOIqb/nE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ob+/6eLkKUo5/SsdR3piuooka957rHW2jiD6g7GEmp4GfNQGbqexHnLCJsBVG/p2nDlG3xdQPi4ekvZGZqORdmHx3A/OoVxfq825MEIMOS+VPr/ymg1sDi3DtarKJDfANrteyUOZgH/UZDJ1jglYyCNnQnghtC2uBwe+VAUVJ8M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aYb2ma4g; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aYb2ma4g" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6b41e02c293so49497297b3.0 for ; Mon, 19 Aug 2024 08:15:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724080521; x=1724685321; darn=vger.kernel.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=JgTjJwDNOOBGmN3SYEoTtFKEJl/MBoFNsUNbiHHZqOc=; b=aYb2ma4gv0PQydD/TFxZECz4p85GHLS0vV9b5+nPJUsab1NipNKN5xu8wxsc35hwkg DSrgTqwzOLJ2ZgwLqAPKid9hHoorOZA2iltvvgYjZEQLsT1MflL381NjlWyFG23nXKVM w4ahOqY3onPUZyJ9DHY+HWkIzO0FDKIUzGeNLo8ZwwDYr1P6LdMWqcQHfNy5ZQnldNyw suFYgXfCSokgsdt00/f9xbg/mObqYEFrcdKIxq38Py9idh5v0VXQvKfzSnj8N/jwj9KX +N69q2zMSo9z8VzDXoLlxuBD5d53hziXKIKrfghw+aNNUAqfDrGJ0E1PYaleZIeAOAQF AWTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724080521; x=1724685321; 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=JgTjJwDNOOBGmN3SYEoTtFKEJl/MBoFNsUNbiHHZqOc=; b=UQYTGyb7Cov/Swagq1naNLM4xXrj6Bl8vMH8cxO1pdvc0ZCBBPoYPGmlxft5pQQZEF t0jOuhgc7TA2JM2vb8q/Pk3EzOWGd4rPOhXjtTcDQjbUzPFfffTurlIt4HBUJSnW33+q r5YHK47MH7r7inPedRomd0Sw7ulXCHdlU2E/fYsDKuqlXIzhvf851vGHNFlCg6A1Ipf9 paZ0jJxuWJMSeT/UfKYs99LpP9ap++c0++WbcSeDLYkmHus8zJmumBu2oTjB1kDyvET7 /6rp1KvgaWWPELhnu71vjE4/DAAFFdrndEM7jboWU+Wky42dIuSZXpl8PphTJbuZG3AZ HXnQ== X-Forwarded-Encrypted: i=1; AJvYcCUMhEMybTvQ6oP6/LK3vt0ygdZIuA23up6MN4pO92MWLr0ZWkqdrDn46i6JKVvVrLdyAemZ422ENYd/YPW8h5FhW//0z/iI8fjy6Drv X-Gm-Message-State: AOJu0YxpTvVYs6C/0hrpM+GcYPI72hcmsbv3P42wouTXjJIFadySr7LA IL52/qXVZI/z+j9K+bMvQmobOQ1o4zeP5eYvuVdMqyl7oJp1ykM7BCxepbVnE/mcbCTHx+arfwk o1w== X-Google-Smtp-Source: AGHT+IF6wQq4hojn2WvBl6Ky3Y5Ec+1miO/ZVJTIsvzHPNXwtXX0T5qfBPLHg5VMXezc7PRztRYvs2+xsUs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:5aea:cf26:50f4:76db]) (user=surenb job=sendgmr) by 2002:a05:690c:4e0e:b0:64a:d1b0:4f24 with SMTP id 00721157ae682-6b1bba55dd5mr2194777b3.7.1724080520869; Mon, 19 Aug 2024 08:15:20 -0700 (PDT) Date: Mon, 19 Aug 2024 08:15:09 -0700 In-Reply-To: <20240819151512.2363698-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240819151512.2363698-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240819151512.2363698-4-surenb@google.com> Subject: [PATCH 3/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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/pgalloc_tag.h | 144 +++++++++++++++++++++++------------- lib/alloc_tag.c | 3 +- 2 files changed, 95 insertions(+), 52 deletions(-) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 244a328dff62..c76b629d0206 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -9,48 +9,76 @@ =20 #ifdef CONFIG_MEM_ALLOC_PROFILING =20 +typedef union codetag_ref pgalloc_tag_ref; + +static inline void read_pgref(pgalloc_tag_ref *pgref, union codetag_ref *r= ef) +{ + ref->ct =3D pgref->ct; +} + +static inline void write_pgref(pgalloc_tag_ref *pgref, union codetag_ref *= ref) +{ + pgref->ct =3D ref->ct; +} #include =20 extern struct page_ext_operations page_alloc_tagging_ops; =20 -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_e= xt) { - return (union codetag_ref *)page_ext_data(page_ext, &page_alloc_tagging_o= ps); + return (pgalloc_tag_ref *)page_ext_data(page_ext, &page_alloc_tagging_ops= ); } =20 -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; } =20 +typedef pgalloc_tag_ref *pgtag_ref_handle; + /* Should be called only if mem_alloc_profiling_enabled() */ -static inline union codetag_ref *get_page_tag_ref(struct page *page) +static inline pgtag_ref_handle get_page_tag_ref(struct page *page, union c= odetag_ref *ref) { if (page) { struct page_ext *page_ext =3D page_ext_get(page); =20 - if (page_ext) - return codetag_ref_from_page_ext(page_ext); + if (page_ext) { + pgalloc_tag_ref *pgref =3D pgref_from_page_ext(page_ext); + + read_pgref(pgref, ref); + return pgref; + } } return NULL; } =20 -static inline void put_page_tag_ref(union codetag_ref *ref) +static inline void put_page_tag_ref(pgtag_ref_handle pgref) { - if (WARN_ON(!ref)) + if (WARN_ON(!pgref)) return; =20 - page_ext_put(page_ext_from_codetag_ref(ref)); + page_ext_put(page_ext_from_pgref(pgref)); +} + +static inline void update_page_tag_ref(pgtag_ref_handle pgref, union codet= ag_ref *ref) +{ + if (WARN_ON(!pgref || !ref)) + return; + + write_pgref(pgref, ref); } =20 static inline void clear_page_tag_ref(struct page *page) { if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref =3D get_page_tag_ref(page); - - if (ref) { - set_codetag_empty(ref); - put_page_tag_ref(ref); + pgtag_ref_handle handle; + union codetag_ref ref; + + handle =3D get_page_tag_ref(page, &ref); + if (handle) { + set_codetag_empty(&ref); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -59,11 +87,14 @@ static inline void pgalloc_tag_add(struct page *page, s= truct task_struct *task, unsigned int nr) { if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref =3D get_page_tag_ref(page); - - if (ref) { - alloc_tag_add(ref, task->alloc_tag, PAGE_SIZE * nr); - put_page_tag_ref(ref); + pgtag_ref_handle handle; + union codetag_ref ref; + + handle =3D get_page_tag_ref(page, &ref); + if (handle) { + alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -71,53 +102,58 @@ 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 =3D get_page_tag_ref(page); - - if (ref) { - alloc_tag_sub(ref, PAGE_SIZE * nr); - put_page_tag_ref(ref); + pgtag_ref_handle handle; + union codetag_ref ref; + + handle =3D get_page_tag_ref(page, &ref); + if (handle) { + alloc_tag_sub(&ref, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } =20 static inline void pgalloc_tag_split(struct page *page, unsigned int nr) { - int i; - struct page_ext *first_page_ext; - struct page_ext *page_ext; - union codetag_ref *ref; + pgtag_ref_handle first_pgref; + union codetag_ref first_ref; struct alloc_tag *tag; + int i; =20 if (!mem_alloc_profiling_enabled()) return; =20 - first_page_ext =3D page_ext =3D page_ext_get(page); - if (unlikely(!page_ext)) + first_pgref =3D get_page_tag_ref(page, &first_ref); + if (unlikely(!first_pgref)) return; =20 - ref =3D codetag_ref_from_page_ext(page_ext); - if (!ref->ct) + if (!first_ref.ct) goto out; =20 - tag =3D ct_to_alloc_tag(ref->ct); - page_ext =3D page_ext_next(page_ext); + tag =3D ct_to_alloc_tag(first_ref.ct); for (i =3D 1; i < nr; i++) { - /* Set new reference to point to the original tag */ - ref =3D codetag_ref_from_page_ext(page_ext); - alloc_tag_add_check(ref, tag); - if (ref) { - ref->ct =3D &tag->ct; + pgtag_ref_handle handle; + union codetag_ref ref; + + page++; + handle =3D get_page_tag_ref(page, &ref); + if (handle) { + /* Set new reference to point to the original tag */ + alloc_tag_add_check(&ref, tag); + ref.ct =3D &tag->ct; /* * We need in increment the call counter every time we split a * large allocation into smaller ones because when we free each * part the counter will be decremented. */ this_cpu_inc(tag->counters->calls); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } - page_ext =3D page_ext_next(page_ext); } out: - page_ext_put(first_page_ext); + put_page_tag_ref(first_pgref); } =20 static inline struct alloc_tag *pgalloc_tag_get(struct page *page) @@ -125,13 +161,15 @@ static inline struct alloc_tag *pgalloc_tag_get(struc= t page *page) struct alloc_tag *tag =3D NULL; =20 if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref =3D get_page_tag_ref(page); - - alloc_tag_sub_check(ref); - if (ref) { - if (ref->ct) - tag =3D ct_to_alloc_tag(ref->ct); - put_page_tag_ref(ref); + pgtag_ref_handle handle; + union codetag_ref ref; + + handle =3D get_page_tag_ref(page, &ref); + if (handle) { + alloc_tag_sub_check(&ref); + if (ref.ct) + tag =3D ct_to_alloc_tag(ref.ct); + put_page_tag_ref(handle); } } =20 @@ -146,8 +184,12 @@ static inline void pgalloc_tag_sub_pages(struct alloc_= tag *tag, unsigned int nr) =20 #else /* CONFIG_MEM_ALLOC_PROFILING */ =20 -static inline union codetag_ref *get_page_tag_ref(struct page *page) { ret= urn NULL; } -static inline void put_page_tag_ref(union codetag_ref *ref) {} +typedef void *pgtag_ref_handle; + +static inline pgtag_ref_handle +get_page_tag_ref(struct page *page, union codetag_ref *ref) { return NULL;= } +static inline void put_page_tag_ref(pgtag_ref_handle handle) {} +static inline void update_page_tag_ref(pgtag_ref_handle handle, union code= tag_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 f33784f48dd2..a1d80d2ef512 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -397,7 +398,7 @@ static __init void init_page_alloc_tagging(void) } =20 struct page_ext_operations page_alloc_tagging_ops =3D { - .size =3D sizeof(union codetag_ref), + .size =3D sizeof(pgalloc_tag_ref), .need =3D need_page_alloc_tagging, .init =3D init_page_alloc_tagging, }; --=20 2.46.0.184.g6999bdac58-goog