Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp661335pxv; Fri, 9 Jul 2021 06:24:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTGXbtueowJHwfgE9Zag/2eOhgFpT8rcDwzeXDOIyswmqafUIzf7gNTEVDJGlU9R3qQnIY X-Received: by 2002:a92:1e06:: with SMTP id e6mr26593169ile.258.1625837051229; Fri, 09 Jul 2021 06:24:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625837051; cv=none; d=google.com; s=arc-20160816; b=LvBu85BzsGI80WMv8O9wud7DWSXFsMT+D+H6pCh/BvF640GurX7nHSV3Ky0Cp+WisD QYPREKK+TZhgZqo8r2vSPeMhyIKtwN00nHSphn8r564yd6S45YkgR7nXeHsIFtBLCwTA L0vY28fAFbHkmK0eQAV3ZoEPQUSWBD5+yc5Q1bUM5OSk5cJ63Vm0c7SIM4JVC4VNukUo W7cIh+qv7Unbpti/DZq199wrqaFp2zDdpBrAwjNr6hGxMlBXxEHo1OkYG81SBBA50u46 iyMUM3NFaumO2S3rh5xCXv2r6yEdf1yuT+QwPVXCp8qXaP7d2z9KPDPBJjg1aCdo8bZW XVCg== 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=JukGzXfA7tFd2WApzPFEVANeLBGK9PR64DsrtrAh5ic=; b=pZdUUsBYfD5eI8By6sDKxWeHS+r1I9HgrDakb1Fc8J8El8iyI3dtvMekluW9XdfT1g aH3NIFfzU908K1pD2k0FxG+cYWUocp8jL/dDrAHYxeA6Lo1iVq8ulIJiyFCmKb1RBOho UeKYjR9oWupVjFXw5wyfhkRPl8pOia3s2HzI3wXu7YiyCSQ/5HF1k6ic9xN8KHA5pW07 tOE0dK4UfxK6TuRAwm6COgs5IIjKEnFvWY/PuwEVptf6/OMKpfBVQbl4tgoLMIJwEXNf sJxgWHxheW/5gC+LBVDCwTrbYGPFTPb9yfViQdkJpiT7DWo/XO21ZyfjkYpzX/G3U6Q3 2l9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rqY8DQDI; 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 x22si5997365iow.99.2021.07.09.06.23.59; Fri, 09 Jul 2021 06:24:11 -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=rqY8DQDI; 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 S232786AbhGINYs (ORCPT + 99 others); Fri, 9 Jul 2021 09:24:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:56330 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231989AbhGINYf (ORCPT ); Fri, 9 Jul 2021 09:24:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 76C7C613C7; Fri, 9 Jul 2021 13:21:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1625836912; bh=BVoCCCulbGBZ6QafdcpFi5G0j+SZFh+9GhnirFMy3RU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rqY8DQDIPwJMJ5ujowOlQhFkmeiFOup/YV9gGuuwmBmFGDs5Xvo5VokKVLrfiVk18 8lgDWEOSqbyXKK9DvArL8xJzBnQm6X6n1P9xXIgGHfFb8B+iSCEGYkDyI2o+3dNMDr ZaIhZtLnn1YidtFD/1zyyLo3mVeisOUZ1lRZ0KAw= 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.19 27/34] kthread_worker: split code for canceling the delayed work timer Date: Fri, 9 Jul 2021 15:20:43 +0200 Message-Id: <20210709131659.288832727@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210709131644.969303901@linuxfoundation.org> References: <20210709131644.969303901@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 @@ -1010,6 +1010,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. * @@ -1023,23 +1050,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