From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (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 81A833438A0 for ; Sun, 12 Jul 2026 15:45:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783871133; cv=none; b=f8/V78WUyM/pcb1PmC4Ffi24eKDlurBv/st4ZGq3SIh2vUl9dG4gHDuCxlME3Hm/MmN+P25qoje/SzJSQt2dMXk0VhIGa4xjd7lUgbtwSjyVi5YAUNlnBbPVl/j5G34qYrEuff5elJo2YazAK2W2KN11yk3aVWiA3pNOj5csXj4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783871133; c=relaxed/simple; bh=CQgzBRruGlOBdStAkSN+ZHVIq/ho3VqxALjNcSGxk4M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QmCR+t+IZvvWXFnkLYN13og/wTwmkN08KjHLdIbdHVWPtJyG7e7ABocvjPPh86Mc2UEbjPZSJ1TcKACa1/WJQN6V5FJsXWHNvgbWEbUE042xbWq4hnhil8gonhD8lOaCaJoQ5vnk8T60Olqk5WhhZ8S7+aqVmbF5W8bcMc2aZeY= 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=PqNwpTHY; arc=none smtp.client-ip=209.85.222.179 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="PqNwpTHY" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-92e7c6ec9dbso93921685a.0 for ; Sun, 12 Jul 2026 08:45:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1783871129; x=1784475929; 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:content-type; bh=9m4Re3kSEFpR6uNzVdviqzECHvX8FpKIStu+nZnUI9c=; b=PqNwpTHY7y6DMkXb/MrspR/ocnAAuYBWuegWRSjspJj/KXn3E+LO1lR/moeWWu3Ehy 6YPA2sN8Bq2j+/hiDzmd5G1CUeQDXHJiQaJbO/86s5gmlLrl9zE639LU0i4cMFB0NEbD Z8vc8yh4JstmbSysOCIMmAazzk5DrRYbrKEozTpMDaXxfY/FiBmaYMhT5ApEBegDQjVJ EB+5wgcVvIkRMZoBkvVQZ9t2izXyCL9hkZhFHKoeX4eEXgTNktTZV6LclXyk1aniFn4f e56V9lTAkKtKo4uDWIQHmBpCCysbsctZQm7udJglkZ0i36G/W052vQ8CUg3mh7UmE3AE wodg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783871129; x=1784475929; 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:content-type; bh=9m4Re3kSEFpR6uNzVdviqzECHvX8FpKIStu+nZnUI9c=; b=n0bCHkTeYY3MwBKBs3d5XVSYwh8dA+vFkzS9bAvZSlN1V1fCuEmEl9CUExXaC038fd J6TfsbnF5Qz3qdazAqZp6lxEHEMeOfkxKp4sWlxu1CL/I8aMp0djWP/Jm/lGSvZTCVdT 9qNL81VaPsvN9vtPyacrwoX0hMbiPdrvPuXno1DzUlGEBVWFecAxdFccN3+ORQDsoNYA 7+UIae5ccHOl8ydwTwOpIkVffoJzfSr6nI4ud0lk1UCcW1m8RWyDIXCwzzdizVN5R8l8 zs7b3OL/M9WzhZuDJq/4vARbrRPIBC8l0fM8aqW+hcwKAh/0s882tI8mLJTP2C6O7Zev R26Q== X-Gm-Message-State: AOJu0YzLo+qibslaRq/tMDTlWFjHxSeEqs+NYCFMzp/ZRnP+9ujhZdh3 l9jJaXtay4JLmxChmDIUFK8AgNiIcUdXmO/p8usXwePJgGkGLcyf/8JBhw+5JqrQ3K8= X-Gm-Gg: AfdE7cmi8QY5GSVFZYMYxkdLZKWrGuqpevICWCspjF3Wy2/ybVlHt/9K38ajHbjuEhv zRrzEZ5lcbu38vQoGFEvmCt5fu3DRinJ+0GwAU8StWkN0gyYyhhYrE8uG1+lMii5grvRwcbk8Am IO7cG5D3Gpk0NcM96vOf8TBZiXi6gt4wqz4OoanvfPe8QkN0cwfcq5pxUCL4sZ1/McVKsmVyJab tnQGK0u9K5SPGqZC0kshQL6f9oiZb+830VMeq34gxURwRv6v9ewChskAKucl6miTUL62qXx1jMe gHCxRFL7CA4341k4OovvE9Gia4TSgxFk3C1QqyKGzpQoFORkKojk9cez4h9GUPACvPWgEkxxmhs f/MSydlxzpIpzkT1YGYU6zY/hSlTj6e1C3IXBG7t97uvAWwVtCzccL6ZvYaGAQa58rESS9W/8hG 636ROByTYMqWChMdZYGJQKUgfH1u0kee0n+smKYSmNC8HeF3rHDS4xbUZGmb+eHZZKNwghw9xzq Q== X-Received: by 2002:a05:620a:2552:b0:92e:8405:7ad2 with SMTP id af79cd13be357-92ef2be2c70mr599484085a.41.1783871129312; Sun, 12 Jul 2026 08:45:29 -0700 (PDT) Received: from gourry-fedora-PF4VCD3F.lan (pool-173-79-60-52.washdc.fios.verizon.net. [173.79.60.52]) by smtp.gmail.com with ESMTPSA id af79cd13be357-92ee5d61facsm862186485a.42.2026.07.12.08.45.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jul 2026 08:45:28 -0700 (PDT) From: Gregory Price To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org, driver-core@lists.linux.dev, nvdimm@lists.linux.dev, linux-kselftest@vger.kernel.org, kernel-team@meta.com, david@kernel.org, osalvador@suse.de, gregkh@linuxfoundation.org, rafael@kernel.org, dakr@kernel.org, djbw@kernel.org, vishal.l.verma@intel.com, dave.jiang@intel.com, alison.schofield@intel.com, akpm@linux-foundation.org, ljs@kernel.org, liam@infradead.org, vbabka@kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, shuah@kernel.org, gourry@gourry.net Subject: [PATCH v7 08/10] dax/kmem: extract hotplug/hotremove helper functions Date: Sun, 12 Jul 2026 11:45:02 -0400 Message-ID: <20260712154505.3564379-9-gourry@gourry.net> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260712154505.3564379-1-gourry@gourry.net> References: <20260712154505.3564379-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: 8bit 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/hotunplug 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. Detatching hotunplug/cleanup allows us to re-use the hotunplug code without destroying the underlying resources. Reviewed-by: Dave Jiang Signed-off-by: Gregory Price --- drivers/dax/kmem.c | 323 +++++++++++++++++++++++++++++++-------------- 1 file changed, 223 insertions(+), 100 deletions(-) diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index 38ed5c4e9c83d..6174f7d3d05bd 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -63,14 +63,206 @@ static void kmem_put_memory_types(void) mt_put_memory_types(&kmem_memory_types); } +/** + * 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 = &dev_dax->dev; + int i, rc, onlined = 0; + mhp_t mhp_flags; + + for (i = 0; i < dev_dax->nr_range; i++) { + struct range range; + + rc = dax_kmem_range(dev_dax, i, &range); + if (rc) + continue; + + /* + * init_resources() is best-effort: if a reservation conflict + * occurs it keeps the range but leaves res[i]=NULL. For hotplug + * on probe systems, this means kmem will partially online. + * + * We have to keep this behavior not to break those systems. + * For those systems - atomicity only applies to valid ranges. + */ + if (!data->res[i]) + continue; + + mhp_flags = MHP_NID_IS_MGID; + if (dev_dax->memmap_on_memory) + mhp_flags |= MHP_MEMMAP_ON_MEMORY; + + /* + * Ensure that future kexec'd kernels will not treat + * this as RAM automatically. + */ + rc = __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); + /* + * Release the reservation for the range that failed to + * add so a later hotremove does not try to remove memory + * that was never added. + */ + if (data->res[i]) { + remove_resource(data->res[i]); + kfree(data->res[i]); + data->res[i] = NULL; + } + 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 = &dev_dax->dev; + int i, rc, mapped = 0; + + for (i = 0; i < dev_dax->nr_range; i++) { + struct resource *res; + struct range range; + + rc = 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 = 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] = 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() later. + */ + res->flags = 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 = &dev_dax->dev; + int i, success = 0; + + for (i = 0; i < dev_dax->nr_range; i++) { + struct range range; + int rc; + + rc = dax_kmem_range(dev_dax, i, &range); + if (rc) + continue; + + /* range was never added during probe, count as removed */ + if (!data->res[i]) { + success++; + continue; + } + + rc = remove_memory(range.start, range_len(&range)); + if (rc == 0) { + /* Release the resource for the successfully removed range */ + remove_resource(data->res[i]); + kfree(data->res[i]); + data->res[i] = NULL; + success++; + continue; + } + any_hotremove_failed = true; + dev_err(dev, "mapping%d: %#llx-%#llx hotremove failed\n", + i, range.start, range.end); + } + + return success; +} +#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 = 0; i < dev_dax->nr_range; i++) { + if (!data->res[i]) + continue; + remove_resource(data->res[i]); + kfree(data->res[i]); + data->res[i] = NULL; + } +} + static int dev_dax_kmem_probe(struct dev_dax *dev_dax) { struct device *dev = &dev_dax->dev; unsigned long total_len = 0, orig_len = 0; struct dax_kmem_data *data; struct memory_dev_type *mtype; - int i, rc, mapped = 0; - mhp_t mhp_flags; + int i, rc; int numa_node; int adist = MEMTIER_DEFAULT_DAX_ADISTANCE; int online_type = mhp_get_default_online_type(); @@ -133,68 +325,22 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) goto err_reg_mgid; data->mgid = rc; - for (i = 0; i < dev_dax->nr_range; i++) { - struct resource *res; - struct range range; - - rc = dax_kmem_range(dev_dax, i, &range); - if (rc) - continue; - - /* Region is permanently reserved if hotremove fails. */ - res = 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; - rc = -EBUSY; - goto err_request_mem; - } - data->res[i] = 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 = IORESOURCE_SYSTEM_RAM; - - mhp_flags = MHP_NID_IS_MGID; - if (dev_dax->memmap_on_memory) - mhp_flags |= MHP_MEMMAP_ON_MEMORY; - - /* - * Ensure that future kexec'd kernels will not treat - * this as RAM automatically. - */ - rc = __add_memory_driver_managed(data->mgid, range.start, - range_len(&range), kmem_name, mhp_flags, - online_type); + dev_set_drvdata(dev, data); - 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] = NULL; - if (mapped) - continue; - goto err_request_mem; - } - mapped++; - } + rc = dax_kmem_init_resources(dev_dax, data); + if (rc < 0) + goto err_resources; - dev_set_drvdata(dev, data); + rc = dax_kmem_do_hotplug(dev_dax, data, online_type); + if (rc < 0) + goto err_hotplug; return 0; -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); @@ -208,7 +354,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 = 0; + int success; int node = dev_dax->target_node; struct device *dev = &dev_dax->dev; struct dax_kmem_data *data = dev_get_drvdata(dev); @@ -219,48 +365,25 @@ static void dev_dax_kmem_remove(struct dev_dax *dev_dax) * there is no way to hotremove this memory until reboot because device * unbind will succeed even if we return failure. */ - for (i = 0; i < dev_dax->nr_range; i++) { - struct range range; - int rc; - - rc = dax_kmem_range(dev_dax, i, &range); - if (rc) - continue; - - /* range was never added during probe */ - if (!data->res[i]) { - success++; - continue; - } - - rc = remove_memory(range.start, range_len(&range)); - if (rc == 0) { - remove_resource(data->res[i]); - kfree(data->res[i]); - data->res[i] = NULL; - success++; - continue; - } - any_hotremove_failed = true; - dev_err(dev, - "mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n", - i, range.start, range.end); + success = dax_kmem_do_hotremove(dev_dax, data); + if (success < dev_dax->nr_range) { + dev_err(dev, "Hotplug regions stuck online until reboot\n"); + return; } - if (success >= 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, NULL); - } + 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, NULL); } #else static void dev_dax_kmem_remove(struct dev_dax *dev_dax) -- 2.53.0-Meta