From nobody Thu Feb 09 16:02:46 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PCM8R0WPRz3pkb2; Thu, 9 Feb 2023 16:02:47 +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 4PCM8Q5Fdfz4F2k; Thu, 9 Feb 2023 16:02:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675958566; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=s1644utmbXqaRgBPiQX+iNcNqLz3Afp8uKc7frlcF2w=; b=iDU7GdPExbjey3OvK5tss0cvVPM8JQswqnoIc1oXYak4eJpjQEcQ/auF4rmeR6XS2EAk5E P2IUXEQ4mwxELh6rg7L2QVq+Napp7DBtxsdjO5YqV1neuxMf8Pjr5r+ZSV0G/pZICneSN5 DRAnHUx2mfWeSzydsHCczLIuzcvxlA70PF7+S451RfP5nKl3rYl4hvbMCSpVdPIyuQkYWy tV3h1fnte53YS6aOuHPcsFQCC5aH+chQgvniCX5NLRQ6HKRMhZvGPK60I+Y/fNVpeXXuXb P8T2dqm6ZRkau1Wc5cFt77/TsbrKfW7ljvPl7JWyDT+nBY83xhx4ZrEZ7ZK4dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675958566; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=s1644utmbXqaRgBPiQX+iNcNqLz3Afp8uKc7frlcF2w=; b=T+4YD8LmHGzDrk4Rkfe5w7/CJGWJZe09WQknCFX56RtsFWAYVdiSf+L2RRnATNM1L0+4DD j/6nYTJ/2R0YcjEgT/O0XpgVKJvWjqz9nSxaOAC6Hpw1523T15fZedEFdgiGlYUyH2m2pm iT/GVHWH8FpbkRqj7vYutMwRyXtyux/TXTo3D2MzvAaLkcTo/5DZAZY+DvNSKKtP1Z3RNg 2OAhm5kLIrvq7FC+EAMvPbjG/63K6TQkfLoza54c5hCFKHMe5KuFDf8c6uhlSwdX08OefI VnUsjCoVmqwTa4aF9K9EZmqRdEfWQKtWPqmIhqcfzQXQbEdU4PEXEk6woL+fkw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1675958566; a=rsa-sha256; cv=none; b=rHxksZfF4LSHsDO1fNQmHvHAKvQokBq1IzUlCIwor3EWyR3BRlLvVqfkPRLKtYfEZ4wsqt nDtdpC7sWXrcbvFLTZwqfX1FKVOeLXiC7k/8dWnlq6R+9h/2Q8unXF4ftZ8Nlj5aGN2R1z EwwR8B3nwKJXJmp6HAmyVqndYF392IgQ1IcmRfL6G4fZrpilJYzyeVCojjVr72AojPa9Cn QL3qvviLmXPM7/eDy5sGYtJ7Eq4kTYr4GJQYgLBWbOq8BoGbZc+Nyg6eL3NmvKvsOV5I0C 9DgCNJWGp1qLiBG2r59R5uNUAK8l/wWPSER8/frCTvPMlLdyDfTUvZ329az0Rw== 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 4PCM8Q4Lyqzjjm; Thu, 9 Feb 2023 16:02:46 +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 319G2k5J094745; Thu, 9 Feb 2023 16:02:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 319G2kuC094744; Thu, 9 Feb 2023 16:02:46 GMT (envelope-from git) Date: Thu, 9 Feb 2023 16:02:46 GMT Message-Id: <202302091602.319G2kuC094744@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: 1029dab63457 - main - mi_switch(): clean up switch types and their usage List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1029dab63457f23dace4dab3b698f1c177939b93 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=1029dab63457f23dace4dab3b698f1c177939b93 commit 1029dab63457f23dace4dab3b698f1c177939b93 Author: Mitchell Horne AuthorDate: 2023-02-09 15:40:29 +0000 Commit: Mitchell Horne CommitDate: 2023-02-09 16:01:32 +0000 mi_switch(): clean up switch types and their usage Overall, this is a non-functional change, except for kernels built with SCHED_STATS. However, the switch types are useful for communicating the intent of the caller. 1. Ensure that every caller provides a type. In most cases, we upgrade the basic yield to sched_relinquish() aka SWT_RELINQUISH. 2. The case of sched_bind() is distinct, so add a new switch type SWT_BIND. 3. Remove the two unused types, SWT_PREEMPT and SWT_SLEEPQTIMO. 4. Remove SWT_NONE altogether and assert that callers always provide a type flag. 5. Reference the mi_switch(9) man page in the comments, as these flags will be documented there. Reviewed by: kib, markj Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D38184 --- sys/dev/dpaa/bman_portals.c | 3 +-- sys/dev/dpaa/qman_portals.c | 3 +-- sys/kern/kern_poll.c | 3 +-- sys/kern/kern_switch.c | 12 ++++-------- sys/kern/kern_synch.c | 9 +++++++-- sys/kern/sched_4bsd.c | 2 +- sys/kern/sched_ule.c | 2 +- sys/kern/vfs_bio.c | 4 ++-- sys/sys/proc.h | 28 +++++++++++++--------------- 9 files changed, 31 insertions(+), 35 deletions(-) diff --git a/sys/dev/dpaa/bman_portals.c b/sys/dev/dpaa/bman_portals.c index 9ef2bf7aa61c..d3c027bb29f8 100644 --- a/sys/dev/dpaa/bman_portals.c +++ b/sys/dev/dpaa/bman_portals.c @@ -141,8 +141,7 @@ bman_portal_setup(struct bman_softc *bsc) } /* Not inititialized and "owned" by another thread */ - thread_lock(curthread); - mi_switch(SW_VOL); + sched_relinquish(curthread); } /* Map portal registers */ diff --git a/sys/dev/dpaa/qman_portals.c b/sys/dev/dpaa/qman_portals.c index 92461665def2..37f9b4eec178 100644 --- a/sys/dev/dpaa/qman_portals.c +++ b/sys/dev/dpaa/qman_portals.c @@ -146,8 +146,7 @@ qman_portal_setup(struct qman_softc *qsc) } /* Not inititialized and "owned" by another thread */ - thread_lock(curthread); - mi_switch(SW_VOL); + sched_relinquish(curthread); } /* Map portal registers */ diff --git a/sys/kern/kern_poll.c b/sys/kern/kern_poll.c index 8232f98f59ef..64deb7132710 100644 --- a/sys/kern/kern_poll.c +++ b/sys/kern/kern_poll.c @@ -565,8 +565,7 @@ poll_idle(void) if (poll_in_idle_loop && poll_handlers > 0) { idlepoll_sleeping = 0; ether_poll(poll_each_burst); - thread_lock(td); - mi_switch(SW_VOL); + sched_relinquish(td); } else { idlepoll_sleeping = 1; tsleep(&idlepoll_sleeping, 0, "pollid", hz * 3); diff --git a/sys/kern/kern_switch.c b/sys/kern/kern_switch.c index 9d48ab188e13..aee0c9cbd1ae 100644 --- a/sys/kern/kern_switch.c +++ b/sys/kern/kern_switch.c @@ -83,25 +83,19 @@ SYSCTL_INT(_kern_sched, OID_AUTO, preemption, CTLFLAG_RD, SYSCTL_NODE(_kern_sched, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, "switch stats"); -/* Switch reasons from mi_switch(). */ +/* Switch reasons from mi_switch(9). */ DPCPU_DEFINE(long, sched_switch_stats[SWT_COUNT]); -SCHED_STAT_DEFINE_VAR(uncategorized, - &DPCPU_NAME(sched_switch_stats[SWT_NONE]), ""); -SCHED_STAT_DEFINE_VAR(preempt, - &DPCPU_NAME(sched_switch_stats[SWT_PREEMPT]), ""); SCHED_STAT_DEFINE_VAR(owepreempt, &DPCPU_NAME(sched_switch_stats[SWT_OWEPREEMPT]), ""); SCHED_STAT_DEFINE_VAR(turnstile, &DPCPU_NAME(sched_switch_stats[SWT_TURNSTILE]), ""); SCHED_STAT_DEFINE_VAR(sleepq, &DPCPU_NAME(sched_switch_stats[SWT_SLEEPQ]), ""); -SCHED_STAT_DEFINE_VAR(sleepqtimo, - &DPCPU_NAME(sched_switch_stats[SWT_SLEEPQTIMO]), ""); SCHED_STAT_DEFINE_VAR(relinquish, &DPCPU_NAME(sched_switch_stats[SWT_RELINQUISH]), ""); SCHED_STAT_DEFINE_VAR(needresched, &DPCPU_NAME(sched_switch_stats[SWT_NEEDRESCHED]), ""); -SCHED_STAT_DEFINE_VAR(idle, +SCHED_STAT_DEFINE_VAR(idle, &DPCPU_NAME(sched_switch_stats[SWT_IDLE]), ""); SCHED_STAT_DEFINE_VAR(iwait, &DPCPU_NAME(sched_switch_stats[SWT_IWAIT]), ""); @@ -111,6 +105,8 @@ SCHED_STAT_DEFINE_VAR(remotepreempt, &DPCPU_NAME(sched_switch_stats[SWT_REMOTEPREEMPT]), ""); SCHED_STAT_DEFINE_VAR(remotewakeidle, &DPCPU_NAME(sched_switch_stats[SWT_REMOTEWAKEIDLE]), ""); +SCHED_STAT_DEFINE_VAR(bind, + &DPCPU_NAME(sched_switch_stats[SWT_BIND]), ""); static int sysctl_stats_reset(SYSCTL_HANDLER_ARGS) diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c index 7d0cb6178fc3..63a78353e9a5 100644 --- a/sys/kern/kern_synch.c +++ b/sys/kern/kern_synch.c @@ -483,7 +483,7 @@ kdb_switch(void) } /* - * The machine independent parts of context switching. + * mi_switch(9): The machine-independent parts of context switching. * * The thread lock is required on entry and is no longer held on return. */ @@ -500,10 +500,15 @@ mi_switch(int flags) if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td)) mtx_assert(&Giant, MA_NOTOWNED); #endif + /* thread_lock() performs spinlock_enter(). */ KASSERT(td->td_critnest == 1 || KERNEL_PANICKED(), - ("mi_switch: switch in a critical section")); + ("mi_switch: switch in a critical section")); KASSERT((flags & (SW_INVOL | SW_VOL)) != 0, ("mi_switch: switch must be voluntary or involuntary")); + KASSERT((flags & SW_TYPE_MASK) != 0, + ("mi_switch: a switch reason (type) must be specified")); + KASSERT((flags & SW_TYPE_MASK) < SWT_COUNT, + ("mi_switch: invalid switch reason %d", (flags & SW_TYPE_MASK))); /* * Don't perform context switches from the debugger. diff --git a/sys/kern/sched_4bsd.c b/sys/kern/sched_4bsd.c index f0da4f8d8496..e81b0c9d46ec 100644 --- a/sys/kern/sched_4bsd.c +++ b/sys/kern/sched_4bsd.c @@ -1582,7 +1582,7 @@ sched_bind(struct thread *td, int cpu) if (PCPU_GET(cpuid) == cpu) return; - mi_switch(SW_VOL); + mi_switch(SW_VOL | SWT_BIND); thread_lock(td); #endif } diff --git a/sys/kern/sched_ule.c b/sys/kern/sched_ule.c index ba179caf5d34..881413ca5e73 100644 --- a/sys/kern/sched_ule.c +++ b/sys/kern/sched_ule.c @@ -2922,7 +2922,7 @@ sched_bind(struct thread *td, int cpu) return; ts->ts_cpu = cpu; /* When we return from mi_switch we'll be on the correct cpu. */ - mi_switch(SW_VOL); + mi_switch(SW_VOL | SWT_BIND); thread_lock(td); } diff --git a/sys/kern/vfs_bio.c b/sys/kern/vfs_bio.c index cb94f3390d8a..3a5afec88a02 100644 --- a/sys/kern/vfs_bio.c +++ b/sys/kern/vfs_bio.c @@ -72,6 +72,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -1425,8 +1426,7 @@ bufshutdown(int show_busybufs) * threads to run. */ for (subiter = 0; subiter < 50 * iter; subiter++) { - thread_lock(curthread); - mi_switch(SW_VOL); + sched_relinquish(curthread); DELAY(1000); } #endif diff --git a/sys/sys/proc.h b/sys/sys/proc.h index d031c2a73ce6..c6670bdc96f8 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -904,22 +904,20 @@ struct proc { #ifdef _KERNEL -/* Types and flags for mi_switch(). */ +/* Types and flags for mi_switch(9). */ #define SW_TYPE_MASK 0xff /* First 8 bits are switch type */ -#define SWT_NONE 0 /* Unspecified switch. */ -#define SWT_PREEMPT 1 /* Switching due to preemption. */ -#define SWT_OWEPREEMPT 2 /* Switching due to owepreempt. */ -#define SWT_TURNSTILE 3 /* Turnstile contention. */ -#define SWT_SLEEPQ 4 /* Sleepq wait. */ -#define SWT_SLEEPQTIMO 5 /* Sleepq timeout wait. */ -#define SWT_RELINQUISH 6 /* yield call. */ -#define SWT_NEEDRESCHED 7 /* NEEDRESCHED was set. */ -#define SWT_IDLE 8 /* Switching from the idle thread. */ -#define SWT_IWAIT 9 /* Waiting for interrupts. */ -#define SWT_SUSPEND 10 /* Thread suspended. */ -#define SWT_REMOTEPREEMPT 11 /* Remote processor preempted. */ -#define SWT_REMOTEWAKEIDLE 12 /* Remote processor preempted idle. */ -#define SWT_COUNT 13 /* Number of switch types. */ +#define SWT_OWEPREEMPT 1 /* Switching due to owepreempt. */ +#define SWT_TURNSTILE 2 /* Turnstile contention. */ +#define SWT_SLEEPQ 3 /* Sleepq wait. */ +#define SWT_RELINQUISH 4 /* yield call. */ +#define SWT_NEEDRESCHED 5 /* NEEDRESCHED was set. */ +#define SWT_IDLE 6 /* Switching from the idle thread. */ +#define SWT_IWAIT 7 /* Waiting for interrupts. */ +#define SWT_SUSPEND 8 /* Thread suspended. */ +#define SWT_REMOTEPREEMPT 9 /* Remote processor preempted. */ +#define SWT_REMOTEWAKEIDLE 10 /* Remote processor preempted idle. */ +#define SWT_BIND 11 /* Thread bound to a new CPU. */ +#define SWT_COUNT 12 /* Number of switch types. */ /* Flags */ #define SW_VOL 0x0100 /* Voluntary switch. */ #define SW_INVOL 0x0200 /* Involuntary switch. */