Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3724648pxv; Mon, 28 Jun 2021 11:13:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxwTlD2nWvq7xrtUKY2HaW34qLSyqgfIRYLel7pw1F5qkRLsMzxDls6Fqs5HBY4ZehZ2xno X-Received: by 2002:a02:84a3:: with SMTP id f32mr768832jai.63.1624903993033; Mon, 28 Jun 2021 11:13:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624903993; cv=none; d=google.com; s=arc-20160816; b=XC4cGsxdq6bB6+HnF4ImvalU5xpgWX8eTcaNLbhsFdRkC8gZQezPFWDkMpR30u34TY JEMXO1fXvIzrPs7ToPHbl4qckmzGjIS2ji37l8SDwF7NKg/0dlYc1DOYlhXrnHbdceup 4nSZzDYHRsD4CquE1OMhkP2URqWvUrQlHBevG8DszidwA6Yv7JmiqWsQfB9WiKtRwdqq CF4kBV2IMmdSEdxKpet4ewJJZWkrnijgPAqc/OSgiSgrBA2H0d7dbC6TT8qw0NClCifg 5z/QQnW1/z7aZz9V3+EHGsVvPImrVAQWehFQyo9+3uVnjClI2mdUIl/HphIbTwhYvfPd h7eA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XhFSA+9yXZ1vCqpi6vj+BKTdNPBTSpkukl9zbEyg/zc=; b=RDFb75sUqo833Y2KVkWakbTS0991qE5nOuhVd8cF4A22JMYWQhB4JuUgPCo42d+Hko uds0aDmvTpVQhxUYGXchs6P+RXhRZMva30g0aCcURGOp7/WHCIkAe2cYvTlejeY24EY2 LG7CLOvpOKz6mS/v5+/UARzDm22yYz/Gs1RRZ/EvSxH8mIxE9ruP67rUPW/sWyb8154L aS99e83XSvSEL11/L5dJ8zb5Je4EmuzSfoAUTyvlG+ru32qP0zpvJDF8ePfxblSwdrf3 /zDAVXJH0CDtbq3KTwvjqIImr1EEkE0H1fynSgvHvzlFwqQm+J9n0oLtH6v/L7fNwwmA 8OAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZTvj3g1V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m14si9040338jaj.123.2021.06.28.11.13.00; Mon, 28 Jun 2021 11:13:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZTvj3g1V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233670AbhF1O1U (ORCPT + 99 others); Mon, 28 Jun 2021 10:27:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:55472 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233384AbhF1OWP (ORCPT ); Mon, 28 Jun 2021 10:22:15 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9AE6661C82; Mon, 28 Jun 2021 14:19:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624889975; bh=Tt4kKcOFumr0HGbvQ6J3GSERtHdLHRwGWxxZHxaiIlA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZTvj3g1VG0tkDI83/HICuKy69/TXhsbgdRd7o7/bMivJVJUfmvsXuaYZJwAzVPLEF Q577CaXQr2qunkwIYJr1SAHsvOpdHi8AOTZmRh6oeYe0Y5ZhZkI+BXW1nPDb6aSmj8 fwsF+0/juC+XGmt7TxxjgKIjrCMTqIOpswyOfXZ0w77lEIQk1bjJwxxd1QFnk9PsTH 9DjhQaTx8BZJgb5q5LWVoDl5j8cXLQqD+GXRT0Hlcd88VhcG045i/LzGeB6ZIcd7Dy HEJmGgu3T72yha/9vLyAnkeFnsx4wJ2/in7H9+yZqZYsz5Is3O7uu37lEtRyQ7ec2F b59J/UqRYa3+A== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Petr Mladek , jenhaochen@google.com, Martin Liu , Minchan Kim , Nathan Chancellor , Nick Desaulniers , Oleg Nesterov , Tejun Heo , Andrew Morton , Linus Torvalds , Greg Kroah-Hartman Subject: [PATCH 5.12 077/110] kthread_worker: split code for canceling the delayed work timer Date: Mon, 28 Jun 2021 10:17:55 -0400 Message-Id: <20210628141828.31757-78-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210628141828.31757-1-sashal@kernel.org> References: <20210628141828.31757-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.12.14-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.12.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.12.14-rc1 X-KernelTest-Deadline: 2021-06-30T14:18+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Petr Mladek commit 34b3d5344719d14fd2185b2d9459b3abcb8cf9d8 upstream. Patch series "kthread_worker: Fix race between kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync()". This patchset fixes the race between kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync() including proper return value handling. This patch (of 2): Simple code refactoring as a preparation step for fixing a race between kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync(). It does not modify the existing behavior. Link: https://lkml.kernel.org/r/20210610133051.15337-2-pmladek@suse.com Signed-off-by: Petr Mladek Cc: Cc: Martin Liu Cc: Minchan Kim Cc: Nathan Chancellor Cc: Nick Desaulniers Cc: Oleg Nesterov Cc: Tejun Heo Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- kernel/kthread.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/kernel/kthread.c b/kernel/kthread.c index 6d3c488a0f82..fc7536079f7e 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -1091,6 +1091,33 @@ void kthread_flush_work(struct kthread_work *work) } EXPORT_SYMBOL_GPL(kthread_flush_work); +/* + * Make sure that the timer is neither set nor running and could + * not manipulate the work list_head any longer. + * + * The function is called under worker->lock. The lock is temporary + * released but the timer can't be set again in the meantime. + */ +static void kthread_cancel_delayed_work_timer(struct kthread_work *work, + unsigned long *flags) +{ + struct kthread_delayed_work *dwork = + container_of(work, struct kthread_delayed_work, work); + struct kthread_worker *worker = work->worker; + + /* + * del_timer_sync() must be called to make sure that the timer + * callback is not running. The lock must be temporary released + * to avoid a deadlock with the callback. In the meantime, + * any queuing is blocked by setting the canceling counter. + */ + work->canceling++; + raw_spin_unlock_irqrestore(&worker->lock, *flags); + del_timer_sync(&dwork->timer); + raw_spin_lock_irqsave(&worker->lock, *flags); + work->canceling--; +} + /* * This function removes the work from the worker queue. Also it makes sure * that it won't get queued later via the delayed work's timer. @@ -1105,23 +1132,8 @@ static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork, unsigned long *flags) { /* Try to cancel the timer if exists. */ - if (is_dwork) { - struct kthread_delayed_work *dwork = - container_of(work, struct kthread_delayed_work, work); - struct kthread_worker *worker = work->worker; - - /* - * del_timer_sync() must be called to make sure that the timer - * callback is not running. The lock must be temporary released - * to avoid a deadlock with the callback. In the meantime, - * any queuing is blocked by setting the canceling counter. - */ - work->canceling++; - raw_spin_unlock_irqrestore(&worker->lock, *flags); - del_timer_sync(&dwork->timer); - raw_spin_lock_irqsave(&worker->lock, *flags); - work->canceling--; - } + if (is_dwork) + kthread_cancel_delayed_work_timer(work, flags); /* * Try to remove the work from a worker list. It might either -- 2.30.2