ethdev: fix rte_eth_dev_owner_unset

Message ID 20180814001926.19630-1-stephen@networkplumber.org
State Rejected, archived
Delegated to: Ferruh Yigit
Headers show
Series
  • ethdev: fix rte_eth_dev_owner_unset
Related show

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Aug. 14, 2018, 12:19 a.m.
The rte_eth_dev_owner_unset function is unusable because
it always returns -EINVAL. This is because the magic (unowned)
value is flagged as not valid.

Move the validation of owner into set and unset as
separate calls.

Fixes: 5b7ba31148a8 ("ethdev: add port ownership")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/librte_ethdev/rte_ethdev.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Matan Azrad Aug. 14, 2018, 5:52 a.m. | #1
Hi Stephen

From: Stephen Hemminger
> The rte_eth_dev_owner_unset function is unusable because it always
> returns -EINVAL. This is because the magic (unowned) value is flagged as not
> valid.
> 

It's OK to raise an error when you do unset for unowned device.
It means that unset owner should be called for owned device.

> Move the validation of owner into set and unset as separate calls.
> 
> Fixes: 5b7ba31148a8 ("ethdev: add port ownership")
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/librte_ethdev/rte_ethdev.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c
> index 4c320250589a..9398550a1189 100644
> --- a/lib/librte_ethdev/rte_ethdev.c
> +++ b/lib/librte_ethdev/rte_ethdev.c
> @@ -443,10 +443,6 @@ _rte_eth_dev_owner_set(const uint16_t port_id,
> const uint64_t old_owner_id,
>  		return -ENODEV;
>  	}
> 
> -	if (!rte_eth_is_valid_owner_id(new_owner->id) &&
> -	    !rte_eth_is_valid_owner_id(old_owner_id))
> -		return -EINVAL;
> -
>  	port_owner = &rte_eth_devices[port_id].data->owner;
>  	if (port_owner->id != old_owner_id) {
>  		RTE_ETHDEV_LOG(ERR,
> @@ -475,6 +471,9 @@ rte_eth_dev_owner_set(const uint16_t port_id,  {
>  	int ret;
> 
> +	if (!rte_eth_is_valid_owner_id(owner->id))
> +		return -EINVAL;
> +
>  	rte_eth_dev_shared_data_prepare();
> 
>  	rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
> @@ -492,6 +491,9 @@ rte_eth_dev_owner_unset(const uint16_t port_id,
> const uint64_t owner_id)
>  			{.id = RTE_ETH_DEV_NO_OWNER, .name = ""};
>  	int ret;
> 
> +	if (!rte_eth_is_valid_owner_id(owner_id))
> +		return -EINVAL;
> +
>  	rte_eth_dev_shared_data_prepare();
> 
>  	rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
> --
> 2.18.0
Stephen Hemminger Aug. 14, 2018, 4:55 p.m. | #2
On Tue, 14 Aug 2018 05:52:20 +0000
Matan Azrad <matan@mellanox.com> wrote:

> Hi Stephen
> 
> From: Stephen Hemminger
> > The rte_eth_dev_owner_unset function is unusable because it always
> > returns -EINVAL. This is because the magic (unowned) value is flagged as not
> > valid.
> >   
> 
> It's OK to raise an error when you do unset for unowned device.
> It means that unset owner should be called for owned device.
> 

Original code was broken. The following would always fail.

	rte_eth_dev_owner_new(&owner.id);
	sprintf(owner.name, "example");
	rte_eth_dev_owner_set(port_id, &owner);
	rte_eth_dev_owner_unset(port_id, owner.id);

That is because of:
	rte_eth_dev_owner_unset(port_id, owner_id)
		_rte_eth_dev_owner_set(port_id, owner_id, &new_owner)  << new_owner.id == RTE_ETH_DEV_NO_OWNER (0)


	if (!rte_eth_is_valid_owner_id(new_owner->id) &&  << new_owner->id == RTE_ETH_DEV_NO_OWNER (which is flagged as invalid)
	    !rte_eth_is_valid_owner_id(old_owner_id))
		return -EINVAL;

The failsafe driver never checks the return value, and therefore doesn't see that it never clears ownership.
Matan Azrad Aug. 14, 2018, 7:46 p.m. | #3
Hi Stephen

From: Stephen Hemminger
> On Tue, 14 Aug 2018 05:52:20 +0000
> Matan Azrad <matan@mellanox.com> wrote:
> 
> > Hi Stephen
> >
> > From: Stephen Hemminger
> > > The rte_eth_dev_owner_unset function is unusable because it always
> > > returns -EINVAL. This is because the magic (unowned) value is
> > > flagged as not valid.
> > >
> >
> > It's OK to raise an error when you do unset for unowned device.
> > It means that unset owner should be called for owned device.
> >
> 
> Original code was broken. The following would always fail.
> 
> 	rte_eth_dev_owner_new(&owner.id);
> 	sprintf(owner.name, "example");
> 	rte_eth_dev_owner_set(port_id, &owner);
> 	rte_eth_dev_owner_unset(port_id, owner.id);
> 
> That is because of:
> 	rte_eth_dev_owner_unset(port_id, owner_id)
> 		_rte_eth_dev_owner_set(port_id, owner_id, &new_owner)
> << new_owner.id == RTE_ETH_DEV_NO_OWNER (0)
> 
> 
> 	if (!rte_eth_is_valid_owner_id(new_owner->id) &&  <<
> new_owner->id == RTE_ETH_DEV_NO_OWNER (which is flagged as invalid)
> 	    !rte_eth_is_valid_owner_id(old_owner_id))
> 		return -EINVAL;
> 

But both should be invalid the new owner and the old owner(&&) to raise an EINVAL error.

In the aforementioned check above the old owner should be valid.

> The failsafe driver never checks the return value, and therefore doesn't see
> that it never clears ownership.
Ferruh Yigit Aug. 24, 2018, 1:55 p.m. | #4
On 8/14/2018 8:46 PM, Matan Azrad wrote:
> Hi Stephen
> 
> From: Stephen Hemminger
>> On Tue, 14 Aug 2018 05:52:20 +0000
>> Matan Azrad <matan@mellanox.com> wrote:
>>
>>> Hi Stephen
>>>
>>> From: Stephen Hemminger
>>>> The rte_eth_dev_owner_unset function is unusable because it always
>>>> returns -EINVAL. This is because the magic (unowned) value is
>>>> flagged as not valid.
>>>>
>>>
>>> It's OK to raise an error when you do unset for unowned device.
>>> It means that unset owner should be called for owned device.
>>>
>>
>> Original code was broken. The following would always fail.
>>
>> 	rte_eth_dev_owner_new(&owner.id);
>> 	sprintf(owner.name, "example");
>> 	rte_eth_dev_owner_set(port_id, &owner);
>> 	rte_eth_dev_owner_unset(port_id, owner.id);
>>
>> That is because of:
>> 	rte_eth_dev_owner_unset(port_id, owner_id)
>> 		_rte_eth_dev_owner_set(port_id, owner_id, &new_owner)
>> << new_owner.id == RTE_ETH_DEV_NO_OWNER (0)
>>
>>
>> 	if (!rte_eth_is_valid_owner_id(new_owner->id) &&  <<
>> new_owner->id == RTE_ETH_DEV_NO_OWNER (which is flagged as invalid)
>> 	    !rte_eth_is_valid_owner_id(old_owner_id))
>> 		return -EINVAL;
>>
> 
> But both should be invalid the new owner and the old owner(&&) to raise an EINVAL error.
> 
> In the aforementioned check above the old owner should be valid.

It looks rte_eth_dev_owner_unset() works, updating patch as rejected.
Stephen Hemminger Aug. 24, 2018, 2:42 p.m. | #5
On Fri, 24 Aug 2018 14:55:56 +0100
Ferruh Yigit <ferruh.yigit@intel.com> wrote:

> On 8/14/2018 8:46 PM, Matan Azrad wrote:
> > Hi Stephen
> > 
> > From: Stephen Hemminger  
> >> On Tue, 14 Aug 2018 05:52:20 +0000
> >> Matan Azrad <matan@mellanox.com> wrote:
> >>  
> >>> Hi Stephen
> >>>
> >>> From: Stephen Hemminger  
> >>>> The rte_eth_dev_owner_unset function is unusable because it always
> >>>> returns -EINVAL. This is because the magic (unowned) value is
> >>>> flagged as not valid.
> >>>>  
> >>>
> >>> It's OK to raise an error when you do unset for unowned device.
> >>> It means that unset owner should be called for owned device.
> >>>  
> >>
> >> Original code was broken. The following would always fail.
> >>
> >> 	rte_eth_dev_owner_new(&owner.id);
> >> 	sprintf(owner.name, "example");
> >> 	rte_eth_dev_owner_set(port_id, &owner);
> >> 	rte_eth_dev_owner_unset(port_id, owner.id);
> >>
> >> That is because of:
> >> 	rte_eth_dev_owner_unset(port_id, owner_id)
> >> 		_rte_eth_dev_owner_set(port_id, owner_id, &new_owner)
> >> << new_owner.id == RTE_ETH_DEV_NO_OWNER (0)
> >>
> >>
> >> 	if (!rte_eth_is_valid_owner_id(new_owner->id) &&  <<
> >> new_owner->id == RTE_ETH_DEV_NO_OWNER (which is flagged as invalid)
> >> 	    !rte_eth_is_valid_owner_id(old_owner_id))
> >> 		return -EINVAL;
> >>  
> > 
> > But both should be invalid the new owner and the old owner(&&) to raise an EINVAL error.
> > 
> > In the aforementioned check above the old owner should be valid.  
> 
> It looks rte_eth_dev_owner_unset() works, updating patch as rejected.
> 

The issue was the incorrect log message, addressed in later patches.

Patch

diff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c
index 4c320250589a..9398550a1189 100644
--- a/lib/librte_ethdev/rte_ethdev.c
+++ b/lib/librte_ethdev/rte_ethdev.c
@@ -443,10 +443,6 @@  _rte_eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id,
 		return -ENODEV;
 	}
 
-	if (!rte_eth_is_valid_owner_id(new_owner->id) &&
-	    !rte_eth_is_valid_owner_id(old_owner_id))
-		return -EINVAL;
-
 	port_owner = &rte_eth_devices[port_id].data->owner;
 	if (port_owner->id != old_owner_id) {
 		RTE_ETHDEV_LOG(ERR,
@@ -475,6 +471,9 @@  rte_eth_dev_owner_set(const uint16_t port_id,
 {
 	int ret;
 
+	if (!rte_eth_is_valid_owner_id(owner->id))
+		return -EINVAL;
+
 	rte_eth_dev_shared_data_prepare();
 
 	rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
@@ -492,6 +491,9 @@  rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id)
 			{.id = RTE_ETH_DEV_NO_OWNER, .name = ""};
 	int ret;
 
+	if (!rte_eth_is_valid_owner_id(owner_id))
+		return -EINVAL;
+
 	rte_eth_dev_shared_data_prepare();
 
 	rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);