Skip to contentSkip to conversation

socket: Allocating Large sized arrays to heap

PATCH Superseded Pooja Dhannawat 4 participants 13 messages Mar 15, 2016 – Mar 18, 2016

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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

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..fd7f39f 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, 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
#1 lore raw

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

Quoted 5 lines · Pooja Dhannawat
net/socket.c +0 -1
@@ -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));

This change is unrelated to allocating buf1 on the heap. What is the purpose of this line?

#2 lore raw
Quoted 10 lines · Stefan Hajnoczi

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

Quoted 5 lines · Pooja Dhannawat
net/socket.c +0 -1
@@ -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));

This change is unrelated to allocating buf1 on the heap. What is the purpose of this line?

I moved buf from stack to Heap, used g_new(), but I got your point if we need to initialize it with 0 then I have to keep that one.

Other wise doing so it gets whatever garbage it has already.

#3 lore raw
Quoted 16 lines · Pooja Dhannawat

On Thu, Mar 17, 2016 at 8:20 PM, Stefan Hajnoczi <[email protected] <mailto:[email protected]>> wrote:

    On Tue, Mar 15, 2016 at 09:29:58PM +0530, Pooja Dhannawat wrote:
    > @@ -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));

    This change is unrelated to allocating buf1 on the heap.  What is the
    purpose of this line?
Quoted 4 lines

I moved buf from stack to Heap, used g_new(), but I got your point if we need to initialize it with 0 then I have to keep that one.

Other wise doing so it gets whatever garbage it has already.

This is s->buf, not buf. Also, the BiteSizedTasks page says "Make the stack array smaller and allocate on the heap in the rare case that the data does not fit in the small array".

Paolo

#4 lore raw
Quoted 19 lines · Paolo Bonzini
Quoted 16 lines · Pooja Dhannawat

On Thu, Mar 17, 2016 at 8:20 PM, Stefan Hajnoczi <[email protected] <mailto:[email protected]>> wrote:

    On Tue, Mar 15, 2016 at 09:29:58PM +0530, Pooja Dhannawat wrote:
    > @@ -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));

    This change is unrelated to allocating buf1 on the heap.  What is the
    purpose of this line?
Quoted 6 lines
Quoted 4 lines

I moved buf from stack to Heap, used g_new(), but I got your point if we need to initialize it with 0 then I have to keep that one.

Other wise doing so it gets whatever garbage it has already.

This is s->buf, not buf.

Exactly, they are different variables.

Stefan

#5 lore raw
Quoted 25 lines · Stefan Hajnoczi
Quoted 8 lines · Paolo Bonzini
Quoted 5 lines · Pooja Dhannawat

On Thu, Mar 17, 2016 at 8:20 PM, Stefan Hajnoczi <[email protected] <mailto:[email protected]>> wrote:

Quoted 8 lines
Quoted 8 lines · Pooja Dhannawat

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

Quoted 5 lines
net/socket.c +0 -1
@@ -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));

This change is unrelated to allocating buf1 on the heap. What is

the

Quoted 4 lines
Quoted 4 lines

purpose of this line?

I moved buf from stack to Heap, used g_new(), but I got your point if

we

Quoted 2 lines
Quoted 2 lines

need to initialize it with 0 then I have to keep that one.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Other wise doing so it gets whatever garbage it has already.

This is s->buf, not buf.

Exactly, they are different variables.

Yes.

The line should not be removed.
Extremely sorry for the noise and my terrible confusion. Will mail the updated patch.

Quoted 2 lines

Stefan

#6 lore raw
In reply to Paolo Bonzini
Quoted 22 lines · Paolo Bonzini
Quoted 19 lines · Pooja Dhannawat

On Thu, Mar 17, 2016 at 8:20 PM, Stefan Hajnoczi <[email protected] <mailto:[email protected]>> wrote:

    On Tue, Mar 15, 2016 at 09:29:58PM +0530, Pooja Dhannawat wrote:
    > @@ -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));

    This change is unrelated to allocating buf1 on the heap.  What is the
    purpose of this line?

