Skip to content

Commit d26914d

Browse files
Mel Gormantorvalds
authored andcommitted
mm: optimize put_mems_allowed() usage
Since put_mems_allowed() is strictly optional, its a seqcount retry, we don't need to evaluate the function if the allocation was in fact successful, saving a smp_rmb some loads and comparisons on some relative fast-paths. Since the naming, get/put_mems_allowed() does suggest a mandatory pairing, rename the interface, as suggested by Mel, to resemble the seqcount interface. This gives us: read_mems_allowed_begin() and read_mems_allowed_retry(), where it is important to note that the return value of the latter call is inverted from its previous incarnation. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Signed-off-by: Mel Gorman <mgorman@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 91ca918 commit d26914d

8 files changed

Lines changed: 38 additions & 39 deletions

File tree

‎include/linux/cpuset.h‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,26 @@ extern void rebuild_sched_domains(void);
8787
extern void cpuset_print_task_mems_allowed(struct task_struct *p);
8888

8989
/*
90-
* get_mems_allowed is required when making decisions involving mems_allowed
91-
* such as during page allocation. mems_allowed can be updated in parallel
92-
* and depending on the new value an operation can fail potentially causing
93-
* process failure. A retry loop with get_mems_allowed and put_mems_allowed
94-
* prevents these artificial failures.
90+
* read_mems_allowed_begin is required when making decisions involving
91+
* mems_allowed such as during page allocation. mems_allowed can be updated in
92+
* parallel and depending on the new value an operation can fail potentially
93+
* causing process failure. A retry loop with read_mems_allowed_begin and
94+
* read_mems_allowed_retry prevents these artificial failures.
9595
*/
96-
static inline unsigned int get_mems_allowed(void)
96+
static inline unsigned int read_mems_allowed_begin(void)
9797
{
9898
return read_seqcount_begin(&current->mems_allowed_seq);
9999
}
100100

101101
/*
102-
* If this returns false, the operation that took place after get_mems_allowed
103-
* may have failed. It is up to the caller to retry the operation if
102+
* If this returns true, the operation that took place after
103+
* read_mems_allowed_begin may have failed artificially due to a concurrent
104+
* update of mems_allowed. It is up to the caller to retry the operation if
104105
* appropriate.
105106
*/
106-
static inline bool put_mems_allowed(unsigned int seq)
107+
static inline bool read_mems_allowed_retry(unsigned int seq)
107108
{
108-
return !read_seqcount_retry(&current->mems_allowed_seq, seq);
109+
return read_seqcount_retry(&current->mems_allowed_seq, seq);
109110
}
110111

111112
static inline void set_mems_allowed(nodemask_t nodemask)
@@ -225,14 +226,14 @@ static inline void set_mems_allowed(nodemask_t nodemask)
225226
{
226227
}
227228

228-
static inline unsigned int get_mems_allowed(void)
229+
static inline unsigned int read_mems_allowed_begin(void)
229230
{
230231
return 0;
231232
}
232233

233-
static inline bool put_mems_allowed(unsigned int seq)
234+
static inline bool read_mems_allowed_retry(unsigned int seq)
234235
{
235-
return true;
236+
return false;
236237
}
237238

238239
#endif /* !CONFIG_CPUSETS */

‎kernel/cpuset.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ static void cpuset_change_task_nodemask(struct task_struct *tsk,
10221022
task_lock(tsk);
10231023
/*
10241024
* Determine if a loop is necessary if another thread is doing
1025-
* get_mems_allowed(). If at least one node remains unchanged and
1025+
* read_mems_allowed_begin(). If at least one node remains unchanged and
10261026
* tsk does not have a mempolicy, then an empty nodemask will not be
10271027
* possible when mems_allowed is larger than a word.
10281028
*/

‎mm/filemap.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,10 @@ struct page *__page_cache_alloc(gfp_t gfp)
520520
if (cpuset_do_page_mem_spread()) {
521521
unsigned int cpuset_mems_cookie;
522522
do {
523-
cpuset_mems_cookie = get_mems_allowed();
523+
cpuset_mems_cookie = read_mems_allowed_begin();
524524
n = cpuset_mem_spread_node();
525525
page = alloc_pages_exact_node(n, gfp, 0);
526-
} while (!put_mems_allowed(cpuset_mems_cookie) && !page);
526+
} while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
527527

528528
return page;
529529
}

