linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] vmalloc: Add for_each_vmap_node() helper
@ 2025-04-08 15:15 Uladzislau Rezki (Sony)
  2025-04-08 15:15 ` [PATCH 2/3] vmalloc: Switch to " Uladzislau Rezki (Sony)
  2025-04-08 15:15 ` [PATCH 3/3] vmalloc: Use for_each_vmap_node() in purge-vmap-area Uladzislau Rezki (Sony)
  0 siblings, 2 replies; 6+ messages in thread
From: Uladzislau Rezki (Sony) @ 2025-04-08 15:15 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-mm, LKML, Christoph Hellwig, Uladzislau Rezki,
	Oleksiy Avramchenko

To simplify iteration over vmap-nodes, add the for_each_vmap_node()
macro that iterates over all nodes in a system. It tends to simplify
the code.

Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
---
 mm/vmalloc.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index bcc90d4357e48..e42ea20713dc7 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -900,6 +900,11 @@ static struct vmap_node *vmap_nodes = &single;
 static __read_mostly unsigned int nr_vmap_nodes = 1;
 static __read_mostly unsigned int vmap_zone_size = 1;
 
+/* A simple iterator over all vmap-nodes. */
+#define for_each_vmap_node(vn)	\
+	for ((vn) = &vmap_nodes[0];	\
+		(vn) < &vmap_nodes[nr_vmap_nodes]; (vn)++)
+
 static inline unsigned int
 addr_to_node_id(unsigned long addr)
 {
-- 
2.39.5



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] vmalloc: Switch to for_each_vmap_node() helper
  2025-04-08 15:15 [PATCH 1/3] vmalloc: Add for_each_vmap_node() helper Uladzislau Rezki (Sony)
@ 2025-04-08 15:15 ` Uladzislau Rezki (Sony)
  2025-04-09 17:39   ` kernel test robot
  2025-04-08 15:15 ` [PATCH 3/3] vmalloc: Use for_each_vmap_node() in purge-vmap-area Uladzislau Rezki (Sony)
  1 sibling, 1 reply; 6+ messages in thread
From: Uladzislau Rezki (Sony) @ 2025-04-08 15:15 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-mm, LKML, Christoph Hellwig, Uladzislau Rezki,
	Oleksiy Avramchenko

There are places which can be updated easily to use the
helper to iterate over all vmap-nodes. This is what this
patch does.

The aim is to improve readability and simplify the code.

Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
---
 mm/vmalloc.c | 36 +++++++++++++-----------------------
 1 file changed, 13 insertions(+), 23 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index e42ea20713dc7..3ff9acd64c077 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -1061,12 +1061,11 @@ find_vmap_area_exceed_addr_lock(unsigned long addr, struct vmap_area **va)
 {
 	unsigned long va_start_lowest;
 	struct vmap_node *vn;
-	int i;
 
 repeat:
-	for (i = 0, va_start_lowest = 0; i < nr_vmap_nodes; i++) {
-		vn = &vmap_nodes[i];
+	va_start_lowest = 0;
 
+	for_each_vmap_node(vn) {
 		spin_lock(&vn->busy.lock);
 		*va = __find_vmap_area_exceed_addr(addr, &vn->busy.root);
 
@@ -4946,11 +4945,8 @@ static void show_purge_info(struct seq_file *m)
 {
 	struct vmap_node *vn;
 	struct vmap_area *va;
-	int i;
-
-	for (i = 0; i < nr_vmap_nodes; i++) {
-		vn = &vmap_nodes[i];
 
+	for_each_vmap_node(vn) {
 		spin_lock(&vn->lazy.lock);
 		list_for_each_entry(va, &vn->lazy.head, list) {
 			seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
@@ -4966,11 +4962,8 @@ static int vmalloc_info_show(struct seq_file *m, void *p)
 	struct vmap_node *vn;
 	struct vmap_area *va;
 	struct vm_struct *v;
-	int i;
-
-	for (i = 0; i < nr_vmap_nodes; i++) {
-		vn = &vmap_nodes[i];
 
+	for_each_vmap_node(vn) {
 		spin_lock(&vn->busy.lock);
 		list_for_each_entry(va, &vn->busy.head, list) {
 			if (!va->vm) {
@@ -5123,8 +5116,7 @@ static void vmap_init_nodes(void)
 	}
 #endif
 
-	for (n = 0; n < nr_vmap_nodes; n++) {
-		vn = &vmap_nodes[n];
+	for_each_vmap_node(vn) {
 		vn->busy.root = RB_ROOT;
 		INIT_LIST_HEAD(&vn->busy.head);
 		spin_lock_init(&vn->busy.lock);
@@ -5145,15 +5137,13 @@ static void vmap_init_nodes(void)
 static unsigned long
 vmap_node_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
 {
-	unsigned long count;
+	unsigned long count = 0;
 	struct vmap_node *vn;
-	int i, j;
-
-	for (count = 0, i = 0; i < nr_vmap_nodes; i++) {
-		vn = &vmap_nodes[i];
+	int i;
 
-		for (j = 0; j < MAX_VA_SIZE_PAGES; j++)
-			count += READ_ONCE(vn->pool[j].len);
+	for_each_vmap_node(vn) {
+		for (i = 0; i < MAX_VA_SIZE_PAGES; i++)
+			count += READ_ONCE(vn->pool[i].len);
 	}
 
 	return count ? count : SHRINK_EMPTY;
@@ -5162,10 +5152,10 @@ vmap_node_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
 static unsigned long
 vmap_node_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 {
-	int i;
+	struct vmap_node *vn;
 
-	for (i = 0; i < nr_vmap_nodes; i++)
-		decay_va_pool_node(&vmap_nodes[i], true);
+	for_each_vmap_node(vn)
+		decay_va_pool_node(vn, true);
 
 	return SHRINK_STOP;
 }
-- 
2.39.5



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] vmalloc: Use for_each_vmap_node() in purge-vmap-area
  2025-04-08 15:15 [PATCH 1/3] vmalloc: Add for_each_vmap_node() helper Uladzislau Rezki (Sony)
  2025-04-08 15:15 ` [PATCH 2/3] vmalloc: Switch to " Uladzislau Rezki (Sony)
@ 2025-04-08 15:15 ` Uladzislau Rezki (Sony)
  1 sibling, 0 replies; 6+ messages in thread
From: Uladzislau Rezki (Sony) @ 2025-04-08 15:15 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-mm, LKML, Christoph Hellwig, Uladzislau Rezki,
	Oleksiy Avramchenko

Update a __purge_vmap_area_lazy() to use introduced helper.
This is last place in vmalloc code. Also this patch introduces
an extra function which is node_to_id() that converts a vmap_node
pointer to an index in array.

__purge_vmap_area_lazy() requires that extra function.

Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
---
 mm/vmalloc.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 3ff9acd64c077..409b8f372647f 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -923,6 +923,19 @@ id_to_node(unsigned int id)
 	return &vmap_nodes[id % nr_vmap_nodes];
 }
 
+static inline unsigned int
+node_to_id(struct vmap_node *node)
+{
+	/* Pointer arithmetic. */
+	unsigned int id = node - vmap_nodes;
+
+	if (likely(id < nr_vmap_nodes))
+		return id;
+
+	WARN_ONCE(1, "An address 0x%p is out-of-bounds.\n", node);
+	return 0;
+}
+
 /*
  * We use the value 0 to represent "no node", that is why
  * an encoded value will be the node-id incremented by 1.
@@ -2259,9 +2272,7 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end,
 	 */
 	purge_nodes = CPU_MASK_NONE;
 
-	for (i = 0; i < nr_vmap_nodes; i++) {
-		vn = &vmap_nodes[i];
-
+	for_each_vmap_node(vn) {
 		INIT_LIST_HEAD(&vn->purge_list);
 		vn->skip_populate = full_pool_decay;
 		decay_va_pool_node(vn, full_pool_decay);
@@ -2280,7 +2291,7 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end,
 		end = max(end, list_last_entry(&vn->purge_list,
 			struct vmap_area, list)->va_end);
 
-		cpumask_set_cpu(i, &purge_nodes);
+		cpumask_set_cpu(node_to_id(vn), &purge_nodes);
 	}
 
 	nr_purge_nodes = cpumask_weight(&purge_nodes);
-- 
2.39.5



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] vmalloc: Switch to for_each_vmap_node() helper
  2025-04-08 15:15 ` [PATCH 2/3] vmalloc: Switch to " Uladzislau Rezki (Sony)
