Skip to contentSkip to conversation

mm: Only re-generate demotion targets when a numa node changes its N_CPU state

PATCH Superseded Oscar Salvador 5 participants 8 messages Mar 9, 2022 – Mar 10, 2022

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: Only re-generate demotion targets when a numa node changes its N_CPU state

Abhishek reported that after patch [1], hotplug operations are taking ~double the expected time. [2]

The reason behind is that the CPU callbacks that migrate_on_reclaim_init() sets always call set_migration_target_nodes() whenever a CPU is brought up/down.
But we only care about numa nodes going from having cpus to become cpuless, and vice versa, as that influences the demotion_target order.

We do already have two CPU callbacks (vmstat_cpu_online() and vmstat_cpu_dead()) that check exactly that, so get rid of the CPU callbacks in migrate_on_reclaim_init() and only call set_migration_target_nodes() from vmstat_cpu_{dead,online}() whenever a numa node change its N_CPU state.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/

---
I think there is further room for improvement like should we call in to set_migration_target_nodes() when demotion is disabled via sysctl? I will have a look into that, but let us go with this quick fix for now. Also, I am not really strong about the Fixes tag, but it can be added if you think it makes sense.

---
 include/linux/migrate.h |  5 +++++
 mm/migrate.c            | 36 +-----------------------------------
 mm/vmstat.c             | 10 +++++++++-
 3 files changed, 15 insertions(+), 36 deletions(-)
include/linux/migrate.h +5 -0
index db96e10eb8da..c64fe2923fb0 100644
@@ -48,6 +48,11 @@ int folio_migrate_mapping(struct address_space *mapping,
 		struct folio *newfolio, struct folio *folio, int extra_count);
 
 extern bool numa_demotion_enabled;
+#ifdef CONFIG_HOTPLUG_CPU
+extern void set_migration_target_nodes(void);
+#else
+static inline void set_migration_target_nodes() {}
+#endif
 #else
 
 static inline void putback_movable_pages(struct list_head *l) {}
mm/migrate.c +1 -35
index c7da064b4781..7847e4de01d7 100644
@@ -3190,7 +3190,7 @@ static void __set_migration_target_nodes(void)
 /*
  * For callers that do not hold get_online_mems() already.
  */
-static void set_migration_target_nodes(void)
+void set_migration_target_nodes(void)
 {
 	get_online_mems();
 	__set_migration_target_nodes();
@@ -3254,47 +3254,13 @@ static int __meminit migrate_on_reclaim_callback(struct notifier_block *self,
 	return notifier_from_errno(0);
 }
 
-/*
- * React to hotplug events that might affect the migration targets
- * like events that online or offline NUMA nodes.
- *
- * The ordering is also currently dependent on which nodes have
- * CPUs.  That means we need CPU on/offline notification too.
- */
-static int migration_online_cpu(unsigned int cpu)
-{
-	set_migration_target_nodes();
-	return 0;
-}
-
-static int migration_offline_cpu(unsigned int cpu)
-{
-	set_migration_target_nodes();
-	return 0;
-}
-
 static int __init migrate_on_reclaim_init(void)
 {
-	int ret;
-
 	node_demotion = kmalloc_array(nr_node_ids,
 				      sizeof(struct demotion_nodes),
 				      GFP_KERNEL);
 	WARN_ON(!node_demotion);
 
-	ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
-					NULL, migration_offline_cpu);
-	/*
-	 * In the unlikely case that this fails, the automatic
-	 * migration targets may become suboptimal for nodes
-	 * where N_CPU changes.  With such a small impact in a
-	 * rare case, do not bother trying to do anything special.
-	 */
-	WARN_ON(ret < 0);
-	ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online",
-				migration_online_cpu, NULL);
-	WARN_ON(ret < 0);
-
 	hotplug_memory_notifier(migrate_on_reclaim_callback, 100);
 	return 0;
 }
mm/vmstat.c +9 -1
index 4057372745d0..0529a83c8f89 100644
@@ -28,6 +28,7 @@
 #include <linux/mm_inline.h>
 #include <linux/page_ext.h>
 #include <linux/page_owner.h>
