Skip to contentSkip to conversation

nbd: changes around allocating buffers for requests

PATCH In review Paolo Bonzini 3 participants 6 messages Jan 7, 2016 – Feb 8, 2016 Block layer

lore
Share & tools
lore
#0 lore raw

cover nbd: changes around allocating buffers for requests

Notably, patch 2 introduces blk_try_blockalign. If it is acked by the block layer maintainers I can submit the patch myself for inclusion.

Paolo

Paolo Bonzini (2):
nbd: do not check request length except for reads and writes
nbd: do not exit on failed memory allocation

 block/block-backend.c          |  5 +++++
 include/sysemu/block-backend.h |  1 +
 nbd.c                          | 20 ++++++++++++--------
 3 files changed, 18 insertions(+), 8 deletions(-)
Signature · 1 line
2.5.0
#1 lore raw

PATCH 1/2 nbd: do not check request length except for reads and writes

Only reads and writes need to allocate memory correspondent to the request length. Other requests can be sent to the storage without allocating any memory, and thus any request length is acceptable.

---
 nbd.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
nbd.c +7 -7
index b3d9654..e395a16 100644
@@ -1227,13 +1227,6 @@ static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *reque
         goto out;
     }
 
-    if (request->len > NBD_MAX_BUFFER_SIZE) {
-        LOG("len (%u) is larger than max len (%u)",
-            request->len, NBD_MAX_BUFFER_SIZE);
-        rc = -EINVAL;
-        goto out;
-    }
-
     if ((request->from + request->len) < request->from) {
         LOG("integer overflow detected! "
             "you're probably being attacked");
@@ -1245,6 +1238,13 @@ static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *reque
 
     command = request->type & NBD_CMD_MASK_COMMAND;
     if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
+        if (request->len > NBD_MAX_BUFFER_SIZE) {
+            LOG("len (%u) is larger than max len (%u)",
+                request->len, NBD_MAX_BUFFER_SIZE);
+            rc = -EINVAL;
+            goto out;
+        }
+
         req->data = blk_blockalign(client->exp->blk, request->len);
     }
     if (command == NBD_CMD_WRITE) {
Signature · 1 line
2.5.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] nbd: do not check request length except for reads and writes

Quoted 10 lines · Paolo Bonzini

Only reads and writes need to allocate memory correspondent to the request length. Other requests can be sent to the storage without allocating any memory, and thus any request length is acceptable.

---
 nbd.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
#3 lore raw

PATCH 2/2 nbd: do not exit on failed memory allocation

In reply to Paolo Bonzini

The amount of memory allocated in nbd_co_receive_request is driven by the NBD client (possibly a virtual machine). Parallel I/O can cause the server to allocate a large amount of memory; check for failures and return ENOMEM in that case.

---
 block/block-backend.c          | 5 +++++
 include/sysemu/block-backend.h | 1 +
 nbd.c                          | 6 +++++-
 3 files changed, 11 insertions(+), 1 deletion(-)
block/block-backend.c +5 -0
index f41d326..e813759 100644
@@ -1033,6 +1033,11 @@ void blk_set_guest_block_size(BlockBackend *blk, int align)
     blk->guest_block_size = align;
 }
 
+void *blk_try_blockalign(BlockBackend *blk, size_t size)
+{
+    return qemu_try_blockalign(blk ? blk->bs : NULL, size);
+}
+
 void *blk_blockalign(BlockBackend *blk, size_t size)
 {
     return qemu_blockalign(blk ? blk->bs : NULL, size);
include/sysemu/block-backend.h +1 -0
index dc24476..1568554 100644
@@ -148,6 +148,7 @@ int blk_get_flags(BlockBackend *blk);
 int blk_get_max_transfer_length(BlockBackend *blk);
 int blk_get_max_iov(BlockBackend *blk);
 void blk_set_guest_block_size(BlockBackend *blk, int align);
+void *blk_try_blockalign(BlockBackend *blk, size_t size);
 void *blk_blockalign(BlockBackend *blk, size_t size);
 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp);
 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason);
nbd.c +5 -1
index e395a16..7dc58a9 100644
@@ -1245,7 +1245,11 @@ static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *reque
             goto out;
         }
 
-        req->data = blk_blockalign(client->exp->blk, request->len);
+        req->data = blk_try_blockalign(client->exp->blk, request->len);
+        if (req->data == NULL) {
+            rc = -ENOMEM;
+            goto out;
+        }
     }
     if (command == NBD_CMD_WRITE) {
         TRACE("Reading %u byte(s)", request->len);
Signature · 1 line
2.5.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] nbd: do not exit on failed memory allocation

Quoted 11 lines · Paolo Bonzini

The amount of memory allocated in nbd_co_receive_request is driven by the NBD client (possibly a virtual machine). Parallel I/O can cause the server to allocate a large amount of memory; check for failures and return ENOMEM in that case.

---
 block/block-backend.c          | 5 +++++
 include/sysemu/block-backend.h | 1 +
 nbd.c                          | 6 +++++-
 3 files changed, 11 insertions(+), 1 deletion(-)
Reviewed-by on the v1 series
#5 lore raw
In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini

Notably, patch 2 introduces blk_try_blockalign. If it is acked by the block layer maintainers I can submit the patch myself for inclusion.

Paolo

Quoted 13 lines

Paolo Bonzini (2):
nbd: do not check request length except for reads and writes
nbd: do not exit on failed memory allocation

 block/block-backend.c          |  5 +++++
 include/sysemu/block-backend.h |  1 +
 nbd.c                          | 20 ++++++++++++--------
 3 files changed, 18 insertions(+), 8 deletions(-)
Signature · 1 line
2.5.0