I moved buf from stack to Heap, used g_new(), but I got your point if we need to initialize it with 0 then I have to keep that one.

Quoted 7 lines
Quoted 1 line

Other wise doing so it gets whatever garbage it has already.

This is s->buf, not buf. Also, the BiteSizedTasks page says "Make the stack array smaller and allocate on the heap in the rare case that the data does not fit in the small array".

So here, should I check with stack consumption(size of array) and if it is

greater than accepted level, then only keep on heap? If no, Can you please help me with this one?

Quoted 2 lines

Paolo

#7 lore raw
Quoted 33 lines · Pooja Dhannawat

On Fri, Mar 18, 2016 at 4:20 AM, Paolo Bonzini <[email protected] <mailto:[email protected]>> wrote:

    On 17/03/2016 16:31, Pooja Dhannawat wrote:
    >
    >
    > On Thu, Mar 17, 2016 at 8:20 PM, Stefan Hajnoczi <[email protected] <mailto:[email protected]>
    > <mailto:[email protected] <mailto:[email protected]>>> wrote:
    >
    >     On Tue, Mar 15, 2016 at 09:29:58PM +0530, Pooja Dhannawat wrote:
    >     > @@ -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));
    >
    >     This change is unrelated to allocating buf1 on the heap.  What is the
    >     purpose of this line?
    >
    >
    > I moved buf from stack to Heap, used g_new(), but I got your point if we
    > need to initialize it with 0 then I have to keep that one.
    >
    > Other wise doing so it gets whatever garbage it has already.

    This is s->buf, not buf.  Also, the BiteSizedTasks page says "Make the
    stack array smaller and allocate on the heap in the rare case that the
    data does not fit in the small array".
Quoted 2 lines

So here, should I check with stack consumption(size of array) and if it is greater than accepted level, then only keep on heap?

If it is greater than the accepted level, the on-stack buffer is not used and you allocate one that has the right size on the heap.

Paolo

Quoted 5 lines

If no, Can you please help me with this one?

Paolo

#8 lore raw
Quoted 39 lines · Paolo Bonzini
Quoted 11 lines · Pooja Dhannawat

On Fri, Mar 18, 2016 at 4:20 AM, Paolo Bonzini <[email protected] <mailto:[email protected]>> wrote:

    On 17/03/2016 16:31, Pooja Dhannawat wrote:
    >
    >
    > On Thu, Mar 17, 2016 at 8:20 PM, Stefan Hajnoczi <

[email protected] <mailto:[email protected]>

Quoted 3 lines
Quoted 3 lines

<mailto:[email protected] <mailto:[email protected]>>> wrote:

    On Tue, Mar 15, 2016 at 09:29:58PM +0530, Pooja Dhannawat
Quoted 7 lines · wrote:
Quoted 7 lines

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

Quoted 5 lines
net/socket.c +0 -1
@@ -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));

This change is unrelated to allocating buf1 on the heap. What

is the

Quoted 4 lines
Quoted 4 lines

purpose of this line?

I moved buf from stack to Heap, used g_new(), but I got your point

if we

Quoted 5 lines
Quoted 3 lines

need to initialize it with 0 then I have to keep that one.

Other wise doing so it gets whatever garbage it has already.

    This is s->buf, not buf.  Also, the BiteSizedTasks page says "Make

the

Quoted 1 line

stack array smaller and allocate on the heap in the rare case that

Quoted 9 lines

the

Quoted 4 lines

data does not fit in the small array".

So here, should I check with stack consumption(size of array) and if it is greater than accepted level, then only keep on heap?

If it is greater than the accepted level, the on-stack buffer is not used and you allocate one that has the right size on the heap.

Yes Okay. Thank you for the comments.
I had one more question.
size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
The one above returns bytes read into buf1 (if large then bytes equivalent to len(buf1) is read) ?
If true, size is the correct measure of buf1? Hence, I should compare the allowed stack size to "size" variable?

