Skip to content

Commit 0ff3849

Browse files
Christoph LameterLinus Torvalds
authored andcommitted
[PATCH] zone_reclaim: dynamic slab reclaim
Currently one can enable slab reclaim by setting an explicit option in /proc/sys/vm/zone_reclaim_mode. Slab reclaim is then used as a final option if the freeing of unmapped file backed pages is not enough to free enough pages to allow a local allocation. However, that means that the slab can grow excessively and that most memory of a node may be used by slabs. We have had a case where a machine with 46GB of memory was using 40-42GB for slab. Zone reclaim was effective in dealing with pagecache pages. However, slab reclaim was only done during global reclaim (which is a bit rare on NUMA systems). This patch implements slab reclaim during zone reclaim. Zone reclaim occurs if there is a danger of an off node allocation. At that point we 1. Shrink the per node page cache if the number of pagecache pages is more than min_unmapped_ratio percent of pages in a zone. 2. Shrink the slab cache if the number of the nodes reclaimable slab pages (patch depends on earlier one that implements that counter) are more than min_slab_ratio (a new /proc/sys/vm tunable). The shrinking of the slab cache is a bit problematic since it is not node specific. So we simply calculate what point in the slab we want to reach (current per node slab use minus the number of pages that neeed to be allocated) and then repeately run the global reclaim until that is unsuccessful or we have reached the limit. I hope we will have zone based slab reclaim at some point which will make that easier. The default for the min_slab_ratio is 5% Also remove the slab option from /proc/sys/vm/zone_reclaim_mode. [akpm@osdl.org: cleanups] Signed-off-by: Christoph Lameter <clameter@sgi.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 972d1a7 commit 0ff3849

7 files changed

Lines changed: 90 additions & 28 deletions

File tree

‎Documentation/sysctl/vm.txt‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Currently, these files are in /proc/sys/vm:
2929
- drop-caches
3030
- zone_reclaim_mode
3131
- min_unmapped_ratio
32+
- min_slab_ratio
3233
- panic_on_oom
3334

3435
==============================================================
@@ -138,7 +139,6 @@ This is value ORed together of
138139
1 = Zone reclaim on
139140
2 = Zone reclaim writes dirty pages out
140141
4 = Zone reclaim swaps pages
141-
8 = Also do a global slab reclaim pass
142142

143143
zone_reclaim_mode is set during bootup to 1 if it is determined that pages
144144
from remote zones will cause a measurable performance reduction. The
@@ -162,18 +162,13 @@ Allowing regular swap effectively restricts allocations to the local
162162
node unless explicitly overridden by memory policies or cpuset
163163
configurations.
164164

165-
It may be advisable to allow slab reclaim if the system makes heavy
166-
use of files and builds up large slab caches. However, the slab
167-
shrink operation is global, may take a long time and free slabs
168-
in all nodes of the system.
169-
170165
=============================================================
171166

172167
min_unmapped_ratio:
173168

174169
This is available only on NUMA kernels.
175170

176-
A percentage of the file backed pages in each zone. Zone reclaim will only
171+
A percentage of the total pages in each zone. Zone reclaim will only
177172
occur if more than this percentage of pages are file backed and unmapped.
178173
This is to insure that a minimal amount of local pages is still available for
179174
file I/O even if the node is overallocated.
@@ -182,6 +177,24 @@ The default is 1 percent.
182177

183178
=============================================================
184179

180+
min_slab_ratio:
181+
182+
This is available only on NUMA kernels.
183+
184+
A percentage of the total pages in each zone. On Zone reclaim
185+
(fallback from the local zone occurs) slabs will be reclaimed if more
186+
than this percentage of pages in a zone are reclaimable slab pages.
187+
This insures that the slab growth stays under control even in NUMA
188+
systems that rarely perform global reclaim.
189+
190+
The default is 5 percent.
191+
192+
Note that slab reclaim is triggered in a per zone / node fashion.
193+
The process of reclaiming slab memory is currently not node specific
194+
and may not be fast.
195+
196+
=============================================================
197+
185198
panic_on_oom
186199

187200
This enables or disables panic on out-of-memory feature. If this is set to 1,

‎include/linux/mmzone.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ struct zone {
171171
* zone reclaim becomes active if more unmapped pages exist.
172172
*/
173173
unsigned long min_unmapped_pages;
174+
unsigned long min_slab_pages;
174175
struct per_cpu_pageset *pageset[NR_CPUS];
175176
#else
176177
struct per_cpu_pageset pageset[NR_CPUS];
@@ -448,6 +449,8 @@ int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int, struct file
448449
void __user *, size_t *, loff_t *);
449450
int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *, int,
450451
struct file *, void __user *, size_t *, loff_t *);
452+
int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *, int,
453+
struct file *, void __user *, size_t *, loff_t *);
451454

452455
#include <linux/topology.h>
453456
/* Returns the number of the current Node. */

‎include/linux/swap.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ extern long vm_total_pages;
193193
#ifdef CONFIG_NUMA
194194
extern int zone_reclaim_mode;
195195
extern int sysctl_min_unmapped_ratio;
196+
extern int sysctl_min_slab_ratio;
196197
extern int zone_reclaim(struct zone *, gfp_t, unsigned int);
197198
#else
198199
#define zone_reclaim_mode 0

‎include/linux/sysctl.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ enum
191191
VM_MIN_UNMAPPED=32, /* Set min percent of unmapped pages */
192192
VM_PANIC_ON_OOM=33, /* panic at out-of-memory */
193193
VM_VDSO_ENABLED=34, /* map VDSO into new processes? */
194+
VM_MIN_SLAB=35, /* Percent pages ignored by zone reclaim */
194195
};
195196

196197

‎kernel/sysctl.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,17 @@ static ctl_table vm_table[] = {
943943
.extra1 = &zero,
944944
.extra2 = &one_hundred,
945945
},
946+
{
947+
.ctl_name = VM_MIN_SLAB,
948+
.procname = "min_slab_ratio",
949+
.data = &sysctl_min_slab_ratio,
950+
.maxlen = sizeof(sysctl_min_slab_ratio),
951+
.mode = 0644,
952+
.proc_handler = &sysctl_min_slab_ratio_sysctl_handler,
953+
.strategy = &sysctl_intvec,
954+
.extra1 = &zero,
955+
.extra2 = &one_hundred,
956+
},
946957
#endif
947958
#ifdef CONFIG_X86_32
948959
{

‎mm/page_alloc.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,7 @@ static void __meminit free_area_init_core(struct pglist_data *pgdat,
20052005
#ifdef CONFIG_NUMA
20062006
zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
20072007
/ 100;
2008+
zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
20082009
#endif
20092010
zone->name = zone_names[j];
20102011
spin_lock_init(&zone->lock);
@@ -2318,6 +2319,22 @@ int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
23182319
sysctl_min_unmapped_ratio) / 100;
23192320
return 0;
23202321
}
2322+
2323+
int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
2324+
struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
2325+
{
2326+
struct zone *zone;
2327+
int rc;
2328+
2329+
rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
2330+
if (rc)
2331+
return rc;
2332+
2333+
for_each_zone(zone)
2334+
zone->min_slab_pages = (zone->present_pages *
2335+
sysctl_min_slab_ratio) / 100;
2336+
return 0;
2337+
}
23212338
#endif
23222339

23232340
/*

‎mm/vmscan.c‎

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,6 @@ int zone_reclaim_mode __read_mostly;
15271527
#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
15281528
#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
15291529
#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1530-
#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
15311530

15321531
/*
15331532
* Priority for ZONE_RECLAIM. This determines the fraction of pages
@@ -1542,6 +1541,12 @@ int zone_reclaim_mode __read_mostly;
15421541
*/
15431542
int sysctl_min_unmapped_ratio = 1;
15441543

1544+
/*
1545+
* If the number of slab pages in a zone grows beyond this percentage then
1546+
* slab reclaim needs to occur.
1547+
*/
1548+
int sysctl_min_slab_ratio = 5;
1549+
15451550
/*
15461551
* Try to free up some pages from this zone through reclaim.
15471552
*/
@@ -1573,29 +1578,37 @@ static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
15731578
reclaim_state.reclaimed_slab = 0;
15741579
p->reclaim_state = &reclaim_state;
15751580

1576-
/*
1577-
* Free memory by calling shrink zone with increasing priorities
1578-
* until we have enough memory freed.
1579-
*/
1580-
priority = ZONE_RECLAIM_PRIORITY;
1581-
do {
1582-
nr_reclaimed += shrink_zone(priority, zone, &sc);
1583-
priority--;
1584-
} while (priority >= 0 && nr_reclaimed < nr_pages);
1581+
if (zone_page_state(zone, NR_FILE_PAGES) -
1582+
zone_page_state(zone, NR_FILE_MAPPED) >
1583+
zone->min_unmapped_pages) {
1584+
/*
1585+
* Free memory by calling shrink zone with increasing
1586+
* priorities until we have enough memory freed.
1587+
*/
1588+
priority = ZONE_RECLAIM_PRIORITY;
1589+
do {
1590+
nr_reclaimed += shrink_zone(priority, zone, &sc);
1591+
priority--;
1592+
} while (priority >= 0 && nr_reclaimed < nr_pages);
1593+
}
15851594

1586-
if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
1595+
if (zone_page_state(zone, NR_SLAB_RECLAIMABLE) > zone->min_slab_pages) {
15871596
/*
15881597
* shrink_slab() does not currently allow us to determine how
1589-
* many pages were freed in this zone. So we just shake the slab
1590-
* a bit and then go off node for this particular allocation
1591-
* despite possibly having freed enough memory to allocate in
1592-
* this zone. If we freed local memory then the next
1593-
* allocations will be local again.
1598+
* many pages were freed in this zone. So we take the current
1599+
* number of slab pages and shake the slab until it is reduced
1600+
* by the same nr_pages that we used for reclaiming unmapped
1601+
* pages.
15941602
*
1595-
* shrink_slab will free memory on all zones and may take
1596-
* a long time.
1603+
* Note that shrink_slab will free memory on all zones and may
1604+
* take a long time.
15971605
*/
1598-
shrink_slab(sc.nr_scanned, gfp_mask, order);
1606+
unsigned long limit = zone_page_state(zone,
1607+
NR_SLAB_RECLAIMABLE) - nr_pages;
1608+
1609+
while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
1610+
zone_page_state(zone, NR_SLAB_RECLAIMABLE) > limit)
1611+
;
15991612
}
16001613

16011614
p->reclaim_state = NULL;
@@ -1609,7 +1622,8 @@ int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
16091622
int node_id;
16101623

16111624
/*
1612-
* Zone reclaim reclaims unmapped file backed pages.
1625+
* Zone reclaim reclaims unmapped file backed pages and
1626+
* slab pages if we are over the defined limits.
16131627
*
16141628
* A small portion of unmapped file backed pages is needed for
16151629
* file I/O otherwise pages read by file I/O will be immediately
@@ -1618,7 +1632,9 @@ int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
16181632
* unmapped file backed pages.
16191633
*/
16201634
if (zone_page_state(zone, NR_FILE_PAGES) -
1621-
zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages)
1635+
zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
1636+
&& zone_page_state(zone, NR_SLAB_RECLAIMABLE)
1637+
<= zone->min_slab_pages)
16221638
return 0;
16231639

16241640
/*

0 commit comments

Comments
 (0)