Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp4360901ima; Mon, 4 Feb 2019 15:12:44 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib1PSCxez5OA6SqD/auDjPHfnummb9uqO11UZe4OcY482Ne+sqB43fFyLLJNBTmrfJ5OnVh X-Received: by 2002:a17:902:5a5:: with SMTP id f34mr1890140plf.161.1549321964076; Mon, 04 Feb 2019 15:12:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549321964; cv=none; d=google.com; s=arc-20160816; b=StkQG101pO0Vy9v2nIKMqgNXUW4gn2w00KdQGOCwRUbhk9gEThc8pcbJeyP6QHC7cZ ozmjgAGweKuCwZqYBHZflPiCChLlw8DHPc8eXay/+550Nnq0tZ93u3VoENw76YaL6iUO RU7awcpCHpCcCGCiVDXblGdZbxNW9tGKyaDNVraFhnZChWDyJEXpNi5JHmy5YZdl+Owh Yr2zy0wj/HeVA8b8JzERCgCjKPvh9DBXA0yzes365sRuVUaJ+SfHvayE1dzWXKoCWGGp 9Pfb+/bbHBuH6DUpggzyQuoqLUsRDVXeVchoQqFir+RweVfVPdQJozGcZ2gW1Rp0IbOq 6Wfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=NCVAts3n2tDe0LNoEqLUdjE98FKwB9oUA9NWkCPnm+Y=; b=HD6vWh1I3z16WzoIsGxoRZwAsQsD6GjPS4ML47HK3sC+qBnEkoGg+GP+670y7cbIV9 yEM49POy5QCiXT/a0OTavYqYEsgo7iXlUhIVxjcBe2mi1eKk2LtrU0jLcFNWyjksBTsk eJYIumsluc+NVp7KCLmua154TbA4Y40Cs6iEZM31TZDRJXeOuPgafNv0YK95ATnK62yV YOiKWRYHht9ctMV/9nEeV2jHifGJ32Dg4net+0ikeVfrZIvXmvhgq4uszN4AOFYX1QdW 761cn+f1vXvIs3twFk64AJM6ryxuh64kCyNsgDEksvvLex+DLaJOc45HLBpOOjc+OnmP rGOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=f9bTmgDp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si1334745plp.114.2019.02.04.15.12.28; Mon, 04 Feb 2019 15:12:44 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=f9bTmgDp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726969AbfBDWKA (ORCPT + 99 others); Mon, 4 Feb 2019 17:10:00 -0500 Received: from mail-it1-f195.google.com ([209.85.166.195]:55876 "EHLO mail-it1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726882AbfBDWJ6 (ORCPT ); Mon, 4 Feb 2019 17:09:58 -0500 Received: by mail-it1-f195.google.com with SMTP id m62so3681694ith.5 for ; Mon, 04 Feb 2019 14:09:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=NCVAts3n2tDe0LNoEqLUdjE98FKwB9oUA9NWkCPnm+Y=; b=f9bTmgDpBLc8Y7EjYzMx1A1I1lU4U9a7k+jmejalOUYW8w/lWnNjXqbLThbHDoSrq3 hzwBhWoLj95UxTxG/Hca4Knfdkgi7m+axnMRRKfpFtzRxggvv9IZSAsUJS7pyIqzVnk/ N3e9wAB7gH2A2UIydSNtmsqVmMnsl7Yc8RLuAtFoYE14LBGrkVg/WU4eDrJCSnAfSQFJ 2tDx1PZ6DtxG9GHgMRDedzzZgFxg/Pn4iEgznkYUH2QKo7CHiYNaziiIIwl40WeS0DTt 6HUDxcPJYt4bKpKJkigDflqj6yMbIjmQBbzpebbA+0r5E06RvuQcwo5/pMmKsB6pGrXy uZTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NCVAts3n2tDe0LNoEqLUdjE98FKwB9oUA9NWkCPnm+Y=; b=GbI4cgK3UXEct112xHkoyCJXaKchfCyuyHVHkuJNBXpaDKXgGfx2q+N9bZ8u9PULfL Nd6BDd7O4uDjrNSlT/a7kI9v2RyMjiyu1X0BXwJPZ+COJ0fajP2O55B+62Ay5Y3Q1jcU 1TSo/B70ojl8VoLikyLaAZ7Btk6lIT35NnMzlD+ew5Vu/yK24qrXCSzsHJCZDIdV7zx9 ncNIpKZasuPdEFwuxusti65eJ0aEjeuCE1Ek5kS3/134FnSGl7B6xnd6QN3pQeSRb9sO htVdM33AsEtdG9b8amKqEnUJHv08C9XCNGUeamX6RabMfReWC+MJXz+JSv+Nvrd4ELRx q6eg== X-Gm-Message-State: AHQUAubZIlPA0yaSVoQ75gcJlTG7aGpr3MgfbA1aY60S//UrVUUeT0AR /dAmsx+0E0F+i4MYIIuNd2k= X-Received: by 2002:a24:a542:: with SMTP id w2mr827285iti.64.1549318197480; Mon, 04 Feb 2019 14:09:57 -0800 (PST) Received: from svens-asus.arcx.com ([184.94.50.30]) by smtp.gmail.com with ESMTPSA id z1sm531602ioi.77.2019.02.04.14.09.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Feb 2019 14:09:56 -0800 (PST) From: Sven Van Asbroeck X-Google-Original-From: Sven Van Asbroeck To: Tejun Heo Cc: Lai Jiangshan , linux-kernel@vger.kernel.org, Sebastian Reichel , Dmitry Torokhov , Kees Cook Subject: [RFC v1 1/3] workqueue: Add resource-managed version of INIT_[DELAYED_]WORK() Date: Mon, 4 Feb 2019 17:09:50 -0500 Message-Id: <20190204220952.30761-2-TheSven73@googlemail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190204220952.30761-1-TheSven73@googlemail.com> References: <20190204220952.30761-1-TheSven73@googlemail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In modules which extensively use devm_ resource management, it is often easy to overlook (delayed) work that is left pending or running after the module is unloaded. This could introduce user-after-free issues. Nudge kernel developers into 'doing the right thing' by introducing a resource-managed version of INIT_[DELAYED_]WORK(). This can be used as an elegant way to ensure that work is not left pending or running after its dependencies are released. Functions introduced in workqueue.h : - devm_init_work() - devm_init_delayed_work() Signed-off-by: Sven Van Asbroeck --- include/linux/workqueue.h | 7 +++++ kernel/workqueue.c | 54 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h index 60d673e15632..eee148eb9908 100644 --- a/include/linux/workqueue.h +++ b/include/linux/workqueue.h @@ -15,6 +15,7 @@ #include #include +struct device; struct workqueue_struct; struct work_struct; @@ -670,4 +671,10 @@ int workqueue_offline_cpu(unsigned int cpu); int __init workqueue_init_early(void); int __init workqueue_init(void); +int __must_check devm_init_work(struct device *dev, struct work_struct *work, + work_func_t func); +int __must_check devm_init_delayed_work(struct device *dev, + struct delayed_work *dw, + work_func_t func); + #endif diff --git a/kernel/workqueue.c b/kernel/workqueue.c index fc5d23d752a5..ab814b0b6c81 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -5837,3 +5837,57 @@ int __init workqueue_init(void) return 0; } + +static void devm_work_release(void *data) +{ + struct work_struct *work = data; + + cancel_work_sync(work); +} + +/** + * devm_init_work - resource-controlled version of INIT_WORK() + * @dev: valid struct device pointer + * @work: work pointer to initialize + * @func: work function to initialize 'work' with + * + * Initialize the work pointer just like INIT_WORK(), but use resource control + * to help ensure work is not left running or pending when dev is destroyed. + * + * Return: 0 on success, -errno on failure. + */ +int __must_check devm_init_work(struct device *dev, struct work_struct *work, + work_func_t func) +{ + INIT_WORK(work, func); + return devm_add_action(dev, devm_work_release, work); +} +EXPORT_SYMBOL_GPL(devm_init_work); + +static void devm_delayed_work_release(void *data) +{ + struct delayed_work *dw = data; + + cancel_delayed_work_sync(dw); +} + +/** + * devm_init_delayed_work - resource-controlled version of INIT_DELAYED_WORK() + * @dev: valid struct device pointer + * @dw: delayed_work pointer to initialize + * @func: work function to initialize 'dw' with + * + * Initialize the delayed_work pointer just like INIT_DELAYED_WORK(), but use + * resource control to help ensure delayed work is not left running or pending + * when dev is destroyed. + * + * Return: 0 on success, -errno on failure. + */ +int __must_check devm_init_delayed_work(struct device *dev, + struct delayed_work *dw, + work_func_t func) +{ + INIT_DELAYED_WORK(dw, func); + return devm_add_action(dev, devm_delayed_work_release, dw); +} +EXPORT_SYMBOL_GPL(devm_init_delayed_work); -- 2.17.1