diff mbox series

[2/2] cxl: Add check for regions before disabling memdev

Message ID 169566964012.3704458.3768477727985056846.stgit@djiang5-mobl3
State New, archived
Headers show
Series [1/2] cxl: Save the decoder committed state | expand

Commit Message

Dave Jiang Sept. 25, 2023, 7:20 p.m. UTC
Add a check for memdev disable to see if there are active regions present
before disabling the device. This is necessary now regions are present to
fulfill the TODO that was left there. The best way to determine if a
region is active is to see if there are decoders enabled for the mem
device. This is also best effort as the state is only a snapshot the
kernel provides and is not atomic WRT the memdev disable operation. The
expectation is the admin issuing the command has full control of the mem
device and there are no other agents also attempt to control the device.

Signed-off-by: Dave Jiang <dave.jiang@intel.com>
---
 cxl/memdev.c |   29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/cxl/memdev.c b/cxl/memdev.c
index f6a2d3f1fdca..644369321649 100644
--- a/cxl/memdev.c
+++ b/cxl/memdev.c
@@ -373,11 +373,36 @@  static int action_free_dpa(struct cxl_memdev *memdev,
 
 static int action_disable(struct cxl_memdev *memdev, struct action_context *actx)
 {
+	struct cxl_decoder *decoder;
+	struct cxl_endpoint *ep;
+	bool committed = false;
+	struct cxl_port *port;
+
 	if (!cxl_memdev_is_enabled(memdev))
 		return 0;
 
-	if (!param.force) {
-		/* TODO: actually detect rather than assume active */
+	ep = cxl_memdev_get_endpoint(memdev);
+	if (!ep)
+		return -ENODEV;
+
+	port = cxl_endpoint_get_port(ep);
+	if (!port)
+		return -ENODEV;
+
+	/*
+	 * Look for a committed decoder, which indicates that the region the
+	 * memdev belongs to is active. This is best effort as the decoder
+	 * state is pulled from sysfs and not atomic. The caller should be in
+	 * control of the device to prevent state changes for the decoder.
+	 */
+	cxl_decoder_foreach(port, decoder) {
+		if (cxl_decoder_is_committed(decoder)) {
+			committed = true;
+			break;
+		}
+	}
+
+	if (committed && !param.force) {
 		log_err(&ml, "%s is part of an active region\n",
 			cxl_memdev_get_devname(memdev));
 		return -EBUSY;