Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp657475pxv; Fri, 9 Jul 2021 06:20:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzstbJLbjzJCyl++Ilxm3/rjbLREqMLrQDUsQiytiQdHQ0nmpYoVdT4K7CWVJQoXVmAU5Ad X-Received: by 2002:a92:c7a7:: with SMTP id f7mr26040362ilk.154.1625836813344; Fri, 09 Jul 2021 06:20:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625836813; cv=none; d=google.com; s=arc-20160816; b=FuM4FOAyoY3JCMBaqiYbywGVW0yEdVG87WLsokjMErF0XGRnV+v/CqyP+iKbL8MXMO DLVHmJpkVGdRAD8yWSdb54n3fIKQl0q8KIrwbk7lyKs85NQK5dwXyeP3P7HbvtRbsgNA DZRFy5qtZA9Kcu5Q7J1HB2K/SsPceftIXFk6OR3Bj1sOzVgy6rrvPLtIgCbTfiiwSRmV Jq2BwbCVpnDCrByEXtQdsS4t+E6a5N9JXXmeI0nKJVVfOcZQ721LcdcE4GpHhsa3vQPB 44dcvYoo7ReCPx59XbzFZFOT8WNeUbQxzf/N3iztiTrkPw0Q3/Jri7GbcTpocsbOrt16 egMA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Nn3JGLXaCpgKmXeMmXt1iX6pGCHGPV+UUkj0oGjFK04=; b=IltBszkcSD73MTbRra6qAHnEb3pM1ESmuSDH25C9uPWw7ah3Lvzu/PPU9G1gROjf0Y BotQSsArkkGjHEXM0KTimZXIlOLU6IAKhlXDPrCHyxPO7Z0PAEsKzH5iWF+r9MkS+ZE4 Z95lCVn2msol2zrkbLakaj4i04uHzCmJ0lIiDyA1ShH18dYplmVaJ5U4VS6lGkO4n5Iu VW73CiyvJ0iTvGMSuBVSU3xruoTve51SU0zvhcvXTs9VAN1Mvry2NEWcDFLjE8LT8uZQ 3OHEkMqR90NaCVhs6HqIW0OJRgBBaVCYk1kbfCYsHicm52K+Ham5Etg9BYgVDl3EUgku UeuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HN8noom+; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a1si6457455jat.32.2021.07.09.06.20.00; Fri, 09 Jul 2021 06:20: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=@linuxfoundation.org header.s=korg header.b=HN8noom+; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232035AbhGINVt (ORCPT + 99 others); Fri, 9 Jul 2021 09:21:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:51918 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232021AbhGINVl (ORCPT ); Fri, 9 Jul 2021 09:21:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E82AF61357; Fri, 9 Jul 2021 13:18:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1625836738; bh=aNIfBMAS+OUJJ23eapl/YTXCKRYtQWg8KfJDh4UNTkY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HN8noom+uIbU1YHnuipGBIwnDRy9YTQTRecRZFP8FJcihOsSemuXXTYt9Sztn3MUN /kz7cuFVgzMG/XcVI23MRMz7f4/OSC2x1ykCf59b2oFYTQc2uEeNf3+a32oxUkW51R PVNgM0dUhP5DmPhAUc0nSEVKJ06e08LpojkiLDoM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Petr Mladek , jenhaochen@google.com, Martin Liu , Minchan Kim , Nathan Chancellor , Nick Desaulniers , Oleg Nesterov , Tejun Heo , Andrew Morton , Linus Torvalds Subject: [PATCH 4.9 7/9] kthread_worker: split code for canceling the delayed work timer Date: Fri, 9 Jul 2021 15:18:34 +0200 Message-Id: <20210709131552.455038494@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210709131542.410636747@linuxfoundation.org> References: <20210709131542.410636747@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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(-) --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -952,6 +952,33 @@ void kthread_flush_work(struct kthread_w 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++; + spin_unlock_irqrestore(&worker->lock, *flags); + del_timer_sync(&dwork->timer); + 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. * @@ -965,23 +992,8 @@ static bool __kthread_cancel_work(struct 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++; - spin_unlock_irqrestore(&worker->lock, *flags); - del_timer_sync(&dwork->timer); - 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