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

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