Skip to content

Commit dd1a239

Browse files
gormanmtorvalds
authored andcommitted
mm: have zonelist contains structs with both a zone pointer and zone_idx
Filtering zonelists requires very frequent use of zone_idx(). This is costly as it involves a lookup of another structure and a substraction operation. As the zone_idx is often required, it should be quickly accessible. The node idx could also be stored here if it was found that accessing zone->node is significant which may be the case on workloads where nodemasks are heavily used. This patch introduces a struct zoneref to store a zone pointer and a zone index. The zonelist then consists of an array of these struct zonerefs which are looked up as necessary. Helpers are given for accessing the zone index as well as the node index. [kamezawa.hiroyu@jp.fujitsu.com: Suggested struct zoneref instead of embedding information in pointers] [hugh@veritas.com: mm-have-zonelist: fix memcg ooms] [hugh@veritas.com: just return do_try_to_free_pages] [hugh@veritas.com: do_try_to_free_pages gfp_mask redundant] Signed-off-by: Mel Gorman <mel@csn.ul.ie> Acked-by: Christoph Lameter <clameter@sgi.com> Acked-by: David Rientjes <rientjes@google.com> Signed-off-by: Lee Schermerhorn <lee.schermerhorn@hp.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Mel Gorman <mel@csn.ul.ie> Cc: Christoph Lameter <clameter@sgi.com> Cc: Nick Piggin <nickpiggin@yahoo.com.au> Signed-off-by: Hugh Dickins <hugh@veritas.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 54a6eb5 commit dd1a239

12 files changed

Lines changed: 158 additions & 100 deletions

File tree

