Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=23.83.209.135 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796226286; b=akfjZOIGXN5hi9Vc+4H4wQjWxhHW2V3PIHY3zBAUjhbal606Vcpa6F/Ozh0dZUdivugH faJsDtUXGztxbTlxsZAak15scHX3Z5BHHbWvvnHCbyzNX/IVoP4UlUnGBaXOKAezUIekA HNXI9r6u+LQZb9cb3hbc557qWUsAzAOOz4HZkJ8XMEvT0jZ6Am4EoNdJ2a5ClF39QP+my T6G3AUNVJofFc2Jfo5B0mpr4oAevNjGNDzFjR5JVmyBHzv/fHR5RaQ2V5k7yGmVoIYlWW aBzHBLLSLf5o7+9pSsEbsnHV6H0LSvK7rSHsg+FgVGutu1XjPB0fyMY3WnwrRA5iyaA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796226286; h=DMARC-Filter:X-Sender-Id:Received:Received:X-Sender-Id:X-MC-Relay: X-MailChannels-SenderId:X-MailChannels-Auth-Id:X-Trade-Drop: X-MC-Loop-Signature:X-MC-Ingress-Time:Received:Received: DKIM-Signature:From:To:Cc:Subject:Date:Message-Id:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=Mg/1mXvGIlqTnAasxdS45PedONhG4ukFU1tsxjUxFR4=; b=SsBy0xnVGxBADKYrvsM0bKMJwIjI6eUcsis63/eCp2Oa9hL/yuxhdEyvrY6kk3pq0pot JeqrdS/FGagB/LMHXwsCygjy0Yt18Pg6ptOPJSdSFklnUCNRB05ADi96poOTSnvGkQRwj uemAHVtfxLPo/UaZ0aN6ZMrNL/sdeHUkJ5XbWyEu4G7WUdo5Jd8d+QB5sn6MawnnBJqpH 0THWQUhiW6SRGRxvwLgHQ2OGRSKpd29JehYlxzS3bkLm4CO3Ajvin6FjflFQJZLks3MwU R9Wpn1C/N/dufgHJgQshrTtJnxpN9BmFqXQ0CK/aOpHtYyjPq9a9yVhAjPVX9DgnH6Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=stgolabs.net; spf=fail smtp.mailfrom=stgolabs.net; dkim=pass header.d=stgolabs.net header.i=@stgolabs.net header.a=rsa-sha256 header.s=dreamhost header.b=KGeQ5KAY; arc=none smtp.remote-ip=23.83.209.135 Received: from olivedrab.birch.relay.mailchannels.net (olivedrab.birch.relay.mailchannels.net [23.83.209.135]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99EC7606A323 for ; Wed, 2 Dec 2026 15:44:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 99EC7606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=stgolabs.net Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=stgolabs.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=stgolabs.net header.i=@stgolabs.net header.a=rsa-sha256 header.s=dreamhost header.b=KGeQ5KAY X-Sender-Id: dreamhost|x-authsender|dave@stgolabs.net Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id ACDFE4C0BE3; Wed, 02 Dec 2026 15:44:39 +0000 (UTC) Received: from pdx1-sub0-mail-a229.dreamhost.com (100-96-7-102.trex-nlb.outbound.svc.cluster.local [100.96.7.102]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 770B74C02F6; Wed, 02 Dec 2026 15:44:39 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|dave@stgolabs.net X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|dave@stgolabs.net X-MailChannels-Auth-Id: dreamhost X-Trade-Drop: 4d53ab9e0120e35a_1796226279646_919759316 X-MC-Loop-Signature: 1796226279646:433117012 X-MC-Ingress-Time: 1796226279646 Received: from pdx1-sub0-mail-a229.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.96.7.102 (trex/8.0.2); Wed, 02 Dec 2026 15:44:39 +0000 Received: from offworld.tailf5af7a.ts.net (unknown [76.167.199.67]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: dave@stgolabs.net) by pdx1-sub0-mail-a229.dreamhost.com (Postfix) with ESMTPSA id 4jfhs259gfzQ1; Wed, 2 Dec 2026 07:44:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stgolabs.net; s=dreamhost; t=1796226279; bh=Mg/1mXvGIlqTnAasxdS45PedONhG4ukFU1tsxjUxFR4=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=KGeQ5KAY6RBwhXoGMW9T/Xg21x3zWKaTGcCDPFrybFA8o8bQqLgwr/36iAMELWHOK EopBhyn+lfSsus9bkNNFK7w8mieubJX0gPQQ7tq7tgVmOn3x8RJAhxdSPUVYy3VKyE EZIB2bBGMU/n4SPKjedpz9YdmeJoc7CyaSbNBI6n+b0FWEUwwS7c8uZlEmoR8fXuJ2 1hYPc1Ek/wF6/sau2LSHdPsToh9chpOQmYJLtIyDsZv2X4EVLdvGIwsxrowxBmDcpH Y+L2VUNAgVUzKLFAz8ZJOOPiXGfPmWRleOPN24OMmxnowkiGlYb4CBXXveUk9CE2IA EY2jGVSOsxfbg== From: Davidlohr Bueso To: dave.jiang@intel.com Cc: jic23@kernel.org, alison.schofield@intel.com, ming.li@zohomail.com, icheng@nvidia.com, benjamin.cheatham@amd.com, unsung0317@gmail.com, linux-cxl@vger.kernel.org, dave@stgolabs.net Subject: [PATCH v2 08/10] cxl/mbox: Allow userspace background commands Date: Wed, 2 Dec 2026 07:44:02 -0800 Message-Id: <20261202154404.4016305-9-dave@stgolabs.net> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20261202154404.4016305-1-dave@stgolabs.net> References: <20261202154404.4016305-1-dave@stgolabs.net> X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit CXL 3.1 introduced the ability to request that the current on-going background command be aborted. As such userspace may be allowed to monopolize the background operation capability if there are no other bg commands that want to run. The policy here are as follows: 1. Give bg-capable commands from the kernel higher priority than those from userspace (albeit currently no command). Therefore a userspace on-going command can be canceled so a kernel one may run instead. 2. Userspace bg commands may cancel each other on demand. It is not up to the kernel to put any policies in place here. 3. A kernel background command is never aborted. This includes one that timed out while being polled for synchronously and is still running: it is no longer tracked, and incoming bg-capable commands get -EBUSY until hardware is done with it. The context of doing the cancellation request is the same as the new incoming command, and will always hold the mbox_mutex, guaranteeing that any successful cancel does not race with a third thread coming in and stealing the effort. Signed-off-by: Davidlohr Bueso --- drivers/cxl/core/mbox.c | 9 ++- drivers/cxl/cxlmem.h | 4 ++ drivers/cxl/pci.c | 126 +++++++++++++++++++++++++++++++++------- include/cxl/mailbox.h | 2 + 4 files changed, 117 insertions(+), 24 deletions(-) diff --git a/drivers/cxl/core/mbox.c b/drivers/cxl/core/mbox.c index 32af282d0fb6..017e1b9319a3 100644 --- a/drivers/cxl/core/mbox.c +++ b/drivers/cxl/core/mbox.c @@ -421,6 +421,7 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox_cmd, *mbox_cmd = (struct cxl_mbox_cmd) { .opcode = opcode, .size_in = in_size, + .user = true, }; if (in_size) { @@ -824,8 +825,12 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel) int enabled = 0; if (cmd) { - set_bit(cmd->info.id, cxl_mbox->enabled_cmds); - enabled++; + /* only enable uapi bgcmds that may be canceled */ + if (!(effect & CXL_CEL_BACKGROUND_OPERATION) || + (effect & CXL_CEL_REQ_ABORT_BACKGROUND_SUPPORTED)) { + set_bit(cmd->info.id, cxl_mbox->enabled_cmds); + enabled++; + } } enabled += check_features_opcodes(opcode, &ro_cmds, diff --git a/drivers/cxl/cxlmem.h b/drivers/cxl/cxlmem.h index 1638798bceac..70037e526e01 100644 --- a/drivers/cxl/cxlmem.h +++ b/drivers/cxl/cxlmem.h @@ -427,12 +427,14 @@ struct cxl_security_state { * @poll_dwork: polling background work item * @poll_tmo_secs: polling timeout * @opcode: async running background command + * @user: whether or not operation was initialized by userspace (uapi) */ struct cxl_background_state { DECLARE_BITMAP(enabled_cmds, CXL_BG_ENABLED_MAX); struct delayed_work poll_dwork; int poll_tmo_secs; int opcode; + bool user; }; #define CXL_MAX_DC_PARTITIONS 8 @@ -508,6 +510,7 @@ to_cxl_memdev_state(struct cxl_dev_state *cxlds) enum cxl_opcode { CXL_MBOX_OP_INVALID = 0x0000, CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID, + CXL_MBOX_OP_REQUEST_ABORT_BG_OP = 0x0005, CXL_MBOX_OP_GET_EVENT_RECORD = 0x0100, CXL_MBOX_OP_CLEAR_EVENT_RECORD = 0x0101, CXL_MBOX_OP_GET_EVT_INT_POLICY = 0x0102, @@ -579,6 +582,7 @@ struct cxl_mbox_get_supported_logs { * CXL 4.0 Section 8.2.10.5.2.1; Table 8-254 */ #define CXL_CEL_BACKGROUND_OPERATION BIT(6) +#define CXL_CEL_REQ_ABORT_BACKGROUND_SUPPORTED BIT(8) struct cxl_cel_entry { __le16 opcode; diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c index ef4853296e86..1cacc9e90a31 100644 --- a/drivers/cxl/pci.c +++ b/drivers/cxl/pci.c @@ -134,7 +134,7 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id) reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET); opcode = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK, reg); - if (opcode == CXL_MBOX_OP_SANITIZE) { + if (opcode == CXL_MBOX_OP_SANITIZE || mds->bg.user) { mutex_lock(&cxl_mbox->mbox_mutex); if (mds->bg.opcode) mod_delayed_work(system_percpu_wq, @@ -149,7 +149,28 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id) } /* - * Sanitization operation polling mode. + * Hardware is done with the async background operation, one way or + * another: let go of it. + */ +static void cxl_mbox_bg_end(struct cxl_memdev_state *mds) +{ + struct cxl_dev_state *cxlds = &mds->cxlds; + + lockdep_assert_held(&cxlds->cxl_mbox.mbox_mutex); + + mds->bg.poll_tmo_secs = 0; + if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE && + mds->security.sanitize_node) + sysfs_notify_dirent(mds->security.sanitize_node); + + dev_dbg(cxlds->dev, "Mailbox background operation (0x%04x) ended\n", + mds->bg.opcode); + mds->bg.opcode = 0; + mds->bg.user = false; +} + +/* + * Async background operation polling mode. */ static void cxl_mbox_bgpoll_work(struct work_struct *work) { @@ -159,16 +180,10 @@ static void cxl_mbox_bgpoll_work(struct work_struct *work) struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox; mutex_lock(&cxl_mbox->mbox_mutex); - if (cxl_mbox_background_done(cxlds)) { - mds->bg.poll_tmo_secs = 0; - if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE && - mds->security.sanitize_node) - sysfs_notify_dirent(mds->security.sanitize_node); - - dev_dbg(cxlds->dev, - "Mailbox background operation (0x%04x) ended\n", - mds->bg.opcode); - mds->bg.opcode = 0; + if (!mds->bg.opcode) { + /* cxl_try_to_cancel_background() got here first */ + } else if (cxl_mbox_background_done(cxlds)) { + cxl_mbox_bg_end(mds); } else { int timeout = min(15 * 60, mds->bg.poll_tmo_secs + 10); @@ -261,13 +276,26 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, } /* - * With sanitize polling, hardware might be done and the poller still - * not be in sync. Ensure no new command comes in until so. Keep the - * hardware semantics and only allow device health status. + * With async bg polling, hardware might be done and the poller still + * not be in sync. At the very least, prevent any incoming bg-capable + * commands during this window. */ - if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE) { + switch (mds->bg.opcode) { + case 0: + /* no async background operation to stay in sync with */ + break; + case CXL_MBOX_OP_SANITIZE: + /* + * Ensure no new command comes in until so. Keep the + * hardware semantics and only allow device health status. + */ if (mbox_cmd->opcode != CXL_MBOX_OP_GET_HEALTH_INFO) return -EBUSY; + break; + default: + if (cxl_is_background_cmd(mds, mbox_cmd->opcode)) + return -EBUSY; + break; } cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK, @@ -309,11 +337,11 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, mbox_cmd->opcode); /* - * Sanitization is a special case which monopolizes the device - * and cannot be timesliced. Handle asynchronously instead, - * and allow userspace to poll(2) for completion. + * Special cases which monopolize the device and cannot be + * timesliced. Handle asynchronously instead. */ - if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE) { + if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE || + mbox_cmd->user) { int timeout; if (mds->bg.opcode) @@ -323,6 +351,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, timeout = 1; mds->bg.poll_tmo_secs = timeout; mds->bg.opcode = mbox_cmd->opcode; + mds->bg.user = mbox_cmd->user; schedule_delayed_work(&mds->bg.poll_dwork, timeout * HZ); } else { @@ -361,6 +390,58 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, return 0; } +/* + * Attempt to cancel an on-going background command. Return true guarantees + * that no such operations are running and any leftover context is gone. + */ +static bool cxl_try_to_cancel_background(struct cxl_mailbox *cxl_mbox) +{ + int rc; + struct cxl_dev_state *cxlds = mbox_to_cxlds(cxl_mbox); + struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds); + struct device *dev = cxlds->dev; + struct cxl_mbox_cmd cmd = { + .opcode = CXL_MBOX_OP_REQUEST_ABORT_BG_OP, + }; + + lockdep_assert_held(&cxl_mbox->mbox_mutex); + + /* everything is finished, new bgcmd may proceed */ + if (cxl_mbox_background_done(cxlds)) + goto done; + if (!mds->bg.user) { + /* + * Policy is around userpace being abortable: + * incoming user bgcmd -> abort on-going user bgcmd + * incoming kernel bgcmd -> abort on-going user bgcmd + */ + return false; + } + + rc = __cxl_pci_mbox_send_cmd(cxl_mbox, &cmd); + if (rc) { + dev_dbg(dev, "Failed to send abort request : %d\n", rc); + return false; + } + + if (!cxl_mbox_background_done(cxlds)) + return false; + + dev_dbg(cxlds->dev, "Mailbox background operation aborted\n"); +done: + if (mds->bg.opcode) { + /* + * Cleanup on behalf of the poller. It may well be blocked + * on the mbox_mutex right now and so cannot be waited for, + * it copes with finding nothing left to do. + */ + cancel_delayed_work(&mds->bg.poll_dwork); + cxl_mbox_bg_end(mds); + } + + return true; +} + static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, struct cxl_mbox_cmd *cmd) { @@ -376,7 +457,8 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, * between while polling. */ if (cxl_is_background_cmd(mds, cmd->opcode) && - atomic_read_acquire(&cxl_mbox->poll_bgop)) + (atomic_read_acquire(&cxl_mbox->poll_bgop) || + !cxl_try_to_cancel_background(cxl_mbox))) return -EBUSY; rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd); @@ -390,7 +472,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, * operations are timesliced in accordance with the nature of the * command. */ - if (cmd->opcode != CXL_MBOX_OP_SANITIZE) { + if (cmd->opcode != CXL_MBOX_OP_SANITIZE && !cmd->user) { int i, timeout; u64 bg_status_reg; diff --git a/include/cxl/mailbox.h b/include/cxl/mailbox.h index 691c2ae21ef5..6bc2465a6b76 100644 --- a/include/cxl/mailbox.h +++ b/include/cxl/mailbox.h @@ -19,6 +19,7 @@ * variable sized output commands, it tells the exact number of bytes * written. * @min_out: (input) internal command output payload size validation + * @user: (input) Whether or not command was submitted from userspace (uapi). * @poll_count: (input) Number of timeouts to attempt. * @poll_interval_ms: (input) Time between mailbox background command polling * interval timeouts. @@ -37,6 +38,7 @@ struct cxl_mbox_cmd { size_t size_in; size_t size_out; size_t min_out; + bool user; int poll_count; int poll_interval_ms; u16 return_code; -- 2.39.5