From: Dave Jiang <dave.jiang@intel.com>
To: jdmason@kudzu.us, allenbh@gmail.com, ntb@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] ntb: Add mutex to make link_event_callback executed linearly.
Date: Fri, 22 Aug 2025 08:32:21 -0700 [thread overview]
Message-ID: <c85a5b3e-569c-4356-935a-19fdd8248ff7@intel.com> (raw)
In-Reply-To: <aKiBi4ZDlbgzed/z@didi-ThinkCentre-M930t-N000>
On 8/22/25 7:41 AM, fuyuanli wrote:
> Since the CPU selected by schedule_work is uncertain, multiple link_event
> callbacks may be executed at same time. For example, after peer's link
> is up, it is down quickly before local link_work completed. If link_cleanup
> is added to the workqueue of another CPU, then link_work and link_cleanup
> may be executed at the same time. So add a mutex to prevent them from being
> executed concurrently.
>
> Signed-off-by: fuyuanli <fuyuanli@didiglobal.com>
> ---
> drivers/ntb/ntb_transport.c | 14 +++++++++++++-
> 1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> index 4f775c3e218f..902968e24c7a 100644
> --- a/drivers/ntb/ntb_transport.c
> +++ b/drivers/ntb/ntb_transport.c
> @@ -59,6 +59,7 @@
> #include <linux/slab.h>
> #include <linux/types.h>
> #include <linux/uaccess.h>
> +#include <linux/mutex.h>
> #include "linux/ntb.h"
> #include "linux/ntb_transport.h"
>
> @@ -241,6 +242,9 @@ struct ntb_transport_ctx {
> struct work_struct link_cleanup;
>
> struct dentry *debugfs_node_dir;
> +
> + /* Make sure workq of link event be executed serially */
> + struct mutex link_event_lock;
> };
>
> enum {
> @@ -1024,7 +1028,9 @@ static void ntb_transport_link_cleanup_work(struct work_struct *work)
> struct ntb_transport_ctx *nt =
> container_of(work, struct ntb_transport_ctx, link_cleanup);
>
> + mutex_lock(&nt->link_event_lock);
Can you please use guard() instead? Should produce less code and not worry about calling all the unlocks.
DJ
> ntb_transport_link_cleanup(nt);
> + mutex_unlock(&nt->link_event_lock);
> }
>
> static void ntb_transport_event_callback(void *data)
> @@ -1047,6 +1053,8 @@ static void ntb_transport_link_work(struct work_struct *work)
> u32 val;
> int rc = 0, i, spad;
>
> + mutex_lock(&nt->link_event_lock);
> +
> /* send the local info, in the opposite order of the way we read it */
>
> if (nt->use_msi) {
> @@ -1125,6 +1133,7 @@ static void ntb_transport_link_work(struct work_struct *work)
> schedule_delayed_work(&qp->link_work, 0);
> }
>
> + mutex_unlock(&nt->link_event_lock);
> return;
>
> out1:
> @@ -1132,10 +1141,13 @@ static void ntb_transport_link_work(struct work_struct *work)
> ntb_free_mw(nt, i);
>
> /* if there's an actual failure, we should just bail */
> - if (rc < 0)
> + if (rc < 0) {
> + mutex_unlock(&nt->link_event_lock);
> return;
> + }
>
> out:
> + mutex_unlock(&nt->link_event_lock);
> if (ntb_link_is_up(ndev, NULL, NULL) == 1)
> schedule_delayed_work(&nt->link_work,
> msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
prev parent reply other threads:[~2025-08-22 15:32 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-22 14:41 [PATCH] ntb: Add mutex to make link_event_callback executed linearly fuyuanli
2025-08-22 15:32 ` Dave Jiang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c85a5b3e-569c-4356-935a-19fdd8248ff7@intel.com \
--to=dave.jiang@intel.com \
--cc=allenbh@gmail.com \
--cc=jdmason@kudzu.us \
--cc=linux-kernel@vger.kernel.org \
--cc=ntb@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox