From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) (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 5D5D2381701 for ; Wed, 14 Jan 2026 08:52:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768380774; cv=none; b=mMp5puY5hxCCY9KPbx46VAkW/TQGcJxDIAelPI9bNTguToNEcc+C5cv0RIw0qL3KvTsDwxVWajcL8bOIgAc6rkrRYM9gwYfsksb/quPp6VDaOTdo2LwmCkiU0vVi/WAj8chRpgJgAsyMZgizyU9BDl5MPoeyqcnJdtU4R73n6QI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768380774; c=relaxed/simple; bh=IRkGW/+6U9xZlWhzp6teA03oy5t+nqHjZEeiuztKzEo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OIsxKEXFAIfdSmlcP5v2vaEYHRt9fu2qxlIsGefJ+zjrKJZ/ak98btFj/73u9ybrr66/kRNPW520Y3mUiRuu3OTW2d06yZww8/vC5WI5fMhXcGtJmXAdnv4z5VDbuzHa0V6ud1EP6EkswMmuXRytOEeED9/mgA5zGMPKxjMHFgo= 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=s9E9NNP7; arc=none smtp.client-ip=209.85.160.182 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="s9E9NNP7" Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-5014db8e268so3047351cf.1 for ; Wed, 14 Jan 2026 00:52:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1768380764; x=1768985564; 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=AkgGAvtlp4w6GbvGEuWsv0IzEAPVl0Fov3QmaIKpKpw=; b=s9E9NNP7Ycan0Xem8faGK8tcnL0Ikl8JBadKBouEvjvh+jpI4YHE0y51EA/Z4ll/ys QQzxhqTj+q/aQf1UtL2bz6HQGR2YkvGj5axxjx4cNjkF4jyOkl4y6xitChcvoaMNYQWf dRNCx3+VL1lBeSEJq/pJK+xFpN85ZrmYqV/eOS9/9xLkJGjHOw+Z0munF7qbBqgW545M pFuf5FcXkhlFNPQg5fVXINWQ/rM2vnI0/mHexVl2R4HFIpV0OnYbedt2r00BpVONGS8Y /D1KBwQFbbAObqpdCky32G8KNnsPCbQERV9bWkqFtgihbOVXVThJv5C9DuVgRAM4p6Wi M+ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768380764; x=1768985564; 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=AkgGAvtlp4w6GbvGEuWsv0IzEAPVl0Fov3QmaIKpKpw=; b=hQw/F81JVqdVnhygv9bb02Ov2c38cb/s7UVO4tOGvmlnas/ocsWLU5z7fm1y0xpmSR Asdj4E0MzkZjX4PQ45eBfcf6GGkG949NjhhrgSEuzwVzbFJykaQNFSt1tNGerSLOvY/z LETc+BFaZeVJz2oDvIgLdZnAQAfUcRb9hAIy+O6M1jhSje2xOm0wQJJP6OIXhd/UI+6C GV6aNuIRaIciEXKYHqZSepqtbjhnmR3aLqU2Al3ra1u6eFHbICCQhtrbPaTTu5qebBvC 92UouZnD0cgOpkzZBZzPyB4aFZd0l0Wc5pIEl6EsMcfYoycbuhyFKhEfxwJKVAuLXlrb sv1Q== X-Forwarded-Encrypted: i=1; AJvYcCXsytCwuDcULLG981cw3groiAE7j6VKlWzYsEXe81fiqJ7En7rrDFrAyXwbDht4atvDz2SJNHy8NcW4ofM=@vger.kernel.org X-Gm-Message-State: AOJu0Yz627QelOw8AZTOhxuir7TK1DfV+S0POi2cTzfqmbde5pRMChkE PI862GXkNUs3k3qnk0WbdA6RqO0wXt+Ymnrm0v3K0Ryp0wAiPkn91CngtnQIHxe9NZw= X-Gm-Gg: AY/fxX4Je346ATOnTORUexne34dhYWUebhABJB35xOWkYSCps+y7fKat/MImr3Anp7j GyimUPhR7W4DciYlyaGr+y1yZmhWljyf3FDU1fixB/GyHq9TO0nB3eYX9pSNFtsNkUvd1XKtZYU hprud+8t5qJJreja2IVXyGqSshdujZTdOaWjawnS6zxJ7sNZS+tXm6ipIDn296rsWQ/oANw30ML l0F3w0gBatkBAxiY9sLoSrN9+eB/6VWup4vDW/963fnKRr+hjTQxu9bgiyani3jVM50wG2HX8M0 8433rDPoRIiBNjx3QDKvf6Bk8z87oI6+EICWMq4gYhhgUaWFweQOT6QSM2fvggYWT79TbKpLkF9 iiMk2l5AZ5KQvewiwQ9/gwvK+9vvj013BC6cYmnTt4tW2+gUTsTcmNfBwMtamTaT4XPSdDwro5j TqzHrk0QuLBwh4Tt1iNMZWmJyi8i6l/sZ6WCYgDVtcFG3dxYdffz9T8+M0azNNUVMPQ7oX4zdGY Ko= X-Received: by 2002:ac8:7dc1:0:b0:4ee:ccd:7215 with SMTP id d75a77b69052e-501481206a5mr26400211cf.0.1768380764400; Wed, 14 Jan 2026 00:52:44 -0800 (PST) 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 d75a77b69052e-50148df8759sm10131931cf.10.2026.01.14.00.52.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jan 2026 00:52:44 -0800 (PST) From: Gregory Price To: linux-mm@kvack.org Cc: linux-cxl@vger.kernel.org, nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, kernel-team@meta.com, dan.j.williams@intel.com, vishal.l.verma@intel.com, dave.jiang@intel.com, david@kernel.org, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com, osalvador@suse.de, akpm@linux-foundation.org Subject: [PATCH 6/8] dax/kmem: add online/offline helper functions Date: Wed, 14 Jan 2026 03:51:58 -0500 Message-ID: <20260114085201.3222597-7-gourry@gourry.net> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260114085201.3222597-1-gourry@gourry.net> References: <20260114085201.3222597-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 Add helper functions for onlining and offlining memory ranges: - dax_kmem_do_online(): online memory with specified type (MMOP_ONLINE or MMOP_ONLINE_MOVABLE) using online_memory_range() - dax_kmem_do_offline(): offline memory using offline_memory() These helpers use the memory hotplug APIs from the memory_hotplug refactoring and will be used by the upcoming sysfs interface to allow userspace control over memory state transitions. No functional change as these helpers are not called yet. Signed-off-by: Gregory Price --- drivers/dax/kmem.c | 103 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index 5225f2bf0b2a..30429f2d5a67 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -190,6 +190,109 @@ static int dax_kmem_do_hotremove(struct dev_dax *dev_dax, } #endif /* CONFIG_MEMORY_HOTREMOVE */ +/** + * dax_kmem_do_online - online memory blocks for dax kmem device + * @dev_dax: the dev_dax instance + * @data: the dax_kmem_data structure with resource tracking + * @online_type: MMOP_ONLINE or MMOP_ONLINE_MOVABLE + * + * Onlines all ranges in the dev_dax region with the specified online type. + * On partial failure, previously onlined ranges are rolled back to offline. + * + * Returns 0 on success, negative error code on failure. + */ +static int dax_kmem_do_online(struct dev_dax *dev_dax, + struct dax_kmem_data *data, int online_type) +{ + int i, j, rc; + + for (i = 0; i < dev_dax->nr_range; i++) { + struct range range; + + rc = dax_kmem_range(dev_dax, i, &range); + if (rc) + continue; + + if (!data->res[i]) + continue; + + rc = online_memory_range(range.start, range_len(&range), + online_type); + if (rc) + goto rollback; + } + + return 0; + +rollback: + /* Rollback previously onlined ranges */ + for (j = 0; j < i; j++) { + struct range range; + + if (dax_kmem_range(dev_dax, j, &range)) + continue; + + if (!data->res[j]) + continue; + + /* Best effort rollback - ignore failures */ + offline_memory(range.start, range_len(&range)); + } + return rc; +} + +/** + * dax_kmem_do_offline - offline memory blocks for dax kmem device + * @dev_dax: the dev_dax instance + * @data: the dax_kmem_data structure with resource tracking + * + * Offlines all ranges in the dev_dax region. + * On partial failure, previously offlined ranges are rolled back to online. + * + * Returns 0 on success, negative error code on failure. + */ +static int dax_kmem_do_offline(struct dev_dax *dev_dax, + struct dax_kmem_data *data) +{ + int i, j, rc; + + for (i = 0; i < dev_dax->nr_range; i++) { + struct range range; + + rc = dax_kmem_range(dev_dax, i, &range); + if (rc) + continue; + + if (!data->res[i]) + continue; + + rc = offline_memory(range.start, range_len(&range)); + if (rc) + goto rollback; + } + + return 0; + +rollback: + /* + * Rollback previously offlined ranges. Use MMOP_ONLINE as a safe + * default - the original online type is not tracked per-range. + */ + for (j = 0; j < i; j++) { + struct range range; + + if (dax_kmem_range(dev_dax, j, &range)) + continue; + + if (!data->res[j]) + continue; + + /* Best effort rollback - ignore failures */ + online_memory_range(range.start, range_len(&range), MMOP_ONLINE); + } + return rc; +} + static int dev_dax_kmem_probe(struct dev_dax *dev_dax) { struct device *dev = &dev_dax->dev; -- 2.52.0