From nobody Tue Nov 07 11:23:03 2023 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 4SPm6b2wRfz4ygn5; Tue, 7 Nov 2023 11:23:03 +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 4SPm6b2Pl6z3cTQ; Tue, 7 Nov 2023 11:23:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1699356183; 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=It/rShlp98Z7/iPDBfFjq02FQuZmPPl+F3zBuj0j43s=; b=E2XokKQAIdhuDg4bHyihR+JLtMMGPAkie4VY8n6YrwIkcto2O7qzgoFc/x+zYrLS2FZWFi CMDpGbNx47sIJnrBHicIeHc9FjhZ9I1R1ya/JqBax5cB8zXcxBm4319ORfMysLhnNjVwjg wixd7C7x2zasqTHZ7e+t9ZnWoWUB1ANDXKvbX09mbjEwKYAo8Hu/0/LGIEhZPGnXktOFcH wiH8G4/J8Gq/lCTAJY6cGAeoY4se6NM52xEYaI8v/vQGjmo6XE6L7lPvQZfuRUaeZ+JnCf ySzJvmjQn32THmUrwvXX5LSDbcwVGsmaXiuVVRA6ZiPYdg1++F2xcaRTKdoHYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1699356183; 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=It/rShlp98Z7/iPDBfFjq02FQuZmPPl+F3zBuj0j43s=; b=lssrIP8CrVhxJO/YP9pGBMg+rr/qA+C4rksI2LWVVGtNICEnXnZOsYljHQeQZ6tomHgh/m mNW9SEY2PKaTn/wUvZelIJT0MBKBv/Z5UNwaIAl2rfOSLh8njtZ3CJHf9RgzNUILnRLkzO wSNfQwQjsO3wrtJjkKwO+Y1r2rm+FDjlDIEV6WHK5lbinxmmjHWXJdZMA3Bu3/69L2uk1E TjyFVc3zJx0QvHv71MUsZYYieVwDMFYmbLiQ42cBtVXVwXndCodbRVTZQuJu9zzRwm4oBv nkxn9uQuH56dxPYJfmQboaK1f64xHyPHniohzYuxA2lFz3gkj7ciphgWtJrW9w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1699356183; a=rsa-sha256; cv=none; b=ZWhScQgqpA5hzFWwbFV34POQbUXS/nH6UCWds7LhXgT3ui48Y0+eDZXlkarOtT/aGWk2RP dr0vMH4CelvbOjrz82j2O/JnSTNrKHvmqc8YgxqPB+iSvrUxfLU5xX45r8mAUN1eqaDyuo REDg0lYVY9Vh0qsLJU/YlM8z24ObstQQwHqsRBQQGs0TzTFkxUShoVl11mZnKizkMq7xSe dHQuMaXXL/0g18UqTlnBsltegsicuHTdE8xt+/H7Tl+wo8C3XH52YlWWp0BCmgt+ErLYle 2UMmz5z9y10OfWHred82O4IBG6Qrd518CvKfts+vEkNBQk+QL1fwWCoE4Olh2w== 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 4SPm6b1SlzzpK6; Tue, 7 Nov 2023 11:23:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3A7BN3qe034090; Tue, 7 Nov 2023 11:23:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3A7BN3DI034087; Tue, 7 Nov 2023 11:23:03 GMT (envelope-from git) Date: Tue, 7 Nov 2023 11:23:03 GMT Message-Id: <202311071123.3A7BN3DI034087@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 05fe82455f26 - main - linuxkpi: races between linux_queue_delayed_work_on() and linux_cancel_delayed_work_sync() 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: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 05fe82455f263ad107a860ce20dd89e1a5c1619c Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=05fe82455f263ad107a860ce20dd89e1a5c1619c commit 05fe82455f263ad107a860ce20dd89e1a5c1619c Author: Konstantin Belousov AuthorDate: 2023-11-04 07:45:48 +0000 Commit: Konstantin Belousov CommitDate: 2023-11-07 10:58:04 +0000 linuxkpi: races between linux_queue_delayed_work_on() and linux_cancel_delayed_work_sync() 1. Suppose that linux_queue_delayed_work_on() is called with non-zero delay and found the work.state WORK_ST_IDLE. It resets the state to WORK_ST_TIMER and locks timer.mtx. Now, if linux_cancel_delayed_work_sync() was also called meantime, read state as WORK_ST_TIMER and already taken the mutex, it is executing callout_stop() on non-armed callout. Then linux_queue_delayed_work_on() continues and schedules callout. But the return value from cancel() is false, making it possible to the requeue from callback to slip in. 2. If linux_cancel_delayed_work_sync() returned true, we need to cancel again. The requeue from callback could have revived the work. The end result is that we schedule callout that might be freed, since cancel_delayed_work_sync() claims that everything was stopped. This contradicts the way the KPI is used in Linux, where consumers expect that cancel_delayed_work_sync() is reliable on its own. Reviewed by: markj Discussed with: bz Sponsored by: NVidia networking MFC after: 1 week Differential revision: https://reviews.freebsd.org/D42468 --- sys/compat/linuxkpi/common/src/linux_work.c | 36 ++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/sys/compat/linuxkpi/common/src/linux_work.c b/sys/compat/linuxkpi/common/src/linux_work.c index 990ba5d20fd5..888ac97dbff6 100644 --- a/sys/compat/linuxkpi/common/src/linux_work.c +++ b/sys/compat/linuxkpi/common/src/linux_work.c @@ -221,16 +221,19 @@ linux_queue_delayed_work_on(int cpu, struct workqueue_struct *wq, [WORK_ST_EXEC] = WORK_ST_TIMER, /* start timeout */ [WORK_ST_CANCEL] = WORK_ST_TIMER, /* start timeout */ }; + bool res; if (atomic_read(&wq->draining) != 0) return (!work_pending(&dwork->work)); + mtx_lock(&dwork->timer.mtx); switch (linux_update_state(&dwork->work.state, states)) { case WORK_ST_EXEC: case WORK_ST_CANCEL: if (delay == 0 && linux_work_exec_unblock(&dwork->work) != 0) { dwork->timer.expires = jiffies; - return (true); + res = true; + goto out; } /* FALLTHROUGH */ case WORK_ST_IDLE: @@ -240,20 +243,21 @@ linux_queue_delayed_work_on(int cpu, struct workqueue_struct *wq, if (delay == 0) { linux_delayed_work_enqueue(dwork); } else if (unlikely(cpu != WORK_CPU_UNBOUND)) { - mtx_lock(&dwork->timer.mtx); callout_reset_on(&dwork->timer.callout, delay, &linux_delayed_work_timer_fn, dwork, cpu); - mtx_unlock(&dwork->timer.mtx); } else { - mtx_lock(&dwork->timer.mtx); callout_reset(&dwork->timer.callout, delay, &linux_delayed_work_timer_fn, dwork); - mtx_unlock(&dwork->timer.mtx); } - return (true); + res = true; + break; default: - return (false); /* already on a queue */ + res = false; + break; } +out: + mtx_unlock(&dwork->timer.mtx); + return (res); } void @@ -467,8 +471,8 @@ linux_cancel_delayed_work(struct delayed_work *dwork) * fashion. It returns non-zero if the work was successfully * cancelled. Else the work was already cancelled. */ -bool -linux_cancel_delayed_work_sync(struct delayed_work *dwork) +static bool +linux_cancel_delayed_work_sync_int(struct delayed_work *dwork) { static const uint8_t states[WORK_ST_MAX] __aligned(8) = { [WORK_ST_IDLE] = WORK_ST_IDLE, /* NOP */ @@ -478,7 +482,6 @@ linux_cancel_delayed_work_sync(struct delayed_work *dwork) [WORK_ST_CANCEL] = WORK_ST_IDLE, /* cancel and drain */ }; struct taskqueue *tq; - bool retval = false; int ret, state; bool cancelled; @@ -490,7 +493,7 @@ linux_cancel_delayed_work_sync(struct delayed_work *dwork) switch (state) { case WORK_ST_IDLE: mtx_unlock(&dwork->timer.mtx); - return (retval); + return (false); case WORK_ST_TIMER: case WORK_ST_CANCEL: cancelled = (callout_stop(&dwork->timer.callout) == 1); @@ -512,6 +515,17 @@ linux_cancel_delayed_work_sync(struct delayed_work *dwork) } } +bool +linux_cancel_delayed_work_sync(struct delayed_work *dwork) +{ + bool res; + + res = false; + while (linux_cancel_delayed_work_sync_int(dwork)) + res = true; + return (res); +} + /* * This function waits until the given work structure is completed. * It returns non-zero if the work was successfully