+#include <linux/migrate.h>
 
 #include "internal.h"
 
@@ -2043,7 +2044,12 @@ static void __init init_cpu_node_state(void)
 static int vmstat_cpu_online(unsigned int cpu)
 {
 	refresh_zone_stat_thresholds();
-	node_set_state(cpu_to_node(cpu), N_CPU);
+
+	if (!node_state(cpu_to_node(cpu), N_CPU)) {
+		node_set_state(cpu_to_node(cpu), N_CPU);
+		set_migration_target_nodes();
+	}
+
 	return 0;
 }
 
@@ -2066,6 +2072,8 @@ static int vmstat_cpu_dead(unsigned int cpu)
 		return 0;
 
 	node_clear_state(node, N_CPU);
+	set_migration_target_nodes();
+
 	return 0;
 }
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 20 lines · On Wed, 9 Mar 2022 15:46:44 +0100 Oscar Salvador <[email protected]> wrote:

Abhishek reported that after patch [1], hotplug operations are taking ~double the expected time. [2]

The reason behind is that the CPU callbacks that migrate_on_reclaim_init() sets always call set_migration_target_nodes() whenever a CPU is brought up/down.
But we only care about numa nodes going from having cpus to become cpuless, and vice versa, as that influences the demotion_target order.

We do already have two CPU callbacks (vmstat_cpu_online() and vmstat_cpu_dead()) that check exactly that, so get rid of the CPU callbacks in migrate_on_reclaim_init() and only call set_migration_target_nodes() from vmstat_cpu_{dead,online}() whenever a numa node change its N_CPU state.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/

...

extern bool numa_demotion_enabled;

include/linux/migrate.h int folio_migrate_mapping(struct address_space *mapping, · line 54

Quoted 4 lines
include/linux/migrate.h +4 -0
+#ifdef CONFIG_HOTPLUG_CPU
+extern void set_migration_target_nodes(void);
+#else
+static inline void set_migration_target_nodes() {}

Compiler won't like that. Please test (compile and runtime) with CONFIG_HOTPLUG_CPU=n.

Quoted 3 lines

...

#2 lore raw
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Wed, 9 Mar 2022 15:46:44 +0100 Oscar Salvador <[email protected]> wrote:

+static inline void set_migration_target_nodes() {}

Compiler won't like that. Please test (compile and runtime) with CONFIG_HOTPLUG_CPU=n.

Sorry, fat fingers. This on top:

include/linux/migrate.h +1 -1
index c64fe2923fb0..b82b4a9a0136 100644
@@ -51,7 +51,7 @@ extern bool numa_demotion_enabled;
 #ifdef CONFIG_HOTPLUG_CPU
 extern void set_migration_target_nodes(void);
 #else
-static inline void set_migration_target_nodes() {}
+static inline void set_migration_target_nodes(void) {}
 #endif
 #else
 

I will wait for some feedback before sending a v2.

thanks Andrew!

Signature · 2 lines
Oscar Salvador
SUSE Labs
#3 lore raw
In reply to Oscar Salvador
Quoted 95 lines · Oscar Salvador

Abhishek reported that after patch [1], hotplug operations are taking ~double the expected time. [2]

The reason behind is that the CPU callbacks that migrate_on_reclaim_init() sets always call set_migration_target_nodes() whenever a CPU is brought up/down.
But we only care about numa nodes going from having cpus to become cpuless, and vice versa, as that influences the demotion_target order.

We do already have two CPU callbacks (vmstat_cpu_online() and vmstat_cpu_dead()) that check exactly that, so get rid of the CPU callbacks in migrate_on_reclaim_init() and only call set_migration_target_nodes() from vmstat_cpu_{dead,online}() whenever a numa node change its N_CPU state.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/

---
I think there is further room for improvement like should we call in to set_migration_target_nodes() when demotion is disabled via sysctl? I will have a look into that, but let us go with this quick fix for now. Also, I am not really strong about the Fixes tag, but it can be added if you think it makes sense.

---
 include/linux/migrate.h |  5 +++++
 mm/migrate.c            | 36 +-----------------------------------
 mm/vmstat.c             | 10 +++++++++-
 3 files changed, 15 insertions(+), 36 deletions(-)
include/linux/migrate.h +5 -0
index db96e10eb8da..c64fe2923fb0 100644
@@ -48,6 +48,11 @@ int folio_migrate_mapping(struct address_space *mapping,
 		struct folio *newfolio, struct folio *folio, int extra_count);
 
 extern bool numa_demotion_enabled;
+#ifdef CONFIG_HOTPLUG_CPU
+extern void set_migration_target_nodes(void);
+#else
+static inline void set_migration_target_nodes() {}
+#endif
 #else
 
 static inline void putback_movable_pages(struct list_head *l) {}
mm/migrate.c +1 -25
index c7da064b4781..7847e4de01d7 100644
@@ -3190,7 +3190,7 @@ static void __set_migration_target_nodes(void)
 /*
  * For callers that do not hold get_online_mems() already.
  */
-static void set_migration_target_nodes(void)
+void set_migration_target_nodes(void)
 {
 	get_online_mems();
 	__set_migration_target_nodes();
@@ -3254,47 +3254,13 @@ static int __meminit migrate_on_reclaim_callback(struct notifier_block *self,
 	return notifier_from_errno(0);
 }
 
-/*
- * React to hotplug events that might affect the migration targets
- * like events that online or offline NUMA nodes.
- *
- * The ordering is also currently dependent on which nodes have
- * CPUs.  That means we need CPU on/offline notification too.
- */
-static int migration_online_cpu(unsigned int cpu)
-{
-	set_migration_target_nodes();
-	return 0;
-}
-
-static int migration_offline_cpu(unsigned int cpu)
-{
-	set_migration_target_nodes();
-	return 0;
-}
-
 static int __init migrate_on_reclaim_init(void)
 {
-	int ret;
-
 	node_demotion = kmalloc_array(nr_node_ids,
 				      sizeof(struct demotion_nodes),
 				      GFP_KERNEL);
 	WARN_ON(!node_demotion);
 
-	ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
-					NULL, migration_offline_cpu);
-	/*

mm/migrate.c static int __meminit migrate_on_reclaim_callback(struct notifier_block *self, · line 3297

Quoted 10 lines
mm/migrate.c +0 -10
-	 * In the unlikely case that this fails, the automatic
-	 * migration targets may become suboptimal for nodes
-	 * where N_CPU changes.  With such a small impact in a
-	 * rare case, do not bother trying to do anything special.
-	 */
-	WARN_ON(ret < 0);
-	ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online",
-				migration_online_cpu, NULL);
-	WARN_ON(ret < 0);
-

We need to call set_migration_target_nodes() during system boot somewhere, either here or in init_mm_internals().

Best Regards,
Huang, Ying

Quoted 37 lines
mm/migrate.c +0 -0
 	hotplug_memory_notifier(migrate_on_reclaim_callback, 100);
 	return 0;
 }
mm/vmstat.c +9 -1
index 4057372745d0..0529a83c8f89 100644
@@ -28,6 +28,7 @@
 #include <linux/mm_inline.h>
 #include <linux/page_ext.h>
 #include <linux/page_owner.h>
+#include <linux/migrate.h>
 
 #include "internal.h"
 
@@ -2043,7 +2044,12 @@ static void __init init_cpu_node_state(void)
 static int vmstat_cpu_online(unsigned int cpu)
 {
 	refresh_zone_stat_thresholds();
-	node_set_state(cpu_to_node(cpu), N_CPU);
+
+	if (!node_state(cpu_to_node(cpu), N_CPU)) {
+		node_set_state(cpu_to_node(cpu), N_CPU);
+		set_migration_target_nodes();
+	}
+
 	return 0;
 }
 
@@ -2066,6 +2072,8 @@ static int vmstat_cpu_dead(unsigned int cpu)
 		return 0;
 
 	node_clear_state(node, N_CPU);
+	set_migration_target_nodes();
+
 	return 0;
 }
#4 lore raw
Quoted 2 lines · Ying

We need to call set_migration_target_nodes() during system boot somewhere, either here or in init_mm_internals().

Hi Huang Ying,

vmstat_cpu_online() already gets called during boot:

static struct cpuhp_step cpuhp_hp_states[] = {
...

	#ifdef CONFIG_SMP
	[CPUHP_CREATE_THREADS]= {
		.name			= "threads:prepare",
		.startup.single		= smpboot_create_threads,
		.teardown.single	= NULL,
		.cant_stop		= true,
	},

...

smpboot_create_threads
__smpboot_create_thread
smpboot_thread_fn
ht->thread_fn()
cpuhp_thread_fun
cpuhp_invoke_callback
vmstat_cpu_online

That for every CPU that is brought up during boot.

So unless I am missing something, I would say we are already covered there, right?

Signature · 2 lines
Oscar Salvador
SUSE Labs
#5 lore raw

Hi Oscar,

Quoted 27 lines · Oscar Salvador
Quoted 2 lines · Ying

We need to call set_migration_target_nodes() during system boot somewhere, either here or in init_mm_internals().

Hi Huang Ying,

vmstat_cpu_online() already gets called during boot:

static struct cpuhp_step cpuhp_hp_states[] = {
...

	#ifdef CONFIG_SMP
	[CPUHP_CREATE_THREADS]= {
		.name			= "threads:prepare",
		.startup.single		= smpboot_create_threads,
		.teardown.single	= NULL,
		.cant_stop		= true,
	},

...

smpboot_create_threads
__smpboot_create_thread
smpboot_thread_fn

    ht->thread_fn()
     cpuhp_thread_fun
      cpuhp_invoke_callback
       vmstat_cpu_online
Quoted 4 lines

That for every CPU that is brought up during boot.

So unless I am missing something, I would say we are already covered there, right?

I've tested your patch, unfortunately it can not work. It already set the node with N_CPU state in init_cpu_node_state() earlier, so in vmstat_cpu_online() ,the sentence 'if(!node_state(cpu_to_node(cpu), N_CPU))' is always false, which means it will not call set_migration_target_nodes().

Like Huang Ying said, we should call set_migration_target_nodes() in migrate_on_reclaim_init() or init_mm_internals().

#6 lore raw
In reply to Oscar Salvador
Thank you for the patch! Perhaps something to improve:

Hi Oscar,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on hnaz-mm/master]

url:    https://github.com/0day-ci/linux/commits/Oscar-Salvador/mm-Only-re-generate-demotion-targets-when-a-numa-node-changes-its-N_CPU-state/20220309-224707
base:   https://github.com/hnaz/linux-mm master
config: sparc-randconfig-r001-20220309 (https://download.01.org/0day-ci/archive/20220310/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/8af0c9ff9475c64e31963a5810b127875081c5ff
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Oscar-Salvador/mm-Only-re-generate-demotion-targets-when-a-numa-node-changes-its-N_CPU-state/20220309-224707 git checkout 8af0c9ff9475c64e31963a5810b127875081c5ff
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=sparc SHELL=/bin/bash kernel/sched/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

In file included from kernel/sched/sched.h:53,
from kernel/sched/core.c:13:
include/linux/migrate.h:54:20: error: function declaration isn't a prototype [-Werror=strict-prototypes]

      54 | static inline void set_migration_target_nodes() {}
         |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/migrate.h: In function 'set_migration_target_nodes':
>> include/linux/migrate.h:54:20: warning: old-style function definition [-Wold-style-definition]

kernel/sched/core.c: At top level:
kernel/sched/core.c:3442:6: warning: no previous prototype for 'sched_set_stop_task' [-Wmissing-prototypes]
3442 | void sched_set_stop_task(int cpu, struct task_struct *stop)
| ^~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
--
In file included from kernel/sched/sched.h:53,
from kernel/sched/loadavg.c:9:
include/linux/migrate.h:54:20: error: function declaration isn't a prototype [-Werror=strict-prototypes]

      54 | static inline void set_migration_target_nodes() {}
         |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/migrate.h: In function 'set_migration_target_nodes':
>> include/linux/migrate.h:54:20: warning: old-style function definition [-Wold-style-definition]

cc1: some warnings being treated as errors
--
In file included from kernel/sched/sched.h:53,
from kernel/sched/fair.c:23:
include/linux/migrate.h:54:20: error: function declaration isn't a prototype [-Werror=strict-prototypes]

      54 | static inline void set_migration_target_nodes() {}
         |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/migrate.h: In function 'set_migration_target_nodes':
>> include/linux/migrate.h:54:20: warning: old-style function definition [-Wold-style-definition]
   kernel/sched/fair.c: At top level:
   kernel/sched/fair.c:11135:6: warning: no previous prototype for 'task_vruntime_update' [-Wmissing-prototypes]
   11135 | void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi)
         |      ^~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors
--
In file included from kernel/sched/sched.h:53,
from kernel/sched/rt.c:6:
include/linux/migrate.h:54:20: error: function declaration isn't a prototype [-Werror=strict-prototypes]