‎mm/hugetlb.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ static struct page *dequeue_huge_page_vma(struct hstate *h,
540540
goto err;
541541

542542
retry_cpuset:
543-
cpuset_mems_cookie = get_mems_allowed();
543+
cpuset_mems_cookie = read_mems_allowed_begin();
544544
zonelist = huge_zonelist(vma, address,
545545
htlb_alloc_mask(h), &mpol, &nodemask);
546546

@@ -562,7 +562,7 @@ static struct page *dequeue_huge_page_vma(struct hstate *h,
562562
}
563563

564564
mpol_cond_put(mpol);
565-
if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
565+
if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
566566
goto retry_cpuset;
567567
return page;
568568

‎mm/mempolicy.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ int node_random(const nodemask_t *maskp)
18991899
* If the effective policy is 'BIND, returns a pointer to the mempolicy's
19001900
* @nodemask for filtering the zonelist.
19011901
*
1902-
* Must be protected by get_mems_allowed()
1902+
* Must be protected by read_mems_allowed_begin()
19031903
*/
19041904
struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
19051905
gfp_t gfp_flags, struct mempolicy **mpol,
@@ -2063,15 +2063,15 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
20632063

20642064
retry_cpuset:
20652065
pol = get_vma_policy(current, vma, addr);
2066-
cpuset_mems_cookie = get_mems_allowed();
2066+
cpuset_mems_cookie = read_mems_allowed_begin();
20672067

20682068
if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
20692069
unsigned nid;
20702070

20712071
nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
20722072
mpol_cond_put(pol);
20732073
page = alloc_page_interleave(gfp, order, nid);
2074-
if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2074+
if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
20752075
goto retry_cpuset;
20762076

20772077
return page;
@@ -2081,7 +2081,7 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
20812081
policy_nodemask(gfp, pol));
20822082
if (unlikely(mpol_needs_cond_ref(pol)))
20832083
__mpol_put(pol);
2084-
if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2084+
if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
20852085
goto retry_cpuset;
20862086
return page;
20872087
}
@@ -2115,7 +2115,7 @@ struct page *alloc_pages_current(gfp_t gfp, unsigned order)
21152115
pol = &default_policy;
21162116

21172117
retry_cpuset:
2118-
cpuset_mems_cookie = get_mems_allowed();
2118+
cpuset_mems_cookie = read_mems_allowed_begin();
21192119

21202120
/*
21212121
* No reference counting needed for current->mempolicy
@@ -2128,7 +2128,7 @@ struct page *alloc_pages_current(gfp_t gfp, unsigned order)
21282128
policy_zonelist(gfp, pol, numa_node_id()),
21292129
policy_nodemask(gfp, pol));
21302130

2131-
if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2131+
if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
21322132
goto retry_cpuset;
21332133

21342134
return page;

‎mm/page_alloc.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
27392739
return NULL;
27402740

27412741
retry_cpuset:
2742-
cpuset_mems_cookie = get_mems_allowed();
2742+
cpuset_mems_cookie = read_mems_allowed_begin();
27432743

27442744
/* The preferred zone is used for statistics later */
27452745
first_zones_zonelist(zonelist, high_zoneidx,
@@ -2777,7 +2777,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
27772777
* the mask is being updated. If a page allocation is about to fail,
27782778
* check if the cpuset changed during allocation and if so, retry.
27792779
*/
2780-
if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2780+
if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
27812781
goto retry_cpuset;
27822782

27832783
memcg_kmem_commit_charge(page, memcg, order);
@@ -3045,9 +3045,9 @@ bool skip_free_areas_node(unsigned int flags, int nid)
30453045
goto out;
30463046

30473047
do {
3048-
cpuset_mems_cookie = get_mems_allowed();
3048+
cpuset_mems_cookie = read_mems_allowed_begin();
30493049
ret = !node_isset(nid, cpuset_current_mems_allowed);
3050-
} while (!put_mems_allowed(cpuset_mems_cookie));
3050+
} while (read_mems_allowed_retry(cpuset_mems_cookie));
30513051
out:
30523052
return ret;
30533053
}

‎mm/slab.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,7 +3073,7 @@ static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
30733073
local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
30743074

30753075
retry_cpuset:
3076-
cpuset_mems_cookie = get_mems_allowed();
3076+
cpuset_mems_cookie = read_mems_allowed_begin();
30773077
zonelist = node_zonelist(slab_node(), flags);
30783078

30793079
retry:
@@ -3131,7 +3131,7 @@ static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
31313131
}
31323132
}
31333133

3134-
if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3134+
if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie)))
31353135
goto retry_cpuset;
31363136
return obj;
31373137
}

‎mm/slub.c‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
16841684
return NULL;
16851685

16861686
do {
1687-
cpuset_mems_cookie = get_mems_allowed();
1687+
cpuset_mems_cookie = read_mems_allowed_begin();
16881688
zonelist = node_zonelist(slab_node(), flags);
16891689
for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
16901690
struct kmem_cache_node *n;
@@ -1696,19 +1696,17 @@ static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
16961696
object = get_partial_node(s, n, c, flags);
16971697
if (object) {
16981698
/*
1699-
* Return the object even if
1700-
* put_mems_allowed indicated that
1701-
* the cpuset mems_allowed was
1702-
* updated in parallel. It's a
1703-
* harmless race between the alloc
1704-
* and the cpuset update.
1699+
* Don't check read_mems_allowed_retry()
1700+
* here - if mems_allowed was updated in
1701+
* parallel, that was a harmless race
1702+
* between allocation and the cpuset
1703+
* update
17051704
*/
1706-
put_mems_allowed(cpuset_mems_cookie);
17071705
return object;
17081706
}
17091707
}
17101708
}
1711-
} while (!put_mems_allowed(cpuset_mems_cookie));
1709+
} while (read_mems_allowed_retry(cpuset_mems_cookie));
17121710
#endif
17131711
return NULL;
17141712
}

0 commit comments

Comments
 (0)