Setting EV_EOF requires modifying kn->kn_flags. However, that relies on

holding the kq_lock of that note's kq.  Rather than exposing this directly,
add new knote_set_eof() and knote_clear_eof() functions that handle the
necessary locking and don't leak as many implementation details to modules.

NetBSD 9.99.91
This commit is contained in:
thorpej 2021-10-11 01:07:36 +00:00
parent 564e383026
commit e305e37ace
10 changed files with 64 additions and 46 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: xmm7360.c,v 1.10 2021/09/26 01:16:09 thorpej Exp $ */
/* $NetBSD: xmm7360.c,v 1.11 2021/10/11 01:07:36 thorpej Exp $ */
/*
* Device driver for Intel XMM7360 LTE modems, eg. Fibocom L850-GL.
@ -75,7 +75,7 @@ MODULE_DEVICE_TABLE(pci, xmm7360_ids);
#include "opt_gateway.h"
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: xmm7360.c,v 1.10 2021/09/26 01:16:09 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: xmm7360.c,v 1.11 2021/10/11 01:07:36 thorpej Exp $");
#endif
#include <sys/param.h>
@ -166,6 +166,7 @@ typedef struct mutex spinlock_t;
#define device_private(devt) (void *)devt;
#define if_deferred_start_init(ifp, arg) /* nothing */
#define IF_OUTPUT_CONST /* nothing */
#define knote_set_eof(kn, f) (kn)->kn_flags |= EV_EOF | (f)
#define tty_lock() int s = spltty()
#define tty_unlock() splx(s)
#define tty_locked() /* nothing */
@ -2762,7 +2763,7 @@ filt_wwancread(struct knote *kn, long hint)
kn->kn_data = 0;
if (!qp->open) {
kn->kn_flags |= EV_EOF;
knote_set_eof(kn, 0);
return (1);
} else {
kn->kn_data = xmm7360_qp_has_data(qp) ? 1 : 0;

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_event.c,v 1.130 2021/10/10 19:11:56 thorpej Exp $ */
/* $NetBSD: kern_event.c,v 1.131 2021/10/11 01:07:36 thorpej Exp $ */
/*-
* Copyright (c) 2008, 2009, 2021 The NetBSD Foundation, Inc.
@ -63,7 +63,7 @@
#endif /* _KERNEL_OPT */
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.130 2021/10/10 19:11:56 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.131 2021/10/11 01:07:36 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -2578,3 +2578,30 @@ knote_activate(struct knote *kn)
out:
mutex_spin_exit(&kq->kq_lock);
}
/*
* Set EV_EOF on the specified knote. Also allows additional
* EV_* flags to be set (e.g. EV_ONESHOT).
*/
void
knote_set_eof(struct knote *kn, uint32_t flags)
{
struct kqueue *kq = kn->kn_kq;
mutex_spin_enter(&kq->kq_lock);
kn->kn_flags |= EV_EOF | flags;
mutex_spin_exit(&kq->kq_lock);
}
/*
* Clear EV_EOF on the specified knote.
*/
void
knote_clear_eof(struct knote *kn)
{
struct kqueue *kq = kn->kn_kq;
mutex_spin_enter(&kq->kq_lock);
kn->kn_flags &= ~EV_EOF;
mutex_spin_exit(&kq->kq_lock);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: sys_pipe.c,v 1.157 2021/10/02 07:35:40 hannken Exp $ */
/* $NetBSD: sys_pipe.c,v 1.158 2021/10/11 01:07:36 thorpej Exp $ */
/*-
* Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@ -68,7 +68,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.157 2021/10/02 07:35:40 hannken Exp $");
__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.158 2021/10/11 01:07:36 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -1056,7 +1056,7 @@ filt_piperead(struct knote *kn, long hint)
if ((rpipe->pipe_state & PIPE_EOF) ||
(wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
kn->kn_flags |= EV_EOF;
knote_set_eof(kn, 0);
rv = 1;
} else {
rv = kn->kn_data > 0;
@ -1082,7 +1082,7 @@ filt_pipewrite(struct knote *kn, long hint)
if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
kn->kn_data = 0;
kn->kn_flags |= EV_EOF;
knote_set_eof(kn, 0);
rv = 1;
} else {
kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;

View File

@ -1,4 +1,4 @@
/* $NetBSD: tty_pty.c,v 1.148 2021/09/29 13:14:39 thorpej Exp $ */
/* $NetBSD: tty_pty.c,v 1.149 2021/10/11 01:07:36 thorpej Exp $ */
/*
* Copyright (c) 1982, 1986, 1989, 1993
@ -37,7 +37,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.148 2021/09/29 13:14:39 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.149 2021/10/11 01:07:36 thorpej Exp $");
#include "opt_ptm.h"
@ -939,7 +939,7 @@ filt_ptcread(struct knote *kn, long hint)
kn->kn_data++;
}
if (!ISSET(tp->t_state, TS_CARR_ON)) {
kn->kn_flags |= EV_EOF;
knote_set_eof(kn, 0);
canread = 1;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: uipc_socket.c,v 1.298 2021/09/29 13:15:45 thorpej Exp $ */
/* $NetBSD: uipc_socket.c,v 1.299 2021/10/11 01:07:36 thorpej Exp $ */
/*
* Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@ -71,7 +71,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.298 2021/09/29 13:15:45 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.299 2021/10/11 01:07:36 thorpej Exp $");
#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
@ -2242,7 +2242,7 @@ filt_soread(struct knote *kn, long hint)
solock(so);
kn->kn_data = so->so_rcv.sb_cc;
if (so->so_state & SS_CANTRCVMORE) {
kn->kn_flags |= EV_EOF;
knote_set_eof(kn, 0);
kn->kn_fflags = so->so_error;
rv = 1;
} else if (so->so_error || so->so_rerror)
@ -2280,7 +2280,7 @@ filt_sowrite(struct knote *kn, long hint)
solock(so);
kn->kn_data = sbspace(&so->so_snd);
if (so->so_state & SS_CANTSENDMORE) {
kn->kn_flags |= EV_EOF;
knote_set_eof(kn, 0);
kn->kn_fflags = so->so_error;
rv = 1;
} else if (so->so_error)

View File

@ -1,4 +1,4 @@
/* $NetBSD: fifo_vnops.c,v 1.90 2021/10/02 18:39:15 thorpej Exp $ */
/* $NetBSD: fifo_vnops.c,v 1.91 2021/10/11 01:07:36 thorpej Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -58,7 +58,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.90 2021/10/02 18:39:15 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.91 2021/10/11 01:07:36 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -608,10 +608,10 @@ filt_fiforead(struct knote *kn, long hint)
solock(so);
kn->kn_data = so->so_rcv.sb_cc;
if (so->so_state & SS_CANTRCVMORE) {
kn->kn_flags |= EV_EOF;
knote_set_eof(kn, 0);
rv = 1;
} else {
kn->kn_flags &= ~EV_EOF;
knote_clear_eof(kn);
rv = (kn->kn_data >= so->so_rcv.sb_lowat);
}
if (hint != NOTE_SUBMIT)
@ -642,10 +642,10 @@ filt_fifowrite(struct knote *kn, long hint)
solock(so);
kn->kn_data = sbspace(&so->so_snd);
if (so->so_state & SS_CANTSENDMORE) {
kn->kn_flags |= EV_EOF;
knote_set_eof(kn, 0);
rv = 1;
} else {
kn->kn_flags &= ~EV_EOF;
knote_clear_eof(kn);
rv = (kn->kn_data >= so->so_snd.sb_lowat);
}
if (hint != NOTE_SUBMIT)

View File

@ -1,4 +1,4 @@
/* $NetBSD: genfs_vnops.c,v 1.213 2021/10/10 23:46:23 thorpej Exp $ */
/* $NetBSD: genfs_vnops.c,v 1.214 2021/10/11 01:07:36 thorpej Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -57,7 +57,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.213 2021/10/10 23:46:23 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.214 2021/10/11 01:07:36 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -68,7 +68,6 @@ __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.213 2021/10/10 23:46:23 thorpej Ex
#include <sys/namei.h>
#include <sys/vnode_impl.h>
#include <sys/fcntl.h>
#include <sys/eventvar.h> /* XXX for kq->kq_lock */
#include <sys/kmem.h>
#include <sys/poll.h>
#include <sys/mman.h>
@ -526,9 +525,7 @@ filt_genfsread(struct knote *kn, long hint)
switch (hint) {
case NOTE_REVOKE:
KASSERT(mutex_owned(vp->v_interlock));
mutex_spin_enter(&kn->kn_kq->kq_lock);
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
mutex_spin_exit(&kn->kn_kq->kq_lock);
knote_set_eof(kn, EV_ONESHOT);
return (1);
case 0:
mutex_enter(vp->v_interlock);
@ -555,9 +552,7 @@ filt_genfswrite(struct knote *kn, long hint)
switch (hint) {
case NOTE_REVOKE:
KASSERT(mutex_owned(vp->v_interlock));
mutex_spin_enter(&kn->kn_kq->kq_lock);
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
mutex_spin_exit(&kn->kn_kq->kq_lock);
knote_set_eof(kn, EV_ONESHOT);
return (1);
case 0:
mutex_enter(vp->v_interlock);
@ -580,9 +575,7 @@ filt_genfsvnode(struct knote *kn, long hint)
switch (hint) {
case NOTE_REVOKE:
KASSERT(mutex_owned(vp->v_interlock));
mutex_spin_enter(&kn->kn_kq->kq_lock);
kn->kn_flags |= EV_EOF;
mutex_spin_exit(&kn->kn_kq->kq_lock);
knote_set_eof(kn, 0);
if ((kn->kn_sfflags & hint) != 0)
kn->kn_fflags |= hint;
return (1);

View File

@ -1,4 +1,4 @@
/* $NetBSD: nfs_kq.c,v 1.29 2021/10/10 23:46:22 thorpej Exp $ */
/* $NetBSD: nfs_kq.c,v 1.30 2021/10/11 01:07:36 thorpej Exp $ */
/*-
* Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.29 2021/10/10 23:46:22 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.30 2021/10/11 01:07:36 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -43,7 +43,6 @@ __KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.29 2021/10/10 23:46:22 thorpej Exp $");
#include <sys/vnode.h>
#include <sys/unistd.h>
#include <sys/file.h>
#include <sys/eventvar.h> /* XXX for kq->kq_lock */
#include <sys/kthread.h>
#include <nfs/rpcv2.h>
@ -232,9 +231,7 @@ filt_nfsread(struct knote *kn, long hint)
switch (hint) {
case NOTE_REVOKE:
KASSERT(mutex_owned(vp->v_interlock));
mutex_spin_enter(&kn->kn_kq->kq_lock);
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
mutex_spin_exit(&kn->kn_kq->kq_lock);
knote_set_eof(kn, EV_ONESHOT);
return (1);
case 0:
mutex_enter(vp->v_interlock);
@ -258,9 +255,7 @@ filt_nfsvnode(struct knote *kn, long hint)
switch (hint) {
case NOTE_REVOKE:
KASSERT(mutex_owned(vp->v_interlock));
mutex_spin_enter(&kn->kn_kq->kq_lock);
kn->kn_flags |= EV_EOF;
mutex_spin_exit(&kn->kn_kq->kq_lock);
knote_set_eof(kn, 0);
if ((kn->kn_sfflags & hint) != 0)
kn->kn_fflags |= hint;
return (1);

View File

@ -1,4 +1,4 @@
/* $NetBSD: event.h,v 1.45 2021/10/10 23:30:44 thorpej Exp $ */
/* $NetBSD: event.h,v 1.46 2021/10/11 01:07:36 thorpej Exp $ */
/*-
* Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
@ -282,6 +282,8 @@ struct timespec;
void kqueue_init(void);
void knote(struct klist *, long);
void knote_fdclose(int);
void knote_set_eof(struct knote *, uint32_t);
void knote_clear_eof(struct knote *);
typedef int (*kevent_fetch_changes_t)(void *, const struct kevent *,
struct kevent *, size_t, int);

View File

@ -1,4 +1,4 @@
/* $NetBSD: param.h,v 1.703 2021/09/30 04:36:25 yamaguchi Exp $ */
/* $NetBSD: param.h,v 1.704 2021/10/11 01:07:36 thorpej Exp $ */
/*-
* Copyright (c) 1982, 1986, 1989, 1993
@ -67,7 +67,7 @@
* 2.99.9 (299000900)
*/
#define __NetBSD_Version__ 999009000 /* NetBSD 9.99.90 */
#define __NetBSD_Version__ 999009100 /* NetBSD 9.99.91 */
#define __NetBSD_Prereq__(M,m,p) (((((M) * 100000000) + \
(m) * 1000000) + (p) * 100) <= __NetBSD_Version__)