From nobody Wed Nov 27 00:41:00 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 9BADB1D0E08 for ; Mon, 14 Oct 2024 20:36:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938215; cv=none; b=B/PJloEI5KxmRR3BYHDUz2CN0JE79Ex1RTwiLhv0XbEd67LYSBI9kgasyAneO3vR88s87UoVLy5iG1Ksb/K/4ABCBdVxYHK63YUqx9JhG1rnTx/sWp77kIr0U6zIdpWW7tx4XtBLgi/+b6P1Z9vAmKraUsOfaaQIMF3QDYx+bZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938215; c=relaxed/simple; bh=WyPp5GR9nupFuOlmPfHdOFY1s2UJmv4QO9RmmtWeFdk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nHPzMoLZnwvH3srMOF3sTdMvxRrZmEjX5baEFX+mtFVtYZKl0hOgn8xHJAzgYnltMU3x4jyB0KtJeURZWuL0VzoQ9J+kbLYsmlHNwmYlY/8zabzCpiFcjAjhXCPj8HDG5brNuV8bjp/P/PvGN8AkfX2cdS4+Tk5W1Wb6ql4jDJs= 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=BixPJo1U; arc=none smtp.client-ip=209.85.128.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="BixPJo1U" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e390b164c7so22996707b3.1 for ; Mon, 14 Oct 2024 13:36:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728938213; x=1729543013; 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=OBL7Vs4MVDC9S11JwgZ8NOTxmnHjmsqVGP9nKZ/sLKY=; b=BixPJo1U8Nkgk5wwILfszlCYdTGZZ3HQJeoN35OeGfLSvXmfhRNz0HzB8ezpupfJTp 3jt82WqGldh0ipsA6TdAv7XQZzBww21AOJuvcUaIDF1MZAgNm5WPbuWiKo8pMXA4diaJ rsjDnwrFuk83+I23OWNi4VBcRhpDQJyqtDPkV4XElJCzUNeS8a9dksoZRBUdCofOBSWp Ns1h80t2ZqjUrSo+XVHazawkx1EjcGu4imaGhMaOjwAFVYG+/oo3zf13gzUprUS4LMeA Ech0jhwMfocq/GExYP6d8NOlHaAeKax+dj4gtKzMCjtMjLCTsrkVzmoVlTfGTUfAPnM7 vrPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728938213; x=1729543013; 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=OBL7Vs4MVDC9S11JwgZ8NOTxmnHjmsqVGP9nKZ/sLKY=; b=YqF8iCOGfa6Gpu+0S/9nTkaHGRAvum+UUiL7hdE5cfT6ZeFAYVpjMO3F9xqTTqs0TP 10GTNyCQ/skRuY4QTW/yzlOT4/c/8jy2VHF+MMKZaxyp6Bt7axOqsk05WGYJ4HCdyph6 QRauzyhATgNGOr42yOkTQIL2R9kbsH2Iw2bfMX+uJTJalCJHV6TH4BH9OYvaMmoCVJPj GeFFEqumzE6f3Envn5sQ0BB4Kpwqa0BssSlgBVZF8A7Wl5I6TzgVFs5whgl24COkkqHL IeoqGC3zGLjPlOyxwUqopnwEn6tra8nwMw0UG5kenUSPvcnzdPSkYlt62Yp2K+FuXaYc LgEQ== X-Forwarded-Encrypted: i=1; AJvYcCVNI+KEN7tA4Dwu1KPaEE10X/NWrqnELjaLQLHjqrpGprpT50HT7eclAwyO31tutlaLHVms6eRuNCVljuk=@vger.kernel.org X-Gm-Message-State: AOJu0YxQbPTqV2FP9nYW8hxl70ACi85O3cljnMVlW1JLr3DhUByhjxII b2AFJ36nuyqvUZJzKchd3HmN/rt/jhr5RPQ5T0W5oR9boWgYnSl6RJyDfA/+i2fP3ZyWzONRvRZ MYg== X-Google-Smtp-Source: AGHT+IF2H6wJeGtAPLdmfc0iDyPr+v9IGwfu4WR2zrx8HQDicf0QvWblIiwnCcZ6i4e+0edwH2bII6UptCk= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:915:bdd7:e08a:7997]) (user=surenb job=sendgmr) by 2002:a05:690c:7446:b0:62c:f6fd:5401 with SMTP id 00721157ae682-6e347b4b2edmr460147b3.6.1728938212594; Mon, 14 Oct 2024 13:36:52 -0700 (PDT) Date: Mon, 14 Oct 2024 13:36:42 -0700 In-Reply-To: <20241014203646.1952505-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: <20241014203646.1952505-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241014203646.1952505-2-surenb@google.com> Subject: [PATCH v3 1/5] 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 c2c11004085e..e7e2caa1a95a 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.47.0.rc1.288.g06298d1525-goog From nobody Wed Nov 27 00:41:00 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 A8A831D150D for ; Mon, 14 Oct 2024 20:36:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938218; cv=none; b=kUvdM7iKnp6g/NyrJR6cvrB0GcOB4+55eORa0dOGH+kRmCpI/eM8YAvXf5bZepdmfocHrgcRPcdmhO9cJx+34bRz7L+3PbCvmuTd7w+pNl06g0dRmWAG8wi/yr8JEmsdhShulJC5NnmV3UGyjrzpCphm3/SFtC5wYAlILeE6ocs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938218; c=relaxed/simple; bh=AwPJ5okAiE8pkGp62AEvRwUXg9wg1tw0bv0/pvtXTLA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZIYMxPTT0DMyUwSnHU2tjBRH1ZTopKC7uYFR2HaVyoD5zrxyjiha8AYK1MvYuUP0y3P04gw8kS8HH0jQhlSickjw/FpSYYE4g7f8VgQjD6Yb9Cu/dkHr9NteetspAwONcymccWTlG2WVdcPdjU/5QIUtePcmaMuKtVbZhV7UJa8= 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=lBxVShkl; arc=none smtp.client-ip=209.85.128.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="lBxVShkl" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6886cd07673so85742457b3.3 for ; Mon, 14 Oct 2024 13:36:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728938215; x=1729543015; 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=TKWP9Sk5eHDUMKo1jGcXlNN0cIhyXRWL15bLbS6pMlc=; b=lBxVShklt1LrKbVFKICX5imZO/3fW9E49xZWvTyjZXNB4CvoTq3jSbzLI/jGaHkk7o 2U+MVwZcGPSeX5INpWNQ43xAzP5DR9V/MhzBYeZIdTKvUFqcxqjMN3OM9G+DKHHsgLrW 6R6tPT96Gam0WFCAaoHg6bkbNXXu1F79cTOwEb2JiJOUa9t/ao3m2c6anyGG7Q/gMo5J H7HYtvMC6As2DBVoSznaBndpQY+ck2pNjnDPVfW31qqjm8ZyJAZpFn9EFnZfP2ks1EA3 +awFg1+a4tacjR849DTS4cEz/BoTdQLWHr6YwEdENK/jzwlQJcnweioEaQdkWVx0KjEB 8UkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728938215; x=1729543015; 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=TKWP9Sk5eHDUMKo1jGcXlNN0cIhyXRWL15bLbS6pMlc=; b=gbm4R4CIg/uZO5wlGMIhYigM24pPkioI+CPcYHaYM1M7FHMXr6luVdj1RJEtzsCgKS X+KWNhRcpkLJW95HtmXopsaYIc04BE+BPfv4Jf1jRNc8CVsGzA3In3uU9fgkh6Adeobr BUIs50U1hipmfviXxN1s9UrWYdaJpS/Lpf9hDtzPEnDo4rrd9+hr4tsE5SbQyjsZUTjZ Geojw1qKY5vooql/8HMZ1eB9E5avJRQWkiOsIl9G2mBDsNRV9qGPRhrK7NHddAY+2rFK eXzGfG7illniw3iKgBI/+Ky+bpFLf06uCzV3NdNR+PKirwxG/6IL7N1r8n29jT/c6Qk7 12/w== X-Forwarded-Encrypted: i=1; AJvYcCUJzS35zzBoWr4kFjWUUDvCS1JdI90uMzZTeJRlzbauv/tqFI9qp8+1ELnwlILHk/ap/2WD42Cz5bOYG/0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5RtVsg3GMohhokHKiGXjUakaFaCwUV2QTWu7FZGYACmSVU3Wa +kWnRU5GHiAbWadozMSoKZJkeL9P3duiXNL8vfASKm3leiHGiAFxsouSy7aHlGPODVsMhKZquVQ 3CQ== X-Google-Smtp-Source: AGHT+IGBNIqOR+aetsuJD2kNoXsUQNEGFlkkyZON6PVRCQ5jP5534CpNDF7V47lVUomGWIrldnmJpVdzxLU= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:915:bdd7:e08a:7997]) (user=surenb job=sendgmr) by 2002:a25:d094:0:b0:e28:f6f6:81a5 with SMTP id 3f1490d57ef6-e2918e5da48mr9605276.0.1728938214624; Mon, 14 Oct 2024 13:36:54 -0700 (PDT) Date: Mon, 14 Oct 2024 13:36:43 -0700 In-Reply-To: <20241014203646.1952505-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: <20241014203646.1952505-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241014203646.1952505-3-surenb@google.com> Subject: [PATCH v3 2/5] alloc_tag: load module tags into separate contiguous 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. Signed-off-by: Suren Baghdasaryan --- include/asm-generic/codetag.lds.h | 19 +++ include/linux/alloc_tag.h | 13 +- include/linux/codetag.h | 37 ++++- kernel/module/main.c | 74 +++++---- lib/alloc_tag.c | 251 +++++++++++++++++++++++++++--- lib/codetag.c | 100 +++++++++++- scripts/module.lds.S | 5 +- 7 files changed, 438 insertions(+), 61 deletions(-) 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 1f0a9ff23a2c..7431757999c5 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 ef54733bd7d2..5569dd052511 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1254,22 +1254,17 @@ 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) { struct module_memory *mem =3D &mod->mem[type]; - void *ptr =3D mem->base; =20 if (mem->is_rox) vfree(mem->rw_copy); =20 - if (!unload_codetags && mod_mem_type_is_core_data(type)) - return; - - execmem_free(ptr); + execmem_free(mem->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]; @@ -1280,25 +1275,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 @@ -1341,7 +1331,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) @@ -1606,6 +1596,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); } @@ -2280,6 +2278,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) { @@ -2291,7 +2290,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 @@ -2300,15 +2299,33 @@ static int move_module(struct module *mod, struct l= oad_info *info) for (i =3D 0; i < info->hdr->e_shnum; i++) { void *dest; Elf_Shdr *shdr =3D &info->sechdrs[i]; - enum mod_mem_type type =3D shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; - unsigned long offset =3D shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK; + const char *sname; unsigned long addr; =20 if (!(shdr->sh_flags & SHF_ALLOC)) continue; =20 - addr =3D (unsigned long)mod->mem[type].base + offset; - dest =3D mod->mem[type].rw_copy + offset; + 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; + } + addr =3D (unsigned long)dest; + codetag_section_found =3D true; + } else { + enum mod_mem_type type =3D shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; + unsigned long offset =3D shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK; + + addr =3D (unsigned long)mod->mem[type].base + offset; + dest =3D mod->mem[type].rw_copy + offset; + } =20 if (shdr->sh_type !=3D SHT_NOBITS) { /* @@ -2320,7 +2337,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); } @@ -2336,9 +2353,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 @@ -2459,6 +2479,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 @@ -2468,7 +2490,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..b10e7f17eeda 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,31 +150,231 @@ 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) +{ + 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) { - struct codetag_iterator iter =3D codetag_get_ct_iter(cttype); - struct alloc_tag_counters counter; - bool module_unused =3D true; + 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++; + } =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_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; + + 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); +} =20 - return module_unused; +#define MODULE_ALLOC_TAG_VMAP_SIZE (100000 * sizeof(struct alloc_tag)) + +static int __init alloc_mod_tags_mem(void) +{ + /* Allocate space to copy allocation tags */ + module_tags.start_addr =3D (unsigned long)execmem_alloc(EXECMEM_MODULE_DA= TA, + MODULE_ALLOC_TAG_VMAP_SIZE); + if (!module_tags.start_addr) + return -ENOMEM; + + module_tags.end_addr =3D module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_S= IZE; + + return 0; +} + +static void __init free_mod_tags_mem(void) +{ + execmem_free((void *)module_tags.start_addr); + module_tags.start_addr =3D 0; } =20 +#else /* CONFIG_MODULES */ + +static inline int alloc_mod_tags_mem(void) { return 0; } +static inline void free_mod_tags_mem(void) {} + +#endif /* CONFIG_MODULES */ + #ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT static bool mem_profiling_support __meminitdata =3D true; #else @@ -255,14 +456,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 d1fbbb7c2ec3..3f3a4f4ca72d 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 /* await any module's kfree_rcu() operations to complete */ kvfree_rcu_barrier(); @@ -246,18 +333,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.47.0.rc1.288.g06298d1525-goog From nobody Wed Nov 27 00:41:00 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 C7BCA1D1E68 for ; Mon, 14 Oct 2024 20:36:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938219; cv=none; b=Vi/wcRFgNFxBWHtyn2UuyXe50tr7Id1q87UEW2wFayJlYWadtWlg2GhJlA+pw2Wjc/EnKIl3a3wwWk2XP28xLTG3fLwWnLQCnbcCwKkuilv7x2LD0YzKN0oNjitblp9kuhr+6ybtcGLe5Y4TmkQ9fvuv8g7w2Aqb8cdHvC46tyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938219; c=relaxed/simple; bh=mT/WeQdX90ZMvRZEhMfvV+ENRkOm1JHryBCb4xMzGtY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=D5XCLGMbb46nmO2M9fP2NMhb0VjsVJ8ifnCHi5MEUg7980tfBqhTFJu8pUfh1TmEoP0wgxMf4WmIwN1IpgUcML04EmcYb0iOWJK+aZeGsZCxT0LiM8VzEmtD3SpHj74fWIIiV8lrnDs7VsNsySrbcKaSiHvlL7lo1HeP2lwBkWU= 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=mCk+1f8z; arc=none smtp.client-ip=209.85.128.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="mCk+1f8z" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e347b1e29dso41445567b3.0 for ; Mon, 14 Oct 2024 13:36:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728938217; x=1729543017; 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=bOvroPog9pmluzu4zs2xrkfEGIpV84j+wuh+gRgw5PA=; b=mCk+1f8zQO9+k6aUmyMjqS2QB3wHdPTfqwgpNwRFPOptphQ0YXYcRFFB49BsL8M2XQ jUFhf3Xh0QIdoUlPUJc4RBVxFbJCYcT1be/E4j4DzD2flsxUCmcyu0NaKBs6Xrj9yLTm /wipIFCSeeSKShzz7szeaX+GQk0j77nrI+VXDe6QGyUJtYvUGBCu++ic/vGAypphkM6B 5u0pRWcHeNFpu6PvZNGSa0r5T8b0NiJgbYu/ADNVSqUqcePpxx0vxo5e3OxAyV3M2gBf 1UFVVYZ2qe+3QCp2N9mFy15a8t5ROBhV2RYHjfNS4QHE0F5sTk5gGIR7RLzS4ua6XhLT IKjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728938217; x=1729543017; 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=bOvroPog9pmluzu4zs2xrkfEGIpV84j+wuh+gRgw5PA=; b=Iwgxje06Er2ucnQCM02O9bW9SzbP95dvJlvlV52TOxxiF//qI4lY61gw1//ZMd1Od+ 1xTSo7AMxfK/+pIdhdx/FKWotXwme5wEv5CZRa8YjoEh88M0DdAkOR3vSn9FAgW/0a1l 0Y2DotSTvQnAwXCWzxCkWVkOHGu/Uii2oLgfxCXzqfPDOycBHWusfEKd14+plR28097c vM+VrSNEy0QVc/hPxuMf+52+FoR+s/MM6X9F8EiNXBP3m1e/njC4YXmvNVnFzNrJ1Ba6 GB3IGPbjF78+oel9dOE/AunxeYGd/zBBHbKpBpgD06h2gllODYvOYbN7PBwBPnvSoqCm RZng== X-Forwarded-Encrypted: i=1; AJvYcCW9nMfZ7Y6Wny0b+0zipnzDAKM4FvxgnVEkCNBiCntZL3Fc9FGS53+DORDA8+StK6B7myw5zj8RsaQhEX0=@vger.kernel.org X-Gm-Message-State: AOJu0YxA99cDQhPJyTUmuoNAP2Brn4Oln107OeLh2iq1nEJUYV4bKOF1 ReKTwqqmZ9SH3l/REzeGPY36QXV7IpaLGlrjgJzogeJ0i4lzeilKLBBZ4koJSCwoFclKrx5dXZZ YiA== X-Google-Smtp-Source: AGHT+IHN+F4HHrAed80eKaPYhTH0JCc5Mv3vEPFTteI9kO52+1hB5IgMv4halrrXePZu5DiEH+LMAHeUbpw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:915:bdd7:e08a:7997]) (user=surenb job=sendgmr) by 2002:a05:690c:c09:b0:6e3:1702:b3e6 with SMTP id 00721157ae682-6e347b368d2mr1991517b3.4.1728938216731; Mon, 14 Oct 2024 13:36:56 -0700 (PDT) Date: Mon, 14 Oct 2024 13:36:44 -0700 In-Reply-To: <20241014203646.1952505-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: <20241014203646.1952505-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241014203646.1952505-4-surenb@google.com> Subject: [PATCH v3 3/5] alloc_tag: populate memory for module tags as needed 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" The memory reserved for module tags does not need to be backed by physical pages until there are tags to store there. Change the way we reserve this memory to allocate only virtual area for the tags and populate it with physical pages as needed when we load a module. Signed-off-by: Suren Baghdasaryan --- include/linux/execmem.h | 11 ++++++ include/linux/vmalloc.h | 9 +++++ lib/alloc_tag.c | 84 +++++++++++++++++++++++++++++++++-------- mm/execmem.c | 16 ++++++++ mm/vmalloc.c | 4 +- 5 files changed, 106 insertions(+), 18 deletions(-) diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 7436aa547818..a159a073270a 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -127,6 +127,17 @@ void *execmem_alloc(enum execmem_type type, size_t siz= e); */ void execmem_free(void *ptr); =20 +/** + * execmem_vmap - create virtual mapping for executable memory + * @type: type of the allocation + * @size: size of the virtual mapping in bytes + * + * Maps virtually contiguous area that can be populated with executable co= de. + * + * Return: the area descriptor on success or %NULL on failure. + */ +struct vm_struct *execmem_vmap(enum execmem_type type, size_t size); + /** * execmem_update_copy - copy an update to executable memory * @dst: destination address to update diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 9a012cd4fad2..9d64cc6f24d1 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -202,6 +202,9 @@ extern int remap_vmalloc_range_partial(struct vm_area_s= truct *vma, extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, unsigned long pgoff); =20 +int vmap_pages_range(unsigned long addr, unsigned long end, + pgprot_t prot, struct page **pages, unsigned int page_shift); + /* * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED = values * and let generic vmalloc and ioremap code know when arch_sync_kernel_map= pings() @@ -239,6 +242,12 @@ extern struct vm_struct *__get_vm_area_caller(unsigned= long size, unsigned long flags, unsigned long start, unsigned long end, const void *caller); +struct vm_struct *__get_vm_area_node(unsigned long size, + unsigned long align, unsigned long shift, + unsigned long flags, unsigned long start, + unsigned long end, int node, gfp_t gfp_mask, + const void *caller); + void free_vm_area(struct vm_struct *area); extern struct vm_struct *remove_vm_area(const void *addr); extern struct vm_struct *find_vm_area(const void *addr); diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index b10e7f17eeda..648f32d52b8d 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -8,6 +8,7 @@ #include #include #include +#include =20 static struct codetag_type *alloc_tag_cttype; =20 @@ -153,6 +154,7 @@ static void __init procfs_init(void) #ifdef CONFIG_MODULES =20 static struct maple_tree mod_area_mt =3D MTREE_INIT(mod_area_mt, MT_FLAGS_= ALLOC_RANGE); +static struct vm_struct *vm_module_tags; /* A dummy object used to indicate an unloaded module */ static struct module unloaded_mod; /* A dummy object used to indicate a module prepended area */ @@ -195,6 +197,25 @@ static void clean_unused_module_areas_locked(void) } } =20 +static int vm_module_tags_grow(unsigned long addr, unsigned long bytes) +{ + struct page **next_page =3D vm_module_tags->pages + vm_module_tags->nr_pa= ges; + unsigned long more_pages =3D ALIGN(bytes, PAGE_SIZE) >> PAGE_SHIFT; + unsigned long nr; + + nr =3D alloc_pages_bulk_array_node(GFP_KERNEL | __GFP_NOWARN, + NUMA_NO_NODE, more_pages, next_page); + if (nr !=3D more_pages) + return -ENOMEM; + + vm_module_tags->nr_pages +=3D nr; + if (vmap_pages_range(addr, addr + (nr << PAGE_SHIFT), + PAGE_KERNEL, next_page, PAGE_SHIFT) < 0) + return -ENOMEM; + + return 0; +} + static void *reserve_module_tags(struct module *mod, unsigned long size, unsigned int prepend, unsigned long align) { @@ -202,7 +223,7 @@ static void *reserve_module_tags(struct module *mod, un= signed long size, MA_STATE(mas, &mod_area_mt, 0, section_size - 1); bool cleanup_done =3D false; unsigned long offset; - void *ret; + void *ret =3D NULL; =20 /* If no tags return NULL */ if (size < sizeof(struct alloc_tag)) @@ -239,7 +260,7 @@ static void *reserve_module_tags(struct module *mod, un= signed long size, goto repeat; } else { ret =3D ERR_PTR(-ENOMEM); - goto out; + goto unlock; } =20 found: @@ -254,7 +275,7 @@ static void *reserve_module_tags(struct module *mod, un= signed long size, mas_store(&mas, &prepend_mod); if (mas_is_err(&mas)) { ret =3D ERR_PTR(xa_err(mas.node)); - goto out; + goto unlock; } mas.index =3D offset; mas.last =3D offset + size - 1; @@ -263,7 +284,7 @@ static void *reserve_module_tags(struct module *mod, un= signed long size, ret =3D ERR_PTR(xa_err(mas.node)); mas.index =3D pad_start; mas_erase(&mas); - goto out; + goto unlock; } =20 } else { @@ -271,18 +292,33 @@ static void *reserve_module_tags(struct module *mod, = unsigned long size, mas_store(&mas, mod); if (mas_is_err(&mas)) { ret =3D ERR_PTR(xa_err(mas.node)); - goto out; + goto unlock; } } +unlock: + mas_unlock(&mas); + if (IS_ERR(ret)) + return ret; =20 - if (module_tags.size < offset + size) - module_tags.size =3D offset + size; + if (module_tags.size < offset + size) { + unsigned long phys_size =3D vm_module_tags->nr_pages << PAGE_SHIFT; =20 - ret =3D (struct alloc_tag *)(module_tags.start_addr + offset); -out: - mas_unlock(&mas); + module_tags.size =3D offset + size; + if (phys_size < module_tags.size) { + int grow_res; + + grow_res =3D vm_module_tags_grow(module_tags.start_addr + phys_size, + module_tags.size - phys_size); + if (grow_res) { + static_branch_disable(&mem_alloc_profiling_key); + pr_warn("Failed to allocate tags memory for module %s. Memory profilin= g is disabled!\n", + mod->name); + return ERR_PTR(grow_res); + } + } + } =20 - return ret; + return (struct alloc_tag *)(module_tags.start_addr + offset); } =20 static void release_module_tags(struct module *mod, bool unused) @@ -351,12 +387,23 @@ static void replace_module(struct module *mod, struct= module *new_mod) =20 static int __init alloc_mod_tags_mem(void) { - /* Allocate space to copy allocation tags */ - module_tags.start_addr =3D (unsigned long)execmem_alloc(EXECMEM_MODULE_DA= TA, - MODULE_ALLOC_TAG_VMAP_SIZE); - if (!module_tags.start_addr) + /* Map space to copy allocation tags */ + vm_module_tags =3D execmem_vmap(EXECMEM_MODULE_DATA, MODULE_ALLOC_TAG_VMA= P_SIZE); + if (!vm_module_tags) { + pr_err("Failed to map %lu bytes for module allocation tags\n", + MODULE_ALLOC_TAG_VMAP_SIZE); + module_tags.start_addr =3D 0; return -ENOMEM; + } =20 + vm_module_tags->pages =3D kmalloc_array(get_vm_area_size(vm_module_tags) = >> PAGE_SHIFT, + sizeof(struct page *), GFP_KERNEL | __GFP_ZERO); + if (!vm_module_tags->pages) { + free_vm_area(vm_module_tags); + return -ENOMEM; + } + + module_tags.start_addr =3D (unsigned long)vm_module_tags->addr; module_tags.end_addr =3D module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_S= IZE; =20 return 0; @@ -364,8 +411,13 @@ static int __init alloc_mod_tags_mem(void) =20 static void __init free_mod_tags_mem(void) { - execmem_free((void *)module_tags.start_addr); + int i; + module_tags.start_addr =3D 0; + for (i =3D 0; i < vm_module_tags->nr_pages; i++) + __free_page(vm_module_tags->pages[i]); + kfree(vm_module_tags->pages); + free_vm_area(vm_module_tags); } =20 #else /* CONFIG_MODULES */ diff --git a/mm/execmem.c b/mm/execmem.c index 97706d8ed720..eb346f4eaaff 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -366,6 +366,22 @@ void execmem_free(void *ptr) vfree(ptr); } =20 +struct vm_struct *execmem_vmap(enum execmem_type type, size_t size) +{ + struct execmem_range *range =3D &execmem_info->ranges[type]; + struct vm_struct *area; + + area =3D __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC, + range->start, range->end, NUMA_NO_NODE, + GFP_KERNEL, __builtin_return_address(0)); + if (!area && range->fallback_start) + area =3D __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC, + range->fallback_start, range->fallback_end, + NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0)); + + return area; +} + void *execmem_update_copy(void *dst, const void *src, size_t size) { return text_poke_copy(dst, src, size); diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 74c0a5eae210..7ed39d104201 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -653,7 +653,7 @@ int vmap_pages_range_noflush(unsigned long addr, unsign= ed long end, * RETURNS: * 0 on success, -errno on failure. */ -static int vmap_pages_range(unsigned long addr, unsigned long end, +int vmap_pages_range(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { int err; @@ -3106,7 +3106,7 @@ static void clear_vm_uninitialized_flag(struct vm_str= uct *vm) vm->flags &=3D ~VM_UNINITIALIZED; } =20 -static struct vm_struct *__get_vm_area_node(unsigned long size, +struct vm_struct *__get_vm_area_node(unsigned long size, unsigned long align, unsigned long shift, unsigned long flags, unsigned long start, unsigned long end, int node, gfp_t gfp_mask, const void *caller) --=20 2.47.0.rc1.288.g06298d1525-goog From nobody Wed Nov 27 00:41:00 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 83F931D1500 for ; Mon, 14 Oct 2024 20:36:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938221; cv=none; b=ogFPFrXDBNfWoHCy+AL5q/cb5nTQ5iX8wzHcIaSwjC/m4P4ylLkrKkRy5G+qILz8mfKmj7XVoOSDJqk8t78G4fu6E8j8xGYGnlWk9RqMe4CM9gnyRNYZq5ku+tTL5zMoMSUUaFKUwdk/FfqykOrZVa14UZDXDvrPdrrrcyJcijY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938221; c=relaxed/simple; bh=yjxn1wVWKUXE4jrDbtBUSLFw9D3HkS0XAuR2v9Mx6rI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HTWbP7h7AhoqhEuggWq2fq1HiScYfbGnmrNqnp0wjlN8v2WhTXxZ25aOnGVWWnRrIkI8LXDCiyU9QGnKFJ0sSBhQv3nDKrKZSKw/Lzo7JBKbl7jr2LNmpxtTxhRdkqbvdIEYy358KE/sDCR3MpZ0FmW8u4XDe0Cz8TslrolEgII= 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=fdT7Xb0Q; arc=none smtp.client-ip=209.85.128.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="fdT7Xb0Q" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e000d68bb1so62228947b3.1 for ; Mon, 14 Oct 2024 13:36:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728938219; x=1729543019; darn=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=KDELffEPLAPDpRQ8WZJTecgr0QxXWhGmdEuRSVQA138=; b=fdT7Xb0QC+icPH7YzRV3mR9O2DJZlTPEIRsXL4u8tkac4ufhVXr0oyNIOFT3lqq//C nXF1HKymKZZV5Ev3jBwh/brkCnwEEARnn0w1KHgelH+mscIlPhHiN3UoofCttP+9nLOK GEqTHwxSsh9dRhIBEU7QjLaVgZxq6ReNN7fL9rK18dwoM9XEROhyklbG6RuAUgRyHxxV ZYWIgzFu3/FPkfUEiIekd4CJFw5z/OaU7zI17bswAnUn4lFmw/IS0lv3e+YYz8tgU5R3 zsI5EwVZR/nAYCia+ctfIaiU8GXdGoR9tGbJyCrpQulgTDyMeL/M8M1kKg9+IeSikHGz 87+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728938219; x=1729543019; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KDELffEPLAPDpRQ8WZJTecgr0QxXWhGmdEuRSVQA138=; b=Rem4HVcpdYZ/tO3tIv2hEqkylX+nrJf+fw5ID354oA/gVu0iELo++FeQMxe+tHwThv oenxjUSrfXQ4XfpAxMky7MyO0PFRNJJV53Xp+NQoQ21NQpiYAA2PPPelbeCF3d4jDCpq h+Ec3bZkqxcPpZ+WfCAPTrLi8t9nR+JRQoBch81cox9ZqCkVgqZ462vJgghG42n1Io1O ga6J4CepKgb5S0qV/YUMaciLgQimERTRty4BzXLJu4bQXGpI5xRfiH1tZcULd3YgFg+L hRRY1rlb06bEUJoixYvjJdP2XyVYanZv4qvepW+Mmv7DI2IBmDagZQdW8i5aUdSx20Hx /mgw== X-Forwarded-Encrypted: i=1; AJvYcCUQVLbllqlXNiRuPC3FvKxt0HkJWotB0E31Lb2lvTMvkjDcLpkM+OyDCPVltFnTw6z8QQUxv2hhv4kO1d4=@vger.kernel.org X-Gm-Message-State: AOJu0Yz/52FowAuYklJGum1WHuxbMR2Fn/3iMlSl0hKbFXwHbhQND4Kx ZreCMJy8jiU4BmJiqfmz7XoVT4qgOojRiOmJwAXTTfb6uq5t4Wv+Sps8+DJl/fPu/8yMr8v/aio CWQ== X-Google-Smtp-Source: AGHT+IFebmDDzFfa9NO21TdKX40eCmjiTPo05JzyoY7+ZX8h6EIqKu1BLOx2oszwApr2jiSzy+GImo1RYfc= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:915:bdd7:e08a:7997]) (user=surenb job=sendgmr) by 2002:a5b:40a:0:b0:e29:9c5:5fcb with SMTP id 3f1490d57ef6-e290bb22b20mr207938276.4.1728938218676; Mon, 14 Oct 2024 13:36:58 -0700 (PDT) Date: Mon, 14 Oct 2024 13:36:45 -0700 In-Reply-To: <20241014203646.1952505-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241014203646.1952505-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241014203646.1952505-5-surenb@google.com> Subject: [PATCH v3 4/5] alloc_tag: introduce pgalloc_tag_ref to abstract page tag references From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, corbet@lwn.net, arnd@arndb.de, mcgrof@kernel.org, rppt@kernel.org, paulmck@kernel.org, thuth@redhat.com, tglx@linutronix.de, bp@alien8.de, xiongwei.song@windriver.com, ardb@kernel.org, david@redhat.com, vbabka@suse.cz, mhocko@suse.com, hannes@cmpxchg.org, roman.gushchin@linux.dev, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, pasha.tatashin@soleen.com, souravpanda@google.com, keescook@chromium.org, dennis@kernel.org, jhubbard@nvidia.com, yuzhao@google.com, vvvvvv@google.com, rostedt@goodmis.org, iamjoonsoo.kim@lge.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kernel-team@android.com, surenb@google.com 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/mm.h | 25 +++++---- include/linux/pgalloc_tag.h | 108 ++++++++++++++++++++++++------------ lib/alloc_tag.c | 3 +- 3 files changed, 88 insertions(+), 48 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 5cd22303fbc0..8efb4a6a1a70 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4180,37 +4180,38 @@ static inline void pgalloc_tag_split(struct folio *= folio, int old_order, int new return; =20 for (i =3D nr_pages; i < (1 << old_order); i +=3D nr_pages) { - union codetag_ref *ref =3D get_page_tag_ref(folio_page(folio, i)); + union pgtag_ref_handle handle; + union codetag_ref ref; =20 - if (ref) { + if (get_page_tag_ref(folio_page(folio, i), &ref, &handle)) { /* Set new reference to point to the original tag */ - alloc_tag_ref_set(ref, tag); - put_page_tag_ref(ref); + alloc_tag_ref_set(&ref, tag); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } =20 static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) { + union pgtag_ref_handle handle; + union codetag_ref ref; struct alloc_tag *tag; - union codetag_ref *ref; =20 tag =3D pgalloc_tag_get(&old->page); if (!tag) return; =20 - ref =3D get_page_tag_ref(&new->page); - if (!ref) + if (!get_page_tag_ref(&new->page, &ref, &handle)) return; =20 /* Clear the old ref to the original allocation tag. */ clear_page_tag_ref(&old->page); /* Decrement the counters of the tag on get_new_folio. */ - alloc_tag_sub(ref, folio_nr_pages(new)); - - __alloc_tag_ref_set(ref, tag); - - put_page_tag_ref(ref); + alloc_tag_sub(&ref, folio_nr_pages(new)); + __alloc_tag_ref_set(&ref, tag); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } #else /* !CONFIG_MEM_ALLOC_PROFILING */ static inline void pgalloc_tag_split(struct folio *folio, int old_order, i= nt new_order) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 59a3deb792a8..bc65710ee1f9 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -9,48 +9,83 @@ =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; +} + +union pgtag_ref_handle { + pgalloc_tag_ref *pgref; /* reference in page extension */ +}; + #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 /* Should be called only if mem_alloc_profiling_enabled() */ -static inline union codetag_ref *get_page_tag_ref(struct page *page) +static inline bool get_page_tag_ref(struct page *page, union codetag_ref *= ref, + union pgtag_ref_handle *handle) { - if (page) { - struct page_ext *page_ext =3D page_ext_get(page); + struct page_ext *page_ext; + pgalloc_tag_ref *pgref; =20 - if (page_ext) - return codetag_ref_from_page_ext(page_ext); - } - return NULL; + if (!page) + return false; + + page_ext =3D page_ext_get(page); + if (!page_ext) + return false; + + pgref =3D pgref_from_page_ext(page_ext); + read_pgref(pgref, ref); + handle->pgref =3D pgref; + return true; +} + +static inline void put_page_tag_ref(union pgtag_ref_handle handle) +{ + if (WARN_ON(!handle.pgref)) + return; + + page_ext_put(page_ext_from_pgref(handle.pgref)); } =20 -static inline void put_page_tag_ref(union codetag_ref *ref) +static inline void update_page_tag_ref(union pgtag_ref_handle handle, + union codetag_ref *ref) { - if (WARN_ON(!ref)) + if (WARN_ON(!handle.pgref || !ref)) return; =20 - page_ext_put(page_ext_from_codetag_ref(ref)); + write_pgref(handle.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); + union pgtag_ref_handle handle; + union codetag_ref ref; =20 - if (ref) { - set_codetag_empty(ref); - put_page_tag_ref(ref); + if (get_page_tag_ref(page, &ref, &handle)) { + set_codetag_empty(&ref); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -59,11 +94,13 @@ static inline void pgalloc_tag_add(struct page *page, s= truct task_struct *task, unsigned int nr) { if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref =3D get_page_tag_ref(page); + union pgtag_ref_handle handle; + union codetag_ref ref; =20 - if (ref) { - alloc_tag_add(ref, task->alloc_tag, PAGE_SIZE * nr); - put_page_tag_ref(ref); + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -71,11 +108,13 @@ static inline void pgalloc_tag_add(struct page *page, = struct task_struct *task, static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) { if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref =3D get_page_tag_ref(page); + union pgtag_ref_handle handle; + union codetag_ref ref; =20 - if (ref) { - alloc_tag_sub(ref, PAGE_SIZE * nr); - put_page_tag_ref(ref); + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_sub(&ref, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -85,13 +124,14 @@ static inline struct alloc_tag *pgalloc_tag_get(struct= page *page) struct alloc_tag *tag =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); + union pgtag_ref_handle handle; + union codetag_ref ref; + + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_sub_check(&ref); + if (ref.ct) + tag =3D ct_to_alloc_tag(ref.ct); + put_page_tag_ref(handle); } } =20 @@ -106,8 +146,6 @@ static inline void pgalloc_tag_sub_pages(struct alloc_t= ag *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) {} static inline void clear_page_tag_ref(struct page *page) {} static inline void pgalloc_tag_add(struct page *page, struct task_struct *= task, unsigned int nr) {} diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 648f32d52b8d..2ef762acc203 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -474,7 +475,7 @@ static __init void init_page_alloc_tagging(void) } =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.47.0.rc1.288.g06298d1525-goog From nobody Wed Nov 27 00:41:00 2024 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 C22C51D0E05 for ; Mon, 14 Oct 2024 20:37:01 +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=1728938225; cv=none; b=APbui6SGrb52ukDLkvrOtAXsO8gGUE8ZEll6o0zezGmbfrDfv0KGdIcvAP/s8T26S431GlnOQQYNqCygqO3OO++aaJX83gXDoyOa+mM7zDxUi+rQiD4QE5X9jNNWwPlJ4DRePdiJGQKCVJxSKzHrBBQCXturoBO4baRTm1UVADg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728938225; c=relaxed/simple; bh=PpJpwll108+Kq61j5T43UFyRUVz4XXDNu8oqzIkCAiU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DuNWyG+JgVj59LvqEHQcECmWhDjmlQcYHqDy575cWq1rF22wZF2e4879KRyS6vPFvQWRnYUuafv6fPkDX8pvXbzQvcfNMN/wdzLzw5DxibPIVkOgifRrFxNl2tZp3bjIXDt3b3B2GMOq42a6m4d4FqhCKdvsAxpl64GlHkDOeck= 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=DoCvWsQU; 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="DoCvWsQU" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e35a643200so48790007b3.0 for ; Mon, 14 Oct 2024 13:37:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728938221; x=1729543021; 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=3awKuOd0YJMzv+83YLmL6cC1+LaUmoopuJw+yqAMR2w=; b=DoCvWsQUlsJORvxmYsG8nOvRa/wWKtIhQUsYNea985BSaC9URvWtRAK2ElfUPgbrsv /TPQOCQbxtVzKqKyamFSE65EJ41TE7rCfgElO9eTyN7nt8FDe0k+h4xMqiRV63w9zPZ6 kH6ypm3go9apODgImEEUfnVJPrJTjZOOVhJYX4nV/0QoaxmNh3y4CbwbUfRg/D8l5Bog 1fumj1k4M/WqkWMbr/2ho/yjxuCqbe0JilTS13xPQM/N2OhE2SqgT4oPNtRGK+HEkNUJ ulGObx91aaEZa7G6W7oY4uKsvgv++gjyF8+sT0ts4xwE7cdUTuvcV3RlCsoOGRDudXb6 XQTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728938221; x=1729543021; 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=3awKuOd0YJMzv+83YLmL6cC1+LaUmoopuJw+yqAMR2w=; b=McC6yS49wNpCGDiGQ2EjU30qbZETmbWWexZrHTgU62MAh7t2jTzj9mPxWU5TWI78Ta F7IxxL7zoynaKKFQECO95ZDFpA/i8SGJabba/cdYYm0uSi1a9bUWUtr09El9eccVKqCm Xtsv3HkiMKA9PpsK01IWWurbOBviJsjNsdtInfARgaSee5xsybPuafCnRjRCvtxBpCaX NMmNZkkYpJhwrskQkk+liSVEEDlFVqIsSrcE4q71Z5HE2KCly/tTN926qVg21FdFoOPp +BAB/0FklkDoFs3L64HjVszFNDhbaJO/Yq5edD/XRnOCShQBrtMmyWvoADkUY0+Vx5Zh GNqQ== X-Forwarded-Encrypted: i=1; AJvYcCVuG+XL8TCqHWpl5QkNtEnPqemPak81VRxS/MDTc+Ndng7Dt9fgWsKg/wucDcXH+7XhsE6m1XlMr8NvJaE=@vger.kernel.org X-Gm-Message-State: AOJu0Yx30fgb8zVpWcY1m7f5YIGstLDuSOB0hxDpOLFryU7QCCYc36aL xGP8NDiM4PcLM/qgzwxHAkFqltO0l8RqNI7fqMouqgwyg0U3ssJmUm6dMqRJ5JQgz8EsgfouOL7 rnA== X-Google-Smtp-Source: AGHT+IFg9tfmO7bztO2On8XBGLSENOaX0zqwxf4uBCk3a2PGQpcqPZvt7HPa1I/o1H4N1GNxRLUB/lu7jhs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:915:bdd7:e08a:7997]) (user=surenb job=sendgmr) by 2002:a05:690c:7446:b0:62c:f6fd:5401 with SMTP id 00721157ae682-6e347b4b2edmr460177b3.6.1728938220715; Mon, 14 Oct 2024 13:37:00 -0700 (PDT) Date: Mon, 14 Oct 2024 13:36:46 -0700 In-Reply-To: <20241014203646.1952505-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: <20241014203646.1952505-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241014203646.1952505-6-surenb@google.com> Subject: [PATCH v3 5/5] 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 eliminates memory overhead caused by page_ext and results in better performance for page allocations. If the number of available page flag bits is insufficient to address all kernel allocations, profiling falls back to using page extensions with an appropriate warning to disable this config. If dynamically loaded modules add enough tags that they can't be addressed anymore with available page flag bits, memory profiling gets disabled and a warning is issued. Signed-off-by: Suren Baghdasaryan --- include/linux/alloc_tag.h | 10 +- include/linux/codetag.h | 3 + include/linux/page-flags-layout.h | 7 ++ include/linux/pgalloc_tag.h | 184 +++++++++++++++++++++++++++++- lib/Kconfig.debug | 19 +++ lib/alloc_tag.c | 90 ++++++++++++++- lib/codetag.c | 4 +- mm/mm_init.c | 5 +- 8 files changed, 310 insertions(+), 12 deletions(-) diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 7431757999c5..4f811ec0ffe0 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/page-flags-layout.h b/include/linux/page-flags-l= ayout.h index 7d79818dc065..4f5c9e979bb9 100644 --- a/include/linux/page-flags-layout.h +++ b/include/linux/page-flags-layout.h @@ -111,5 +111,12 @@ ZONES_WIDTH - LRU_GEN_WIDTH - SECTIONS_WIDTH - \ NODES_WIDTH - KASAN_TAG_WIDTH - LAST_CPUPID_WIDTH) =20 +#define NR_NON_PAGEFLAG_BITS (SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH += \ + LAST_CPUPID_SHIFT + KASAN_TAG_WIDTH + \ + LRU_GEN_WIDTH + LRU_REFS_WIDTH) + +#define NR_UNUSED_PAGEFLAG_BITS (BITS_PER_LONG - \ + (NR_NON_PAGEFLAG_BITS + NR_PAGEFLAGS)) + #endif #endif /* _LINUX_PAGE_FLAGS_LAYOUT */ diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index bc65710ee1f9..69976cf747a6 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -9,6 +9,93 @@ =20 #ifdef CONFIG_MEM_ALLOC_PROFILING =20 +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + +typedef u16 pgalloc_tag_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); + +#else /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + typedef union codetag_ref pgalloc_tag_ref; =20 static inline void read_pgref(pgalloc_tag_ref *pgref, union codetag_ref *r= ef) @@ -21,8 +108,13 @@ static inline void write_pgref(pgalloc_tag_ref *pgref, = union codetag_ref *ref) pgref->ct =3D ref->ct; } =20 +static inline void alloc_tag_sec_init(void) {} + +#endif /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + union pgtag_ref_handle { pgalloc_tag_ref *pgref; /* reference in page extension */ + struct page *page; /* reference in page flags */ }; =20 #include @@ -40,8 +132,8 @@ static inline struct page_ext *page_ext_from_pgref(pgall= oc_tag_ref *pgref) } =20 /* Should be called only if mem_alloc_profiling_enabled() */ -static inline bool get_page_tag_ref(struct page *page, union codetag_ref *= ref, - union pgtag_ref_handle *handle) +static inline bool page_ext_get_page_tag_ref(struct page *page, union code= tag_ref *ref, + union pgtag_ref_handle *handle) { struct page_ext *page_ext; pgalloc_tag_ref *pgref; @@ -59,7 +151,7 @@ static inline bool get_page_tag_ref(struct page *page, u= nion codetag_ref *ref, return true; } =20 -static inline void put_page_tag_ref(union pgtag_ref_handle handle) +static inline void page_ext_put_page_tag_ref(union pgtag_ref_handle handle) { if (WARN_ON(!handle.pgref)) return; @@ -67,8 +159,8 @@ static inline void put_page_tag_ref(union pgtag_ref_hand= le handle) page_ext_put(page_ext_from_pgref(handle.pgref)); } =20 -static inline void update_page_tag_ref(union pgtag_ref_handle handle, - union codetag_ref *ref) +static inline void page_ext_update_page_tag_ref(union pgtag_ref_handle han= dle, + union codetag_ref *ref) { if (WARN_ON(!handle.pgref || !ref)) return; @@ -76,6 +168,87 @@ static inline void update_page_tag_ref(union pgtag_ref_= handle handle, write_pgref(handle.pgref, ref); } =20 +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + +DECLARE_STATIC_KEY_TRUE(mem_profiling_use_pageflags); +extern unsigned long alloc_tag_ref_mask; +extern int alloc_tag_ref_offs; + +/* Should be called only if mem_alloc_profiling_enabled() */ +static inline bool get_page_tag_ref(struct page *page, union codetag_ref *= ref, + union pgtag_ref_handle *handle) +{ + pgalloc_tag_ref pgref; + + if (!static_key_enabled(&mem_profiling_use_pageflags)) + return page_ext_get_page_tag_ref(page, ref, handle); + + if (!page) + return false; + + pgref =3D (page->flags >> alloc_tag_ref_offs) & alloc_tag_ref_mask; + read_pgref(&pgref, ref); + handle->page =3D page; + return true; +} + +static inline void put_page_tag_ref(union pgtag_ref_handle handle) +{ + if (!static_key_enabled(&mem_profiling_use_pageflags)) { + page_ext_put_page_tag_ref(handle); + return; + } + + WARN_ON(!handle.page); +} + +static inline void update_page_tag_ref(union pgtag_ref_handle handle, unio= n codetag_ref *ref) +{ + unsigned long old_flags, flags, val; + struct page *page =3D handle.page; + pgalloc_tag_ref pgref; + + if (!static_key_enabled(&mem_profiling_use_pageflags)) { + page_ext_update_page_tag_ref(handle, ref); + return; + } + + 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_offs; + do { + old_flags =3D READ_ONCE(page->flags); + flags =3D old_flags; + flags &=3D ~(alloc_tag_ref_mask << alloc_tag_ref_offs); + flags |=3D val; + } while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags))); +} + +#else /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + +/* Should be called only if mem_alloc_profiling_enabled() */ +static inline bool get_page_tag_ref(struct page *page, union codetag_ref *= ref, + union pgtag_ref_handle *handle) +{ + return page_ext_get_page_tag_ref(page, ref, handle); +} + +static inline void put_page_tag_ref(union pgtag_ref_handle handle) +{ + page_ext_put_page_tag_ref(handle); +} + +static inline void update_page_tag_ref(union pgtag_ref_handle handle, + union codetag_ref *ref) +{ + page_ext_update_page_tag_ref(handle, ref); +} + +#endif /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + static inline void clear_page_tag_ref(struct page *page) { if (mem_alloc_profiling_enabled()) { @@ -152,6 +325,7 @@ static inline void pgalloc_tag_add(struct page *page, s= truct task_struct *task, static inline void pgalloc_tag_sub(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 7315f643817a..f4c272c30719 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1017,6 +1017,25 @@ 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 if they fit, otherwise they are encoded in page extensions. + + Setting this flag reduces memory and performance overhead of memory + allocation profiling but it requires enough unused page flag bits to + address all kernel allocations. If enabled but there are not enough + unused page flag bits, profiling will fall back to using page + extensions and a warning will be issued to disable this config. + If dynamically loaded modules add enough tags that they can't be + addressed anymore with available page flag bits, profiling for such + modules will be disabled and a warning will be issued. + + Say N if unsure. + 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 2ef762acc203..341a5c826449 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -152,6 +153,42 @@ static void __init procfs_init(void) proc_create_seq("allocinfo", 0400, NULL, &allocinfo_seq_op); } =20 +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + +DEFINE_STATIC_KEY_TRUE(mem_profiling_use_pageflags); +unsigned long alloc_tag_ref_mask; +int alloc_tag_ref_offs; + +#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; + + /* Check if kernel tags fit into page flags */ + if (kernel_tags.count > (1UL << NR_UNUSED_PAGEFLAG_BITS)) { + static_branch_disable(&mem_profiling_use_pageflags); + pr_warn("%lu kernel tags do not fit into %d available page flag bits, pa= ge extensions will be used instead!\n", + kernel_tags.count, NR_UNUSED_PAGEFLAG_BITS); + } else { + alloc_tag_ref_offs =3D (LRU_REFS_PGOFF - NR_UNUSED_PAGEFLAG_BITS); + alloc_tag_ref_mask =3D ((1UL << NR_UNUSED_PAGEFLAG_BITS) - 1); + pr_info("All unused page flags (%d bits) are used to store page tag refe= rences!\n", + NR_UNUSED_PAGEFLAG_BITS); + } +} + +#endif /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + #ifdef CONFIG_MODULES =20 static struct maple_tree mod_area_mt =3D MTREE_INIT(mod_area_mt, MT_FLAGS_= ALLOC_RANGE); @@ -161,7 +198,29 @@ 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; + +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS +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); +} + +static inline bool tags_addressable(void) +{ + unsigned long tag_idx_count =3D CODETAG_ID_FIRST + kernel_tags.count + + module_tags.size / sizeof(struct alloc_tag); + + return tag_idx_count < (1UL << NR_UNUSED_PAGEFLAG_BITS); +} + +#else /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + +static inline bool tags_addressable(void) { return true; } + +#endif /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ =20 static bool needs_section_mem(struct module *mod, unsigned long size) { @@ -237,6 +296,21 @@ static void *reserve_module_tags(struct module *mod, u= nsigned long size, if (!align) align =3D 1; =20 +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + /* + * 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, = disable CONFIG_PGALLOC_TAG_USE_PAGEFLAGS", + 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 /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ mas_lock(&mas); repeat: /* Try finding exact size and hope the start is aligned */ @@ -305,6 +379,12 @@ static void *reserve_module_tags(struct module *mod, u= nsigned long size, unsigned long phys_size =3D vm_module_tags->nr_pages << PAGE_SHIFT; =20 module_tags.size =3D offset + size; + if (!tags_addressable() && mem_alloc_profiling_enabled()) { + static_branch_disable(&mem_alloc_profiling_key); + pr_warn("With module %s there are too many tags to fit in %d page flag = bits. Memory profiling is disabled!\n", + mod->name, NR_UNUSED_PAGEFLAG_BITS); + } + if (phys_size < module_tags.size) { int grow_res; =20 @@ -406,6 +486,10 @@ static int __init alloc_mod_tags_mem(void) =20 module_tags.start_addr =3D (unsigned long)vm_module_tags->addr; module_tags.end_addr =3D module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_S= IZE; +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + /* Ensure the base is alloc_tag aligned */ + module_tags.start_addr =3D alloc_tag_align(module_tags.start_addr); +#endif =20 return 0; } @@ -467,6 +551,10 @@ early_param("sysctl.vm.mem_profiling", setup_early_mem= _profiling); =20 static __init bool need_page_alloc_tagging(void) { +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + if (static_key_enabled(&mem_profiling_use_pageflags)) + return false; +#endif return mem_profiling_support; } =20 diff --git a/lib/codetag.c b/lib/codetag.c index 3f3a4f4ca72d..cda5a852ef30 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..1c205b0a86ed 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -83,8 +83,7 @@ void __init mminit_verify_pageflags_layout(void) unsigned long or_mask, add_mask; =20 shift =3D BITS_PER_LONG; - width =3D shift - SECTIONS_WIDTH - NODES_WIDTH - ZONES_WIDTH - - LAST_CPUPID_SHIFT - KASAN_TAG_WIDTH - LRU_GEN_WIDTH - LRU_REFS_WIDTH; + width =3D shift - NR_NON_PAGEFLAG_BITS; mminit_dprintk(MMINIT_TRACE, "pageflags_layout_widths", "Section %d Node %d Zone %d Lastcpupid %d Kasantag %d Gen %d Tier %d Fla= gs %d\n", SECTIONS_WIDTH, @@ -2639,7 +2638,7 @@ void __init mm_core_init(void) BUILD_BUG_ON(MAX_ZONELISTS > 2); build_all_zonelists(NULL); page_alloc_init_cpuhp(); - + alloc_tag_sec_init(); /* * page_ext requires contiguous pages, * bigger than MAX_PAGE_ORDER unless SPARSEMEM. --=20 2.47.0.rc1.288.g06298d1525-goog