LPC17xx-40xx emac driver memory leak on TX (found and fixed)

cancel
Showing results for 
Search instead for 
Did you mean: 

LPC17xx-40xx emac driver memory leak on TX (found and fixed)

881 Views
engineering1
Contributor II

Hello,

I have found and fixed a memory leak in the EMAC driver, in file lpc17xx_40xx_emac.c.
Note: I have NO_SYS=1 and LPC_NUM_BUFF_TXDESCS=4

The memory leak happens when 4 calls of the lpc_low_level_output() functions are performed from the LWIP stack before returning to the user code (for example the ARP protocol adds some packets each ARP_AGE_REREQUEST_USED_UNICAST seconds).
Then if the user code spends some time doing other things, the CONSUME register can make a 'full loop' and go back to its original value.
When this happens, the lpc_tx_reclaim() function does not detect it and returns immediately leaving 4 pbuf which pointers will be overwritten at the next lpc_low_level_output() calls. These pbuf will never be freed.

To fix this, I have added the following code inside the lpc_low_level_output() function:

        if (dn == 0) {
              /* Save size of packet and signal it's ready */
              lpc_enetif->ptxd[idx].Control = ENET_TCTRL_SIZE(q->len) | ENET_TCTRL_INT |
                                              ENET_TCTRL_LAST;
// NEW CODE
            if (lpc_enetif->txb[idx] != NULL) {
              pbuf_free(lpc_enetif->txb[idx]);
            }
// END NEW CODE

            lpc_enetif->txb[idx] = p;
          }

Note that calling the lpc_tx_reclaim() function is not needed any more, although it can be a good idea to free the pbuf on idle time, rather than at the time we need to send a packet.

Thank you,
Sébastien

Labels (1)
Tags (2)
0 Kudos
2 Replies

698 Views
craigmcqueenir
Contributor III

Thanks for sharing this. I am working on an LPC17xx-based project that uses the EMAC, with FreeRTOS and lwIP. I haven't noticed this bug during my testing, but perhaps it has occurred and I haven't noticed.

I'm interested to know if NXP engineers have done testing and/or code reviews of this bug fix, and what their assessment is.

0 Kudos

698 Views
FelipeGarcia
NXP TechSupport
NXP TechSupport

Hello,

 

I am glad to hear that the problem has been resolved, and thanks for sharing the fix. Please don't hesitate to contact our technical support if you have any other question.

 

Best regards,

Felipe

0 Kudos