Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=23.83.222.55 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796226284; b=CbjEYEM4o1pDfHrUXojLneEnyloxMn7tjrNMfYjvOaxcRU5PldrLgWWxW9NrSMm0m8xa qg+81cNkI1cHMpwYCTPilYCYKG91p6a9vm/f6KKcn82SNS33TYXhYUiDV06nLd5OyDVka ilhuCgQImHBPB3ABKaffxIIckhJwD/NjhxRMK9dWue8Hs+KA86Qp0cigr/Rhwbb62sg9i i+8WqgI94nrR5h9zoEGR8XNcbHMsUPLapQGqTUpT5KrX/ZMa+XJxOGiTeDSFHA2xMY0ET gcsShEGj1NOxoWqIUwJM/m46Km7cfLez+kFRVBZKeXIg0CagntN8gPOoFMs7doesuNg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796226284; h=DMARC-Filter:X-Sender-Id:Received:Received:X-Sender-Id:X-MC-Relay: X-MailChannels-SenderId:X-MailChannels-Auth-Id:X-Imminent-Stretch: 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=6sYiwDgEYS2+jN+2xE+h+yDlrQntKytlhcVaj8y8QX4=; b=SgTWFc62bC5miJqiS7mEC4a4JdDOEZsGmpwqJ8VaJBCVSp7v9B4EUCZlYaJ+718GgIBN /loywtc9yQygmzl8He9au7IOc6GvdIlCV3OzkStdfmeG0tDIDkf1wsQCdPzwN0Vh9CyLP JgFindkG4Sx4FoVXyfrKcbNMeblslhpz3AoifAneMRiStS8dxImfJ09WAKLPt2d62H3rV j/OX0kkdncv4IqYvp6hHjgaRDS1DgCPMJ1FVQZATcYVSD4KZFSP0YKUkAwnoNNl7jI4jN QTDur8Z2bUTv2GsXD5DuRMSfpdHCQNuQbuA0vMJ+Sgd3HkttWiqioacfdPgI6qbebSQ== 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=q6jBJrwp; arc=none smtp.remote-ip=23.83.222.55 Received: from eastern.ash.relay.mailchannels.net (eastern.ash.relay.mailchannels.net [23.83.222.55]) (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 F0C43606A323 for ; Wed, 2 Dec 2026 15:44:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F0C43606A323 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=q6jBJrwp 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 79AD49425EA; Wed, 02 Dec 2026 15:44:37 +0000 (UTC) Received: from pdx1-sub0-mail-a229.dreamhost.com (100-96-12-11.trex-nlb.outbound.svc.cluster.local [100.96.12.11]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 46E5B940470; Wed, 02 Dec 2026 15:44:37 +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-Imminent-Stretch: 25fbe1fa23de99dc_1796226277426_979348524 X-MC-Loop-Signature: 1796226277426:1609176119 X-MC-Ingress-Time: 1796226277426 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.12.11 (trex/8.0.2); Wed, 02 Dec 2026 15:44:37 +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 4jfhs03srDzTC; Wed, 2 Dec 2026 07:44:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stgolabs.net; s=dreamhost; t=1796226277; bh=6sYiwDgEYS2+jN+2xE+h+yDlrQntKytlhcVaj8y8QX4=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=q6jBJrwpZ+TBdsXSS6wdrhU9mmvjA4jbjN8QQ8H3EEeY5cfiUO5EJpX09Bj5kHAGr gqVzw2qc5unoGJv5KDPBnSWVaOKUCLzJW0PhV6Hdki/lHSZ9JnSbQvnqvmlEgQ4MhC bRNA3fx2hmKErVsYlC5J2sXLw7eph8vEbHHIorAwfAYFKWIUcEhmjrxCYkvaskuLb+ cGWxK0bsTVC7HS0mtCnHrGKr/S5LXKNcK8xzW9EMf0kqJ2e+oPXatp0mB8Y3GuNQTd x9YEV7q81kca4gcg0UvQjU5U5bK5NGhTm03/FoAM18xFDp5JJWpRQ/8U6E70E+LBJU 5YITXIqmK3PPA== 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 05/10] cxl/pci: Lockless background synchronous polling Date: Wed, 2 Dec 2026 07:43:59 -0800 Message-Id: <20261202154404.4016305-6-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 For timesliced background commands we rely on holding the mbox_mutex throughout the duration of the operation. This causes other incoming commands to queue up behind, and interleaving executions while the background command is timesliced by the user. However, in order to support the mbox request cancel background operation command, the lock will need to be available to actually perform the request. Semantically this allows other commands to many times be at the mercy of hardware returning the respective error. Potentially users would be exposed to changes in the form of errors instead of commands taking longer to run - but this is not foreseen as a problem. In order to not loose sync with the hardware, introduce a mailbox atomic that blocks any other incoming bg operations while the driver is still polling (synchronously) on the current one. Signed-off-by: Davidlohr Bueso --- drivers/cxl/core/mbox.c | 1 + drivers/cxl/pci.c | 136 +++++++++++++++++++++++++--------------- include/cxl/mailbox.h | 2 + 3 files changed, 90 insertions(+), 49 deletions(-) diff --git a/drivers/cxl/core/mbox.c b/drivers/cxl/core/mbox.c index 41d25880173c..32af282d0fb6 100644 --- a/drivers/cxl/core/mbox.c +++ b/drivers/cxl/core/mbox.c @@ -1792,6 +1792,7 @@ int cxl_mailbox_init(struct cxl_mailbox *cxl_mbox, struct device *host) cxl_mbox->host = host; mutex_init(&cxl_mbox->mbox_mutex); mutex_init(&cxl_mbox->feat_mutex); + atomic_set(&cxl_mbox->poll_bgop, 0); rcuwait_init(&cxl_mbox->mbox_wait); return 0; diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c index 1bc28ad17200..8cb9c5e48489 100644 --- a/drivers/cxl/pci.c +++ b/drivers/cxl/pci.c @@ -141,7 +141,7 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id) &mds->bg.poll_dwork, 0); mutex_unlock(&cxl_mbox->mbox_mutex); } else { - /* short-circuit the wait in __cxl_pci_mbox_send_cmd() */ + /* short-circuit the wait in cxl_pci_mbox_send() */ rcuwait_wake_up(&cxl_mbox->mbox_wait); } @@ -178,6 +178,25 @@ static void cxl_mbox_bgpoll_work(struct work_struct *work) mutex_unlock(&cxl_mbox->mbox_mutex); } +static inline bool cxl_is_background_cmd(struct cxl_memdev_state *mds, + u16 opcode) +{ + switch (opcode) { + case CXL_MBOX_OP_TRANSFER_FW: + return test_bit(CXL_BG_ENABLED_TRANSFER_FW, mds->bg.enabled_cmds); + case CXL_MBOX_OP_ACTIVATE_FW: + return test_bit(CXL_BG_ENABLED_ACTIVATE_FW, mds->bg.enabled_cmds); + case CXL_MBOX_OP_DO_MAINTENANCE: + return test_bit(CXL_BG_ENABLED_DO_MAINTENANCE, mds->bg.enabled_cmds); + case CXL_MBOX_OP_SCAN_MEDIA: + return test_bit(CXL_BG_ENABLED_SCAN_MEDIA, mds->bg.enabled_cmds); + case CXL_MBOX_OP_SANITIZE: + return test_bit(CXL_BG_ENABLED_SANITIZE, mds->bg.enabled_cmds); + default: + return false; + } +} + /** * __cxl_pci_mbox_send_cmd() - Execute a mailbox command * @cxl_mbox: CXL mailbox context @@ -285,23 +304,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, mbox_cmd->return_code = FIELD_GET(CXLDEV_MBOX_STATUS_RET_CODE_MASK, status_reg); - /* - * Handle the background command in a synchronous manner. - * - * All other mailbox commands will serialize/queue on the mbox_mutex, - * which we currently hold. Furthermore this also guarantees that - * cxl_mbox_background_complete() checks are safe amongst each other, - * in that no new bg operation can occur in between. - * - * Background operations are timesliced in accordance with the nature - * of the command. In the event of timeout, the mailbox state is - * indeterminate until the next successful command submission and the - * driver can get back in sync with the hardware state. - */ if (mbox_cmd->return_code == CXL_MBOX_CMD_RC_BACKGROUND) { - u64 bg_status_reg; - int i, timeout; - dev_dbg(dev, "Mailbox background operation (0x%04x) started\n", mbox_cmd->opcode); @@ -311,6 +314,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, * and allow userspace to poll(2) for completion. */ if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE) { + int timeout; + if (mds->bg.opcode) return -EBUSY; @@ -320,41 +325,17 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, mds->bg.opcode = mbox_cmd->opcode; schedule_delayed_work(&mds->bg.poll_dwork, timeout * HZ); - goto success; - } - - timeout = mbox_cmd->poll_interval_ms; - for (i = 0; i < mbox_cmd->poll_count; i++) { - if (rcuwait_wait_event_timeout(&cxl_mbox->mbox_wait, - cxl_mbox_background_complete(cxlds), - TASK_UNINTERRUPTIBLE, - msecs_to_jiffies(timeout)) > 0) - break; - } - - if (!cxl_mbox_background_complete(cxlds)) { - dev_err(dev, "timeout waiting for background (%d ms)\n", - timeout * mbox_cmd->poll_count); - return -ETIMEDOUT; + } else { + /* pairs with release/acquire semantics */ + WARN_ON_ONCE(atomic_xchg(&cxl_mbox->poll_bgop, + mbox_cmd->opcode)); } - - bg_status_reg = readq(cxlds->regs.mbox + - CXLDEV_MBOX_BG_CMD_STATUS_OFFSET); - mbox_cmd->return_code = - FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK, - bg_status_reg); - dev_dbg(dev, - "Mailbox background operation (0x%04x) completed\n", - mbox_cmd->opcode); - } - - if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) { + } else if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) { dev_dbg(dev, "Mailbox operation had an error: %s\n", cxl_mbox_cmd_rc2str(mbox_cmd)); return 0; /* completed but caller must check return_code */ } -success: /* #7 */ cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET); out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg); @@ -384,10 +365,67 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, struct cxl_mbox_cmd *cmd) { int rc; + struct cxl_dev_state *cxlds = mbox_to_cxlds(cxl_mbox); + struct device *dev = cxlds->dev; + struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds); - mutex_lock(&cxl_mbox->mbox_mutex); - rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd); - mutex_unlock(&cxl_mbox->mbox_mutex); + scoped_guard(mutex, &cxl_mbox->mbox_mutex) { + /* + * Ensure cxl_mbox_background_complete() checks are safe + * amongst each other: no new bg operation can occur in + * between while polling. + */ + if (cxl_is_background_cmd(mds, cmd->opcode) && + atomic_read_acquire(&cxl_mbox->poll_bgop)) + return -EBUSY; + + rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd); + } + + if (cmd->return_code != CXL_MBOX_CMD_RC_BACKGROUND) + return rc; + + /* + * Handle the background command in a synchronous manner. Background + * operations are timesliced in accordance with the nature of the + * command. + */ + if (cmd->opcode != CXL_MBOX_OP_SANITIZE) { + int i, timeout; + u64 bg_status_reg; + + timeout = cmd->poll_interval_ms; + for (i = 0; i < cmd->poll_count; i++) { + if (rcuwait_wait_event_timeout(&cxl_mbox->mbox_wait, + cxl_mbox_background_complete(cxlds), + TASK_UNINTERRUPTIBLE, + msecs_to_jiffies(timeout)) > 0) + break; + } + + /* + * In the event of timeout, the mailbox state is indeterminate + * until the next successful command submission and the driver + * can get back in sync with the hardware state. + */ + if (!cxl_mbox_background_complete(cxlds)) { + dev_err(dev, "timeout waiting for background (%d ms)\n", + timeout * cmd->poll_count); + rc = -ETIMEDOUT; + goto done; + } + + bg_status_reg = readq(cxlds->regs.mbox + + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET); + cmd->return_code = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK, + bg_status_reg); + dev_dbg(dev, + "Mailbox background operation (0x%04x) completed\n", + cmd->opcode); +done: + /* ensure clearing poll_bgop is the last operation */ + atomic_set_release(&cxl_mbox->poll_bgop, 0); + } return rc; } diff --git a/include/cxl/mailbox.h b/include/cxl/mailbox.h index d008b9db07aa..691c2ae21ef5 100644 --- a/include/cxl/mailbox.h +++ b/include/cxl/mailbox.h @@ -52,6 +52,7 @@ struct cxl_mbox_cmd { * @mbox_mutex: mutex protects device mailbox and firmware * @feat_mutex: serializes multi-part Get/Set Feature transfers * @mbox_wait: rcuwait for mailbox + * @poll_bgop: current background operation being polled on * @mbox_send: @dev specific transport for transmitting mailbox commands * @feat_cap: Features capability */ @@ -63,6 +64,7 @@ struct cxl_mailbox { struct mutex mbox_mutex; /* lock to protect mailbox context */ struct mutex feat_mutex; struct rcuwait mbox_wait; + atomic_t poll_bgop; int (*mbox_send)(struct cxl_mailbox *cxl_mbox, struct cxl_mbox_cmd *cmd); enum cxl_features_capability feat_cap; }; -- 2.39.5