Skip to content

Commit 6b50026

Browse files
rientjesJiri Slaby
authored andcommitted
mm, pcp: allow restoring percpu_pagelist_fraction default
commit 7cd2b0a upstream. Oleg reports a division by zero error on zero-length write() to the percpu_pagelist_fraction sysctl: divide error: 0000 [#1] SMP DEBUG_PAGEALLOC CPU: 1 PID: 9142 Comm: badarea_io Not tainted 3.15.0-rc2-vm-nfs+ #19 Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011 task: ffff8800d5aeb6e0 ti: ffff8800d87a2000 task.ti: ffff8800d87a2000 RIP: 0010: percpu_pagelist_fraction_sysctl_handler+0x84/0x120 RSP: 0018:ffff8800d87a3e78 EFLAGS: 00010246 RAX: 0000000000000f89 RBX: ffff88011f7fd000 RCX: 0000000000000000 RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000010 RBP: ffff8800d87a3e98 R08: ffffffff81d002c8 R09: ffff8800d87a3f50 R10: 000000000000000b R11: 0000000000000246 R12: 0000000000000060 R13: ffffffff81c3c3e0 R14: ffffffff81cfddf8 R15: ffff8801193b0800 FS: 00007f614f1e9740(0000) GS:ffff88011f440000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b CR2: 00007f614f1fa000 CR3: 00000000d9291000 CR4: 00000000000006e0 Call Trace: proc_sys_call_handler+0xb3/0xc0 proc_sys_write+0x14/0x20 vfs_write+0xba/0x1e0 SyS_write+0x46/0xb0 tracesys+0xe1/0xe6 However, if the percpu_pagelist_fraction sysctl is set by the user, it is also impossible to restore it to the kernel default since the user cannot write 0 to the sysctl. This patch allows the user to write 0 to restore the default behavior. It still requires a fraction equal to or larger than 8, however, as stated by the documentation for sanity. If a value in the range [1, 7] is written, the sysctl will return EINVAL. This successfully solves the divide by zero issue at the same time. Signed-off-by: David Rientjes <rientjes@google.com> Reported-by: Oleg Drokin <green@linuxhacker.ru> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 78a97b5 commit 6b50026

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

Documentation/sysctl/vm.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@ The batch value of each per cpu pagelist is also updated as a result. It is
664664
set to pcp->high/4. The upper limit of batch is (PAGE_SHIFT * 8)
665665

666666
The initial value is zero. Kernel does not use this value at boot time to set
667-
the high water marks for each per cpu page list.
667+
the high water marks for each per cpu page list. If the user writes '0' to this
668+
sysctl, it will revert to this default behavior.
668669

669670
==============================================================
670671

kernel/sysctl.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ static unsigned long dirty_bytes_min = 2 * PAGE_SIZE;
138138
/* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
139139
static int maxolduid = 65535;
140140
static int minolduid;
141-
static int min_percpu_pagelist_fract = 8;
142141

143142
static int ngroups_max = NGROUPS_MAX;
144143
static const int cap_last_cap = CAP_LAST_CAP;
@@ -1287,7 +1286,7 @@ static struct ctl_table vm_table[] = {
12871286
.maxlen = sizeof(percpu_pagelist_fraction),
12881287
.mode = 0644,
12891288
.proc_handler = percpu_pagelist_fraction_sysctl_handler,
1290-
.extra1 = &min_percpu_pagelist_fract,
1289+
.extra1 = &zero,
12911290
},
12921291
#ifdef CONFIG_MMU
12931292
{

mm/page_alloc.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969

7070
/* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
7171
static DEFINE_MUTEX(pcp_batch_high_lock);
72+
#define MIN_PERCPU_PAGELIST_FRACTION (8)
7273

7374
#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
7475
DEFINE_PER_CPU(int, numa_node);
@@ -4079,7 +4080,7 @@ static void __meminit zone_init_free_lists(struct zone *zone)
40794080
memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
40804081
#endif
40814082

4082-
static int __meminit zone_batchsize(struct zone *zone)
4083+
static int zone_batchsize(struct zone *zone)
40834084
{
40844085
#ifdef CONFIG_MMU
40854086
int batch;
@@ -4195,8 +4196,8 @@ static void pageset_set_high(struct per_cpu_pageset *p,
41954196
pageset_update(&p->pcp, high, batch);
41964197
}
41974198

4198-
static void __meminit pageset_set_high_and_batch(struct zone *zone,
4199-
struct per_cpu_pageset *pcp)
4199+
static void pageset_set_high_and_batch(struct zone *zone,
4200+
struct per_cpu_pageset *pcp)
42004201
{
42014202
if (percpu_pagelist_fraction)
42024203
pageset_set_high(pcp,
@@ -5789,23 +5790,38 @@ int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
57895790
void __user *buffer, size_t *length, loff_t *ppos)
57905791
{
57915792
struct zone *zone;
5792-
unsigned int cpu;
5793+
int old_percpu_pagelist_fraction;
57935794
int ret;
57945795

5796+
mutex_lock(&pcp_batch_high_lock);
5797+
old_percpu_pagelist_fraction = percpu_pagelist_fraction;
5798+
57955799
ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
5796-
if (!write || (ret < 0))
5797-
return ret;
5800+
if (!write || ret < 0)
5801+
goto out;
5802+
5803+
/* Sanity checking to avoid pcp imbalance */
5804+
if (percpu_pagelist_fraction &&
5805+
percpu_pagelist_fraction < MIN_PERCPU_PAGELIST_FRACTION) {
5806+
percpu_pagelist_fraction = old_percpu_pagelist_fraction;
5807+
ret = -EINVAL;
5808+
goto out;
5809+
}
5810+
5811+
/* No change? */
5812+
if (percpu_pagelist_fraction == old_percpu_pagelist_fraction)
5813+
goto out;
57985814

5799-
mutex_lock(&pcp_batch_high_lock);
58005815
for_each_populated_zone(zone) {
5801-
unsigned long high;
5802-
high = zone->managed_pages / percpu_pagelist_fraction;
5816+
unsigned int cpu;
5817+
58035818
for_each_possible_cpu(cpu)
5804-
pageset_set_high(per_cpu_ptr(zone->pageset, cpu),
5805-
high);
5819+
pageset_set_high_and_batch(zone,
5820+
per_cpu_ptr(zone->pageset, cpu));
58065821
}
5822+
out:
58075823
mutex_unlock(&pcp_batch_high_lock);
5808-
return 0;
5824+
return ret;
58095825
}
58105826

58115827
int hashdist = HASHDIST_DEFAULT;

0 commit comments

Comments
 (0)