[PATCH 1/2] mm/mempolicy: use SRCU for the weighted interleave state
From: Gregory Price
Date: Fri Aug 28 2026 - 22:00:04 EST
alloc_pages_bulk_weighted_interleave() copies iw_table into a scratch
array on every call so it can walk the weights outside of RCU. The copy
exists only because the loop may sleep in the page allocator and so cannot
hold rcu_read_lock().
Use SRCU to pin the global iw_table object and use it in-place instead.
Retire through both flavors - call_srcu() for the sleeping readers, then
kfree_rcu() for the reference-less ones - so writers no longer block on
synchronize_rcu() either.
Tested in a VM with KASAN, PROVE_LOCKING and DEBUG_OBJECTS_RCU_HEAD,
with a udelay() injected into the read section to widen the race against
concurrent sysfs weight writers, and placement checked against the
configured weights.
Every retired state reached its callback. Swapping the deferred free for
a bare kfree() in the same test reports a use-after-free immediately.
Suggested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Suggested-by: Matthew Wilcox <willy@xxxxxxxxxxxxx>
Assisted-by: Claude:claude-opus-5
Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
mm/mempolicy.c | 70 ++++++++++++++++++++++++--------------------------
1 file changed, 34 insertions(+), 36 deletions(-)
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 060a0eb26917..2643915dc966 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -112,6 +112,7 @@
#include <linux/printk.h>
#include <linux/leafops.h>
#include <linux/gcd.h>
+#include <linux/srcu.h>
#include <asm/tlbflush.h>
#include <asm/tlb.h>
@@ -157,6 +158,7 @@ static const int weightiness = 32;
*/
struct weighted_interleave_state {
bool mode_auto;
+ struct rcu_head rcu;
u8 iw_table[];
};
static struct weighted_interleave_state __rcu *wi_state;
@@ -168,6 +170,24 @@ static unsigned int *node_bw_table;
*/
static DEFINE_MUTEX(wi_state_lock);
+/* Readers that sleep while walking iw_table hold this instead */
+DEFINE_STATIC_SRCU_FAST(wi_srcu);
+
+static void wi_state_free_rcu(struct rcu_head *head)
+{
+ struct weighted_interleave_state *state =
+ container_of(head, struct weighted_interleave_state, rcu);
+
+ kfree_rcu(state, rcu);
+}
+
+/* Retire through both flavors: sleeping readers use SRCU, the rest RCU */
+static void wi_state_retire(struct weighted_interleave_state *state)
+{
+ if (state)
+ call_srcu(&wi_srcu, &state->rcu, wi_state_free_rcu);
+}
+
static u8 get_il_weight(int node)
{
struct weighted_interleave_state *state;
@@ -266,10 +286,7 @@ int mempolicy_set_node_perf(unsigned int node, struct access_coordinate *coords)
rcu_assign_pointer(wi_state, new_wi_state);
mutex_unlock(&wi_state_lock);
- if (old_wi_state) {
- synchronize_rcu();
- kfree(old_wi_state);
- }
+ wi_state_retire(old_wi_state);
out:
kfree(old_bw);
return 0;
@@ -2644,7 +2661,8 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
unsigned long nr_allocated = 0;
unsigned long rounds;
unsigned long node_pages, delta;
- u8 *weights, weight;
+ struct srcu_ctr __percpu *scp;
+ u8 *table, weight;
unsigned int weight_total = 0;
unsigned long rem_pages = nr_pages;
nodemask_t nodes;
@@ -2688,25 +2706,14 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
me->il_weight = 0;
prev_node = node;
- /* create a local copy of node weights to operate on outside rcu */
- weights = kmalloc(nr_node_ids, gfp & GFP_RECLAIM_MASK);
- if (!weights)
- return total_allocated;
-
- rcu_read_lock();
- state = rcu_dereference(wi_state);
- if (state) {
- memcpy(weights, state->iw_table, nr_node_ids * sizeof(u8));
- rcu_read_unlock();
- } else {
- rcu_read_unlock();
- for (i = 0; i < nr_node_ids; i++)
- weights[i] = 1;
- }
+ /* The page allocator may sleep, pin the weight table with SRCU */
+ scp = srcu_read_lock_fast(&wi_srcu);
+ state = srcu_dereference(wi_state, &wi_srcu);
+ table = state ? state->iw_table : NULL;
/* calculate total, detect system default usage */
for_each_node_mask(node, nodes)
- weight_total += weights[node];
+ weight_total += table ? table[node] : 1;
/*
* Calculate rounds/partial rounds to minimize __alloc_pages_bulk calls.
@@ -2718,10 +2725,10 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
rounds = rem_pages / weight_total;
delta = rem_pages % weight_total;
resume_node = next_node_in(prev_node, nodes);
- resume_weight = weights[resume_node];
+ resume_weight = table ? table[resume_node] : 1;
for (i = 0; i < nnodes; i++) {
node = next_node_in(prev_node, nodes);
- weight = weights[node];
+ weight = table ? table[node] : 1;
node_pages = weight * rounds;
/* If a delta exists, add this node's portion of the delta */
if (delta > weight) {
@@ -2747,7 +2754,7 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
}
me->il_prev = resume_node;
me->il_weight = resume_weight;
- kfree(weights);
+ srcu_read_unlock_fast(&wi_srcu, scp);
return total_allocated;
}
@@ -3673,10 +3680,7 @@ static ssize_t node_store(struct kobject *kobj, struct kobj_attribute *attr,
rcu_assign_pointer(wi_state, new_wi_state);
mutex_unlock(&wi_state_lock);
- if (old_wi_state) {
- synchronize_rcu();
- kfree(old_wi_state);
- }
+ wi_state_retire(old_wi_state);
return count;
}
@@ -3742,10 +3746,7 @@ static ssize_t weighted_interleave_auto_store(struct kobject *kobj,
update_wi_state:
rcu_assign_pointer(wi_state, new_wi_state);
mutex_unlock(&wi_state_lock);
- if (old_wi_state) {
- synchronize_rcu();
- kfree(old_wi_state);
- }
+ wi_state_retire(old_wi_state);
return count;
}
@@ -3789,10 +3790,7 @@ static void wi_state_free(void)
rcu_assign_pointer(wi_state, NULL);
mutex_unlock(&wi_state_lock);
- if (old_wi_state) {
- synchronize_rcu();
- kfree(old_wi_state);
- }
+ wi_state_retire(old_wi_state);
}
static struct kobj_attribute wi_auto_attr = {
--
2.55.0