All of lore.kernel.org
 help / color / mirror / Atom feed
* Advisory file locking behaviour of bpf_link (and others?)
@ 2020-08-25 13:38 Lorenz Bauer
  2020-08-25 18:06 ` Alexei Starovoitov
  0 siblings, 1 reply; 5+ messages in thread
From: Lorenz Bauer @ 2020-08-25 13:38 UTC (permalink / raw)
  To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko; +Cc: kernel-team

Hi,

I was playing around a bit, and noticed that trying to acquire an
exclusive POSIX record lock on a bpf_link fd fails. I've traced this
to the call to anon_inode_getfile from bpf_link_prime which
effectively specifies O_RDONLY on the bpf_link struct file. This makes
check_fmode_for_setlk return EBADF.

This means the following:
* flock(link, LOCK_EX): works
* fcntl(link, SETLK, F_RDLCK): works
* fcntl(link, SETLK, F_WRLCK): doesn't work

Especially the discrepancy between flock(EX) and fcntl(WRLCK) has me
puzzled. Should fcntl(WRLCK) work on a link?

program fds are always O_RDWR as far as I can tell (so all locks
work), while maps depend on map_flags.

Best
Lorenz

-- 
Lorenz Bauer  |  Systems Engineer
6th Floor, County Hall/The Riverside Building, SE1 7PB, UK

www.cloudflare.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Advisory file locking behaviour of bpf_link (and others?)
  2020-08-25 13:38 Advisory file locking behaviour of bpf_link (and others?) Lorenz Bauer
@ 2020-08-25 18:06 ` Alexei Starovoitov
  2020-08-26  7:50   ` Lorenz Bauer
  0 siblings, 1 reply; 5+ messages in thread
From: Alexei Starovoitov @ 2020-08-25 18:06 UTC (permalink / raw)
  To: Lorenz Bauer
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, kernel-team

On Tue, Aug 25, 2020 at 6:39 AM Lorenz Bauer <lmb@cloudflare.com> wrote:
>
> Hi,
>
> I was playing around a bit, and noticed that trying to acquire an
> exclusive POSIX record lock on a bpf_link fd fails. I've traced this
> to the call to anon_inode_getfile from bpf_link_prime which
> effectively specifies O_RDONLY on the bpf_link struct file. This makes
> check_fmode_for_setlk return EBADF.
>
> This means the following:
> * flock(link, LOCK_EX): works
> * fcntl(link, SETLK, F_RDLCK): works
> * fcntl(link, SETLK, F_WRLCK): doesn't work
>
> Especially the discrepancy between flock(EX) and fcntl(WRLCK) has me
> puzzled. Should fcntl(WRLCK) work on a link?
>
> program fds are always O_RDWR as far as I can tell (so all locks
> work), while maps depend on map_flags.

Because for links fd/file flags are reserved for the future use.
progs are rdwr for historical reasons while maps can have three combinations:
/* Flags for accessing BPF object from syscall side. */
        BPF_F_RDONLY            = (1U << 3),
        BPF_F_WRONLY            = (1U << 4),
by default they are rdwr.
What is your use case to use flock on bpf_link fd?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Advisory file locking behaviour of bpf_link (and others?)
  2020-08-25 18:06 ` Alexei Starovoitov
@ 2020-08-26  7:50   ` Lorenz Bauer
  2020-08-26  9:22     ` Toke Høiland-Jørgensen
  0 siblings, 1 reply; 5+ messages in thread
From: Lorenz Bauer @ 2020-08-26  7:50 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, kernel-team

On Tue, 25 Aug 2020 at 19:06, Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Aug 25, 2020 at 6:39 AM Lorenz Bauer <lmb@cloudflare.com> wrote:
> >
> > Hi,
> >
> > I was playing around a bit, and noticed that trying to acquire an
> > exclusive POSIX record lock on a bpf_link fd fails. I've traced this
> > to the call to anon_inode_getfile from bpf_link_prime which
> > effectively specifies O_RDONLY on the bpf_link struct file. This makes
> > check_fmode_for_setlk return EBADF.
> >
> > This means the following:
> > * flock(link, LOCK_EX): works
> > * fcntl(link, SETLK, F_RDLCK): works
> > * fcntl(link, SETLK, F_WRLCK): doesn't work
> >
> > Especially the discrepancy between flock(EX) and fcntl(WRLCK) has me
> > puzzled. Should fcntl(WRLCK) work on a link?
> >
> > program fds are always O_RDWR as far as I can tell (so all locks
> > work), while maps depend on map_flags.
>
> Because for links fd/file flags are reserved for the future use.
> progs are rdwr for historical reasons while maps can have three combinations:
> /* Flags for accessing BPF object from syscall side. */
>         BPF_F_RDONLY            = (1U << 3),
>         BPF_F_WRONLY            = (1U << 4),
> by default they are rdwr.
> What is your use case to use flock on bpf_link fd?

The idea is to prevent concurrent access / modification of pinned maps
+ pinned link from a command line tool. I could just as well lock one
of the maps for this, but conceptually the link is the thing that
actually controls what maps are used via the attached BPF program.
FWIW I'm using flock(EX) on the link for now, which is fine for my use
case. I just thought I'd raise this in case it was an oversight :)

Best
Lorenz

-- 
Lorenz Bauer  |  Systems Engineer
6th Floor, County Hall/The Riverside Building, SE1 7PB, UK

www.cloudflare.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Advisory file locking behaviour of bpf_link (and others?)
  2020-08-26  7:50   ` Lorenz Bauer
@ 2020-08-26  9:22     ` Toke Høiland-Jørgensen
  2020-08-26 13:32       ` Lorenz Bauer
  0 siblings, 1 reply; 5+ messages in thread
From: Toke Høiland-Jørgensen @ 2020-08-26  9:22 UTC (permalink / raw)
  To: Lorenz Bauer, Alexei Starovoitov
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, kernel-team

Lorenz Bauer <lmb@cloudflare.com> writes:

> On Tue, 25 Aug 2020 at 19:06, Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
>>
>> On Tue, Aug 25, 2020 at 6:39 AM Lorenz Bauer <lmb@cloudflare.com> wrote:
>> >
>> > Hi,
>> >
>> > I was playing around a bit, and noticed that trying to acquire an
>> > exclusive POSIX record lock on a bpf_link fd fails. I've traced this
>> > to the call to anon_inode_getfile from bpf_link_prime which
>> > effectively specifies O_RDONLY on the bpf_link struct file. This makes
>> > check_fmode_for_setlk return EBADF.
>> >
>> > This means the following:
>> > * flock(link, LOCK_EX): works
>> > * fcntl(link, SETLK, F_RDLCK): works
>> > * fcntl(link, SETLK, F_WRLCK): doesn't work
>> >
>> > Especially the discrepancy between flock(EX) and fcntl(WRLCK) has me
>> > puzzled. Should fcntl(WRLCK) work on a link?
>> >
>> > program fds are always O_RDWR as far as I can tell (so all locks
>> > work), while maps depend on map_flags.
>>
>> Because for links fd/file flags are reserved for the future use.
>> progs are rdwr for historical reasons while maps can have three combinations:
>> /* Flags for accessing BPF object from syscall side. */
>>         BPF_F_RDONLY            = (1U << 3),
>>         BPF_F_WRONLY            = (1U << 4),
>> by default they are rdwr.
>> What is your use case to use flock on bpf_link fd?
>
> The idea is to prevent concurrent access / modification of pinned maps
> + pinned link from a command line tool. I could just as well lock one
> of the maps for this, but conceptually the link is the thing that
> actually controls what maps are used via the attached BPF program.
> FWIW I'm using flock(EX) on the link for now, which is fine for my use
> case. I just thought I'd raise this in case it was an oversight :)

FWIW I'm doing something similar in libxdp, except I'm using flock(EX)
on the parent directory (i.e., /sys/fs/bpf/xdp) since I need to protect
multiple modifications inside it:

https://github.com/xdp-project/xdp-tools/blob/master/lib/libxdp/libxdp.c#L245

-Toke


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Advisory file locking behaviour of bpf_link (and others?)
  2020-08-26  9:22     ` Toke Høiland-Jørgensen
@ 2020-08-26 13:32       ` Lorenz Bauer
  0 siblings, 0 replies; 5+ messages in thread
From: Lorenz Bauer @ 2020-08-26 13:32 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen
  Cc: Alexei Starovoitov, bpf, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, kernel-team

On Wed, 26 Aug 2020 at 10:22, Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>
> Lorenz Bauer <lmb@cloudflare.com> writes:
>
> > On Tue, 25 Aug 2020 at 19:06, Alexei Starovoitov
> > <alexei.starovoitov@gmail.com> wrote:
> >>
> >> On Tue, Aug 25, 2020 at 6:39 AM Lorenz Bauer <lmb@cloudflare.com> wrote:
> >> >
> >> > Hi,
> >> >
> >> > I was playing around a bit, and noticed that trying to acquire an
> >> > exclusive POSIX record lock on a bpf_link fd fails. I've traced this
> >> > to the call to anon_inode_getfile from bpf_link_prime which
> >> > effectively specifies O_RDONLY on the bpf_link struct file. This makes
> >> > check_fmode_for_setlk return EBADF.
> >> >
> >> > This means the following:
> >> > * flock(link, LOCK_EX): works
> >> > * fcntl(link, SETLK, F_RDLCK): works
> >> > * fcntl(link, SETLK, F_WRLCK): doesn't work
> >> >
> >> > Especially the discrepancy between flock(EX) and fcntl(WRLCK) has me
> >> > puzzled. Should fcntl(WRLCK) work on a link?
> >> >
> >> > program fds are always O_RDWR as far as I can tell (so all locks
> >> > work), while maps depend on map_flags.
> >>
> >> Because for links fd/file flags are reserved for the future use.
> >> progs are rdwr for historical reasons while maps can have three combinations:
> >> /* Flags for accessing BPF object from syscall side. */
> >>         BPF_F_RDONLY            = (1U << 3),
> >>         BPF_F_WRONLY            = (1U << 4),
> >> by default they are rdwr.
> >> What is your use case to use flock on bpf_link fd?
> >
> > The idea is to prevent concurrent access / modification of pinned maps
> > + pinned link from a command line tool. I could just as well lock one
> > of the maps for this, but conceptually the link is the thing that
> > actually controls what maps are used via the attached BPF program.
> > FWIW I'm using flock(EX) on the link for now, which is fine for my use
> > case. I just thought I'd raise this in case it was an oversight :)
>
> FWIW I'm doing something similar in libxdp, except I'm using flock(EX)
> on the parent directory (i.e., /sys/fs/bpf/xdp) since I need to protect
> multiple modifications inside it:

Thank you for the suggestion, that is indeed much nicer! Now why did I
bother with fcntl in the first place? :)

Best

-- 
Lorenz Bauer  |  Systems Engineer
6th Floor, County Hall/The Riverside Building, SE1 7PB, UK

www.cloudflare.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-08-26 13:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-25 13:38 Advisory file locking behaviour of bpf_link (and others?) Lorenz Bauer
2020-08-25 18:06 ` Alexei Starovoitov
2020-08-26  7:50   ` Lorenz Bauer
2020-08-26  9:22     ` Toke Høiland-Jørgensen
2020-08-26 13:32       ` Lorenz Bauer

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.