From patchwork Wed Apr 2 01:49:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rakie Kim X-Patchwork-Id: 14035484 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7BF5C36018 for ; Wed, 2 Apr 2025 01:50:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDA78280003; Tue, 1 Apr 2025 21:50:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E60DB280001; Tue, 1 Apr 2025 21:50:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD910280003; Tue, 1 Apr 2025 21:50:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B0E82280001 for ; Tue, 1 Apr 2025 21:50:06 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B00DFC0C58 for ; Wed, 2 Apr 2025 01:50:07 +0000 (UTC) X-FDA: 83287423254.19.514F661 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by imf14.hostedemail.com (Postfix) with ESMTP id B31C4100002 for ; Wed, 2 Apr 2025 01:50:05 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf14.hostedemail.com: domain of rakie.kim@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=rakie.kim@sk.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743558606; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AuqHi7mGAIDOeFG6u34nYIiyymHcFWv4ZZjjDkStn1U=; b=7rejIFep8U5pAMW+FO+ybD/j83otMrwBU2RZXEw6etmQ62juTwF+H5wFwAaVX7vfJHxTZ6 MExfOyN7EOTEVKCJ9J+JT/cEZaruM9nwQwk5Qe296+FvsRKBh63OhvDUTDOoON6Vb3NRay 1/Y4/PMobN9MvGew4bvhdkAYTW3yCcI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743558606; a=rsa-sha256; cv=none; b=lVHEPbCoIpQQ+aNoNoKUCXGbsj2utVElqKouUKWBoRJtj01ZSHWJnmNEd0PVwB3D7LK7iI nf1+nG5M93PGvljaR4quta3rLuce+cvMPEqG6AXk6lRtdoaCpwKEQjj1gnC78DZtkBJoal l04G0R4rYV2SUGt5If1UrQQi4xCjybU= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf14.hostedemail.com: domain of rakie.kim@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=rakie.kim@sk.com X-AuditID: a67dfc5b-681ff7000002311f-d0-67ec97c91b7a From: Rakie Kim To: gourry@gourry.net Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org, joshua.hahnjy@gmail.com, dan.j.williams@intel.com, ying.huang@linux.alibaba.com, david@redhat.com, Jonathan.Cameron@huawei.com, kernel_team@skhynix.com, honggyu.kim@sk.com, yunjeong.mun@sk.com, rakie.kim@sk.com Subject: [PATCH v5 1/3] mm/mempolicy: Fix memory leaks in weighted interleave sysfs Date: Wed, 2 Apr 2025 10:49:02 +0900 Message-ID: <20250402014906.1086-2-rakie.kim@sk.com> X-Mailer: git-send-email 2.48.1.windows.1 In-Reply-To: <20250402014906.1086-1-rakie.kim@sk.com> References: <20250402014906.1086-1-rakie.kim@sk.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrHLMWRmVeSWpSXmKPExsXC9ZZnoe7J6W/SDXY9VLOYs34Nm8X0qRcY Lb6u/8Vs8fPucXaLVQuvsVkc3zqP3eL8rFMsFpd3zWGzuLfmP6vF6jUZDlweO2fdZffobrvM 7tFy5C2rx+I9L5k8Nn2axO5xYsZvFo+dDy093u+7yubxeZNcAGcUl01Kak5mWWqRvl0CV8a8 JY+YCs7IVJyacIyxgXGGeBcjJ4eEgInEgi2HmGHs5u1LGLsYOTjYBJQkju2NAQmLCIhKzDs6 m6WLkYuDWeAak8SBzzdZQGqEBYIl5p+NBTFZBFQl9t0XBCnnBZpy5fcJdoiJmhINl+4xgZRw CphKfO0GCwsBlTTdXsICUS4ocXLmEzCbWUBeonnrbGaQTRICz9kkdq48xgIxR1Li4IobLBMY +Wch6ZmFpGcBI9MqRqHMvLLcxMwcE72MyrzMCr3k/NxNjMBQX1b7J3oH46cLwYcYBTgYlXh4 G3jfpAuxJpYVV+YeYpTgYFYS4Y34+jJdiDclsbIqtSg/vqg0J7X4EKM0B4uSOK/Rt/IUIYH0 xJLU7NTUgtQimCwTB6dUA6ORaOq7jgCrWUrLTJ9kXuMX+hlxQXVvoOgypwZPtwO3taPn31zj YN1/uta3UeDoZI3kX48uLjJa761hL9X6QmjD263/7JZeKrzy3MLMnbWb+XXKyTDN6yFGAXIF n36mxq6r93Gs5H97+Py8U5riQsG2/PI/tsfd/L7Jx/X2uqfTtm9MXmjprqbEUpyRaKjFXFSc CAB8DQbOcQIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrBLMWRmVeSWpSXmKPExsXCNUNNS/fk9DfpBst65S3mrF/DZjF96gVG i6/rfzFb/Lx7nN3i87PXzBarFl5jszi+dR67xeG5J1ktzs86xWJxedccNot7a/6zWhy69pzV YvWaDIvf21awOfB57Jx1l92ju+0yu0fLkbesHov3vGTy2PRpErvHiRm/WTx2PrT0eL/vKpvH t9seHotffGDy+LxJLoA7issmJTUnsyy1SN8ugStj3pJHTAVnZCpOTTjG2MA4Q7yLkZNDQsBE onn7EsYuRg4ONgEliWN7Y0DCIgKiEvOOzmbpYuTiYBa4xiRx4PNNFpAaYYFgiflnY0FMFgFV iX33BUHKeYGmXPl9gh1ioqZEw6V7TCAlnAKmEl+7wcJCQCVNt5ewQJQLSpyc+QTMZhaQl2je Opt5AiPPLCSpWUhSCxiZVjGKZOaV5SZm5pjqFWdnVOZlVugl5+duYgSG97LaPxN3MH657H6I UYCDUYmHV6DnTboQa2JZcWXuIUYJDmYlEd6Iry/ThXhTEiurUovy44tKc1KLDzFKc7AoifN6 hacmCAmkJ5akZqemFqQWwWSZODilGhgZJQsXX35twzTB5/iidIcdj9f9l7/zQeaRH4/3zH7P PVYLTzFqSLRls7rtslNKmrlKJUiHu+XgPR1HxaM2qXKSLKIZvYw+gWp8+1IdTILiGeT8gk0S d9c/Vf+h8bpfJfP+5gtHX67UuuH8Nk5nxvUzXZPmbDypIll/Y+sanf+CzOs6Jv8+46bEUpyR aKjFXFScCACBHzHQawIAAA== X-CFilter-Loop: Reflected X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B31C4100002 X-Stat-Signature: 78yma4x9h8d7j3tj1665i71pcc7h3tbe X-Rspam-User: X-HE-Tag: 1743558605-7385 X-HE-Meta: U2FsdGVkX1+O3t8zDLhSZGJ9QhX4UrXWzToWmynyWxP4XoQFkhI5kFC65VqFwtz76r7naNYXiPsqDSI55ShtcS/JAwHpZNkNUnKrCV5pUtS1DtRzNyygy4BHgFpQ2ANPPRKc/GeDfwAuhVzOmvZi6FkQO/W4i/G1zwEuRctbOCN+VHHXrr7sDo6gDHZU2pSXNNTcI17lIurSacJPoVZ7gYKpW5SNcB8W6GRjj/IuVAsD7ZHZHJT/D71j4EhruTcS0kkng29+qZCkmmbH+9M1ZYe7TwyIBGvv5rtRSJP8lQmgc1nhre/cmfIkpoTXPln3Fi33XDejabTJEruWcHmK4wKogvfMdbTVVf3+bGP2b/IipFQ1YVHGo98y6T/KjT2+YwWX/0Frr/qLKnFppFloQhZ5x5boOnRYuHM5WrJuDuMjBJ84P1O3gmKaMIvGUFV7FUqiEs7fc8lOOCJzberLGudxpT2hZArmyiYUf4RuO1TAByOc6lPQyV+05gwEBQ4UhoWq5nWPKGQk2OG5F11aVbV0ZSoqrG0Ng/ShZmF43Cgt0jp3cmuaC81nnXMN3cQ4aw7tcvpiimUJzLVMZvjPLomVXkcK2ZUo07D+kNzH1qjfXZPWaapJ5+TyLz8Fy6DYL2OiPHjXW3LdmVJSEe2vipKC3m/TzBkHDxdHdNPOx03mP7y5ls3BWFqwMaa3QGqI+jtX8NKK8f/ycaXj9FY61yWeG7K7Z1ngod3zptbzfWA4mDsClyhNtYff5y0pxRvelxzR1POPIMFKUPQRSgofIDFIxScuPoMDBZ+CYZzsFIAwdr9JC9PpQpiX4FDhrpLq32c3C80U2oMPI69IokJYerbZwly91znX07x33J/K0n0cIlAtvrO/QX/L1LRF2cFtOLzulZi9mnyTAHUj2o62ZWy6nqAIpWJ3ljSmGbTNfSInrLAgzNYKXOIJkqqzRQyMscHI2nM1D5wDnmyheUI WIJzwyjU QjuACor15FwNhoEZKIMWq6IOhPONzF5mNQ4tNHa8v+W9NPYG50y/PsRQ03hiLMEbHIgQBi9B2iuF4PYnHTJv964InvqGNrwdfr/6/PtFvk/DRlW4VJs8yLJzaelnt/bTejvmf1UYMrgjx3M3aFNvphVwiwiusxgpwcfWW X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Memory leaks occurred when removing sysfs attributes for weighted interleave. Improper kobject deallocation led to unreleased memory when initialization failed or when nodes were removed. This patch resolves the issue by replacing unnecessary `kfree()` calls with `kobject_put()`, ensuring proper cleanup and preventing memory leaks. By correctly using `kobject_put()`, the release function now properly deallocates memory without causing resource leaks, thereby improving system stability. Fixes: dce41f5ae253 ("mm/mempolicy: implement the sysfs-based weighted_interleave interface") Signed-off-by: Rakie Kim Signed-off-by: Honggyu Kim Signed-off-by: Yunjeong Mun Reviewed-by: Gregory Price --- mm/mempolicy.c | 61 +++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index bbaadbeeb291..5950d5d5b85e 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -3448,7 +3448,9 @@ static void sysfs_wi_release(struct kobject *wi_kobj) for (i = 0; i < nr_node_ids; i++) sysfs_wi_node_release(node_attrs[i], wi_kobj); - kobject_put(wi_kobj); + + kfree(node_attrs); + kfree(wi_kobj); } static const struct kobj_type wi_ktype = { @@ -3494,15 +3496,22 @@ static int add_weighted_interleave_group(struct kobject *root_kobj) struct kobject *wi_kobj; int nid, err; - wi_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL); - if (!wi_kobj) + node_attrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *), + GFP_KERNEL); + if (!node_attrs) return -ENOMEM; + wi_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL); + if (!wi_kobj) { + err = -ENOMEM; + goto node_out; + } + err = kobject_init_and_add(wi_kobj, &wi_ktype, root_kobj, "weighted_interleave"); if (err) { - kfree(wi_kobj); - return err; + kobject_put(wi_kobj); + goto err_out; } for_each_node_state(nid, N_POSSIBLE) { @@ -3512,9 +3521,17 @@ static int add_weighted_interleave_group(struct kobject *root_kobj) break; } } - if (err) + if (err) { kobject_put(wi_kobj); + goto err_out; + } + return 0; + +node_out: + kfree(node_attrs); +err_out: + return err; } static void mempolicy_kobj_release(struct kobject *kobj) @@ -3528,7 +3545,6 @@ static void mempolicy_kobj_release(struct kobject *kobj) mutex_unlock(&iw_table_lock); synchronize_rcu(); kfree(old); - kfree(node_attrs); kfree(kobj); } @@ -3542,37 +3558,22 @@ static int __init mempolicy_sysfs_init(void) static struct kobject *mempolicy_kobj; mempolicy_kobj = kzalloc(sizeof(*mempolicy_kobj), GFP_KERNEL); - if (!mempolicy_kobj) { - err = -ENOMEM; - goto err_out; - } - - node_attrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *), - GFP_KERNEL); - if (!node_attrs) { - err = -ENOMEM; - goto mempol_out; - } + if (!mempolicy_kobj) + return -ENOMEM; err = kobject_init_and_add(mempolicy_kobj, &mempolicy_ktype, mm_kobj, "mempolicy"); if (err) - goto node_out; + goto err_out; err = add_weighted_interleave_group(mempolicy_kobj); - if (err) { - pr_err("mempolicy sysfs structure failed to initialize\n"); - kobject_put(mempolicy_kobj); - return err; - } + if (err) + goto err_out; + + return 0; - return err; -node_out: - kfree(node_attrs); -mempol_out: - kfree(mempolicy_kobj); err_out: - pr_err("failed to add mempolicy kobject to the system\n"); + kobject_put(mempolicy_kobj); return err; } From patchwork Wed Apr 2 01:49:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rakie Kim X-Patchwork-Id: 14035485 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4FBF5C3601A for ; Wed, 2 Apr 2025 01:50:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B18F280004; Tue, 1 Apr 2025 21:50:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 76292280001; Tue, 1 Apr 2025 21:50:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5DAE5280004; Tue, 1 Apr 2025 21:50:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4601D280001 for ; Tue, 1 Apr 2025 21:50:09 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2BDFD80C2A for ; Wed, 2 Apr 2025 01:50:10 +0000 (UTC) X-FDA: 83287423380.26.6B11043 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by imf14.hostedemail.com (Postfix) with ESMTP id 389EC100002 for ; Wed, 2 Apr 2025 01:50:07 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf14.hostedemail.com: domain of rakie.kim@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=rakie.kim@sk.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743558608; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=befy9cWoq2fdAXvB9CziNnQG+vt1sGUmwiK+CoNht/0=; b=p/M/C8GHJMPFsNZbFuiCVV5cC19gcdgfyZT8Sb50gbyEJ8N5nBFlLSQYT/xUXCD5muRYdp cxr0gSKpLkAYdZfs8hnYziAfUIFTwcvDAJzJ9ObTtfqAZ9jrGmlAqmnKiK+2vTyz2NL2xm N2+J8JvyR3YyX10gkBum4J8S2ShH06o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743558608; a=rsa-sha256; cv=none; b=dS6yNjLDm6DzmDsXS8h1Y9yjpymCEOWDcpVLhOJYnFawKsLYcTheEQbwFk0qeI0pSGpk1p ThXk97SdOmQbeFky27Fhw6nScjsvMap7d3ECfInWXv8q5TmnkXhGPgAQIgYR0OAwxx5BBL 9yEUrTKQPcqD/iDkUmJHHRoO2dBCyGk= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf14.hostedemail.com: domain of rakie.kim@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=rakie.kim@sk.com X-AuditID: a67dfc5b-681ff7000002311f-db-67ec97cb42b4 From: Rakie Kim To: gourry@gourry.net Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org, joshua.hahnjy@gmail.com, dan.j.williams@intel.com, ying.huang@linux.alibaba.com, david@redhat.com, Jonathan.Cameron@huawei.com, kernel_team@skhynix.com, honggyu.kim@sk.com, yunjeong.mun@sk.com, rakie.kim@sk.com Subject: [PATCH v5 2/3] mm/mempolicy: Support dynamic sysfs updates for weighted interleave Date: Wed, 2 Apr 2025 10:49:03 +0900 Message-ID: <20250402014906.1086-3-rakie.kim@sk.com> X-Mailer: git-send-email 2.48.1.windows.1 In-Reply-To: <20250402014906.1086-1-rakie.kim@sk.com> References: <20250402014906.1086-1-rakie.kim@sk.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrHLMWRmVeSWpSXmKPExsXC9ZZnoe7p6W/SDW5eMLaYs34Nm8X0qRcY Lb6u/8Vs8fPucXaLVQuvsVkc3zqP3eL8rFMsFpd3zWGzuLfmP6vF6jUZDlweO2fdZffobrvM 7tFy5C2rx+I9L5k8Nn2axO5xYsZvFo+dDy093u+7yubxeZNcAGcUl01Kak5mWWqRvl0CV8aq hX+YCs4oVWzYco+9gXGOTBcjJ4eEgInElB03mboYOcDs6S/kQEw2ASWJY3tjQCpEBEQl5h2d zdLFyMXBLHCNSeLA55ssIDXCAtESV25UgtSwCKhK7N27mgXE5gWa8uDkY3aI6ZoSDZfugU3n FDCV+NoNFhYCKmm6vQSqXFDi5MwnYDazgLxE89bZzCCrJASes0nMeH+HFWKOpMTBFTdYJjDy z0LSMwtJzwJGplWMQpl5ZbmJmTkmehmVeZkVesn5uZsYgaG+rPZP9A7GTxeCDzEKcDAq8fA2 8L5JF2JNLCuuzD3EKMHBrCTCG/H1ZboQb0piZVVqUX58UWlOavEhRmkOFiVxXqNv5SlCAumJ JanZqakFqUUwWSYOTqkGRoM9fZ1vRT2DxU9tjF3/nH2NQ+nP4yt787Mz2bX35+0q2euvU257 rb/x0RzmkwFaOmvZ1/+S3vS5t2uCRtAE+VXJX16ISjdoZJf2vHTIuy/gNzvqwKZnLDfW6vz7 bOPv8mWeyn+zGRrFc7gf7v1bF/8tsDFSWfr872+hF2/lXhF0Wnty1+SXM5VYijMSDbWYi4oT Aa98sNhxAgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrBLMWRmVeSWpSXmKPExsXCNUNNS/f09DfpBvv/alnMWb+GzWL61AuM Fl/X/2K2+Hn3OLvF52evmS1WLbzGZnF86zx2i8NzT7JanJ91isXi8q45bBb31vxntTh07Tmr xeo1GRa/t61gc+Dz2DnrLrtHd9tldo+WI29ZPRbvecnksenTJHaPEzN+s3jsfGjp8X7fVTaP b7c9PBa/+MDk8XmTXAB3FJdNSmpOZllqkb5dAlfGqoV/mArOKFVs2HKPvYFxjkwXIweHhICJ xPQXciAmm4CSxLG9MV2MnBwiAqIS847OZuli5OJgFrjGJHHg800WkBphgWiJKzcqQWpYBFQl 9u5dzQJi8wJNeXDyMTuILSGgKdFw6R4TSDmngKnE126wsBBQSdPtJVDlghInZz4Bs5kF5CWa t85mnsDIMwtJahaS1AJGplWMIpl5ZbmJmTmmesXZGZV5mRV6yfm5mxiB4b2s9s/EHYxfLrsf YhTgYFTi4RXoeZMuxJpYVlyZe4hRgoNZSYQ34uvLdCHelMTKqtSi/Pii0pzU4kOM0hwsSuK8 XuGpCUIC6YklqdmpqQWpRTBZJg5OqQbGlsBNvbeZ7rjmH3FjLRI5yRop6f+mkNWhK/OpOF9r SlTAownFRpHHG6KWxebqe+nsezBt+3VXp+ezDRkkHqb0nw/8e++mkpXvvZU3Xe9sjeF04jvD vVC/6YDXRaZzUle3h7QxRCc0XA9m/zyt3KrjJX/F3AlzKrzdrc692G7ZFf52gQu/w1YlluKM REMt5qLiRABObDj4awIAAA== X-CFilter-Loop: Reflected X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 389EC100002 X-Stat-Signature: hsy387ak7wy6y6khhaanuguxojik9ap1 X-Rspam-User: X-HE-Tag: 1743558607-494154 X-HE-Meta: U2FsdGVkX19kai/fREULF3mEj5n4+htW/b6OboLE+7fhp8qmmBlYEFjavfMF0ZnCBCB9BoyuH5RSTlWOiVs5PGPYs0yffcews8S1HXVEo4487rAmEVt4LIoYXdjyky4dLBFRHyOt5ChfR83bhJDA5/kWoYmqLQF5JOJfuRnYbUn4eRVpXPqpRo7XJbsYeK5lg5PVv2AhLMUjRSRpmIWU5kwyS8ScJRgV4iFCBCpRzOssZrFLD9yM5bVymszZE/xTbuwlwF139kx7yPqbzfKzzTb7Yn0HogKpWDyhBmsFqYYA+AZYltbP/53od9AoczGOvM5I4DbKDHPqQ8dCaAwkivy70B7MloVouysAmaPhbEWQBXOJM7E852kiPetrpYvJiGnuOcml9lT2jIYTBx+XwTn0A27z8kQDhS4wGFmjAljLsG3fdG4iHkDSpQ+c+mNK/WgY+5y9KHSRw31SDIjYN47WA46zXG1OisRuKmPT5s8aqmtqH78JkXttrMYz38jd8NwL2W94GRnczW5IblTZWMTpz+XZp0dqJNF4JAz50/Lpqg0aYO5iFyq5jeGMXiVJYdEEbwHcm7D6YHrTwbhabZogqoijOzxZULts5rm7fQH4nK8UffVcmLU+uzVnjgnpR3vsq0bGvHfKuCYQTCihcxai86+FOUdJm0bRtUV9UwCgma8v3KFkiIlsuSQWqD3+Dcj+SzWRPrLTMcAgjgUYr/qt+8vcJmK9Few044YDZmdxX7pEY7chtzH7cswMZlFvlCt0wIryuI58m9L0iGYWpb3ld8E2SkWsLInvhKvVR29kxQsQLLhpgcQws63YRmF0AUppReA+v/3VQeVMr6tkAlK8Zj6+UFn/0VlVOhduJCgJWQ5qaQ+ov7hNn/nf8l9vZlBse4vSMflG7H55WRmUdWAH1Hci592y8x2TEDKkb8UAOJRDIiQMFnumnGgjZR/n9ozYmZhm7ORKHAGTVTF f9f/TwX3 U7m7MlqiEKWZNoUVHmKjVJSPNt9P+/X7+CUfC6fZ8BwNXAlLyvHTS3k7O3+8q4gXDnuMi45v2BLrcxUzToGHeY6mbT9mDJjB06g5XbSK5RXewU75JiQ1TX4UyLXhHBtC3mEt5ziZISDDTmNU1C7oOBvieW9Mgdv828Wr+ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Previously, the weighted interleave sysfs structure was statically managed, preventing dynamic updates when nodes were added or removed. This patch restructures the weighted interleave sysfs to support dynamic insertion and deletion. The sysfs that was part of the 'weighted_interleave_group' is now globally accessible, allowing external access to that sysfs. With this change, sysfs management for weighted interleave is more flexible, supporting hotplug events and runtime updates more effectively. Signed-off-by: Rakie Kim Signed-off-by: Honggyu Kim Signed-off-by: Yunjeong Mun Reviewed-by: Gregory Price --- mm/mempolicy.c | 71 ++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 40 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 5950d5d5b85e..3092a792bd28 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -3388,6 +3388,13 @@ struct iw_node_attr { int nid; }; +struct sysfs_wi_group { + struct kobject wi_kobj; + struct iw_node_attr *nattrs[]; +}; + +static struct sysfs_wi_group *wi_group; + static ssize_t node_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -3430,27 +3437,24 @@ static ssize_t node_store(struct kobject *kobj, struct kobj_attribute *attr, return count; } -static struct iw_node_attr **node_attrs; - -static void sysfs_wi_node_release(struct iw_node_attr *node_attr, - struct kobject *parent) +static void sysfs_wi_node_release(int nid) { - if (!node_attr) + if (!wi_group->nattrs[nid]) return; - sysfs_remove_file(parent, &node_attr->kobj_attr.attr); - kfree(node_attr->kobj_attr.attr.name); - kfree(node_attr); + + sysfs_remove_file(&wi_group->wi_kobj, + &wi_group->nattrs[nid]->kobj_attr.attr); + kfree(wi_group->nattrs[nid]->kobj_attr.attr.name); + kfree(wi_group->nattrs[nid]); } static void sysfs_wi_release(struct kobject *wi_kobj) { - int i; - - for (i = 0; i < nr_node_ids; i++) - sysfs_wi_node_release(node_attrs[i], wi_kobj); + int nid; - kfree(node_attrs); - kfree(wi_kobj); + for (nid = 0; nid < nr_node_ids; nid++) + sysfs_wi_node_release(nid); + kfree(wi_group); } static const struct kobj_type wi_ktype = { @@ -3458,7 +3462,7 @@ static const struct kobj_type wi_ktype = { .release = sysfs_wi_release, }; -static int add_weight_node(int nid, struct kobject *wi_kobj) +static int sysfs_wi_node_add(int nid) { struct iw_node_attr *node_attr; char *name; @@ -3480,57 +3484,44 @@ static int add_weight_node(int nid, struct kobject *wi_kobj) node_attr->kobj_attr.store = node_store; node_attr->nid = nid; - if (sysfs_create_file(wi_kobj, &node_attr->kobj_attr.attr)) { + if (sysfs_create_file(&wi_group->wi_kobj, &node_attr->kobj_attr.attr)) { kfree(node_attr->kobj_attr.attr.name); kfree(node_attr); pr_err("failed to add attribute to weighted_interleave\n"); return -ENOMEM; } - node_attrs[nid] = node_attr; + wi_group->nattrs[nid] = node_attr; return 0; } -static int add_weighted_interleave_group(struct kobject *root_kobj) +static int add_weighted_interleave_group(struct kobject *mempolicy_kobj) { - struct kobject *wi_kobj; int nid, err; - node_attrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *), - GFP_KERNEL); - if (!node_attrs) + wi_group = kzalloc(sizeof(struct sysfs_wi_group) + \ + nr_node_ids * sizeof(struct iw_node_attr *), \ + GFP_KERNEL); + if (!wi_group) return -ENOMEM; - wi_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL); - if (!wi_kobj) { - err = -ENOMEM; - goto node_out; - } - - err = kobject_init_and_add(wi_kobj, &wi_ktype, root_kobj, + err = kobject_init_and_add(&wi_group->wi_kobj, &wi_ktype, mempolicy_kobj, "weighted_interleave"); - if (err) { - kobject_put(wi_kobj); + if (err) goto err_out; - } for_each_node_state(nid, N_POSSIBLE) { - err = add_weight_node(nid, wi_kobj); + err = sysfs_wi_node_add(nid); if (err) { pr_err("failed to add sysfs [node%d]\n", nid); - break; + goto err_out; } } - if (err) { - kobject_put(wi_kobj); - goto err_out; - } return 0; -node_out: - kfree(node_attrs); err_out: + kobject_put(&wi_group->wi_kobj); return err; } From patchwork Wed Apr 2 01:49:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rakie Kim X-Patchwork-Id: 14035486 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3DE7C36010 for ; Wed, 2 Apr 2025 01:50:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC165280005; Tue, 1 Apr 2025 21:50:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEA0F280001; Tue, 1 Apr 2025 21:50:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8ED22280005; Tue, 1 Apr 2025 21:50:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 749A7280001 for ; Tue, 1 Apr 2025 21:50:11 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 765F457E98 for ; Wed, 2 Apr 2025 01:50:12 +0000 (UTC) X-FDA: 83287423464.08.ED50DD8 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by imf14.hostedemail.com (Postfix) with ESMTP id 7DAA5100002 for ; Wed, 2 Apr 2025 01:50:10 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf14.hostedemail.com: domain of rakie.kim@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=rakie.kim@sk.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743558610; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ntM4ibfvMDiBQ8cneIv0eJ8XUJ6Zf+qj+Z5X9DD4CgA=; b=rae29SJy/juKG5Y8fCv4Evusf9lJ8RG339ouRsDb6f2xli4Z2h0b/q/T+91rw/nsV7pCQb GT3humWZTOg2h/fzJXjnMy2BnvSOxBJx0ZmOphgumX/3n4FTK+dBXQCs+VO23Qz+t3PFHm 7hUyeQtkFKv/h1radd8SFQEZwh12DPs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743558610; a=rsa-sha256; cv=none; b=3YMbJ0WWSgGWjw+Xv3kfliFD2tWvLNgTlRuFNBb20Ws1MsUblXvmfrJ4kaUSNsEHQP1Z0r N5U4ZRJPD4rhFTfWnd4t8cesX5GAcinj0a1wUSMo3Fsw8Pmkj7vHO5gZfBJL+xVquyNbrB etVxyPomhttVxOLhEqO8dp3DlvwkBns= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf14.hostedemail.com: domain of rakie.kim@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=rakie.kim@sk.com X-AuditID: a67dfc5b-681ff7000002311f-e8-67ec97ce1b74 From: Rakie Kim To: gourry@gourry.net Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org, joshua.hahnjy@gmail.com, dan.j.williams@intel.com, ying.huang@linux.alibaba.com, david@redhat.com, Jonathan.Cameron@huawei.com, kernel_team@skhynix.com, honggyu.kim@sk.com, yunjeong.mun@sk.com, rakie.kim@sk.com Subject: [PATCH v5 3/3] mm/mempolicy: Support memory hotplug in weighted interleave Date: Wed, 2 Apr 2025 10:49:04 +0900 Message-ID: <20250402014906.1086-4-rakie.kim@sk.com> X-Mailer: git-send-email 2.48.1.windows.1 In-Reply-To: <20250402014906.1086-1-rakie.kim@sk.com> References: <20250402014906.1086-1-rakie.kim@sk.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrALMWRmVeSWpSXmKPExsXC9ZZnoe656W/SDV42clrMWb+GzWL61AuM Fl/X/2K2+Hn3OLvFqoXX2CyOb53HbnF+1ikWi8u75rBZ3Fvzn9Vi9ZoMBy6PnbPusnt0t11m 92g58pbVY/Gel0wemz5NYvc4MeM3i8fOh5Ye7/ddZfP4vEkugDOKyyYlNSezLLVI3y6BK6P/ 8H32gibjiqkvrzM1ME7T6mLk5JAQMJF40r+YDcY+9X8SaxcjBwebgJLEsb0xIGERAVGJeUdn s3QxcnEwC1xjkjjw+SYLSEJYIFhi5ZVjYDaLgKrEn//z2EFsXqA5S+Z9YoWYqSnRcOkeE8hM TgFTia/dYCVCQCVNt5ewQJQLSpyc+QTMZhaQl2jeOpsZZJeEwHM2iV39v1kg5khKHFxxg2UC I/8sJD2zkPQsYGRaxSiUmVeWm5iZY6KXUZmXWaGXnJ+7iREY7stq/0TvYPx0IfgQowAHoxIP bwPvm3Qh1sSy4srcQ4wSHMxKIrwRX1+mC/GmJFZWpRblxxeV5qQWH2KU5mBREuc1+laeIiSQ nliSmp2aWpBaBJNl4uCUamD0lCxKm/rnl9xsb9mpPfmhLTdPGhyTSbqh+P17zISrs5ZeZzO9 6rdDL8d/sk5nsoVXNEOX3Mm3VyZNkdvPManM+NusuR1LP1uw+TyMYNPzuKG0680X+2wpvdrZ rkk6ShIrH8VszVJNqjh/ycpHm/eAS5wWt+KfnaHHc8uLxZZ+2+y5Uvrs4j1KLMUZiYZazEXF iQDjDgjFcwIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrFLMWRmVeSWpSXmKPExsXCNUNNS/fs9DfpBrduG1vMWb+GzWL61AuM Fl/X/2K2+Hn3OLvF52evmS1WLbzGZnF86zx2i8NzT7JanJ91isXi8q45bBb31vxntTh07Tmr xeo1GRa/t61gc+Dz2DnrLrtHd9tldo+WI29ZPRbvecnksenTJHaPEzN+s3jsfGjp8X7fVTaP b7c9PBa/+MDk8XmTXAB3FJdNSmpOZllqkb5dAldG/+H77AVNxhVTX15namCcptXFyMkhIWAi cer/JNYuRg4ONgEliWN7Y0DCIgKiEvOOzmbpYuTiYBa4xiRx4PNNFpCEsECwxMorx8BsFgFV iT//57GD2LxAc5bM+8QKMVNTouHSPSaQmZwCphJfu8FKhIBKmm4vYYEoF5Q4OfMJmM0sIC/R vHU28wRGnllIUrOQpBYwMq1iFMnMK8tNzMwx1SvOzqjMy6zQS87P3cQIDPFltX8m7mD8ctn9 EKMAB6MSD69Az5t0IdbEsuLK3EOMEhzMSiK8EV9fpgvxpiRWVqUW5ccXleakFh9ilOZgURLn 9QpPTRASSE8sSc1OTS1ILYLJMnFwSjUw1quf6/c1ujPnwEfje2v/WqycdPny/lXzwm6t+21z hDnMc8cvhxKro6/Pdfg/0o2u28/R8bHXc4W1U6VcnqJh0JcN006uqvqh4h93fO/TrGnzWE5W znDLe5/qICQyfbLskQ+dbu6aO05NNFSZpaOVlRp4cUaKV9d6saqLOm6f99flbl+uFnrqtBJL cUaioRZzUXEiAGQV7optAgAA X-CFilter-Loop: Reflected X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7DAA5100002 X-Stat-Signature: gf9qygus6pjpeixda6fchu5r7r4uk6fk X-Rspam-User: X-HE-Tag: 1743558610-980780 X-HE-Meta: U2FsdGVkX1+k0rbC9g5uBbZbp1qOTY3qfKEguR6KnJ46U7WxDX/gExMUWGlGZu/8key13UEDlU678YdHHC896OFOZQONjV2x65IqMO4kkIh2hZnm54Qc0SbsKLys+r2vqcDIMr1yojdZHg1gwi0ZoCSlw6SVHrEP5GvXzgbwmHMZuCqxX4tgormtt4R3NxqVkWDLCv+aW57zpLcBZ9SqAxn07Pnq2vv+Vw8i0RdBmUOgF0svNGx4S3q0HGSrR+GkDeL6mzfkdrU7GU7JZ/cv+g8+2eXer6kDxC80Bcg2WoB/ubOZMurqNPMqlJqicIL4SrrsmSVz2/NXcABoOj20Ru3spBWnibaueMqAK5JTlo587JLvl30ZvXJh8Tv3bYjSnQM0phVa6DY2WS14LEZYeVxPBoCRaCqESiIuKQN0aeazsZbzQb8pEYqR/qpggM7BGTT7nP05sHIQVyBvf6r3JUgkwUfIZtrP9DOYi4KxFFq2x0seeqbRf/iHSmYZJ5DYiO77JnIR7Et5nj2i92QJFA3zF6lhTD9ZiiubnACBKNKsJ4A5cv3QlGFVjmrZydG1RPSCqwHzK8+b7Ko2c67njxEGnFnKQhs0881oJlFNO5KztYuDueDUSOqR6w9sMUKqYy+A03JMmzhU8lFYuEUY2EmeTuFBRhJpnW6u628cbM+H2WXvnnCXlPZ3trH7ZUjEdZklOPYkfkq4HmBsc3e/kOqIjbLuxkcCxJGfnZu47ZWKsaZ/pN6iovgWuVmaHGHhe/je43RttvfbL5XmEkNWsToQb+9qW5jb4RIFKZ8lNFRODFe0PSJFiv3jPGaLrhkWe5ePx3ITvSmMl1XzAcQDR+c/CADyviSxPKEf9tRgkClxQ9TdY/pJGbySRF/nS7nijOZjwulV1K0AA56eIAVjB9YJQCWQOZcEdtps44Ugnq7pU0QgGvHVxeTj9/PHpJE/UIIrnnrzbB4zMXoDZOZ 1z6viAdw wCm1ENN2KqxZX2en6SwECfqPNDhtT9Kqe6OwZi2oF7F0RueBevqG5tPzTS6/eTDjuPMy/pu51bwH7UTexMmYDyT9GPqmReFC8RpoL X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The weighted interleave policy distributes page allocations across multiple NUMA nodes based on their performance weight, thereby improving memory bandwidth utilization. The weight values for each node are configured through sysfs. Previously, sysfs entries for configuring weighted interleave were created for all possible nodes (N_POSSIBLE) at initialization, including nodes that might not have memory. However, not all nodes in N_POSSIBLE are usable at runtime, as some may remain memoryless or offline. This led to sysfs entries being created for unusable nodes, causing potential misconfiguration issues. To address this issue, this patch modifies the sysfs creation logic to: 1) Limit sysfs entries to nodes that are online and have memory, avoiding the creation of sysfs entries for nodes that cannot be used. 2) Support memory hotplug by dynamically adding and removing sysfs entries based on whether a node transitions into or out of the N_MEMORY state. Additionally, the patch ensures that sysfs attributes are properly managed when nodes go offline, preventing stale or redundant entries from persisting in the system. By making these changes, the weighted interleave policy now manages its sysfs entries more efficiently, ensuring that only relevant nodes are considered for interleaving, and dynamically adapting to memory hotplug events. Signed-off-by: Rakie Kim Signed-off-by: Honggyu Kim Signed-off-by: Yunjeong Mun --- mm/mempolicy.c | 110 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 87 insertions(+), 23 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 3092a792bd28..ea4f3f3f2cdd 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -113,6 +113,7 @@ #include #include #include +#include #include "internal.h" @@ -3390,6 +3391,7 @@ struct iw_node_attr { struct sysfs_wi_group { struct kobject wi_kobj; + struct mutex kobj_lock; struct iw_node_attr *nattrs[]; }; @@ -3439,13 +3441,24 @@ static ssize_t node_store(struct kobject *kobj, struct kobj_attribute *attr, static void sysfs_wi_node_release(int nid) { - if (!wi_group->nattrs[nid]) + struct iw_node_attr *attr; + + if (nid < 0 || nid >= nr_node_ids) + return; + + mutex_lock(&wi_group->kobj_lock); + attr = wi_group->nattrs[nid]; + if (!attr) { + mutex_unlock(&wi_group->kobj_lock); return; + } + + wi_group->nattrs[nid] = NULL; + mutex_unlock(&wi_group->kobj_lock); - sysfs_remove_file(&wi_group->wi_kobj, - &wi_group->nattrs[nid]->kobj_attr.attr); - kfree(wi_group->nattrs[nid]->kobj_attr.attr.name); - kfree(wi_group->nattrs[nid]); + sysfs_remove_file(&wi_group->wi_kobj, &attr->kobj_attr.attr); + kfree(attr->kobj_attr.attr.name); + kfree(attr); } static void sysfs_wi_release(struct kobject *wi_kobj) @@ -3464,35 +3477,81 @@ static const struct kobj_type wi_ktype = { static int sysfs_wi_node_add(int nid) { - struct iw_node_attr *node_attr; + int ret = 0; char *name; + struct iw_node_attr *new_attr = NULL; - node_attr = kzalloc(sizeof(*node_attr), GFP_KERNEL); - if (!node_attr) + if (nid < 0 || nid >= nr_node_ids) { + pr_err("Invalid node id: %d\n", nid); + return -EINVAL; + } + + new_attr = kzalloc(sizeof(struct iw_node_attr), GFP_KERNEL); + if (!new_attr) return -ENOMEM; name = kasprintf(GFP_KERNEL, "node%d", nid); if (!name) { - kfree(node_attr); + kfree(new_attr); return -ENOMEM; } - sysfs_attr_init(&node_attr->kobj_attr.attr); - node_attr->kobj_attr.attr.name = name; - node_attr->kobj_attr.attr.mode = 0644; - node_attr->kobj_attr.show = node_show; - node_attr->kobj_attr.store = node_store; - node_attr->nid = nid; + mutex_lock(&wi_group->kobj_lock); + if (wi_group->nattrs[nid]) { + mutex_unlock(&wi_group->kobj_lock); + pr_info("Node [%d] already exists\n", nid); + kfree(new_attr); + kfree(name); + return 0; + } + wi_group->nattrs[nid] = new_attr; - if (sysfs_create_file(&wi_group->wi_kobj, &node_attr->kobj_attr.attr)) { - kfree(node_attr->kobj_attr.attr.name); - kfree(node_attr); - pr_err("failed to add attribute to weighted_interleave\n"); - return -ENOMEM; + sysfs_attr_init(&wi_group->nattrs[nid]->kobj_attr.attr); + wi_group->nattrs[nid]->kobj_attr.attr.name = name; + wi_group->nattrs[nid]->kobj_attr.attr.mode = 0644; + wi_group->nattrs[nid]->kobj_attr.show = node_show; + wi_group->nattrs[nid]->kobj_attr.store = node_store; + wi_group->nattrs[nid]->nid = nid; + + ret = sysfs_create_file(&wi_group->wi_kobj, + &wi_group->nattrs[nid]->kobj_attr.attr); + if (ret) { + kfree(wi_group->nattrs[nid]->kobj_attr.attr.name); + kfree(wi_group->nattrs[nid]); + wi_group->nattrs[nid] = NULL; + pr_err("Failed to add attribute to weighted_interleave: %d\n", ret); } + mutex_unlock(&wi_group->kobj_lock); - wi_group->nattrs[nid] = node_attr; - return 0; + return ret; +} + +static int wi_node_notifier(struct notifier_block *nb, + unsigned long action, void *data) +{ + int err; + struct memory_notify *arg = data; + int nid = arg->status_change_nid; + + if (nid < 0) + goto notifier_end; + + switch(action) { + case MEM_ONLINE: + err = sysfs_wi_node_add(nid); + if (err) { + pr_err("failed to add sysfs [node%d]\n", nid); + return NOTIFY_BAD; + } + break; + case MEM_OFFLINE: + if (!node_state(nid, N_MEMORY)) + sysfs_wi_node_release(nid); + break; + } + +notifier_end: + return NOTIFY_OK; } static int add_weighted_interleave_group(struct kobject *mempolicy_kobj) @@ -3504,13 +3563,17 @@ static int add_weighted_interleave_group(struct kobject *mempolicy_kobj) GFP_KERNEL); if (!wi_group) return -ENOMEM; + mutex_init(&wi_group->kobj_lock); err = kobject_init_and_add(&wi_group->wi_kobj, &wi_ktype, mempolicy_kobj, "weighted_interleave"); if (err) goto err_out; - for_each_node_state(nid, N_POSSIBLE) { + for_each_online_node(nid) { + if (!node_state(nid, N_MEMORY)) + continue; + err = sysfs_wi_node_add(nid); if (err) { pr_err("failed to add sysfs [node%d]\n", nid); @@ -3518,6 +3581,7 @@ static int add_weighted_interleave_group(struct kobject *mempolicy_kobj) } } + hotplug_memory_notifier(wi_node_notifier, DEFAULT_CALLBACK_PRI); return 0; err_out: