From mboxrd@z Thu Jan 1 00:00:00 1970 From: Tom Rini Date: Sat, 12 Sep 2015 08:51:26 -0400 Subject: [U-Boot] [U-Boot,05/10] dfu: don't keep freeing/reallocating In-Reply-To: <1441425831-3441-5-git-send-email-swarren@wwwdotorg.org> References: <1441425831-3441-5-git-send-email-swarren@wwwdotorg.org> Message-ID: <20150912125126.GC26226@bill-the-cat> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de On Fri, Sep 04, 2015 at 10:03:46PM -0600, Stephen Warren wrote: > From: Stephen Warren > > DFU currently allocates buffer memory at the start of each data transfer > operation and frees it at the end. Especially since memalign() is used to > allocate the buffer, and various other allocations happen during the > transfer, this can expose the code to heap fragmentation, which prevents > the allocation from succeeding on subsequent transfers. > > Fix the code to allocate the buffer once when DFU mode is initialized, > and free the buffer once when DFU mode is exited, to reduce the exposure > to heap fragmentation. > > The failure mode is: > > // Internally to memalign(), this allocates a lot more than s to guarantee > // that alignment can occur, then returns chunks of memory at the start/ > // end of the allocated buffer to the heap. > p = memalign(a, s); > // Various other malloc()s occur here, some of which allocate the RAM > // immediately before/after "p". > // > // DFU transfer is complete, so buffer is released. > free(p); > // By chance, no other malloc()/free() here, in DFU at least. > // > // A new DFU transfer starts, so the buffer is allocated again. > // In theory this should succeed since we just free()d a buffer of the > // same size. However, this fails because memalign() internally attempts > // to allocate much more than "s", yet free(p) above only free()d a > // little more than "s". > p = memalign(a, s); > > Signed-off-by: Stephen Warren > Acked-by: Lukasz Majewski > Tested-by: Lukasz Majewski Applied to u-boot/master, thanks! -- Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: Digital signature URL: