Module Name: src
Committed By: martin
Date: Fri Sep 13 14:13:06 UTC 2024
Modified Files:
src/sys/net [netbsd-10]: bpf.c bpfdesc.h
Log Message:
Pull up following revision(s) (requested by ozaki-r in ticket #858):
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.249.2.2 -r1.249.2.3 src/sys/net/bpf.c
cvs rdiff -u -r1.48 -r1.48.10.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.249.2.2 src/sys/net/bpf.c:1.249.2.3
--- src/sys/net/bpf.c:1.249.2.2 Thu Aug 22 19:31:08 2024
+++ src/sys/net/bpf.c Fri Sep 13 14:13:05 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: bpf.c,v 1.249.2.2 2024/08/22 19:31:08 martin Exp $ */
+/* $NetBSD: bpf.c,v 1.249.2.3 2024/09/13 14:13:05 martin Exp $ */
/*
* Copyright (c) 1990, 1991, 1993
@@ -39,7 +39,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.249.2.2 2024/08/22 19:31:08 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.249.2.3 2024/09/13 14:13:05 martin Exp $");
#if defined(_KERNEL_OPT)
#include "opt_bpf.h"
@@ -271,6 +271,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",
@@ -605,6 +606,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_rfilter = NULL;
d->bd_wfilter = NULL;
@@ -644,10 +646,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);
@@ -663,6 +667,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);
@@ -707,12 +712,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
@@ -797,13 +802,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
@@ -811,13 +826,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);
}
@@ -979,11 +994,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);
if (d->bd_locked) {
switch (cmd) {
@@ -1560,6 +1575,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)) {
@@ -1573,6 +1589,7 @@ bpf_poll(struct file *fp, int events)
d->bd_state = BPF_WAITING;
}
}
+ mutex_exit(d->bd_buf_mtx);
mutex_exit(d->bd_mtx);
}
@@ -1601,12 +1618,18 @@ filt_bpfread(struct knote *kn, long hint
*/
d->bd_pid = curproc->p_pid;
- 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;
}
@@ -2094,7 +2117,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)
@@ -2102,6 +2124,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.48 src/sys/net/bpfdesc.h:1.48.10.1
--- src/sys/net/bpfdesc.h:1.48 Wed Jun 9 15:44:15 2021
+++ src/sys/net/bpfdesc.h Fri Sep 13 14:13:05 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: bpfdesc.h,v 1.48 2021/06/09 15:44:15 martin Exp $ */
+/* $NetBSD: bpfdesc.h,v 1.48.10.1 2024/09/13 14:13:05 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 */
@@ -130,7 +129,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
};