cxl: Handle background commands
Davidlohr Bueso
maintainercover cxl: Handle background commands
Hi,
This decouples the general synchronous approach series to bg commands from the sanitation changes[0], and is sent as a stand alone series with the following changes:
o Introduced patch 1 which gives us the correct semantics to wait/wake without using queued wait flavors.
o Replaced global waitqueue with per-device wait. (Dave)
o The current tear down is left as is simply because kicking the wait doesn't necessarily mean that there is no further waits left (->poll_count), so it really doesn't gain us much -- if this is ever an actual issue it could be revisited.
o Removed bogus bg return code check at the end of __cxl_pci_mbox_send_cmd (Ming).
o Small cosmetic updates + picked up Dave's review for patch 2.
Currently there are no users, but it is expected that firmware update and scan media be the first two to use it.
Applies against 'next' from cxl.git.
[0] https://lore.kernel.org/linux-cxl/[email protected]/
Thanks!
Davidlohr Bueso (3):
rcuwait: Support timeouts
cxl/pci: Allocate irq vectors earlier in pci probe cxl/mbox: Add background cmd handling machinery
drivers/cxl/core/mbox.c | 3 +- drivers/cxl/cxl.h | 7 +++ drivers/cxl/cxlmem.h | 7 +++ drivers/cxl/pci.c | 110 ++++++++++++++++++++++++++++++++++++++-- include/linux/rcuwait.h | 23 +++++++-- 5 files changed, 142 insertions(+), 8 deletions(-)
--
2.40.1
Davidlohr Bueso
maintainerPATCH 1/3 rcuwait: Support timeouts
Introduce rcuwait_wait_event_timeout(), with semantics equivalent to calls for the simple and regular waitqueue counterparts.
---
Only cc'ing sched folks this patch to avoid spamming. fyi the actual user comes up in patch 3 (cxl/mbox: Add background cmd handling machinery), but found a few other potential users in various drivers, so more could be added.
include/linux/rcuwait.h | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
include/linux/rcuwait.h +20 -3
@@ -49,9 +49,9 @@ static inline void prepare_to_rcuwait(struct rcuwait *w)extern void finish_rcuwait (struct rcuwait *w); -#define rcuwait_wait_event (w, condition, state) \ +#define ___rcuwait_wait_event (w, condition, state, ret, cmd) \ ({ \ - int __ret = 0; \ + long __ret = ret; \ prepare_to_rcuwait(w); \ for (;;) { \/* \
@@ -67,10 +67,27 @@ extern void finish_rcuwait(struct rcuwait *w); break; \ } \ \ -schedule (); \ + cmd; \ } \finish_rcuwait (w); \ __ret; \ }) +#define rcuwait_wait_event (w, condition, state) \ + ___rcuwait_wait_event(w, condition, state, 0, schedule()) + +#define __rcuwait_wait_event_timeout (w, condition, state, timeout) \ + ___rcuwait_wait_event(w, ___wait_cond_timeout(condition), \ + state, timeout, \ + __ret = schedule_timeout(__ret)) + +#define rcuwait_wait_event_timeout (w, condition, state, timeout) \ +({ \ + long __ret = timeout; \ + if (!___wait_cond_timeout(condition)) \ + __ret = __rcuwait_wait_event_timeout(w, condition, \ + state, timeout); \ + __ret; \ +}) +#endif /* _LINUX_RCUWAIT_H_ */
Signature · 1 line
2.40.1
RE: [PATCH 1/3] rcuwait: Support timeouts
Quoted 2 lines · Davidlohr Bueso
Introduce
rcuwait_wait_event_timeout(), with semantics equivalent to calls for the simple and regular waitqueue counterparts.
So my first reaction to this is "what is rcuwait, never heard of that before?", and "how did Davidlohr find this facility and why does he think it suitable here?". Then I go to read the history to get smarter about this thing and the author is: Davidlohr Bueso <[email protected]>, aha!
The critical insight for me was:
https://lore.kernel.org/all/[email protected]/
...i.e. that rcuwait is suitable for waiting for a condition to fire while holding another exclusive lock like a mutex() as is the case with the CXL mbox code. Did I get that right?
My recommendation would be to include some text like "Recall that rcuwait is suitable in this scenario because..." reminders at least until more driver people understand when and how to use it.
Quoted 8 lines
---
Only cc'ing sched folks this patch to avoid spamming. fyi the actual user comes up in patch 3 (cxl/mbox: Add background cmd handling machinery), but found a few other potential users in various drivers, so more could be added.
FWIW this looks like a straightforward addition of timeout support to rcuwait_wait_event() to me.
What acks are required to take this through the CXL tree? Or are you the rcuwait maintainer?
Quoted 52 lines
include/linux/rcuwait.h | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)include/linux/rcuwait.h +20
-3@@ -49,9 +49,9 @@ static inline void prepare_to_rcuwait(struct rcuwait *w)extern void finish_rcuwait (struct rcuwait *w); -#define rcuwait_wait_event (w, condition, state) \ +#define ___rcuwait_wait_event (w, condition, state, ret, cmd) \ ({ \ - int __ret = 0; \ + long __ret = ret; \ prepare_to_rcuwait(w); \ for (;;) { \/* \ @@ -67,10 +67,27 @@ extern void finish_rcuwait(struct rcuwait *w); break; \ } \ \ -schedule (); \ + cmd; \ } \finish_rcuwait (w); \ __ret; \ }) +#define rcuwait_wait_event (w, condition, state) \ + ___rcuwait_wait_event(w, condition, state, 0, schedule()) + +#define __rcuwait_wait_event_timeout (w, condition, state, timeout) \ + ___rcuwait_wait_event(w, ___wait_cond_timeout(condition), \ + state, timeout, \ + __ret = schedule_timeout(__ret)) + +#define rcuwait_wait_event_timeout (w, condition, state, timeout) \ +({ \ + long __ret = timeout; \ + if (!___wait_cond_timeout(condition)) \ + __ret = __rcuwait_wait_event_timeout(w, condition, \ + state, timeout); \ + __ret; \ +}) +#endif /* _LINUX_RCUWAIT_H_ */ Signature · 1 line
2.40.1
Davidlohr Bueso
maintainerRe: [PATCH 1/3] rcuwait: Support timeouts
Quoted 10 lines · Dan Williams
Quoted 2 lines · Davidlohr Bueso
Introduce
rcuwait_wait_event_timeout(), with semantics equivalent to calls for the simple and regular waitqueue counterparts.So my first reaction to this is "what is rcuwait, never heard of that before?", and "how did Davidlohr find this facility and why does he think it suitable here?". Then I go to read the history to get smarter about this thing and the author is: Davidlohr Bueso <[email protected]>, aha!
Quoted 3 lines
The critical insight for me was:
Just a general note that per
154abafc68b (tasks, sched/core: With a grace period after finish_task_switch(), remove unnecessary code)
... that the exit_notify() caveat no longer applies.
Quoted 3 lines
...i.e. that rcuwait is suitable for waiting for a condition to fire while holding another exclusive lock like a
mutex()as is the case with the CXL mbox code. Did I get that right?
Yes. For this particular patchset it made sense to go this way in terms of semantics (this use-case is obviously not a performance sensitive path, nor do any of the rt benefits of not having to deal with a q->lock).
Quoted 3 lines
My recommendation would be to include some text like "Recall that rcuwait is suitable in this scenario because..." reminders at least until more driver people understand when and how to use it.
Sure.
Quoted 9 lines
Quoted 8 lines
---
Only cc'ing sched folks this patch to avoid spamming. fyi the actual user comes up in patch 3 (cxl/mbox: Add background cmd handling machinery), but found a few other potential users in various drivers, so more could be added.
Quoted 5 lines
FWIW this looks like a straightforward addition of timeout support to
rcuwait_wait_event()to me.What acks are required to take this through the CXL tree? Or are you the rcuwait maintainer?
No, I am not the maintainer, changes have been routed usually through tip folks. Conceptually at least I think Peter was ok with it, but ultimately an explicit ack/nack would be required.
Thanks,
Davidlohr
Peter Zijlstra
Acked-byRe: [PATCH 1/3] rcuwait: Support timeouts
Quoted 8 lines · Davidlohr Bueso
Quoted 2 lines · Dan Williams
What acks are required to take this through the CXL tree? Or are you the rcuwait maintainer?
No, I am not the maintainer, changes have been routed usually through tip folks. Conceptually at least I think Peter was ok with it, but ultimately an explicit ack/nack would be required.
Yeah, would normally be me routing it through the sched tree -- that also takes all the other wait (wait and swait) crud.
If this needs to go elsewhere,
Davidlohr Bueso
maintainerPATCH 2/3 cxl/pci: Allocate irq vectors earlier in pci probe
In reply to Davidlohr BuesoMove the cxl_alloc_irq_vectors() call further up in the probing in order to allow for mailbox interrupt usage. No change in semantics.
--- drivers/cxl/pci.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/cxl/pci.c +4 -4
@@ -708,6 +708,10 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (rc)dev_dbg (&pdev->dev,"Failed to map RAS capability.\n" ); + rc =cxl_alloc_irq_vectors (pdev); +if (rc) +return rc; + rc =cxl_pci_setup_mailbox (cxlds);if (rc)return rc;
@@ -732,10 +736,6 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (rc)return rc; - rc =cxl_alloc_irq_vectors (pdev); -if (rc) -return rc; - cxlmd =devm_cxl_add_memdev (cxlds);if (IS_ERR (cxlmd))return PTR_ERR (cxlmd);
Signature · 1 line
2.40.1
Re: [PATCH 2/3] cxl/pci: Allocate irq vectors earlier in pci probe
Quoted 6 lines · On Tue, 2 May 2023 10:18:40 -0700 Davidlohr Bueso <[email protected]> wrote:
Move the
cxl_alloc_irq_vectors()call further up in the probing in order to allow for mailbox interrupt usage. No change in semantics.Reviewed-by Dave Jiang <[email protected]>Signed-off-by Davidlohr Bueso <[email protected]>
Chasing this patch through various places it appeared :)
Quoted 30 lines
--- drivers/cxl/pci.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)drivers/cxl/pci.c +4
-4@@ -708,6 +708,10 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (rc)dev_dbg (&pdev->dev,"Failed to map RAS capability.\n" ); + rc =cxl_alloc_irq_vectors (pdev); +if (rc) +return rc; + rc =cxl_pci_setup_mailbox (cxlds);if (rc)return rc;@@ -732,10 +736,6 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (rc)return rc; - rc =cxl_alloc_irq_vectors (pdev); -if (rc) -return rc; - cxlmd =devm_cxl_add_memdev (cxlds);if (IS_ERR (cxlmd))return PTR_ERR (cxlmd);
Davidlohr Bueso
maintainerPATCH 3/3 cxl/mbox: Add background cmd handling machinery
In reply to Davidlohr BuesoThis adds support for handling background operations, as defined in the CXL 3.0 spec. Commands that can take too long (over ~2 seconds) can run in the background asynchronously (to the hardware).
The driver will deal with such commands synchronously, blocking all other incoming commands for a specified period of time, allowing time-slicing the command such that the caller can send incremental requests to avoid monopolizing the driver/device. This approach makes the code simpler, where any out of sync (timeout) between the driver and hardware is just disregarded as an invalid state until the next successful submission.
On devices where mbox interrupts are supported, this will still use a poller that will wakeup in the specified wait intervals. The irq handler will simply awake the blocked cmd, which is also safe vs a task that is either waking (timing out) or already awoken. Similarly any irq setup error during the probing falls back to polling, thus avoids unnecessarily erroring out.
--- drivers/cxl/core/mbox.c | 3 +- drivers/cxl/cxl.h | 7 +++ drivers/cxl/cxlmem.h | 7 +++ drivers/cxl/pci.c | 102 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-)
drivers/cxl/core/mbox.c +2 -1
@@ -220,7 +220,8 @@ int cxl_internal_send_cmd(struct cxl_dev_state *cxlds,if (rc)return rc; -if (mbox_cmd->return_code !=CXL_MBOX_CMD_RC_SUCCESS ) +if (mbox_cmd->return_code !=CXL_MBOX_CMD_RC_SUCCESS && + mbox_cmd->return_code !=CXL_MBOX_CMD_RC_BACKGROUND )return cxl_mbox_cmd_rc2errno (mbox_cmd);if (!out_size)
drivers/cxl/cxl.h +7 -0
@@ -176,14 +176,21 @@ static inline int ways_to_eiw(unsigned int ways, u8 *eiw)/* CXL 2.0 8.2.8.4 Mailbox Registers */ #define CXLDEV_MBOX_CAPS_OFFSET 0x00#define CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0) +#define CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK GENMASK(10, 7) +#define CXLDEV_MBOX_CAP_BG_CMD_IRQ BIT(6)#define CXLDEV_MBOX_CTRL_OFFSET 0x04#define CXLDEV_MBOX_CTRL_DOORBELL BIT(0) +#define CXLDEV_MBOX_CTRL_BG_CMD_IRQ BIT(2)#define CXLDEV_MBOX_CMD_OFFSET 0x08#define CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)#define CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16)#define CXLDEV_MBOX_STATUS_OFFSET 0x10 +#define CXLDEV_MBOX_STATUS_BG_CMD BIT(0)#define CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32)#define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18 +#define CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0) +#define CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK GENMASK_ULL(22, 16) +#define CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK GENMASK_ULL(47, 32)#define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20/*
drivers/cxl/cxlmem.h +7 -0
@@ -5,6 +5,7 @@#include <uapi/linux/cxl_mem.h> #include <linux/cdev.h> #include <linux/uuid.h> +#include <linux/rcuwait.h> #include "cxl.h" /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
@@ -108,6 +109,9 @@ static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,* variable sized output commands, it tells the exact number of bytes * written. * @min_out: (input) internal command output payload size validation +* @poll_count: (input) Number of timeouts to attempt. +* @poll_interval: (input) Number of ms between mailbox background command +* polling intervals timeouts. * @return_code: (output) Error code returned from hardware. * * This is the primary mechanism used to send commands to the hardware.
@@ -123,6 +127,8 @@ struct cxl_mbox_cmd {size_t size_in;size_t size_out;size_t min_out; +int poll_count; +int poll_interval;u16 return_code; };
@@ -329,6 +335,7 @@ struct cxl_dev_state {struct cxl_event_state event;struct cxl_poison_state poison; +struct rcuwait mbox_wait;int (*mbox_send)(struct cxl_dev_state *cxlds,struct cxl_mbox_cmd *cmd); };
drivers/cxl/pci.c +102 -0
126 lines not on this page · raw message
Signature · 1 line
2.40.1
Davidlohr Bueso
maintainerRe: [PATCH 3/3] cxl/mbox: Add background cmd handling machinery
Quoted 5 lines · Davidlohr Bueso
drivers/cxl/pci.c +5
-0+/* +* 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
drivers/cxl/pci.c static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, · line 222
Quoted 10 lines
drivers/cxl/pci.c +10
-0+* 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 ) { +int i, ret;
bleh this "ret" really wants to be a long.
Re: [PATCH 3/3] cxl/mbox: Add background cmd handling machinery
In reply to Davidlohr BuesoHi Davidlohr,
kernel test robot noticed the following build warnings:
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Davidlohr-Bueso/rcuwait-Support-timeouts/20230503-015128 base: https://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl.git next patch link: https://lore.kernel.org/r/20230502171841.21317-4-dave%40stgolabs.net patch subject: [PATCH 3/3] cxl/mbox: Add background cmd handling machinery config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20230503/[email protected]/config) compiler: gcc-11 (Debian 11.3.0-12) 11.3.0
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Link: https://lore.kernel.org/r/[email protected]/
smatch warnings:drivers/cxl/pci.c:230 __cxl_pci_mbox_send_cmd() warn: maybe use && instead of &
vim +230 drivers/cxl/pci.c
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 221 if (mbox_cmd->return_code == CXL_MBOX_CMD_RC_BACKGROUND) {
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 222 int i, ret;
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 223 u64 bg_status_reg;
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 224 int timeout = mbox_cmd->poll_interval;
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 225
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 226 dev_dbg(dev, "Mailbox background operation (0x%04x) started\n",
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 227 mbox_cmd->opcode);
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 228
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 229 for (i = 0; i < mbox_cmd->poll_count; i++) {
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 @230 ret = rcuwait_wait_event_timeout(&cxlds->mbox_wait,
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 231 TASK_INTERRUPTIBLE,
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 232 cxl_mbox_background_complete(cxlds),
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 233 msecs_to_jiffies(timeout));These TASK_INTERRUPTIBLE and cxl_mbox_background_complete() arguments are swapped. The condition is supposed the second argument.
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 234 if (ret > 0)
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 235 break;
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 236 if (ret < 0) /* interrupted by a signal */
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 237 return ret;
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 238 }
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 239
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 240 if (!cxl_mbox_background_complete(cxlds)) {
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 241 u64 md_status =
914e64b06cee69 drivers/cxl/pci.c Davidlohr Bueso 2023-05-02 242 readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Davidlohr Bueso
maintainerPATCH v2 1/1 cxl/mbox: Add background cmd handling machinery
In reply to Davidlohr BuesoThis adds support for handling background operations, as defined in the CXL 3.0 spec. Commands that can take too long (over ~2 seconds) can run in the background asynchronously (to the hardware).
The driver will deal with such commands synchronously, blocking all other incoming commands for a specified period of time, allowing time-slicing the command such that the caller can send incremental requests to avoid monopolizing the driver/device. This approach makes the code simpler, where any out of sync (timeout) between the driver and hardware is just disregarded as an invalid state until the next successful submission.
On devices where mbox interrupts are supported, this will still use a poller that will wakeup in the specified wait intervals. The irq handler will simply awake the blocked cmd, which is also safe vs a task that is either waking (timing out) or already awoken. Similarly any irq setup error during the probing falls back to polling, thus avoids unnecessarily erroring out.
---
Changes from v2:
- make the wait ret a long.
- pass correct param orders to the wait (cond and state were inversed).
drivers/cxl/core/mbox.c | 3 +- drivers/cxl/cxl.h | 7 +++ drivers/cxl/cxlmem.h | 7 +++ drivers/cxl/pci.c | 102 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-)
drivers/cxl/core/mbox.c +0 -0
@@ -220,7 +220,8 @@ int cxl_internal_send_cmd(struct cxl_dev_state *cxlds,
if (rc) return rc;
- if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS)
+ if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS &&
+ mbox_cmd->return_code != CXL_MBOX_CMD_RC_BACKGROUND)
return cxl_mbox_cmd_rc2errno(mbox_cmd); if (!out_size)
drivers/cxl/cxl.h +7 -0
@@ -176,14 +176,21 @@ static inline int ways_to_eiw(unsigned int ways, u8 *eiw)/* CXL 2.0 8.2.8.4 Mailbox Registers */ #define CXLDEV_MBOX_CAPS_OFFSET 0x00#define CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0) +#define CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK GENMASK(10, 7) +#define CXLDEV_MBOX_CAP_BG_CMD_IRQ BIT(6)#define CXLDEV_MBOX_CTRL_OFFSET 0x04#define CXLDEV_MBOX_CTRL_DOORBELL BIT(0) +#define CXLDEV_MBOX_CTRL_BG_CMD_IRQ BIT(2)#define CXLDEV_MBOX_CMD_OFFSET 0x08#define CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)#define CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16)#define CXLDEV_MBOX_STATUS_OFFSET 0x10 +#define CXLDEV_MBOX_STATUS_BG_CMD BIT(0)#define CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32)#define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18 +#define CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0) +#define CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK GENMASK_ULL(22, 16) +#define CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK GENMASK_ULL(47, 32)#define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20/*
drivers/cxl/cxlmem.h +4 -0
@@ -5,6 +5,7 @@#include <uapi/linux/cxl_mem.h> #include <linux/cdev.h> #include <linux/uuid.h> +#include <linux/rcuwait.h> #include "cxl.h" /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
@@ -108,6 +109,9 @@ static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,* variable sized output commands, it tells the exact number of bytes * written. * @min_out: (input) internal command output payload size validation +* @poll_count: (input) Number of timeouts to attempt. +* @poll_interval: (input) Number of ms between mailbox background command +* polling intervals timeouts. * @return_code: (output) Error code returned from hardware. * * This is the primary mechanism used to send commands to the hardware.
@@ -123,6 +127,8 @@ struct cxl_mbox_cmd {
size_t size_in; size_t size_out; size_t min_out; + int poll_count; + int poll_interval; u16 return_code;
};
@@ -329,6 +335,7 @@ struct cxl_dev_state {
struct cxl_event_state event;
struct cxl_poison_state poison;+ struct rcuwait mbox_wait; int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
};
drivers/cxl/pci.c +1 -0
@@ -51,6 +51,7 @@ static unsigned short mbox_ready_timeout = 60;module_param (mbox_ready_timeout, ushort,0644 );MODULE_PARM_DESC (mbox_ready_timeout,"seconds to wait for mailbox ready" ); +static int cxl_pci_mbox_wait_for_doorbell (struct cxl_dev_state *cxlds) {
const unsigned long start = jiffies; @@ -84,6 +85,33 @@ static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds) status & CXLMDEV_DEV_FATAL ? " fatal" : "", \ status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
+static bool cxl_mbox_background_complete(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;
+}
+
+static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
+{
+ struct cxl_dev_state *cxlds = id;
+
+ /* spurious or raced with hw? */
+ if (unlikely(!cxl_mbox_background_complete(cxlds))) {
+ struct pci_dev *pdev = to_pci_dev(cxlds->dev);
+
+ dev_warn(&pdev->dev,
+ "Mailbox background operation IRQ but incomplete\n");
+ goto done;
+ }
+
+ /* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
+ rcuwait_wake_up(&cxlds->mbox_wait);
+done:
+ return IRQ_HANDLED;
+}
+
/**
* __cxl_pci_mbox_send_cmd() - Execute a mailbox command
* @cxlds: The device state to communicate with.
@@ -177,6 +205,57 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
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) {
+ long ret;
+ u64 bg_status_reg;
+ int i, timeout = mbox_cmd->poll_interval;
+
+ dev_dbg(dev, "Mailbox background operation (0x%04x) started\n",
+ mbox_cmd->opcode);
+
+ for (i = 0; i < mbox_cmd->poll_count; i++) {
+ ret = rcuwait_wait_event_timeout(&cxlds->mbox_wait,
+ cxl_mbox_background_complete(cxlds),
+ TASK_INTERRUPTIBLE,
+ msecs_to_jiffies(timeout));
+ if (ret > 0)
+ break;
+ if (ret < 0) /* interrupted by a signal */
+ return ret;
+ }
+
+ if (!cxl_mbox_background_complete(cxlds)) {
+ u64 md_status =
+ readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
+
+ cxl_cmd_err(cxlds->dev, mbox_cmd, md_status,
+ "background timeout");
+ return -ETIMEDOUT;
+ }
+
+ 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) {
dev_dbg(dev, "Mailbox operation had an error: %s\n",
cxl_mbox_cmd_rc2str(mbox_cmd));
@@ -271,6 +350,29 @@ static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds)
dev_dbg(cxlds->dev, "Mailbox payload sized %zu",
cxlds->payload_size);+ rcuwait_init(&cxlds->mbox_wait);
+ if (cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) {
+ int irq, msgnum;
+ struct pci_dev *pdev = to_pci_dev(cxlds->dev);
+
+ msgnum = FIELD_GET(CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK, cap);
+ irq = pci_irq_vector(pdev, msgnum);
+ if (irq < 0)
+ goto mbox_poll;
+
+ if (devm_request_irq(cxlds->dev, irq, cxl_pci_mbox_irq,
+ IRQF_SHARED, NULL, cxlds))
+ goto mbox_poll;
+
+ /* only enable background cmd mbox irq support */
+ writel(CXLDEV_MBOX_CTRL_BG_CMD_IRQ,
+ cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
+
+ return 0;
+ }
+
+mbox_poll:
+ dev_dbg(cxlds->dev, "Mailbox interrupts are unsupported"); return 0;
}
--
2.40.1
Re: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 12 lines · On Wed, 3 May 2023 07:57:56 -0700 Davidlohr Bueso <[email protected]> wrote:
This adds support for handling background operations, as defined in the CXL 3.0 spec. Commands that can take too long (over ~2 seconds) can run in the background asynchronously (to the hardware).
The driver will deal with such commands synchronously, blocking all other incoming commands for a specified period of time, allowing time-slicing the command such that the caller can send incremental requests to avoid monopolizing the driver/device. This approach makes the code simpler, where any out of sync (timeout) between the driver and hardware is just disregarded as an invalid state until the next successful submission.
Quoted 6 lines
On devices where mbox interrupts are supported, this will still use a poller that will wakeup in the specified wait intervals. The irq handler will simply awake the blocked cmd, which is also safe vs a task that is either waking (timing out) or already awoken. Similarly any irq setup error during the probing falls back to polling, thus avoids unnecessarily erroring out.
This raises the question of why we don't support Doorbell Interrupts. 2 seconds is rather a long time to poll for. I can't really remember the reasoning but maybe it's that we don't expect anyone to every produce hardware that takes that long.
Ah well, job for another day perhaps.
I'm not following why the rcuwait is needed versus other options. Perhaps some minimal text here for those of us not familiar with that particular mechanism vs a completion of similar.
Quoted 2 lines
Signed-off-by Davidlohr Bueso <[email protected]>
Otherwise just a comment inline on dev_warn for unexpected interrupt on an interrupt that you've marked as IRQF_SHARED which pretty much guarantees you need to handle unexpected interrupts as 'normal'.
Jonathan
Quoted 57 lines
---
Changes from v2:
- make the wait ret a long.
- pass correct param orders to the wait (cond and state were inversed).drivers/cxl/core/mbox.c | 3 +- drivers/cxl/cxl.h | 7 +++ drivers/cxl/cxlmem.h | 7 +++ drivers/cxl/pci.c | 102 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-)drivers/cxl/cxlmem.h +7
-0@@ -5,6 +5,7 @@#include <uapi/linux/cxl_mem.h> #include <linux/cdev.h> #include <linux/uuid.h> +#include <linux/rcuwait.h> #include "cxl.h" /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */ @@ -108,6 +109,9 @@ static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,* variable sized output commands, it tells the exact number of bytes * written. * @min_out: (input) internal command output payload size validation +* @poll_count: (input) Number of timeouts to attempt. +* @poll_interval: (input) Number of ms between mailbox background command +* polling intervals timeouts. * @return_code: (output) Error code returned from hardware. * * This is the primary mechanism used to send commands to the hardware. @@ -123,6 +127,8 @@ struct cxl_mbox_cmd {size_t size_in;size_t size_out;size_t min_out; +int poll_count; +int poll_interval;u16 return_code; };@@ -329,6 +335,7 @@ struct cxl_dev_state {struct cxl_event_state event;struct cxl_poison_state poison; +struct rcuwait mbox_wait;int (*mbox_send)(struct cxl_dev_state *cxlds,struct cxl_mbox_cmd *cmd); };drivers/cxl/pci.c +0
-0
drivers/cxl/pci.c static unsigned short mbox_ready_timeout = 60; · line 54
Quoted 4 lines
drivers/cxl/pci.c +1
-0@@ -51,6 +51,7 @@ static unsigned short mbox_ready_timeout = 60;module_param (mbox_ready_timeout, ushort,0644 );MODULE_PARM_DESC (mbox_ready_timeout,"seconds to wait for mailbox ready" ); +
:) You know what I'm going to say - so I won't bother saying it.
Quoted 15 lines
drivers/cxl/pci.c +8
-0static int cxl_pci_mbox_wait_for_doorbell (struct cxl_dev_state *cxlds) {const unsigned long start = jiffies;@@ -84,6 +85,33 @@ static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds) status & CXLMDEV_DEV_FATAL ? " fatal" : "", \ status & CXLMDEV_FW_HALT ? " firmware-halt" : "") +static bool cxl_mbox_background_complete (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 ; +} +
drivers/cxl/pci.c static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds) · line 105
Quoted 10 lines
drivers/cxl/pci.c +10
-0+static irqreturn_t cxl_pci_mbox_irq (int irq,void *id) +{ +struct cxl_dev_state *cxlds = id; + +/* spurious or raced with hw? */ +if (unlikely (!cxl_mbox_background_complete (cxlds))) { +struct pci_dev *pdev =to_pci_dev (cxlds->dev); + +dev_warn (&pdev->dev, +"Mailbox background operation IRQ but incomplete\n" );
It's a shared IRQ, there are many possible reasons we might get here that aren't things we should warn about. Would be nice if we could detect the race might have happened and only return IRQ_HANDLED if that's the case as opposed to it's someone else's interrupt.
Quoted 8 lines
drivers/cxl/pci.c +8
-0+ goto done; + } + +/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */ +rcuwait_wake_up (&cxlds->mbox_wait); +done: +return IRQ_HANDLED ; +}
Davidlohr Bueso
maintainerRe: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 27 lines · Jonathan Cameron
Quoted 12 lines · On Wed, 3 May 2023 07:57:56 -0700 Davidlohr Bueso <[email protected]> wrote:
This adds support for handling background operations, as defined in the CXL 3.0 spec. Commands that can take too long (over ~2 seconds) can run in the background asynchronously (to the hardware).
The driver will deal with such commands synchronously, blocking all other incoming commands for a specified period of time, allowing time-slicing the command such that the caller can send incremental requests to avoid monopolizing the driver/device. This approach makes the code simpler, where any out of sync (timeout) between the driver and hardware is just disregarded as an invalid state until the next successful submission.
Quoted 6 lines
On devices where mbox interrupts are supported, this will still use a poller that will wakeup in the specified wait intervals. The irq handler will simply awake the blocked cmd, which is also safe vs a task that is either waking (timing out) or already awoken. Similarly any irq setup error during the probing falls back to polling, thus avoids unnecessarily erroring out.
This raises the question of why we don't support Doorbell Interrupts. 2 seconds is rather a long time to poll for. I can't really remember the reasoning but maybe it's that we don't expect anyone to every produce hardware that takes that long.
Quoted 5 lines
Ah well, job for another day perhaps.
I'm not following why the rcuwait is needed versus other options. Perhaps some minimal text here for those of us not familiar with that particular mechanism vs a completion of similar.
As mentioned before, rcuwait gives us the single wait/wake semantics we need (we are serialized by the mbox_mutex) - completions use queued wait, which is an overkill for this case.
Quoted 4 lines
Quoted 1 line
Signed-off-by Davidlohr Bueso <[email protected]>Otherwise just a comment inline on
dev_warnfor unexpected interrupt on an interrupt that you've marked asIRQF_SHAREDwhich pretty much guarantees you need to handle unexpected interrupts as 'normal'.
Per your feedback in the the previous series, I will remove this dev_warn altogether.
Thanks,
Davidlohr
Re: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 31 lines · On Mon, 15 May 2023 08:40:30 -0700 Davidlohr Bueso <[email protected]> wrote:
Quoted 29 lines · Jonathan Cameron
Quoted 12 lines · On Wed, 3 May 2023 07:57:56 -0700 Davidlohr Bueso <[email protected]> wrote:
This adds support for handling background operations, as defined in the CXL 3.0 spec. Commands that can take too long (over ~2 seconds) can run in the background asynchronously (to the hardware).
The driver will deal with such commands synchronously, blocking all other incoming commands for a specified period of time, allowing time-slicing the command such that the caller can send incremental requests to avoid monopolizing the driver/device. This approach makes the code simpler, where any out of sync (timeout) between the driver and hardware is just disregarded as an invalid state until the next successful submission.
Quoted 6 lines
On devices where mbox interrupts are supported, this will still use a poller that will wakeup in the specified wait intervals. The irq handler will simply awake the blocked cmd, which is also safe vs a task that is either waking (timing out) or already awoken. Similarly any irq setup error during the probing falls back to polling, thus avoids unnecessarily erroring out.
This raises the question of why we don't support Doorbell Interrupts. 2 seconds is rather a long time to poll for. I can't really remember the reasoning but maybe it's that we don't expect anyone to every produce hardware that takes that long.
Ah well, job for another day perhaps.
Quoted 7 lines
Quoted 3 lines
I'm not following why the rcuwait is needed versus other options. Perhaps some minimal text here for those of us not familiar with that particular mechanism vs a completion of similar.
As mentioned before, rcuwait gives us the single wait/wake semantics we need (we are serialized by the
mbox_mutex) - completions use queued wait, which is an overkill for this case.
Fair enough I guess. New toy that I'd not come across before.
Jonathan
Quoted 11 lines
Quoted 5 lines
Quoted 2 lines
Signed-off-by Davidlohr Bueso <[email protected]>Otherwise just a comment inline on
dev_warnfor unexpected interrupt on an interrupt that you've marked asIRQF_SHAREDwhich pretty much guarantees you need to handle unexpected interrupts as 'normal'.Per your feedback in the the previous series, I will remove this
dev_warnaltogether.Thanks,
Davidlohr
Re: [PATCH v2] cxl/mbox: Add background cmd handling machinery
In reply to Davidlohr BuesoQuoted 27 lines · Davidlohr Bueso
/**
*__cxl_pci_mbox_send_cmd()- Execute a mailbox command
* @cxlds: The device state to communicate with.(quoted diff) +0
-0@@ -177,6 +205,57 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,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 thembox_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) { + long ret;
+ u64bg_status_reg;
+ int i, timeout =mbox_cmd->poll_interval;(quoted diff) +3
-0+ + dev_dbg(dev, "Mailbox background operation (0x%04x) started\n", + mbox_cmd->opcode);
Review
Quoted 10 lines
(quoted diff) +10
-0+ + for (i = 0; i < mbox_cmd->poll_count; i++) { + ret = rcuwait_wait_event_timeout(&cxlds->mbox_wait, + cxl_mbox_background_complete(cxlds), + TASK_INTERRUPTIBLE, + msecs_to_jiffies(timeout)); + if (ret > 0) + break; + if (ret < 0) /* interrupted by a signal */ + return ret;
What do you think if adding a dev_dbg() here for outputting current percentage complete for debugging? is it helpful?
Thanks
Ming
Quoted 56 lines
+ }
(quoted diff) +20
-0+ + if (!cxl_mbox_background_complete(cxlds)) { + u64 md_status = + readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET); + + cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, + "background timeout"); + return -ETIMEDOUT; + } + + 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) {dev_dbg(dev, "Mailbox operation had an error: %s\n", cxl_mbox_cmd_rc2str(mbox_cmd));(quoted diff) +23
-0@@ -271,6 +350,29 @@ static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) dev_dbg(cxlds->dev, "Mailbox payload sized %zu", cxlds->payload_size); + rcuwait_init(&cxlds->mbox_wait); + if (cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) { + int irq, msgnum; + struct pci_dev *pdev = to_pci_dev(cxlds->dev); + + msgnum = FIELD_GET(CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK, cap); + irq = pci_irq_vector(pdev, msgnum); + if (irq < 0) + goto mbox_poll; + + if (devm_request_irq(cxlds->dev, irq, cxl_pci_mbox_irq, + IRQF_SHARED, NULL, cxlds)) + goto mbox_poll; + + /* only enable background cmd mbox irq support */ + writel(CXLDEV_MBOX_CTRL_BG_CMD_IRQ, + cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET); + + return 0; + } + +mbox_poll: + dev_dbg(cxlds->dev, "Mailbox interrupts are unsupported"); return 0; }Signature · 1 line
2.40.1
Davidlohr Bueso
maintainerRe: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 33 lines · Ming
Quoted 31 lines · Davidlohr Bueso
/**
*__cxl_pci_mbox_send_cmd()- Execute a mailbox command
* @cxlds: The device state to communicate with.(quoted diff) +0
-0@@ -177,6 +205,57 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,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 thembox_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) { + long ret;
+ u64bg_status_reg;
+ int i, timeout =mbox_cmd->poll_interval;(quoted diff) +7
-0+ + dev_dbg(dev, "Mailbox background operation (0x%04x) started\n", + mbox_cmd->opcode); + + for (i = 0; i < mbox_cmd->poll_count; i++) { + ret = rcuwait_wait_event_timeout(&cxlds->mbox_wait, + cxl_mbox_background_complete(cxlds),
Quoted 8 lines
Quoted 6 lines
+
TASK_INTERRUPTIBLE,
+msecs_to_jiffies(timeout)); + if (ret > 0)
+ break;
+ if (ret < 0) /* interrupted by a signal */ + return ret;What do you think if adding a
dev_dbg()here for outputting current percentage complete for debugging? is it helpful?
I don't see it being very relevant here, honestly. Something like that might be more usful in a longer async context without timeouts.
Thanks,
Davidlohr
RE: [PATCH v2] cxl/mbox: Add background cmd handling machinery
In reply to Davidlohr BuesoQuoted 131 lines · Davidlohr Bueso
This adds support for handling background operations, as defined in the CXL 3.0 spec. Commands that can take too long (over ~2 seconds) can run in the background asynchronously (to the hardware).
The driver will deal with such commands synchronously, blocking all other incoming commands for a specified period of time, allowing time-slicing the command such that the caller can send incremental requests to avoid monopolizing the driver/device. This approach makes the code simpler, where any out of sync (timeout) between the driver and hardware is just disregarded as an invalid state until the next successful submission.
On devices where mbox interrupts are supported, this will still use a poller that will wakeup in the specified wait intervals. The irq handler will simply awake the blocked cmd, which is also safe vs a task that is either waking (timing out) or already awoken. Similarly any irq setup error during the probing falls back to polling, thus avoids unnecessarily erroring out.
Signed-off-by Davidlohr Bueso <[email protected]>---
Changes from v2:
- make the wait ret a long.
- pass correct param orders to the wait (cond and state were inversed).drivers/cxl/core/mbox.c | 3 +- drivers/cxl/cxl.h | 7 +++ drivers/cxl/cxlmem.h | 7 +++ drivers/cxl/pci.c | 102 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-)drivers/cxl/core/mbox.c +2
-1@@ -220,7 +220,8 @@ int cxl_internal_send_cmd(struct cxl_dev_state *cxlds,if (rc)return rc; -if (mbox_cmd->return_code !=CXL_MBOX_CMD_RC_SUCCESS ) +if (mbox_cmd->return_code !=CXL_MBOX_CMD_RC_SUCCESS && + mbox_cmd->return_code !=CXL_MBOX_CMD_RC_BACKGROUND )return cxl_mbox_cmd_rc2errno (mbox_cmd);if (!out_size)drivers/cxl/cxl.h +7
-0@@ -176,14 +176,21 @@ static inline int ways_to_eiw(unsigned int ways, u8 *eiw)/* CXL 2.0 8.2.8.4 Mailbox Registers */ #define CXLDEV_MBOX_CAPS_OFFSET 0x00#define CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0) +#define CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK GENMASK(10, 7) +#define CXLDEV_MBOX_CAP_BG_CMD_IRQ BIT(6)#define CXLDEV_MBOX_CTRL_OFFSET 0x04#define CXLDEV_MBOX_CTRL_DOORBELL BIT(0) +#define CXLDEV_MBOX_CTRL_BG_CMD_IRQ BIT(2)#define CXLDEV_MBOX_CMD_OFFSET 0x08#define CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)#define CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16)#define CXLDEV_MBOX_STATUS_OFFSET 0x10 +#define CXLDEV_MBOX_STATUS_BG_CMD BIT(0)#define CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32)#define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18 +#define CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0) +#define CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK GENMASK_ULL(22, 16) +#define CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK GENMASK_ULL(47, 32)#define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20/* drivers/cxl/cxlmem.h +7
-0@@ -5,6 +5,7 @@#include <uapi/linux/cxl_mem.h> #include <linux/cdev.h> #include <linux/uuid.h> +#include <linux/rcuwait.h> #include "cxl.h" /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */ @@ -108,6 +109,9 @@ static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,* variable sized output commands, it tells the exact number of bytes * written. * @min_out: (input) internal command output payload size validation +* @poll_count: (input) Number of timeouts to attempt. +* @poll_interval: (input) Number of ms between mailbox background command +* polling intervals timeouts. * @return_code: (output) Error code returned from hardware. * * This is the primary mechanism used to send commands to the hardware. @@ -123,6 +127,8 @@ struct cxl_mbox_cmd {size_t size_in;size_t size_out;size_t min_out; +int poll_count; +int poll_interval;u16 return_code; };@@ -329,6 +335,7 @@ struct cxl_dev_state {struct cxl_event_state event;struct cxl_poison_state poison; +struct rcuwait mbox_wait;int (*mbox_send)(struct cxl_dev_state *cxlds,struct cxl_mbox_cmd *cmd); };drivers/cxl/pci.c +5
-0@@ -51,6 +51,7 @@ static unsigned short mbox_ready_timeout = 60;module_param (mbox_ready_timeout, ushort,0644 );MODULE_PARM_DESC (mbox_ready_timeout,"seconds to wait for mailbox ready" ); +static int cxl_pci_mbox_wait_for_doorbell (struct cxl_dev_state *cxlds) {const unsigned long start = jiffies;@@ -84,6 +85,33 @@ static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds) status & CXLMDEV_DEV_FATAL ? " fatal" : "", \ status & CXLMDEV_FW_HALT ? " firmware-halt" : "") +static bool cxl_mbox_background_complete (struct cxl_dev_state *cxlds) +{ +u64 reg; +
drivers/cxl/pci.c static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds) · line 101
Quoted 10 lines
drivers/cxl/pci.c +10
-0+ reg =readq (cxlds->regs.mbox +CXLDEV_MBOX_BG_CMD_STATUS_OFFSET ); +return FIELD_GET (CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK , reg) ==100 ; +} + +static irqreturn_t cxl_pci_mbox_irq (int irq,void *id) +{ +struct cxl_dev_state *cxlds = id; + +/* spurious or raced with hw? */ +if (unlikely (!cxl_mbox_background_complete (cxlds))) {
I expect this will fire all the time. While the specification allows for a vector per-reason there's nothing stopping an implementation from using one vector for all reasons. There's little incentive for spending silicon gates on exclusive vectors since none of the CXL interrupts service software fast paths.
Quoted 39 lines
drivers/cxl/pci.c +32
-0+struct pci_dev *pdev =to_pci_dev (cxlds->dev); + +dev_warn (&pdev->dev, +"Mailbox background operation IRQ but incomplete\n" ); + goto done; + } + +/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */ +rcuwait_wake_up (&cxlds->mbox_wait); +done: +return IRQ_HANDLED ; +} +/** * __cxl_pci_mbox_send_cmd() - Execute a mailbox command * @cxlds: The device state to communicate with. @@ -177,6 +205,57 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, 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 ) { +long ret; +u64 bg_status_reg; +int i, timeout = mbox_cmd->poll_interval; + + dev_dbg(dev, "Mailbox background operation (0x%04x) started\n",
drivers/cxl/pci.c static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, · line 236
Quoted 10 lines
drivers/cxl/pci.c +10
-0+ mbox_cmd->opcode); + +for (i =0 ; i < mbox_cmd->poll_count; i++) { + ret =rcuwait_wait_event_timeout (&cxlds->mbox_wait, +cxl_mbox_background_complete (cxlds), +TASK_INTERRUPTIBLE , +msecs_to_jiffies (timeout)); +if (ret >0 ) +break ; +if (ret <0 )/* interrupted by a signal */
Have you tested this path? I am curious what happens on the next command submission? As far as I can see foreground commands would not be impacted but the next background submission would fail instead of wait, right?
Perhaps a dev_err_ratelimited() message and an EBUSY status for when that happens to at least indicate, "hardware is still chewing on a cancelled request". Later we can think about whether that's recoverable via a reset.
Quoted 31 lines
drivers/cxl/pci.c +24
-0+return ret; + } + +if (!cxl_mbox_background_complete (cxlds)) { +u64 md_status = +readq (cxlds->regs.memdev +CXLMDEV_STATUS_OFFSET ); + +cxl_cmd_err (cxlds->dev, mbox_cmd, md_status, +"background timeout" ); +return -ETIMEDOUT ; + } + + 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 ) {dev_dbg (dev,"Mailbox operation had an error: %s\n" ,cxl_mbox_cmd_rc2str (mbox_cmd));@@ -271,6 +350,29 @@ static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds)dev_dbg (cxlds->dev,"Mailbox payload sized %zu" , cxlds->payload_size); +rcuwait_init (&cxlds->mbox_wait); +if (cap &CXLDEV_MBOX_CAP_BG_CMD_IRQ ) {
drivers/cxl/pci.c static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 364
Quoted 10 lines
drivers/cxl/pci.c +10
-0+int irq, msgnum; +struct pci_dev *pdev =to_pci_dev (cxlds->dev); + + msgnum =FIELD_GET (CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK , cap); + irq =pci_irq_vector (pdev, msgnum); +if (irq <0 ) + goto mbox_poll; + +if (devm_request_irq (cxlds->dev, irq, cxl_pci_mbox_irq, +IRQF_SHARED ,NULL , cxlds))
This needs IRQF_ONESHOT and that @dev_id needs to be globally unique in case @irq is shared. So I think you want to factor out a common helper from cxl_event_req_irq() that this can reuse. I.e. cxl_event_req_irq() already gets the flags and @dev_id right, so just create a cxl_req_irq() that takes the irq as an argument and share it.
drivers/cxl/pci.c static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 369
Quoted 5 lines
drivers/cxl/pci.c +5
-0+ goto mbox_poll; + +/* only enable background cmd mbox irq support */ +writel (CXLDEV_MBOX_CTRL_BG_CMD_IRQ , + cxlds->regs.mbox +CXLDEV_MBOX_CTRL_OFFSET );
Just in case some other code ever enables doorbell interrupts it would be nice if this performed a read-modify-write rather than assuming it can clobber that setting.
Quoted 11 lines
drivers/cxl/pci.c +6
-1+ +return 0 ; + } + +mbox_poll: +dev_dbg (cxlds->dev,"Mailbox interrupts are unsupported" );return 0 ; } --2.40.1
Davidlohr Bueso
maintainerRe: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 4 lines · Dan Williams
Quoted 4 lines
(quoted diff) +4
-0+static irqreturn_t cxl_pci_mbox_irq(int irq, void *id) +{ + struct cxl_dev_state *cxlds = id; +
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* spurious or raced with hw? */ + if (unlikely(!cxl_mbox_background_complete(cxlds))) {I expect this will fire all the time. While the specification allows for a vector per-reason there's nothing stopping an implementation from using one vector for all reasons. There's little incentive for spending silicon gates on exclusive vectors since none of the CXL interrupts service software fast paths.
fyi I have removed the warn below entirely. Handler is now simply:
if (cxl_mbox_background_complete()) wake_up(); return IRQ_HANDLED;
Quoted 47 lines
Quoted 47 lines
(quoted diff) +39
-0+ struct pci_dev *pdev = to_pci_dev(cxlds->dev); + + dev_warn(&pdev->dev, + "Mailbox background operation IRQ but incomplete\n"); + goto done; + } + + /* short-circuit the wait in __cxl_pci_mbox_send_cmd() */ + rcuwait_wake_up(&cxlds->mbox_wait); +done: + return IRQ_HANDLED; +} + /** * __cxl_pci_mbox_send_cmd() - Execute a mailbox command * @cxlds: The device state to communicate with.@@ -177,6 +205,57 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, 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) { + long ret; + u64 bg_status_reg; + int i, timeout = mbox_cmd->poll_interval; + + dev_dbg(dev, "Mailbox background operation (0x%04x) started\n", + mbox_cmd->opcode); + + for (i = 0; i < mbox_cmd->poll_count; i++) { + ret = rcuwait_wait_event_timeout(&cxlds->mbox_wait, + cxl_mbox_background_complete(cxlds), + TASK_INTERRUPTIBLE, + msecs_to_jiffies(timeout));
Quoted 8 lines
Review static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, · line 236
Quoted 3 lines
(quoted diff) +3
-0+ if (ret > 0) + break; + if (ret < 0) /* interrupted by a signal */Have you tested this path? I am curious what happens on the next command submission? As far as I can see foreground commands would not be impacted but the next background submission would fail instead of wait, right?
No, I haven't tested this path. My expectation here is that this would be similar to the timeout case in that the driver returns but the hw keeps processing the command. The next command, fg or bg, would be at the mercy of the hw doing the right thing until it's done with the canceled one.
Quoted 4 lines
Perhaps a
dev_err_ratelimited()message and an EBUSY status for when that happens to at least indicate, "hardware is still chewing on a cancelled request". Later we can think about whether that's recoverable via a reset.
Well but reaching this path already assumes hw didn't error out the current command because the canceled one was still being run.
Alternatively we could also just make the sleep uninterruptible, but because this comes from the user triggering the bg op, I thought against it.
Quoted 1 line
Review static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, · line 237
Quoted 1 line
(quoted diff) +1
-0+ return ret;
rcuwait, unlike regular wait, returns -EINTR instead of -ERESTARTSYS. I've updated the return value here to the latter.
Quoted 38 lines
Quoted 38 lines
(quoted diff) +31
-0+ } + + if (!cxl_mbox_background_complete(cxlds)) { + u64 md_status = + readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET); + + cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, + "background timeout"); + return -ETIMEDOUT; + } + + 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) { dev_dbg(dev, "Mailbox operation had an error: %s\n", cxl_mbox_cmd_rc2str(mbox_cmd));@@ -271,6 +350,29 @@ static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) dev_dbg(cxlds->dev, "Mailbox payload sized %zu", cxlds->payload_size); + rcuwait_init(&cxlds->mbox_wait); + if (cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) { + int irq, msgnum; + struct pci_dev *pdev = to_pci_dev(cxlds->dev); + + msgnum = FIELD_GET(CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK, cap); + irq = pci_irq_vector(pdev, msgnum); + if (irq < 0) + goto mbox_poll; +
Quoted 8 lines
Review static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 364
Quoted 2 lines
(quoted diff) +2
-0+ if (devm_request_irq(cxlds->dev, irq, cxl_pci_mbox_irq, + IRQF_SHARED, NULL, cxlds))This needs
IRQF_ONESHOTand that @dev_id needs to be globally unique in case @irq is shared. So I think you want to factor out a common helper fromcxl_event_req_irq()that this can reuse. I.e.cxl_event_req_irq()already gets the flags and @dev_id right, so just create acxl_req_irq()that takes the irq as an argument and share it.
For the oneshot this was a hardirq, so I don't think this is needed, but regardless, I've added the following helper in a new patch which both events and mbox can call:
static int cxl_request_irq(struct cxl_dev_state *cxlds,
int irq, irq_handler_t handler)
{
struct device *dev = cxlds->dev;
struct cxl_dev_id *dev_id;
/* dev_id must be globally unique and must contain the cxlds */
dev_id = devm_kzalloc(dev, sizeof(*dev_id), GFP_KERNEL);
if (!dev_id)
return -ENOMEM;
dev_id->cxlds = cxlds;
return devm_request_threaded_irq(dev, irq, NULL, handler,
IRQF_SHARED | IRQF_ONESHOT, NULL,
dev_id);
}Quoted 10 lines
Review static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 369
Quoted 6 lines
(quoted diff) +5
-0+ goto mbox_poll; + + /* only enable background cmd mbox irq support */ + writel(CXLDEV_MBOX_CTRL_BG_CMD_IRQ, + cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);Just in case some other code ever enables doorbell interrupts it would be nice if this performed a read-modify-write rather than assuming it can clobber that setting.
Hmm do you mean enabling/disabling dynamically? Otherwise I would have expected this to be a one time thing which we could just OR both bits. Maybe this could be revisited when needed? Jonathan suggested enabling doorbell irqs in the future, which I plan on looking into at some point.
Thanks,
Davidlohr
Re: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 77 lines · Davidlohr Bueso
Quoted 4 lines · Dan Williams
Quoted 4 lines
(quoted diff) +4
-0+static irqreturn_t cxl_pci_mbox_irq(int irq, void *id) +{ + struct cxl_dev_state *cxlds = id; +Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* spurious or raced with hw? */ + if (unlikely(!cxl_mbox_background_complete(cxlds))) {I expect this will fire all the time. While the specification allows for a vector per-reason there's nothing stopping an implementation from using one vector for all reasons. There's little incentive for spending silicon gates on exclusive vectors since none of the CXL interrupts service software fast paths.
fyi I have removed the warn below entirely. Handler is now simply:
if (cxl_mbox_background_complete()) wake_up(); return IRQ_HANDLED;Quoted 47 lines
Quoted 47 lines
(quoted diff) +39
-0+ struct pci_dev *pdev = to_pci_dev(cxlds->dev); + + dev_warn(&pdev->dev, + "Mailbox background operation IRQ but incomplete\n"); + goto done; + } + + /* short-circuit the wait in __cxl_pci_mbox_send_cmd() */ + rcuwait_wake_up(&cxlds->mbox_wait); +done: + return IRQ_HANDLED; +} + /** * __cxl_pci_mbox_send_cmd() - Execute a mailbox command * @cxlds: The device state to communicate with.@@ -177,6 +205,57 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, 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) { + long ret; + u64 bg_status_reg; + int i, timeout = mbox_cmd->poll_interval; + + dev_dbg(dev, "Mailbox background operation (0x%04x) started\n", + mbox_cmd->opcode); + + for (i = 0; i < mbox_cmd->poll_count; i++) { + ret = rcuwait_wait_event_timeout(&cxlds->mbox_wait, + cxl_mbox_background_complete(cxlds), + TASK_INTERRUPTIBLE, + msecs_to_jiffies(timeout));Quoted 8 lines
Review static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, · line 236
Quoted 3 lines
(quoted diff) +3
-0+ if (ret > 0) + break; + if (ret < 0) /* interrupted by a signal */Have you tested this path? I am curious what happens on the next command submission? As far as I can see foreground commands would not be impacted but the next background submission would fail instead of wait, right?
Quoted 4 lines
No, I haven't tested this path. My expectation here is that this would be similar to the timeout case in that the driver returns but the hw keeps processing the command. The next command, fg or bg, would be at the mercy of the hw doing the right thing until it's done with the canceled one.
Right, it's that "mercy" part where there needs to be documentation of what to expect.
If someone sends SIGTERM after triggering a scan media and then immediately resubmits it maybe the right answer is to go into another interruptible sleep awaiting the last background state to clear out. It just seems like returning an error leaves userspace to fend for itself with little visibility of what it needs to do next. SIGTERM during a background command means, "I do not care about the previous result". Given the driver is mitigating long running background cycles that implies that waiting before submitting the next colliding command would not be onerous. They can always SIGTERM again if the wait gets too long.
Quoted 10 lines
Quoted 4 lines
Perhaps a
dev_err_ratelimited()message and an EBUSY status for when that happens to at least indicate, "hardware is still chewing on a cancelled request". Later we can think about whether that's recoverable via a reset.Well but reaching this path already assumes hw didn't error out the current command because the canceled one was still being run.
Alternatively we could also just make the sleep uninterruptible, but because this comes from the user triggering the bg op, I thought against it.
I don't think that would solve the problem of how to detect when the driver is out of sync with the device and what to do to resolve that situation. An inopportune SIGTERM is indistinguishable from getting the polling interval wrong.
Quoted 53 lines
Quoted 2 lines
Review static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, · line 237
Quoted 2 lines
(quoted diff) +1
-0+ return ret;rcuwait, unlike regular wait, returns
-EINTRinstead of-ERESTARTSYS. I've updated the return value here to the latter.Quoted 38 lines
Quoted 38 lines
(quoted diff) +31
-0+ } + + if (!cxl_mbox_background_complete(cxlds)) { + u64 md_status = + readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET); + + cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, + "background timeout"); + return -ETIMEDOUT; + } + + 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) { dev_dbg(dev, "Mailbox operation had an error: %s\n", cxl_mbox_cmd_rc2str(mbox_cmd));@@ -271,6 +350,29 @@ static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) dev_dbg(cxlds->dev, "Mailbox payload sized %zu", cxlds->payload_size); + rcuwait_init(&cxlds->mbox_wait); + if (cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) { + int irq, msgnum; + struct pci_dev *pdev = to_pci_dev(cxlds->dev); + + msgnum = FIELD_GET(CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK, cap); + irq = pci_irq_vector(pdev, msgnum); + if (irq < 0) + goto mbox_poll; +Quoted 8 lines
Review static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 364
Quoted 2 lines
(quoted diff) +2
-0+ if (devm_request_irq(cxlds->dev, irq, cxl_pci_mbox_irq, + IRQF_SHARED, NULL, cxlds))This needs
IRQF_ONESHOTand that @dev_id needs to be globally unique in case @irq is shared. So I think you want to factor out a common helper fromcxl_event_req_irq()that this can reuse. I.e.cxl_event_req_irq()already gets the flags and @dev_id right, so just create acxl_req_irq()that takes the irq as an argument and share it.
Quoted 3 lines
For the oneshot this was a hardirq, so I don't think this is needed, but regardless, I've added the following helper in a new patch which both events and mbox can call:
Ah, right, this was hard-irq only, I missed that.
I wouldn't be opposed to cxl_request_irq() following pci_request_irq()'s lead and taking multiple handler arguments to skip the thread invocation when it is not needed.
Quoted 29 lines
static int cxl_request_irq(struct
cxl_dev_state*cxlds,
int irq,irq_handler_thandler){ struct device *dev = cxlds->dev; struct cxl_dev_id *dev_id; /* dev_id must be globally unique and must contain the cxlds */ dev_id = devm_kzalloc(dev, sizeof(*dev_id), GFP_KERNEL); if (!dev_id) return -ENOMEM; dev_id->cxlds = cxlds; return devm_request_threaded_irq(dev, irq, NULL, handler, IRQF_SHARED | IRQF_ONESHOT, NULL, dev_id); }Quoted 10 lines
Review static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 369
Quoted 6 lines
(quoted diff) +5
-0+ goto mbox_poll; + + /* only enable background cmd mbox irq support */ + writel(CXLDEV_MBOX_CTRL_BG_CMD_IRQ, + cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);Just in case some other code ever enables doorbell interrupts it would be nice if this performed a read-modify-write rather than assuming it can clobber that setting.
Quoted 1 line
Hmm do you mean enabling/disabling dynamically?
No, grep for:
"write.*CTRL"
...and observe that all writes to control registers are doing a read-modify-write sequence.
Quoted 4 lines
Otherwise I would have
expected this to be a one time thing which we could just OR both bits. Maybe this could be revisited when needed? Jonathan suggested enabling doorbell irqs in the future, which I plan on looking into at some point.
Sure, that code might be in another function also performing its own read-modify-write, so my comment just asking for this to be consistent with other control register updates in the driver.
Davidlohr Bueso
maintainerRe: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 63 lines · Dan Williams
Quoted 35 lines · Davidlohr Bueso
Quoted 26 lines
Quoted 26 lines
(quoted diff) +26
-0+ /* + * 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) { + long ret; + u64 bg_status_reg; + int i, timeout = mbox_cmd->poll_interval; + + dev_dbg(dev, "Mailbox background operation (0x%04x) started\n", + mbox_cmd->opcode); + + for (i = 0; i < mbox_cmd->poll_count; i++) { + ret = rcuwait_wait_event_timeout(&cxlds->mbox_wait, + cxl_mbox_background_complete(cxlds), + TASK_INTERRUPTIBLE, + msecs_to_jiffies(timeout));Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ if (ret > 0) + break; + if (ret < 0) /* interrupted by a signal */Have you tested this path? I am curious what happens on the next command submission? As far as I can see foreground commands would not be impacted but the next background submission would fail instead of wait, right?
Quoted 4 lines
No, I haven't tested this path. My expectation here is that this would be similar to the timeout case in that the driver returns but the hw keeps processing the command. The next command, fg or bg, would be at the mercy of the hw doing the right thing until it's done with the canceled one.
Right, it's that "mercy" part where there needs to be documentation of what to expect.
If someone sends SIGTERM after triggering a scan media and then immediately resubmits it maybe the right answer is to go into another interruptible sleep awaiting the last background state to clear out. It just seems like returning an error leaves userspace to fend for itself with little visibility of what it needs to do next. SIGTERM during a background command means, "I do not care about the previous result". Given the driver is mitigating long running background cycles that implies that waiting before submitting the next colliding command would not be onerous. They can always SIGTERM again if the wait gets too long.
Quoted 8 lines
Quoted 4 lines
Perhaps a
dev_err_ratelimited()message and an EBUSY status for when that happens to at least indicate, "hardware is still chewing on a cancelled request". Later we can think about whether that's recoverable via a reset.Well but reaching this path already assumes hw didn't error out the current command because the canceled one was still being run.
Quoted 7 lines
Quoted 2 lines
Alternatively we could also just make the sleep uninterruptible, but because this comes from the user triggering the bg op, I thought against it.
I don't think that would solve the problem of how to detect when the driver is out of sync with the device and what to do to resolve that situation. An inopportune SIGTERM is indistinguishable from getting the polling interval wrong.
It at least removes one point of potential disambiguity. This out of sync issue with timeouts was one of the reasons I originally proposed dealing with all bg commands asynchronously (which doesn't prevent users from still time-slicing). This approach is supposed to make the code simpler, but leaves this mess. Currently we state:
* 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.
Quoted 58 lines
Quoted 54 lines
Quoted 3 lines
Review
Quoted 3 lines
(quoted diff) +1
-0+ return ret;rcuwait, unlike regular wait, returns
-EINTRinstead of-ERESTARTSYS. I've updated the return value here to the latter.Quoted 38 lines
Quoted 38 lines
(quoted diff) +21
-0+ } + + if (!cxl_mbox_background_complete(cxlds)) { + u64 md_status = + readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET); + + cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, + "background timeout"); + return -ETIMEDOUT; + } + + 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) { dev_dbg(dev, "Mailbox operation had an error: %s\n", cxl_mbox_cmd_rc2str(mbox_cmd));(quoted diff) +10
-0@@ -271,6 +350,29 @@ static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) dev_dbg(cxlds->dev, "Mailbox payload sized %zu", cxlds->payload_size); + rcuwait_init(&cxlds->mbox_wait); + if (cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) { + int irq, msgnum; + struct pci_dev *pdev = to_pci_dev(cxlds->dev); + + msgnum = FIELD_GET(CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK, cap); + irq = pci_irq_vector(pdev, msgnum); + if (irq < 0) + goto mbox_poll; +Quoted 8 lines
Review static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 364
Quoted 2 lines
(quoted diff) +2
-0+ if (devm_request_irq(cxlds->dev, irq, cxl_pci_mbox_irq, + IRQF_SHARED, NULL, cxlds))This needs
IRQF_ONESHOTand that @dev_id needs to be globally unique in case @irq is shared. So I think you want to factor out a common helper fromcxl_event_req_irq()that this can reuse. I.e.cxl_event_req_irq()already gets the flags and @dev_id right, so just create acxl_req_irq()that takes the irq as an argument and share it.Quoted 3 lines
For the oneshot this was a hardirq, so I don't think this is needed, but regardless, I've added the following helper in a new patch which both events and mbox can call:
Quoted 5 lines
Ah, right, this was hard-irq only, I missed that.
I wouldn't be opposed to
cxl_request_irq()following pci_request_irq()'s lead and taking multiple handler arguments to skip the thread invocation when it is not needed.
Sure. I was fine with just doing everything in the BH, but allowing that flexibility seems best.
Quoted 29 lines
Quoted 29 lines
static int cxl_request_irq(struct
cxl_dev_state*cxlds,
int irq,irq_handler_thandler){ struct device *dev = cxlds->dev; struct cxl_dev_id *dev_id; /* dev_id must be globally unique and must contain the cxlds */ dev_id = devm_kzalloc(dev, sizeof(*dev_id), GFP_KERNEL); if (!dev_id) return -ENOMEM; dev_id->cxlds = cxlds; return devm_request_threaded_irq(dev, irq, NULL, handler, IRQF_SHARED | IRQF_ONESHOT, NULL, dev_id); }Quoted 10 lines
Review static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 369
Quoted 6 lines
(quoted diff) +5
-0+ goto mbox_poll; + + /* only enable background cmd mbox irq support */ + writel(CXLDEV_MBOX_CTRL_BG_CMD_IRQ, + cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);Just in case some other code ever enables doorbell interrupts it would be nice if this performed a read-modify-write rather than assuming it can clobber that setting.
Quoted 8 lines
Quoted 1 line
Hmm do you mean enabling/disabling dynamically?
No, grep for:
"write.*CTRL"
...and observe that all writes to control registers are doing a read-modify-write sequence.
Oh, ok I get it.
Quoted 8 lines
Quoted 4 lines
Otherwise I would have
expected this to be a one time thing which we could just OR both bits. Maybe this could be revisited when needed? Jonathan suggested enabling doorbell irqs in the future, which I plan on looking into at some point.Sure, that code might be in another function also performing its own read-modify-write, so my comment just asking for this to be consistent with other control register updates in the driver.
Thanks for clarifying, I had not seen it like that - will definitely update in the next version.
Thanks,
Davidlohr
Re: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 9 lines · Davidlohr Bueso
It at least removes one point of potential disambiguity. This out of sync issue with timeouts was one of the reasons I originally proposed dealing with all bg commands asynchronously (which doesn't prevent users from still time-slicing). This approach is supposed to make the code simpler, but leaves this mess. Currently we state:
* 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.
Right, but my struggle is how to explain to userspace what to do next. Does userspace keep submitting until it gets a successful acceptance? How does userspace know that its failure is induced by background command collision?
That's why I think it is ok to block and wait on the previous background command completion when the next command is submitted. That seems to satisfy least surprise. The submitter asked for the last command to end in failure and is asking for the next one to queue up and complete.
I think this means that cxl_mbox_background_complete() needs to be called before every submission and needs to check for "background operation" (mailbox status bit0) being cleared to manage potential collisions.
Davidlohr Bueso
maintainerRe: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 7 lines · Dan Williams
Quoted 6 lines · Davidlohr Bueso
It at least removes one point of potential disambiguity. This out of sync issue with timeouts was one of the reasons I originally proposed dealing with all bg commands asynchronously (which doesn't prevent users from still time-slicing). This approach is supposed to make the code simpler, but leaves this mess. Currently we state:
Quoted 6 lines
Quoted 3 lines
* 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.Right, but my struggle is how to explain to userspace what to do next. Does userspace keep submitting until it gets a successful acceptance?
Yes. It would be the users' responsibility to deal with timeouts.
Quoted 2 lines
How does userspace know that its failure is induced by background command collision?
Knowing about the apriori timeout, would the user care?
Quoted 9 lines
That's why I think it is ok to block and wait on the previous background command completion when the next command is submitted. That seems to satisfy least surprise. The submitter asked for the last command to end in failure and is asking for the next one to queue up and complete.
I think this means that
cxl_mbox_background_complete()needs to be called before every submission and needs to check for "background operation" (mailbox status bit0) being cleared to manage potential collisions.
All this really defeats the "simpler" angle. Without accepting that it's the user who needs to resubmit until in sync, it seems doing synchronously is sw shooting itself in the foot.
Thanks,
Davidlohr
Re: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 16 lines · Davidlohr Bueso
Quoted 7 lines · Dan Williams
Quoted 6 lines · Davidlohr Bueso
It at least removes one point of potential disambiguity. This out of sync issue with timeouts was one of the reasons I originally proposed dealing with all bg commands asynchronously (which doesn't prevent users from still time-slicing). This approach is supposed to make the code simpler, but leaves this mess. Currently we state:
Quoted 6 lines
Quoted 3 lines
* 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.Right, but my struggle is how to explain to userspace what to do next. Does userspace keep submitting until it gets a successful acceptance?
Quoted 6 lines
Yes. It would be the users' responsibility to deal with timeouts.
Quoted 2 lines
How does userspace know that its failure is induced by background command collision?
Knowing about the apriori timeout, would the user care?
It's not just timeouts its also SIGTERM. If the user is allowed to say "I do not care about the previous completion" then the driver should have some way to get back in sync. Either the next submission needs to wait, or there needs to be a facility to flush previous commands.
Quoted 5 lines
Quoted 5 lines
That's why I think it is ok to block and wait on the previous background command completion when the next command is submitted. That seems to satisfy least surprise. The submitter asked for the last command to end in failure and is asking for the next one to queue up and complete.
Quoted 7 lines
Quoted 3 lines
I think this means that
cxl_mbox_background_complete()needs to be called before every submission and needs to check for "background operation" (mailbox status bit0) being cleared to manage potential collisions.All this really defeats the "simpler" angle. Without accepting that it's the user who needs to resubmit until in sync, it seems doing synchronously is sw shooting itself in the foot.
Make it as simple as possible, but no simpler. The driver saying "you are on your own after first cancellation or timeout" is too simple. Especially when the driver designed with an eye towards not allowing long running background commands be submitted (outside of the special sanitization consideration).
So the choices are either allow the background command wait to be interruptible and then do an implied sync on the next submission, or make it uninterruptible where timeouts are either a real device problem or a driver issue that needs to reduce the size of the background operation to fit the timeout.
This middle ground of allowing SIGTERM and then userspace is on its own to repair the connection violates least surprise.
Davidlohr Bueso
maintainerRe: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 5 lines · Dan Williams
So the choices are either allow the background command wait to be interruptible and then do an implied sync on the next submission, or make it uninterruptible where timeouts are either a real device problem or a driver issue that needs to reduce the size of the background operation to fit the timeout.
I obviously vote for the uninterruptible option and treat timeouts as a rare occurrence. In my answers I was only mentioning timeout case because of that uninterruptible wait context. The amount of bg capable commands are limited, and would expect timeout parameters to be fairly well inspected(?).
So if ok with you a v3 would have these semantics.
Thanks,
Davidlohr
Re: [PATCH v2] cxl/mbox: Add background cmd handling machinery
Quoted 8 lines · Davidlohr Bueso
Quoted 5 lines · Dan Williams
So the choices are either allow the background command wait to be interruptible and then do an implied sync on the next submission, or make it uninterruptible where timeouts are either a real device problem or a driver issue that needs to reduce the size of the background operation to fit the timeout.
Quoted 7 lines
I obviously vote for the uninterruptible option and treat timeouts as a rare occurrence. In my answers I was only mentioning timeout case because of that uninterruptible wait context. The amount of bg capable commands are limited, and would expect timeout parameters to be fairly well inspected(?).
So if ok with you a v3 would have these semantics.
Sounds good, we can always complicate later if need be.