From nobody Fri Dec 19 08:56:28 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 6025C14BF9B for ; Mon, 2 Sep 2024 04:41:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252097; cv=none; b=rK6499S6N4cCEWT9+mgXh0uQC836HYY7ZGkGGe/pwjcI0MGBode+p4iK4xRg80d/3GTzLzLRqxNlVgkmB79yYF59SIcX9pv0hR+Qttoh9N206/jJkxMW8zutaBeFZ83Ga+7dlueO5oOK9YYBERBfnAYOnlNLj1Rmwprbk2qiFqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252097; c=relaxed/simple; bh=knnBZu7kPS2XiZHCOoomuagEtSw2A+8o8O+N0AJpu1I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Nghw0gSuzGzfRiPpIh41iDGA6WTp+IfpaT1EqFAT3lEFSYPm4QDCf7z9CzESBJQ7UWoUmU9zn9cayzFzJ1YV6H33G7XfJXtaejd8PFkvfR7Ys59Fmi/ER5rWy1ojmljMBwDIUNyEyw377lQTwNMLcyxvdLWO/ID2cCNpGpsxlZU= 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=ObbJEsFl; arc=none smtp.client-ip=209.85.219.201 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="ObbJEsFl" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e02fff66a83so6351082276.0 for ; Sun, 01 Sep 2024 21:41:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725252094; x=1725856894; 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=kLpU39ux3x2TTO/xeONsOF6qZRhVUZNcpjltFBicrmM=; b=ObbJEsFlLJj8q/uKPICo68h7XtFK06WRs42lj1wo+168rApzvRYv51HHAlJitWtqhe /uNRbCQ8Zmt/9Igg0ogICyUA3lZO9IfvX3pUV25vgVQz+ys9AdH++a+hDTIkc6e1tqHt fdybiZ7BtsHMX4zid+OMrBrn90A1oPdVW3rHcZvmkOTSkO/tI7piYo0ajYi78Al6IAmJ qaG8Rwy/6Ebw8cZtQyzfQPIAW2G++DWwCYOn5Kbh9ugsFMlCLN1O0xkdSKpS/DTZQ0H5 1Zmd/csCJnyNXSm8miBpMWZa+P6PIF4PupFNY1iySiwyLlidYQfNw5OZbl/ZqaiqSyp8 BXnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725252094; x=1725856894; 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=kLpU39ux3x2TTO/xeONsOF6qZRhVUZNcpjltFBicrmM=; b=RFBWsGFpqkHJLGEuURJIKPJ0b+EblKWfL98vpNzK43fP1Dopizf0a7G8qX/PSv1AVc DAouFDkPtGd4ObESxRNm35nmDR88rdhSLuOAElRkZDN9N0Q4i8m3u2QZzWBBaz+i7aXI Di7prukNgUdJqB0kkUJ3FxEJC2tSeDtAo/GOF9h4UWWSWuyBG/lS8iEHtatutZEKgboP 1cvbRmvHsXkQPLRvLxwYZ+8rKjbnAyOtmRUR5XFUCDh8x6DwO3JN9rXodJY8SpUakffU YTiIV5o/iOnt4vZWC9FJiG60ku5GNSJVsbB+vFmI8GTSu5NznKVU2Vs6GVa4nl3qG3UM GCGA== X-Forwarded-Encrypted: i=1; AJvYcCUiyJPOUPEnOOW1eRvIfTesPLTcnxvBpS31nZLrxYnT9nj4APpRL9fAamMbr88xNM5hCsv1KVCCH7EqyVA=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6OQvif666rW4TpvdfVmQML85FGSBhXlrAFZNRJoB2wWqTuU97 zaLYxI3MYas182artmDV4VF7YPHxFRLXPB/2yVLwFtLAN43A7i9H9hPPb88E4Wp1sAXUj5o7TKp ihA== X-Google-Smtp-Source: AGHT+IE+2G0xxDfv2kbdFSiqwhGjdz1DeNRKuCcVpqvSyerK6sADotCLCA7OoS3t5ommCEFjIJVLeySgq3I= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:7343:ecd2:aed:5b8f]) (user=surenb job=sendgmr) by 2002:a25:83c4:0:b0:e03:3683:e67f with SMTP id 3f1490d57ef6-e1a79fed1aamr15821276.5.1725252094100; Sun, 01 Sep 2024 21:41:34 -0700 (PDT) Date: Sun, 1 Sep 2024 21:41:23 -0700 In-Reply-To: <20240902044128.664075-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: <20240902044128.664075-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240902044128.664075-2-surenb@google.com> Subject: [PATCH v2 1/6] maple_tree: add mas_for_each_rev() helper 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, "Liam R. Howlett" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add mas_for_each_rev() function to iterate maple tree nodes in reverse order. Suggested-by: Liam R. Howlett Signed-off-by: Suren Baghdasaryan --- include/linux/maple_tree.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h index 8e1504a81cd2..45e633806da2 100644 --- a/include/linux/maple_tree.h +++ b/include/linux/maple_tree.h @@ -592,6 +592,20 @@ static __always_inline void mas_reset(struct ma_state = *mas) #define mas_for_each(__mas, __entry, __max) \ while (((__entry) =3D mas_find((__mas), (__max))) !=3D NULL) =20 +/** + * mas_for_each_rev() - Iterate over a range of the maple tree in reverse = order. + * @__mas: Maple Tree operation state (maple_state) + * @__entry: Entry retrieved from the tree + * @__min: minimum index to retrieve from the tree + * + * When returned, mas->index and mas->last will hold the entire range for = the + * entry. + * + * Note: may return the zero entry. + */ +#define mas_for_each_rev(__mas, __entry, __min) \ + while (((__entry) =3D mas_find_rev((__mas), (__min))) !=3D NULL) + #ifdef CONFIG_DEBUG_MAPLE_TREE enum mt_dump_format { mt_dump_dec, --=20 2.46.0.469.g59c65b2a67-goog From nobody Fri Dec 19 08:56:28 2025 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 752DB15250F for ; Mon, 2 Sep 2024 04:41:37 +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=1725252100; cv=none; b=f9Fe/h7JPoixKi/+sATmSpyJUfdp/SL8E21QGQMMSmLgFinD6vNa36nSHEdtpXnETKaideD5zCTUYSoDC9RRECtXH99I7fo9alTOXSXVVIwC7oXWnDiaOXc/hTbcIHTUKCYR7+ea/wjuhQLqUneNlYjIv9K3d278bPjPGTCzrdE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252100; c=relaxed/simple; bh=lOxkgf8EnbY8t+TCKSnY1dPXjKXs2qlyAArAYfs10Rg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eLxncMg4AipNdkNSTOy5ZaSE3pxexA7czUUL54JKKaqoqsOyxLsxEJbq1Dn6ABe71LMvC7YvMJbmh+HAFflyWOP4JW/aIVL+mJo6h7MjM/DKOnoMrwiBFnaVXUt71gRmJc3it25+cgPAHEG9UEccjd6A8WTSZ9ijiE+ebG6nw4Y= 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=KAD/66HA; 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="KAD/66HA" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6d4b832fb63so51625417b3.2 for ; Sun, 01 Sep 2024 21:41:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725252096; x=1725856896; 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=dp4HLFsSdaCRLqkUULTSVafVtTTe/LDKPcvkJXQF4Ug=; b=KAD/66HAA1fZ10r6acv6HcDBc4WQn9Fu8XxfRCDmIW9vh2rkfC/2CO2kmybhxaOoUM OLRx2ttDBDyp5ChJgCeDai8oKTPg95L148puV4/Ny+SJr2SzQh8p8aJA3/lmBQn2oy8D aTLOcX4DFvA8HiAtvNJ/tzO9ZVjhT+vcOIPCegG7HhLtUUT4wEetnz2OjxIDpA1oC9Yd Oj0vbnycBBZAKKq6paVVMNaL5HmURIMZ3yh2gWRbs8yE9xuzI1H5FOJqdb8PGgw8gh+d Nw6M71FOz311MBLpvfcR8X37ZD4dYtNd5btyW8Zr3V56BjsBSGlKEiEGgzAygyeF/Von UvpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725252096; x=1725856896; 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=dp4HLFsSdaCRLqkUULTSVafVtTTe/LDKPcvkJXQF4Ug=; b=HmvNQZg/4+J0e3PgLXRWfXSc6jpPa7qJ/Y4ipBMsMUqVkXO2A+EdhG9lliomp0hpht 2tB55pVoqDOvWU6X7lZaeFJAzkCsRkeXX1n5pXjd5XQdWKrO9huXEc2eFp4GPQqtLSas YCYwRHwVjZjiaOa3S1O82QvxEdylg/4vkaNIXX+r75+AQ/TIuizyZtPs8y/34g3NC1BP O+zE54ICr/EOKTLCX3IEeKrxErjDsWxuIS5fa8dUZkHdhSSIaSrF01LuiKub623rZHqO dIfL6lfyP1p2wTaW1y5d8vws8Hfo+tzPxJezFoDSGogik1pvbK17dODm2vX3ZsFbAAt6 aSjw== X-Forwarded-Encrypted: i=1; AJvYcCWrwVktqhT8CejKvRgFRZj8FIYBb7lg2Lv0PhZBNrNf6dJ2jdBpXnGBqBYpNjRDaRVlaK0/kCcZ0euSNgI=@vger.kernel.org X-Gm-Message-State: AOJu0YwaCVEJGUc5Eyx/7oD9zPwTW83QSkvPmzZLs3WHZU5vhn3uN1md KZVFkyFE3IFCMrzyR9arevI/evGp4G8Y1VfRh+OvKQ2qxu2OBphTzopSUCcJ5oSU3SNIf53AMWm EQw== X-Google-Smtp-Source: AGHT+IEB96AfQMtGaWG34HjG3YUsNj+jjgM1XA7e+3hjtS0U7EuT1s7DnlwBcK3PHwYEM5jDMTEBB/kupyA= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:7343:ecd2:aed:5b8f]) (user=surenb job=sendgmr) by 2002:a05:690c:3386:b0:68d:52a1:bed with SMTP id 00721157ae682-6d40d88f6eemr5995947b3.1.1725252096434; Sun, 01 Sep 2024 21:41:36 -0700 (PDT) Date: Sun, 1 Sep 2024 21:41:24 -0700 In-Reply-To: <20240902044128.664075-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: <20240902044128.664075-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240902044128.664075-3-surenb@google.com> Subject: [PATCH v2 2/6] alloc_tag: load module tags into separate continuous memory 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" When a module gets unloaded there is a possibility that some of the allocations it made are still used and therefore the allocation tags corresponding to these allocations are still referenced. As such, the memory for these tags can't be freed. This is currently handled as an abnormal situation and module's data section is not being unloaded. To handle this situation without keeping module's data in memory, allow codetags with longer lifespan than the module to be loaded into their own separate memory. The in-use memory areas and gaps after module unloading in this separate memory are tracked using maple trees. Allocation tags arrange their separate memory so that it is virtually contiguous and that will allow simple allocation tag indexing later on in this patchset. The size of this virtually contiguous memory is set to store up to 100000 allocation tags and max_module_alloc_tags kernel parameter is introduced to change this size. Signed-off-by: Suren Baghdasaryan --- .../admin-guide/kernel-parameters.txt | 4 + include/asm-generic/codetag.lds.h | 19 ++ include/linux/alloc_tag.h | 13 +- include/linux/codetag.h | 37 ++- kernel/module/main.c | 67 +++-- lib/alloc_tag.c | 265 ++++++++++++++++-- lib/codetag.c | 100 ++++++- scripts/module.lds.S | 5 +- 8 files changed, 451 insertions(+), 59 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index 8dd0aefea01e..991b1c9ecf0e 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -3205,6 +3205,10 @@ devices can be requested on-demand with the /dev/loop-control interface. =20 + + max_module_alloc_tags=3D [KNL] Max supported number of allocation tags + in modules. + mce [X86-32] Machine Check Exception =20 mce=3Doption [X86-64] See Documentation/arch/x86/x86_64/boot-options.rst diff --git a/include/asm-generic/codetag.lds.h b/include/asm-generic/codeta= g.lds.h index 64f536b80380..372c320c5043 100644 --- a/include/asm-generic/codetag.lds.h +++ b/include/asm-generic/codetag.lds.h @@ -11,4 +11,23 @@ #define CODETAG_SECTIONS() \ SECTION_WITH_BOUNDARIES(alloc_tags) =20 +/* + * Module codetags which aren't used after module unload, therefore have t= he + * same lifespan as the module and can be safely unloaded with the module. + */ +#define MOD_CODETAG_SECTIONS() + +#define MOD_SEPARATE_CODETAG_SECTION(_name) \ + .codetag.##_name : { \ + SECTION_WITH_BOUNDARIES(_name) \ + } + +/* + * For codetags which might be used after module unload, therefore might s= tay + * longer in memory. Each such codetag type has its own section so that we= can + * unload them individually once unused. + */ +#define MOD_SEPARATE_CODETAG_SECTIONS() \ + MOD_SEPARATE_CODETAG_SECTION(alloc_tags) + #endif /* __ASM_GENERIC_CODETAG_LDS_H */ diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 8c61ccd161ba..99cbc7f086ad 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -30,6 +30,13 @@ struct alloc_tag { struct alloc_tag_counters __percpu *counters; } __aligned(8); =20 +struct alloc_tag_module_section { + unsigned long start_addr; + unsigned long end_addr; + /* used size */ + unsigned long size; +}; + #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG =20 #define CODETAG_EMPTY ((void *)1) @@ -54,6 +61,8 @@ static inline void set_codetag_empty(union codetag_ref *r= ef) {} =20 #ifdef CONFIG_MEM_ALLOC_PROFILING =20 +#define ALLOC_TAG_SECTION_NAME "alloc_tags" + struct codetag_bytes { struct codetag *ct; s64 bytes; @@ -76,7 +85,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_= tag); =20 #define DEFINE_ALLOC_TAG(_alloc_tag) \ static struct alloc_tag _alloc_tag __used __aligned(8) \ - __section("alloc_tags") =3D { \ + __section(ALLOC_TAG_SECTION_NAME) =3D { \ .ct =3D CODE_TAG_INIT, \ .counters =3D &_shared_alloc_tag }; =20 @@ -85,7 +94,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_= tag); #define DEFINE_ALLOC_TAG(_alloc_tag) \ static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \ static struct alloc_tag _alloc_tag __used __aligned(8) \ - __section("alloc_tags") =3D { \ + __section(ALLOC_TAG_SECTION_NAME) =3D { \ .ct =3D CODE_TAG_INIT, \ .counters =3D &_alloc_tag_cntr }; =20 diff --git a/include/linux/codetag.h b/include/linux/codetag.h index c2a579ccd455..fb4e7adfa746 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -35,8 +35,15 @@ struct codetag_type_desc { size_t tag_size; void (*module_load)(struct codetag_type *cttype, struct codetag_module *cmod); - bool (*module_unload)(struct codetag_type *cttype, + void (*module_unload)(struct codetag_type *cttype, struct codetag_module *cmod); +#ifdef CONFIG_MODULES + void (*module_replaced)(struct module *mod, struct module *new_mod); + bool (*needs_section_mem)(struct module *mod, unsigned long size); + void *(*alloc_section_mem)(struct module *mod, unsigned long size, + unsigned int prepend, unsigned long align); + void (*free_section_mem)(struct module *mod, bool unused); +#endif }; =20 struct codetag_iterator { @@ -71,11 +78,31 @@ struct codetag_type * codetag_register_type(const struct codetag_type_desc *desc); =20 #if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) + +bool codetag_needs_module_section(struct module *mod, const char *name, + unsigned long size); +void *codetag_alloc_module_section(struct module *mod, const char *name, + unsigned long size, unsigned int prepend, + unsigned long align); +void codetag_free_module_sections(struct module *mod); +void codetag_module_replaced(struct module *mod, struct module *new_mod); void codetag_load_module(struct module *mod); -bool codetag_unload_module(struct module *mod); -#else +void codetag_unload_module(struct module *mod); + +#else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */ + +static inline bool +codetag_needs_module_section(struct module *mod, const char *name, + unsigned long size) { return false; } +static inline void * +codetag_alloc_module_section(struct module *mod, const char *name, + unsigned long size, unsigned int prepend, + unsigned long align) { return NULL; } +static inline void codetag_free_module_sections(struct module *mod) {} +static inline void codetag_module_replaced(struct module *mod, struct modu= le *new_mod) {} static inline void codetag_load_module(struct module *mod) {} -static inline bool codetag_unload_module(struct module *mod) { return true= ; } -#endif +static inline void codetag_unload_module(struct module *mod) {} + +#endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */ =20 #endif /* _LINUX_CODETAG_H */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 71396e297499..d195d788835c 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1225,18 +1225,12 @@ static int module_memory_alloc(struct module *mod, = enum mod_mem_type type) return 0; } =20 -static void module_memory_free(struct module *mod, enum mod_mem_type type, - bool unload_codetags) +static void module_memory_free(struct module *mod, enum mod_mem_type type) { - void *ptr =3D mod->mem[type].base; - - if (!unload_codetags && mod_mem_type_is_core_data(type)) - return; - - execmem_free(ptr); + execmem_free(mod->mem[type].base); } =20 -static void free_mod_mem(struct module *mod, bool unload_codetags) +static void free_mod_mem(struct module *mod) { for_each_mod_mem_type(type) { struct module_memory *mod_mem =3D &mod->mem[type]; @@ -1247,25 +1241,20 @@ static void free_mod_mem(struct module *mod, bool u= nload_codetags) /* Free lock-classes; relies on the preceding sync_rcu(). */ lockdep_free_key_range(mod_mem->base, mod_mem->size); if (mod_mem->size) - module_memory_free(mod, type, unload_codetags); + module_memory_free(mod, type); } =20 /* MOD_DATA hosts mod, so free it at last */ lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); - module_memory_free(mod, MOD_DATA, unload_codetags); + module_memory_free(mod, MOD_DATA); } =20 /* Free a module, remove from lists, etc. */ static void free_module(struct module *mod) { - bool unload_codetags; - trace_module_free(mod); =20 - unload_codetags =3D codetag_unload_module(mod); - if (!unload_codetags) - pr_warn("%s: memory allocation(s) from the module still alive, cannot un= load cleanly\n", - mod->name); + codetag_unload_module(mod); =20 mod_sysfs_teardown(mod); =20 @@ -1308,7 +1297,7 @@ static void free_module(struct module *mod) kfree(mod->args); percpu_modfree(mod); =20 - free_mod_mem(mod, unload_codetags); + free_mod_mem(mod); } =20 void *__symbol_get(const char *symbol) @@ -1573,6 +1562,14 @@ static void __layout_sections(struct module *mod, st= ruct load_info *info, bool i if (WARN_ON_ONCE(type =3D=3D MOD_INVALID)) continue; =20 + /* + * Do not allocate codetag memory as we load it into + * preallocated contiguous memory. s->sh_entsize will + * not be used for this section, so leave it as is. + */ + if (codetag_needs_module_section(mod, sname, s->sh_size)) + continue; + s->sh_entsize =3D module_get_offset_and_type(mod, type, s, i); pr_debug("\t%s\n", sname); } @@ -2247,6 +2244,7 @@ static int move_module(struct module *mod, struct loa= d_info *info) int i; enum mod_mem_type t =3D 0; int ret =3D -ENOMEM; + bool codetag_section_found =3D false; =20 for_each_mod_mem_type(type) { if (!mod->mem[type].size) { @@ -2257,7 +2255,7 @@ static int move_module(struct module *mod, struct loa= d_info *info) ret =3D module_memory_alloc(mod, type); if (ret) { t =3D type; - goto out_enomem; + goto out_err; } } =20 @@ -2267,11 +2265,27 @@ static int move_module(struct module *mod, struct l= oad_info *info) void *dest; Elf_Shdr *shdr =3D &info->sechdrs[i]; enum mod_mem_type type =3D shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; + const char *sname; =20 if (!(shdr->sh_flags & SHF_ALLOC)) continue; =20 - dest =3D mod->mem[type].base + (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MAS= K); + sname =3D info->secstrings + shdr->sh_name; + /* + * Load codetag sections separately as they might still be used + * after module unload. + */ + if (codetag_needs_module_section(mod, sname, shdr->sh_size)) { + dest =3D codetag_alloc_module_section(mod, sname, shdr->sh_size, + arch_mod_section_prepend(mod, i), shdr->sh_addralign); + if (IS_ERR(dest)) { + ret =3D PTR_ERR(dest); + goto out_err; + } + codetag_section_found =3D true; + } else { + dest =3D mod->mem[type].base + (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MA= SK); + } =20 if (shdr->sh_type !=3D SHT_NOBITS) { /* @@ -2283,7 +2297,7 @@ static int move_module(struct module *mod, struct loa= d_info *info) if (i =3D=3D info->index.mod && (WARN_ON_ONCE(shdr->sh_size !=3D sizeof(struct module)))) { ret =3D -ENOEXEC; - goto out_enomem; + goto out_err; } memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size); } @@ -2299,9 +2313,12 @@ static int move_module(struct module *mod, struct lo= ad_info *info) } =20 return 0; -out_enomem: +out_err: for (t--; t >=3D 0; t--) - module_memory_free(mod, t, true); + module_memory_free(mod, t); + if (codetag_section_found) + codetag_free_module_sections(mod); + return ret; } =20 @@ -2422,6 +2439,8 @@ static struct module *layout_and_allocate(struct load= _info *info, int flags) /* Module has been copied to its final place now: return it. */ mod =3D (void *)info->sechdrs[info->index.mod].sh_addr; kmemleak_load_module(mod, info); + codetag_module_replaced(info->mod, mod); + return mod; } =20 @@ -2431,7 +2450,7 @@ static void module_deallocate(struct module *mod, str= uct load_info *info) percpu_modfree(mod); module_arch_freeing_init(mod); =20 - free_mod_mem(mod, true); + free_mod_mem(mod); } =20 int __weak module_finalize(const Elf_Ehdr *hdr, diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 81e5f9a70f22..19ef02a18611 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only #include +#include #include #include #include @@ -149,30 +150,244 @@ static void __init procfs_init(void) proc_create_seq("allocinfo", 0400, NULL, &allocinfo_seq_op); } =20 -static bool alloc_tag_module_unload(struct codetag_type *cttype, - struct codetag_module *cmod) +#ifdef CONFIG_MODULES + +static struct maple_tree mod_area_mt =3D MTREE_INIT(mod_area_mt, MT_FLAGS_= ALLOC_RANGE); +/* A dummy object used to indicate an unloaded module */ +static struct module unloaded_mod; +/* A dummy object used to indicate a module prepended area */ +static struct module prepend_mod; + +static struct alloc_tag_module_section module_tags; + +static bool needs_section_mem(struct module *mod, unsigned long size) { - struct codetag_iterator iter =3D codetag_get_ct_iter(cttype); - struct alloc_tag_counters counter; - bool module_unused =3D true; + return size >=3D sizeof(struct alloc_tag); +} + +/* Called under RCU read lock */ +static void clean_unused_module_areas_locked(void) +{ + MA_STATE(mas, &mod_area_mt, 0, module_tags.size); + struct module *val; + + mas_for_each(&mas, val, module_tags.size) { + if (val =3D=3D &unloaded_mod) { + struct alloc_tag *tag; + struct alloc_tag *last; + bool unused =3D true; + + tag =3D (struct alloc_tag *)(module_tags.start_addr + mas.index); + last =3D (struct alloc_tag *)(module_tags.start_addr + mas.last); + while (tag <=3D last) { + struct alloc_tag_counters counter; + + counter =3D alloc_tag_read(tag); + if (counter.bytes) { + unused =3D false; + break; + } + tag++; + } + if (unused) + mas_erase(&mas); + } + } +} + +static void *reserve_module_tags(struct module *mod, unsigned long size, + unsigned int prepend, unsigned long align) +{ + unsigned long section_size =3D module_tags.end_addr - module_tags.start_a= ddr; + MA_STATE(mas, &mod_area_mt, 0, section_size - 1); + bool cleanup_done =3D false; + unsigned long offset; + void *ret; + + /* If no tags return NULL */ + if (size < sizeof(struct alloc_tag)) + return NULL; + + /* + * align is always power of 2, so we can use IS_ALIGNED and ALIGN. + * align 0 or 1 means no alignment, to simplify set to 1. + */ + if (!align) + align =3D 1; + + mas_lock(&mas); +repeat: + /* Try finding exact size and hope the start is aligned */ + if (mas_empty_area(&mas, 0, section_size - 1, prepend + size)) + goto cleanup; + + if (IS_ALIGNED(mas.index + prepend, align)) + goto found; + + /* Try finding larger area to align later */ + mas_reset(&mas); + if (!mas_empty_area(&mas, 0, section_size - 1, + size + prepend + align - 1)) + goto found; + +cleanup: + /* No free area, try cleanup stale data and repeat the search once */ + if (!cleanup_done) { + clean_unused_module_areas_locked(); + cleanup_done =3D true; + mas_reset(&mas); + goto repeat; + } else { + ret =3D ERR_PTR(-ENOMEM); + goto out; + } + +found: + offset =3D mas.index; + offset +=3D prepend; + offset =3D ALIGN(offset, align); + + if (offset !=3D mas.index) { + unsigned long pad_start =3D mas.index; + + mas.last =3D offset - 1; + mas_store(&mas, &prepend_mod); + if (mas_is_err(&mas)) { + ret =3D ERR_PTR(xa_err(mas.node)); + goto out; + } + mas.index =3D offset; + mas.last =3D offset + size - 1; + mas_store(&mas, mod); + if (mas_is_err(&mas)) { + ret =3D ERR_PTR(xa_err(mas.node)); + mas.index =3D pad_start; + mas_erase(&mas); + goto out; + } + + } else { + mas.last =3D offset + size - 1; + mas_store(&mas, mod); + if (mas_is_err(&mas)) { + ret =3D ERR_PTR(xa_err(mas.node)); + goto out; + } + } + + if (module_tags.size < offset + size) + module_tags.size =3D offset + size; + + ret =3D (struct alloc_tag *)(module_tags.start_addr + offset); +out: + mas_unlock(&mas); + + return ret; +} + +static void release_module_tags(struct module *mod, bool unused) +{ + MA_STATE(mas, &mod_area_mt, module_tags.size, module_tags.size); + struct alloc_tag *last; struct alloc_tag *tag; - struct codetag *ct; + struct module *val; =20 - for (ct =3D codetag_next_ct(&iter); ct; ct =3D codetag_next_ct(&iter)) { - if (iter.cmod !=3D cmod) - continue; + if (unused) + return; + + mas_lock(&mas); + mas_for_each_rev(&mas, val, 0) + if (val =3D=3D mod) + break; + + if (!val) /* module not found */ + goto out; + + tag =3D (struct alloc_tag *)(module_tags.start_addr + mas.index); + last =3D (struct alloc_tag *)(module_tags.start_addr + mas.last); + + unused =3D true; + while (tag <=3D last) { + struct alloc_tag_counters counter; =20 - tag =3D ct_to_alloc_tag(ct); counter =3D alloc_tag_read(tag); + if (counter.bytes) { + struct codetag *ct =3D &tag->ct; + + pr_info("%s:%u module %s func:%s has %llu allocated at module unload\n", + ct->filename, ct->lineno, ct->modname, + ct->function, counter.bytes); + unused =3D false; + break; + } + tag++; + } + + mas_store(&mas, unused ? NULL : &unloaded_mod); + val =3D mas_prev_range(&mas, 0); + if (val =3D=3D &prepend_mod) + mas_store(&mas, NULL); +out: + mas_unlock(&mas); +} + +static void replace_module(struct module *mod, struct module *new_mod) +{ + MA_STATE(mas, &mod_area_mt, 0, module_tags.size); + struct module *val; =20 - if (WARN(counter.bytes, - "%s:%u module %s func:%s has %llu allocated at module unload", - ct->filename, ct->lineno, ct->modname, ct->function, counter.bytes)) - module_unused =3D false; + mas_lock(&mas); + mas_for_each(&mas, val, module_tags.size) { + if (val !=3D mod) + continue; + + mas_store_gfp(&mas, new_mod, GFP_KERNEL); + break; } + mas_unlock(&mas); +} + +static unsigned long max_module_alloc_tags __initdata =3D 100000; =20 - return module_unused; +static int __init set_max_module_alloc_tags(char *arg) +{ + if (!arg) + return -EINVAL; + + return kstrtoul(arg, 0, &max_module_alloc_tags); } +early_param("max_module_alloc_tags", set_max_module_alloc_tags); + +static int __init alloc_mod_tags_mem(void) +{ + unsigned long module_tags_mem_sz; + + module_tags_mem_sz =3D max_module_alloc_tags * sizeof(struct alloc_tag); + pr_info("%lu bytes reserved for module allocation tags\n", module_tags_me= m_sz); + + /* Allocate space to copy allocation tags */ + module_tags.start_addr =3D (unsigned long)execmem_alloc(EXECMEM_MODULE_DA= TA, + module_tags_mem_sz); + if (!module_tags.start_addr) + return -ENOMEM; + + module_tags.end_addr =3D module_tags.start_addr + module_tags_mem_sz; + + return 0; +} + +static void __init free_mod_tags_mem(void) +{ + execmem_free((void *)module_tags.start_addr); + module_tags.start_addr =3D 0; +} + +#else /* CONFIG_MODULES */ + +static inline int alloc_mod_tags_mem(void) { return 0; } +static inline void free_mod_tags_mem(void) {} + +#endif /* CONFIG_MODULES */ =20 #ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT static bool mem_profiling_support __meminitdata =3D true; @@ -255,14 +470,26 @@ static inline void sysctl_init(void) {} static int __init alloc_tag_init(void) { const struct codetag_type_desc desc =3D { - .section =3D "alloc_tags", - .tag_size =3D sizeof(struct alloc_tag), - .module_unload =3D alloc_tag_module_unload, + .section =3D ALLOC_TAG_SECTION_NAME, + .tag_size =3D sizeof(struct alloc_tag), +#ifdef CONFIG_MODULES + .needs_section_mem =3D needs_section_mem, + .alloc_section_mem =3D reserve_module_tags, + .free_section_mem =3D release_module_tags, + .module_replaced =3D replace_module, +#endif }; + int res; + + res =3D alloc_mod_tags_mem(); + if (res) + return res; =20 alloc_tag_cttype =3D codetag_register_type(&desc); - if (IS_ERR(alloc_tag_cttype)) + if (IS_ERR(alloc_tag_cttype)) { + free_mod_tags_mem(); return PTR_ERR(alloc_tag_cttype); + } =20 sysctl_init(); procfs_init(); diff --git a/lib/codetag.c b/lib/codetag.c index afa8a2d4f317..60463ef4bb85 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -207,6 +207,94 @@ static int codetag_module_init(struct codetag_type *ct= type, struct module *mod) } =20 #ifdef CONFIG_MODULES +#define CODETAG_SECTION_PREFIX ".codetag." + +/* Some codetag types need a separate module section */ +bool codetag_needs_module_section(struct module *mod, const char *name, + unsigned long size) +{ + const char *type_name; + struct codetag_type *cttype; + bool ret =3D false; + + if (strncmp(name, CODETAG_SECTION_PREFIX, strlen(CODETAG_SECTION_PREFIX))) + return false; + + type_name =3D name + strlen(CODETAG_SECTION_PREFIX); + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + if (strcmp(type_name, cttype->desc.section) =3D=3D 0) { + if (!cttype->desc.needs_section_mem) + break; + + down_write(&cttype->mod_lock); + ret =3D cttype->desc.needs_section_mem(mod, size); + up_write(&cttype->mod_lock); + break; + } + } + mutex_unlock(&codetag_lock); + + return ret; +} + +void *codetag_alloc_module_section(struct module *mod, const char *name, + unsigned long size, unsigned int prepend, + unsigned long align) +{ + const char *type_name =3D name + strlen(CODETAG_SECTION_PREFIX); + struct codetag_type *cttype; + void *ret =3D NULL; + + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + if (strcmp(type_name, cttype->desc.section) =3D=3D 0) { + if (WARN_ON(!cttype->desc.alloc_section_mem)) + break; + + down_write(&cttype->mod_lock); + ret =3D cttype->desc.alloc_section_mem(mod, size, prepend, align); + up_write(&cttype->mod_lock); + break; + } + } + mutex_unlock(&codetag_lock); + + return ret; +} + +void codetag_free_module_sections(struct module *mod) +{ + struct codetag_type *cttype; + + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + if (!cttype->desc.free_section_mem) + continue; + + down_write(&cttype->mod_lock); + cttype->desc.free_section_mem(mod, true); + up_write(&cttype->mod_lock); + } + mutex_unlock(&codetag_lock); +} + +void codetag_module_replaced(struct module *mod, struct module *new_mod) +{ + struct codetag_type *cttype; + + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + if (!cttype->desc.module_replaced) + continue; + + down_write(&cttype->mod_lock); + cttype->desc.module_replaced(mod, new_mod); + up_write(&cttype->mod_lock); + } + mutex_unlock(&codetag_lock); +} + void codetag_load_module(struct module *mod) { struct codetag_type *cttype; @@ -220,13 +308,12 @@ void codetag_load_module(struct module *mod) mutex_unlock(&codetag_lock); } =20 -bool codetag_unload_module(struct module *mod) +void codetag_unload_module(struct module *mod) { struct codetag_type *cttype; - bool unload_ok =3D true; =20 if (!mod) - return true; + return; =20 mutex_lock(&codetag_lock); list_for_each_entry(cttype, &codetag_types, link) { @@ -243,18 +330,17 @@ bool codetag_unload_module(struct module *mod) } if (found) { if (cttype->desc.module_unload) - if (!cttype->desc.module_unload(cttype, cmod)) - unload_ok =3D false; + cttype->desc.module_unload(cttype, cmod); =20 cttype->count -=3D range_size(cttype, &cmod->range); idr_remove(&cttype->mod_idr, mod_id); kfree(cmod); } up_write(&cttype->mod_lock); + if (found && cttype->desc.free_section_mem) + cttype->desc.free_section_mem(mod, false); } mutex_unlock(&codetag_lock); - - return unload_ok; } #endif /* CONFIG_MODULES */ =20 diff --git a/scripts/module.lds.S b/scripts/module.lds.S index 3f43edef813c..711c6e029936 100644 --- a/scripts/module.lds.S +++ b/scripts/module.lds.S @@ -50,7 +50,7 @@ SECTIONS { .data : { *(.data .data.[0-9a-zA-Z_]*) *(.data..L*) - CODETAG_SECTIONS() + MOD_CODETAG_SECTIONS() } =20 .rodata : { @@ -59,9 +59,10 @@ SECTIONS { } #else .data : { - CODETAG_SECTIONS() + MOD_CODETAG_SECTIONS() } #endif + MOD_SEPARATE_CODETAG_SECTIONS() } =20 /* bring in arch-specific sections */ --=20 2.46.0.469.g59c65b2a67-goog From nobody Fri Dec 19 08:56:28 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 91E281547EE for ; Mon, 2 Sep 2024 04:41:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252101; cv=none; b=FaAj576hDh0LyThxbcrIZIYcdfvWFL4JFTrnQcni8Ycve4DP4ivOG7o+FcPj3oGVySASF1yk26wb7AJV2kAhfFuioR3+3iaGvf/mkevR9jhZH2yolW9rVkeLcOKYoM42bSSKWiXjye/W/5uDRYSBP+bcmrP50jkHuX4hgdtjAL0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252101; c=relaxed/simple; bh=WZGSZDDwwBcvVvHjYbdkt42ukG4S2dTzJF74pYfBzvM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KkgplOTYc9BENi1o7Jg0w0raThrXoncQ2dDehdsjXDi7E4kYwHMwgyoBgmw92p9aWCc1WBfoJnFJo05vvDkY1pbXBD/HAKR4LSN0ARR2mXgt3TLt2DMS83Q+ZBEZ7TYtkr6rjYiaCiyoGk6KnR00e2zLMvXh+m/F6B7HAk+cLwc= 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=4JmcXONf; arc=none smtp.client-ip=209.85.219.201 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="4JmcXONf" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1a8de19944so3258694276.1 for ; Sun, 01 Sep 2024 21:41:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725252099; x=1725856899; 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=GtHW3QbMIbL8VkRhZ9UoruDWIR+5Ejb5CjRZSzRSfG0=; b=4JmcXONfpduox86Z3T05T4h09vS+JR41ggjQq6WCOLEgnoRyFAMTcFf3DOepWglhnB riXhLkSAS+5zSOeFkeJ56aRodHoLWuayhLtpXrtzWY05cLrjvjsqcCbGJ4wB8jE82tqT Isd5rtc0Tqa5jga9M7OhgSXpSPf2gMhRlr2+YoM50ANE/fRhFkMsgTMo39ZHIweY1yIM IId71c0yzaJKmVmDZpZQhLcLSVCVKsfRuSeO1Yk0INoWJtxcZBDCp7w1mqX/46NsOJ5X U7NHWHGF1RSIV5xKICoWdy2e/cAFpK4TNFWRT55MzckkodKgT7/+ZMvhArFjhrjGtBoC KdkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725252099; x=1725856899; 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=GtHW3QbMIbL8VkRhZ9UoruDWIR+5Ejb5CjRZSzRSfG0=; b=m8ys/c4upaWDDjQUSpZmmMfjbijGnYhO/rZQ6RwNjfXP8D1fikaBJE6kikid9REpav DscNV5yL/4UpSq/WjoCUoOUztYoqsH86QwHgtBwu6l9LZDI2Q0Gi59sT2j1tkWS+a/DD d6i9OZZ+2a6wCeizPqp9UYvbnWQyBaFrsIkhaWwnlw362QAGKky2wZZSCQ26/UxvGVpB iMudAIM/cxNbZ9DWmC4fUc+JGFKgcFaG3eEMK06gdRDoX2eD9CsoJ+ym2dhoVxLhvv2q KWtfdDAEFFprNKTZJlg6sQoho5z7jWNATnakk7fxPyJlvKuSnrPdK7pe0N8SXtG3UWi2 Bjvg== X-Forwarded-Encrypted: i=1; AJvYcCU9UGg4qQ4G2Gf1HALlrsiSUK2Yo4UiK/ntvazYKvwb1pXoGMdZ79tLMYHDvEqwKm5iS2G2LLMl/2NW7ig=@vger.kernel.org X-Gm-Message-State: AOJu0YydO3HxYgZlfXWmLYqPtpqYRkYbAuQIJkreaSVPBvZwPtFKX9bN LL5acceG5lAOCkduWZXV8oEHWShi8BesrX/N2WD+zXEAuIGzTgdRvc7IfWblvxJ2r1t84zUuraZ Lpw== X-Google-Smtp-Source: AGHT+IGHUG44Vos/pxlFWbZNjhZtZ0OaAmv4KSGmEpEw821HAQXG3t5pT+l+pURZYG9PiSEp/j/9qgeoB6s= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:7343:ecd2:aed:5b8f]) (user=surenb job=sendgmr) by 2002:a25:7e46:0:b0:e0b:ab63:b9c8 with SMTP id 3f1490d57ef6-e1a7a3dbab3mr14893276.11.1725252098507; Sun, 01 Sep 2024 21:41:38 -0700 (PDT) Date: Sun, 1 Sep 2024 21:41:25 -0700 In-Reply-To: <20240902044128.664075-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: <20240902044128.664075-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240902044128.664075-4-surenb@google.com> Subject: [PATCH v2 3/6] alloc_tag: eliminate alloc_tag_ref_set 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 further refactoring, open-code the only two callers of alloc_tag_ref_set(). Signed-off-by: Suren Baghdasaryan --- include/linux/alloc_tag.h | 25 ++----------------------- include/linux/pgalloc_tag.h | 12 +++++++++++- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 99cbc7f086ad..21e3098220e3 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -143,36 +143,15 @@ static inline void alloc_tag_add_check(union codetag_= ref *ref, struct alloc_tag static inline void alloc_tag_sub_check(union codetag_ref *ref) {} #endif =20 -/* Caller should verify both ref and tag to be valid */ -static inline void __alloc_tag_ref_set(union codetag_ref *ref, struct allo= c_tag *tag) -{ - ref->ct =3D &tag->ct; - /* - * We need in increment the call counter every time we have a new - * allocation or when we split a large allocation into smaller ones. - * Each new reference for every sub-allocation needs to increment call - * counter because when we free each part the counter will be decremented. - */ - this_cpu_inc(tag->counters->calls); -} - -static inline void alloc_tag_ref_set(union codetag_ref *ref, struct alloc_= tag *tag) -{ - alloc_tag_add_check(ref, tag); - if (!ref || !tag) - return; - - __alloc_tag_ref_set(ref, tag); -} - static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag = *tag, size_t bytes) { alloc_tag_add_check(ref, tag); if (!ref || !tag) return; =20 - __alloc_tag_ref_set(ref, tag); + ref->ct =3D &tag->ct; this_cpu_add(tag->counters->bytes, bytes); + this_cpu_inc(tag->counters->calls); } =20 static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 207f0c83c8e9..244a328dff62 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -103,7 +103,17 @@ static inline void pgalloc_tag_split(struct page *page= , unsigned int nr) page_ext =3D page_ext_next(page_ext); for (i =3D 1; i < nr; i++) { /* Set new reference to point to the original tag */ - alloc_tag_ref_set(codetag_ref_from_page_ext(page_ext), tag); + ref =3D codetag_ref_from_page_ext(page_ext); + alloc_tag_add_check(ref, tag); + if (ref) { + 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); + } page_ext =3D page_ext_next(page_ext); } out: --=20 2.46.0.469.g59c65b2a67-goog From nobody Fri Dec 19 08:56:28 2025 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 6E24F14900E for ; Mon, 2 Sep 2024 04:41:41 +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=1725252103; cv=none; b=BiAhWWfqmnTUHcymxehgnR79rfB/knnOiFrglt1sblU1UJ20nhvT2SelCNXHZiiUQ+cUh18ZFX9ZYnE/MbkkabmobFcpzMk27bG2nUNP6zevKD6DXXVer3ouS+nwwxC3lU6ATJVqC8wdH42NypOCuv6xQCsEW0bkM411ZMPIpI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252103; c=relaxed/simple; bh=Rhk63JfhtTaFNKYYhJhXrWW/4atvFHUKwZPMdC92TXw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ddbCwpDZKU2v395Xx89QIfiKEm+xsXCTX+Xv9GQUgaylFOqo4ONWeiAkSgoKLJEo9pDEy8kepyxro9TJdsj3kzl6OV8afAMj1bIJR0Hja8LLT50hFv0Igw64mbYHiGOzwAyh9EWTrEXQptauavKViMqC5DcrA5XjInnzfsZgZvg= 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=EU19C1Gk; 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="EU19C1Gk" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6b41e02c293so79582087b3.0 for ; Sun, 01 Sep 2024 21:41:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725252100; x=1725856900; 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=Nlt0d2kKMzLV8FpvC+sskMXVjz4TJlE3ZyIfZQBAJUA=; b=EU19C1GkZYfto3rmE/KOMuBGkJ+BgoMXANAGGFThodvFXae5qmFoyEnmUGXinaBZ9A 4ejpY/qMA0RF0smVdrtwHlMie/iDZrXZ9kJ4eDIkN12n9vR3XODNkxeTmZzQKTGxjIcB eB69HMiYgPF2biz0X8lCjYL4DGdMoZWRQjosMfVRJensx+gDG78pK6S17uUOHVuJmQlx Swp0cu2M9NHh+dGix/4+XDVTxLjOA94swQtnb6wrzHTrswCeTMl5flanEp04xZTGeiuX XUEhHa0kCoq0MfKhgXLJBPLEn4zlwRKpoPWsBGezJWAwA02Gmd94hygpP5weszBHewNK 6XAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725252100; x=1725856900; 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=Nlt0d2kKMzLV8FpvC+sskMXVjz4TJlE3ZyIfZQBAJUA=; b=DqNhI9NwJH0J6QJIQm1qV7ETzQcbVf/cN7dG+jvlDIsro6zdw0yYUF8nYwao5k12Yc QP9APemi+VmGfZ0EA6hwbrtTNgIgNileJ8E7De95nYVr4B82UKbqZfvbZJDFxQX0XmSJ bDaS2C3jf9+s0iAsbqHsm2XTEo4ne28MHWhtLZzOJ3KfE3p5johCvtYCA6D8z7UYmgu+ NjtAkhDeZ+W1q9jlOej9HXJfgbZnTX1hwmqr/5Qg9SKnhQlFGV+JskRvgTVGh3Ao5Cqx eLUTN1urCFVBLshR+TieHzKOw8t4THkekXnzZKiHYwuFQ9eMaDWuuuHae4JXHvFb5+an 2XSg== X-Forwarded-Encrypted: i=1; AJvYcCUZqgUf3l1cfCTVDjLy1FyYxsnchpT1maa9YXN6xzYliHfc7yXSMJ5FxzSANZGmKEeKmju+Igy8Eo8R/h0=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0G7tKAowaZKV/YkEa3TCCjZ7MzS+lZ0AlzsTRYXQvsXd2iy/H GstjsqdL1+mwkWpYtGU23ocN/wvCORmqjMXzayUwi/uOt3SQq2It5SOzMhwqt/Is4qtS+jLpsTj 4Xg== X-Google-Smtp-Source: AGHT+IHSPd8nZJZH/UC+NYWl2T7llxAohdiabcKHZvTC/kXLRpqPrIn5YCFkqt976gAwR4y0XtmrRfdj/lI= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:7343:ecd2:aed:5b8f]) (user=surenb job=sendgmr) by 2002:a05:690c:6309:b0:691:41f5:7f42 with SMTP id 00721157ae682-6d40f340f7dmr6255217b3.4.1725252100605; Sun, 01 Sep 2024 21:41:40 -0700 (PDT) Date: Sun, 1 Sep 2024 21:41:26 -0700 In-Reply-To: <20240902044128.664075-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: <20240902044128.664075-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240902044128.664075-5-surenb@google.com> Subject: [PATCH v2 4/6] 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 19ef02a18611..53bd3236d30b 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -436,7 +437,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.469.g59c65b2a67-goog From nobody Fri Dec 19 08:56:28 2025 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 A837B15573F for ; Mon, 2 Sep 2024 04:41:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252105; cv=none; b=LrrcF/Lm8D1qfklI3JQGE3jtsDIt5WKSmug6atrQMi3qZMEqMrKZ6tpUkiTXkl/ooADycIXNPTQCmX1m8F4FWjPX0gyysSQ7gSPUDFEQLckm1KDqldblIK/0uzCPxvqnJiNXKanA9URZ693vxW7xokBo1EfpXnBOz9QOOY5IfMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252105; c=relaxed/simple; bh=E4oc23KhDb6ZE9N3AIvlxHWCldpXbbwIWiTUFvrD6V4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UwegcMWuadfx3zGDD60xsYfo8EOTZoC/YXOJBcn9dHxdxM8Wi3TkNryDOy4FTEnJusWHqoiIMvwDmTPabT7vQEF79L27a3VcfFlfy2bDY97vhB90B6U95iEjnlLAEm7Zl/WrCQdRHbfP63LJbzw6wNKZDdt2yt8gnK3RWW62TSg= 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=Lubv6Fnf; arc=none smtp.client-ip=209.85.219.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="Lubv6Fnf" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e02a4de4f4eso7932798276.1 for ; Sun, 01 Sep 2024 21:41:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725252102; x=1725856902; 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=jIAzqL0tmqWuUadipZSgPIzfdbVk1YeOcL9yjZr6O/Q=; b=Lubv6FnfD+nPCUWXX+IoCGhSr1VA+iBmf7a6V6w4v7e5wADCA+icU8uLLKrnpYRFzf FpZI5lqFxXZpK6ifx/ijy5yw9pxvKnwo+lh2JsDNmUIPgmHzZ9PHWECC0D2YK58ZKvo8 dc/bulEdShbMIGCS6IfqbcIPtXOnTwdTh2KM9ZB2A1aDf1hnQvA6hTWSZMkMZyZMpVXl wMsom8T4MU+BZnAWZPi/41b5dZdfMJB3uL63tSDEBQ29fYkMuQuSWFq4/15f3bsLGucs E1aJ3SSjq9un2c7X00effOFUMCDX3JnBBqMEsfxkHSqkJ+b6EYi4B8xmviDztLyIjFHr EuJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725252102; x=1725856902; 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=jIAzqL0tmqWuUadipZSgPIzfdbVk1YeOcL9yjZr6O/Q=; b=Uq3PE2dTNFP/8Mc3/i1IFC7MFqlkCCh3uLYRLzyTacJbk0XlBaPTMCKzkO/bCmHrel amAfF4IfSyPNcxZ7i7ieufPLvrEgBuDipnbbK0LGQqM0x3J/zlyxGu2gFP0kmu2xFGP5 NxO95WEmP223vMWHjmAuj+kALr4922uHNc+jg/2x1vFN0FD6b5CqmH1CNq4vAg+ccJk3 KNA5iiNrD/8MT4jOk9HGahUDO3DW/ahFf942neqChLsNTXfgKemmvbbVLf9LQrDrxo5B st/WXoL+AAnkOMGJ7FwJW0tSAX4h+k6m/auBK8RpNogHY0V0BJ/wA7TXasvCClJ2Vstq FQbg== X-Forwarded-Encrypted: i=1; AJvYcCVS6UFzoydQfUISKb7K7k0PKfIyWZnihq+eyKjV7kYUmoJtWMja90Z2uzntKWlniFBIZA9Pkhr3w474pUs=@vger.kernel.org X-Gm-Message-State: AOJu0YyyZ+iytPIBzUJCuWGEX7MFK3O3Uis6YcOx03kAUcW5hCnhg+W+ ayecE6BbIUWD6Jr6+hbsZSKc2dZ8UiHaD8rzv0hh6TwDI8/xRTRhjuNbw7vvsaqDMqRdGtfsRCR +ZQ== X-Google-Smtp-Source: AGHT+IGweUdtvzfkyW3WmJuGFzyyd2e8B2d2obHsNq5jvXmRb4BdxuAkJtcuoa6tBEzRO4VbtFxeXiTxIrs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:7343:ecd2:aed:5b8f]) (user=surenb job=sendgmr) by 2002:a5b:d06:0:b0:e11:6a73:b0d with SMTP id 3f1490d57ef6-e1a7a171852mr16567276.6.1725252102521; Sun, 01 Sep 2024 21:41:42 -0700 (PDT) Date: Sun, 1 Sep 2024 21:41:27 -0700 In-Reply-To: <20240902044128.664075-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: <20240902044128.664075-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240902044128.664075-6-surenb@google.com> Subject: [PATCH v2 5/6] alloc_tag: make page allocation tag reference size configurable 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" Introduce CONFIG_PGALLOC_TAG_REF_BITS to control the size of the page allocation tag references. When the size is configured to be less than a direct pointer, the tags are searched using an index stored as the tag reference. Signed-off-by: Suren Baghdasaryan --- include/linux/alloc_tag.h | 10 +++- include/linux/codetag.h | 3 ++ include/linux/pgalloc_tag.h | 99 +++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 11 +++++ lib/alloc_tag.c | 51 ++++++++++++++++++- lib/codetag.c | 4 +- mm/mm_init.c | 1 + 7 files changed, 175 insertions(+), 4 deletions(-) diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 21e3098220e3..b5cf24517333 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -30,8 +30,16 @@ struct alloc_tag { struct alloc_tag_counters __percpu *counters; } __aligned(8); =20 +struct alloc_tag_kernel_section { + struct alloc_tag *first_tag; + unsigned long count; +}; + struct alloc_tag_module_section { - unsigned long start_addr; + union { + unsigned long start_addr; + struct alloc_tag *first_tag; + }; unsigned long end_addr; /* used size */ unsigned long size; diff --git a/include/linux/codetag.h b/include/linux/codetag.h index fb4e7adfa746..401fc297eeda 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -13,6 +13,9 @@ struct codetag_module; struct seq_buf; struct module; =20 +#define CODETAG_SECTION_START_PREFIX "__start_" +#define CODETAG_SECTION_STOP_PREFIX "__stop_" + /* * An instance of this structure is created in a special ELF section at ev= ery * code location being tagged. At runtime, the special section is treated= as diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index c76b629d0206..a7f8f00c118f 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -9,7 +9,18 @@ =20 #ifdef CONFIG_MEM_ALLOC_PROFILING =20 +#if !defined(CONFIG_PGALLOC_TAG_REF_BITS) || CONFIG_PGALLOC_TAG_REF_BITS >= 32 +#define PGALLOC_TAG_DIRECT_REF typedef union codetag_ref pgalloc_tag_ref; +#else /* !defined(CONFIG_PGALLOC_TAG_REF_BITS) || CONFIG_PGALLOC_TAG_REF_B= ITS > 32 */ +#if CONFIG_PGALLOC_TAG_REF_BITS > 16 +typedef u32 pgalloc_tag_ref; +#else +typedef u16 pgalloc_tag_ref; +#endif +#endif /* !defined(CONFIG_PGALLOC_TAG_REF_BITS) || CONFIG_PGALLOC_TAG_REF_= BITS > 32 */ + +#ifdef PGALLOC_TAG_DIRECT_REF =20 static inline void read_pgref(pgalloc_tag_ref *pgref, union codetag_ref *r= ef) { @@ -20,6 +31,93 @@ static inline void write_pgref(pgalloc_tag_ref *pgref, u= nion codetag_ref *ref) { pgref->ct =3D ref->ct; } + +static inline void alloc_tag_sec_init(void) {} + +#else /* PGALLOC_TAG_DIRECT_REF */ + +extern struct alloc_tag_kernel_section kernel_tags; + +#define CODETAG_ID_NULL 0 +#define CODETAG_ID_EMPTY 1 +#define CODETAG_ID_FIRST 2 + +#ifdef CONFIG_MODULES + +extern struct alloc_tag_module_section module_tags; + +static inline struct codetag *get_module_ct(pgalloc_tag_ref pgref) +{ + return &module_tags.first_tag[pgref - kernel_tags.count].ct; +} + +static inline pgalloc_tag_ref get_module_pgref(struct alloc_tag *tag) +{ + return CODETAG_ID_FIRST + kernel_tags.count + (tag - module_tags.first_ta= g); +} + +#else /* CONFIG_MODULES */ + +static inline struct codetag *get_module_ct(pgalloc_tag_ref pgref) +{ + pr_warn("invalid page tag reference %lu\n", (unsigned long)pgref); + return NULL; +} + +static inline pgalloc_tag_ref get_module_pgref(struct alloc_tag *tag) +{ + pr_warn("invalid page tag 0x%lx\n", (unsigned long)tag); + return CODETAG_ID_NULL; +} + +#endif /* CONFIG_MODULES */ + +static inline void read_pgref(pgalloc_tag_ref *pgref, union codetag_ref *r= ef) +{ + pgalloc_tag_ref pgref_val =3D *pgref; + + switch (pgref_val) { + case (CODETAG_ID_NULL): + ref->ct =3D NULL; + break; + case (CODETAG_ID_EMPTY): + set_codetag_empty(ref); + break; + default: + pgref_val -=3D CODETAG_ID_FIRST; + ref->ct =3D pgref_val < kernel_tags.count ? + &kernel_tags.first_tag[pgref_val].ct : + get_module_ct(pgref_val); + break; + } +} + +static inline void write_pgref(pgalloc_tag_ref *pgref, union codetag_ref *= ref) +{ + struct alloc_tag *tag; + + if (!ref->ct) { + *pgref =3D CODETAG_ID_NULL; + return; + } + + if (is_codetag_empty(ref)) { + *pgref =3D CODETAG_ID_EMPTY; + return; + } + + tag =3D ct_to_alloc_tag(ref->ct); + if (tag >=3D kernel_tags.first_tag && tag < kernel_tags.first_tag + kerne= l_tags.count) { + *pgref =3D CODETAG_ID_FIRST + (tag - kernel_tags.first_tag); + return; + } + + *pgref =3D get_module_pgref(tag); +} + +void __init alloc_tag_sec_init(void); + +#endif /* PGALLOC_TAG_DIRECT_REF */ #include =20 extern struct page_ext_operations page_alloc_tagging_ops; @@ -197,6 +295,7 @@ static inline void pgalloc_tag_sub(struct page *page, u= nsigned int nr) {} static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {} static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { retur= n NULL; } static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned i= nt nr) {} +static inline void alloc_tag_sec_init(void) {} =20 #endif /* CONFIG_MEM_ALLOC_PROFILING */ =20 diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index a30c03a66172..253f9c2028da 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1000,6 +1000,17 @@ config MEM_ALLOC_PROFILING_DEBUG Adds warnings with helpful error messages for memory allocation profiling. =20 +config PGALLOC_TAG_REF_BITS + int "Number of bits for page allocation tag reference (10-64)" + range 10 64 + default "64" + depends on MEM_ALLOC_PROFILING + help + Number of bits used to encode a page allocation tag reference. + + Smaller number results in less memory overhead but limits the number of + allocations which can be tagged (including allocations from modules). + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan" diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 53bd3236d30b..73791aa55ab6 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -151,6 +152,26 @@ static void __init procfs_init(void) proc_create_seq("allocinfo", 0400, NULL, &allocinfo_seq_op); } =20 +#ifndef PGALLOC_TAG_DIRECT_REF + +#define SECTION_START(NAME) (CODETAG_SECTION_START_PREFIX NAME) +#define SECTION_STOP(NAME) (CODETAG_SECTION_STOP_PREFIX NAME) + +struct alloc_tag_kernel_section kernel_tags =3D { NULL, 0 }; + +void __init alloc_tag_sec_init(void) +{ + struct alloc_tag *last_codetag; + + kernel_tags.first_tag =3D (struct alloc_tag *)kallsyms_lookup_name( + SECTION_START(ALLOC_TAG_SECTION_NAME)); + last_codetag =3D (struct alloc_tag *)kallsyms_lookup_name( + SECTION_STOP(ALLOC_TAG_SECTION_NAME)); + kernel_tags.count =3D last_codetag - kernel_tags.first_tag; +} + +#endif /* PGALLOC_TAG_DIRECT_REF */ + #ifdef CONFIG_MODULES =20 static struct maple_tree mod_area_mt =3D MTREE_INIT(mod_area_mt, MT_FLAGS_= ALLOC_RANGE); @@ -159,7 +180,16 @@ static struct module unloaded_mod; /* A dummy object used to indicate a module prepended area */ static struct module prepend_mod; =20 -static struct alloc_tag_module_section module_tags; +struct alloc_tag_module_section module_tags; + +#ifndef PGALLOC_TAG_DIRECT_REF +static inline unsigned long alloc_tag_align(unsigned long val) +{ + if (val % sizeof(struct alloc_tag) =3D=3D 0) + return val; + return ((val / sizeof(struct alloc_tag)) + 1) * sizeof(struct alloc_tag); +} +#endif /* PGALLOC_TAG_DIRECT_REF */ =20 static bool needs_section_mem(struct module *mod, unsigned long size) { @@ -216,6 +246,21 @@ static void *reserve_module_tags(struct module *mod, u= nsigned long size, if (!align) align =3D 1; =20 +#ifndef PGALLOC_TAG_DIRECT_REF + /* + * If alloc_tag size is not a multiple of required alignment tag + * indexing does not work. + */ + if (!IS_ALIGNED(sizeof(struct alloc_tag), align)) { + pr_err("%s: alignment %lu is incompatible with allocation tag indexing (= CONFIG_PGALLOC_TAG_REF_BITS)", + mod->name, align); + return ERR_PTR(-EINVAL); + } + + /* Ensure prepend consumes multiple of alloc_tag-sized blocks */ + if (prepend) + prepend =3D alloc_tag_align(prepend); +#endif /* PGALLOC_TAG_DIRECT_REF */ mas_lock(&mas); repeat: /* Try finding exact size and hope the start is aligned */ @@ -373,6 +418,10 @@ static int __init alloc_mod_tags_mem(void) return -ENOMEM; =20 module_tags.end_addr =3D module_tags.start_addr + module_tags_mem_sz; +#ifndef PGALLOC_TAG_DIRECT_REF + /* Ensure the base is alloc_tag aligned */ + module_tags.start_addr =3D alloc_tag_align(module_tags.start_addr); +#endif =20 return 0; } diff --git a/lib/codetag.c b/lib/codetag.c index 60463ef4bb85..cb3aa1631417 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -149,8 +149,8 @@ static struct codetag_range get_section_range(struct mo= dule *mod, const char *section) { return (struct codetag_range) { - get_symbol(mod, "__start_", section), - get_symbol(mod, "__stop_", section), + get_symbol(mod, CODETAG_SECTION_START_PREFIX, section), + get_symbol(mod, CODETAG_SECTION_STOP_PREFIX, section), }; } =20 diff --git a/mm/mm_init.c b/mm/mm_init.c index 4ba5607aaf19..231a95782455 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -2650,6 +2650,7 @@ void __init mm_core_init(void) report_meminit(); kmsan_init_shadow(); stack_depot_early_init(); + alloc_tag_sec_init(); mem_init(); kmem_cache_init(); /* --=20 2.46.0.469.g59c65b2a67-goog From nobody Fri Dec 19 08:56:28 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 730B916DC28 for ; Mon, 2 Sep 2024 04:41:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252107; cv=none; b=BK3N2HMqJT15yjxbZTJtlYuZBmgBvuGeuPt8/kemXSTo/jP3MBlzyhiXPwXmFDGADZkE6IFisQzStjATmsFuUfI/Uf0HgsMcLH3t1HtwVvB7ksvxTQrQrwwMTl7ZNCJljedWn6ewM+eLxgW48u6gpHH3TLk3QQ7MzSNTiCw71nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725252107; c=relaxed/simple; bh=Pf8Ai6mn9Z/XDost5C7B5IyWwUvzclGVwz6K3kHE9Qs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JyDPAEj3AVgeuS9spS/I9tcl/cVKGSO3B4UrgWwRXjUJwEUfCORYjjyUtPJeD3CCsm4P8xatPQK3NtGbmLHI1Ze1u86DMMUlQGSPiqWTxpVrJow4ENmBCdYHgeOGgA7xS1v6Hez4Uw6n1MmfiThh9pGq2LcuTlWxo4AVzJZws90= 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=iPimVhla; arc=none smtp.client-ip=209.85.219.201 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="iPimVhla" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e11368fa2e3so7174997276.3 for ; Sun, 01 Sep 2024 21:41:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725252104; x=1725856904; 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=o+zkIQ79nR91+bZ9fiMOs0/SZD3fit87wxI6zeuv5qM=; b=iPimVhlaxupwceEDULblaCOlPdM/dg9K4RcO+5VLUga7uad+ARJiguyRaHflWckpUj 3USvgDbBMjn9AS8vnoUM4dyt0OBtAWNsga5jbaqhG9pklmijhax/pVbitrTGR/6bUaUf lUbAYsggFQ5MS+Bi7rdxZ+YJJSW2YJS0aUkVWRiEQGAtB25Ma2mzZ8Z+xpdm4ynQT7xu rcV3d5Bf6tOKvaTiue7BliVuiq2289nMxX1YhltQYGkUmBaXLspnF0QRWCf3sVHU7uRi 8z2Ui/A/d4RdRFgAkOBBtBNkXN6ClNi2cfzebcs4aDIgxeuM/9SPnnoIAnm1wNznayDR q8ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725252104; x=1725856904; 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=o+zkIQ79nR91+bZ9fiMOs0/SZD3fit87wxI6zeuv5qM=; b=HaxaCbMrgrNUWA7e8jNU6FvPRGIj7ahkzqEc/LLcB5yb5ZCZuf2D570yeF4pLBiy6w IowfroYqBb1/rW9QxsAfluIgj3aIXjo7kUZcEp+Hfk5UtKyEMe7Iq0f565y+lzSZk8my WxJdKzfTQf1U4CIcuRGD1CRrGdtG7BVErxbUM0Mv/jnyQyS9s1ueOn73eP0yFjbwxq5Y ftMwjEx291O4HUrRwFFTuiEPEoawUVV2wo8LD182c6LytDywrJWxZEoeQLdcVVOnJ341 dqYVMcVvPhLupALrPbXV1KYR4B+uR6zkfmxSDHjUc1Go14q0PBVHfwCjMT6YtAl/xNju +J1A== X-Forwarded-Encrypted: i=1; AJvYcCXlylTYR6u9lDZBxojel588xiO3kSPnmSwLy2vB+aLPPAFea/ZtDF5+TOGSNttY63PLigrZqGE9tF3+9d4=@vger.kernel.org X-Gm-Message-State: AOJu0YzLDmLuG6jAOA/G+cxVqie33UsSvq9+xUmZBXMRil9xCDp8f6C9 rVNQ3DRuxYu9XrRxIbQvJgWckQO3FG98raai8Tz5wxA7hLPF+wp377W0Hb2oj/Lpgss5lfyLQRP TEw== X-Google-Smtp-Source: AGHT+IGESmFEaOyUz7H46Wj9ZUskazUKRvv1KzwKzYF26vqNkzVhO5DbeT/NbEzV1QldF78OxMgid1k7qes= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:7343:ecd2:aed:5b8f]) (user=surenb job=sendgmr) by 2002:a05:6902:1b0a:b0:e0b:a2a7:df77 with SMTP id 3f1490d57ef6-e1a79faf966mr117475276.2.1725252104383; Sun, 01 Sep 2024 21:41:44 -0700 (PDT) Date: Sun, 1 Sep 2024 21:41:28 -0700 In-Reply-To: <20240902044128.664075-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: <20240902044128.664075-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240902044128.664075-7-surenb@google.com> Subject: [PATCH v2 6/6] alloc_tag: config to store page allocation tag refs in page flags 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" Add CONFIG_PGALLOC_TAG_USE_PAGEFLAGS to store allocation tag references directly in the page flags. This removes dependency on page_ext and results in better performance for page allocations as well as reduced page_ext memory overhead. CONFIG_PGALLOC_TAG_REF_BITS controls the number of bits required to be available in the page flags to store the references. If the number of page flag bits is insufficient, the build will fail and either CONFIG_PGALLOC_TAG_REF_BITS would have to be lowered or CONFIG_PGALLOC_TAG_USE_PAGEFLAGS should be disabled. Signed-off-by: Suren Baghdasaryan --- include/linux/mmzone.h | 3 ++ include/linux/page-flags-layout.h | 10 +++++-- include/linux/pgalloc_tag.h | 48 +++++++++++++++++++++++++++++++ lib/Kconfig.debug | 27 +++++++++++++++-- lib/alloc_tag.c | 4 +++ mm/page_ext.c | 2 +- 6 files changed, 89 insertions(+), 5 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 17506e4a2835..0dd2b42f7cb6 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1085,6 +1085,7 @@ static inline bool zone_is_empty(struct zone *zone) #define KASAN_TAG_PGOFF (LAST_CPUPID_PGOFF - KASAN_TAG_WIDTH) #define LRU_GEN_PGOFF (KASAN_TAG_PGOFF - LRU_GEN_WIDTH) #define LRU_REFS_PGOFF (LRU_GEN_PGOFF - LRU_REFS_WIDTH) +#define ALLOC_TAG_REF_PGOFF (LRU_REFS_PGOFF - ALLOC_TAG_REF_WIDTH) =20 /* * Define the bit shifts to access each section. For non-existent @@ -1096,6 +1097,7 @@ static inline bool zone_is_empty(struct zone *zone) #define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH !=3D 0)) #define LAST_CPUPID_PGSHIFT (LAST_CPUPID_PGOFF * (LAST_CPUPID_WIDTH !=3D 0= )) #define KASAN_TAG_PGSHIFT (KASAN_TAG_PGOFF * (KASAN_TAG_WIDTH !=3D 0)) +#define ALLOC_TAG_REF_PGSHIFT (ALLOC_TAG_REF_PGOFF * (ALLOC_TAG_REF_WIDTH = !=3D 0)) =20 /* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator = */ #ifdef NODE_NOT_IN_PAGE_FLAGS @@ -1116,6 +1118,7 @@ static inline bool zone_is_empty(struct zone *zone) #define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1) #define KASAN_TAG_MASK ((1UL << KASAN_TAG_WIDTH) - 1) #define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1) +#define ALLOC_TAG_REF_MASK ((1UL << ALLOC_TAG_REF_WIDTH) - 1) =20 static inline enum zone_type page_zonenum(const struct page *page) { diff --git a/include/linux/page-flags-layout.h b/include/linux/page-flags-l= ayout.h index 7d79818dc065..21bba7c8c965 100644 --- a/include/linux/page-flags-layout.h +++ b/include/linux/page-flags-layout.h @@ -5,6 +5,12 @@ #include #include =20 +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS +#define ALLOC_TAG_REF_WIDTH CONFIG_PGALLOC_TAG_REF_BITS +#else +#define ALLOC_TAG_REF_WIDTH 0 +#endif + /* * When a memory allocation must conform to specific limitations (such * as being suitable for DMA) the caller will pass in hints to the @@ -91,7 +97,7 @@ #endif =20 #if ZONES_WIDTH + LRU_GEN_WIDTH + SECTIONS_WIDTH + NODES_WIDTH + \ - KASAN_TAG_WIDTH + LAST_CPUPID_SHIFT <=3D BITS_PER_LONG - NR_PAGEFLAGS + KASAN_TAG_WIDTH + ALLOC_TAG_REF_WIDTH + LAST_CPUPID_SHIFT <=3D BITS_PER_L= ONG - NR_PAGEFLAGS #define LAST_CPUPID_WIDTH LAST_CPUPID_SHIFT #else #define LAST_CPUPID_WIDTH 0 @@ -102,7 +108,7 @@ #endif =20 #if ZONES_WIDTH + LRU_GEN_WIDTH + SECTIONS_WIDTH + NODES_WIDTH + \ - KASAN_TAG_WIDTH + LAST_CPUPID_WIDTH > BITS_PER_LONG - NR_PAGEFLAGS + KASAN_TAG_WIDTH + ALLOC_TAG_REF_WIDTH + LAST_CPUPID_WIDTH > BITS_PER_LONG= - NR_PAGEFLAGS #error "Not enough bits in page flags" #endif =20 diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index a7f8f00c118f..dcb6706dee15 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -118,6 +118,52 @@ static inline void write_pgref(pgalloc_tag_ref *pgref,= union codetag_ref *ref) void __init alloc_tag_sec_init(void); =20 #endif /* PGALLOC_TAG_DIRECT_REF */ + +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + +typedef struct page *pgtag_ref_handle; + +/* Should be called only if mem_alloc_profiling_enabled() */ +static inline pgtag_ref_handle get_page_tag_ref(struct page *page, + union codetag_ref *ref) +{ + if (page) { + pgalloc_tag_ref pgref; + + pgref =3D (page->flags >> ALLOC_TAG_REF_PGSHIFT) & ALLOC_TAG_REF_MASK; + read_pgref(&pgref, ref); + return page; + } + + return NULL; +} + +static inline void put_page_tag_ref(pgtag_ref_handle page) +{ + WARN_ON(!page); +} + +static inline void update_page_tag_ref(pgtag_ref_handle page, union codeta= g_ref *ref) +{ + unsigned long old_flags, flags, val; + pgalloc_tag_ref pgref; + + if (WARN_ON(!page || !ref)) + return; + + write_pgref(&pgref, ref); + val =3D (unsigned long)pgref; + val =3D (val & ALLOC_TAG_REF_MASK) << ALLOC_TAG_REF_PGSHIFT; + do { + old_flags =3D READ_ONCE(page->flags); + flags =3D old_flags; + flags &=3D ~(ALLOC_TAG_REF_MASK << ALLOC_TAG_REF_PGSHIFT); + flags |=3D val; + } while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags))); +} + +#else /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + #include =20 extern struct page_ext_operations page_alloc_tagging_ops; @@ -166,6 +212,8 @@ static inline void update_page_tag_ref(pgtag_ref_handle= pgref, union codetag_ref write_pgref(pgref, ref); } =20 +#endif /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + static inline void clear_page_tag_ref(struct page *page) { if (mem_alloc_profiling_enabled()) { diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 253f9c2028da..9fc8c1981f27 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -979,7 +979,7 @@ config MEM_ALLOC_PROFILING depends on PROC_FS depends on !DEBUG_FORCE_WEAK_PER_CPU select CODE_TAGGING - select PAGE_EXTENSION + select PAGE_EXTENSION if !PGALLOC_TAG_USE_PAGEFLAGS select SLAB_OBJ_EXT help Track allocation source code and record total allocation size @@ -1000,10 +1000,26 @@ config MEM_ALLOC_PROFILING_DEBUG Adds warnings with helpful error messages for memory allocation profiling. =20 +config PGALLOC_TAG_USE_PAGEFLAGS + bool "Use pageflags to encode page allocation tag reference" + default n + depends on MEM_ALLOC_PROFILING + help + When set, page allocation tag references are encoded inside page + flags, otherwise they are encoded in page extensions. + + Setting this flag reduces memory and performance overhead of memory + allocation profiling but also limits how many allocations can be + tagged. The number of bits is set by PGALLOC_TAG_USE_PAGEFLAGS and + they must fit in the page flags field. + + Say N if unsure. + config PGALLOC_TAG_REF_BITS int "Number of bits for page allocation tag reference (10-64)" range 10 64 - default "64" + default "16" if PGALLOC_TAG_USE_PAGEFLAGS + default "64" if !PGALLOC_TAG_USE_PAGEFLAGS depends on MEM_ALLOC_PROFILING help Number of bits used to encode a page allocation tag reference. @@ -1011,6 +1027,13 @@ config PGALLOC_TAG_REF_BITS Smaller number results in less memory overhead but limits the number of allocations which can be tagged (including allocations from modules). =20 + If PGALLOC_TAG_USE_PAGEFLAGS is set, the number of requested bits should + fit inside the page flags. + + If PGALLOC_TAG_USE_PAGEFLAGS is not set, the number of bits used to sto= re + a reference is rounded up to the closest basic type. If set higher than= 32, + a direct pointer to the allocation tag is stored for performance reason= s. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan" diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 73791aa55ab6..34820650186d 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -476,6 +476,8 @@ static int __init setup_early_mem_profiling(char *str) } early_param("sysctl.vm.mem_profiling", setup_early_mem_profiling); =20 +#ifndef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + static __init bool need_page_alloc_tagging(void) { return mem_profiling_support; @@ -492,6 +494,8 @@ struct page_ext_operations page_alloc_tagging_ops =3D { }; EXPORT_SYMBOL(page_alloc_tagging_ops); =20 +#endif /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + #ifdef CONFIG_SYSCTL static struct ctl_table memory_allocation_profiling_sysctls[] =3D { { diff --git a/mm/page_ext.c b/mm/page_ext.c index 641d93f6af4c..5f993c271ee7 100644 --- a/mm/page_ext.c +++ b/mm/page_ext.c @@ -83,7 +83,7 @@ static struct page_ext_operations *page_ext_ops[] __initd= ata =3D { #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT) &page_idle_ops, #endif -#ifdef CONFIG_MEM_ALLOC_PROFILING +#if defined(CONFIG_MEM_ALLOC_PROFILING) && !defined(CONFIG_PGALLOC_TAG_USE= _PAGEFLAGS) &page_alloc_tagging_ops, #endif #ifdef CONFIG_PAGE_TABLE_CHECK --=20 2.46.0.469.g59c65b2a67-goog