Skip to contentSkip to conversation

scsi: Implement alloc_req_iov callback

PATCH In review Hannes Reinecke 3 participants 4 messages Nov 22, 2010 – Nov 23, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: Implement alloc_req_iov callback

Add callback to create a request with a predefined iovec. This is required for drivers which can use the iovec of a command directly.

---
 hw/scsi-disk.c    |   25 +++++++++++++++++++++----
 hw/scsi-generic.c |   44 ++++++++++++++++++++++++++++++++++----------
 hw/scsi.h         |    2 ++
 3 files changed, 57 insertions(+), 14 deletions(-)
hw/scsi-disk.c +21 -4
index d1b7f74..88a2f74 100644
@@ -96,14 +96,30 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
     return req;
 }
 
+static SCSIRequest *scsi_new_request_iovec(SCSIDevice *d, uint32_t tag,
+        uint32_t lun, struct iovec *iov, int iov_num)
+{
+    SCSIRequest *req;
+    SCSIDiskReq *r;
+
+    req = scsi_req_alloc(sizeof(SCSIDiskReq), d, tag, lun);
+    r = DO_UPCAST(SCSIDiskReq, req, req);
+    r->iov = iov;
+    r->iov_num = iov_num;
+    r->iov_buf = NULL;
+    return req;
+}
+
 static void scsi_remove_request(SCSIRequest *req)
 {
     SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
 
-    qemu_vfree(r->iov);
-    r->iov = NULL;
-    qemu_vfree(r->iov_buf);
-    r->iov_buf = NULL;
+    if (r->iov_buf) {
+        qemu_vfree(r->iov);
+        r->iov = NULL;
+        qemu_vfree(r->iov_buf);
+        r->iov_buf = NULL;
+    }
     scsi_req_free(&r->req);
 }
 
@@ -1207,6 +1223,7 @@ static SCSIDeviceInfo scsi_disk_info = {
     .init         = scsi_disk_initfn,
     .destroy      = scsi_destroy,
     .alloc_req    = scsi_new_request,
+    .alloc_req_iov = scsi_new_request_iovec,
     .free_req     = scsi_remove_request,
     .send_command = scsi_send_command,
     .read_data    = scsi_read_data,
hw/scsi-generic.c +34 -10

75 lines not on this page · raw message

hw/scsi.h +2 -0
index 0c467d1..538ae54 100644
@@ -72,6 +72,8 @@ struct SCSIDeviceInfo {
     scsi_qdev_initfn init;
     void (*destroy)(SCSIDevice *s);
     SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun);
+    SCSIRequest *(*alloc_req_iov)(SCSIDevice *s, uint32_t tag, uint32_t lun,
+                                  struct iovec *iov, int iov_num);
     void (*free_req)(SCSIRequest *req);
     int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
     void (*read_data)(SCSIRequest *req);
Signature · 1 line
1.6.0.2
#1 lore raw

On Mon, Nov 22, 2010 at 10:15 AM, Hannes Reinecke <[email protected]> wrote:
Looks good. If you send out another version of the patchset you might like to fix this nitpick:

Quoted 4 lines

+    if (!r->io_header.iovec_count) {
+        if (r->buflen != r->req.cmd.xfer) {
+            if (r->buf != NULL)
+                qemu_free(r->buf);

qemu_free(NULL) is a nop so it's safe to drop the if (r->buf != NULL) check and just use qemu_free(r->buf) unconditionally. That's nice since it also fixes the if statement without curly braces.

Stefan

#2 lore raw
Quoted 4 lines · Stefan Hajnoczi

On Mon, Nov 22, 2010 at 10:15 AM, Hannes Reinecke <[email protected]> wrote:
Looks good. If you send out another version of the patchset you might like to fix this nitpick:

Quoted 9 lines

hw/scsi-generic.c static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd) · line 407

Quoted 4 lines
hw/scsi-generic.c +4 -0
+    if (!r->io_header.iovec_count) {
+        if (r->buflen != r->req.cmd.xfer) {
+            if (r->buf != NULL)
+                qemu_free(r->buf);

qemu_free(NULL) is a nop so it's safe to drop the if (r->buf != NULL) check and just use qemu_free(r->buf) unconditionally. That's nice since it also fixes the if statement without curly braces.

Really?

qemu-malloc.c has:

void qemu_free(void *ptr)

{
    trace_qemu_free(ptr);
    free(ptr);
}

and 'free' doesn't normally do an error checking on the argument. Am I missing something?

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Markus Rex, HRB 16746 (AG Nürnberg)
#3 lore raw
Quoted 11 lines · Hannes Reinecke

qemu-malloc.c has:

void qemu_free(void *ptr)

{
     trace_qemu_free(ptr);
     free(ptr);
}

and 'free' doesn't normally do an error checking on the argument. Am I missing something?

It's not error checking: from free(3),

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

Which means, that unless ptr is so often NULL that there is a measurable overhead from the call (unlikely in any case, not just this one) the "if" is actually going to be done by "free", and thus causing actually worse performance.

Not that man pages are always right, but in this case they agree with POSIX. :)

Paolo