Skip to contentSkip to conversation

net/socket: Allocating Large sized arrays to heap

PATCH In review Pooja Dhannawat 4 participants 8 messages Mar 12, 2016 – Mar 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/socket: Allocating Large sized arrays to heap

---
 net/socket.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/socket.c +3 -3
index e32e3cb..483dcac 100644
@@ -147,10 +147,10 @@ static void net_socket_send(void *opaque)
     NetSocketState *s = opaque;
     int size, err;
     unsigned l;
-    uint8_t buf1[NET_BUFSIZE];
+    uint8_t *buf1 = g_new(uint8_t, 1);
     const uint8_t *buf;
 
-    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
+    size = qemu_recv(s->fd, (uint8_t *)buf1, sizeof(uint8_t), 0);
     if (size < 0) {
         err = socket_error();
         if (err != EWOULDBLOCK)
@@ -170,7 +170,6 @@ static void net_socket_send(void *opaque)
         s->index = 0;
         s->packet_len = 0;
         s->nc.link_down = true;
-        memset(s->buf, 0, sizeof(s->buf));
         memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
 
         return;
@@ -222,6 +221,7 @@ static void net_socket_send(void *opaque)
             break;
         }
     }
+    g_free(buf1);
 }
 
 static void net_socket_send_dgram(void *opaque)
Signature · 1 line
2.5.0
#1 lore raw

Don't get why we need this changes, could you explain more for it? and it seem it's not exactly correct, see below.

Quoted 10 lines · Pooja Dhannawat
---
  net/socket.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
net/socket.c +0 -0
index e32e3cb..483dcac 100644

net/socket.c static void net_socket_send(void *opaque) · line 153

Quoted 9 lines
net/socket.c +2 -2
@@ -147,10 +147,10 @@ static void net_socket_send(void *opaque)
      NetSocketState *s = opaque;
      int size, err;
      unsigned l;
-    uint8_t buf1[NET_BUFSIZE];
+    uint8_t *buf1 = g_new(uint8_t, 1);
      const uint8_t *buf;
 
-    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
+    size = qemu_recv(s->fd, (uint8_t *)buf1, sizeof(uint8_t), 0);

one byte for each reading, it looks expensive

net/socket.c static void net_socket_send(void *opaque) · line 174

Quoted 10 lines
net/socket.c +0 -1
      if (size < 0) {
          err = socket_error();
          if (err != EWOULDBLOCK)
@@ -170,7 +170,6 @@ static void net_socket_send(void *opaque)
          s->index = 0;
          s->packet_len = 0;
          s->nc.link_down = true;
-        memset(s->buf, 0, sizeof(s->buf));
          memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
 

forget to release buf1.

Thanks
Li ZHijian

Quoted 10 lines
net/socket.c +1 -0
          return;
@@ -222,6 +221,7 @@ static void net_socket_send(void *opaque)
              break;
          }
      }
+    g_free(buf1);
  }
 
  static void net_socket_send_dgram(void *opaque)
#2 lore raw

PATCH v2 1/1 socket: Allocating Large sized arrays to heap

In reply to Pooja Dhannawat

net_socket_send has a huge stack usage of 69712 bytes approx. Moving large arrays to heap to reduce stack usage.

---
 net/socket.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/socket.c +5 -3
index e32e3cb..3fcd7a6 100644
@@ -147,10 +147,10 @@ static void net_socket_send(void *opaque)
     NetSocketState *s = opaque;
     int size, err;
     unsigned l;
-    uint8_t buf1[NET_BUFSIZE];
+    uint8_t *buf1 = g_new(uint8_t, NET_BUFSIZE);
     const uint8_t *buf;
 
-    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
+    size = qemu_recv(s->fd, (uint8_t *)buf1, sizeof(uint8_t), 0);
     if (size < 0) {
         err = socket_error();
         if (err != EWOULDBLOCK)
@@ -170,8 +170,8 @@ static void net_socket_send(void *opaque)
         s->index = 0;
         s->packet_len = 0;
         s->nc.link_down = true;
-        memset(s->buf, 0, sizeof(s->buf));
         memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
+        g_free(buf1);
 
         return;
     }
@@ -222,6 +222,8 @@ static void net_socket_send(void *opaque)
             break;
         }
     }
+
+    g_free(buf1);
 }
 
 static void net_socket_send_dgram(void *opaque)
Signature · 1 line
2.5.0
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/1] socket: Allocating Large sized arrays to heap

Quoted 13 lines · Pooja Dhannawat

net_socket_send has a huge stack usage of 69712 bytes approx. Moving large arrays to heap to reduce stack usage.

---
 net/socket.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/socket.c +0 -0
index e32e3cb..3fcd7a6 100644

net/socket.c static void net_socket_send(void *opaque) · line 150

Quoted 5 lines
net/socket.c +1 -1
@@ -147,10 +147,10 @@ static void net_socket_send(void *opaque)
     NetSocketState *s = opaque;
     int size, err;
     unsigned l;
-    uint8_t buf1[NET_BUFSIZE];
+    uint8_t *buf1 = g_new(uint8_t, NET_BUFSIZE);

You're allocating NET_BUFSIZE worth of uint8_t's

net/socket.c static void net_socket_send(void *opaque) · line 153

Quoted 4 lines
net/socket.c +1 -1
     const uint8_t *buf;
 
-    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
+    size = qemu_recv(s->fd, (uint8_t *)buf1, sizeof(uint8_t), 0);

But only reading 1 byte which is clearly wrong. You likely wanted NET_BUFSIZE here, not sizeof(uint8_t)

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/1] socket: Allocating Large sized arrays to heap

Quoted 27 lines · Daniel P. Berrange
Quoted 13 lines · Pooja Dhannawat

net_socket_send has a huge stack usage of 69712 bytes approx. Moving large arrays to heap to reduce stack usage.

---
 net/socket.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/socket.c +0 -0
index e32e3cb..3fcd7a6 100644

net/socket.c static void net_socket_send(void *opaque) · line 150

Quoted 5 lines
net/socket.c +1 -1
@@ -147,10 +147,10 @@ static void net_socket_send(void *opaque)
     NetSocketState *s = opaque;
     int size, err;
     unsigned l;
-    uint8_t buf1[NET_BUFSIZE];
+    uint8_t *buf1 = g_new(uint8_t, NET_BUFSIZE);

You're allocating NET_BUFSIZE worth of uint8_t's

I didn't get you clear.

Quoted 2 lines
net/socket.c +0 -0
     const uint8_t *buf;
 
Quoted 7 lines

net/socket.c static void net_socket_send(void *opaque) · line 153

Quoted 2 lines
net/socket.c +1 -1
-    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
+    size = qemu_recv(s->fd, (uint8_t *)buf1, sizeof(uint8_t), 0);

But only reading 1 byte which is clearly wrong. You likely wanted NET_BUFSIZE here, not sizeof(uint8_t)

Correct me If I am wrong. This should also work :

size = qemu_recv(s->fd, (uint8_t *)buf1, NET_BUFSIZE * sizeof(uint8_t), 0);

Quoted 12 lines

Regards,
Daniel
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/
:|
|: http://libvirt.org -o- http://virt-manager.org
:|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/
:|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc
:|

#5 lore raw

Re: [Qemu-devel] [PATCH v2 1/1] socket: Allocating Large sized arrays to heap

Quoted 30 lines · Pooja Dhannawat
Quoted 27 lines · Daniel P. Berrange
Quoted 13 lines · Pooja Dhannawat

net_socket_send has a huge stack usage of 69712 bytes approx. Moving large arrays to heap to reduce stack usage.

---
 net/socket.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/socket.c +0 -0
index e32e3cb..3fcd7a6 100644

net/socket.c static void net_socket_send(void *opaque) · line 150

Quoted 5 lines
net/socket.c +1 -1
@@ -147,10 +147,10 @@ static void net_socket_send(void *opaque)
     NetSocketState *s = opaque;
     int size, err;
     unsigned l;
-    uint8_t buf1[NET_BUFSIZE];
+    uint8_t *buf1 = g_new(uint8_t, NET_BUFSIZE);

You're allocating NET_BUFSIZE worth of uint8_t's

I didn't get you clear.

Quoted 2 lines
net/socket.c +0 -0
     const uint8_t *buf;
 
