# Re: \[PATCH V3 6/6\] vDPA: fix 'cast to restricted le16' warnings in vdpa.c

- From: Zhu, Lingshan \<hidden\>
- Date: 2022-07-29 09:35:45 
- List: netdev
- Page: https://ratatoskr.run/netdev/2022/07/11236248
- Thread: https://ratatoskr.run/netdev/2022/07/11233886/t/2.md

```text


On 7/29/2022 5:23 PM, Michael S. Tsirkin wrote:
> On Fri, Jul 29, 2022 at 05:20:17PM +0800, Zhu, Lingshan wrote:
>>
>> On 7/29/2022 5:17 PM, Michael S. Tsirkin wrote:
>>> On Fri, Jul 29, 2022 at 05:07:11PM +0800, Zhu, Lingshan wrote:
>>>> On 7/29/2022 4:53 PM, Michael S. Tsirkin wrote:
>>>>> On Fri, Jul 01, 2022 at 09:28:26PM +0800, Zhu Lingshan wrote:
>>>>>> This commit fixes spars warnings: cast to restricted __le16
>>>>>> in function vdpa_dev_net_config_fill() and
>>>>>> vdpa_fill_stats_rec()
>>>>>>
>>>>>> Signed-off-by: Zhu Lingshan <redacted>
>>>>>> ---
>>>>>>     drivers/vdpa/vdpa.c | 6 +++---
>>>>>>     1 file changed, 3 insertions(+), 3 deletions(-)
>>>>>>
>>>>>> diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
>>>>>> index 846dd37f3549..ed49fe46a79e 100644
>>>>>> --- a/drivers/vdpa/vdpa.c
>>>>>> +++ b/drivers/vdpa/vdpa.c
>>>>>> @@ -825,11 +825,11 @@ static int vdpa_dev_net_config_fill(struct vdpa_device *vdev, struct sk_buff *ms
>>>>>>     		    config.mac))
>>>>>>     		return -EMSGSIZE;
>>>>>> -	val_u16 = le16_to_cpu(config.status);
>>>>>> +	val_u16 = __virtio16_to_cpu(true, config.status);
>>>>>>     	if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_STATUS, val_u16))
>>>>>>     		return -EMSGSIZE;
>>>>>> -	val_u16 = le16_to_cpu(config.mtu);
>>>>>> +	val_u16 = __virtio16_to_cpu(true, config.mtu);
>>>>>>     	if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MTU, val_u16))
>>>>>>     		return -EMSGSIZE;
>>>>> Wrong on BE platforms with legacy interface, isn't it?
>>>>> We generally don't handle legacy properly in VDPA so it's
>>>>> not a huge deal, but maybe add a comment at least?
>>>> Sure, I can add a comment here: this is for modern devices only.
>>>>
>>>> Thanks,
>>>> Zhu Lingshan
>>> Hmm. what "this" is for modern devices only here?
>> this cast, for LE modern devices.
> I think status existed in legacy for sure, and it's possible that
> some legacy devices backported mtu and max_virtqueue_pairs otherwise
> we would have these fields as __le not as __virtio, right?
yes, that's the reason why it is virtio_16 than just le16.

I may find a better solution to detect whether it is LE, or BE without a virtio_dev structure.
Check whether vdpa_device->get_device_features() has VIRTIO_F_VERISON_1. If the device offers _F_VERSION_1, then it is a LE device,
or it is a BE device, then we use __virtio16_to_cpu(false, config.status).

Does this look good?

>
>>>>>> @@ -911,7 +911,7 @@ static int vdpa_fill_stats_rec(struct vdpa_device *vdev, struct sk_buff *msg,
>>>>>>     	}
>>>>>>     	vdpa_get_config_unlocked(vdev, 0, &config, sizeof(config));
>>>>>> -	max_vqp = le16_to_cpu(config.max_virtqueue_pairs);
>>>>>> +	max_vqp = __virtio16_to_cpu(true, config.max_virtqueue_pairs);
>>>>>>     	if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MAX_VQP, max_vqp))
>>>>>>     		return -EMSGSIZE;
>>>>>> -- 
>>>>>> 2.31.1

```
