From: Ahmad Fatoum <[email protected]> __tfree() recurses along the sibling list too, so stack usage grows with the number of siblings and a chunk is only freed after all of its older siblings.
Let's free the children in a loop instead, each one completely before the next, most recently added first, like Samba does. This will determine the order destructors run in. Assisted-by: Claude:opus-5.5 Signed-off-by: Ahmad Fatoum <[email protected]> --- lib/talloc.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/lib/talloc.c b/lib/talloc.c index 2bfbac2e9c29..6a0fe81b9762 100644 --- a/lib/talloc.c +++ b/lib/talloc.c @@ -229,22 +229,18 @@ void *talloc_realloc_size(const void *parent, void *mem, size_t size) EXPORT_SYMBOL(talloc_realloc_size); /** - * __tfree() - Deallocate all the descendants of given parent recursively. + * __talloc_free() - Deallocate a memory chunk and all its descendants. * * @hdr: pointer to previously talloc'ed memory chunk. */ -static void __tfree(struct talloc *hdr) +static void __talloc_free(struct talloc *hdr) { - if (ZERO_OR_NULL_PTR(hdr)) - return; + /* Unlink first so cycles terminate */ + mem_set_parent(hdr, NULL); - /* Fail if the tree hierarchy has cycles. */ + while (hdr->child) + __talloc_free(hdr->child); - ASSERT(hdr->prev); - hdr->prev = NULL; - - __tfree(hdr->child); - __tfree(hdr->next); free(hdr); } @@ -260,11 +256,7 @@ void talloc_free(void *mem) if (ZERO_OR_NULL_PTR(mem)) return; - mem_set_parent(hdr, NULL); - - __tfree(hdr->child); - - free(hdr); + __talloc_free(hdr); } EXPORT_SYMBOL(talloc_free); -- 2.47.3
