On 2018/10/19 下午6:04, David Sterba wrote:
> On Fri, Oct 19, 2018 at 05:46:28PM +0800, Qu Wenruo wrote:
>> Did you mean some like this is possible?
>>
>> rbtree_postorder_for_each_entry_safe() {
>> kfree(entry);
>> }
>>
>> If so, I still don't really believe it's OK.
>>
>> For the following tree:
>> 4
>> / \
>> 2 6
>> / \ / \
>> 1 3 5 7
>>
>> If current entry is 2, next is 3.
>> And 2 get freed.
>> Then we go 3, to reach next we need to go back to access 2, which is
>> already freed, we will trigger use-after-free.
>>
>> So the only correct way to free the whole rbtree is still that tried and
>> true while(rb_first()) loop.
>>
>> Or did I miss something?
>
> It's postorder traversal so the node is ready for processing after all
> it's children are processed. In the above example, it will result in the
> followin sequence: 1 3 2 5 7 6 4 .
Oh, I totally misunderstand the word post-order.
Post-order is LRN, so upper nodes are always accessed last, then kfree()
inside post-order is completely OK.
Thanks,
Qu
>
> The iterator is safe in the sense that there's enough information saved
> before going to the node so it's not needed to be accessed later, eg.
> after it's freed.
>
> prelim_release uses the same postorder/kfree pattern.
>
Attachment:
signature.asc
Description: OpenPGP digital signature
