From owner-dev-commits-src-main@freebsd.org Sun Aug 8 22:29:57 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 0A2976697BD; Sun, 8 Aug 2021 22:29:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4GjYn06sqBz4b2G; Sun, 8 Aug 2021 22:29:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id CF55D1CD4B; Sun, 8 Aug 2021 22:29:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 178MTuoK047591; Sun, 8 Aug 2021 22:29:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 178MTutC047590; Sun, 8 Aug 2021 22:29:56 GMT (envelope-from git) Date: Sun, 8 Aug 2021 22:29:56 GMT Message-Id: <202108082229.178MTutC047590@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Alexander Motin Subject: git: e5018628e76a - main - kbdmux(4): Make callout handler mpsafe. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mav X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: e5018628e76a27e0f61ca03e2aa2247b3c62a158 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 08 Aug 2021 22:29:57 -0000 The branch main has been updated by mav: URL: https://cgit.FreeBSD.org/src/commit/?id=e5018628e76a27e0f61ca03e2aa2247b3c62a158 commit e5018628e76a27e0f61ca03e2aa2247b3c62a158 Author: Alexander Motin AuthorDate: 2021-08-08 22:19:08 +0000 Commit: Alexander Motin CommitDate: 2021-08-08 22:29:48 +0000 kbdmux(4): Make callout handler mpsafe. Both callout and taskqueue now have drain() routines not requiring external locking. It allows to remove TASK flag and manual drain, so the only thing remaining for lock to protect inside the callout handler is ks_inq_length zero comparison, that can be lockless. MFC after: 2 weeks --- sys/dev/kbdmux/kbdmux.c | 61 ++++++++----------------------------------------- 1 file changed, 10 insertions(+), 51 deletions(-) diff --git a/sys/dev/kbdmux/kbdmux.c b/sys/dev/kbdmux/kbdmux.c index 409e52c21716..3bbdc78b7c1a 100644 --- a/sys/dev/kbdmux/kbdmux.c +++ b/sys/dev/kbdmux/kbdmux.c @@ -105,12 +105,6 @@ MALLOC_DEFINE(M_KBDMUX, KEYBOARD_NAME, "Keyboard multiplexor"); mtx_unlock(&(s)->ks_lock) #define KBDMUX_LOCK_ASSERT(s, w) \ mtx_assert(&(s)->ks_lock, (w)) -#define KBDMUX_SLEEP(s, f, d, t) \ - msleep(&(s)->f, &(s)->ks_lock, PCATCH | (PZERO + 1), (d), (t)) -#define KBDMUX_CALLOUT_INIT(s) \ - callout_init_mtx(&(s)->ks_timo, &(s)->ks_lock, 0) -#define KBDMUX_QUEUE_INTR(s) \ - taskqueue_enqueue(taskqueue_swi_giant, &(s)->ks_task) #else #define KBDMUX_LOCK_DECL_GLOBAL @@ -124,12 +118,6 @@ MALLOC_DEFINE(M_KBDMUX, KEYBOARD_NAME, "Keyboard multiplexor"); #define KBDMUX_LOCK_ASSERT(s, w) -#define KBDMUX_SLEEP(s, f, d, t) \ - tsleep(&(s)->f, PCATCH | (PZERO + 1), (d), (t)) -#define KBDMUX_CALLOUT_INIT(s) \ - callout_init(&(s)->ks_timo, 0) -#define KBDMUX_QUEUE_INTR(s) \ - taskqueue_enqueue(taskqueue_swi_giant, &(s)->ks_task) #endif /* not yet */ /* @@ -157,7 +145,6 @@ struct kbdmux_state int ks_flags; /* flags */ #define COMPOSE (1 << 0) /* compose char flag */ -#define TASK (1 << 2) /* interrupt task queued */ int ks_polling; /* poll nesting count */ int ks_mode; /* K_XLATE, K_RAW, K_CODE */ @@ -223,16 +210,8 @@ void kbdmux_kbd_intr(void *xkbd, int pending) { keyboard_t *kbd = (keyboard_t *) xkbd; - kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; kbdd_intr(kbd, NULL); - - KBDMUX_LOCK(state); - - state->ks_flags &= ~TASK; - wakeup(&state->ks_task); - - KBDMUX_UNLOCK(state); } /* @@ -243,23 +222,12 @@ kbdmux_kbd_intr_timo(void *xstate) { kbdmux_state_t *state = (kbdmux_state_t *) xstate; - KBDMUX_LOCK_ASSERT(state, MA_OWNED); - - if (callout_pending(&state->ks_timo)) - return; /* callout was reset */ - - if (!callout_active(&state->ks_timo)) - return; /* callout was stopped */ - - callout_deactivate(&state->ks_timo); - /* queue interrupt task if needed */ - if (state->ks_inq_length > 0 && !(state->ks_flags & TASK) && - KBDMUX_QUEUE_INTR(state) == 0) - state->ks_flags |= TASK; + if (state->ks_inq_length > 0) + taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task); /* re-schedule timeout */ - callout_reset(&state->ks_timo, TICKS, kbdmux_kbd_intr_timo, state); + callout_schedule(&state->ks_timo, TICKS); } /* @@ -299,9 +267,8 @@ kbdmux_kbd_event(keyboard_t *kbd, int event, void *arg) } /* queue interrupt task if needed */ - if (state->ks_inq_length > 0 && !(state->ks_flags & TASK) && - KBDMUX_QUEUE_INTR(state) == 0) - state->ks_flags |= TASK; + if (state->ks_inq_length > 0) + taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task); KBDMUX_UNLOCK(state); } break; @@ -444,7 +411,7 @@ kbdmux_init(int unit, keyboard_t **kbdp, void *arg, int flags) KBDMUX_LOCK_INIT(state); TASK_INIT(&state->ks_task, 0, kbdmux_kbd_intr, (void *) kbd); - KBDMUX_CALLOUT_INIT(state); + callout_init(&state->ks_timo, 1); SLIST_INIT(&state->ks_kbds); } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { return (0); @@ -521,9 +488,7 @@ kbdmux_init(int unit, keyboard_t **kbdp, void *arg, int flags) KBD_CONFIG_DONE(kbd); - KBDMUX_LOCK(state); callout_reset(&state->ks_timo, TICKS, kbdmux_kbd_intr_timo, state); - KBDMUX_UNLOCK(state); } return (0); @@ -555,16 +520,8 @@ kbdmux_term(keyboard_t *kbd) kbdmux_state_t *state = (kbdmux_state_t *) kbd->kb_data; kbdmux_kbd_t *k; - KBDMUX_LOCK(state); - - /* kill callout */ - callout_stop(&state->ks_timo); - - /* wait for interrupt task */ - while (state->ks_flags & TASK) - KBDMUX_SLEEP(state, ks_task, "kbdmuxc", 0); - /* release all keyboards from the mux */ + KBDMUX_LOCK(state); while ((k = SLIST_FIRST(&state->ks_kbds)) != NULL) { kbd_release(k->kbd, &k->kbd); SLIST_REMOVE_HEAD(&state->ks_kbds, next); @@ -573,9 +530,11 @@ kbdmux_term(keyboard_t *kbd) free(k, M_KBDMUX); } - KBDMUX_UNLOCK(state); + callout_drain(&state->ks_timo); + taskqueue_drain(taskqueue_swi_giant, &state->ks_task); + kbd_unregister(kbd); #ifdef EVDEV_SUPPORT