From patchwork Fri Sep 1 15:40:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 9934619 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3CCBC6016C for ; Fri, 1 Sep 2017 15:44:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3037922BF1 for ; Fri, 1 Sep 2017 15:44:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 248E1237F1; Fri, 1 Sep 2017 15:44:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C821822BF1 for ; Fri, 1 Sep 2017 15:44:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752011AbdIAPk6 (ORCPT ); Fri, 1 Sep 2017 11:40:58 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48737 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751983AbdIAPk5 (ORCPT ); Fri, 1 Sep 2017 11:40:57 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A9EFEC027390; Fri, 1 Sep 2017 15:40:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A9EFEC027390 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=dhowells@redhat.com Received: from warthog.procyon.org.uk (ovpn-124-227.rdu2.redhat.com [10.10.124.227]) by smtp.corp.redhat.com (Postfix) with ESMTP id D3D296062A; Fri, 1 Sep 2017 15:40:53 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [RFC PATCH 01/11] workqueue: Add a decrement-after-return and wake if 0 facility From: David Howells To: linux-afs@lists.infradead.org Cc: Tejun Heo , linux-fsdevel@vger.kernel.org, dhowells@redhat.com, Lai Jiangshan , linux-kernel@vger.kernel.org Date: Fri, 01 Sep 2017 16:40:53 +0100 Message-ID: <150428045304.25051.1778333106306853298.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 01 Sep 2017 15:40:56 +0000 (UTC) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a facility to the workqueue subsystem whereby an atomic_t can be registered by a work function such that the work function dispatcher will decrement the atomic after the work function has returned and then call wake_up_atomic() on it if it reached 0. This is analogous to complete_and_exit() for kernel threads and is used to avoid a race between notifying that a work item is about to finish and the .text segment from a module being discarded. The way this is used is that the work function calls: dec_after_work(atomic_t *counter); to register the counter and then process_one_work() calls it, potentially wakes it and clears the registration. The reason I've used an atomic_t rather than a completion is that (1) it takes up less space and (2) it can monitor multiple objects. Signed-off-by: David Howells cc: Tejun Heo cc: Lai Jiangshan --- include/linux/workqueue.h | 1 + kernel/workqueue.c | 25 +++++++++++++++++++++++++ kernel/workqueue_internal.h | 1 + 3 files changed, 27 insertions(+) diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h index db6dc9dc0482..ceaed1387e9b 100644 --- a/include/linux/workqueue.h +++ b/include/linux/workqueue.h @@ -451,6 +451,7 @@ extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, extern void flush_workqueue(struct workqueue_struct *wq); extern void drain_workqueue(struct workqueue_struct *wq); +extern void dec_after_work(atomic_t *counter); extern int schedule_on_each_cpu(work_func_t func); diff --git a/kernel/workqueue.c b/kernel/workqueue.c index ca937b0c3a96..2936ad0ab293 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -2112,6 +2112,12 @@ __acquires(&pool->lock) dump_stack(); } + if (worker->dec_after) { + if (atomic_dec_and_test(worker->dec_after)) + wake_up_atomic_t(worker->dec_after); + worker->dec_after = NULL; + } + /* * The following prevents a kworker from hogging CPU on !PREEMPT * kernels, where a requeueing work item waiting for something to @@ -3087,6 +3093,25 @@ int schedule_on_each_cpu(work_func_t func) } /** + * dec_after_work - Register counter to dec and wake after work func returns + * @counter: The counter to decrement and wake + * + * Register an atomic counter to be decremented after a work function returns + * to the core. The counter is 'woken' if it is decremented to 0. This allows + * synchronisation to be effected by one or more work functions in a module + * without leaving a window in which the work function code can be unloaded. + */ +void dec_after_work(atomic_t *counter) +{ + struct worker *worker = current_wq_worker(); + + BUG_ON(!worker); + BUG_ON(worker->dec_after); + worker->dec_after = counter; +} +EXPORT_SYMBOL(dec_after_work); + +/** * execute_in_process_context - reliably execute the routine with user context * @fn: the function to execute * @ew: guaranteed storage for the execute work structure (must diff --git a/kernel/workqueue_internal.h b/kernel/workqueue_internal.h index 8635417c587b..94ea1ca9b01f 100644 --- a/kernel/workqueue_internal.h +++ b/kernel/workqueue_internal.h @@ -28,6 +28,7 @@ struct worker { struct work_struct *current_work; /* L: work being processed */ work_func_t current_func; /* L: current_work's fn */ + atomic_t *dec_after; /* Decrement after func returns */ struct pool_workqueue *current_pwq; /* L: current_work's pwq */ bool desc_valid; /* ->desc is valid */ struct list_head scheduled; /* L: scheduled works */