Skip to contentSkip to conversation

Reducing stack frame size in stream_process_mem2s()

PATCH Superseded [email protected] 3 participants 6 messages Oct 3, 2016 – Oct 4, 2016 Arm/Zynq architecture

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Reducing stack frame size in stream_process_mem2s()

From: Rutuja Shah <[email protected]>

This patch allocates memory for txbuf array on the heap rather than the stack. As a result, the stack frame size is reduced.

---
 hw/dma/xilinx_axidma.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/dma/xilinx_axidma.c +3 -1
index b135a5f..6c63575 100644
@@ -256,13 +256,14 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
                                  StreamSlave *tx_control_dev)
 {
     uint32_t prev_d;
-    unsigned char txbuf[16 * 1024];
+    unsigned char *txbuf;
     unsigned int txlen;
 
     if (!stream_running(s) || stream_idle(s)) {
         return;
     }
 
+    txbuf = g_malloc(16 * 1024);
     while (1) {
         stream_desc_load(s, s->regs[R_CURDESC]);
 
@@ -304,6 +305,7 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
             break;
         }
     }
+    g_free(txbuf);
 }
 
 static size_t stream_process_s2mem(struct Stream *s, unsigned char *buf,
Signature · 1 line
1.9.1
#1 lore raw

++ stefan Sorry for the typo.
Regards
Rutuja Shah

Quoted 41 lines · On Mon, Oct 3, 2016 at 10:26 PM, <[email protected]> wrote:

From: Rutuja Shah <[email protected]>

This patch allocates memory for txbuf array on the heap rather than the stack. As a result, the stack frame size is reduced.

---
 hw/dma/xilinx_axidma.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/dma/xilinx_axidma.c +3 -2
index b135a5f..6c63575 100644
@@ -256,13 +256,14 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
                                  StreamSlave *tx_control_dev)
 {
     uint32_t prev_d;
-    unsigned char txbuf[16 * 1024];
+    unsigned char *txbuf;
     unsigned int txlen;
 
     if (!stream_running(s) || stream_idle(s)) {
         return;
     }
 
+    txbuf = g_malloc(16 * 1024);
     while (1) {
         stream_desc_load(s, s->regs[R_CURDESC]);
 
@@ -304,6 +305,7 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
             break;
         }
     }
+    g_free(txbuf);
 }
 
 static size_t stream_process_s2mem(struct Stream *s, unsigned char *buf,
--

1.9.1

#2 lore raw
Quoted 28 lines · Rutuja Shah

++ stefan Sorry for the typo.
Regards
Rutuja Shah

Quoted 22 lines · On Mon, Oct 3, 2016 at 10:26 PM, <[email protected]> wrote:

From: Rutuja Shah <[email protected]>

This patch allocates memory for txbuf array on the heap rather than the stack. As a result, the stack frame size is reduced.

---
 hw/dma/xilinx_axidma.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/dma/xilinx_axidma.c +1 -1
index b135a5f..6c63575 100644
@@ -256,13 +256,14 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
                                  StreamSlave *tx_control_dev)
 {
     uint32_t prev_d;
-    unsigned char txbuf[16 * 1024];
+    unsigned char *txbuf;
     unsigned int txlen;
 
Quoted 5 lines

hw/dma/xilinx_axidma.c static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev, · line 266

Quoted 5 lines
hw/dma/xilinx_axidma.c +1 -0
     if (!stream_running(s) || stream_idle(s)) {
         return;
     }
 
+    txbuf = g_malloc(16 * 1024);

Hi,

Two comments.

We need to move the allocation from the data-path to initialization of the DMA objects. (e.g put txbuf into the Stream struct) We also need to fix up the use of sizeof txbuf.

Best regards,
Edgar

Quoted 14 lines
Quoted 14 lines
hw/dma/xilinx_axidma.c +1 -1
     while (1) {
         stream_desc_load(s, s->regs[R_CURDESC]);
 
@@ -304,6 +305,7 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
             break;
         }
     }
+    g_free(txbuf);
 }
 
 static size_t stream_process_s2mem(struct Stream *s, unsigned char *buf,
--

1.9.1

#3 lore raw

Hi,
Allocating txbuf in struct Stream seems to be good. I can see other pointers of struct Stream being allocated in xilinx_axidma_realize(), but I am not able to find their deallocation point anywhere? Regards
Rutuja Shah

Quoted 60 lines · Edgar E. Iglesias
Quoted 28 lines · Rutuja Shah

++ stefan Sorry for the typo.
Regards
Rutuja Shah

Quoted 22 lines · On Mon, Oct 3, 2016 at 10:26 PM, <[email protected]> wrote:

From: Rutuja Shah <[email protected]>

This patch allocates memory for txbuf array on the heap rather than the stack. As a result, the stack frame size is reduced.

---
 hw/dma/xilinx_axidma.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/dma/xilinx_axidma.c +1 -1
index b135a5f..6c63575 100644
@@ -256,13 +256,14 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
                                  StreamSlave *tx_control_dev)
 {
     uint32_t prev_d;
-    unsigned char txbuf[16 * 1024];
+    unsigned char *txbuf;
     unsigned int txlen;
 
Quoted 5 lines

hw/dma/xilinx_axidma.c static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev, · line 266

Quoted 5 lines
hw/dma/xilinx_axidma.c +1 -0
     if (!stream_running(s) || stream_idle(s)) {
         return;
     }
 
+    txbuf = g_malloc(16 * 1024);

Hi,

Two comments.

We need to move the allocation from the data-path to initialization of the DMA objects. (e.g put txbuf into the Stream struct) We also need to fix up the use of sizeof txbuf.

Best regards,
Edgar

Quoted 14 lines
Quoted 14 lines
hw/dma/xilinx_axidma.c +1 -1
     while (1) {
         stream_desc_load(s, s->regs[R_CURDESC]);
 
@@ -304,6 +305,7 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
             break;
         }
     }
+    g_free(txbuf);
 }
 
 static size_t stream_process_s2mem(struct Stream *s, unsigned char *buf,
--

1.9.1

#4 lore raw
Quoted 4 lines · Rutuja Shah

Hi,
Allocating txbuf in struct Stream seems to be good. I can see other pointers of struct Stream being allocated in xilinx_axidma_realize(), but I am not able to find their deallocation point anywhere?

Hi,

Actually, since the allocation is of fixed size you can just use an array in the Stream struct.
Otherwise, there's an unrealize method that I think is meant to be used for cleanup.

Best regards,
Edgar

Quoted 66 lines

Regards
Rutuja Shah

Quoted 60 lines · Edgar E. Iglesias
Quoted 28 lines · Rutuja Shah

++ stefan Sorry for the typo.
Regards
Rutuja Shah

Quoted 22 lines · On Mon, Oct 3, 2016 at 10:26 PM, <[email protected]> wrote:

From: Rutuja Shah <[email protected]>

This patch allocates memory for txbuf array on the heap rather than the stack. As a result, the stack frame size is reduced.

---
 hw/dma/xilinx_axidma.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/dma/xilinx_axidma.c +1 -1
index b135a5f..6c63575 100644
@@ -256,13 +256,14 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
                                  StreamSlave *tx_control_dev)
 {
     uint32_t prev_d;
-    unsigned char txbuf[16 * 1024];
+    unsigned char *txbuf;
     unsigned int txlen;
 
Quoted 5 lines

hw/dma/xilinx_axidma.c static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev, · line 266

Quoted 5 lines
hw/dma/xilinx_axidma.c +1 -0
     if (!stream_running(s) || stream_idle(s)) {
         return;
     }
 
+    txbuf = g_malloc(16 * 1024);

Hi,

Two comments.

We need to move the allocation from the data-path to initialization of the DMA objects. (e.g put txbuf into the Stream struct) We also need to fix up the use of sizeof txbuf.

Best regards,
Edgar

Quoted 14 lines
Quoted 14 lines
hw/dma/xilinx_axidma.c +1 -1
     while (1) {
         stream_desc_load(s, s->regs[R_CURDESC]);
 
@@ -304,6 +305,7 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
             break;
         }
     }
+    g_free(txbuf);
 }
 
 static size_t stream_process_s2mem(struct Stream *s, unsigned char *buf,
--

1.9.1

#5 lore raw
In reply to [email protected]
Quoted 17 lines · On 03/10/2016 18:56, [email protected] wrote:

From: Rutuja Shah <[email protected]>

This patch allocates memory for txbuf array on the heap rather than the stack. As a result, the stack frame size is reduced.

---
 hw/dma/xilinx_axidma.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/dma/xilinx_axidma.c +0 -0
index b135a5f..6c63575 100644
@@ -256,13 +256,14 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
                                  StreamSlave *tx_control_dev)
 {

hw/dma/xilinx_axidma.c static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev, · line 266

Quoted 10 lines
hw/dma/xilinx_axidma.c +2 -1
     uint32_t prev_d;
-    unsigned char txbuf[16 * 1024];
+    unsigned char *txbuf;
     unsigned int txlen;
 
     if (!stream_running(s) || stream_idle(s)) {
         return;
     }
 
+    txbuf = g_malloc(16 * 1024);

Hello, please place the allocated buffer in struct Stream (see xilinx_axidma_realize) instead. This function is a hot path, and it's better to keep allocations outside it.

Thanks,

Paolo

Quoted 12 lines
hw/dma/xilinx_axidma.c +1 -0
     while (1) {
         stream_desc_load(s, s->regs[R_CURDESC]);
 
@@ -304,6 +305,7 @@ static void stream_process_mem2s(struct Stream *s, StreamSlave *tx_data_dev,
             break;
         }
     }
+    g_free(txbuf);
 }
 
 static size_t stream_process_s2mem(struct Stream *s, unsigned char *buf,