‎arch/parisc/mm/init.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ void show_mem(void)
608608
for (i = 0; i < npmem_ranges; i++) {
609609
zl = node_zonelist(i);
610610
for (j = 0; j < MAX_NR_ZONES; j++) {
611-
struct zone **z;
611+
struct zoneref *z;
612612
struct zone *zone;
613613

614614
printk("Zone list for zone %d on node %d: ", j, i);

‎fs/buffer.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,16 @@ void invalidate_bdev(struct block_device *bdev)
360360
*/
361361
static void free_more_memory(void)
362362
{
363-
struct zone **zones;
363+
struct zoneref *zrefs;
364364
int nid;
365365

366366
wakeup_pdflush(1024);
367367
yield();
368368

369369
for_each_online_node(nid) {
370-
zones = first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
370+
zrefs = first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
371371
gfp_zone(GFP_NOFS));
372-
if (*zones)
372+
if (zrefs->zone)
373373
try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
374374
GFP_NOFS);
375375
}

‎include/linux/mmzone.h‎

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,15 @@ struct zonelist_cache {
468468
struct zonelist_cache;
469469
#endif
470470

471+
/*
472+
* This struct contains information about a zone in a zonelist. It is stored
473+
* here to avoid dereferences into large structures and lookups of tables
474+
*/
475+
struct zoneref {
476+
struct zone *zone; /* Pointer to actual zone */
477+
int zone_idx; /* zone_idx(zoneref->zone) */
478+
};
479+
471480
/*
472481
* One allocation request operates on a zonelist. A zonelist
473482
* is a list of zones, the first one is the 'goal' of the
@@ -476,11 +485,18 @@ struct zonelist_cache;
476485
*
477486
* If zlcache_ptr is not NULL, then it is just the address of zlcache,
478487
* as explained above. If zlcache_ptr is NULL, there is no zlcache.
488+
* *
489+
* To speed the reading of the zonelist, the zonerefs contain the zone index
490+
* of the entry being read. Helper functions to access information given
491+
* a struct zoneref are
492+
*
493+
* zonelist_zone() - Return the struct zone * for an entry in _zonerefs
494+
* zonelist_zone_idx() - Return the index of the zone for an entry
495+
* zonelist_node_idx() - Return the index of the node for an entry
479496
*/
480-
481497
struct zonelist {
482498
struct zonelist_cache *zlcache_ptr; // NULL or &zlcache
483-
struct zone *zones[MAX_ZONES_PER_ZONELIST + 1]; // NULL delimited
499+
struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1];
484500
#ifdef CONFIG_NUMA
485501
struct zonelist_cache zlcache; // optional ...
486502
#endif
@@ -713,26 +729,52 @@ extern struct zone *next_zone(struct zone *zone);
713729
zone; \
714730
zone = next_zone(zone))
715731

732+
static inline struct zone *zonelist_zone(struct zoneref *zoneref)
733+
{
734+
return zoneref->zone;
735+
}
736+
737+
static inline int zonelist_zone_idx(struct zoneref *zoneref)
738+
{
739+
return zoneref->zone_idx;
740+
}
741+
742+
static inline int zonelist_node_idx(struct zoneref *zoneref)
743+
{
744+
#ifdef CONFIG_NUMA
745+
/* zone_to_nid not available in this context */
746+
return zoneref->zone->node;
747+
#else
748+
return 0;
749+
#endif /* CONFIG_NUMA */
750+
}
751+
752+
static inline void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
753+
{
754+
zoneref->zone = zone;
755+
zoneref->zone_idx = zone_idx(zone);
756+
}
757+
716758
/* Returns the first zone at or below highest_zoneidx in a zonelist */
717-
static inline struct zone **first_zones_zonelist(struct zonelist *zonelist,
759+
static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
718760
enum zone_type highest_zoneidx)
719761
{
720-
struct zone **z;
762+
struct zoneref *z;
721763

722764
/* Find the first suitable zone to use for the allocation */
723-
z = zonelist->zones;
724-
while (*z && zone_idx(*z) > highest_zoneidx)
765+
z = zonelist->_zonerefs;
766+
while (zonelist_zone_idx(z) > highest_zoneidx)
725767
z++;
726768

727769
return z;
728770
}
729771

730772
/* Returns the next zone at or below highest_zoneidx in a zonelist */
731-
static inline struct zone **next_zones_zonelist(struct zone **z,
773+
static inline struct zoneref *next_zones_zonelist(struct zoneref *z,
732774
enum zone_type highest_zoneidx)
733775
{
734776
/* Find the next suitable zone to use for the allocation */
735-
while (*z && zone_idx(*z) > highest_zoneidx)
777+
while (zonelist_zone_idx(z) > highest_zoneidx)
736778
z++;
737779

738780
return z;
@@ -748,9 +790,11 @@ static inline struct zone **next_zones_zonelist(struct zone **z,
748790
* This iterator iterates though all zones at or below a given zone index.
749791
*/
750792
#define for_each_zone_zonelist(zone, z, zlist, highidx) \
751-
for (z = first_zones_zonelist(zlist, highidx), zone = *z++; \
793+
for (z = first_zones_zonelist(zlist, highidx), \
794+
zone = zonelist_zone(z++); \
752795
zone; \
753-
z = next_zones_zonelist(z, highidx), zone = *z++)
796+
z = next_zones_zonelist(z, highidx), \
797+
zone = zonelist_zone(z++))
754798

755799
#ifdef CONFIG_SPARSEMEM
756800
#include <asm/sparsemem.h>

‎include/linux/oom.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ enum oom_constraint {
2323
CONSTRAINT_MEMORY_POLICY,
2424
};
2525

26-
extern int try_set_zone_oom(struct zonelist *zonelist);
27-
extern void clear_zonelist_oom(struct zonelist *zonelist);
26+
extern int try_set_zone_oom(struct zonelist *zonelist, gfp_t gfp_flags);
27+
extern void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_flags);
2828

2929
extern void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order);
3030
extern int register_oom_notifier(struct notifier_block *nb);

‎kernel/cpuset.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,8 +1967,8 @@ int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
19671967
{
19681968
int i;
19691969

1970-
for (i = 0; zl->zones[i]; i++) {
1971-
int nid = zone_to_nid(zl->zones[i]);
1970+
for (i = 0; zl->_zonerefs[i].zone; i++) {
1971+
int nid = zonelist_node_idx(&zl->_zonerefs[i]);
19721972

19731973
if (node_isset(nid, current->mems_allowed))
19741974
return 1;

‎mm/hugetlb.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
9797
struct mempolicy *mpol;
9898
struct zonelist *zonelist = huge_zonelist(vma, address,
9999
htlb_alloc_mask, &mpol);
100-
struct zone *zone, **z;
100+
struct zone *zone;
101+
struct zoneref *z;
101102

102103
for_each_zone_zonelist(zone, z, zonelist, MAX_NR_ZONES - 1) {
103104
nid = zone_to_nid(zone);

‎mm/mempolicy.c‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static struct zonelist *bind_zonelist(nodemask_t *nodes)
186186
for_each_node_mask(nd, *nodes) {
187187
struct zone *z = &NODE_DATA(nd)->node_zones[k];
188188
if (z->present_pages > 0)
189-
zl->zones[num++] = z;
189+
zoneref_set_zone(z, &zl->_zonerefs[num++]);
190190
}
191191
if (k == 0)
192192
break;
@@ -196,7 +196,8 @@ static struct zonelist *bind_zonelist(nodemask_t *nodes)
196196
kfree(zl);
197197
return ERR_PTR(-EINVAL);
198198
}
199-
zl->zones[num] = NULL;
199+
zl->_zonerefs[num].zone = NULL;
200+
zl->_zonerefs[num].zone_idx = 0;
200201
return zl;
201202
}
202203

@@ -504,9 +505,11 @@ static void get_zonemask(struct mempolicy *p, nodemask_t *nodes)
504505
nodes_clear(*nodes);
505506
switch (p->policy) {
506507
case MPOL_BIND:
507-
for (i = 0; p->v.zonelist->zones[i]; i++)
508-
node_set(zone_to_nid(p->v.zonelist->zones[i]),
509-
*nodes);
508+
for (i = 0; p->v.zonelist->_zonerefs[i].zone; i++) {
509+
struct zoneref *zref;
510+
zref = &p->v.zonelist->_zonerefs[i];
511+
node_set(zonelist_node_idx(zref), *nodes);
512+
}
510513
break;
511514
case MPOL_DEFAULT:
512515
break;
@@ -1212,12 +1215,13 @@ unsigned slab_node(struct mempolicy *policy)
12121215
case MPOL_INTERLEAVE:
12131216
return interleave_nodes(policy);
12141217

1215-
case MPOL_BIND:
1218+
case MPOL_BIND: {
12161219
/*
12171220
* Follow bind policy behavior and start allocation at the
12181221
* first node.
12191222
*/
1220-
return zone_to_nid(policy->v.zonelist->zones[0]);
1223+
return zonelist_node_idx(policy->v.zonelist->_zonerefs);
1224+
}
12211225

12221226
case MPOL_PREFERRED:
12231227
if (policy->v.preferred_node >= 0)
@@ -1323,7 +1327,7 @@ static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
13231327

13241328
zl = node_zonelist(nid, gfp);
13251329
page = __alloc_pages(gfp, order, zl);
1326-
if (page && page_zone(page) == zl->zones[0])
1330+
if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
13271331
inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
13281332
return page;
13291333
}
@@ -1463,10 +1467,14 @@ int __mpol_equal(struct mempolicy *a, struct mempolicy *b)
14631467
return a->v.preferred_node == b->v.preferred_node;
14641468
case MPOL_BIND: {
14651469
int i;
1466-
for (i = 0; a->v.zonelist->zones[i]; i++)
1467-
if (a->v.zonelist->zones[i] != b->v.zonelist->zones[i])
1470+
for (i = 0; a->v.zonelist->_zonerefs[i].zone; i++) {
1471+
struct zone *za, *zb;
1472+
za = zonelist_zone(&a->v.zonelist->_zonerefs[i]);
1473+
zb = zonelist_zone(&b->v.zonelist->_zonerefs[i]);
1474+
if (za != zb)
14681475
return 0;
1469-
return b->v.zonelist->zones[i] == NULL;
1476+
}
1477+
return b->v.zonelist->_zonerefs[i].zone == NULL;
14701478
}
14711479
default:
14721480
BUG();
@@ -1785,12 +1793,12 @@ static void mpol_rebind_policy(struct mempolicy *pol,
17851793
break;
17861794
case MPOL_BIND: {
17871795
nodemask_t nodes;
1788-
struct zone **z;
1796+
struct zoneref *z;
17891797
struct zonelist *zonelist;
17901798

17911799
nodes_clear(nodes);
1792-
for (z = pol->v.zonelist->zones; *z; z++)
1793-
node_set(zone_to_nid(*z), nodes);
1800+
for (z = pol->v.zonelist->_zonerefs; z->zone; z++)
1801+
node_set(zonelist_node_idx(z), nodes);
17941802
nodes_remap(tmp, nodes, *mpolmask, *newmask);
17951803
nodes = tmp;
17961804

‎mm/oom_kill.c‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static inline enum oom_constraint constrained_alloc(struct zonelist *zonelist,
176176
{
177177
#ifdef CONFIG_NUMA
178178
struct zone *zone;
179-
struct zone **z;
179+
struct zoneref *z;
180180
enum zone_type high_zoneidx = gfp_zone(gfp_mask);
181181
nodemask_t nodes = node_states[N_HIGH_MEMORY];
182182

@@ -462,29 +462,29 @@ EXPORT_SYMBOL_GPL(unregister_oom_notifier);
462462
* if a parallel OOM killing is already taking place that includes a zone in
463463
* the zonelist. Otherwise, locks all zones in the zonelist and returns 1.
464464
*/
465-
int try_set_zone_oom(struct zonelist *zonelist)
465+
int try_set_zone_oom(struct zonelist *zonelist, gfp_t gfp_mask)
466466
{
467-
struct zone **z;
467+
struct zoneref *z;
468+
struct zone *zone;
468469
int ret = 1;
469470

470-
z = zonelist->zones;
471-
472471
spin_lock(&zone_scan_mutex);
473-
do {
474-
if (zone_is_oom_locked(*z)) {
472+
for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
473+
if (zone_is_oom_locked(zone)) {
475474
ret = 0;
476475
goto out;
477476
}
478-
} while (*(++z) != NULL);
477+
}
478+
479+
for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
480+
/*
481+
* Lock each zone in the zonelist under zone_scan_mutex so a
482+
* parallel invocation of try_set_zone_oom() doesn't succeed
483+
* when it shouldn't.
484+
*/
485+
zone_set_flag(zone, ZONE_OOM_LOCKED);
486+
}
479487

480-
/*
481-
* Lock each zone in the zonelist under zone_scan_mutex so a parallel
482-
* invocation of try_set_zone_oom() doesn't succeed when it shouldn't.
483-
*/
484-
z = zonelist->zones;
485-
do {
486-
zone_set_flag(*z, ZONE_OOM_LOCKED);
487-
} while (*(++z) != NULL);
488488
out:
489489
spin_unlock(&zone_scan_mutex);
490490
return ret;
@@ -495,16 +495,15 @@ int try_set_zone_oom(struct zonelist *zonelist)
495495
* allocation attempts with zonelists containing them may now recall the OOM
496496
* killer, if necessary.
497497
*/
498-
void clear_zonelist_oom(struct zonelist *zonelist)
498+
void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_mask)
499499
{
500-
struct zone **z;
501-
502-
z = zonelist->zones;
500+
struct zoneref *z;
501+
struct zone *zone;
503502

504503
spin_lock(&zone_scan_mutex);
505-
do {
506-
zone_clear_flag(*z, ZONE_OOM_LOCKED);
507-
} while (*(++z) != NULL);
504+
for_each_zone_zonelist(zone, z, zonelist, gfp_zone(gfp_mask)) {
505+
zone_clear_flag(zone, ZONE_OOM_LOCKED);
506+
}
508507
spin_unlock(&zone_scan_mutex);
509508
}
510509

0 commit comments

Comments
 (0)