cxl: Userspace background commands
Davidlohr Bueso
maintainerv2 cover cxl: Userspace background commands
Hi,
Changes from v1:
o Dropped handling Activate FW asynchronously. Nothing bounds how long an activation may take, so there is no good answer to how long the firmware upload should wait for its result. Deferred until the spec has the device report it. Patch 4 is what is left of that patch, the decoupling from security, and Jonathan's tag is dropped as such.
o New fixes in patches 1 and 2, and new patch 7.
o Fixed the cxl_test build. (Unsung)
o Converted to scoped_guard() and clarified comments. (Jonathan)
o Rebased on v7.4-rc5.
This is v2 of the mailbox background command series[1]. It lets userspace make use of the background operation capability, with the kernel aborting such an operation whenever it needs the device for one of its own, and adds Populate Log as the first command to use it.
v1 has been sitting since June of last year. Unsung's work on online firmware activation[2] ran into the same background handling, and the discussion there is what brought this back.
o Patches 1-2 are fixes to the existing background handling, and can go in on their own.
o Patch 3 tracks which commands the CEL has as background capable.
o Patch 4 moves the async handling out of the security state.
o Patches 5-6 no longer hold the mbox_mutex while polling for a background command the kernel waits for.
o Patch 7 adds the return codes an abort request can come back with.
o Patches 8-9 allow userspace background commands.
o Patch 10 adds Populate Log.
Testing was done with cxl_test and the ndctl suite, and with the qemu based tests at [3], which also carry the qemu fixes they rely on.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://github.com/davidlohr/cxl-bg-tests
Thanks!
Davidlohr Bueso (9):
cxl/pci: Reschedule the poller with the capped timeout cxl/pci: Preserve Mailbox Control bits when ringing the doorbell cxl/mbox: Track background commands from CEL cxl/mbox: Decouple async background handling from security cxl/pci: Lockless background synchronous polling cxl/mbox: Stronger cxl_mbox_background_complete() semantics cxl/mbox: Add missing mailbox return codes
cxl/mbox: Allow userspace background commands cxl/mbox: Shout upon async bgcmd race
Ravi Shankar (1):
cxl/mbox: Add Populate Log support
drivers/cxl/core/hdm.c | 2 +- drivers/cxl/core/mbox.c | 45 ++++- drivers/cxl/core/memdev.c | 7 +- drivers/cxl/cxlmem.h | 53 +++++- drivers/cxl/pci.c | 318 +++++++++++++++++++++++++---------- include/cxl/mailbox.h | 4 + include/uapi/linux/cxl_mem.h | 1 + tools/testing/cxl/test/mem.c | 10 +- 8 files changed, 334 insertions(+), 106 deletions(-)
base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 1/10 cxl/pci: Reschedule the poller with the capped timeout
cxl_mbox_sanitize_work() backs off by 10 seconds every time it finds the sanitize still running, up to 15 minutes. The cap is only applied to the stored poll_tmo_secs however, while the work is rescheduled with the uncapped value. Once the limit is reached the poller runs every 910 seconds instead.
Compute the capped value first, and use it for both.
--- drivers/cxl/pci.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/pci.c +2 -2
@@ -166,9 +166,9 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)dev_dbg (cxlds->dev,"Sanitization operation ended\n" ); }else { -int timeout = mds->security.poll_tmo_secs +10 ; +int timeout =min (15 *60 , mds->security.poll_tmo_secs +10 ); - mds->security.poll_tmo_secs =min (15 *60 , timeout); + mds->security.poll_tmo_secs = timeout;schedule_delayed_work (&mds->security.poll_dwork, timeout *HZ ); }mutex_unlock (&cxl_mbox->mbox_mutex);
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 2/10 cxl/pci: Preserve Mailbox Control bits when ringing the doorbell
In reply to Davidlohr Buesocxl_pci_setup_mailbox() sets Background Command Complete Interrupt (bit 2) in the Mailbox Control register once, at probe. The doorbell is bit 0 of that same register, and __cxl_pci_mbox_send_cmd() rings it by writing CXLDEV_MBOX_CTRL_DOORBELL alone, zeroing everything else. The interrupt enable is RW, and only RO while the doorbell is set, so the first command sent after probe clears it again.
The driver then asks for no interrupt on background completion. Nothing breaks outright, as completion is still found by polling: the synchronous wait checks every poll_interval_ms, and the sanitize poller backs off up to 15 minutes between checks.
Ring the doorbell with a read-modify-write, as the enable itself is already set. This also stops writing zeroes to the remaining bits of the register, which are not the driver's to change.
--- drivers/cxl/pci.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cxl/pci.c +3 -1
@@ -205,6 +205,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,struct device *dev = cxlds->dev;u64 cmd_reg, status_reg;size_t out_len; +u32 ctrl;int rc;lockdep_assert_held (&cxl_mbox->mbox_mutex);
@@ -262,7 +263,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,/* #4 */ dev_dbg (dev,"Sending command: 0x%04x\n" , mbox_cmd->opcode); -writel (CXLDEV_MBOX_CTRL_DOORBELL , + ctrl =readl (cxlds->regs.mbox +CXLDEV_MBOX_CTRL_OFFSET ); +writel (ctrl |CXLDEV_MBOX_CTRL_DOORBELL , cxlds->regs.mbox +CXLDEV_MBOX_CTRL_OFFSET );/* #5 */
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 3/10 cxl/mbox: Track background commands from CEL
In reply to Davidlohr BuesoRemember whether or not a command is background-capable, per the CEL. This will later be used to consult the bitmap to evaluate incoming mbox commands.
Further, "enable" any background commands that didn't fall in the uapi, poison or security categories, such as fw related. This prevents situations where the command is incorrectly listed as unsupported by the driver.
--- drivers/cxl/core/mbox.c | 31 +++++++++++++++++++++++++++++++ drivers/cxl/cxlmem.h | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
drivers/cxl/core/mbox.c +31 -0
@@ -235,6 +235,31 @@ static void cxl_set_poison_cmd_enabled(struct cxl_poison_state *poison, } } +static bool cxl_set_bgcmd_enabled (struct cxl_background_state *bg,u16 opcode) +{ +switch (opcode) { +case CXL_MBOX_OP_TRANSFER_FW : +set_bit (CXL_BG_ENABLED_TRANSFER_FW , bg->enabled_cmds); +break ; +case CXL_MBOX_OP_ACTIVATE_FW : +set_bit (CXL_BG_ENABLED_ACTIVATE_FW , bg->enabled_cmds); +break ; +case CXL_MBOX_OP_DO_MAINTENANCE : +set_bit (CXL_BG_ENABLED_DO_MAINTENANCE , bg->enabled_cmds); +break ; +case CXL_MBOX_OP_SCAN_MEDIA : +set_bit (CXL_BG_ENABLED_SCAN_MEDIA , bg->enabled_cmds); +break ; +case CXL_MBOX_OP_SANITIZE : +set_bit (CXL_BG_ENABLED_SANITIZE , bg->enabled_cmds); +break ; +default : +return false; + } + +return true; +} +static struct cxl_mem_command *cxl_mem_find_command (u16 opcode) {struct cxl_mem_command *c;
@@ -794,6 +819,7 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)for (i =0 ; i < cel_entries; i++) {u16 opcode =le16_to_cpu (cel_entry[i].opcode); +u16 effect =le16_to_cpu (cel_entry[i].effect);struct cxl_mem_command *cmd =cxl_mem_find_command (opcode);int enabled =0 ;
@@ -820,6 +846,11 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel) enabled++; } +/* may already have been detected as enabled above */ +if (effect &CXL_CEL_BACKGROUND_OPERATION ) +if (cxl_set_bgcmd_enabled (&mds->bg, opcode)) + enabled++; +dev_dbg (dev,"Opcode 0x%04x %s\n" , opcode, enabled ?"enabled" :"unsupported by driver" ); }
drivers/cxl/cxlmem.h +27 -0
@@ -293,6 +293,16 @@ enum security_cmd_enabled_bits { CXL_SEC_ENABLED_MAX }; +/* Background-capable commands the device advertises in the CEL */ +enum background_cmd_enabled_bits { +CXL_BG_ENABLED_TRANSFER_FW , +CXL_BG_ENABLED_ACTIVATE_FW , +CXL_BG_ENABLED_DO_MAINTENANCE , +CXL_BG_ENABLED_SCAN_MEDIA , +CXL_BG_ENABLED_SANITIZE , +CXL_BG_ENABLED_MAX +}; +/** * struct cxl_poison_state - Driver poison state info *
@@ -410,6 +420,15 @@ struct cxl_security_state {struct kernfs_node *sanitize_node; }; +/** +* struct cxl_background_state - Driver background operation state info +* +* @enabled_cmds: All background commands enabled in the CEL +*/ +struct cxl_background_state { + DECLARE_BITMAP(enabled_cmds, CXL_BG_ENABLED_MAX); +}; +#define CXL_MAX_DC_PARTITIONS 8 static inline resource_size_t cxl_part_size(struct cxl_dev_state *cxlds,
@@ -448,6 +467,7 @@ static inline struct cxl_dev_state *mbox_to_cxlds(struct cxl_mailbox *cxl_mbox)* @poison: poison driver state info * @security: security driver state info * @fw: firmware upload / activation state +* @bg: background operation state * * See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for * details on capacity parameters.
@@ -468,6 +488,7 @@ struct cxl_memdev_state {struct cxl_poison_state poison;struct cxl_security_state security;struct cxl_fw_state fw; +struct cxl_background_state bg; }; static inline struct cxl_memdev_state *
@@ -547,6 +568,12 @@ struct cxl_mbox_get_supported_logs { }__packed entry[]; } __packed; +/* +* Command Effects Log (CEL) +* CXL 4.0 Section 8.2.10.5.2.1; Table 8-254 +*/ +#define CXL_CEL_BACKGROUND_OPERATION BIT(6) +struct cxl_cel_entry {__le16 opcode;__le16 effect;
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 4/10 cxl/mbox: Decouple async background handling from security
In reply to Davidlohr BuesoThe asynchronous handling of a background command, the state that says one is running and the work that polls for its completion, lives in the security state and is hardwired to Sanitize. Move it to the background state and track the running operation by its opcode, such that other commands can make use of it.
Now that the interrupt handler kicks the poller based on the tracked operation instead of the sanitize sysfs node, the sanitize notifier teardown no longer keeps the poller from being rescheduled. Cancel it with a devm action of its own that runs after the interrupt is gone.
Sanitize remains the only command handled asynchronously.
--- drivers/cxl/core/hdm.c | 2 +- drivers/cxl/core/memdev.c | 7 ++-- drivers/cxl/cxlmem.h | 12 +++---- drivers/cxl/pci.c | 64 +++++++++++++++++++++++------------- tools/testing/cxl/test/mem.c | 10 +++--- 5 files changed, 57 insertions(+), 38 deletions(-)
drivers/cxl/core/hdm.c +1 -1
@@ -879,7 +879,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlmd->cxlds); -if (mds && mds->security.sanitize_active) { +if (mds && mds->bg.opcode ==CXL_MBOX_OP_SANITIZE ) {dev_dbg (&cxlmd->dev,"attempted to commit %s during sanitize\n" ,dev_name (&cxld->dev));
drivers/cxl/core/memdev.c +3 -4
@@ -133,7 +133,7 @@ static ssize_t security_state_show(struct device *dev,/* sync with latest submission state */ mutex_lock (&cxl_mbox->mbox_mutex); -if (mds->security.sanitize_active) +if (mds->bg.opcode ==CXL_MBOX_OP_SANITIZE ) rc =sysfs_emit (buf,"sanitize\n" );mutex_unlock (&cxl_mbox->mbox_mutex);if (rc)
@@ -1301,15 +1301,14 @@ static void sanitize_teardown_notifier(void *data)struct kernfs_node *state;/* -* Prevent new irq triggered invocations of the workqueue and -* flush inflight invocations. +* Stop the poller from notifying and flush inflight invocations. */ mutex_lock (&cxl_mbox->mbox_mutex); state = mds->security.sanitize_node; mds->security.sanitize_node =NULL ;mutex_unlock (&cxl_mbox->mbox_mutex); -cancel_delayed_work_sync (&mds->security.poll_dwork); +cancel_delayed_work_sync (&mds->bg.poll_dwork);sysfs_put (state); }
drivers/cxl/cxlmem.h +6 -6
@@ -406,17 +406,11 @@ struct cxl_fw_state {* * @state: state of last security operation * @enabled_cmds: All security commands enabled in the CEL -* @poll_tmo_secs: polling timeout -* @sanitize_active: sanitize completion pending -* @poll_dwork: polling work item * @sanitize_node: sanitation sysfs file to notify */ struct cxl_security_state {unsigned long state; DECLARE_BITMAP(enabled_cmds, CXL_SEC_ENABLED_MAX); -int poll_tmo_secs; -bool sanitize_active; -struct delayed_work poll_dwork;struct kernfs_node *sanitize_node; };
@@ -424,9 +418,15 @@ struct cxl_security_state {* struct cxl_background_state - Driver background operation state info * * @enabled_cmds: All background commands enabled in the CEL +* @poll_dwork: polling background work item +* @poll_tmo_secs: polling timeout +* @opcode: async running background command */ struct cxl_background_state { DECLARE_BITMAP(enabled_cmds, CXL_BG_ENABLED_MAX); +struct delayed_work poll_dwork; +int poll_tmo_secs; +int opcode; };#define CXL_MAX_DC_PARTITIONS 8
drivers/cxl/pci.c +42 -22
131 lines not on this page · raw message
tools/testing/cxl/test/mem.c +5 -5
@@ -665,13 +665,13 @@ static int mock_partition_info(struct cxl_mbox_cmd *cmd)void cxl_mockmem_sanitize_work (struct work_struct *work) {struct cxl_memdev_state *mds = -container_of (work,typeof (*mds), security.poll_dwork.work); +container_of (work,typeof (*mds), bg.poll_dwork.work);struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;mutex_lock (&cxl_mbox->mbox_mutex);if (mds->security.sanitize_node)sysfs_notify_dirent (mds->security.sanitize_node); - mds->security.sanitize_active = false; + mds->bg.opcode =0 ;mutex_unlock (&cxl_mbox->mbox_mutex);dev_dbg (mds->cxlds.dev,"sanitize complete\n" );
@@ -700,9 +700,9 @@ static int mock_sanitize(struct cxl_mockmem_data *mdata, }mutex_lock (&cxl_mbox->mbox_mutex); -if (schedule_delayed_work (&mds->security.poll_dwork, +if (schedule_delayed_work (&mds->bg.poll_dwork,msecs_to_jiffies (mdata->sanitize_timeout))) { - mds->security.sanitize_active = true; + mds->bg.opcode =CXL_MBOX_OP_SANITIZE ;dev_dbg (mds->cxlds.dev,"sanitize issued\n" ); }else rc = -EBUSY ;
@@ -1766,7 +1766,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev) cxl_mbox->mbox_send = cxl_mock_mbox_send; cxl_mbox->payload_size =SZ_4K ; mds->event.buf = (struct cxl_get_event_payload *) mdata->event_buf; -INIT_DELAYED_WORK (&mds->security.poll_dwork, cxl_mockmem_sanitize_work); +INIT_DELAYED_WORK (&mds->bg.poll_dwork, cxl_mockmem_sanitize_work);if (is_rcd (pdev)) cxlds->rcd = true;
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 5/10 cxl/pci: Lockless background synchronous polling
In reply to Davidlohr BuesoFor 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.
--- drivers/cxl/core/mbox.c | 1 + drivers/cxl/pci.c | 136 +++++++++++++++++++++++++--------------- include/cxl/mailbox.h | 2 + 3 files changed, 90 insertions(+), 49 deletions(-)
drivers/cxl/core/mbox.c +1 -0
@@ -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 ;
drivers/cxl/pci.c +87 -49
180 lines not on this page · raw message
include/cxl/mailbox.h +2 -0
@@ -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; };
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 6/10 cxl/mbox: Stronger cxl_mbox_background_complete() semantics
In reply to Davidlohr BuesoAccount for all situations where hw has no on-going background command. Current semantics do no change with this patch, but will serve for later supporting cancel requests. As such rename the helper accordingly.
--- drivers/cxl/pci.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/cxl/pci.c +8 -8
@@ -112,12 +112,12 @@ static int cxl_request_irq(struct cxl_dev_state *cxlds, int irq, dev_id); } -static bool cxl_mbox_background_complete (struct cxl_dev_state *cxlds) +static bool cxl_mbox_background_done (struct cxl_dev_state *cxlds) {u64 reg; - reg =readq (cxlds->regs.mbox +CXLDEV_MBOX_BG_CMD_STATUS_OFFSET ); -return FIELD_GET (CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK , reg) ==100 ; + reg =readq (cxlds->regs.mbox +CXLDEV_MBOX_STATUS_OFFSET ); +return FIELD_GET (CXLDEV_MBOX_STATUS_BG_CMD , reg) ==0 ; }static irqreturn_t cxl_pci_mbox_irq (int irq,void *id)
@@ -129,7 +129,7 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox;struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlds); -if (!cxl_mbox_background_complete (cxlds)) +if (!cxl_mbox_background_done (cxlds))return IRQ_NONE ; reg =readq (cxlds->regs.mbox +CXLDEV_MBOX_BG_CMD_STATUS_OFFSET );
@@ -159,7 +159,7 @@ 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_complete (cxlds)) { +if (cxl_mbox_background_done (cxlds)) { mds->bg.poll_tmo_secs =0 ;if (mds->bg.opcode ==CXL_MBOX_OP_SANITIZE && mds->security.sanitize_node)
@@ -371,7 +371,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, scoped_guard(mutex, &cxl_mbox->mbox_mutex) {/* -* Ensure cxl_mbox_background_complete() checks are safe +* Ensure cxl_mbox_background_done() checks are safe * amongst each other: no new bg operation can occur in * between while polling. */
@@ -397,7 +397,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, 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), +cxl_mbox_background_done (cxlds),TASK_UNINTERRUPTIBLE ,msecs_to_jiffies (timeout)) >0 )break ;
@@ -408,7 +408,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,* until the next successful command submission and the driver * can get back in sync with the hardware state. */ -if (!cxl_mbox_background_complete (cxlds)) { +if (!cxl_mbox_background_done (cxlds)) {dev_err (dev,"timeout waiting for background (%d ms)\n" , timeout * cmd->poll_count); rc = -ETIMEDOUT ;
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 7/10 cxl/mbox: Add missing mailbox return codes
In reply to Davidlohr BuesoThe mailbox return code table ends at Invalid Extent List, yet the spec has since defined two more: Transfer Out of Order and Request Abort Not Supported by Background Operation. The table is looked up with whatever the device returns, unchecked, so either of them reads beyond it.
The second one is about to become an expected answer, as the driver will send Request Abort Background Operation without knowing whether the running operation supports it. Add both codes, and treat any other unknown one as a generic failure.
--- drivers/cxl/cxlmem.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/cxl/cxlmem.h +7 -1
@@ -157,7 +157,7 @@ static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port, }/* -* Per CXL 3.0 Section 8.2.8.4.5.1 +* Per CXL 4.0 Section 8.2.9.4.5.1 */ #define CMD_CMD_RC_TABLE \ C(SUCCESS, 0, NULL), \
@@ -191,6 +191,8 @@ static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,C (FEATURETRANSFEROOO , -ENXIO ,"feature transfer out of order" ), \C (RESOURCEEXHAUSTED , -ENXIO ,"resources are exhausted" ), \C (EXTLIST , -ENXIO ,"invalid Extent List" ), \ +C (TRANSFEROOO , -ENXIO ,"transfer out of order" ), \ +C (ABORTNOTSUP , -ENXIO ,"background cmd does not support abort" ), \ #undef C #define C(a, b, c) CXL_MBOX_CMD_RC_##a
@@ -208,11 +210,15 @@ struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE };static inline const char *cxl_mbox_cmd_rc2str (struct cxl_mbox_cmd *mbox_cmd) { +if (mbox_cmd->return_code >=ARRAY_SIZE (cxl_mbox_cmd_rctable)) +return "unknown return code" ;return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc; }static inline int cxl_mbox_cmd_rc2errno (struct cxl_mbox_cmd *mbox_cmd) { +if (mbox_cmd->return_code >=ARRAY_SIZE (cxl_mbox_cmd_rctable)) +return -ENXIO ;return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err; }
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 8/10 cxl/mbox: Allow userspace background commands
In reply to Davidlohr BuesoCXL 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.
--- 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(-)
drivers/cxl/core/mbox.c +7 -2
@@ -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,
drivers/cxl/cxlmem.h +4 -0
@@ -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;
drivers/cxl/pci.c +104 -22
184 lines not on this page · raw message
include/cxl/mailbox.h +2 -0
@@ -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;
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH v2 08/10] cxl/mbox: Allow userspace background commands
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…
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 <[email protected]>
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.
drivers/cxl/pci.c static irqreturn_t cxl_pci_mbox_irq(int irq, void *id) · line 140
Quoted 13 lines
drivers/cxl/pci.c +1
-1@@ -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.
Quoted 11 lines
drivers/cxl/pci.c +0
-7@@ -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,
drivers/cxl/pci.c static void cxl_mbox_bgpoll_work(struct work_struct *work) · line 189
Quoted 10 lines
drivers/cxl/pci.c +4
-3-"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) <-- falseBecause 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.
Quoted 19 lines
drivers/cxl/pci.c +10
-4@@ -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
drivers/cxl/pci.c static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, · line 299
Quoted 10 lines
drivers/cxl/pci.c +7
-0+* 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().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Davidlohr Bueso
maintainerPATCH v2 9/10 cxl/mbox: Shout upon async bgcmd race
In reply to Davidlohr BuesoBy the time a command answers Background Command Started, any operation the driver was tracking has been ended, either by the poller or by cxl_try_to_cancel_background() on behalf of the new command. Finding one still tracked means the driver's view of the background operation no longer matches what hardware is running.
This is not expected to happen. Make the condition a warning instead of quietly returning an error, as it is the kind of thing that is harder to debug further down the line.
--- drivers/cxl/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cxl/pci.c +1 -1
@@ -344,7 +344,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, mbox_cmd->user) {int timeout; -if (mds->bg.opcode) +if (WARN_ON_ONCE (mds->bg.opcode))return -EBUSY ;/* give first timeout a second */
Signature · 1 line
2.39.5
Davidlohr Bueso
maintainerPATCH v2 10/10 cxl/mbox: Add Populate Log support
In reply to Davidlohr BuesoFrom: Ravi Shankar <[email protected]>
Adding UAPI support for
CXL r4.0 8.2.10.5.5 Populate Log.
Populate Log may run in the background, in which case the command returns once the device has started it and the kernel does not report its outcome. For the Component State Dump Log this is observed through Get Log (8.2.10.5.2.3): it returns Retry Required until the log is fully populated, and then the log, with the length and the timestamp of the data in its header.
--- drivers/cxl/core/mbox.c | 4 ++++ drivers/cxl/cxlmem.h | 2 ++ drivers/cxl/pci.c | 2 ++ include/uapi/linux/cxl_mem.h | 1 + 4 files changed, 9 insertions(+)
drivers/cxl/core/mbox.c +4 -0
@@ -59,6 +59,7 @@ static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = {CXL_CMD (GET_LOG_CAPS ,0x10 ,0x4 ,0 ),CXL_CMD (CLEAR_LOG ,0x10 ,0 ,0 ),CXL_CMD (GET_SUP_LOG_SUBLIST ,0x2 ,CXL_VARIABLE_PAYLOAD ,0 ), +CXL_CMD (POPULATE_LOG ,0x10 ,0 ,0 ),CXL_CMD (SET_PARTITION_INFO ,0x0a ,0 ,0 ),CXL_CMD (SET_LSA ,CXL_VARIABLE_PAYLOAD ,0 ,0 ), CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0),
@@ -244,6 +245,9 @@ static bool cxl_set_bgcmd_enabled(struct cxl_background_state *bg, u16 opcode)case CXL_MBOX_OP_ACTIVATE_FW :set_bit (CXL_BG_ENABLED_ACTIVATE_FW , bg->enabled_cmds);break ; +case CXL_MBOX_OP_POPULATE_LOG : +set_bit (CXL_BG_ENABLED_POPULATE_LOG , bg->enabled_cmds); +break ;case CXL_MBOX_OP_DO_MAINTENANCE :set_bit (CXL_BG_ENABLED_DO_MAINTENANCE , bg->enabled_cmds);break ;
drivers/cxl/cxlmem.h +2 -0
@@ -303,6 +303,7 @@ enum security_cmd_enabled_bits {enum background_cmd_enabled_bits {CXL_BG_ENABLED_TRANSFER_FW ,CXL_BG_ENABLED_ACTIVATE_FW , +CXL_BG_ENABLED_POPULATE_LOG ,CXL_BG_ENABLED_DO_MAINTENANCE ,CXL_BG_ENABLED_SCAN_MEDIA ,CXL_BG_ENABLED_SANITIZE ,
@@ -524,6 +525,7 @@ enum cxl_opcode {CXL_MBOX_OP_GET_LOG =0x0401 ,CXL_MBOX_OP_GET_LOG_CAPS =0x0402 ,CXL_MBOX_OP_CLEAR_LOG =0x0403 , +CXL_MBOX_OP_POPULATE_LOG =0x0404 ,CXL_MBOX_OP_GET_SUP_LOG_SUBLIST =0x0405 ,CXL_MBOX_OP_GET_SUPPORTED_FEATURES =0x0500 , CXL_MBOX_OP_GET_FEATURE = 0x0501,
drivers/cxl/pci.c +2 -0
@@ -201,6 +201,8 @@ static inline bool cxl_is_background_cmd(struct cxl_memdev_state *mds,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_POPULATE_LOG : +return test_bit (CXL_BG_ENABLED_POPULATE_LOG , 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 :
include/uapi/linux/cxl_mem.h +1 -0
@@ -50,6 +50,7 @@___C (GET_LOG_CAPS ,"Get Log Capabilities" ), \___C (CLEAR_LOG ,"Clear Log" ), \___C (GET_SUP_LOG_SUBLIST ,"Get Supported Logs Sub-List" ), \ +___C (POPULATE_LOG ,"Populate Log" ), \ ___C(MAX, "invalid / last command")#define ___C (a, b) CXL_MEM_COMMAND_ID_##a
Signature · 1 line
2.39.5