Quoted 10 lines

Paolo

Quoted 5 lines

If no, Can you please help me with this one?

Paolo

Signature · 2 lines
Regards,
Jaya
#9 lore raw
Quoted 54 lines · Jaya Tiwari
Quoted 40 lines · Paolo Bonzini
Quoted 11 lines · Pooja Dhannawat

On Fri, Mar 18, 2016 at 4:20 AM, Paolo Bonzini <[email protected] <mailto:[email protected]>> wrote:

    On 17/03/2016 16:31, Pooja Dhannawat wrote:
    >
    >
    > On Thu, Mar 17, 2016 at 8:20 PM, Stefan Hajnoczi <

[email protected] <mailto:[email protected]>

Quoted 3 lines
Quoted 3 lines

<mailto:[email protected] <mailto:[email protected]>>> wrote:

    On Tue, Mar 15, 2016 at 09:29:58PM +0530, Pooja Dhannawat
Quoted 1 line · wrote:
Quoted 1 line

Review static void net_socket_send(void

Quoted 1 line
(quoted diff) +0 -0
@@ -170,8 +170,8 @@ static void net_socket_send(void

*opaque)

Quoted 6 lines
Quoted 6 lines

Review static void net_socket_send(void · line 173

Quoted 4 lines
(quoted diff) +0 -1
         s->index = 0;
         s->packet_len = 0;
         s->nc.link_down = true;
-        memset(s->buf, 0, sizeof(s->buf));

This change is unrelated to allocating buf1 on the heap.

What is the

Quoted 4 lines
Quoted 4 lines

purpose of this line?

I moved buf from stack to Heap, used g_new(), but I got your

point if we

Quoted 5 lines
Quoted 3 lines

need to initialize it with 0 then I have to keep that one.

Other wise doing so it gets whatever garbage it has already.

    This is s->buf, not buf.  Also, the BiteSizedTasks page says "Make

the

Quoted 1 line

stack array smaller and allocate on the heap in the rare case that

Quoted 9 lines

the

Quoted 4 lines

data does not fit in the small array".

So here, should I check with stack consumption(size of array) and if it is greater than accepted level, then only keep on heap?

If it is greater than the accepted level, the on-stack buffer is not used and you allocate one that has the right size on the heap.

Quoted 8 lines

Yes Okay. Thank you for the comments.
I had one more question.
size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
The one above returns bytes read into buf1 (if large then bytes equivalent to len(buf1) is read) ?
If true, size is the correct measure of buf1? Hence, I should compare the allowed stack size to "size" variable?

So isnt here size should be compared to "size" varibale paolo?

Quoted 16 lines
Quoted 10 lines

Paolo

Quoted 5 lines

If no, Can you please help me with this one?

Paolo

--
Regards,
Jaya

Signature · 2 lines
Regards,
Jaya
#10 lore raw
Quoted 66 lines · Jaya Tiwari
Quoted 54 lines · Jaya Tiwari
Quoted 40 lines · Paolo Bonzini
Quoted 11 lines · Pooja Dhannawat

On Fri, Mar 18, 2016 at 4:20 AM, Paolo Bonzini <[email protected] <mailto:[email protected]>> wrote:

    On 17/03/2016 16:31, Pooja Dhannawat wrote:
    >
    >
    > On Thu, Mar 17, 2016 at 8:20 PM, Stefan Hajnoczi <

[email protected] <mailto:[email protected]>

Quoted 3 lines
Quoted 3 lines

<mailto:[email protected] <mailto:[email protected]>>> wrote:

    On Tue, Mar 15, 2016 at 09:29:58PM +0530, Pooja Dhannawat
Quoted 1 line · wrote:
Quoted 1 line

Review static void net_socket_send(void

Quoted 1 line
(quoted diff) +0 -0
@@ -170,8 +170,8 @@ static void net_socket_send(void

*opaque)

Quoted 6 lines
Quoted 6 lines

Review static void net_socket_send(void · line 173

Quoted 4 lines
(quoted diff) +0 -1
         s->index = 0;
         s->packet_len = 0;
         s->nc.link_down = true;
-        memset(s->buf, 0, sizeof(s->buf));

This change is unrelated to allocating buf1 on the heap.

What is the

Quoted 4 lines
Quoted 4 lines

purpose of this line?

I moved buf from stack to Heap, used g_new(), but I got your

point if we

Quoted 5 lines
Quoted 3 lines

need to initialize it with 0 then I have to keep that one.

Other wise doing so it gets whatever garbage it has already.

    This is s->buf, not buf.  Also, the BiteSizedTasks page says "Make

the

Quoted 1 line

stack array smaller and allocate on the heap in the rare case that

Quoted 9 lines

the

Quoted 4 lines

data does not fit in the small array".

So here, should I check with stack consumption(size of array) and if it is greater than accepted level, then only keep on heap?

If it is greater than the accepted level, the on-stack buffer is not used and you allocate one that has the right size on the heap.

Quoted 8 lines

Yes Okay. Thank you for the comments.
I had one more question.
size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
The one above returns bytes read into buf1 (if large then bytes equivalent to len(buf1) is read) ?
If true, size is the correct measure of buf1? Hence, I should compare the allowed stack size to "size" variable?

Quoted 2 lines

So isnt here size should be compared to "size" varibale paolo?

So instead of comparing with NET_BUFSIZE, should I compare with "size" variable?
Can you help me with this?

Quoted 23 lines
Quoted 12 lines
Quoted 10 lines

Paolo

Quoted 5 lines

If no, Can you please help me with this one?

Paolo

Quoted 4 lines

--
Regards,
Jaya

--
Regards,
Jaya

#11 lore raw
Quoted 14 lines · Pooja Dhannawat

If it is greater than the accepted level, the on-stack buffer is not
used and you allocate one that has the right size on the heap.

Yes Okay. Thank you for the comments.
I had one more question.
size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
The one above returns bytes read into buf1 (if large then bytes equivalent to len(buf1) is read) ?
If true, size is the correct measure of buf1? Hence, I should compare the allowed stack size to "size" variable?

Quoted 4 lines

So isnt here size should be compared to "size" varibale paolo?

So instead of comparing with NET_BUFSIZE, should I compare with "size" variable? Can you help me with this?

I was a bit confused myself; this function actually is a bit different from the others because it does not really need a large buffer. The function already takes care of moving data in pieces from buf1 to s->buf. If you make the buffer smaller, the only change is that one call to net_socket_send will process fewer bytes.

So you can just send a trivial patch that changes the size of the array to something like 2048.

Thanks, and sorry for putting you on a false track!

Paolo

#12 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 5 lines · Pooja Dhannawat

If it is greater than the accepted level, the on-stack buffer is not
used and you allocate one that has the right size on the

heap.

Quoted 20 lines
Quoted 9 lines

Yes Okay. Thank you for the comments.
I had one more question.
size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
The one above returns bytes read into buf1 (if large then bytes equivalent to len(buf1) is read) ?
If true, size is the correct measure of buf1? Hence, I should compare the allowed stack size to "size" variable?

Quoted 4 lines

So isnt here size should be compared to "size" varibale paolo?

So instead of comparing with NET_BUFSIZE, should I compare with "size" variable? Can you help me with this?

I was a bit confused myself; this function actually is a bit different from the others because it does not really need a large buffer. The function already takes care of moving data in pieces from buf1 to s->buf. If you make the buffer smaller, the only change is that one call to net_socket_send will process fewer bytes.

Quoted 5 lines

So you can just send a trivial patch that changes the size of the array to something like 2048.

Thanks, and sorry for putting you on a false track!

No, it's completely fine, I really appreciate your help.

Quoted 2 lines

Paolo