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

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