All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Davidlohr Bueso" <dave@stgolabs.net>
Cc: linux-cxl@vger.kernel.org
Subject: Re: [PATCH v2 08/10] cxl/mbox: Allow userspace background commands
Date: Wed, 02 Dec 2026 16:10:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-246198@kernel.org> (raw)
In-Reply-To: <20261202154404.4016305-9-dave@stgolabs.net>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] cxl/pci: unlocked read of mds->bg.user in cxl_pci_mbox_irq()
- [Medium] cxl/pci: stale cxl_mbox_bgpoll_work() acts on newly started
  bg command
- [Low] cxl/pci: unreachable default branch in __cxl_pci_mbox_send_cmd()
--

commit 8459d3a327df488babc367e3665eac99ff3c874f
Author: Davidlohr Bueso <dave@stgolabs.net>

cxl/mbox: Allow userspace background commands

This commit enables abortable userspace background mailbox commands, tracks
them asynchronously via the background polling worker, and allows incoming
background commands to abort an active userspace background command.

> diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
> index ef4853296e860..1cacc9e90a311 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,

[Severity: Medium]
Can reading mds->bg.user outside cxl_mbox->mbox_mutex in cxl_pci_mbox_irq()
race with __cxl_pci_mbox_send_cmd() and cxl_mbox_bg_end()?

__cxl_pci_mbox_send_cmd() only sets mds->bg.user after ringing and waiting
for the command doorbell under mbox_mutex:

CPU0                                  CPU1 (threaded IRQ)
__cxl_pci_mbox_send_cmd()
  mutex_lock(&cxl_mbox->mbox_mutex)
  writel(... | DOORBELL, ...)
  cxl_pci_mbox_wait_for_doorbell()
                                      cxl_pci_mbox_irq()
                                        if (... || mds->bg.user) <-- false
                                        rcuwait_wake_up(&cxl_mbox->mbox_wait)
  mds->bg.opcode = mbox_cmd->opcode
  mds->bg.user = mbox_cmd->user
  schedule_delayed_work(..., 1 * HZ)
  mutex_unlock(&cxl_mbox->mbox_mutex)

When a fast userspace background command completes before mds->bg.user is
stored, the IRQ handler takes the else branch and fails to expedite
mds->bg.poll_dwork.

Conversely, if cxl_pci_mbox_irq() reads mds->bg.user == true for a finished
or aborted command and blocks on mbox_mutex while a new command is
submitted, it will either kick poll_dwork immediately against a newly
started userspace background command (bumping its poll backoff from 1s to
11s) or exit without calling rcuwait_wake_up(&cxl_mbox->mbox_wait) if a
synchronous kernel background command started and completed in the meantime.

> @@ -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);
>  

[Severity: Medium]
Can an in-flight cxl_mbox_bgpoll_work() blocked on mbox_mutex act on a
newly started background command instead of noticing the cancellation?

In cxl_pci_mbox_send(), cxl_try_to_cancel_background() and
__cxl_pci_mbox_send_cmd() run under the same mbox_mutex acquisition:

CPU0                                  CPU1 (kworker)
cxl_pci_mbox_send()
  mutex_lock(&cxl_mbox->mbox_mutex)
                                      cxl_mbox_bgpoll_work()
                                        mutex_lock(&cxl_mbox->mbox_mutex)
  cxl_try_to_cancel_background()
    cancel_delayed_work(&mds->bg.poll_dwork)
    cxl_mbox_bg_end()
      mds->bg.opcode = 0
  __cxl_pci_mbox_send_cmd()
    mds->bg.poll_tmo_secs = 1
    mds->bg.opcode = mbox_cmd->opcode
    schedule_delayed_work(..., 1 * HZ)
  mutex_unlock(&cxl_mbox->mbox_mutex)
                                        if (!mds->bg.opcode) <-- false

Because mds->bg.opcode is overwritten before mbox_mutex is released, the
stale worker sees the new command's opcode.  If the new command is still
running, the stale worker immediately increases mds->bg.poll_tmo_secs from
1s to 11s; if the new command has already finished, it calls
cxl_mbox_bg_end() and clears mds->bg.opcode to 0 while the 1s delayed work
scheduled by __cxl_pci_mbox_send_cmd() remains pending.

> @@ -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;
>  	}

[Severity: Low]
Is the default branch in this switch reachable?

__cxl_pci_mbox_send_cmd() is only called from cxl_try_to_cancel_background()
(with CXL_MBOX_OP_REQUEST_ABORT_BG_OP, where cxl_is_background_cmd() is
false) and from cxl_pci_mbox_send().  In cxl_pci_mbox_send(), any command
where cxl_is_background_cmd() is true first calls
cxl_try_to_cancel_background() under mbox_mutex, which either fails and
returns -EBUSY before calling __cxl_pci_mbox_send_cmd(), or succeeds and
clears mds->bg.opcode to 0 via cxl_mbox_bg_end().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202154404.4016305-1-dave@stgolabs.net?part=8

  reply	other threads:[~2026-12-02 16:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 01/10] cxl/pci: Reschedule the poller with the capped timeout Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 02/10] cxl/pci: Preserve Mailbox Control bits when ringing the doorbell Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 03/10] cxl/mbox: Track background commands from CEL Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 04/10] cxl/mbox: Decouple async background handling from security Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 05/10] cxl/pci: Lockless background synchronous polling Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 06/10] cxl/mbox: Stronger cxl_mbox_background_complete() semantics Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 07/10] cxl/mbox: Add missing mailbox return codes Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 08/10] cxl/mbox: Allow userspace background commands Davidlohr Bueso
2026-12-02 16:10   ` sashiko-bot [this message]
2026-12-02 15:44 ` [PATCH v2 09/10] cxl/mbox: Shout upon async bgcmd race Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 10/10] cxl/mbox: Add Populate Log support Davidlohr Bueso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-246198@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dave@stgolabs.net \
    --cc=linux-cxl@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.