@ 2025-04-09 17:39   ` kernel test robot
  2025-04-10  0:32     ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: kernel test robot @ 2025-04-09 17:39 UTC (permalink / raw)
  To: Uladzislau Rezki (Sony), Andrew Morton
  Cc: oe-kbuild-all, Linux Memory Management List, LKML,
	Christoph Hellwig, Uladzislau Rezki, Oleksiy Avramchenko

Hi Uladzislau,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master v6.15-rc1 next-20250409]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Uladzislau-Rezki-Sony/vmalloc-Switch-to-for_each_vmap_node-helper/20250408-231807
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250408151549.77937-2-urezki%40gmail.com
patch subject: [PATCH 2/3] vmalloc: Switch to for_each_vmap_node() helper
config: sparc-randconfig-002-20250409 (https://download.01.org/0day-ci/archive/20250410/202504100130.OjlBJLkQ-lkp@intel.com/config)
compiler: sparc-linux-gcc (GCC) 7.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250410/202504100130.OjlBJLkQ-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202504100130.OjlBJLkQ-lkp@intel.com/

All warnings (new ones prefixed by >>):

   mm/vmalloc.c: In function 'vmap_init_nodes':
>> mm/vmalloc.c:5087:9: warning: unused variable 'n' [-Wunused-variable]
     int i, n;
            ^


vim +/n +5087 mm/vmalloc.c

7fa8cee003166e Uladzislau Rezki (Sony  2024-01-02  5083) 
d093602919ad59 Uladzislau Rezki (Sony  2024-01-02  5084) static void vmap_init_nodes(void)
d093602919ad59 Uladzislau Rezki (Sony  2024-01-02  5085) {
d093602919ad59 Uladzislau Rezki (Sony  2024-01-02  5086) 	struct vmap_node *vn;
8f33a2ff307248 Uladzislau Rezki (Sony  2024-01-02 @5087) 	int i, n;
8f33a2ff307248 Uladzislau Rezki (Sony  2024-01-02  5088) 

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] vmalloc: Switch to for_each_vmap_node() helper
  2025-04-09 17:39   ` kernel test robot
@ 2025-04-10  0:32     ` Andrew Morton
  2025-04-10  9:34       ` Uladzislau Rezki
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2025-04-10  0:32 UTC (permalink / raw)
  To: kernel test robot
  Cc: Uladzislau Rezki (Sony), oe-kbuild-all,
	Linux Memory Management List, LKML, Christoph Hellwig,
	Oleksiy Avramchenko

On Thu, 10 Apr 2025 01:39:24 +0800 kernel test robot <lkp@intel.com> wrote:

> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202504100130.OjlBJLkQ-lkp@intel.com/
> 
> All warnings (new ones prefixed by >>):
> 
>    mm/vmalloc.c: In function 'vmap_init_nodes':
> >> mm/vmalloc.c:5087:9: warning: unused variable 'n' [-Wunused-variable]
>      int i, n;
>             ^

Thanks, I added

--- a/mm/vmalloc.c~vmalloc-switch-to-for_each_vmap_node-helper-fix
+++ a/mm/vmalloc.c
@@ -5084,7 +5084,7 @@ static void __init vmap_init_free_space(
 static void vmap_init_nodes(void)
 {
 	struct vmap_node *vn;
-	int i, n;
+	int i;
 
 #if BITS_PER_LONG == 64
 	/*
@@ -5101,7 +5101,7 @@ static void vmap_init_nodes(void)
 	 * set of cores. Therefore a per-domain purging is supposed to
 	 * be added as well as a per-domain balancing.
 	 */
-	n = clamp_t(unsigned int, num_possible_cpus(), 1, 128);
+	int n = clamp_t(unsigned int, num_possible_cpus(), 1, 128);
 
 	if (n > 1) {
 		vn = kmalloc_array(n, sizeof(*vn), GFP_NOWAIT | __GFP_NOWARN);
_



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] vmalloc: Switch to for_each_vmap_node() helper
  2025-04-10  0:32     ` Andrew Morton
@ 2025-04-10  9:34       ` Uladzislau Rezki
  0 siblings, 0 replies; 6+ messages in thread
From: Uladzislau Rezki @ 2025-04-10  9:34 UTC (permalink / raw)
  To: Andrew Morton
  Cc: kernel test robot, Uladzislau Rezki (Sony), oe-kbuild-all,
	Linux Memory Management List, LKML, Christoph Hellwig,
	Oleksiy Avramchenko

On Wed, Apr 09, 2025 at 05:32:17PM -0700, Andrew Morton wrote:
> On Thu, 10 Apr 2025 01:39:24 +0800 kernel test robot <lkp@intel.com> wrote:
> 
> > | Reported-by: kernel test robot <lkp@intel.com>
> > | Closes: https://lore.kernel.org/oe-kbuild-all/202504100130.OjlBJLkQ-lkp@intel.com/
> > 
> > All warnings (new ones prefixed by >>):
> > 
> >    mm/vmalloc.c: In function 'vmap_init_nodes':
> > >> mm/vmalloc.c:5087:9: warning: unused variable 'n' [-Wunused-variable]
> >      int i, n;
> >             ^
> 
> Thanks, I added
> 
> --- a/mm/vmalloc.c~vmalloc-switch-to-for_each_vmap_node-helper-fix
> +++ a/mm/vmalloc.c
> @@ -5084,7 +5084,7 @@ static void __init vmap_init_free_space(
>  static void vmap_init_nodes(void)
>  {
>  	struct vmap_node *vn;
> -	int i, n;
> +	int i;
>  
>  #if BITS_PER_LONG == 64
>  	/*
> @@ -5101,7 +5101,7 @@ static void vmap_init_nodes(void)
>  	 * set of cores. Therefore a per-domain purging is supposed to
>  	 * be added as well as a per-domain balancing.
>  	 */
> -	n = clamp_t(unsigned int, num_possible_cpus(), 1, 128);
> +	int n = clamp_t(unsigned int, num_possible_cpus(), 1, 128);
>  
>  	if (n > 1) {
>  		vn = kmalloc_array(n, sizeof(*vn), GFP_NOWAIT | __GFP_NOWARN);
> _
> 
I was about to send a fix for build warning. Thank you Andrew for sorting this out :)

--
Uladzislau Rezki


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-04-10  9:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-08 15:15 [PATCH 1/3] vmalloc: Add for_each_vmap_node() helper Uladzislau Rezki (Sony)
2025-04-08 15:15 ` [PATCH 2/3] vmalloc: Switch to " Uladzislau Rezki (Sony)
2025-04-09 17:39   ` kernel test robot
2025-04-10  0:32     ` Andrew Morton
2025-04-10  9:34       ` Uladzislau Rezki
2025-04-08 15:15 ` [PATCH 3/3] vmalloc: Use for_each_vmap_node() in purge-vmap-area Uladzislau Rezki (Sony)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).