anitazha / rpms / ndctl

Forked from rpms/ndctl 2 years ago
Clone

Blame 0016-libndctl-check-for-active-system-ram-before-disablin.patch

Jeff Moyer 2c91dc
From 573f0d46cff15fff2804b3fb444d1e34f482e788 Mon Sep 17 00:00:00 2001
Jeff Moyer 2c91dc
From: Vishal Verma <vishal.l.verma@intel.com>
Jeff Moyer 2c91dc
Date: Tue, 30 Mar 2021 20:54:55 -0600
Jeff Moyer 2c91dc
Subject: [PATCH 016/217] libndctl: check for active system-ram before
Jeff Moyer 2c91dc
 disabling daxctl devices
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
Teach ndctl_namespace_disable_safe() to look at the state of a
Jeff Moyer 2c91dc
daxctl_dev with respect to whether it is active in 'system-ram' mode
Jeff Moyer 2c91dc
before disabling it. This is similar to checking whether a filesystem is
Jeff Moyer 2c91dc
actively mounted on a namespace before disabling it.
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
Without this, libndctl would happily disable a devdax namespace while the
Jeff Moyer 2c91dc
device was active in system-ram mode. If the namespace was subsequently
Jeff Moyer 2c91dc
also destroyed, this would leave the memory without any sort of a
Jeff Moyer 2c91dc
'handle' to perform any subsequent operation on it, and the system would
Jeff Moyer 2c91dc
have to be rebooted to get out of this situation.
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
Reported-by: Chunye Xu <chunye.xu@intel.com>
Jeff Moyer 2c91dc
Cc: Dan Williams <dan.j.williams@intel.com>
Jeff Moyer 2c91dc
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Jeff Moyer 2c91dc
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
Jeff Moyer 2c91dc
---
Jeff Moyer 2c91dc
 ndctl/lib/libndctl.c   | 25 ++++++++++++++++++++++++-
Jeff Moyer 2c91dc
 test/daxctl-devices.sh | 16 ++++++++++++++++
Jeff Moyer 2c91dc
 2 files changed, 40 insertions(+), 1 deletion(-)
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
Jeff Moyer 2c91dc
index 2f6d806..2eda56c 100644
Jeff Moyer 2c91dc
--- a/ndctl/lib/libndctl.c
Jeff Moyer 2c91dc
+++ b/ndctl/lib/libndctl.c
Jeff Moyer 2c91dc
@@ -4593,21 +4593,40 @@ NDCTL_EXPORT int ndctl_namespace_disable_invalidate(struct ndctl_namespace *ndns
Jeff Moyer 2c91dc
 	return ndctl_namespace_disable(ndns);
Jeff Moyer 2c91dc
 }
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
+static int ndctl_dax_has_active_memory(struct ndctl_dax *dax)
Jeff Moyer 2c91dc
+{
Jeff Moyer 2c91dc
+	struct daxctl_region *dax_region;
Jeff Moyer 2c91dc
+	struct daxctl_dev *dax_dev;
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+	dax_region = ndctl_dax_get_daxctl_region(dax);
Jeff Moyer 2c91dc
+	if (!dax_region)
Jeff Moyer 2c91dc
+		return 0;
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+	daxctl_dev_foreach(dax_region, dax_dev)
Jeff Moyer 2c91dc
+		if (daxctl_dev_has_online_memory(dax_dev))
Jeff Moyer 2c91dc
+			return 1;
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+	return 0;
Jeff Moyer 2c91dc
+}
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
 NDCTL_EXPORT int ndctl_namespace_disable_safe(struct ndctl_namespace *ndns)
Jeff Moyer 2c91dc
 {
Jeff Moyer 2c91dc
 	const char *devname = ndctl_namespace_get_devname(ndns);
Jeff Moyer 2c91dc
 	struct ndctl_ctx *ctx = ndctl_namespace_get_ctx(ndns);
Jeff Moyer 2c91dc
 	struct ndctl_pfn *pfn = ndctl_namespace_get_pfn(ndns);
Jeff Moyer 2c91dc
 	struct ndctl_btt *btt = ndctl_namespace_get_btt(ndns);
Jeff Moyer 2c91dc
+	struct ndctl_dax *dax = ndctl_namespace_get_dax(ndns);
Jeff Moyer 2c91dc
 	const char *bdev = NULL;
Jeff Moyer 2c91dc
+	int fd, active = 0;
Jeff Moyer 2c91dc
 	char path[50];
Jeff Moyer 2c91dc
-	int fd;
Jeff Moyer 2c91dc
 	unsigned long long size = ndctl_namespace_get_size(ndns);
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	if (pfn && ndctl_pfn_is_enabled(pfn))
Jeff Moyer 2c91dc
 		bdev = ndctl_pfn_get_block_device(pfn);
Jeff Moyer 2c91dc
 	else if (btt && ndctl_btt_is_enabled(btt))
Jeff Moyer 2c91dc
 		bdev = ndctl_btt_get_block_device(btt);
Jeff Moyer 2c91dc
+	else if (dax && ndctl_dax_is_enabled(dax))
Jeff Moyer 2c91dc
+		active = ndctl_dax_has_active_memory(dax);
Jeff Moyer 2c91dc
 	else if (ndctl_namespace_is_enabled(ndns))
Jeff Moyer 2c91dc
 		bdev = ndctl_namespace_get_block_device(ndns);
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
@@ -4632,6 +4651,10 @@ NDCTL_EXPORT int ndctl_namespace_disable_safe(struct ndctl_namespace *ndns)
Jeff Moyer 2c91dc
 					devname, bdev, strerror(errno));
Jeff Moyer 2c91dc
 			return -errno;
Jeff Moyer 2c91dc
 		}
Jeff Moyer 2c91dc
+	} else if (active) {
Jeff Moyer 2c91dc
+		dbg(ctx, "%s: active as system-ram, refusing to disable\n",
Jeff Moyer 2c91dc
+				devname);
Jeff Moyer 2c91dc
+		return -EBUSY;
Jeff Moyer 2c91dc
 	} else {
Jeff Moyer 2c91dc
 		if (size == 0)
Jeff Moyer 2c91dc
 			/* No disable necessary due to no capacity allocated */
Jeff Moyer 2c91dc
diff --git a/test/daxctl-devices.sh b/test/daxctl-devices.sh
Jeff Moyer 2c91dc
index eed5906..56c9691 100755
Jeff Moyer 2c91dc
--- a/test/daxctl-devices.sh
Jeff Moyer 2c91dc
+++ b/test/daxctl-devices.sh
Jeff Moyer 2c91dc
@@ -105,6 +105,22 @@ daxctl_test()
Jeff Moyer 2c91dc
 	"$DAXCTL" reconfigure-device -f -m devdax "$daxdev"
Jeff Moyer 2c91dc
 	[[ $(daxctl_get_mode "$daxdev") == "devdax" ]]
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
+	# fail 'ndctl-disable-namespace' while the devdax namespace is active
Jeff Moyer 2c91dc
+	# as system-ram. If this test fails, a reboot will be required to
Jeff Moyer 2c91dc
+	# recover from the resulting state.
Jeff Moyer 2c91dc
+	test -n "$testdev"
Jeff Moyer 2c91dc
+	"$DAXCTL" reconfigure-device -m system-ram "$daxdev"
Jeff Moyer 2c91dc
+	[[ $(daxctl_get_mode "$daxdev") == "system-ram" ]]
Jeff Moyer 2c91dc
+	if ! "$NDCTL" disable-namespace "$testdev"; then
Jeff Moyer 2c91dc
+		echo "disable-namespace failed as expected"
Jeff Moyer 2c91dc
+	else
Jeff Moyer 2c91dc
+		echo "disable-namespace succeded, expected failure"
Jeff Moyer 2c91dc
+		echo "reboot required to recover from this state"
Jeff Moyer 2c91dc
+		return 1
Jeff Moyer 2c91dc
+	fi
Jeff Moyer 2c91dc
+	"$DAXCTL" reconfigure-device -f -m devdax "$daxdev"
Jeff Moyer 2c91dc
+	[[ $(daxctl_get_mode "$daxdev") == "devdax" ]]
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
 	# this tests for reconfiguration failure if an online-policy is set
Jeff Moyer 2c91dc
 	set_online_policy
Jeff Moyer 2c91dc
 	: "This command is expected to fail:"
Jeff Moyer 2c91dc
-- 
Jeff Moyer 2c91dc
2.27.0
Jeff Moyer 2c91dc