Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp565409rwj; Fri, 23 Dec 2022 05:23:49 -0800 (PST) X-Google-Smtp-Source: AMrXdXuZc6bgmY33q5T4eKdEQ9L4IItBBSGZsZg+HVICjOs1qAcgyN0WFmHgREJHa5BSPCfoIF3u X-Received: by 2002:a17:907:7f04:b0:7c1:36:9002 with SMTP id qf4-20020a1709077f0400b007c100369002mr9114826ejc.67.1671801829587; Fri, 23 Dec 2022 05:23:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671801829; cv=none; d=google.com; s=arc-20160816; b=HKF7dcJt3AJlPSjb9k9iNs24xZPx9kWMdXIsIdcdiQ9d3RdXbDV4M4+ps4xs0eunG2 4Myo2weo+fMtmfILvCzFRyQkHJd2/ROnxVzdG9ShribuCzPtT5Giuqca+dHOIsws1JF3 5V1xyEG9KDI8354+y0OwbouPum9ylN979aCyTFFCIQ39b539CT+8Jqd2uZxlx7wTmKZV jESFauZZJ8bX2xPHIElIn+4Ot5t3YpHj6wbdQlOyl9ijgcBROsU9EyBh3rxFbltRdkEW nUHRXe6+yR2ZiHvnyIWevRXX/AKeKTqLsEaERti1Ugc0dVn6N8XPdrBoWHFWNicLGAok N+KA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=SsOYvUO4JaqrBb5Nu5K5FLSJ62kk3wxVoVF7v9Vqj3k=; b=CG71e0WGHn/g6ieEb7489tJMhlJXo47R2qyFxcmZLUABJDlnyd1L0xJ0k4lcG2Ci+R Ya6D36EqwhgcZmexI4R+9zkqIzqsqSAmkcjBlZ/fx1qpB5+epgEtFd2Yzfwjly+j1oUa ZGhFtq8OmQQLY41o5ckJEf68IT98WMbjk0RzIsd62Rz90n/raCRRNMA+RFW3o8A0CQSc +wE3LCZj1c6tolfr//pEX4LLT2VrMh+wd4dR1eDIxn0UguhAXLEwsi0L1O6J01+5EAE6 4hfgf7v6az7nA5fsGlkrk+oAFzcRJ66g8SV8n5gsLeHYmlJVr1xRhSS6/ttvx7/rhtnU WGSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jo7eK5tX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dz19-20020a0564021d5300b0045d050f0fa3si3549922edb.565.2022.12.23.05.23.34; Fri, 23 Dec 2022 05:23:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jo7eK5tX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230312AbiLWNKd (ORCPT + 65 others); Fri, 23 Dec 2022 08:10:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230106AbiLWNKb (ORCPT ); Fri, 23 Dec 2022 08:10:31 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABE682EC for ; Fri, 23 Dec 2022 05:10:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1671801030; x=1703337030; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=tV8wUuZIsiyTKBUsG9YZqXJnycLYvbf9P55YbqnPIaQ=; b=jo7eK5tX5jkYBYTGtKAkHNxS3d4aeTFD1XPP8aYLwtqqThFb/70hPbRb Z4LKTvvTsC4u/sRUZHO+BbzRZeHg8ODCbcjJcE7DC+EBvbRqdrO0nbIiB l8vxjQ7QJ+IY1XY9RZrIr+7VBGHjBiV/iVnobfFbDhjjN/kpz+4ZzdpPl wkoxgKtLGomQC6vaCHxJKcv/F+ZSTZsZXKB4IhguTzNOxHMtb5cUNlL9f IT5Ub2Z2t3Y9ploiIGT3mXW+Jy5HPNA3PAoCkvkBzMD3BA31j4DdNq7fq 96kaDk8IHtfJYY0QSEKN8XW6T3dVtfOboBIT1+qxx3sY+H36y4YM1xKPk A==; X-IronPort-AV: E=McAfee;i="6500,9779,10569"; a="300003075" X-IronPort-AV: E=Sophos;i="5.96,268,1665471600"; d="scan'208";a="300003075" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Dec 2022 05:10:30 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10569"; a="826292911" X-IronPort-AV: E=Sophos;i="5.96,268,1665471600"; d="scan'208";a="826292911" Received: from zq-optiplex-7090.bj.intel.com ([10.238.156.129]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Dec 2022 05:10:28 -0800 From: Zqiang To: pmladek@suse.com, akpm@linux-foundation.org, frederic@kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH] kthread_worker: Flush all delayed works when destroy kthread worker Date: Fri, 23 Dec 2022 21:16:01 +0800 Message-Id: <20221223131601.237244-1-qiang1.zhang@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When destroy a kthread worker, only flush all current works on kthread worker, this is not very sufficient, there may be some delayed works in the pending state, this commit therefore add flush delayed works function in kthread_destroy_worker(). Signed-off-by: Zqiang --- include/linux/kthread.h | 1 + kernel/kthread.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/linux/kthread.h b/include/linux/kthread.h index 30e5bec81d2b..8616228abb3b 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -206,6 +206,7 @@ bool kthread_mod_delayed_work(struct kthread_worker *worker, void kthread_flush_work(struct kthread_work *work); void kthread_flush_worker(struct kthread_worker *worker); +void kthread_flush_delayed_works(struct kthread_worker *worker); bool kthread_cancel_work_sync(struct kthread_work *work); bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work); diff --git a/kernel/kthread.c b/kernel/kthread.c index f97fd01a2932..2744f6b769d1 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -1375,6 +1375,35 @@ void kthread_flush_worker(struct kthread_worker *worker) } EXPORT_SYMBOL_GPL(kthread_flush_worker); +/** + * kthread_flush_delayed_works - flush all current delayed works on a + * kthread_worker. + * @worker: worker to flush + * + * Wait until all currently executing or pending delayed works are + * queued completed. + */ +void kthread_flush_delayed_works(struct kthread_worker *worker) +{ + unsigned long flags; + struct kthread_delayed_work *dwork; + struct kthread_work *work; + + raw_spin_lock_irqsave(&worker->lock, flags); + while (!list_empty(&worker->delayed_work_list)) { + work = list_first_entry(&worker->delayed_work_list, + struct kthread_work, node); + list_del_init(&work->node); + dwork = container_of(work, struct kthread_delayed_work, work); + raw_spin_unlock_irqrestore(&worker->lock, flags); + if (del_timer_sync(&dwork->timer)) + kthread_queue_work(worker, &dwork->work); + raw_spin_lock_irqsave(&worker->lock, flags); + } + raw_spin_unlock_irqrestore(&worker->lock, flags); +} +EXPORT_SYMBOL_GPL(kthread_flush_delayed_works); + /** * kthread_destroy_worker - destroy a kthread worker * @worker: worker to be destroyed @@ -1391,8 +1420,10 @@ void kthread_destroy_worker(struct kthread_worker *worker) if (WARN_ON(!task)) return; + kthread_flush_delayed_works(worker); kthread_flush_worker(worker); kthread_stop(task); + WARN_ON(!list_empty(&worker->delayed_work_list)); WARN_ON(!list_empty(&worker->work_list)); kfree(worker); } -- 2.25.1