From patchwork Thu Jul 20 12:45:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 123251 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp3096981vqt; Thu, 20 Jul 2023 05:58:54 -0700 (PDT) X-Google-Smtp-Source: APBJJlF1iLizilyGoWmcpQL9VeATyr7Wm8QYZy10Pfxy0WsJfZjrqysY7mD/+kzOou+3mTCaPxh3 X-Received: by 2002:a17:906:518b:b0:994:1fee:3065 with SMTP id y11-20020a170906518b00b009941fee3065mr6064030ejk.15.1689857934229; Thu, 20 Jul 2023 05:58:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689857934; cv=none; d=google.com; s=arc-20160816; b=GL73aOoWXMmAXsIvU3lLupX2f3kjq39qx7M1e/PrGIsHpfhnh7dkhqUtuU89+FZ4FK houzwQ4frH6S0CH9yGn0+0lFXg2qkHwEBBajn73ceEz7sYsFMecnzafxXDO+MLPmzb0M aNKt4/yVGECFy+fs2mC3HTeMB6k2YWHPZK62kEdGPPwl1Zwxs/KVUbFzSvtFm2WtM/5D Wts5nHDkH9sCRAirLTeFKMySw/qQid8AP4c/9xDsyNNzILpLkLYaQ2Ek3/FCyk9qIPS2 AGbOK6nhaWlnI12OJuG0F3TQeC3pvdSdJFOyD0UtYAxDWRtduuEneKxrhXatmS0D4Sea EzgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=BeFYDrFeNhbNv5fbs9quH2+QW/ZfHklHrCW15+Y8VWI=; fh=tmUio+7MS/EkQoXm7KwEO9jjpqLQersHC1z4ZawnwsI=; b=ol7soBeHKVPfC6/OmJkNnSgQVeqqrOSc/aCpH/MLfCUHnCNus8Q+mhJvUB7rlJM+Ge zuFyJCGUNBib+9OAXNNXyXG8gDxh4ZKmd6anGpqdk+teh0mp2zvFLG02NATWJ2jDvmJH XI4/7DPTWml0FLLPJFiZDvxC1FHTLzXLDLLbod1FnH4Q0wjHCr0YUNF/N/8LEbDou2o9 /L7BmHu2OHxgoIini2anq8YXr9B7ETZ6mLjXvKFdPXgkvSorLQ9meGdw4JgK7x75PXRF Ts/ckskB/8ePbzT1NNKwPB8XAQYhQLiPq2KIDCVnD78QbV6OgOMgAIyg9b/n7of3BVu0 TljQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Y3WHwurC; 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=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f10-20020a170906084a00b0096f4deef5basi611151ejd.649.2023.07.20.05.58.30; Thu, 20 Jul 2023 05:58:54 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=Y3WHwurC; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231497AbjGTMpb (ORCPT + 99 others); Thu, 20 Jul 2023 08:45:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229703AbjGTMpZ (ORCPT ); Thu, 20 Jul 2023 08:45:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34D6D26A9; Thu, 20 Jul 2023 05:45:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B6E4361A8B; Thu, 20 Jul 2023 12:45:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9D9CDC433C8; Thu, 20 Jul 2023 12:45:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689857119; bh=8IOySnpTmyXSWuK7L3QmCKUbkD1t8RLwEFPQIb+aA90=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Y3WHwurCpZ0welbIc4KXl0Xeh9ELtXr67OKJBxPyeK6mFXR9Ym8GS+QGAfWcnkzkL c3h8Rvmv7y0oOKZxVd1OjNhJejc/ifFgkdUj21PlYCEGDF1HbQgX6lvANZmkmRv1fJ EHFii4cXMk+hUgG5Uym+z+KiGubGBRs7wX3dZnbC0E5pzOJo9RQ4SnhjMuUpAfNdOQ 6GpkcxbRvHAWPpGjLe1cmaWNWBErCkcInabqBy/KHP+2dKuMRy31XyE86Xz59CYAiB JSs3xjuV45ksTQIMtu6ESDAHDNGh7aEEzjP3Z64QV2Ipr0N46YIy9tsK1VnNe4Fnkb zwVuo6/2XAe7Q== From: Maxime Ripard Date: Thu, 20 Jul 2023 14:45:07 +0200 Subject: [PATCH v3 1/3] drivers: base: Add basic devm tests for root devices MIME-Version: 1.0 Message-Id: <20230720-kunit-devm-inconsistencies-test-v3-1-6aa7e074f373@kernel.org> References: <20230720-kunit-devm-inconsistencies-test-v3-0-6aa7e074f373@kernel.org> In-Reply-To: <20230720-kunit-devm-inconsistencies-test-v3-0-6aa7e074f373@kernel.org> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Brendan Higgins , David Gow , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Maxime Ripard X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=5424; i=mripard@kernel.org; h=from:subject:message-id; bh=8IOySnpTmyXSWuK7L3QmCKUbkD1t8RLwEFPQIb+aA90=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDCk7dSIcznNsPz63S5mXKS32vVN/l6qZGcvFj+ql37mSu T7EsYd0lLIwiHExyIopssQImy+JOzXrdScb3zyYOaxMIEMYuDgFYCKv1BgZXkytyJzWcKvDmrO8 PNs0Z83r/y/O3tjz3XrlkvfbWAMV7BkZzhnETwyquMpiU21iZVV5banKD6UNFu0t8sJ7J4j92yH CBwA= X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771944472846785670 X-GMAIL-MSGID: 1771944472846785670 The root devices show some odd behaviours compared to regular "bus" devices that have been probed through the usual mechanism, so let's create kunit tests to exercise those paths and odd cases. It's not clear whether root devices are even allowed to use device managed resources, but the fact that it works in some cases but not others like shown in that test suite shouldn't happen either way: we want to make it consistent and documented. These tests will (after the following patches) ensure that consistency and effectively document that it's allowed. If it ever turns out to be a bad idea, we can always roll back and modify the tests then. Reviewed-by: David Gow Signed-off-by: Maxime Ripard --- drivers/base/test/.kunitconfig | 2 + drivers/base/test/Kconfig | 4 ++ drivers/base/test/Makefile | 2 + drivers/base/test/root-device-test.c | 110 +++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) diff --git a/drivers/base/test/.kunitconfig b/drivers/base/test/.kunitconfig new file mode 100644 index 000000000000..473923f0998b --- /dev/null +++ b/drivers/base/test/.kunitconfig @@ -0,0 +1,2 @@ +CONFIG_KUNIT=y +CONFIG_DM_KUNIT_TEST=y diff --git a/drivers/base/test/Kconfig b/drivers/base/test/Kconfig index 610a1ba7a467..9d42051f8f8e 100644 --- a/drivers/base/test/Kconfig +++ b/drivers/base/test/Kconfig @@ -9,6 +9,10 @@ config TEST_ASYNC_DRIVER_PROBE If unsure say N. +config DM_KUNIT_TEST + tristate "KUnit Tests for the device model" if !KUNIT_ALL_TESTS + depends on KUNIT + config DRIVER_PE_KUNIT_TEST bool "KUnit Tests for property entry API" if !KUNIT_ALL_TESTS depends on KUNIT=y diff --git a/drivers/base/test/Makefile b/drivers/base/test/Makefile index 7f76fee6f989..d589ca3fa8fc 100644 --- a/drivers/base/test/Makefile +++ b/drivers/base/test/Makefile @@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE) += test_async_driver_probe.o +obj-$(CONFIG_DM_KUNIT_TEST) += root-device-test.o + obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN) diff --git a/drivers/base/test/root-device-test.c b/drivers/base/test/root-device-test.c new file mode 100644 index 000000000000..9a3e6cccae13 --- /dev/null +++ b/drivers/base/test/root-device-test.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright 2023 Maxime Ripard + +#include + +#include + +#define DEVICE_NAME "test" + +struct test_priv { + bool probe_done; + bool release_done; + wait_queue_head_t release_wq; + struct device *dev; +}; + +static int root_device_devm_init(struct kunit *test) +{ + struct test_priv *priv; + + priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv); + init_waitqueue_head(&priv->release_wq); + + test->priv = priv; + + return 0; +} + +static void devm_device_action(void *ptr) +{ + struct test_priv *priv = ptr; + + priv->release_done = true; + wake_up_interruptible(&priv->release_wq); +} + +#define RELEASE_TIMEOUT_MS 100 + +/* + * Tests that a bus-less, non-probed device will run its device-managed + * actions when unregistered. + */ +static void root_device_devm_register_unregister_test(struct kunit *test) +{ + struct test_priv *priv = test->priv; + int ret; + + priv->dev = root_device_register(DEVICE_NAME); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev); + + ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv); + KUNIT_ASSERT_EQ(test, ret, 0); + + root_device_unregister(priv->dev); + + ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done, + msecs_to_jiffies(RELEASE_TIMEOUT_MS)); + KUNIT_EXPECT_GT(test, ret, 0); +} + +static void devm_put_device_action(void *ptr) +{ + struct test_priv *priv = ptr; + + put_device(priv->dev); + priv->release_done = true; + wake_up_interruptible(&priv->release_wq); +} + +/* + * Tests that a bus-less, non-probed device will run its device-managed + * actions when unregistered, even if someone still holds a reference to + * it. + */ +static void root_device_devm_register_get_unregister_with_devm_test(struct kunit *test) +{ + struct test_priv *priv = test->priv; + int ret; + + kunit_skip(test, "This needs to be fixed in the core."); + + priv->dev = root_device_register(DEVICE_NAME); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev); + + get_device(priv->dev); + + ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv); + KUNIT_ASSERT_EQ(test, ret, 0); + + root_device_unregister(priv->dev); + + ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done, + msecs_to_jiffies(RELEASE_TIMEOUT_MS)); + KUNIT_EXPECT_GT(test, ret, 0); +} + +static struct kunit_case root_device_devm_tests[] = { + KUNIT_CASE(root_device_devm_register_unregister_test), + KUNIT_CASE(root_device_devm_register_get_unregister_with_devm_test), + {} +}; + +static struct kunit_suite root_device_devm_test_suite = { + .name = "root-device-devm", + .init = root_device_devm_init, + .test_cases = root_device_devm_tests, +}; + +kunit_test_suite(root_device_devm_test_suite); From patchwork Thu Jul 20 12:45:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 123259 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp3111068vqt; Thu, 20 Jul 2023 06:16:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlEw5s6e3r04CPN6bSH5uS/WGJJWzDTdEshrnVTzScmFeOomTilpQ1BIiUm9ImjogV2uexPo X-Received: by 2002:a17:902:da81:b0:1b8:c828:f742 with SMTP id j1-20020a170902da8100b001b8c828f742mr6528118plx.14.1689859017219; Thu, 20 Jul 2023 06:16:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689859017; cv=none; d=google.com; s=arc-20160816; b=ZJK+x80RzX8CCSS45TI8kKeTVp2L2mEP1sKpswfZWGsYtQsbj2RcwajDs2BAcjLAvd jgiRtAqHHfb6eDpDrhMP7P0tKZbgfW7/IwkS0XoZfXgqeHTGRddi2FitzPRsGWI8qupA kdHiBjPxdrFyT0HZwFEwBqGACwHjflCa4yVqxRqTOkK7Cx/nELu49xE+Rn/v/dSE3xdR 1rwd432nEc1IITTclpCr6f8xWKEqRlJvjky3Ct7/X2d+uT94+KRj3pAel7lK4NQOKkBm 2GiJcrX5qzBgp5d2WbffvR2iKCPPTuyzuPDUscC8/YgUBFY1wpTUs7PIbx7/h16YZqRS bkAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=ag0ExgYj3W4P+c5qbUc0P8lZ4FdvgjlOLlLBBfFu+ro=; fh=tmUio+7MS/EkQoXm7KwEO9jjpqLQersHC1z4ZawnwsI=; b=yU94djB4giZN9VQoTUjpfwSCN1aUWgdpD3acby+tkEtOmWZcILXl1dnjqO6AXAmjJX 6/tRrz/RpD1+L8VZMq3pywe3XHTGnYENU9nRx9oPFZGibB6pZ9tOksSlhPeF9C05RXnF DtY9g+8T9K3qc1oiu7ybtv7XutQEsSiFGN9ewrx9GUmIgs5nNh/U1qAs//SHyN+QIqX7 9Xm9OLBnC8k9ZkM3mDpf65vsHv43hW8UeJS5cUkd4zO+6R/JAylrAYk0NhvDSJt9+kXd 1coNg8ewGrDzT5nBbvGlyldkslOEfVXMEnzl9oCexMkzkjUZPo2t7UIKonoRb8+4Qf+b z/jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rjNy7fks; 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=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b2-20020a170902ed0200b001aaf639c4easi929388pld.109.2023.07.20.06.16.37; Thu, 20 Jul 2023 06:16:57 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=rjNy7fks; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231628AbjGTMpr (ORCPT + 99 others); Thu, 20 Jul 2023 08:45:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231462AbjGTMpa (ORCPT ); Thu, 20 Jul 2023 08:45:30 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B1242690; Thu, 20 Jul 2023 05:45:23 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 96F0961A77; Thu, 20 Jul 2023 12:45:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 803C7C433C7; Thu, 20 Jul 2023 12:45:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689857121; bh=jh5HlgHr0tntYKi2YKW8GWjCSxPgRIdKfqKt13WVbd4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rjNy7fkseKFpz7WLroBf2VO4giyz1i9dlNUN5sRveyRv2IrTI6VrR2VLzirvxbl0h UTVjg3vw6k5vK5IELq5tUx3kr9yJoYIlCwoUHdnpFgd+pqmnof4teNtXDaEbAF1BVD Os3bU+sVTBZVTDTMT0HWHy2brPJbcsogcXkNrRlXZ3818rxb78pG3VJNykqV5sVbCb lfH/uPg+JRqLPRir5SyGHGJI2SrXnsG2IIFxPwIVS53ue8aa2MeLlqg3FF8mrkgWjU 5lg60kk2CHtO7R/VoHHX/D7r2pMYVhKNBjq+hzti+OuLPnc/XgwyY1DXmQREOGNDek pM45waRGfhR5g== From: Maxime Ripard Date: Thu, 20 Jul 2023 14:45:08 +0200 Subject: [PATCH v3 2/3] drivers: base: Add basic devm tests for platform devices MIME-Version: 1.0 Message-Id: <20230720-kunit-devm-inconsistencies-test-v3-2-6aa7e074f373@kernel.org> References: <20230720-kunit-devm-inconsistencies-test-v3-0-6aa7e074f373@kernel.org> In-Reply-To: <20230720-kunit-devm-inconsistencies-test-v3-0-6aa7e074f373@kernel.org> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Brendan Higgins , David Gow , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Maxime Ripard X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=8336; i=mripard@kernel.org; h=from:subject:message-id; bh=jh5HlgHr0tntYKi2YKW8GWjCSxPgRIdKfqKt13WVbd4=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDCk7dSI3BLfk+fxk/ypyR9PxRNux3k1cpr9tv+TcOFzXM 5dNaxdfRykLgxgXg6yYIkuMsPmSuFOzXney8c2DmcPKBDKEgYtTACZiGMvwVzK66IDd1dYfq/Nb Go99vJnqefhKaMyO0K8XrEpeXnux4gHDP6trv2ZKT5199dhnniV/K4W4P6zJbb8rlT27kb2ZzaH 8HC8A X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771945608484516120 X-GMAIL-MSGID: 1771945608484516120 Platform devices show some inconsistencies with how devm resources are released when the device has been probed and when it hasn't. In particular, we can register device-managed actions no matter if the device has be bound to a driver or not, but devres_release_all() will only be called if it was bound to a driver or if there's no reference held to it anymore. If it wasn't bound to a driver and we still have a reference, devres_release_all() will never get called. This is surprising considering that if the driver isn't bound but doesn't have any reference to it anymore, that function will get called, and if it was bound to a driver but still has references, that function will get called as well. Even if that case is fairly unusual, it can easily lead to memory leaks. The plan is, with the next patch, to make it consistent and enforce that devres_release_all() is called no matter what situation we're in. For now, it just tests for the current behaviour and skips over the inconsistencies. Reviewed-by: David Gow Signed-off-by: Maxime Ripard --- drivers/base/test/Makefile | 1 + drivers/base/test/platform-device-test.c | 222 +++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+) diff --git a/drivers/base/test/Makefile b/drivers/base/test/Makefile index d589ca3fa8fc..e321dfc7e922 100644 --- a/drivers/base/test/Makefile +++ b/drivers/base/test/Makefile @@ -2,6 +2,7 @@ obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE) += test_async_driver_probe.o obj-$(CONFIG_DM_KUNIT_TEST) += root-device-test.o +obj-$(CONFIG_DM_KUNIT_TEST) += platform-device-test.o obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN) diff --git a/drivers/base/test/platform-device-test.c b/drivers/base/test/platform-device-test.c new file mode 100644 index 000000000000..b6ebf1dcdffb --- /dev/null +++ b/drivers/base/test/platform-device-test.c @@ -0,0 +1,222 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +#include +#include + +#define DEVICE_NAME "test" + +struct test_priv { + bool probe_done; + bool release_done; + wait_queue_head_t probe_wq; + wait_queue_head_t release_wq; + struct device *dev; +}; + +static int platform_device_devm_init(struct kunit *test) +{ + struct test_priv *priv; + + priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv); + init_waitqueue_head(&priv->probe_wq); + init_waitqueue_head(&priv->release_wq); + + test->priv = priv; + + return 0; +} + +static void devm_device_action(void *ptr) +{ + struct test_priv *priv = ptr; + + priv->release_done = true; + wake_up_interruptible(&priv->release_wq); +} + +static void devm_put_device_action(void *ptr) +{ + struct test_priv *priv = ptr; + + put_device(priv->dev); + priv->release_done = true; + wake_up_interruptible(&priv->release_wq); +} + +#define RELEASE_TIMEOUT_MS 100 + +/* + * Tests that a platform bus, non-probed device will run its + * device-managed actions when unregistered. + */ +static void platform_device_devm_register_unregister_test(struct kunit *test) +{ + struct platform_device *pdev; + struct test_priv *priv = test->priv; + int ret; + + pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + + ret = platform_device_add(pdev); + KUNIT_ASSERT_EQ(test, ret, 0); + + priv->dev = &pdev->dev; + + ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv); + KUNIT_ASSERT_EQ(test, ret, 0); + + platform_device_unregister(pdev); + + ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done, + msecs_to_jiffies(RELEASE_TIMEOUT_MS)); + KUNIT_EXPECT_GT(test, ret, 0); +} + +/* + * Tests that a platform bus, non-probed device will run its + * device-managed actions when unregistered, even if someone still holds + * a reference to it. + */ +static void platform_device_devm_register_get_unregister_with_devm_test(struct kunit *test) +{ + struct platform_device *pdev; + struct test_priv *priv = test->priv; + int ret; + + kunit_skip(test, "This needs to be fixed in the core."); + + pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + + ret = platform_device_add(pdev); + KUNIT_ASSERT_EQ(test, ret, 0); + + priv->dev = &pdev->dev; + + get_device(priv->dev); + + ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv); + KUNIT_ASSERT_EQ(test, ret, 0); + + platform_device_unregister(pdev); + + ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done, + msecs_to_jiffies(RELEASE_TIMEOUT_MS)); + KUNIT_EXPECT_GT(test, ret, 0); +} + +static int fake_probe(struct platform_device *pdev) +{ + struct test_priv *priv = platform_get_drvdata(pdev); + + priv->probe_done = true; + wake_up_interruptible(&priv->probe_wq); + + return 0; +} + +static struct platform_driver fake_driver = { + .probe = fake_probe, + .driver = { + .name = DEVICE_NAME, + }, +}; + +/* + * Tests that a platform bus, probed device will run its device-managed + * actions when unregistered. + */ +static void probed_platform_device_devm_register_unregister_test(struct kunit *test) +{ + struct platform_device *pdev; + struct test_priv *priv = test->priv; + int ret; + + ret = platform_driver_register(&fake_driver); + KUNIT_ASSERT_EQ(test, ret, 0); + + pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + + priv->dev = &pdev->dev; + platform_set_drvdata(pdev, priv); + + ret = platform_device_add(pdev); + KUNIT_ASSERT_EQ(test, ret, 0); + + ret = wait_event_interruptible_timeout(priv->probe_wq, priv->probe_done, + msecs_to_jiffies(RELEASE_TIMEOUT_MS)); + KUNIT_ASSERT_GT(test, ret, 0); + + ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv); + KUNIT_ASSERT_EQ(test, ret, 0); + + platform_device_unregister(pdev); + + ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done, + msecs_to_jiffies(RELEASE_TIMEOUT_MS)); + KUNIT_EXPECT_GT(test, ret, 0); + + platform_driver_unregister(&fake_driver); +} + +/* + * Tests that a platform bus, probed device will run its device-managed + * actions when unregistered, even if someone still holds a reference to + * it. + */ +static void probed_platform_device_devm_register_get_unregister_with_devm_test(struct kunit *test) +{ + struct platform_device *pdev; + struct test_priv *priv = test->priv; + int ret; + + ret = platform_driver_register(&fake_driver); + KUNIT_ASSERT_EQ(test, ret, 0); + + pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + + priv->dev = &pdev->dev; + platform_set_drvdata(pdev, priv); + + ret = platform_device_add(pdev); + KUNIT_ASSERT_EQ(test, ret, 0); + + ret = wait_event_interruptible_timeout(priv->probe_wq, priv->probe_done, + msecs_to_jiffies(RELEASE_TIMEOUT_MS)); + KUNIT_ASSERT_GT(test, ret, 0); + + get_device(priv->dev); + + ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv); + KUNIT_ASSERT_EQ(test, ret, 0); + + platform_device_unregister(pdev); + + ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done, + msecs_to_jiffies(RELEASE_TIMEOUT_MS)); + KUNIT_EXPECT_GT(test, ret, 0); + + platform_driver_unregister(&fake_driver); +} + +static struct kunit_case platform_device_devm_tests[] = { + KUNIT_CASE(platform_device_devm_register_unregister_test), + KUNIT_CASE(platform_device_devm_register_get_unregister_with_devm_test), + KUNIT_CASE(probed_platform_device_devm_register_unregister_test), + KUNIT_CASE(probed_platform_device_devm_register_get_unregister_with_devm_test), + {} +}; + +static struct kunit_suite platform_device_devm_test_suite = { + .name = "platform-device-devm", + .init = platform_device_devm_init, + .test_cases = platform_device_devm_tests, +}; + +kunit_test_suite(platform_device_devm_test_suite); From patchwork Thu Jul 20 12:45:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 123269 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp3118118vqt; Thu, 20 Jul 2023 06:27:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlGi7172jSn3YQH0TE4rRZDTC0MpO7Zem4/2FVO4BJ+9Rp3vRas3seuRKDyUUgWlRHEZN/LO X-Received: by 2002:a17:902:a589:b0:1b8:95fc:d12 with SMTP id az9-20020a170902a58900b001b895fc0d12mr4790465plb.54.1689859676960; Thu, 20 Jul 2023 06:27:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689859676; cv=none; d=google.com; s=arc-20160816; b=fK1lltTJmqI+8NZbGE0lPmBvlYCqjBcj+hhdVfeklVhdBKmEuPEX20Llia89yzDI1T 7ZzdIBkIblOuiiB8KYbROy5rpqaf1Bj9cvqbQDT6dRO4W7RYpHMn+35uSPvTRzZGLkO/ 8ZCV086HR3M+MP1JXs+5dNlU6Apg+a7aBinnMW/8yp+IGBVl+u8nPeg0rgyTnTEkbUNw XXOyyrhe+Pa1HtW96MnB6DV0AorAUqQFeffDSBpS9sAuY3R6w/efMn5gYAG9n94kyKHZ LIlZgIZQ69EqOOyzz183OHpMoe2tsTH504dcZwk3QAHCpFyOfD7L6MeHjb8U02mEH9kE tamg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=xTdWYqnAP/mWMHqAQAvGeCLXoEC9T5VtJJEC0JU2J2g=; fh=tmUio+7MS/EkQoXm7KwEO9jjpqLQersHC1z4ZawnwsI=; b=sq9ZCMVDTMLWrT9W0waK5GUYUpfZ1CkD71iEOaBDGudwfd6nHBJHmHHl1whLYouPha ZKsCRsTcKLEhJm2DfAl3D3drl+bgUB9xgqLOPZ0mT+nAcNcNtzeNzFWnDiw/JjrrPcK1 qKXAXgEtAFS+aPfNPKP5UKM+k7ZS8ttK/QsdlzzP8qEnx3GkxlME9nuITsbFpoBTyvOV CaBJ/Mm6vH+qapQTwGMn9cTj+kpxV2A4+4DSULhUeEhdcv9ZNhqB0XhPaViFwYA513LU DazAHkMZTFwS35ARwdrcfrJfYVMhmUz7kHQXpdP1OgzWz5qIUw9yoeCmjeM5ommh2WPw 1LFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Ati3GUNI; 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=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o9-20020a170903210900b001b8a897cd35si831601ple.489.2023.07.20.06.27.43; Thu, 20 Jul 2023 06:27:56 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=Ati3GUNI; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231533AbjGTMpu (ORCPT + 99 others); Thu, 20 Jul 2023 08:45:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231138AbjGTMpf (ORCPT ); Thu, 20 Jul 2023 08:45:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC6B726A5; Thu, 20 Jul 2023 05:45:25 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 46A8061A3D; Thu, 20 Jul 2023 12:45:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E1BFC433C7; Thu, 20 Jul 2023 12:45:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689857124; bh=/t17Dw8Ji4dvYfH3v5c7ic+nA+UyzWEDYc0SP3u2a0E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Ati3GUNIpz+rGEpQ502H4eeDTtqI8lLdRbIv4/5aYKCZRupFSRUez0RG6V7W9MJaG SILac4zUP+3cEN+9nqYlVrtMDg1xgRbYSfA5U6UvUnvTREfn9jawn1cC1m9lA4m30y pE6Z2Cg6i7JoN4v5pLFN3Qcc/38UbX4856fsNjmoSm4BWBTPVHloBpO+r7rEks5HaP cxrgwMi0gxIFc2is8wC0tVbMar3QEm0oP4M6+iBwg4LoR9jvCbVJOs6ZD8omTjYYIW hTeXlTg9pw4ktrcsdwG0kx2PLM4j2nRlESsELL5e6kp4mn5tv2TaO71bfJkV4+g4vw Flpdc3feFmcbA== From: Maxime Ripard Date: Thu, 20 Jul 2023 14:45:09 +0200 Subject: [PATCH v3 3/3] drivers: base: Free devm resources when unregistering a device MIME-Version: 1.0 Message-Id: <20230720-kunit-devm-inconsistencies-test-v3-3-6aa7e074f373@kernel.org> References: <20230720-kunit-devm-inconsistencies-test-v3-0-6aa7e074f373@kernel.org> In-Reply-To: <20230720-kunit-devm-inconsistencies-test-v3-0-6aa7e074f373@kernel.org> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Brendan Higgins , David Gow , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Maxime Ripard X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3344; i=mripard@kernel.org; h=from:subject:message-id; bh=dK5NenGOGX4zkAO7TDHxkXUxqTtkK1TPL9bi3DSmzKI=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDCk7dSK10souHee+q/LaRWFGoYlAl3evuuubNG63G+yyl +4Yhyp0lLIwiHExyIopssQImy+JOzXrdScb3zyYOaxMIEMYuDgFYCIydxkZzky9WXDY9Oq8kFeH TDOuP65/bx3Oe6rEPVE9anmxxpLIjYwM557FzFxhKhxkzbP8Mf99YauS2ttWnRzFrr8rNmv7HHj MDgA= X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771946300563567480 X-GMAIL-MSGID: 1771946300563567480 From: David Gow In the current code, devres_release_all() only gets called if the device has a bus and has been probed. This leads to issues when using bus-less or driver-less devices where the device might never get freed if a managed resource holds a reference to the device. This is happening in the DRM framework for example. We should thus call devres_release_all() in the device_del() function to make sure that the device-managed actions are properly executed when the device is unregistered, even if it has neither a bus nor a driver. This is effectively the same change than commit 2f8d16a996da ("devres: release resources on device_del()") that got reverted by commit a525a3ddeaca ("driver core: free devres in device_release") over memory leaks concerns. This patch effectively combines the two commits mentioned above to release the resources both on device_del() and device_release() and get the best of both worlds. Fixes: a525a3ddeaca ("driver core: free devres in device_release") Signed-off-by: David Gow Signed-off-by: Maxime Ripard --- drivers/base/core.c | 11 +++++++++++ drivers/base/test/platform-device-test.c | 2 -- drivers/base/test/root-device-test.c | 2 -- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 3dff5037943e..6ceaf50f5a67 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -3817,6 +3817,17 @@ void device_del(struct device *dev) device_platform_notify_remove(dev); device_links_purge(dev); + /* + * If a device does not have a driver attached, we need to clean + * up any managed resources. We do this in device_release(), but + * it's never called (and we leak the device) if a managed + * resource holds a reference to the device. So release all + * managed resources here, like we do in driver_detach(). We + * still need to do so again in device_release() in case someone + * adds a new resource after this point, though. + */ + devres_release_all(dev); + bus_notify(dev, BUS_NOTIFY_REMOVED_DEVICE); kobject_uevent(&dev->kobj, KOBJ_REMOVE); glue_dir = get_glue_dir(dev); diff --git a/drivers/base/test/platform-device-test.c b/drivers/base/test/platform-device-test.c index b6ebf1dcdffb..1ae5ce8bd366 100644 --- a/drivers/base/test/platform-device-test.c +++ b/drivers/base/test/platform-device-test.c @@ -87,8 +87,6 @@ static void platform_device_devm_register_get_unregister_with_devm_test(struct k struct test_priv *priv = test->priv; int ret; - kunit_skip(test, "This needs to be fixed in the core."); - pdev = platform_device_alloc(DEVICE_NAME, PLATFORM_DEVID_NONE); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); diff --git a/drivers/base/test/root-device-test.c b/drivers/base/test/root-device-test.c index 9a3e6cccae13..780d07455f57 100644 --- a/drivers/base/test/root-device-test.c +++ b/drivers/base/test/root-device-test.c @@ -78,8 +78,6 @@ static void root_device_devm_register_get_unregister_with_devm_test(struct kunit struct test_priv *priv = test->priv; int ret; - kunit_skip(test, "This needs to be fixed in the core."); - priv->dev = root_device_register(DEVICE_NAME); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);