Subject: Re: [PATCH bpf-next v2] libbpf: Use dynamically allocated buffer when receiving netlink messages

From: Toke Høiland-Jørgensen <[email-masked]>
Date: Mon, 14 Feb 2022 17:52:25 +0100

Andrii Nakryiko <[email-masked]> writes:

> On Sun, Feb 13, 2022 at 7:17 AM Toke Høiland-Jørgensen <[email-masked]> wrote:
>>
>> Andrii Nakryiko <[email-masked]> writes:
>>
>> > On Fri, Feb 11, 2022 at 3:49 PM Toke Høiland-Jørgensen <[email-masked]> wrote:
>> >>
>> >> When receiving netlink messages, libbpf was using a statically allocated
>> >> stack buffer of 4k bytes. This happened to work fine on systems with a 4k
>> >> page size, but on systems with larger page sizes it can lead to truncated
>> >> messages. The user-visible impact of this was that libbpf would insist no
>> >> XDP program was attached to some interfaces because that bit of the netlink
>> >> message got chopped off.
>> >>
>> >> Fix this by switching to a dynamically allocated buffer; we borrow the
>> >> approach from iproute2 of using recvmsg() with MSG_PEEK|MSG_TRUNC to get
>> >> the actual size of the pending message before receiving it, adjusting the
>> >> buffer as necessary. While we're at it, also add retries on interrupted
>> >> system calls around the recvmsg() call.
>> >>
>> >> v2:
>> >>   - Move peek logic to libbpf_netlink_recv(), don't double free on ENOMEM.
>> >>
>> >> Reported-by: Zhiqian Guan <[email-masked]>
>> >> Fixes: 8bbb77b7c7a2 ("libbpf: Add various netlink helpers")
>> >> Acked-by: Kumar Kartikeya Dwivedi <[email-masked]>
>> >> Signed-off-by: Toke Høiland-Jørgensen <[email-masked]>
>> >> ---
>> >
>> > Applied to bpf-next.
>>
>> Awesome, thanks!
>>
>> > One improvement would be to avoid initial malloc of 4096, especially
>> > if that size is enough for most cases. You could detect this through
>> > iov.iov_base == buf and not free(iov.iov_base) at the end. Seems
>> > reliable and simple enough. I'll leave it up to you to follow up, if
>> > you think it's a good idea.
>>
>> Hmm, seems distributions tend to default the stack size limit to 8k; so
>> not sure if blowing half of that on a buffer just to avoid a call to
>> malloc() in a non-performance-sensitive is ideal to begin with? I think
>> I'd prefer to just keep the dynamic allocation...
>
> 8KB for user-space thread stack, really? Not 2MB by default? Are you
> sure you are not confusing this with kernel threads?

Ha, oops! I was looking in the right place, just got the units wrong;
those were kbytes not bytes, so 8M stack size. Sorry for the confusion :)

-Toke