[1/2] eal/hotplug: allow monitor to be setup by multiple places
diff mbox series

Message ID 1606809383-26660-1-git-send-email-longli@linuxonhyperv.com
State Superseded
Delegated to: Thomas Monjalon
Headers show
Series
  • [1/2] eal/hotplug: allow monitor to be setup by multiple places
Related show

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Long Li Dec. 1, 2020, 7:56 a.m. UTC
From: Stephen Hemminger <stephen@networkplumber.org>

In some cases, a device or infrastructure may want to enable hotplug
but application may also try and start hotplug as well. Therefore
change the monitor_started from a boolean into a reference count.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Long Li <longli@microsoft.com>
---
 lib/librte_eal/linux/eal_dev.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

Comments

Luca Boccassi Dec. 17, 2020, 3:08 p.m. UTC | #1
On Mon, 2020-11-30 at 23:56 -0800, Long Li wrote:
> From: Stephen Hemminger <stephen@networkplumber.org>
> 
> In some cases, a device or infrastructure may want to enable hotplug
> but application may also try and start hotplug as well. Therefore
> change the monitor_started from a boolean into a reference count.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> Signed-off-by: Long Li <longli@microsoft.com>
> ---
>  lib/librte_eal/linux/eal_dev.c | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/lib/librte_eal/linux/eal_dev.c b/lib/librte_eal/linux/eal_dev.c
> index 5c0e752b2d..5fa679989e 100644
> --- a/lib/librte_eal/linux/eal_dev.c
> +++ b/lib/librte_eal/linux/eal_dev.c
> @@ -23,8 +23,11 @@
>  
>  #include "eal_private.h"
>  
> -static struct rte_intr_handle intr_handle = {.fd = -1 };
> -static bool monitor_started;
> +static struct rte_intr_handle intr_handle = {
> +	.type = RTE_INTR_HANDLE_DEV_EVENT,
> +	.fd = -1,
> +};
> +static uint32_t monitor_refcount;
>  static bool hotplug_handle;
>  
>  #define EAL_UEV_MSG_LEN 4096
> @@ -300,7 +303,7 @@ rte_dev_event_monitor_start(void)
>  {
>  	int ret;
>  
> -	if (monitor_started)
> +	if (__atomic_fetch_add(&monitor_refcount, 1, __ATOMIC_RELAXED))
>  		return 0;

If dev_uev_socket_fd_create or rte_intr_callback_register fail, you'll
have incremented the refcount but there won't be anything registered,
and calls to rte_dev_event_monitor_start will do nothing. Will that be
a problem?

In other words, it seems to me the semantics is changing from call
rte_dev_event_monitor_start until it succeeds, to call
rte_dev_event_monitor_start, and if it fails call
rte_dev_event_monitor_stop and then rte_dev_event_monitor_start again

>  	ret = dev_uev_socket_fd_create();
> @@ -309,7 +312,6 @@ rte_dev_event_monitor_start(void)
>  		return -1;
>  	}
>  
> -	intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT;
>  	ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL);
>  
>  	if (ret) {
> @@ -317,8 +319,6 @@ rte_dev_event_monitor_start(void)
>  		return -1;
>  	}
>  
> -	monitor_started = true;
> -
>  	return 0;
>  }
>  
> @@ -327,7 +327,7 @@ rte_dev_event_monitor_stop(void)
>  {
>  	int ret;
>  
> -	if (!monitor_started)
> +	if (__atomic_sub_fetch(&monitor_refcount, 1, __ATOMIC_RELAXED))
>  		return 0;

Same question in reverse - if rte_intr_callback_unregister fails, the
refcount will have been decreased anyway, so nothing will close the
file handle, right?

>  	ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
> @@ -339,7 +339,6 @@ rte_dev_event_monitor_stop(void)
>  
>  	close(intr_handle.fd);
>  	intr_handle.fd = -1;
> -	monitor_started = false;
>  
>  	return 0;
>  }
Long Li Dec. 17, 2020, 10:37 p.m. UTC | #2
> Subject: Re: [dpdk-dev] [PATCH 1/2] eal/hotplug: allow monitor to be setup
> by multiple places
> 
> On Mon, 2020-11-30 at 23:56 -0800, Long Li wrote:
> > From: Stephen Hemminger <stephen@networkplumber.org>
> >
> > In some cases, a device or infrastructure may want to enable hotplug
> > but application may also try and start hotplug as well. Therefore
> > change the monitor_started from a boolean into a reference count.
> >
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> > Signed-off-by: Long Li <longli@microsoft.com>
> > ---
> >  lib/librte_eal/linux/eal_dev.c | 15 +++++++--------
> >  1 file changed, 7 insertions(+), 8 deletions(-)
> >
> > diff --git a/lib/librte_eal/linux/eal_dev.c
> > b/lib/librte_eal/linux/eal_dev.c index 5c0e752b2d..5fa679989e 100644
> > --- a/lib/librte_eal/linux/eal_dev.c
> > +++ b/lib/librte_eal/linux/eal_dev.c
> > @@ -23,8 +23,11 @@
> >
> >  #include "eal_private.h"
> >
> > -static struct rte_intr_handle intr_handle = {.fd = -1 }; -static bool
> > monitor_started;
> > +static struct rte_intr_handle intr_handle = {
> > +	.type = RTE_INTR_HANDLE_DEV_EVENT,
> > +	.fd = -1,
> > +};
> > +static uint32_t monitor_refcount;
> >  static bool hotplug_handle;
> >
> >  #define EAL_UEV_MSG_LEN 4096
> > @@ -300,7 +303,7 @@ rte_dev_event_monitor_start(void)  {
> >  	int ret;
> >
> > -	if (monitor_started)
> > +	if (__atomic_fetch_add(&monitor_refcount, 1,
> __ATOMIC_RELAXED))
> >  		return 0;
> 
> If dev_uev_socket_fd_create or rte_intr_callback_register fail, you'll have
> incremented the refcount but there won't be anything registered, and calls
> to rte_dev_event_monitor_start will do nothing. Will that be a problem?
> 
> In other words, it seems to me the semantics is changing from call
> rte_dev_event_monitor_start until it succeeds, to call
> rte_dev_event_monitor_start, and if it fails call
> rte_dev_event_monitor_stop and then rte_dev_event_monitor_start again
> 
> >  	ret = dev_uev_socket_fd_create();
> > @@ -309,7 +312,6 @@ rte_dev_event_monitor_start(void)
> >  		return -1;
> >  	}
> >
> > -	intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT;
> >  	ret = rte_intr_callback_register(&intr_handle, dev_uev_handler,
> > NULL);
> >
> >  	if (ret) {
> > @@ -317,8 +319,6 @@ rte_dev_event_monitor_start(void)
> >  		return -1;
> >  	}
> >
> > -	monitor_started = true;
> > -
> >  	return 0;
> >  }
> >
> > @@ -327,7 +327,7 @@ rte_dev_event_monitor_stop(void)  {
> >  	int ret;
> >
> > -	if (!monitor_started)
> > +	if (__atomic_sub_fetch(&monitor_refcount, 1,
> __ATOMIC_RELAXED))
> >  		return 0;
> 
> Same question in reverse - if rte_intr_callback_unregister fails, the refcount
> will have been decreased anyway, so nothing will close the file handle, right?

I'm sending v2 to address the comments.

> 
> >  	ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
> @@
> > -339,7 +339,6 @@ rte_dev_event_monitor_stop(void)
> >
> >  	close(intr_handle.fd);
> >  	intr_handle.fd = -1;
> > -	monitor_started = false;
> >
> >  	return 0;
> >  }

Patch
diff mbox series

diff --git a/lib/librte_eal/linux/eal_dev.c b/lib/librte_eal/linux/eal_dev.c
index 5c0e752b2d..5fa679989e 100644
--- a/lib/librte_eal/linux/eal_dev.c
+++ b/lib/librte_eal/linux/eal_dev.c
@@ -23,8 +23,11 @@ 
 
 #include "eal_private.h"
 
-static struct rte_intr_handle intr_handle = {.fd = -1 };
-static bool monitor_started;
+static struct rte_intr_handle intr_handle = {
+	.type = RTE_INTR_HANDLE_DEV_EVENT,
+	.fd = -1,
+};
+static uint32_t monitor_refcount;
 static bool hotplug_handle;
 
 #define EAL_UEV_MSG_LEN 4096
@@ -300,7 +303,7 @@  rte_dev_event_monitor_start(void)
 {
 	int ret;
 
-	if (monitor_started)
+	if (__atomic_fetch_add(&monitor_refcount, 1, __ATOMIC_RELAXED))
 		return 0;
 
 	ret = dev_uev_socket_fd_create();
@@ -309,7 +312,6 @@  rte_dev_event_monitor_start(void)
 		return -1;
 	}
 
-	intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT;
 	ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL);
 
 	if (ret) {
@@ -317,8 +319,6 @@  rte_dev_event_monitor_start(void)
 		return -1;
 	}
 
-	monitor_started = true;
-
 	return 0;
 }
 
@@ -327,7 +327,7 @@  rte_dev_event_monitor_stop(void)
 {
 	int ret;
 
-	if (!monitor_started)
+	if (__atomic_sub_fetch(&monitor_refcount, 1, __ATOMIC_RELAXED))
 		return 0;
 
 	ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
@@ -339,7 +339,6 @@  rte_dev_event_monitor_stop(void)
 
 	close(intr_handle.fd);
 	intr_handle.fd = -1;
-	monitor_started = false;
 
 	return 0;
 }