Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp659751pxv; Fri, 9 Jul 2021 06:22:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxH3oCn2y4BjVyI4MYQbFalYaYLp1j3cVU1pf9O6GHeuJgDkamPwRlnDr+ApILsFQ5ocFi+ X-Received: by 2002:a05:6638:380b:: with SMTP id i11mr31855939jav.57.1625836953048; Fri, 09 Jul 2021 06:22:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625836953; cv=none; d=google.com; s=arc-20160816; b=HYGRyqJBBQjUI+U/zVy+AhzVOFLRS0UqLMqclPCYhpkwewJXQf4aurmnQz1tDsgVmE q6dUGxPxrpkQQAQ3h6xUWfgfOCM5yk8aVnAene1+Z93/JnnO4MFfT7ZLa7uUdlr3vafR DGtSSBFSncaoRJ+uVCjF+i9JGyZxvMyLyrdnMRtenmP0WMwxRXMIW8KTMKUbMyYCWmUr /nzliuiDKFcJyQzUSgKp9S+c0yMnjNVhBQEpNMjl6EYW6hmDVWAYbvkJ0Lda9mApegTp ekus66Tu1ntQjXoJaUSMfY1lboxl83hd3LYqIx7KrMVtDrRnbozoMBHVeRdnh+s7rNEy 0Tfg== 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=neKo8tfr43hJZExQ1mVBzycBRCdMZvwLpryVXsj+aBk=; b=eEnU9IL1HiqkCnkJTFPH935gWWgdji32SdMWpfyudyGC5fK1riivxa7q8SjY34OTm+ OET9uJtw/x9vY86SiBffCr2E9jEpLVWKg6fb0ELOz0JsZ+TtsUdmtR6Lh6H5oB87xSmc EasCvLZNIZ5vgP3uX6VjRAuJiRzCkKe6KWLP0Z5BdpAjprwfu7IPURQZrsZDa7XVEKQE nmp2vcg26Ihn1N9Omr9AnGLLwAXo1TYpy5RpeTzQqW4MoIdACdIbZtyeu3tuXhrPaAqY zocSl7f0YXYbNngCivMxKHuWSo8NnwVI3DJPoAR+8NCBfl58GMSxCLR46UYQ2JFtMLTr M6eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0+jxBs4R; 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 n11si5778128ilt.138.2021.07.09.06.22.20; Fri, 09 Jul 2021 06:22:33 -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=0+jxBs4R; 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 S232443AbhGINWt (ORCPT + 99 others); Fri, 9 Jul 2021 09:22:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:53756 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232345AbhGINWl (ORCPT ); Fri, 9 Jul 2021 09:22:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 846C4613BC; Fri, 9 Jul 2021 13:19:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1625836797; bh=3Ei0ei7YxQdPQNOdlj5NGcivwPBaeICKjFjBEicd5Hk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0+jxBs4RkOyyNZPYQnpMr/tL5smh1GvpU0cuUq9YtaXLmLKkuo15nx/OtKjUvdVU9 tfjVnr0LJIAnDGm/jDuk4QpBRkJIXx7DECVi7j+W1BOl/mCmPHdC1+6uAmcsHuLYfs df+ABNTNMzepA+0XYaIbBHYr+xRm1fa7NJRoAWMs= 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.14 23/25] kthread_worker: split code for canceling the delayed work timer Date: Fri, 9 Jul 2021 15:18:54 +0200 Message-Id: <20210709131641.826091010@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210709131627.928131764@linuxfoundation.org> References: <20210709131627.928131764@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 @@ -979,6 +979,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. * @@ -992,23 +1019,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