Quoted 8 lines
Quoted 7 lines

net/socket.c static void net_socket_send(void *opaque) · line 153

Quoted 2 lines
net/socket.c +1 -1
-    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
+    size = qemu_recv(s->fd, (uint8_t *)buf1, sizeof(uint8_t), 0);

But only reading 1 byte which is clearly wrong. You likely wanted NET_BUFSIZE here, not sizeof(uint8_t)

Correct me If I am wrong. This should also work :

size = qemu_recv(s->fd, (uint8_t *)buf1, NET_BUFSIZE * sizeof(uint8_t), 0);

Sure, if you want to add a pointless 'multiply by 1' to the code, which you didn't bother doing for the g_new call.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#6 lore raw

Re: [Qemu-devel] [PATCH v2 1/1] socket: Allocating Large sized arrays to heap

In reply to Pooja Dhannawat
Quoted 30 lines · Pooja Dhannawat
Quoted 27 lines · Daniel P. Berrange
Quoted 13 lines · Pooja Dhannawat

net_socket_send has a huge stack usage of 69712 bytes approx. Moving large arrays to heap to reduce stack usage.

---
 net/socket.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/socket.c +0 -0
index e32e3cb..3fcd7a6 100644

net/socket.c static void net_socket_send(void *opaque) · line 150

Quoted 5 lines
net/socket.c +1 -1
@@ -147,10 +147,10 @@ static void net_socket_send(void *opaque)
     NetSocketState *s = opaque;
     int size, err;
     unsigned l;
-    uint8_t buf1[NET_BUFSIZE];
+    uint8_t *buf1 = g_new(uint8_t, NET_BUFSIZE);

You're allocating NET_BUFSIZE worth of uint8_t's

I didn't get you clear.

Quoted 2 lines
net/socket.c +0 -0
     const uint8_t *buf;
 
Quoted 8 lines
Quoted 7 lines

net/socket.c static void net_socket_send(void *opaque) · line 153

Quoted 2 lines
net/socket.c +1 -1
-    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
+    size = qemu_recv(s->fd, (uint8_t *)buf1, sizeof(uint8_t), 0);

But only reading 1 byte which is clearly wrong. You likely wanted NET_BUFSIZE here, not sizeof(uint8_t)

Correct me If I am wrong. This should also work :

size = qemu_recv(s->fd, (uint8_t *)buf1, NET_BUFSIZE * sizeof(uint8_t), 0);

An expression of type "T[]" deteriorates to "T *" without a cast. Therefore, your cast of buf1 to uint8_t * is redundant and clutters the code, please drop it.

In general, try hard to avoid casts not just to reduce clutter, but also because casts can hide mistakes from the type checker.

sizeof(uint8_t) is always one. Multiplying by it clutters the code, please drop it.

#7 lore raw

PATCH v3 1/1 socket: Allocating Large sized arrays to heap

In reply to Pooja Dhannawat

net_socket_send has a huge stack usage of 69712 bytes approx. Moving large arrays to heap to reduce stack usage.

---
 net/socket.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/socket.c +5 -3
index e32e3cb..d7fa8ce 100644
@@ -147,10 +147,10 @@ static void net_socket_send(void *opaque)
     NetSocketState *s = opaque;
     int size, err;
     unsigned l;
-    uint8_t buf1[NET_BUFSIZE];
+    uint8_t *buf1 = g_new(uint8_t, NET_BUFSIZE);
     const uint8_t *buf;
 
-    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
+    size = qemu_recv(s->fd, (uint8_t *)buf1, NET_BUFSIZE, 0);
     if (size < 0) {
         err = socket_error();
         if (err != EWOULDBLOCK)
@@ -170,8 +170,8 @@ static void net_socket_send(void *opaque)
         s->index = 0;
         s->packet_len = 0;
         s->nc.link_down = true;
-        memset(s->buf, 0, sizeof(s->buf));
         memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
+        g_free(buf1);
 
         return;
     }
@@ -222,6 +222,8 @@ static void net_socket_send(void *opaque)
             break;
         }
     }
+
+    g_free(buf1);
 }
 
 static void net_socket_send_dgram(void *opaque)
Signature · 1 line
2.5.0