From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.112 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793708967; b=CMJV5VxyfBPCGTh5D6HfIlZTZWkR8+XpCCGh2VWqX5HTbYNEq5vYc9E/K/NNY+R1TH0a RHuC4MSZ0/9CHNjcLNGKVcP1i7HrY87yM1nbJTA4Nd4Afxgo16u21l3efUKsh1NpTvDVX aqcsVo6ArWOFWIN7m0VAKQiQ6shABAd4MsnE08Qd/vpOnzxpOvTtsCXjKWDdXEGGGFdeK x2joIXIFawEeXC0YKgK0bJktPoBfTzTCl5xPmhND+D9xJKdHRhUQcUFeIlPbUYuG0vqbY vv9m7tdwZ0Nqxiz20RuBdI6jMF1FS6bbOAt/33YnfNBnpTPwlDNxNutsCS12osZQ5pA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793708967; h=DMARC-Filter:DKIM-Signature:Subject:Message-Id:Cc:In-Reply-To: References:Content-Type:Mime-Version:X-Original-From:Date:X-Mailer: Content-Transfer-Encoding:X-Lms-Return-Path:To:From; bh=FP7kG4/RawLD0z06EWd0ekkozMQmyFz/TouOQGf11tc=; b=MbdXT1lCFHsNIe60MNsgswRyWoi1+liWbRgn68v5YMAt0P4Pd5tnLQzx6YRwsGY7U4cS KcJsLoZUFJmsmfaRVMM+QQTMQ64tBe+NcEkWHg90w5h1OCYWQrCOQG9y1OLH6ncRISNwu WVVTB6whtv0WFJAKQ60Ldc2sTGLqH7ryMA2lpBxcbY2YkRPexemMfqOSctrmojqRTcaON RJN1zyqQi7c2Dmo6ezv3IopLAhI4is5pEdUvXU/Y6EsegLE2GUIoipIyg5oTw4SyfjHp2 mveFP9M8upnL/l7TGCruLS4Pk+SAD7H5Q0H0koFFfIuZoabzTdme9CCY5X43EXEqM1A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=AIzGQocF; arc=none smtp.remote-ip=209.127.231.112 Received: from va-2-112.ptr.blmpb.com (va-2-112.ptr.blmpb.com [209.127.231.112]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E69DB606D815 for ; Tue, 3 Nov 2026 12:29:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E69DB606D815 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=AIzGQocF DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1793708961; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=FP7kG4/RawLD0z06EWd0ekkozMQmyFz/TouOQGf11tc=; b=AIzGQocFfgWdNoTakhQX2iAHKob9UPvw3QMcbq68VyAdWi6E25Tmf6ynmTo3xB6pAmH/25 rtNA71csOKYn3tQRJ7uAolU5wnZ3/U1LlUyFZt24qucmB0OvVG+MPYdOKnHUTE2xGmpmnt 5ZcMbwudaRME8thCbSHFkx5XiihwWDK5YUQokCGyGyzFE8kdGJrH+T99OpPk6lhXYGAqUX Dj+jd/Ki7nUasP9ODN5pODRvZmlA1jxm3kwVaQ7uvE/6BcVZaw8GxGJtCZO8zk/YBYyURx NQ1zsONrrDFO0wbP5aXQP9tFAgI5g2jtCpXGI+ErBsCOo5sCSZt1KroD6xRrpw== Subject: [PATCH 1/3] mm/mempolicy: factor weighted interleave state helpers Message-Id: <20261103122816.48037-2-lizhe.67@bytedance.com> Cc: , , , , "Li Zhe" In-Reply-To: <20261103122816.48037-1-lizhe.67@bytedance.com> References: <20261103122816.48037-1-lizhe.67@bytedance.com> Content-Type: text/plain; charset=UTF-8 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Original-From: Li Zhe Date: Tue, 3 Nov 2026 20:28:14 +0800 X-Mailer: git-send-email 2.45.2 Content-Transfer-Encoding: 7bit X-Lms-Return-Path: To: , , , , , , , , , , , , , From: "Li Zhe" Weighted interleave updates repeat the same allocation, default table initialization, table copy and default table setup patterns in the sysfs and auto-weighting paths. Factor those pieces into small helpers so later changes can reuse one state lifecycle for both the global table and cgroup-local override tables. Keep the existing upstream wi_srcu retirement path unchanged. No functional change is intended. Signed-off-by: Li Zhe --- mm/mempolicy.c | 54 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 0884c629670d..fb9962302664 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -188,6 +188,30 @@ static void wi_state_retire(struct weighted_interleave_state *state) call_srcu(&wi_srcu, &state->rcu, wi_state_free_rcu); } +static struct weighted_interleave_state *wi_state_alloc(void) +{ + struct weighted_interleave_state *state; + + return kzalloc_flex(*state, iw_table, nr_node_ids, GFP_KERNEL); +} + +static void wi_state_init(struct weighted_interleave_state *state) +{ + for (int i = 0; i < nr_node_ids; i++) + state->iw_table[i] = 1; +} + +#if defined(CONFIG_SYSFS) || defined(CONFIG_BPF_WEIGHTED_INTERLEAVE) +static void wi_state_init_from(struct weighted_interleave_state *dst, + struct weighted_interleave_state *src) +{ + if (src) + memcpy(dst->iw_table, src->iw_table, nr_node_ids * sizeof(u8)); + else + wi_state_init(dst); +} +#endif + static u8 get_il_weight(int node) { struct weighted_interleave_state *state; @@ -243,22 +267,17 @@ int mempolicy_set_node_perf(unsigned int node, struct access_coordinate *coords) struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL; unsigned int *old_bw, *new_bw; unsigned int bw_val; - int i; bw_val = min(coords->read_bandwidth, coords->write_bandwidth); new_bw = kcalloc(nr_node_ids, sizeof(unsigned int), GFP_KERNEL); if (!new_bw) return -ENOMEM; - new_wi_state = kmalloc_flex(*new_wi_state, iw_table, nr_node_ids); + new_wi_state = wi_state_alloc(); if (!new_wi_state) { kfree(new_bw); return -ENOMEM; } - new_wi_state->mode_auto = true; - for (i = 0; i < nr_node_ids; i++) - new_wi_state->iw_table[i] = 1; - /* * Update bandwidth info, even in manual mode. That way, when switching * to auto mode in the future, iw_table can be overwritten using @@ -282,6 +301,8 @@ int mempolicy_set_node_perf(unsigned int node, struct access_coordinate *coords) } /* NULL wi_state assumes auto=true; reduce weights and update wi_state*/ + wi_state_init(new_wi_state); + new_wi_state->mode_auto = true; reduce_interleave_weights(new_bw, new_wi_state->iw_table); rcu_assign_pointer(wi_state, new_wi_state); @@ -3672,27 +3693,20 @@ static ssize_t node_store(struct kobject *kobj, struct kobj_attribute *attr, struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL; struct iw_node_attr *node_attr; u8 weight = 0; - int i; node_attr = container_of(attr, struct iw_node_attr, kobj_attr); if (count == 0 || sysfs_streq(buf, "") || kstrtou8(buf, 0, &weight) || weight == 0) return -EINVAL; - new_wi_state = kzalloc_flex(*new_wi_state, iw_table, nr_node_ids); + new_wi_state = wi_state_alloc(); if (!new_wi_state) return -ENOMEM; mutex_lock(&wi_state_lock); old_wi_state = rcu_dereference_protected(wi_state, lockdep_is_held(&wi_state_lock)); - if (old_wi_state) { - memcpy(new_wi_state->iw_table, old_wi_state->iw_table, - nr_node_ids * sizeof(u8)); - } else { - for (i = 0; i < nr_node_ids; i++) - new_wi_state->iw_table[i] = 1; - } + wi_state_init_from(new_wi_state, old_wi_state); new_wi_state->iw_table[node_attr->nid] = weight; new_wi_state->mode_auto = false; @@ -3723,16 +3737,13 @@ static ssize_t weighted_interleave_auto_store(struct kobject *kobj, struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL; unsigned int *bw; bool input; - int i; if (kstrtobool(buf, &input)) return -EINVAL; - new_wi_state = kzalloc_flex(*new_wi_state, iw_table, nr_node_ids); + new_wi_state = wi_state_alloc(); if (!new_wi_state) return -ENOMEM; - for (i = 0; i < nr_node_ids; i++) - new_wi_state->iw_table[i] = 1; mutex_lock(&wi_state_lock); old_wi_state = rcu_dereference_protected(wi_state, @@ -3745,9 +3756,7 @@ static ssize_t weighted_interleave_auto_store(struct kobject *kobj, } if (!input) { - if (old_wi_state) - memcpy(new_wi_state->iw_table, old_wi_state->iw_table, - nr_node_ids * sizeof(u8)); + wi_state_init_from(new_wi_state, old_wi_state); goto update_wi_state; } @@ -3758,6 +3767,7 @@ static ssize_t weighted_interleave_auto_store(struct kobject *kobj, return -ENODEV; } + wi_state_init(new_wi_state); new_wi_state->mode_auto = true; reduce_interleave_weights(bw, new_wi_state->iw_table); -- 2.20.1