From owner-dev-commits-src-all@freebsd.org Thu Feb 4 18:55:44 2021 Return-Path: Delivered-To: dev-commits-src-all@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 A949452BE1D; Thu, 4 Feb 2021 18:55:44 +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 4DWnnD3W58z4mHn; Thu, 4 Feb 2021 18:55:44 +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 6B0F610F3F; Thu, 4 Feb 2021 18:55:44 +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 114Iti7T020378; Thu, 4 Feb 2021 18:55:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 114Itiod020377; Thu, 4 Feb 2021 18:55:44 GMT (envelope-from git) Date: Thu, 4 Feb 2021 18:55:44 GMT Message-Id: <202102041855.114Itiod020377@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ryan Stone Subject: git: b58cf1cb35ab - main - Fix race condition in linuxkpi workqueue MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rstone X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b58cf1cb35abc095d7fb9773630794b38bbc6b1d Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Feb 2021 18:55:44 -0000 The branch main has been updated by rstone: URL: https://cgit.FreeBSD.org/src/commit/?id=b58cf1cb35abc095d7fb9773630794b38bbc6b1d commit b58cf1cb35abc095d7fb9773630794b38bbc6b1d Author: Ryan Stone AuthorDate: 2021-01-07 17:25:56 +0000 Commit: Ryan Stone CommitDate: 2021-02-04 18:54:53 +0000 Fix race condition in linuxkpi workqueue Consider the following scenario: 1. A delayed_work struct in the WORK_ST_TIMER state. 2. Thread A calls mod_delayed_work() 3. Thread B (a callout thread) simultaneously calls linux_delayed_work_timer_fn() The following sequence of events is possible: A: Call linux_cancel_delayed_work() A: Change state from TIMER TO CANCEL B: Change state from CANCEL to TASK B: taskqueue_enqueue() the task A: taskqueue_cancel() the task A: Call linux_queue_delayed_work_on(). This is a no-op because the state is WORK_ST_TASK. As a result, the delayed_work struct will never be invoked. This is causing address resolution in ib_addr.c to stop permanently, as it never tries to reschedule a task that it thinks is already scheduled. Fix this by introducing locking into the cancel path (which corresponds with the lock held while the callout runs). This will prevent the callout from changing the state of the task until the cancel is complete, preventing the race. Differential Revision: https://reviews.freebsd.org/D28420 Reviewed by: hselasky MFC after: 2 months --- sys/compat/linuxkpi/common/src/linux_work.c | 49 ++++++++++++++++------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/sys/compat/linuxkpi/common/src/linux_work.c b/sys/compat/linuxkpi/common/src/linux_work.c index 45025378baa9..f9cf62928760 100644 --- a/sys/compat/linuxkpi/common/src/linux_work.c +++ b/sys/compat/linuxkpi/common/src/linux_work.c @@ -435,13 +435,17 @@ linux_cancel_delayed_work(struct delayed_work *dwork) [WORK_ST_CANCEL] = WORK_ST_CANCEL, /* NOP */ }; struct taskqueue *tq; + bool cancelled; + mtx_lock(&dwork->timer.mtx); switch (linux_update_state(&dwork->work.state, states)) { case WORK_ST_TIMER: case WORK_ST_CANCEL: - if (linux_cancel_timer(dwork, 0)) { + cancelled = (callout_stop(&dwork->timer.callout) == 1); + if (cancelled) { atomic_cmpxchg(&dwork->work.state, WORK_ST_CANCEL, WORK_ST_IDLE); + mtx_unlock(&dwork->timer.mtx); return (true); } /* FALLTHROUGH */ @@ -450,10 +454,12 @@ linux_cancel_delayed_work(struct delayed_work *dwork) if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) == 0) { atomic_cmpxchg(&dwork->work.state, WORK_ST_CANCEL, WORK_ST_IDLE); + mtx_unlock(&dwork->timer.mtx); return (true); } /* FALLTHROUGH */ default: + mtx_unlock(&dwork->timer.mtx); return (false); } } @@ -475,37 +481,36 @@ linux_cancel_delayed_work_sync(struct delayed_work *dwork) }; struct taskqueue *tq; bool retval = false; + int ret, state; + bool cancelled; WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "linux_cancel_delayed_work_sync() might sleep"); -retry: - switch (linux_update_state(&dwork->work.state, states)) { + mtx_lock(&dwork->timer.mtx); + + state = linux_update_state(&dwork->work.state, states); + switch (state) { case WORK_ST_IDLE: + mtx_unlock(&dwork->timer.mtx); return (retval); - case WORK_ST_EXEC: - tq = dwork->work.work_queue->taskqueue; - if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) != 0) - taskqueue_drain(tq, &dwork->work.work_task); - goto retry; /* work may have restarted itself */ case WORK_ST_TIMER: case WORK_ST_CANCEL: - if (linux_cancel_timer(dwork, 1)) { - /* - * Make sure taskqueue is also drained before - * returning: - */ - tq = dwork->work.work_queue->taskqueue; - taskqueue_drain(tq, &dwork->work.work_task); - retval = true; - goto retry; - } - /* FALLTHROUGH */ + cancelled = (callout_stop(&dwork->timer.callout) == 1); + + tq = dwork->work.work_queue->taskqueue; + ret = taskqueue_cancel(tq, &dwork->work.work_task, NULL); + mtx_unlock(&dwork->timer.mtx); + + callout_drain(&dwork->timer.callout); + taskqueue_drain(tq, &dwork->work.work_task); + return (cancelled || (ret != 0)); default: tq = dwork->work.work_queue->taskqueue; - if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) != 0) + ret = taskqueue_cancel(tq, &dwork->work.work_task, NULL); + mtx_unlock(&dwork->timer.mtx); + if (ret != 0) taskqueue_drain(tq, &dwork->work.work_task); - retval = true; - goto retry; + return (ret != 0); } }