From nobody Mon Apr 6 10:44:17 2026 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D442028B7DA for ; Sat, 21 Mar 2026 15:04:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774105467; cv=none; b=G2QVI3SdTvGgFF2HqwWvtq1gXi0toyrL+pbYAQ76FFBYtRVsc79o0RK850nuGkJAn9nJcTN8D1Cac8Mb4JJjcLK2IaePWH59edE6mWvbAcKUdYWsPBqKatYUOpo29JuAxCaqFQh8kcHErFBoenaxqCc+/+fAzMoUB1ThJKchSAA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774105467; c=relaxed/simple; bh=/Fxb0cxh0HZTdxAGbTO6shs2WBHqTx7oRHAWfPW0NKo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n2ZUr6PfybBTCsN9U/IFpDwEXskQnz6tGvs0XkAiKJjVOZ+6Dud1Mi/LtAlVFrrx3SMPL1hfhQN6H+KT5aIPRvpHWFfkxIhgqP5gqdhuciuepbEflRDm4KBtMF/xchyxqYcdcbbSZJhrAP2OtW0xtSmb00olKxVYhA0RzEGmDpE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net; spf=pass smtp.mailfrom=gourry.net; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.b=LXEh5mGz; arc=none smtp.client-ip=209.85.222.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gourry.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.b="LXEh5mGz" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-8cb20bcff5aso244487485a.3 for ; Sat, 21 Mar 2026 08:04:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1774105462; x=1774710262; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KixtzO6qBa+4sBSVnEmaKMzBG/EvPApyoC742IFx6xs=; b=LXEh5mGzLypM58FmFvQqNdsKUnow/tQ5Z1HA5Gnp129g2Y0Eb2SIsgj4rcKXvJGGz3 IAc2PeHw2TmYuxiJY6fYKic/Q7JcXzll2kUeKGCVp/0yRDpaedrJSHf1D3wt08RDEzCX zlREtDcPiE5etLjqlICpxYdSVTviFLU77I/YPXuMMyvvTPsCLMrHU42mwHZY5OTvbmWM J37hbQb+hDjWlqE4jKDgwcCi2RG7S/ymgvT4HQHCslnNf28fEg+8cDryfBnxYT+ZJjcn NRBOMGANv3lgAoeJKDMaPB8XKPkRBNpC5E6UTOHg8tHyiKr4l30ZW2uKWXSzHCqToFBS xMlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774105462; x=1774710262; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=KixtzO6qBa+4sBSVnEmaKMzBG/EvPApyoC742IFx6xs=; b=IuUnmO6rgg3GxGcGBthWNejhB2nEhmrDb7ApIXUjTYSFpYeUkbs90JoZLDr3r32OVP nWfu9vlJVFKYWr6hLQ3wpyahzp7QUhqVPpKZz9OqV9teEvZt3YhmMfXQcuIQe318e8pc az/Wgpy5gWZ5ylZD8Whvg3myW+TgqKTasGWHO47CA7UC1tnJKY5uRAM6/HqsK5+iDLfA YYEF0kJQZbzN//QuwkGG0pnxb17BET8D7CQW4zxvr5H7z4eAetLzY/FIEiFBnOHDnd3V Pg98mpusMJZrkEJzEy9wbXi3gAkSgBOJBlrKWA/qfKQdv99lHWW6peas370uSLgBXXW6 w1mA== X-Forwarded-Encrypted: i=1; AJvYcCWrkH1umeRtnnL7WqBp3ueqT0Kp9pd+4tJ8hLK5YvzYYGlj93sualMILrW0qEtPkGeqW4pMLxbFkbxBS/U=@vger.kernel.org X-Gm-Message-State: AOJu0YyneIDQsxXWJQXgMAe1xsMiniadyGtfoYvaXU7K72bVKRaY6ydO +xj+YQDqNMPQoUYrKLXyo5c8+zJdU8Mcb+2Ue2sjg9+o+9MPpm3UbWYinUmLawjANiA= X-Gm-Gg: ATEYQzwdVHakR+Z6iGvssTKu6IK5igSMbJxy+bxenb5835rJpJo5WSCc7OzoELjcj0l X0/bpXob2zWzupQCfezEwPPu8E21GtRwc7VwWfPsm0dCROgmCkTfQnD+9EyDRR/ry+L4UOxU5w2 GyigMqbPxSuJ+l6mITVf85ImbUs7uewW7KHwh3qq500E0sNRYy2Fd6TyDzXTSci5VPkv+PPGfa4 yLSrHUp/fe7V9Z4Kdu+1ogflbIoj3oB5dlwteMY7w4e2j1YCMrr39bVQUniw5PINqn19305rjx9 cfcZONVSiiDbnz1UNeJLWjc9shgHyH0p+JQe09uhoOa0ygDe6CqgrFx09Ppr/ubXgi5tayQjIit q5pXCSlJ6ve0tNg+AN4WMucJnVNX2SDwYXulq2dSvWwpqHIXWDcAuQjHliDHa7ebAstnLqU5DJE hTKJrVX4DMtVr7W8wWVukCPLFE3Et+4iJYHMFB+KRDAZq0nfcSt/U0TjaLXZUdAcLGBo0QBDH4+ cM0cJ6epxl5kSE= X-Received: by 2002:a05:620a:254c:b0:8cd:d91f:b61 with SMTP id af79cd13be357-8cfc7f6a4camr969415785a.51.1774105461527; Sat, 21 Mar 2026 08:04:21 -0700 (PDT) Received: from gourry-fedora-PF4VCD3F.lan (pool-96-255-20-138.washdc.ftas.verizon.net. [96.255.20.138]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8cfc90ba89fsm391979885a.40.2026.03.21.08.04.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Mar 2026 08:04:21 -0700 (PDT) From: Gregory Price To: linux-mm@kvack.org, vishal.l.verma@intel.com, dave.jiang@intel.com, akpm@linux-foundation.org, david@kernel.org, osalvador@suse.de Cc: dan.j.williams@intel.com, ljs@kernel.org, Liam.Howlett@oracle.com, vbabka@kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, linux-kernel@vger.kernel.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, kernel-team@meta.com Subject: [PATCH 7/8] dax/kmem: extract hotplug/hotremove helper functions Date: Sat, 21 Mar 2026 11:04:03 -0400 Message-ID: <20260321150404.3288786-8-gourry@gourry.net> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260321150404.3288786-1-gourry@gourry.net> References: <20260321150404.3288786-1-gourry@gourry.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor kmem _probe() _remove() by extracting init, cleanup, hotplug, and hot-remove logic into separate helper functions: - dax_kmem_init_resources: inits IO_RESOURCE w/ request_mem_region - dax_kmem_cleanup_resources: cleans up initialized IO_RESOURCE - dax_kmem_do_hotplug: handles memory region reservation and adding - dax_kmem_do_hotremove: handles memory removal and resource cleanup This is a pure refactoring with no functional change. The helpers will enable future extensions to support more granular control over memory hotplug operations. We need to split hotplug/remove and init/cleanup in order to have the resources available for hot-add. Otherwise, when probe occurs, the dax devices are never added to sysfs because the resources are never registered. Signed-off-by: Gregory Price --- drivers/dax/kmem.c | 308 ++++++++++++++++++++++++++++++--------------- 1 file changed, 210 insertions(+), 98 deletions(-) diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index d4c34b2e3766..8be9286f0ea3 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -47,15 +47,189 @@ struct dax_kmem_data { struct resource *res[]; }; =20 +/** + * dax_kmem_do_hotplug - hotplug memory for dax kmem device + * @dev_dax: the dev_dax instance + * @data: the dax_kmem_data structure with resource tracking + * + * Hotplugs all ranges in the dev_dax region as system memory. + * + * Returns the number of successfully mapped ranges, or negative error. + */ +static int dax_kmem_do_hotplug(struct dev_dax *dev_dax, + struct dax_kmem_data *data, + int online_type) +{ + struct device *dev =3D &dev_dax->dev; + int i, rc, onlined =3D 0; + mhp_t mhp_flags; + + for (i =3D 0; i < dev_dax->nr_range; i++) { + struct range range; + + rc =3D dax_kmem_range(dev_dax, i, &range); + if (rc) + continue; + + mhp_flags =3D MHP_NID_IS_MGID; + if (dev_dax->memmap_on_memory) + mhp_flags |=3D MHP_MEMMAP_ON_MEMORY; + + /* + * Ensure that future kexec'd kernels will not treat + * this as RAM automatically. + */ + rc =3D __add_memory_driver_managed(data->mgid, range.start, + range_len(&range), kmem_name, mhp_flags, + online_type); + + if (rc) { + dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n", + i, range.start, range.end); + if (onlined) + continue; + return rc; + } + onlined++; + } + + return onlined; +} + +/** + * dax_kmem_init_resources - create memory regions for dax kmem + * @dev_dax: the dev_dax instance + * @data: the dax_kmem_data structure with resource tracking + * + * Initializes all the resources for the DAX + * + * Returns the number of successfully mapped ranges, or negative error. + */ +static int dax_kmem_init_resources(struct dev_dax *dev_dax, + struct dax_kmem_data *data) +{ + struct device *dev =3D &dev_dax->dev; + int i, rc, mapped =3D 0; + + for (i =3D 0; i < dev_dax->nr_range; i++) { + struct resource *res; + struct range range; + + rc =3D dax_kmem_range(dev_dax, i, &range); + if (rc) + continue; + + /* Skip ranges already added */ + if (data->res[i]) + continue; + + /* Region is permanently reserved if hotremove fails. */ + res =3D request_mem_region(range.start, range_len(&range), + data->res_name); + if (!res) { + dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n", + i, range.start, range.end); + /* + * Once some memory has been onlined we can't + * assume that it can be un-onlined safely. + */ + if (mapped) + continue; + return -EBUSY; + } + data->res[i] =3D res; + /* + * Set flags appropriate for System RAM. Leave ..._BUSY clear + * so that add_memory() can add a child resource. Do not + * inherit flags from the parent since it may set new flags + * unknown to us that will break add_memory() below. + */ + res->flags =3D IORESOURCE_SYSTEM_RAM; + mapped++; + } + return mapped; +} + +#ifdef CONFIG_MEMORY_HOTREMOVE +/** + * dax_kmem_do_hotremove - hot-remove memory for dax kmem device + * @dev_dax: the dev_dax instance + * @data: the dax_kmem_data structure with resource tracking + * + * Removes all ranges in the dev_dax region. + * + * Returns the number of successfully removed ranges. + */ +static int dax_kmem_do_hotremove(struct dev_dax *dev_dax, + struct dax_kmem_data *data) +{ + struct device *dev =3D &dev_dax->dev; + int i, success =3D 0; + + for (i =3D 0; i < dev_dax->nr_range; i++) { + struct range range; + int rc; + + rc =3D dax_kmem_range(dev_dax, i, &range); + if (rc) + continue; + + /* Skip ranges not currently added */ + if (!data->res[i]) + continue; + + rc =3D remove_memory(range.start, range_len(&range)); + if (rc =3D=3D 0) { + /* Release the resource for the successfully removed range */ + remove_resource(data->res[i]); + kfree(data->res[i]); + data->res[i] =3D NULL; + success++; + continue; + } + any_hotremove_failed =3D true; + dev_err(dev, "mapping%d: %#llx-%#llx hotremove failed\n", + i, range.start, range.end); + } + + return success; +} +#else +static int dax_kmem_do_hotremove(struct dev_dax *dev_dax, + struct dax_kmem_data *data) +{ + return -EBUSY; +} +#endif /* CONFIG_MEMORY_HOTREMOVE */ + +/** + * dax_kmem_cleanup_resources - remove the dax memory resources + * @dev_dax: the dev_dax instance + * @data: the dax_kmem_data structure with resource tracking + * + * Removes all resources in the dev_dax region. + */ +static void dax_kmem_cleanup_resources(struct dev_dax *dev_dax, + struct dax_kmem_data *data) +{ + int i; + + for (i =3D 0; i < dev_dax->nr_range; i++) { + if (!data->res[i]) + continue; + remove_resource(data->res[i]); + kfree(data->res[i]); + data->res[i] =3D NULL; + } +} + static int dev_dax_kmem_probe(struct dev_dax *dev_dax) { struct device *dev =3D &dev_dax->dev; unsigned long total_len =3D 0, orig_len =3D 0; struct dax_kmem_data *data; struct memory_dev_type *mtype; - int i, rc, mapped =3D 0; - enum mmop online_type; - mhp_t mhp_flags; + int i, rc; int numa_node; int adist =3D MEMTIER_DEFAULT_LOWTIER_ADISTANCE; =20 @@ -116,72 +290,27 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) if (rc < 0) goto err_reg_mgid; data->mgid =3D rc; - - online_type =3D dev_dax->online_type; - - for (i =3D 0; i < dev_dax->nr_range; i++) { - struct resource *res; - struct range range; - - rc =3D dax_kmem_range(dev_dax, i, &range); - if (rc) - continue; - - /* Region is permanently reserved if hotremove fails. */ - res =3D request_mem_region(range.start, range_len(&range), data->res_nam= e); - if (!res) { - dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n", - i, range.start, range.end); - /* - * Once some memory has been onlined we can't - * assume that it can be un-onlined safely. - */ - if (mapped) - continue; - rc =3D -EBUSY; - goto err_request_mem; - } - data->res[i] =3D res; - - /* - * Set flags appropriate for System RAM. Leave ..._BUSY clear - * so that add_memory() can add a child resource. Do not - * inherit flags from the parent since it may set new flags - * unknown to us that will break add_memory() below. - */ - res->flags =3D IORESOURCE_SYSTEM_RAM; - - mhp_flags =3D MHP_NID_IS_MGID; - if (dev_dax->memmap_on_memory) - mhp_flags |=3D MHP_MEMMAP_ON_MEMORY; - - /* - * Ensure that future kexec'd kernels will not treat - * this as RAM automatically. - */ - rc =3D __add_memory_driver_managed(data->mgid, range.start, - range_len(&range), kmem_name, mhp_flags, - online_type); - - if (rc) { - dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n", - i, range.start, range.end); - remove_resource(res); - kfree(res); - data->res[i] =3D NULL; - if (mapped) - continue; - goto err_request_mem; - } - mapped++; - } data->mtype =3D mtype; =20 dev_set_drvdata(dev, data); =20 + rc =3D dax_kmem_init_resources(dev_dax, data); + if (rc < 0) + goto err_resources; + + /* + * Hotplug using the configured online type for this device. + */ + rc =3D dax_kmem_do_hotplug(dev_dax, data, dev_dax->online_type); + if (rc < 0) + goto err_hotplug; + return 0; =20 -err_request_mem: +err_hotplug: + dax_kmem_cleanup_resources(dev_dax, data); +err_resources: + dev_set_drvdata(dev, NULL); memory_group_unregister(data->mgid); err_reg_mgid: kfree(data->res_name); @@ -195,7 +324,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) #ifdef CONFIG_MEMORY_HOTREMOVE static void dev_dax_kmem_remove(struct dev_dax *dev_dax) { - int i, success =3D 0; + int success; int node =3D dev_dax->target_node; struct device *dev =3D &dev_dax->dev; struct dax_kmem_data *data =3D dev_get_drvdata(dev); @@ -206,42 +335,25 @@ static void dev_dax_kmem_remove(struct dev_dax *dev_d= ax) * there is no way to hotremove this memory until reboot because device * unbind will succeed even if we return failure. */ - for (i =3D 0; i < dev_dax->nr_range; i++) { - struct range range; - int rc; - - rc =3D dax_kmem_range(dev_dax, i, &range); - if (rc) - continue; - - rc =3D remove_memory(range.start, range_len(&range)); - if (rc =3D=3D 0) { - remove_resource(data->res[i]); - kfree(data->res[i]); - data->res[i] =3D NULL; - success++; - continue; - } - any_hotremove_failed =3D true; - dev_err(dev, - "mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n", - i, range.start, range.end); + success =3D dax_kmem_do_hotremove(dev_dax, data); + if (success < dev_dax->nr_range) { + dev_err(dev, "Hotplug regions stuck online until reboot\n"); + return; } =20 - if (success >=3D dev_dax->nr_range) { - memory_group_unregister(data->mgid); - kfree(data->res_name); - kfree(data); - dev_set_drvdata(dev, NULL); - /* - * Clear the memtype association on successful unplug. - * If not, we have memory blocks left which can be - * offlined/onlined later. We need to keep memory_dev_type - * for that. This implies this reference will be around - * till next reboot. - */ - clear_node_memory_type(node, data->mtype); - } + dax_kmem_cleanup_resources(dev_dax, data); + memory_group_unregister(data->mgid); + kfree(data->res_name); + kfree(data); + dev_set_drvdata(dev, NULL); + /* + * Clear the memtype association on successful unplug. + * If not, we have memory blocks left which can be + * offlined/onlined later. We need to keep memory_dev_type + * for that. This implies this reference will be around + * till next reboot. + */ + clear_node_memory_type(node, data->mtype); } #else static void dev_dax_kmem_remove(struct dev_dax *dev_dax) --=20 2.53.0