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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Feb 13, 2022 at 7:17 AM Toke Høiland-Jørgensen <toke@xxxxxxxxxx> wrote:
>
> Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> writes:
>
> > On Fri, Feb 11, 2022 at 3:49 PM Toke Høiland-Jørgensen <toke@xxxxxxxxxx> 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 <zhguan@xxxxxxxxxx>
> >> Fixes: 8bbb77b7c7a2 ("libbpf: Add various netlink helpers")
> >> Acked-by: Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx>
> >> Signed-off-by: Toke Høiland-Jørgensen <toke@xxxxxxxxxx>
> >> ---
> >
> > 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?

>
> -Toke
>




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux