rng-random: implement request queue
v2 cover rng-random: implement request queue
As suggested by Paolo, I have moved the RngRequest implementation up to the RngBackend parent class and made both child classes use it. Apart from the refactoring, the only functional change compared to v1 is the use of heap instead of stack allocation for the read buffer in rng-random.
The parent class takes care of creating new requests and adding them to the queue, as well as removing them from the queue and deleting them. Child classes have access to the raw GSList * to do whatever else they need to do (walk the queue, peek the head of the queue, ..)
[PATCH v2 1/4] rng: remove the unused request cancellation code [PATCH v2 2/4] rng: move request queue from RngEgd to RngBackend [PATCH v2 3/4] rng: move request queue cleanup from RngEgd to [PATCH v2 4/4] rng: add request queue support to rng-random
PATCH v2 1/4 rng: remove the unused request cancellation code
rng_backend_cancel_requests has no callers and none of the code deleted in this commit ever runs.
--- backends/rng-egd.c | 12 ------------ backends/rng.c | 9 --------- include/sysemu/rng.h | 11 ----------- 3 files changed, 32 deletions(-)
backends/rng-egd.c +0 -12
@@ -125,17 +125,6 @@ static void rng_egd_free_requests(RngEgd *s) s->requests =NULL ; } -static void rng_egd_cancel_requests (RngBackend *b) -{ -RngEgd *s =RNG_EGD (b); - -/* We simply delete the list of pending requests. If there is data in the -* queue waiting to be read, this is okay, because there will always be -* more data than we requested originally -*/ -rng_egd_free_requests (s); -} -static void rng_egd_opened (RngBackend *b,Error **errp) {RngEgd *s =RNG_EGD (b);
@@ -213,7 +202,6 @@ static void rng_egd_class_init(ObjectClass *klass, void *data)RngBackendClass *rbc =RNG_BACKEND_CLASS (klass); rbc->request_entropy = rng_egd_request_entropy; - rbc->cancel_requests = rng_egd_cancel_requests; rbc->opened = rng_egd_opened; }
backends/rng.c +0 -9
@@ -26,15 +26,6 @@ void rng_backend_request_entropy(RngBackend *s, size_t size, } } -void rng_backend_cancel_requests (RngBackend *s) -{ -RngBackendClass *k =RNG_BACKEND_GET_CLASS (s); - -if (k->cancel_requests) { - k->cancel_requests (s); - } -} -static bool rng_backend_prop_get_opened (Object *obj,Error **errp) {RngBackend *s =RNG_BACKEND (obj);
include/sysemu/rng.h +0 -11
@@ -38,7 +38,6 @@ struct RngBackendClassvoid (*request_entropy)(RngBackend *s,size_t size,EntropyReceiveFunc *receive_entropy,void *opaque); -void (*cancel_requests)(RngBackend *s);void (*opened)(RngBackend *s,Error **errp); };
@@ -69,14 +68,4 @@ struct RngBackendvoid rng_backend_request_entropy (RngBackend *s,size_t size,EntropyReceiveFunc *receive_entropy,void *opaque); - -/** -* rng_backend_cancel_requests: -* @s: the backend to cancel all pending requests in -* -* Cancels all pending requests submitted by @rng_backend_request_entropy. This -* should be used by a device during reset or in preparation for live migration -* to stop tracking any request. -*/ -void rng_backend_cancel_requests (RngBackend *s);#endif
Signature · 1 line
2.5.0
PATCH v2 2/4 rng: move request queue from RngEgd to RngBackend
In reply to Ladi ProsekThe 'requests' field now lives in the RngBackend parent class. There are no functional changes in this commit.
--- backends/rng-egd.c | 28 +++++++++------------------- include/sysemu/rng.h | 11 +++++++++++ 2 files changed, 20 insertions(+), 19 deletions(-)
backends/rng-egd.c +9 -19
@@ -25,19 +25,8 @@ typedef struct RngEgdCharDriverState *chr;char *chr_name; - -GSList *requests; } RngEgd; -typedef struct RngRequest -{ -EntropyReceiveFunc *receive_entropy; -uint8_t *data; -void *opaque; -size_t offset; -size_t size; -}RngRequest ; -static void rng_egd_request_entropy (RngBackend *b,size_t size,EntropyReceiveFunc *receive_entropy,void *opaque)
@@ -66,7 +55,7 @@ static void rng_egd_request_entropy(RngBackend *b, size_t size, size -= len; } - s->requests =g_slist_append (s->requests, req); + s->parent.requests =g_slist_append (s->parent.requests, req); }static void rng_egd_free_request (RngRequest *req)
@@ -81,7 +70,7 @@ static int rng_egd_chr_can_read(void *opaque)GSList *i;int size =0 ; -for (i = s->requests; i; i = i->next) { +for (i = s->parent.requests; i; i = i->next) {RngRequest *req = i->data; size += req->size - req->offset; }
@@ -94,8 +83,8 @@ static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size)RngEgd *s =RNG_EGD (opaque);size_t buf_offset =0 ; -while (size >0 && s->requests) { -RngRequest *req = s->requests->data; +while (size >0 && s->parent.requests) { +RngRequest *req = s->parent.requests->data;int len =MIN (size, req->size - req->offset);memcpy (req->data + req->offset, buf + buf_offset, len);
@@ -104,7 +93,8 @@ static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size) size -= len;if (req->offset == req->size) { - s->requests =g_slist_remove_link (s->requests, s->requests); + s->parent.requests =g_slist_remove_link (s->parent.requests, + s->parent.requests); req->receive_entropy (req->opaque, req->data, req->size);
@@ -117,12 +107,12 @@ static void rng_egd_free_requests(RngEgd *s) {GSList *i; -for (i = s->requests; i; i = i->next) { +for (i = s->parent.requests; i; i = i->next) {rng_egd_free_request (i->data); } -g_slist_free (s->requests); - s->requests =NULL ; +g_slist_free (s->parent.requests); + s->parent.requests =NULL ; }static void rng_egd_opened (RngBackend *b,Error **errp)
include/sysemu/rng.h +11 -0
@@ -25,6 +25,7 @@#define RNG_BACKEND_CLASS (klass) \ OBJECT_CLASS_CHECK(RngBackendClass, (klass), TYPE_RNG_BACKEND) +typedef struct RngRequest RngRequest ;typedef struct RngBackendClass RngBackendClass ;typedef struct RngBackend RngBackend ;
@@ -32,6 +33,15 @@ typedef void (EntropyReceiveFunc)(void *opaque,const void *data, size_t size); +struct RngRequest +{ +EntropyReceiveFunc *receive_entropy; +uint8_t *data; +void *opaque; +size_t offset; +size_t size; +}; +struct RngBackendClass {ObjectClass parent_class;
@@ -48,6 +58,7 @@ struct RngBackend/*< protected >*/ bool opened; +GSList *requests; };/**
Signature · 1 line
2.5.0
PATCH v2 3/4 rng: move request queue cleanup from RngEgd to RngBackend
In reply to Ladi ProsekRngBackend is now in charge of cleaning up the linked list on instance finalization. It also exposes a function to finalize individual RngRequest instances, called by its child classes.
--- backends/rng-egd.c | 25 +------------------------ backends/rng.c | 32 ++++++++++++++++++++++++++++++++ include/sysemu/rng.h | 12 ++++++++++++ 3 files changed, 45 insertions(+), 24 deletions(-)
backends/rng-egd.c +1 -24
@@ -58,12 +58,6 @@ static void rng_egd_request_entropy(RngBackend *b, size_t size, s->parent.requests =g_slist_append (s->parent.requests, req); } -static void rng_egd_free_request (RngRequest *req) -{ -g_free (req->data); -g_free (req); -} -static int rng_egd_chr_can_read (void *opaque) {RngEgd *s =RNG_EGD (opaque);
@@ -93,28 +87,13 @@ static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size) size -= len;if (req->offset == req->size) { - s->parent.requests =g_slist_remove_link (s->parent.requests, - s->parent.requests); - req->receive_entropy (req->opaque, req->data, req->size); -rng_egd_free_request (req); +rng_backend_finalize_request (&s->parent, req); } } } -static void rng_egd_free_requests (RngEgd *s) -{ -GSList *i; - -for (i = s->parent.requests; i; i = i->next) { -rng_egd_free_request (i->data); - } - -g_slist_free (s->parent.requests); - s->parent.requests =NULL ; -} -static void rng_egd_opened (RngBackend *b,Error **errp) {RngEgd *s =RNG_EGD (b);
@@ -183,8 +162,6 @@ static void rng_egd_finalize(Object *obj) }g_free (s->chr_name); - -rng_egd_free_requests (s); }static void rng_egd_class_init (ObjectClass *klass,void *data)
backends/rng.c +32 -0
@@ -64,6 +64,30 @@ static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp) s->opened = true; } +static void rng_backend_free_request (RngRequest *req) +{ +g_free (req->data); +g_free (req); +} + +static void rng_backend_free_requests (RngBackend *s) +{ +GSList *i; + +for (i = s->requests; i; i = i->next) { +rng_backend_free_request (i->data); + } + +g_slist_free (s->requests); + s->requests =NULL ; +} + +void rng_backend_finalize_request (RngBackend *s,RngRequest *req) +{ + s->requests =g_slist_remove (s->requests, req); +rng_backend_free_request (req); +} +static void rng_backend_init (Object *obj) { object_property_add_bool(obj, "opened",
@@ -72,6 +96,13 @@ static void rng_backend_init(Object *obj) NULL); } +static void rng_backend_finalize (Object *obj) +{ +RngBackend *s =RNG_BACKEND (obj); + +rng_backend_free_requests (s); +} +static void rng_backend_class_init (ObjectClass *oc,void *data) {UserCreatableClass *ucc =USER_CREATABLE_CLASS (oc);
@@ -84,6 +115,7 @@ static const TypeInfo rng_backend_info = { .parent = TYPE_OBJECT, .instance_size = sizeof(RngBackend), .instance_init = rng_backend_init, + .instance_finalize = rng_backend_finalize, .class_size = sizeof(RngBackendClass), .class_init = rng_backend_class_init, .abstract = true,
include/sysemu/rng.h +12 -0
@@ -61,6 +61,7 @@ struct RngBackendGSList *requests; }; +/** * rng_backend_request_entropy: * @s: the backend to request entropy from
@@ -79,4 +80,15 @@ struct RngBackendvoid rng_backend_request_entropy (RngBackend *s,size_t size,EntropyReceiveFunc *receive_entropy,void *opaque); + +/** +* rng_backend_free_request: +* @s: the backend that created the request +* @req: the request to finalize +* +* Used by child rng backend classes to finalize requests once they've been +* processed. The request is removed from the list of active requests and +* deleted. +*/ +void rng_backend_finalize_request (RngBackend *s,RngRequest *req);#endif
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v2 3/4] rng: move request queue cleanup from RngEgd to RngBackend
backends/rng-egd.c static void rng_egd_finalize(Object *obj) · line 187
Quoted 12 lines · Ladi Prosek
RngBackend is now in charge of cleaning up the linked list on instance finalization. It also exposes a function to finalize individual RngRequest instances, called by its child classes.
Signed-off-by Ladi Prosek <[email protected]>backends/rng-egd.c +0
-2@@ -183,8 +162,6 @@ static void rng_egd_finalize(Object *obj) }g_free (s->chr_name); - -rng_egd_free_requests (s);
Missing call to rng_backend_free_requests()?
include/sysemu/rng.h struct RngBackend · line 65
Quoted 10 lines
include/sysemu/rng.h +1
-0@@ -61,6 +61,7 @@ struct RngBackendGSList *requests; }; +/**
Extra whitespace?
Amit
Re: [Qemu-devel] [PATCH v2 3/4] rng: move request queue cleanup from RngEgd to RngBackend
Quoted 7 lines · Amit Shah
Quoted 5 lines · Ladi Prosek
RngBackend is now in charge of cleaning up the linked list on instance finalization. It also exposes a function to finalize individual RngRequest instances, called by its child classes.
Signed-off-by Ladi Prosek <[email protected]>
Quoted 8 lines
backends/rng-egd.c static void rng_egd_finalize(Object *obj) · line 187
Quoted 6 lines
backends/rng-egd.c +0
-2@@ -183,8 +162,6 @@ static void rng_egd_finalize(Object *obj) }g_free (s->chr_name); - -rng_egd_free_requests (s);Missing call to
rng_backend_free_requests()?
Not needed here, this is handled by the parent class. Its instance_finalize callback calls rng_backend_free_requests.
Quoted 8 lines
Quoted 8 lines
include/sysemu/rng.h +0
-0@@ -61,6 +61,7 @@ struct RngBackendGSList *requests; };
Quoted 4 lines
include/sysemu/rng.h struct RngBackend · line 65
Quoted 2 lines
include/sysemu/rng.h +1
-0+/** Extra whitespace?
This was intentional to separate internal declarations and public entry points. Same thing exists for example in tpm_backend.h in the same directory. I can certainly delete the empty line if you'd prefer that.
Thanks,
Ladi
Quoted 2 lines
Amit
Re: [Qemu-devel] [PATCH v2 3/4] rng: move request queue cleanup from RngEgd to RngBackend
Quoted 11 lines · Ladi Prosek
Quoted 10 lines · Amit Shah
Quoted 9 lines · Ladi Prosek
RngBackend is now in charge of cleaning up the linked list on instance finalization. It also exposes a function to finalize individual RngRequest instances, called by its child classes.
Signed-off-by Ladi Prosek <[email protected]>(quoted diff) +0
-0@@ -183,8 +162,6 @@ static void rng_egd_finalize(Object *obj) }
Quoted 8 lines
Quoted 5 lines
backends/rng-egd.c static void rng_egd_finalize(Object *obj) · line 187
Quoted 3 lines
backends/rng-egd.c +0
-2g_free (s->chr_name); - -rng_egd_free_requests (s);Missing call to
rng_backend_free_requests()?Not needed here, this is handled by the parent class. Its
instance_finalizecallback callsrng_backend_free_requests.
OK.
Quoted 13 lines
Quoted 8 lines
Quoted 8 lines
include/sysemu/rng.h +0
-0@@ -61,6 +61,7 @@ struct RngBackendGSList *requests; };Quoted 4 lines
include/sysemu/rng.h struct RngBackend · line 65
Quoted 2 lines
include/sysemu/rng.h +1
-0+/** Extra whitespace?
Quoted 4 lines
This was intentional to separate internal declarations and public entry points. Same thing exists for example in
tpm_backend.hin the same directory. I can certainly delete the empty line if you'd prefer that.
No, this is fine, and I agree with the separation.
Amit
PATCH v2 4/4 rng: add request queue support to rng-random
In reply to Ladi ProsekRequests are now created in the RngBackend parent class and the code path is shared by both rng-egd and rng-random.
This commit fixes the rng-random implementation which currently processes only one request at a time and simply discards all but the most recent one. In the guest this manifests as delayed completion of reads from virtio-rng, i.e. a read is completed only after another read is issued.
--- backends/rng-egd.c | 16 ++-------------- backends/rng-random.c | 43 +++++++++++++++++++------------------------ backends/rng.c | 13 ++++++++++++- include/sysemu/rng.h | 3 +-- 4 files changed, 34 insertions(+), 41 deletions(-)
backends/rng-egd.c +2 -14
@@ -27,20 +27,10 @@ typedef struct RngEgdchar *chr_name; } RngEgd; -static void rng_egd_request_entropy (RngBackend *b,size_t size, -EntropyReceiveFunc *receive_entropy, -void *opaque) +static void rng_egd_request_entropy (RngBackend *b,RngRequest *req) {RngEgd *s =RNG_EGD (b); -RngRequest *req; - - req =g_malloc (sizeof (*req)); - - req->offset =0 ; - req->size = size; - req->receive_entropy = receive_entropy; - req->opaque = opaque; - req->data =g_malloc (req->size); +size_t size = req->size;while (size >0 ) {uint8_t header[2 ];
@@ -54,8 +44,6 @@ static void rng_egd_request_entropy(RngBackend *b, size_t size, size -= len; } - - s->parent.requests =g_slist_append (s->parent.requests, req); }static int rng_egd_chr_can_read (void *opaque)
backends/rng-random.c +19 -24
65 lines not on this page · raw message
backends/rng.c +12 -1
@@ -20,9 +20,20 @@ void rng_backend_request_entropy(RngBackend *s, size_t size, void *opaque) {RngBackendClass *k =RNG_BACKEND_GET_CLASS (s); +RngRequest *req;if (k->request_entropy) { - k->request_entropy (s, size, receive_entropy, opaque); + req =g_malloc (sizeof (*req)); + + req->offset =0 ; + req->size = size; + req->receive_entropy = receive_entropy; + req->opaque = opaque; + req->data =g_malloc (req->size); + + k->request_entropy (s, req); + + s->requests =g_slist_append (s->requests, req); } }
include/sysemu/rng.h +1 -2
@@ -46,8 +46,7 @@ struct RngBackendClass {ObjectClass parent_class; -void (*request_entropy)(RngBackend *s,size_t size, -EntropyReceiveFunc *receive_entropy,void *opaque); +void (*request_entropy)(RngBackend *s,RngRequest *req);void (*opened)(RngBackend *s,Error **errp); };
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v2 4/4] rng: add request queue support to rng-random
backends/rng.c void rng_backend_request_entropy(RngBackend *s, size_t size, · line 37
Quoted 9 lines · Ladi Prosek
backends/rng.c +8
-0+ req->size = size; + req->receive_entropy = receive_entropy; + req->opaque = opaque; + req->data =g_malloc (req->size); + + k->request_entropy (s, req); + + s->requests =g_slist_append (s->requests, req); }
g_slist_append has to traverse the entire list to find the place to add the node. You probably are better off using QSIMPLEQ (which is an intrusive list unlike GSList).
Thanks,
Paolo
Re: [Qemu-devel] [PATCH v2 4/4] rng: add request queue support to rng-random
Quoted 8 lines · Ladi Prosek
Quoted 5 lines
backends/rng.c +5
-0+ req->size = size; + req->receive_entropy = receive_entropy; + req->opaque = opaque; + req->data =g_malloc (req->size); +
Quoted 8 lines
backends/rng.c void rng_backend_request_entropy(RngBackend *s, size_t size, · line 37
Quoted 4 lines
backends/rng.c +3
-0+ k->request_entropy (s, req); + + s->requests =g_slist_append (s->requests, req); }
g_slist_appendhas to traverse the entire list to find the place to add the node. You probably are better off using QSIMPLEQ (which is an intrusive list unlike GSList).
This is what rng-egd does today and I would argue that since the expected length of the list is very small - it's going to be longer than 1 only very rarely - a simple lightweight data structure is a better choice than trying to be O(1) in the worst case.
I'll be happy to switch to QSIMPLEQ if you want though. Your call.
Thanks!
Ladi
Quoted 5 lines
Thanks,
Paolo
Re: [Qemu-devel] [PATCH v2 4/4] rng: add request queue support to rng-random
Quoted 17 lines · Ladi Prosek
Quoted 8 lines · Ladi Prosek
Quoted 5 lines
backends/rng.c +5
-0+ req->size = size; + req->receive_entropy = receive_entropy; + req->opaque = opaque; + req->data =g_malloc (req->size); +Quoted 8 lines
backends/rng.c void rng_backend_request_entropy(RngBackend *s, size_t size, · line 37
Quoted 4 lines
backends/rng.c +3
-0+ k->request_entropy (s, req); + + s->requests =g_slist_append (s->requests, req); }
g_slist_appendhas to traverse the entire list to find the place to add the node. You probably are better off using QSIMPLEQ (which is an intrusive list unlike GSList).
Quoted 6 lines
This is what rng-egd does today and I would argue that since the expected length of the list is very small - it's going to be longer than 1 only very rarely - a simple lightweight data structure is a better choice than trying to be O(1) in the worst case.
I'll be happy to switch to QSIMPLEQ if you want though. Your call.
Ok, it can be done on top I guess. I'll let others review the patches more closely!
Paolo
Re: [Qemu-devel] [PATCH v2 4/4] rng: add request queue support to rng-random
In reply to Ladi ProsekQuoted 8 lines · Ladi Prosek
Requests are now created in the RngBackend parent class and the code path is shared by both rng-egd and rng-random.
This commit fixes the rng-random implementation which currently processes only one request at a time and simply discards all but the most recent one. In the guest this manifests as delayed completion of reads from virtio-rng, i.e. a read is completed only after another read is issued.
Nice commit message, but one convention I like is: when you're fixing something, refer to the bug in past tense, so it's not confusing with existing behaviour (i.e. since you're already fixing that bug in this patch, it's better to write in the past tense about this bug).
Also, since you mentioned the move from stack-based allocation to heap-based in the cover letter, and since this patch actually does that, please include a one-liner here for it too.
Otherwise, the series is fine.
Amit
Re: [Qemu-devel] [PATCH v2 4/4] rng: add request queue support to rng-random
Quoted 10 lines · Amit Shah
Quoted 8 lines · Ladi Prosek
Requests are now created in the RngBackend parent class and the code path is shared by both rng-egd and rng-random.
This commit fixes the rng-random implementation which currently processes only one request at a time and simply discards all but the most recent one. In the guest this manifests as delayed completion of reads from virtio-rng, i.e. a read is completed only after another read is issued.
Quoted 8 lines
Nice commit message, but one convention I like is: when you're fixing something, refer to the bug in past tense, so it's not confusing with existing behaviour (i.e. since you're already fixing that bug in this patch, it's better to write in the past tense about this bug).
Also, since you mentioned the move from stack-based allocation to heap-based in the cover letter, and since this patch actually does that, please include a one-liner here for it too.
Thanks for the review. I'll update the commit messages and send v3.
Quoted 3 lines
Otherwise, the series is fine.
Amit
Quoted 8 lines
As suggested by Paolo, I have moved the RngRequest implementation up to the RngBackend parent class and made both child classes use it. Apart from the refactoring, the only functional change compared to v1 is the use of heap instead of stack allocation for the read buffer in rng-random.
Quoted 5 lines
The parent class takes care of creating new requests and adding them to the queue, as well as removing them from the queue and deleting them. Child classes have access to the raw GSList * to do whatever else they need to do (walk the queue, peek the head of the queue, ..)
Hi Ladi,
Overall code changes looks good to me.
Quoted 7 lines
[PATCH v2 1/4] rng: remove the unused request cancellation code [PATCH v2 2/4] rng: move request queue from RngEgd to RngBackend [PATCH v2 3/4] rng: move request queue cleanup from RngEgd to [PATCH v2 4/4] rng: add request queue support to rng-random