diff mbox series

[v4,3/3] eal: fix a wrong returned value when callback exists

Message ID 3b5329802b1628466e4984de4bde8ac8b6e11508.1593768308.git.wangyunjian@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers show
Series fixes for device event | expand

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/travis-robot success Travis build: passed
ci/checkpatch success coding style OK

Commit Message

wangyunjian July 3, 2020, 9:46 a.m. UTC
From: Yunjian Wang <wangyunjian@huawei.com>

We should return an error value, when the callback is already exist.

Fixes: a753e53d517b ("eal: add device event monitor framework")
Cc: stable@dpdk.org

Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
---
 lib/librte_eal/common/eal_common_dev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

David Marchand Oct. 20, 2020, 1:18 p.m. UTC | #1
On Fri, Jul 3, 2020 at 11:47 AM wangyunjian <wangyunjian@huawei.com> wrote:
>
> From: Yunjian Wang <wangyunjian@huawei.com>
>
> We should return an error value, when the callback is already exist.
>
> Fixes: a753e53d517b ("eal: add device event monitor framework")
> Cc: stable@dpdk.org
>
> Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> ---
>  lib/librte_eal/common/eal_common_dev.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
> index d990bfd..2a097aa 100644
> --- a/lib/librte_eal/common/eal_common_dev.c
> +++ b/lib/librte_eal/common/eal_common_dev.c
> @@ -431,7 +431,7 @@ static int cmp_dev_name(const struct rte_device *dev, const void *_name)
>                                 void *cb_arg)
>  {
>         struct dev_event_callback *event_cb;
> -       int ret;
> +       int ret = 0;
>
>         if (!cb_fn)
>                 return -EINVAL;
> @@ -484,7 +484,7 @@ static int cmp_dev_name(const struct rte_device *dev, const void *_name)
>         }
>
>         rte_spinlock_unlock(&dev_event_lock);
> -       return 0;
> +       return ret;
>  error:
>         free(event_cb);
>         rte_spinlock_unlock(&dev_event_lock);
> --
> 1.8.3.1

A simpler fix is to directly jump to the error label.
This has the advantage of having all errors go through a single cleanup code:

diff --git a/lib/librte_eal/common/eal_common_dev.c
b/lib/librte_eal/common/eal_common_dev.c
index 9e4f09d83e..fa47074b0b 100644
--- a/lib/librte_eal/common/eal_common_dev.c
+++ b/lib/librte_eal/common/eal_common_dev.c
@@ -480,7 +480,9 @@ rte_dev_event_callback_register(const char *device_name,
                RTE_LOG(ERR, EAL,
                        "The callback is already exist, no need "
                        "to register again.\n");
+               event_cb = NULL;
                ret = -EEXIST;
+               goto error;
        }

        rte_spinlock_unlock(&dev_event_lock);

What do you think?
wangyunjian Oct. 20, 2020, 2:31 p.m. UTC | #2
> -----Original Message-----
> From: David Marchand [mailto:david.marchand@redhat.com]
> Sent: Tuesday, October 20, 2020 9:19 PM
> To: wangyunjian <wangyunjian@huawei.com>; Jeff Guo <jia.guo@intel.com>
> Cc: dev <dev@dpdk.org>; Lilijun (Jerry) <jerry.lilijun@huawei.com>; xudingke
> <xudingke@huawei.com>; dpdk stable <stable@dpdk.org>
> Subject: Re: [dpdk-stable] [dpdk-dev] [PATCH v4 3/3] eal: fix a wrong returned
> value when callback exists
> 
> On Fri, Jul 3, 2020 at 11:47 AM wangyunjian <wangyunjian@huawei.com>
> wrote:
> >
> > From: Yunjian Wang <wangyunjian@huawei.com>
> >
> > We should return an error value, when the callback is already exist.
> >
> > Fixes: a753e53d517b ("eal: add device event monitor framework")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> > ---
> >  lib/librte_eal/common/eal_common_dev.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/lib/librte_eal/common/eal_common_dev.c
> > b/lib/librte_eal/common/eal_common_dev.c
> > index d990bfd..2a097aa 100644
> > --- a/lib/librte_eal/common/eal_common_dev.c
> > +++ b/lib/librte_eal/common/eal_common_dev.c
> > @@ -431,7 +431,7 @@ static int cmp_dev_name(const struct rte_device
> *dev, const void *_name)
> >                                 void *cb_arg)  {
> >         struct dev_event_callback *event_cb;
> > -       int ret;
> > +       int ret = 0;
> >
> >         if (!cb_fn)
> >                 return -EINVAL;
> > @@ -484,7 +484,7 @@ static int cmp_dev_name(const struct rte_device
> *dev, const void *_name)
> >         }
> >
> >         rte_spinlock_unlock(&dev_event_lock);
> > -       return 0;
> > +       return ret;
> >  error:
> >         free(event_cb);
> >         rte_spinlock_unlock(&dev_event_lock);
> > --
> > 1.8.3.1
> 
> A simpler fix is to directly jump to the error label.
> This has the advantage of having all errors go through a single cleanup code:
> 
> diff --git a/lib/librte_eal/common/eal_common_dev.c
> b/lib/librte_eal/common/eal_common_dev.c
> index 9e4f09d83e..fa47074b0b 100644
> --- a/lib/librte_eal/common/eal_common_dev.c
> +++ b/lib/librte_eal/common/eal_common_dev.c
> @@ -480,7 +480,9 @@ rte_dev_event_callback_register(const char
> *device_name,
>                 RTE_LOG(ERR, EAL,
>                         "The callback is already exist, no need "
>                         "to register again.\n");
> +               event_cb = NULL;
>                 ret = -EEXIST;
> +               goto error;
>         }
> 
>         rte_spinlock_unlock(&dev_event_lock);
> 
> What do you think?

Agree, I will update it in next version.

Yunjian

> 
> 
> --
> David Marchand
diff mbox series

Patch

diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
index d990bfd..2a097aa 100644
--- a/lib/librte_eal/common/eal_common_dev.c
+++ b/lib/librte_eal/common/eal_common_dev.c
@@ -431,7 +431,7 @@  static int cmp_dev_name(const struct rte_device *dev, const void *_name)
 				void *cb_arg)
 {
 	struct dev_event_callback *event_cb;
-	int ret;
+	int ret = 0;
 
 	if (!cb_fn)
 		return -EINVAL;
@@ -484,7 +484,7 @@  static int cmp_dev_name(const struct rte_device *dev, const void *_name)
 	}
 
 	rte_spinlock_unlock(&dev_event_lock);
-	return 0;
+	return ret;
 error:
 	free(event_cb);
 	rte_spinlock_unlock(&dev_event_lock);