Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp659953pxv; Fri, 9 Jul 2021 06:22:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyx/WmXlfanMzV2aaLgahWt47LhbR0s88tnNAhz4otrlZLSaj4BXy3O4i0uxzrKyVa3l3pw X-Received: by 2002:a92:440b:: with SMTP id r11mr1530083ila.305.1625836964610; Fri, 09 Jul 2021 06:22:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625836964; cv=none; d=google.com; s=arc-20160816; b=sgt3ng4cEPAoEW5W7hamr9f0aMwngQGIpXvVNXc8YjhX/oAWeub3DRWH/BmD1C10ns KuB0NJdNU/yO4iHYO19FBthEVMAig+Q6ri50M378y2F69f8rstVRaVOmQZPLNMA36alY r2b91+09tGWA/bh/bBkIt7w58Zu8LZfrXOTkFRYOcTNltVfUKpG+GtNyannVfwgsU+We AEqiyv+vGC0wskr0fFZ99uDsAhJGaUT9TV5bCngaAHBf4QGiGwzhFxvzycMpgPDj2Ylc laLeVfFIyg55pAfY7eyDa0GjVokNRrMIQLkhZd993LoOqOkPsT1AFCv+qhvTPQSx+CbE g/RA== 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=CItHtyZJSRnyxV//HZVlXmYTAnmZFCwBZsqEgtl4JgI=; b=I7xQb5st2K6Ozptm3NEGSarXd4mS7+epxUkwO5cLh/egNqvs51/qifYQ/ctoSuWk2J x0RQ3NxKW5eMXdfQylHETabk5QVJsGykHfXzB7/dqJvjXCcw+EkpeMhH0YpC5B2OZ8Fj BYrZOs1S13ywbGJhP5LfHUlgEwIK6Iof1YHB91ja2CPIUkCDc66agGnJX5895E9ewybT hXE2Y5pIKxbpVERAcLqQr/P1ARIjQhFtprRy8xNVAwqcuVJUlLzqRHNLjCf5xKlqSWtx i8Cfdrj8zWAv+aXpoMhR5NZVOkORlJ1yiqXCN1nSW24OSsbWeiNMqs6bjkYE0LPsb3v4 CuQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PDOTE7uk; 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 j17si6358806jap.126.2021.07.09.06.22.29; Fri, 09 Jul 2021 06:22:44 -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=PDOTE7uk; 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 S232453AbhGINWx (ORCPT + 99 others); Fri, 9 Jul 2021 09:22:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:53840 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232386AbhGINWm (ORCPT ); Fri, 9 Jul 2021 09:22:42 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id AA8A2613C3; Fri, 9 Jul 2021 13:19:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1625836799; bh=bbUEBjXvmRthSEased0vM4NUCYT5Doe2Ds5jTyGtS1E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PDOTE7ukGziAxd2e6DH9X7GRAL7O0jnLObBVMYtrnExP4cUJuzcHC22u2dYl2maLI Y3g1gYFnvi9H2YzkaW/u3pdgNh6zNJBvvdBkWNblY1pw6bHjQhTDhh3fXXfwRDlCQA udG9+N7yfdXeQdvpzyTmRWghPsS3GJM6632i7sTw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Petr Mladek , Martin Liu , jenhaochen@google.com, Minchan Kim , Nathan Chancellor , Nick Desaulniers , Oleg Nesterov , Tejun Heo , Andrew Morton , Linus Torvalds Subject: [PATCH 4.14 24/25] kthread: prevent deadlock when kthread_mod_delayed_work() races with kthread_cancel_delayed_work_sync() Date: Fri, 9 Jul 2021 15:18:55 +0200 Message-Id: <20210709131642.430593730@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 5fa54346caf67b4b1b10b1f390316ae466da4d53 upstream. The system might hang with the following backtrace: schedule+0x80/0x100 schedule_timeout+0x48/0x138 wait_for_common+0xa4/0x134 wait_for_completion+0x1c/0x2c kthread_flush_work+0x114/0x1cc kthread_cancel_work_sync.llvm.16514401384283632983+0xe8/0x144 kthread_cancel_delayed_work_sync+0x18/0x2c xxxx_pm_notify+0xb0/0xd8 blocking_notifier_call_chain_robust+0x80/0x194 pm_notifier_call_chain_robust+0x28/0x4c suspend_prepare+0x40/0x260 enter_state+0x80/0x3f4 pm_suspend+0x60/0xdc state_store+0x108/0x144 kobj_attr_store+0x38/0x88 sysfs_kf_write+0x64/0xc0 kernfs_fop_write_iter+0x108/0x1d0 vfs_write+0x2f4/0x368 ksys_write+0x7c/0xec It is caused by the following race between kthread_mod_delayed_work() and kthread_cancel_delayed_work_sync(): CPU0 CPU1 Context: Thread A Context: Thread B kthread_mod_delayed_work() spin_lock() __kthread_cancel_work() spin_unlock() del_timer_sync() kthread_cancel_delayed_work_sync() spin_lock() __kthread_cancel_work() spin_unlock() del_timer_sync() spin_lock() work->canceling++ spin_unlock spin_lock() queue_delayed_work() // dwork is put into the worker->delayed_work_list spin_unlock() kthread_flush_work() // flush_work is put at the tail of the dwork wait_for_completion() Context: IRQ kthread_delayed_work_timer_fn() spin_lock() list_del_init(&work->node); spin_unlock() BANG: flush_work is not longer linked and will never get proceed. The problem is that kthread_mod_delayed_work() checks work->canceling flag before canceling the timer. A simple solution is to (re)check work->canceling after __kthread_cancel_work(). But then it is not clear what should be returned when __kthread_cancel_work() removed the work from the queue (list) and it can't queue it again with the new @delay. The return value might be used for reference counting. The caller has to know whether a new work has been queued or an existing one was replaced. The proper solution is that kthread_mod_delayed_work() will remove the work from the queue (list) _only_ when work->canceling is not set. The flag must be checked after the timer is stopped and the remaining operations can be done under worker->lock. Note that kthread_mod_delayed_work() could remove the timer and then bail out. It is fine. The other canceling caller needs to cancel the timer as well. The important thing is that the queue (list) manipulation is done atomically under worker->lock. Link: https://lkml.kernel.org/r/20210610133051.15337-3-pmladek@suse.com Fixes: 9a6b06c8d9a220860468a ("kthread: allow to modify delayed kthread work") Signed-off-by: Petr Mladek Reported-by: Martin Liu Cc: 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 | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -1006,8 +1006,11 @@ static void kthread_cancel_delayed_work_ } /* - * 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. + * This function removes the work from the worker queue. + * + * It is called under worker->lock. The caller must make sure that + * the timer used by delayed work is not running, e.g. by calling + * kthread_cancel_delayed_work_timer(). * * The work might still be in use when this function finishes. See the * current_work proceed by the worker. @@ -1015,13 +1018,8 @@ static void kthread_cancel_delayed_work_ * Return: %true if @work was pending and successfully canceled, * %false if @work was not pending */ -static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork, - unsigned long *flags) +static bool __kthread_cancel_work(struct kthread_work *work) { - /* Try to cancel the timer if exists. */ - if (is_dwork) - kthread_cancel_delayed_work_timer(work, flags); - /* * Try to remove the work from a worker list. It might either * be from worker->work_list or from worker->delayed_work_list. @@ -1074,11 +1072,23 @@ bool kthread_mod_delayed_work(struct kth /* Work must not be used with >1 worker, see kthread_queue_work() */ WARN_ON_ONCE(work->worker != worker); - /* Do not fight with another command that is canceling this work. */ + /* + * Temporary cancel the work but do not fight with another command + * that is canceling the work as well. + * + * It is a bit tricky because of possible races with another + * mod_delayed_work() and cancel_delayed_work() callers. + * + * The timer must be canceled first because worker->lock is released + * when doing so. But the work can be removed from the queue (list) + * only when it can be queued again so that the return value can + * be used for reference counting. + */ + kthread_cancel_delayed_work_timer(work, &flags); if (work->canceling) goto out; + ret = __kthread_cancel_work(work); - ret = __kthread_cancel_work(work, true, &flags); fast_queue: __kthread_queue_delayed_work(worker, dwork, delay); out: @@ -1100,7 +1110,10 @@ static bool __kthread_cancel_work_sync(s /* Work must not be used with >1 worker, see kthread_queue_work(). */ WARN_ON_ONCE(work->worker != worker); - ret = __kthread_cancel_work(work, is_dwork, &flags); + if (is_dwork) + kthread_cancel_delayed_work_timer(work, &flags); + + ret = __kthread_cancel_work(work); if (worker->current_work != work) goto out_fast;