Module Name: src
Committed By: martin
Date: Fri Sep 13 14:14:41 UTC 2024
Modified Files:
src/sys/net [netbsd-9]: bpf.c bpfdesc.h
Log Message:
Pull up following revision(s) (requested by ozaki-r in ticket #1886):
sys/net/bpfdesc.h: revision 1.49
sys/net/bpf.c: revision 1.256
sys/net/bpf.c: revision 1.257
sys/net/bpfdesc.h: revision 1.50
bpf: restore wakeup softint
This change fixes the issue that fownsignal which can take an
adaptive mutex is called inside a pserialize read section in
bpf_deliver.
Fix issue #4 (only the latter of two) in PR#58596
bpf: protect selnotify and selrecord with bd_buf_mtx
We have to make updates and checks of buffers and calls of
selnotify/selrecord atomic to satisfy constraints of sel* API.
Also, bd_state and bd_cv are protected by bd_buf_mtx now.
Fix issue #3 of PR#58596
Part of the fix is inspired by riastradh's patch.
To generate a diff of this commit:
cvs rdiff -u -r1.229.2.3 -r1.229.2.4 src/sys/net/bpf.c
cvs rdiff -u -r1.46 -r1.46.6.1 src/sys/net/bpfdesc.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/net/bpf.c
diff -u src/sys/net/bpf.c:1.229.2.3 src/sys/net/bpf.c:1.229.2.4
--- src/sys/net/bpf.c:1.229.2.3 Fri Aug 4 14:57:52 2023
+++ src/sys/net/bpf.c Fri Sep 13 14:14:41 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: bpf.c,v 1.229.2.3 2023/08/04 14:57:52 martin Exp $ */
+/* $NetBSD: bpf.c,v 1.229.2.4 2024/09/13 14:14:41 martin Exp $ */
/*
* Copyright (c) 1990, 1991, 1993
@@ -39,7 +39,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.229.2.3 2023/08/04 14:57:52 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.229.2.4 2024/09/13 14:14:41 martin Exp $");
#if defined(_KERNEL_OPT)
#include "opt_bpf.h"
@@ -268,6 +268,7 @@ static int bpf_poll(struct file *, int);
static int bpf_stat(struct file *, struct stat *);
static int bpf_close(struct file *);
static int bpf_kqfilter(struct file *, struct knote *);
+static void bpf_softintr(void *);
static const struct fileops bpf_fileops = {
.fo_name = "bpf",
@@ -562,6 +563,7 @@ bpfopen(dev_t dev, int flag, int mode, s
d->bd_atime = d->bd_mtime = d->bd_btime;
callout_init(&d->bd_callout, CALLOUT_MPSAFE);
selinit(&d->bd_sel);
+ d->bd_sih = softint_establish(SOFTINT_CLOCK, bpf_softintr, d);
d->bd_jitcode = NULL;
d->bd_filter = NULL;
BPF_DLIST_ENTRY_INIT(d);
@@ -599,10 +601,12 @@ bpf_close(struct file *fp)
*/
d->bd_pid = curproc->p_pid;
- mutex_enter(d->bd_mtx);
+ mutex_enter(d->bd_buf_mtx);
if (d->bd_state == BPF_WAITING)
- callout_halt(&d->bd_callout, d->bd_mtx);
+ callout_halt(&d->bd_callout, d->bd_buf_mtx);
d->bd_state = BPF_IDLE;
+ mutex_exit(d->bd_buf_mtx);
+ mutex_enter(d->bd_mtx);
if (d->bd_bif)
bpf_detachd(d);
mutex_exit(d->bd_mtx);
@@ -618,6 +622,7 @@ bpf_close(struct file *fp)
bpf_freed(d);
callout_destroy(&d->bd_callout);
seldestroy(&d->bd_sel);
+ softint_disestablish(d->bd_sih);
mutex_obj_free(d->bd_mtx);
mutex_obj_free(d->bd_buf_mtx);
cv_destroy(&d->bd_cv);
@@ -657,12 +662,12 @@ bpf_read(struct file *fp, off_t *offp, s
if (uio->uio_resid != d->bd_bufsize)
return (EINVAL);
- mutex_enter(d->bd_mtx);
+ mutex_enter(d->bd_buf_mtx);
if (d->bd_state == BPF_WAITING)
- callout_halt(&d->bd_callout, d->bd_mtx);
+ callout_halt(&d->bd_callout, d->bd_buf_mtx);
timed_out = (d->bd_state == BPF_TIMED_OUT);
d->bd_state = BPF_IDLE;
- mutex_exit(d->bd_mtx);
+ mutex_exit(d->bd_buf_mtx);
/*
* If the hold buffer is empty, then do a timed sleep, which
* ends when the timeout expires or when enough packets
@@ -747,13 +752,23 @@ static inline void
bpf_wakeup(struct bpf_d *d)
{
- mutex_enter(d->bd_buf_mtx);
+ KASSERT(mutex_owned(d->bd_buf_mtx));
+
cv_broadcast(&d->bd_cv);
- mutex_exit(d->bd_buf_mtx);
if (d->bd_async)
+ softint_schedule(d->bd_sih);
+ selnotify(&d->bd_sel, 0, NOTE_SUBMIT);
+}
+
+static void
+bpf_softintr(void *cookie)
+{
+ struct bpf_d *d;
+
+ d = cookie;
+ if (d->bd_async)
fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
- selnotify(&d->bd_sel, 0, 0);
}
static void
@@ -761,13 +776,13 @@ bpf_timed_out(void *arg)
{
struct bpf_d *d = arg;
- mutex_enter(d->bd_mtx);
+ mutex_enter(d->bd_buf_mtx);
if (d->bd_state == BPF_WAITING) {
d->bd_state = BPF_TIMED_OUT;
if (d->bd_slen != 0)
bpf_wakeup(d);
}
- mutex_exit(d->bd_mtx);
+ mutex_exit(d->bd_buf_mtx);
}
@@ -919,11 +934,11 @@ bpf_ioctl(struct file *fp, u_long cmd, v
d->bd_compat32 = 0;
#endif
- mutex_enter(d->bd_mtx);
+ mutex_enter(d->bd_buf_mtx);
if (d->bd_state == BPF_WAITING)
- callout_halt(&d->bd_callout, d->bd_mtx);
+ callout_halt(&d->bd_callout, d->bd_buf_mtx);
d->bd_state = BPF_IDLE;
- mutex_exit(d->bd_mtx);
+ mutex_exit(d->bd_buf_mtx);
switch (cmd) {
@@ -1468,6 +1483,7 @@ bpf_poll(struct file *fp, int events)
* An imitation of the FIONREAD ioctl code.
*/
mutex_enter(d->bd_mtx);
+ mutex_enter(d->bd_buf_mtx);
if (d->bd_hlen != 0 ||
((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
d->bd_slen != 0)) {
@@ -1481,6 +1497,7 @@ bpf_poll(struct file *fp, int events)
d->bd_state = BPF_WAITING;
}
}
+ mutex_exit(d->bd_buf_mtx);
mutex_exit(d->bd_mtx);
}
@@ -1504,12 +1521,18 @@ filt_bpfread(struct knote *kn, long hint
struct bpf_d *d = kn->kn_hook;
int rv;
- mutex_enter(d->bd_buf_mtx);
+ if (hint & NOTE_SUBMIT)
+ KASSERT(mutex_owned(d->bd_buf_mtx));
+ else
+ mutex_enter(d->bd_buf_mtx);
kn->kn_data = d->bd_hlen;
if (d->bd_immediate)
kn->kn_data += d->bd_slen;
rv = (kn->kn_data > 0);
- mutex_exit(d->bd_buf_mtx);
+ if (hint & NOTE_SUBMIT)
+ KASSERT(mutex_owned(d->bd_buf_mtx));
+ else
+ mutex_exit(d->bd_buf_mtx);
return rv;
}
@@ -1996,7 +2019,6 @@ catchpacket(struct bpf_d *d, u_char *pkt
*/
(*cpfn)(h + hdrlen, pkt, caplen);
d->bd_slen = curlen + totlen;
- mutex_exit(d->bd_buf_mtx);
/*
* Call bpf_wakeup after bd_slen has been updated so that kevent(2)
@@ -2004,6 +2026,8 @@ catchpacket(struct bpf_d *d, u_char *pkt
*/
if (do_wakeup)
bpf_wakeup(d);
+
+ mutex_exit(d->bd_buf_mtx);
}
/*
Index: src/sys/net/bpfdesc.h
diff -u src/sys/net/bpfdesc.h:1.46 src/sys/net/bpfdesc.h:1.46.6.1
--- src/sys/net/bpfdesc.h:1.46 Tue Jun 26 06:48:02 2018
+++ src/sys/net/bpfdesc.h Fri Sep 13 14:14:40 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: bpfdesc.h,v 1.46 2018/06/26 06:48:02 msaitoh Exp $ */
+/* $NetBSD: bpfdesc.h,v 1.46.6.1 2024/09/13 14:14:40 martin Exp $ */
/*
* Copyright (c) 1990, 1991, 1993
@@ -113,7 +113,6 @@ struct bpf_d {
pid_t bd_pid; /* corresponding PID */
/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
LIST_ENTRY(bpf_d) _bd_list; /* list of all BPF's */
- /* DEPRECATED. Keep it to avoid breaking kvm(3) users */
void *bd_sih; /* soft interrupt handle */
struct timespec bd_atime; /* access time */
struct timespec bd_mtime; /* modification time */
@@ -128,7 +127,7 @@ struct bpf_d {
struct pslist_entry bd_bif_dlist_entry; /* For bpf_if */
struct pslist_entry bd_bpf_dlist_entry; /* For the global list */
kmutex_t *bd_mtx;
- kmutex_t *bd_buf_mtx;
+ kmutex_t *bd_buf_mtx; /* For buffers, bd_state, bd_sel and bd_cv */
kcondvar_t bd_cv;
#endif
};