From nobody Fri Jun 20 02:42:54 2025 X-Original-To: dev-commits-src-main@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 4bNhbg39khz5yLbp; Fri, 20 Jun 2025 02:42:55 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bNhbf6Tcmz3VLc; Fri, 20 Jun 2025 02:42:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750387374; 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=IG1wBTIIAvML9v2xrRC4CB3GNvM/kfyqHeZcs8oMpYs=; b=PGIC8RRXF807Z1GFbnKuzzzn6GcZTMyf9qsx+BeLGTMeBAiY89y2ifJvz0C+JKJ03qYES7 njRHaDnR2QRS3Af4l5gYVuwKfnddCNFmOfBxB3hw9TPYKVCaUKzbD7qVPTLQNYpcaNSAEN bKYFIvn18IpYnZexgxMGRBAQJaYR1gRldGVRZ/pxjKRihQDQYvN3PQFVNP7eQZKTmxFbwz 6mOFUaz3qjb4w45LE+X+bRg+SQ4b/DzXY6PdP2m+KMDDnkmBzpVUCLm1lsST+T2jqBFXq9 Yv4NwTpENVtyKjJHoz9GqACEx0PpzGhc2+aNmeoUTSvUGYBKBYVdsgkvxK9Beg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750387374; 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=IG1wBTIIAvML9v2xrRC4CB3GNvM/kfyqHeZcs8oMpYs=; b=eIMjdk9CXxbwL71A6ZOJ6oWPTtC15Rth4OZgYwQEn7ybFeznYOJ5lO0kGPDJjBKDD7xgN9 YzYopklM8V6aeXB6Ug4TVX6RVo0Ds1NT+ZN2uJnBek5AFTxGIgJpWTSw110hYvHADef+Dl t2ZOXpa5NSzEpp/pCGzpSCyGK5iyVaNN4lwI87ChzDilKu4yWApHfy0n5Fu+GMCA2BL1TF jkCBUL1Vd9GZEArvSTWUrmvSnKbvAeRahzZT/BAK1sTK1bS2ymclTil0qr6GDQjco+14RX 69Ehft+EeV3xBrUnVR+lQBUQp3kuIej3OchGpZxPhp3euTTykjiWLVeWC57iJw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1750387374; a=rsa-sha256; cv=none; b=YsuNMTA4ymukZs2CjL1RYZF5y88TOfaNP6Ai9LxhHO/AIaetuDPYd+chAGo71VwweOyEZi pRc1Uj/cbp2AgwrF11nY/cwHm11iEjT7uzTWHunQibhDIp8xXOv0Ve1B2GZTMjTkk5Pqnc yIwnwzRjJF09m6Ools6H6j8UarLRHYzYXDW6vPLLG1gfgmK3bU5Z7NlcbpYvs2WifQO/IQ npPIMdObwswTVARfiimNssNEuvo9UnGBNDytr/pLzzbgUEByh6unwWyQVOqldfTkrpS3Ge SXo0B5NWO4F71np0efTxphUnHbfQIxdHy8KiG2lMQ377FUUckF7WXJRonwTDuw== 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 4bNhbf5rMjz9rb; Fri, 20 Jun 2025 02:42:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 55K2gsYS093666; Fri, 20 Jun 2025 02:42:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 55K2gsup093663; Fri, 20 Jun 2025 02:42:54 GMT (envelope-from git) Date: Fri, 20 Jun 2025 02:42:54 GMT Message-Id: <202506200242.55K2gsup093663@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Olivier Certner Subject: git: be8e84c2e924 - main - runq: runq_not_empty() to support racy lookups again List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: olce X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: be8e84c2e9240110ce99bb8d14259073340e4ef6 Auto-Submitted: auto-generated The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=be8e84c2e9240110ce99bb8d14259073340e4ef6 commit be8e84c2e9240110ce99bb8d14259073340e4ef6 Author: Olivier Certner AuthorDate: 2025-06-20 02:36:09 +0000 Commit: Olivier Certner CommitDate: 2025-06-20 02:42:16 +0000 runq: runq_not_empty() to support racy lookups again The 4BSD assumes that its sched_runnable(), which then calls runq_not_empty(), can be called racily (another CPU may be grabbing a thread from the global queue at the same time). This makes runq_not_empty() trip on an assertion in runq_find() detecting a mismatch between some bit in the status word and the state of the corresponding queue (empty/not empty). Re-implement runq_not_empty() in an independent and simpler way as an alternative to removing the assertion, which remains useful to detect races when removing or adding a thread in some runqueue. Fixes: 79f68322c892 ("runq: runq_check(): Re-implement on top of runq_findq()") MFC after: 1 month Event: Kitchener-Waterloo Hackathon 202506 Sponsored by: The FreeBSD Foundation --- sys/kern/kern_switch.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/sys/kern/kern_switch.c b/sys/kern/kern_switch.c index 917f73682564..42fee61a99a0 100644 --- a/sys/kern/kern_switch.c +++ b/sys/kern/kern_switch.c @@ -493,7 +493,7 @@ int runq_findq(struct runq *const rq, const int lvl_min, const int lvl_max, runq_pred_t *const pred, void *const pred_data) { - rqsw_t const (*const rqsw)[RQSW_NB] = &rq->rq_status.rq_sw; + const rqsw_t (*const rqsw)[RQSW_NB] = &rq->rq_status.rq_sw; rqsw_t w; int i, last, idx; @@ -568,20 +568,27 @@ runq_first_thread(struct runq *const rq) /* * Return true if there are some processes of any priority on the run queue, - * false otherwise. Has no side effects. + * false otherwise. Has no side effects. Supports racy lookups (required by + * 4BSD). */ bool runq_not_empty(struct runq *rq) { - struct thread *const td = runq_first_thread(rq); + const rqsw_t (*const rqsw)[RQSW_NB] = &rq->rq_status.rq_sw; + int sw_idx; - if (td != NULL) { - CTR2(KTR_RUNQ, "runq_not_empty: idx=%d, td=%p", - td->td_rqindex, td); - return (true); + for (sw_idx = 0; sw_idx < RQSW_NB; ++sw_idx) { + const rqsw_t w = (*rqsw)[sw_idx]; + + if (w != 0) { + CTR3(KTR_RUNQ, "runq_not_empty: not empty; " + "rq=%p, sw_idx=%d, bits=" RQSW_PRI, + rq, sw_idx, w); + return (true); + } } - CTR0(KTR_RUNQ, "runq_not_empty: empty"); + CTR1(KTR_RUNQ, "runq_not_empty: empty; rq=%p", rq); return (false); }