From: mvs Date: Mon, 12 Aug 2024 17:02:58 +0000 (+0000) Subject: Prepare bpf_sysctl() for upcoming net_sysctl() unlocking. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=34cc435a0e374a24ae444ba80def705a51376f39;p=openbsd Prepare bpf_sysctl() for upcoming net_sysctl() unlocking. Both NET_BPF_MAXBUFSIZE and NET_BPF_BUFSIZE (`bpf_maxbufsize' and `bpf_bufsize' respectively) are atomically accessed integers. No locks required to modify them. ok bluhm --- diff --git a/sys/net/bpf.c b/sys/net/bpf.c index 6adc594f1b3..b7c2f1bfd71 100644 --- a/sys/net/bpf.c +++ b/sys/net/bpf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bpf.c,v 1.223 2024/08/05 23:56:10 dlg Exp $ */ +/* $OpenBSD: bpf.c,v 1.224 2024/08/12 17:02:58 mvs Exp $ */ /* $NetBSD: bpf.c,v 1.33 1997/02/21 23:59:35 thorpej Exp $ */ /* @@ -83,11 +83,16 @@ #define PRINET 26 /* interruptible */ +/* + * Locks used to protect data: + * a atomic + */ + /* * The default read buffer size is patchable. */ -int bpf_bufsize = BPF_BUFSIZE; -int bpf_maxbufsize = BPF_MAXBUFSIZE; +int bpf_bufsize = BPF_BUFSIZE; /* [a] */ +int bpf_maxbufsize = BPF_MAXBUFSIZE; /* [a] */ /* * bpf_iflist is the list of interfaces; each corresponds to an ifnet @@ -117,8 +122,6 @@ int filt_bpfread(struct knote *, long); int filt_bpfreadmodify(struct kevent *, struct knote *); int filt_bpfreadprocess(struct knote *, struct kevent *); -int bpf_sysctl_locked(int *, u_int, void *, size_t *, void *, size_t); - struct bpf_d *bpfilter_lookup(int); /* @@ -137,9 +140,6 @@ void bpf_d_smr(void *); void bpf_get(struct bpf_d *); void bpf_put(struct bpf_d *); - -struct rwlock bpf_sysctl_lk = RWLOCK_INITIALIZER("bpfsz"); - int bpf_movein(struct uio *uio, struct bpf_d *d, struct mbuf **mp, struct sockaddr *sockp) @@ -393,7 +393,7 @@ bpfopen(dev_t dev, int flag, int mode, struct proc *p) /* Mark "free" and do most initialization. */ bd->bd_unit = unit; - bd->bd_bufsize = bpf_bufsize; + bd->bd_bufsize = atomic_load_int(&bpf_bufsize); bd->bd_sig = SIGIO; mtx_init(&bd->bd_mtx, IPL_NET); task_set(&bd->bd_wake_task, bpf_wakeup_cb, bd); @@ -853,9 +853,11 @@ bpfioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) error = EINVAL; else { u_int size = *(u_int *)addr; + int bpf_maxbufsize_local = + atomic_load_int(&bpf_maxbufsize); - if (size > bpf_maxbufsize) - *(u_int *)addr = size = bpf_maxbufsize; + if (size > bpf_maxbufsize_local) + *(u_int *)addr = size = bpf_maxbufsize_local; else if (size < BPF_MINBUFSIZE) *(u_int *)addr = size = BPF_MINBUFSIZE; mtx_enter(&d->bd_mtx); @@ -1815,42 +1817,25 @@ bpfsdetach(void *p) } int -bpf_sysctl_locked(int *name, u_int namelen, void *oldp, size_t *oldlenp, - void *newp, size_t newlen) +bpf_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, + size_t newlen) { + if (namelen != 1) + return (ENOTDIR); + switch (name[0]) { case NET_BPF_BUFSIZE: return sysctl_int_bounded(oldp, oldlenp, newp, newlen, - &bpf_bufsize, BPF_MINBUFSIZE, bpf_maxbufsize); + &bpf_bufsize, BPF_MINBUFSIZE, + atomic_load_int(&bpf_maxbufsize)); case NET_BPF_MAXBUFSIZE: return sysctl_int_bounded(oldp, oldlenp, newp, newlen, &bpf_maxbufsize, BPF_MINBUFSIZE, INT_MAX); default: return (EOPNOTSUPP); } -} - -int -bpf_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, - size_t newlen) -{ - int flags = RW_INTR; - int error; - if (namelen != 1) - return (ENOTDIR); - - flags |= (newp == NULL) ? RW_READ : RW_WRITE; - - error = rw_enter(&bpf_sysctl_lk, flags); - if (error != 0) - return (error); - - error = bpf_sysctl_locked(name, namelen, oldp, oldlenp, newp, newlen); - - rw_exit(&bpf_sysctl_lk); - - return (error); + /* NOTREACHED */ } struct bpf_d *