Skip to content

Commit 4518085

Browse files
Kemi Wangtorvalds
authored andcommitted
mm, sysctl: make NUMA stats configurable
This is the second step which introduces a tunable interface that allow numa stats configurable for optimizing zone_statistics(), as suggested by Dave Hansen and Ying Huang. ========================================================================= When page allocation performance becomes a bottleneck and you can tolerate some possible tool breakage and decreased numa counter precision, you can do: echo 0 > /proc/sys/vm/numa_stat In this case, numa counter update is ignored. We can see about *4.8%*(185->176) drop of cpu cycles per single page allocation and reclaim on Jesper's page_bench01 (single thread) and *8.1%*(343->315) drop of cpu cycles per single page allocation and reclaim on Jesper's page_bench03 (88 threads) running on a 2-Socket Broadwell-based server (88 threads, 126G memory). Benchmark link provided by Jesper D Brouer (increase loop times to 10000000): https://github.com/netoptimizer/prototype-kernel/tree/master/kernel/mm/bench ========================================================================= When page allocation performance is not a bottleneck and you want all tooling to work, you can do: echo 1 > /proc/sys/vm/numa_stat This is system default setting. Many thanks to Michal Hocko, Dave Hansen, Ying Huang and Vlastimil Babka for comments to help improve the original patch. [[email protected]: make sure mutex is a global static] Link: http://lkml.kernel.org/r/20171107213809.GA4314@beast Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Kemi Wang <[email protected]> Signed-off-by: Kees Cook <[email protected]> Reported-by: Jesper Dangaard Brouer <[email protected]> Suggested-by: Dave Hansen <[email protected]> Suggested-by: Ying Huang <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Acked-by: Michal Hocko <[email protected]> Cc: "Luis R . Rodriguez" <[email protected]> Cc: Kees Cook <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Christopher Lameter <[email protected]> Cc: Sebastian Andrzej Siewior <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Tim Chen <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Aaron Lu <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 9a8ec03 commit 4518085

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

Documentation/sysctl/vm.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Currently, these files are in /proc/sys/vm:
5858
- percpu_pagelist_fraction
5959
- stat_interval
6060
- stat_refresh
61+
- numa_stat
6162
- swappiness
6263
- user_reserve_kbytes
6364
- vfs_cache_pressure
@@ -799,6 +800,21 @@ with no ill effects: errors and warnings on these stats are suppressed.)
799800

800801
==============================================================
801802

803+
numa_stat
804+
805+
This interface allows runtime configuration of numa statistics.
806+
807+
When page allocation performance becomes a bottleneck and you can tolerate
808+
some possible tool breakage and decreased numa counter precision, you can
809+
do:
810+
echo 0 > /proc/sys/vm/numa_stat
811+
812+
When page allocation performance is not a bottleneck and you want all
813+
tooling to work, you can do:
814+
echo 1 > /proc/sys/vm/numa_stat
815+
816+
==============================================================
817+
802818
swappiness
803819

804820
This control is used to define how aggressive the kernel will swap

include/linux/vmstat.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@
77
#include <linux/mmzone.h>
88
#include <linux/vm_event_item.h>
99
#include <linux/atomic.h>
10+
#include <linux/static_key.h>
1011

1112
extern int sysctl_stat_interval;
1213

14+
#ifdef CONFIG_NUMA
15+
#define ENABLE_NUMA_STAT 1
16+
#define DISABLE_NUMA_STAT 0
17+
extern int sysctl_vm_numa_stat;
18+
DECLARE_STATIC_KEY_TRUE(vm_numa_stat_key);
19+
extern int sysctl_vm_numa_stat_handler(struct ctl_table *table,
20+
int write, void __user *buffer, size_t *length, loff_t *ppos);
21+
#endif
22+
1323
#ifdef CONFIG_VM_EVENT_COUNTERS
1424
/*
1525
* Light weight per cpu counter implementation.

kernel/sysctl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,15 @@ static struct ctl_table vm_table[] = {
13561356
.mode = 0644,
13571357
.proc_handler = &hugetlb_mempolicy_sysctl_handler,
13581358
},
1359+
{
1360+
.procname = "numa_stat",
1361+
.data = &sysctl_vm_numa_stat,
1362+
.maxlen = sizeof(int),
1363+
.mode = 0644,
1364+
.proc_handler = sysctl_vm_numa_stat_handler,
1365+
.extra1 = &zero,
1366+
.extra2 = &one,
1367+
},
13591368
#endif
13601369
{
13611370
.procname = "hugetlb_shm_group",

mm/mempolicy.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,9 @@ static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
19151915
struct page *page;
19161916

19171917
page = __alloc_pages(gfp, order, nid);
1918+
/* skip NUMA_INTERLEAVE_HIT counter update if numa stats is disabled */
1919+
if (!static_branch_likely(&vm_numa_stat_key))
1920+
return page;
19181921
if (page && page_to_nid(page) == nid) {
19191922
preempt_disable();
19201923
__inc_numa_state(page_zone(page), NUMA_INTERLEAVE_HIT);

mm/page_alloc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ DEFINE_PER_CPU(int, numa_node);
8282
EXPORT_PER_CPU_SYMBOL(numa_node);
8383
#endif
8484

85+
DEFINE_STATIC_KEY_TRUE(vm_numa_stat_key);
86+
8587
#ifdef CONFIG_HAVE_MEMORYLESS_NODES
8688
/*
8789
* N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
@@ -2777,6 +2779,10 @@ static inline void zone_statistics(struct zone *preferred_zone, struct zone *z)
27772779
#ifdef CONFIG_NUMA
27782780
enum numa_stat_item local_stat = NUMA_LOCAL;
27792781

2782+
/* skip numa counters update if numa stats is disabled */
2783+
if (!static_branch_likely(&vm_numa_stat_key))
2784+
return;
2785+
27802786
if (z->node != numa_node_id())
27812787
local_stat = NUMA_OTHER;
27822788

mm/vmstat.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,77 @@
3232

3333
#define NUMA_STATS_THRESHOLD (U16_MAX - 2)
3434

35+
#ifdef CONFIG_NUMA
36+
int sysctl_vm_numa_stat = ENABLE_NUMA_STAT;
37+
38+
/* zero numa counters within a zone */
39+
static void zero_zone_numa_counters(struct zone *zone)
40+
{
41+
int item, cpu;
42+
43+
for (item = 0; item < NR_VM_NUMA_STAT_ITEMS; item++) {
44+
atomic_long_set(&zone->vm_numa_stat[item], 0);
45+
for_each_online_cpu(cpu)
46+
per_cpu_ptr(zone->pageset, cpu)->vm_numa_stat_diff[item]
47+
= 0;
48+
}
49+
}
50+
51+
/* zero numa counters of all the populated zones */
52+
static void zero_zones_numa_counters(void)
53+
{
54+
struct zone *zone;
55+
56+
for_each_populated_zone(zone)
57+
zero_zone_numa_counters(zone);
58+
}
59+
60+
/* zero global numa counters */
61+
static void zero_global_numa_counters(void)
62+
{
63+
int item;
64+
65+
for (item = 0; item < NR_VM_NUMA_STAT_ITEMS; item++)
66+
atomic_long_set(&vm_numa_stat[item], 0);
67+
}
68+
69+
static void invalid_numa_statistics(void)
70+
{
71+
zero_zones_numa_counters();
72+
zero_global_numa_counters();
73+
}
74+
75+
static DEFINE_MUTEX(vm_numa_stat_lock);
76+
77+
int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
78+
void __user *buffer, size_t *length, loff_t *ppos)
79+
{
80+
int ret, oldval;
81+
82+
mutex_lock(&vm_numa_stat_lock);
83+
if (write)
84+
oldval = sysctl_vm_numa_stat;
85+
ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
86+
if (ret || !write)
87+
goto out;
88+
89+
if (oldval == sysctl_vm_numa_stat)
90+
goto out;
91+
else if (sysctl_vm_numa_stat == ENABLE_NUMA_STAT) {
92+
static_branch_enable(&vm_numa_stat_key);
93+
pr_info("enable numa statistics\n");
94+
} else {
95+
static_branch_disable(&vm_numa_stat_key);
96+
invalid_numa_statistics();
97+
pr_info("disable numa statistics, and clear numa counters\n");
98+
}
99+
100+
out:
101+
mutex_unlock(&vm_numa_stat_lock);
102+
return ret;
103+
}
104+
#endif
105+
35106
#ifdef CONFIG_VM_EVENT_COUNTERS
36107
DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
37108
EXPORT_PER_CPU_SYMBOL(vm_event_states);

0 commit comments

Comments
 (0)