anitazha / rpms / ndctl

Forked from rpms/ndctl 2 years ago
Clone

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

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