mm: vmalloc: add a shrinker to drain vmap pools
authorUladzislau Rezki (Sony) <urezki@gmail.com>
Tue, 2 Jan 2024 18:46:33 +0000 (19:46 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 24 Feb 2024 01:48:21 +0000 (17:48 -0800)
The added shrinker is used to return back current cached VAs into a global
vmap space, when a system enters into a low memory mode.

Link: https://lkml.kernel.org/r/20240102184633.748113-12-urezki@gmail.com
Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
Cc: Kazuhito Hagio <k-hagio-ab@nec.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Joel Fernandes (Google) <joel@joelfernandes.org>
Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
Cc: Lorenzo Stoakes <lstoakes@gmail.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Oleksiy Avramchenko <oleksiy.avramchenko@sony.com>
Cc: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/vmalloc.c

index 4d90a6ad83aa7bf7f3a853020885babcf6b272ad..6746491de381ef03b02d689737afeb750fa7db98 100644 (file)
@@ -4917,8 +4917,37 @@ static void vmap_init_nodes(void)
        }
 }
 
+static unsigned long
+vmap_node_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
+{
+       unsigned long count;
+       struct vmap_node *vn;
+       int i, j;
+
+       for (count = 0, i = 0; i < nr_vmap_nodes; i++) {
+               vn = &vmap_nodes[i];
+
+               for (j = 0; j < MAX_VA_SIZE_PAGES; j++)
+                       count += READ_ONCE(vn->pool[j].len);
+       }
+
+       return count ? count : SHRINK_EMPTY;
+}
+
+static unsigned long
+vmap_node_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
+{
+       int i;
+
+       for (i = 0; i < nr_vmap_nodes; i++)
+               decay_va_pool_node(&vmap_nodes[i], true);
+
+       return SHRINK_STOP;
+}
+
 void __init vmalloc_init(void)
 {
+       struct shrinker *vmap_node_shrinker;
        struct vmap_area *va;
        struct vmap_node *vn;
        struct vm_struct *tmp;
@@ -4966,4 +4995,14 @@ void __init vmalloc_init(void)
         */
        vmap_init_free_space();
        vmap_initialized = true;
+
+       vmap_node_shrinker = shrinker_alloc(0, "vmap-node");
+       if (!vmap_node_shrinker) {
+               pr_err("Failed to allocate vmap-node shrinker!\n");
+               return;
+       }
+
+       vmap_node_shrinker->count_objects = vmap_node_shrink_count;
+       vmap_node_shrinker->scan_objects = vmap_node_shrink_scan;
+       shrinker_register(vmap_node_shrinker);
 }