      54 | static inline void set_migration_target_nodes() {}
         |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/migrate.h: In function 'set_migration_target_nodes':
>> include/linux/migrate.h:54:20: warning: old-style function definition [-Wold-style-definition]

kernel/sched/rt.c: At top level:
kernel/sched/rt.c:730:6: warning: no previous prototype for 'sched_rt_bandwidth_account' [-Wmissing-prototypes]
730 | bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

vim +54 include/linux/migrate.h

    36	
    37	extern void migrate_page_states(struct page *newpage, struct page *page);
    38	extern void migrate_page_copy(struct page *newpage, struct page *page);
    39	extern int migrate_huge_page_move_mapping(struct address_space *mapping,
    40					  struct page *newpage, struct page *page);
    41	extern int migrate_page_move_mapping(struct address_space *mapping,
    42			struct page *newpage, struct page *page, int extra_count);
    43	void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
    44					spinlock_t *ptl);
    45	void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
    46	void folio_migrate_copy(struct folio *newfolio, struct folio *folio);
    47	int folio_migrate_mapping(struct address_space *mapping,
    48			struct folio *newfolio, struct folio *folio, int extra_count);
    49	
    50	extern bool numa_demotion_enabled;
    51	#ifdef CONFIG_HOTPLUG_CPU
    52	extern void set_migration_target_nodes(void);
    53	#else
  > 54	static inline void set_migration_target_nodes() {}
    55	#endif
    56	#else
    57	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#7 lore raw
In reply to Oscar Salvador
Thank you for the patch! Yet something to improve:

Hi Oscar,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on hnaz-mm/master]

url:    https://github.com/0day-ci/linux/commits/Oscar-Salvador/mm-Only-re-generate-demotion-targets-when-a-numa-node-changes-its-N_CPU-state/20220309-224707
base:   https://github.com/hnaz/linux-mm master
config: riscv-randconfig-s032-20220309 (https://download.01.org/0day-ci/archive/20220310/[email protected]/config)
compiler: riscv32-linux-gcc (GCC) 11.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-dirty
# https://github.com/0day-ci/linux/commit/8af0c9ff9475c64e31963a5810b127875081c5ff
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Oscar-Salvador/mm-Only-re-generate-demotion-targets-when-a-numa-node-changes-its-N_CPU-state/20220309-224707 git checkout 8af0c9ff9475c64e31963a5810b127875081c5ff
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=riscv SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

mm/vmstat.c: In function 'vmstat_cpu_online':

>> mm/vmstat.c:2056:17: error: implicit declaration of function 'set_migration_target_nodes' [-Werror=implicit-function-declaration]
    2056 |                 set_migration_target_nodes();
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

vim +/set_migration_target_nodes +2056 mm/vmstat.c

2049
2050 static int vmstat_cpu_online(unsigned int cpu)
2051 {
2052 refresh_zone_stat_thresholds();
2053
2054 if (!node_state(cpu_to_node(cpu), N_CPU)) {
2055 node_set_state(cpu_to_node(cpu), N_CPU);

> 2056			set_migration_target_nodes();

2057 }
2058
2059 return 0;
2060 }
2061

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]