* [PATCH] virtio: Change typecasts in vring_init() @ 2019-08-27 15:20 Matej Genci 2019-08-30 3:48 ` Jason Wang 2019-08-30 14:02 ` Michael S. Tsirkin 0 siblings, 2 replies; 8+ messages in thread From: Matej Genci @ 2019-08-27 15:20 UTC (permalink / raw) To: virtualization@lists.linux-foundation.org, mst@redhat.com, jasowang@redhat.com, linux-kernel@vger.kernel.org Cc: Matej Genci Compilers such as g++ 7.3 complain about assigning void* variable to a non-void* variable (like struct pointers) and pointer arithmetics on void*. Signed-off-by: Matej Genci <matej.genci@nutanix.com> --- include/uapi/linux/virtio_ring.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h index 4c4e24c291a5..2c339b9e2923 100644 --- a/include/uapi/linux/virtio_ring.h +++ b/include/uapi/linux/virtio_ring.h @@ -168,10 +168,11 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, unsigned long align) { vr->num = num; - vr->desc = p; - vr->avail = p + num*sizeof(struct vring_desc); - vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) - + align-1) & ~(align - 1)); + vr->desc = (struct vring_desc *)p; + vr->avail = (struct vring_avail *)((uintptr_t)p + + num*sizeof(struct vring_desc)); + vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] + + sizeof(__virtio16) + align-1) & ~(align - 1)); } static inline unsigned vring_size(unsigned int num, unsigned long align) -- 2.22.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] virtio: Change typecasts in vring_init() 2019-08-27 15:20 [PATCH] virtio: Change typecasts in vring_init() Matej Genci @ 2019-08-30 3:48 ` Jason Wang 2019-08-30 17:26 ` Matej Genci 2019-08-30 14:02 ` Michael S. Tsirkin 1 sibling, 1 reply; 8+ messages in thread From: Jason Wang @ 2019-08-30 3:48 UTC (permalink / raw) To: Matej Genci, virtualization@lists.linux-foundation.org, mst@redhat.com, linux-kernel@vger.kernel.org On 2019/8/27 下午11:20, Matej Genci wrote: > Compilers such as g++ 7.3 complain about assigning void* variable to > a non-void* variable (like struct pointers) and pointer arithmetics > on void*. > > Signed-off-by: Matej Genci <matej.genci@nutanix.com> > --- > include/uapi/linux/virtio_ring.h | 9 +++++---- > 1 file changed, 5 insertions(+), 4 deletions(-) > > diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h > index 4c4e24c291a5..2c339b9e2923 100644 > --- a/include/uapi/linux/virtio_ring.h > +++ b/include/uapi/linux/virtio_ring.h > @@ -168,10 +168,11 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, > unsigned long align) > { > vr->num = num; > - vr->desc = p; > - vr->avail = p + num*sizeof(struct vring_desc); > - vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) > - + align-1) & ~(align - 1)); > + vr->desc = (struct vring_desc *)p; > + vr->avail = (struct vring_avail *)((uintptr_t)p > + + num*sizeof(struct vring_desc)); It's better to let the code pass checkpath.pl here. Other looks good. Thanks > + vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] > + + sizeof(__virtio16) + align-1) & ~(align - 1)); > } > > static inline unsigned vring_size(unsigned int num, unsigned long align) ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] virtio: Change typecasts in vring_init() 2019-08-30 3:48 ` Jason Wang @ 2019-08-30 17:26 ` Matej Genci 0 siblings, 0 replies; 8+ messages in thread From: Matej Genci @ 2019-08-30 17:26 UTC (permalink / raw) To: Jason Wang, virtualization@lists.linux-foundation.org, mst@redhat.com, linux-kernel@vger.kernel.org On 8/30/2019 4:48 AM, Jason Wang wrote: > > On 2019/8/27 下午11:20, Matej Genci wrote: >> Compilers such as g++ 7.3 complain about assigning void* variable to >> a non-void* variable (like struct pointers) and pointer arithmetics >> on void*. >> >> Signed-off-by: Matej Genci <matej.genci@nutanix.com> >> --- >> include/uapi/linux/virtio_ring.h | 9 +++++---- >> 1 file changed, 5 insertions(+), 4 deletions(-) >> >> diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h >> index 4c4e24c291a5..2c339b9e2923 100644 >> --- a/include/uapi/linux/virtio_ring.h >> +++ b/include/uapi/linux/virtio_ring.h >> @@ -168,10 +168,11 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, >> unsigned long align) >> { >> vr->num = num; >> - vr->desc = p; >> - vr->avail = p + num*sizeof(struct vring_desc); >> - vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) >> - + align-1) & ~(align - 1)); >> + vr->desc = (struct vring_desc *)p; >> + vr->avail = (struct vring_avail *)((uintptr_t)p >> + + num*sizeof(struct vring_desc)); > > > It's better to let the code pass checkpath.pl here. > It passes for me --------8<-------- ./scripts/checkpatch.pl 0001-virtio-Change-typecasts-in-vring_init.patch total: 0 errors, 0 warnings, 15 lines checked 0001-virtio-Change-typecasts-in-vring_init.patch has no obvious style problems and is ready for submission. --------8<-------- Is there something I'm missing? > Other looks good. > > Thanks > > >> + vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] >> + + sizeof(__virtio16) + align-1) & ~(align - 1)); >> } >> >> static inline unsigned vring_size(unsigned int num, unsigned long align) ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] virtio: Change typecasts in vring_init() 2019-08-27 15:20 [PATCH] virtio: Change typecasts in vring_init() Matej Genci 2019-08-30 3:48 ` Jason Wang @ 2019-08-30 14:02 ` Michael S. Tsirkin 2019-08-30 17:58 ` Matej Genci 1 sibling, 1 reply; 8+ messages in thread From: Michael S. Tsirkin @ 2019-08-30 14:02 UTC (permalink / raw) To: Matej Genci Cc: virtualization@lists.linux-foundation.org, jasowang@redhat.com, linux-kernel@vger.kernel.org On Tue, Aug 27, 2019 at 03:20:57PM +0000, Matej Genci wrote: > Compilers such as g++ 7.3 complain about assigning void* variable to > a non-void* variable (like struct pointers) and pointer arithmetics > on void*. > > Signed-off-by: Matej Genci <matej.genci@nutanix.com> > --- > include/uapi/linux/virtio_ring.h | 9 +++++---- > 1 file changed, 5 insertions(+), 4 deletions(-) > > diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h > index 4c4e24c291a5..2c339b9e2923 100644 > --- a/include/uapi/linux/virtio_ring.h > +++ b/include/uapi/linux/virtio_ring.h > @@ -168,10 +168,11 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, > unsigned long align) > { > vr->num = num; > - vr->desc = p; > - vr->avail = p + num*sizeof(struct vring_desc); > - vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) > - + align-1) & ~(align - 1)); > + vr->desc = (struct vring_desc *)p; > + vr->avail = (struct vring_avail *)((uintptr_t)p > + + num*sizeof(struct vring_desc)); > + vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] > + + sizeof(__virtio16) + align-1) & ~(align - 1)); > } > > static inline unsigned vring_size(unsigned int num, unsigned long align) I'm not really interested in building with g++, sorry. Centainly not if it makes code less robust by forcing casts where they weren't previously necessary. However, vring_init and vring_size are legacy APIs anyway, so I'd be happy to add ifndefs that will allow userspace simply hide these functions if it does not need them. > -- > 2.22.0 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] virtio: Change typecasts in vring_init() 2019-08-30 14:02 ` Michael S. Tsirkin @ 2019-08-30 17:58 ` Matej Genci 2019-08-31 17:43 ` Michael S. Tsirkin 0 siblings, 1 reply; 8+ messages in thread From: Matej Genci @ 2019-08-30 17:58 UTC (permalink / raw) To: Michael S. Tsirkin Cc: virtualization@lists.linux-foundation.org, jasowang@redhat.com, linux-kernel@vger.kernel.org On 8/30/2019 3:02 PM, Michael S. Tsirkin wrote: > On Tue, Aug 27, 2019 at 03:20:57PM +0000, Matej Genci wrote: >> Compilers such as g++ 7.3 complain about assigning void* variable to >> a non-void* variable (like struct pointers) and pointer arithmetics >> on void*. >> >> Signed-off-by: Matej Genci <matej.genci@nutanix.com> >> --- >> include/uapi/linux/virtio_ring.h | 9 +++++---- >> 1 file changed, 5 insertions(+), 4 deletions(-) >> >> diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h >> index 4c4e24c291a5..2c339b9e2923 100644 >> --- a/include/uapi/linux/virtio_ring.h >> +++ b/include/uapi/linux/virtio_ring.h >> @@ -168,10 +168,11 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, >> unsigned long align) >> { >> vr->num = num; >> - vr->desc = p; >> - vr->avail = p + num*sizeof(struct vring_desc); >> - vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) >> - + align-1) & ~(align - 1)); >> + vr->desc = (struct vring_desc *)p; >> + vr->avail = (struct vring_avail *)((uintptr_t)p >> + + num*sizeof(struct vring_desc)); >> + vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] >> + + sizeof(__virtio16) + align-1) & ~(align - 1)); >> } >> >> static inline unsigned vring_size(unsigned int num, unsigned long align) > > I'm not really interested in building with g++, sorry. > Centainly not if it makes code less robust by forcing > casts where they weren't previously necessary. Can you elaborate on how these casts make the code less robust? They aren't necessary in C but I think being explicit can improve readability as argued in https://softwareengineering.stackexchange.com/a/275714 > > However, vring_init and vring_size are legacy APIs anyway, > so I'd be happy to add ifndefs that will allow userspace > simply hide these functions if it does not need them. > I feel like my patch is a harmless way of allowing this header to be used in C++ projects, but I'm happy to drop it in lieu of the guards if you feel strongly about it. Thanks. Matej > >> -- >> 2.22.0 >> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] virtio: Change typecasts in vring_init() 2019-08-30 17:58 ` Matej Genci @ 2019-08-31 17:43 ` Michael S. Tsirkin 2019-09-02 9:56 ` Matej Genci 0 siblings, 1 reply; 8+ messages in thread From: Michael S. Tsirkin @ 2019-08-31 17:43 UTC (permalink / raw) To: Matej Genci Cc: virtualization@lists.linux-foundation.org, jasowang@redhat.com, linux-kernel@vger.kernel.org On Fri, Aug 30, 2019 at 05:58:23PM +0000, Matej Genci wrote: > On 8/30/2019 3:02 PM, Michael S. Tsirkin wrote: > > On Tue, Aug 27, 2019 at 03:20:57PM +0000, Matej Genci wrote: > >> Compilers such as g++ 7.3 complain about assigning void* variable to > >> a non-void* variable (like struct pointers) and pointer arithmetics > >> on void*. > >> > >> Signed-off-by: Matej Genci <matej.genci@nutanix.com> > >> --- > >> include/uapi/linux/virtio_ring.h | 9 +++++---- > >> 1 file changed, 5 insertions(+), 4 deletions(-) > >> > >> diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h > >> index 4c4e24c291a5..2c339b9e2923 100644 > >> --- a/include/uapi/linux/virtio_ring.h > >> +++ b/include/uapi/linux/virtio_ring.h > >> @@ -168,10 +168,11 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, > >> unsigned long align) > >> { > >> vr->num = num; > >> - vr->desc = p; > >> - vr->avail = p + num*sizeof(struct vring_desc); > >> - vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) > >> - + align-1) & ~(align - 1)); > >> + vr->desc = (struct vring_desc *)p; > >> + vr->avail = (struct vring_avail *)((uintptr_t)p > >> + + num*sizeof(struct vring_desc)); > >> + vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] > >> + + sizeof(__virtio16) + align-1) & ~(align - 1)); > >> } > >> > >> static inline unsigned vring_size(unsigned int num, unsigned long align) > > > > I'm not really interested in building with g++, sorry. > > Centainly not if it makes code less robust by forcing > > casts where they weren't previously necessary. > > Can you elaborate on how these casts make the code less robust? If we ever change the variable types build will still pass because of the cast. > They aren't necessary in C but I think being explicit can improve > readability as argued in > https://softwareengineering.stackexchange.com/a/275714 > > > > > However, vring_init and vring_size are legacy APIs anyway, > > so I'd be happy to add ifndefs that will allow userspace > > simply hide these functions if it does not need them. > > > > I feel like my patch is a harmless way of allowing this header > to be used in C++ projects, but I'm happy to drop it in lieu of > the guards if you feel strongly about it. > > Thanks. > Matej Yea let's not even start. > > > >> -- > >> 2.22.0 > >> > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] virtio: Change typecasts in vring_init() 2019-08-31 17:43 ` Michael S. Tsirkin @ 2019-09-02 9:56 ` Matej Genci 2019-09-03 15:00 ` Michael S. Tsirkin 0 siblings, 1 reply; 8+ messages in thread From: Matej Genci @ 2019-09-02 9:56 UTC (permalink / raw) To: Michael S. Tsirkin Cc: virtualization@lists.linux-foundation.org, jasowang@redhat.com, linux-kernel@vger.kernel.org On 8/31/2019 6:43 PM, Michael S. Tsirkin wrote: > On Fri, Aug 30, 2019 at 05:58:23PM +0000, Matej Genci wrote: >> On 8/30/2019 3:02 PM, Michael S. Tsirkin wrote: >>> On Tue, Aug 27, 2019 at 03:20:57PM +0000, Matej Genci wrote: >>>> Compilers such as g++ 7.3 complain about assigning void* variable to >>>> a non-void* variable (like struct pointers) and pointer arithmetics >>>> on void*. >>>> >>>> Signed-off-by: Matej Genci <matej.genci@nutanix.com> >>>> --- >>>> include/uapi/linux/virtio_ring.h | 9 +++++---- >>>> 1 file changed, 5 insertions(+), 4 deletions(-) >>>> >>>> diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h >>>> index 4c4e24c291a5..2c339b9e2923 100644 >>>> --- a/include/uapi/linux/virtio_ring.h >>>> +++ b/include/uapi/linux/virtio_ring.h >>>> @@ -168,10 +168,11 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, >>>> unsigned long align) >>>> { >>>> vr->num = num; >>>> - vr->desc = p; >>>> - vr->avail = p + num*sizeof(struct vring_desc); >>>> - vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) >>>> - + align-1) & ~(align - 1)); >>>> + vr->desc = (struct vring_desc *)p; >>>> + vr->avail = (struct vring_avail *)((uintptr_t)p >>>> + + num*sizeof(struct vring_desc)); >>>> + vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] >>>> + + sizeof(__virtio16) + align-1) & ~(align - 1)); >>>> } >>>> >>>> static inline unsigned vring_size(unsigned int num, unsigned long align) >>> >>> I'm not really interested in building with g++, sorry. >>> Centainly not if it makes code less robust by forcing >>> casts where they weren't previously necessary. >> >> Can you elaborate on how these casts make the code less robust? > > If we ever change the variable types build will still pass > because of the cast. > Wouldn't that be the case in the original as well? You're assigning void*, which is implicitly cast to everything. >> They aren't necessary in C but I think being explicit can improve >> readability as argued in >> https://urldefense.proofpoint.com/v2/url?u=https-3A__softwareengineering.stackexchange.com_a_275714&d=DwIBAg&c=s883GpUCOChKOHiocYtGcg&r=dbPDDn52JgZndd-WPvGcL5PLZTrms-72TItYJx-If5I&m=sw6xxC2EOF9g3XtUKuI6OvT5xhYF7XcWBqyQvGb-UMw&s=QWoZHF4XlOzPesnnbfsf1_KORrzkXb6yfd6yQGCwepc&e= >> >>> >>> However, vring_init and vring_size are legacy APIs anyway, >>> so I'd be happy to add ifndefs that will allow userspace >>> simply hide these functions if it does not need them. >>> >> >> I feel like my patch is a harmless way of allowing this header >> to be used in C++ projects, but I'm happy to drop it in lieu of >> the guards if you feel strongly about it. >> >> Thanks. >> Matej > > Yea let's not even start. > Sure. I can re-send the patch with guards. But for my own sake, can you elaborate on the above? >>> >>>> -- >>>> 2.22.0 >>>> >> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] virtio: Change typecasts in vring_init() 2019-09-02 9:56 ` Matej Genci @ 2019-09-03 15:00 ` Michael S. Tsirkin 0 siblings, 0 replies; 8+ messages in thread From: Michael S. Tsirkin @ 2019-09-03 15:00 UTC (permalink / raw) To: Matej Genci Cc: virtualization@lists.linux-foundation.org, jasowang@redhat.com, linux-kernel@vger.kernel.org On Mon, Sep 02, 2019 at 09:56:42AM +0000, Matej Genci wrote: > On 8/31/2019 6:43 PM, Michael S. Tsirkin wrote: > > On Fri, Aug 30, 2019 at 05:58:23PM +0000, Matej Genci wrote: > >> On 8/30/2019 3:02 PM, Michael S. Tsirkin wrote: > >>> On Tue, Aug 27, 2019 at 03:20:57PM +0000, Matej Genci wrote: > >>>> Compilers such as g++ 7.3 complain about assigning void* variable to > >>>> a non-void* variable (like struct pointers) and pointer arithmetics > >>>> on void*. > >>>> > >>>> Signed-off-by: Matej Genci <matej.genci@nutanix.com> > >>>> --- > >>>> include/uapi/linux/virtio_ring.h | 9 +++++---- > >>>> 1 file changed, 5 insertions(+), 4 deletions(-) > >>>> > >>>> diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h > >>>> index 4c4e24c291a5..2c339b9e2923 100644 > >>>> --- a/include/uapi/linux/virtio_ring.h > >>>> +++ b/include/uapi/linux/virtio_ring.h > >>>> @@ -168,10 +168,11 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, > >>>> unsigned long align) > >>>> { > >>>> vr->num = num; > >>>> - vr->desc = p; > >>>> - vr->avail = p + num*sizeof(struct vring_desc); > >>>> - vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) > >>>> - + align-1) & ~(align - 1)); > >>>> + vr->desc = (struct vring_desc *)p; > >>>> + vr->avail = (struct vring_avail *)((uintptr_t)p > >>>> + + num*sizeof(struct vring_desc)); > >>>> + vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] > >>>> + + sizeof(__virtio16) + align-1) & ~(align - 1)); > >>>> } > >>>> > >>>> static inline unsigned vring_size(unsigned int num, unsigned long align) > >>> > >>> I'm not really interested in building with g++, sorry. > >>> Centainly not if it makes code less robust by forcing > >>> casts where they weren't previously necessary. > >> > >> Can you elaborate on how these casts make the code less robust? > > > > If we ever change the variable types build will still pass > > because of the cast. > > > > Wouldn't that be the case in the original as well? > You're assigning void*, which is implicitly cast to everything. Right. And if we change that void * to something else, build will fail. Not so with a cast. > >> They aren't necessary in C but I think being explicit can improve > >> readability as argued in > >> https://urldefense.proofpoint.com/v2/url?u=https-3A__softwareengineering.stackexchange.com_a_275714&d=DwIBAg&c=s883GpUCOChKOHiocYtGcg&r=dbPDDn52JgZndd-WPvGcL5PLZTrms-72TItYJx-If5I&m=sw6xxC2EOF9g3XtUKuI6OvT5xhYF7XcWBqyQvGb-UMw&s=QWoZHF4XlOzPesnnbfsf1_KORrzkXb6yfd6yQGCwepc&e= > >> > >>> > >>> However, vring_init and vring_size are legacy APIs anyway, > >>> so I'd be happy to add ifndefs that will allow userspace > >>> simply hide these functions if it does not need them. > >>> > >> > >> I feel like my patch is a harmless way of allowing this header > >> to be used in C++ projects, but I'm happy to drop it in lieu of > >> the guards if you feel strongly about it. > >> > >> Thanks. > >> Matej > > > > Yea let's not even start. > > > > Sure. I can re-send the patch with guards. But for my own sake, > can you elaborate on the above? > > >>> > >>>> -- > >>>> 2.22.0 > >>>> > >> > ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2019-09-03 15:00 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2019-08-27 15:20 [PATCH] virtio: Change typecasts in vring_init() Matej Genci 2019-08-30 3:48 ` Jason Wang 2019-08-30 17:26 ` Matej Genci 2019-08-30 14:02 ` Michael S. Tsirkin 2019-08-30 17:58 ` Matej Genci 2019-08-31 17:43 ` Michael S. Tsirkin 2019-09-02 9:56 ` Matej Genci 2019-09-03 15:00 ` Michael S. Tsirkin
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox