Fix pointer arithmetic in indirect read for libvhost-user and libvduse
cover Fix pointer arithmetic in indirect read for libvhost-user and libvduse
Hello! I have found a problem with virtqueue_read_indirect_desc function, which was advancing pointer to struct as it was a byte pointer, so every element comming after first chunk would be copied somewhere out of buffer.
As I understand this is cold path, but nevertheless worth fixing.
Also, exacly same problem in vduse_queue_read_indirect_desc function, because as I understand it is a copy of virtqueue_read_indirect_desc with vduse backend.
I was not sure if element of scattered buffer may end in the middle of vring_desc struct data, so instead of writing
desc += read_len/sizeof(struct vring_desc)
have implemented fix with proper byte pointer arithmetic.
Sincerely,
Temir.
Temir Zharaspayev (2):
libvhost-user: Fix pointer arithmetic in indirect read
libvduse: Fix pointer arithmetic in indirect read
subprojects/libvduse/libvduse.c | 11 ++++++----- subprojects/libvhost-user/libvhost-user.c | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 libvhost-user: Fix pointer arithmetic in indirect read
When zero-copy usage of indirect descriptors buffer table isn't possible, library gather scattered memory chunks in a local copy. This commit fixes the issue with pointer arithmetic for the local copy buffer.
--- subprojects/libvhost-user/libvhost-user.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
subprojects/libvhost-user/libvhost-user.c +6 -5
@@ -2307,7 +2307,7 @@ static int virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, uint64_t addr, size_t len) { -struct vring_desc *ori_desc; +uint8_t *src_cursor, *dst_cursor;uint64_t read_len;if (len > (VIRTQUEUE_MAX_SIZE *sizeof (struct vring_desc ))) {
@@ -2318,17 +2318,18 @@ virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,return -1 ; } + dst_cursor = (uint8_t *) desc;while (len) { read_len = len; - ori_desc =vu_gpa_to_va (dev, &read_len, addr); -if (!ori_desc) { + src_cursor =vu_gpa_to_va (dev, &read_len, addr); +if (!src_cursor) {return -1 ; } -memcpy (desc, ori_desc, read_len); +memcpy (dst_cursor, src_cursor, read_len); len -= read_len; addr += read_len; - desc += read_len; + dst_cursor += read_len; }return 0 ;
Signature · 1 line
2.34.1
Re: [PATCH 1/2] libvhost-user: Fix pointer arithmetic in indirect read
Quoted 33 lines · Temir Zharaspayev
When zero-copy usage of indirect descriptors buffer table isn't possible, library gather scattered memory chunks in a local copy. This commit fixes the issue with pointer arithmetic for the local copy buffer.
Signed-off-by Temir Zharaspayev <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)subprojects/libvhost-user/libvhost-user.c +3
-3@@ -2307,7 +2307,7 @@ static int virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, uint64_t addr, size_t len) { -struct vring_desc *ori_desc; +uint8_t *src_cursor, *dst_cursor;uint64_t read_len;if (len > (VIRTQUEUE_MAX_SIZE *sizeof (struct vring_desc ))) {@@ -2318,17 +2318,18 @@ virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,return -1 ; } + dst_cursor = (uint8_t *) desc;while (len) { read_len = len; - ori_desc =vu_gpa_to_va (dev, &read_len, addr); -if (!ori_desc) { + src_cursor =vu_gpa_to_va (dev, &read_len, addr);
subprojects/libvhost-user/libvhost-user.c virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, · line 2332
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +3
-2+if (!src_cursor) {return -1 ; } -memcpy (desc, ori_desc, read_len); +memcpy (dst_cursor, src_cursor, read_len); len -= read_len; addr += read_len; - desc += read_len; + dst_cursor += read_len;
The ori_desc -> src_cursor changes don't look to have any functional effect. Having that change present obscures the functional part of the patch, which is this line. FWIW, it is generally preferrable to not mix functional and non-functional changes in the same patch
It now interprets 'read_len' as the number of bytes to increment the address by, rather than incrementing by the number of elements of size 'sizeof(struct vring_desc)'.
I don't know enough about this area of QEMU code to say which semantics were desired, so I'll defer to the Michael as maintainer to give a formal review.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 1/2] libvhost-user: Fix pointer arithmetic in indirect read
The change looks right to me. As is, it looks like the code is skipping over descriptors when the intent should be to bounce data into a single descriptor.
I agree the variable rename should go in as a separate change.
Quoted 24 lines · Daniel P. Berrangé
Quoted 22 lines · Temir Zharaspayev
When zero-copy usage of indirect descriptors buffer table isn't possible, library gather scattered memory chunks in a local copy. This commit fixes the issue with pointer arithmetic for the local copy buffer.
Signed-off-by Temir Zharaspayev <[email protected]>--- subprojects/libvhost-user/libvhost-user.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)subprojects/libvhost-user/libvhost-user.c +1
-1@@ -2307,7 +2307,7 @@ static int virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, uint64_t addr, size_t len) { -struct vring_desc *ori_desc; +uint8_t *src_cursor, *dst_cursor;uint64_t read_len;
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, · line 2321
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +1
-0if (len > (VIRTQUEUE_MAX_SIZE *sizeof (struct vring_desc ))) {@@ -2318,17 +2318,18 @@ virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,return -1 ; } + dst_cursor = (uint8_t *) desc;
Nit - no space on cast
Quoted 38 lines
Quoted 5 lines
subprojects/libvhost-user/libvhost-user.c +1
-2while (len) { read_len = len; - ori_desc =vu_gpa_to_va (dev, &read_len, addr); -if (!ori_desc) { + src_cursor =vu_gpa_to_va (dev, &read_len, addr);subprojects/libvhost-user/libvhost-user.c virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, · line 2332
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +3
-2+if (!src_cursor) {return -1 ; } -memcpy (desc, ori_desc, read_len); +memcpy (dst_cursor, src_cursor, read_len); len -= read_len; addr += read_len; - desc += read_len; + dst_cursor += read_len;The
ori_desc->src_cursorchanges don't look to have any functional effect. Having that change present obscures the functional part of the patch, which is this line. FWIW, it is generally preferrable to not mix functional and non-functional changes in the same patchIt now interprets '
read_len' as the number of bytes to increment the address by, rather than incrementing by the number of elements of size 'sizeof(struct vring_desc)'.I don't know enough about this area of QEMU code to say which semantics were desired, so I'll defer to the Michael as maintainer to give a formal review.
With regards,
Daniel
--|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 2/2 libvduse: Fix pointer arithmetic in indirect read
In reply to Temir ZharaspayevWhen zero-copy usage of indirect descriptors buffer table isn't possible, library gather scattered memory chunks in a local copy. This commit fixes the issue with pointer arithmetic for the local copy buffer.
--- subprojects/libvduse/libvduse.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
subprojects/libvduse/libvduse.c +6 -5
@@ -465,7 +465,7 @@ static int vduse_queue_read_indirect_desc(VduseDev *dev, struct vring_desc *desc, uint64_t addr, size_t len) { -struct vring_desc *ori_desc; +uint8_t *src_cursor, *dst_cursor;uint64_t read_len;if (len > (VIRTQUEUE_MAX_SIZE *sizeof (struct vring_desc ))) {
@@ -476,17 +476,18 @@ vduse_queue_read_indirect_desc(VduseDev *dev, struct vring_desc *desc,return -1 ; } + dst_cursor = (uint8_t *) desc;while (len) { read_len = len; - ori_desc =iova_to_va (dev, &read_len, addr); -if (!ori_desc) { + src_cursor =iova_to_va (dev, &read_len, addr); +if (!src_cursor) {return -1 ; } -memcpy (desc, ori_desc, read_len); +memcpy (dst_cursor, src_cursor, read_len); len -= read_len; addr += read_len; - desc += read_len; + dst_cursor += read_len; }return 0 ;
Signature · 1 line
2.34.1
Hello, I am very sorry for bothering community on a such minor problem again, but I got no response for a few weeks, so maybe I have started thread on a wrong mailing list, so I made an issue in gitlab issue tracker:
https://gitlab.com/qemu-project/qemu/-/issues/2149 referencing this thread.
Maybe, it would help attract proper eyes to such a simple problem, so no one bothers in trying to fix it, albeit it lives in the codebase for some time already and is being copied around.
Sincerely,
Temir.
сб, 13 янв. 2024 г. в 04:28, Temir Zharaspayev <[email protected]>:
Quoted 32 lines
Hello! I have found a problem with
virtqueue_read_indirect_descfunction, which
was advancing pointer to struct as it was a byte pointer, so every element comming after first chunk would be copied somewhere out of buffer.As I understand this is cold path, but nevertheless worth fixing.
Also, exacly same problem in
vduse_queue_read_indirect_descfunction, because
as I understand it is a copy ofvirtqueue_read_indirect_descwith vduse backend.I was not sure if element of scattered buffer may end in the middle of
vring_descstruct data, so instead of writing
desc += read_len/sizeof(structvring_desc)
have implemented fix with proper byte pointer arithmetic.Sincerely,
Temir.Temir Zharaspayev (2):
libvhost-user: Fix pointer arithmetic in indirect read
libvduse: Fix pointer arithmetic in indirect readsubprojects/libvduse/libvduse.c | 11 ++++++----- subprojects/libvhost-user/libvhost-user.c | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-)--
2.34.1
Temir: yeah, this was our fault, apologies for not responding.
Michael, David, Raphael -- looks like we unfortunately lost track of this patchset -- could one of you have a look and review it, please?
thanks
-- PMM
Quoted 38 lines · Тимур
Hello, I am very sorry for bothering community on a such minor problem again, but I got no response for a few weeks, so maybe I have started thread on a wrong mailing list, so I made an issue in gitlab issue tracker: https://gitlab.com/qemu-project/qemu/-/issues/2149 referencing this thread.
Maybe, it would help attract proper eyes to such a simple problem, so no one bothers in trying to fix it, albeit it lives in the codebase for some time already and is being copied around.
Sincerely,
Temir.сб, 13 янв. 2024 г. в 04:28, Temir Zharaspayev <[email protected]>:
Quoted 29 lines
Hello! I have found a problem with
virtqueue_read_indirect_descfunction, which was advancing pointer to struct as it was a byte pointer, so every element comming after first chunk would be copied somewhere out of buffer.As I understand this is cold path, but nevertheless worth fixing.
Also, exacly same problem in
vduse_queue_read_indirect_descfunction, because as I understand it is a copy ofvirtqueue_read_indirect_descwith vduse backend.I was not sure if element of scattered buffer may end in the middle of
vring_descstruct data, so instead of writing
desc += read_len/sizeof(structvring_desc)
have implemented fix with proper byte pointer arithmetic.Sincerely,
Temir.Temir Zharaspayev (2):
libvhost-user: Fix pointer arithmetic in indirect read
libvduse: Fix pointer arithmetic in indirect readsubprojects/libvduse/libvduse.c | 11 ++++++----- subprojects/libvhost-user/libvhost-user.c | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-)--
2.34.
Adding Michael back to the CC, since he's the designated maintainer for libvhost-user/
Michael, could you give these patches a review since they've been pending for many months now.
Quoted 5 lines · Тимур
Hello, I am very sorry for bothering community on a such minor problem again, but I got no response for a few weeks, so maybe I have started thread on a wrong mailing list, so I made an issue in gitlab issue tracker:
https://gitlab.com/qemu-project/qemu/-/issues/2149 referencing this thread.
Quoted 41 lines
Maybe, it would help attract proper eyes to such a simple problem, so no one bothers in trying to fix it, albeit it lives in the codebase for some time already and is being copied around.
Sincerely,
Temir.сб, 13 янв. 2024 г. в 04:28, Temir Zharaspayev <[email protected]>:
Quoted 32 lines
Hello! I have found a problem with
virtqueue_read_indirect_descfunction, which
was advancing pointer to struct as it was a byte pointer, so every element comming after first chunk would be copied somewhere out of buffer.As I understand this is cold path, but nevertheless worth fixing.
Also, exacly same problem in
vduse_queue_read_indirect_descfunction, because
as I understand it is a copy ofvirtqueue_read_indirect_descwith vduse backend.I was not sure if element of scattered buffer may end in the middle of
vring_descstruct data, so instead of writing
desc += read_len/sizeof(structvring_desc)
have implemented fix with proper byte pointer arithmetic.Sincerely,
Temir.Temir Zharaspayev (2):
libvhost-user: Fix pointer arithmetic in indirect read
libvduse: Fix pointer arithmetic in indirect readsubprojects/libvduse/libvduse.c | 11 ++++++----- subprojects/libvhost-user/libvhost-user.c | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-)--
2.34.1
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|