Skip to content

Commit 19770b3

Browse files
gormanmtorvalds
authored andcommitted
mm: filter based on a nodemask as well as a gfp_mask
The MPOL_BIND policy creates a zonelist that is used for allocations controlled by that mempolicy. As the per-node zonelist is already being filtered based on a zone id, this patch adds a version of __alloc_pages() that takes a nodemask for further filtering. This eliminates the need for MPOL_BIND to create a custom zonelist. A positive benefit of this is that allocations using MPOL_BIND now use the local node's distance-ordered zonelist instead of a custom node-id-ordered zonelist. I.e., pages will be allocated from the closest allowed node with available memory. [Lee.Schermerhorn@hp.com: Mempolicy: update stale documentation and comments] [Lee.Schermerhorn@hp.com: Mempolicy: make dequeue_huge_page_vma() obey MPOL_BIND nodemask] [Lee.Schermerhorn@hp.com: Mempolicy: make dequeue_huge_page_vma() obey MPOL_BIND nodemask rework] Signed-off-by: Mel Gorman <mel@csn.ul.ie> Acked-by: Christoph Lameter <clameter@sgi.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: Hugh Dickins <hugh@veritas.com> Cc: Nick Piggin <nickpiggin@yahoo.com.au> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent dd1a239 commit 19770b3

11 files changed

Lines changed: 224 additions & 191 deletions

File tree

‎Documentation/vm/numa_memory_policy.txt‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,9 @@ Components of Memory Policies
182182
The Default mode does not use the optional set of nodes.
183183

184184
MPOL_BIND: This mode specifies that memory must come from the
185-
set of nodes specified by the policy.
186-
187-
The memory policy APIs do not specify an order in which the nodes
188-
will be searched. However, unlike "local allocation", the Bind
189-
policy does not consider the distance between the nodes. Rather,
190-
allocations will fallback to the nodes specified by the policy in
191-
order of numeric node id. Like everything in Linux, this is subject
192-
to change.
185+
set of nodes specified by the policy. Memory will be allocated from
186+
the node in the set with sufficient free memory that is closest to
187+
the node where the allocation takes place.
193188

194189
MPOL_PREFERRED: This mode specifies that the allocation should be
195190
attempted from the single node specified in the policy. If that

‎fs/buffer.c‎

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

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

369369
for_each_online_node(nid) {
370-
zrefs = first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
371-
gfp_zone(GFP_NOFS));
372-
if (zrefs->zone)
370+
(void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
371+
gfp_zone(GFP_NOFS), NULL,
372+
&zone);
373+
if (zone)
373374
try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
374375
GFP_NOFS);
375376
}

‎include/linux/cpuset.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern nodemask_t cpuset_mems_allowed(struct task_struct *p);
2626
#define cpuset_current_mems_allowed (current->mems_allowed)
2727
void cpuset_init_current_mems_allowed(void);
2828
void cpuset_update_task_memory_state(void);
29-
int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl);
29+
int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask);
3030

3131
extern int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask);
3232
extern int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask);
@@ -103,7 +103,7 @@ static inline nodemask_t cpuset_mems_allowed(struct task_struct *p)
103103
static inline void cpuset_init_current_mems_allowed(void) {}
104104
static inline void cpuset_update_task_memory_state(void) {}
105105

106-
static inline int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
106+
static inline int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
107107
{
108108
return 1;
109109
}

‎include/linux/gfp.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ static inline void arch_alloc_page(struct page *page, int order) { }
182182

183183
extern struct page *__alloc_pages(gfp_t, unsigned int, struct zonelist *);
184184

185+
extern struct page *
186+
__alloc_pages_nodemask(gfp_t, unsigned int,
187+
struct zonelist *, nodemask_t *nodemask);
188+
185189
static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,
186190
unsigned int order)
187191
{

‎include/linux/mempolicy.h‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,20 @@ struct mm_struct;
5454
* mmap_sem.
5555
*
5656
* Freeing policy:
57-
* When policy is MPOL_BIND v.zonelist is kmalloc'ed and must be kfree'd.
58-
* All other policies don't have any external state. mpol_free() handles this.
57+
* Mempolicy objects are reference counted. A mempolicy will be freed when
58+
* mpol_free() decrements the reference count to zero.
5959
*
6060
* Copying policy objects:
61-
* For MPOL_BIND the zonelist must be always duplicated. mpol_clone() does this.
61+
* mpol_copy() allocates a new mempolicy and copies the specified mempolicy
62+
* to the new storage. The reference count of the new object is initialized
63+
* to 1, representing the caller of mpol_copy().
6264
*/
6365
struct mempolicy {
6466
atomic_t refcnt;
6567
short policy; /* See MPOL_* above */
6668
union {
67-
struct zonelist *zonelist; /* bind */
6869
short preferred_node; /* preferred */
69-
nodemask_t nodes; /* interleave */
70+
nodemask_t nodes; /* interleave/bind */
7071
/* undefined for default */
7172
} v;
7273
nodemask_t cpuset_mems_allowed; /* mempolicy relative to these nodes */
@@ -151,7 +152,8 @@ extern void mpol_fix_fork_child_flag(struct task_struct *p);
151152

152153
extern struct mempolicy default_policy;
153154
extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
154-
unsigned long addr, gfp_t gfp_flags, struct mempolicy **mpol);
155+
unsigned long addr, gfp_t gfp_flags,
156+
struct mempolicy **mpol, nodemask_t **nodemask);
155157
extern unsigned slab_node(struct mempolicy *policy);
156158

157159
extern enum zone_type policy_zone;
@@ -239,8 +241,11 @@ static inline void mpol_fix_fork_child_flag(struct task_struct *p)
239241
}
240242

241243
static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
242-
unsigned long addr, gfp_t gfp_flags, struct mempolicy **mpol)
244+
unsigned long addr, gfp_t gfp_flags,
245+
struct mempolicy **mpol, nodemask_t **nodemask)
243246
{
247+
*mpol = NULL;
248+
*nodemask = NULL;
244249
return node_zonelist(0, gfp_flags);
245250
}
246251

‎include/linux/mmzone.h‎

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -749,36 +749,60 @@ static inline int zonelist_node_idx(struct zoneref *zoneref)
749749
#endif /* CONFIG_NUMA */
750750
}
751751

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-
}
752+
/**
753+
* next_zones_zonelist - Returns the next zone at or below highest_zoneidx within the allowed nodemask using a cursor within a zonelist as a starting point
754+
* @z - The cursor used as a starting point for the search
755+
* @highest_zoneidx - The zone index of the highest zone to return
756+
* @nodes - An optional nodemask to filter the zonelist with
757+
* @zone - The first suitable zone found is returned via this parameter
758+
*
759+
* This function returns the next zone at or below a given zone index that is
760+
* within the allowed nodemask using a cursor as the starting point for the
761+
* search. The zoneref returned is a cursor that is used as the next starting
762+
* point for future calls to next_zones_zonelist().
763+
*/
764+
struct zoneref *next_zones_zonelist(struct zoneref *z,
765+
enum zone_type highest_zoneidx,
766+
nodemask_t *nodes,
767+
struct zone **zone);
757768

758-
/* Returns the first zone at or below highest_zoneidx in a zonelist */
769+
/**
770+
* first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist
771+
* @zonelist - The zonelist to search for a suitable zone
772+
* @highest_zoneidx - The zone index of the highest zone to return
773+
* @nodes - An optional nodemask to filter the zonelist with
774+
* @zone - The first suitable zone found is returned via this parameter
775+
*
776+
* This function returns the first zone at or below a given zone index that is
777+
* within the allowed nodemask. The zoneref returned is a cursor that can be
778+
* used to iterate the zonelist with next_zones_zonelist. The cursor should
779+
* not be used by the caller as it does not match the value of the zone
780+
* returned.
781+
*/
759782
static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
760-
enum zone_type highest_zoneidx)
783+
enum zone_type highest_zoneidx,
784+
nodemask_t *nodes,
785+
struct zone **zone)
761786
{
762-
struct zoneref *z;
763-
764-
/* Find the first suitable zone to use for the allocation */
765-
z = zonelist->_zonerefs;
766-
while (zonelist_zone_idx(z) > highest_zoneidx)
767-
z++;
768-
769-
return z;
787+
return next_zones_zonelist(zonelist->_zonerefs, highest_zoneidx, nodes,
788+
zone);
770789
}
771790

772-
/* Returns the next zone at or below highest_zoneidx in a zonelist */
773-
static inline struct zoneref *next_zones_zonelist(struct zoneref *z,
774-
enum zone_type highest_zoneidx)
775-
{
776-
/* Find the next suitable zone to use for the allocation */
777-
while (zonelist_zone_idx(z) > highest_zoneidx)
778-
z++;
779-
780-
return z;
781-
}
791+
/**
792+
* for_each_zone_zonelist_nodemask - helper macro to iterate over valid zones in a zonelist at or below a given zone index and within a nodemask
793+
* @zone - The current zone in the iterator
794+
* @z - The current pointer within zonelist->zones being iterated
795+
* @zlist - The zonelist being iterated
796+
* @highidx - The zone index of the highest zone to return
797+
* @nodemask - Nodemask allowed by the allocator
798+
*
799+
* This iterator iterates though all zones at or below a given zone index and
800+
* within a given nodemask
801+
*/
802+
#define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
803+
for (z = first_zones_zonelist(zlist, highidx, nodemask, &zone); \
804+
zone; \
805+
z = next_zones_zonelist(z, highidx, nodemask, &zone)) \
782806

783807
/**
784808
* for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
@@ -790,11 +814,7 @@ static inline struct zoneref *next_zones_zonelist(struct zoneref *z,
790814
* This iterator iterates though all zones at or below a given zone index.
791815
*/
792816
#define for_each_zone_zonelist(zone, z, zlist, highidx) \
793-
for (z = first_zones_zonelist(zlist, highidx), \
794-
zone = zonelist_zone(z++); \
795-
zone; \
796-
z = next_zones_zonelist(z, highidx), \
797-
zone = zonelist_zone(z++))
817+
for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
798818

799819
#ifdef CONFIG_SPARSEMEM
800820
#include <asm/sparsemem.h>

‎kernel/cpuset.c‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,22 +1958,14 @@ nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
19581958
}
19591959

19601960
/**
1961-
* cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
1962-
* @zl: the zonelist to be checked
1961+
* cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
1962+
* @nodemask: the nodemask to be checked
19631963
*
1964-
* Are any of the nodes on zonelist zl allowed in current->mems_allowed?
1964+
* Are any of the nodes in the nodemask allowed in current->mems_allowed?
19651965
*/
1966-
int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
1966+
int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
19671967
{
1968-
int i;
1969-
1970-
for (i = 0; zl->_zonerefs[i].zone; i++) {
1971-
int nid = zonelist_node_idx(&zl->_zonerefs[i]);
1972-
1973-
if (node_isset(nid, current->mems_allowed))
1974-
return 1;
1975-
}
1976-
return 0;
1968+
return nodes_intersects(*nodemask, current->mems_allowed);
19771969
}
19781970

19791971
/*

‎mm/hugetlb.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,14 @@ static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
9595
int nid;
9696
struct page *page = NULL;
9797
struct mempolicy *mpol;
98+
nodemask_t *nodemask;
9899
struct zonelist *zonelist = huge_zonelist(vma, address,
99-
htlb_alloc_mask, &mpol);
100+
htlb_alloc_mask, &mpol, &nodemask);
100101
struct zone *zone;
101102
struct zoneref *z;
102103

103-
for_each_zone_zonelist(zone, z, zonelist, MAX_NR_ZONES - 1) {
104+
for_each_zone_zonelist_nodemask(zone, z, zonelist,
105+
MAX_NR_ZONES - 1, nodemask) {
104106
nid = zone_to_nid(zone);
105107
if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
106108
!list_empty(&hugepage_freelists[nid])) {

0 commit comments

Comments
 (0)