From patchwork Wed Jun 28 09:49:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 113757 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp8804423vqr; Wed, 28 Jun 2023 03:06:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6RQpMq4fcawRQMY5P1WMQ/fOypIx6uSu8OakEJBeQqCePgZjr26r72y+ZFffD5sH7FifCA X-Received: by 2002:a05:6808:1513:b0:3a1:f36c:3fdb with SMTP id u19-20020a056808151300b003a1f36c3fdbmr4986889oiw.47.1687946803046; Wed, 28 Jun 2023 03:06:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687946803; cv=none; d=google.com; s=arc-20160816; b=RPSPAIZV6G4Wp9guYEZe6gzu8xqW60WEuT0GyRtrJDbJw8xei4MOwClTVDOf9MXwhi gQF9fMJpCJcowFpnhPY048hB5i+v2VSUwNyu+l083J5LmIO3XjuBDSagPSUUkQzUv+sp XY1II2VMNTeyteU33mlPMy2keTrZpRN+0OKAuQ6sAubNHzCAJOXUugJ1F1/EvG899X4F ZP+fLwAb3MJne18EUAdUF3HEUJ8kAUFrqkQ+fcGfGZiQeHSHpxXUed9Oq+2n6BhEsIDW jx3VtqqMh8j4N8oAD+6HJ92NrKc43TJJBDXwGqQJplmj+C3o2iGC9H/t4/xWU9g8l5Xl X4wA== 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=j0UEiwpIy08i/fJ5h3fdAK+WOgvZlFEoKmozExzUPgg=; fh=L/ZJaf37Ro1R9cy1Bn48Hmf1sYUdjUQuFU8816ag5K8=; b=PXcAAp9DDFqiaqmEy+ebFVB0XhtL85P21Mrb9llUyvLfycznrpJY3zIpDHnlziupUL rj9YZLFyyLeSWSMK+osFvT8mZsCharI0Ga08UBwtImFxgVKwMcYCZKw3WKhihxvZuQRk V8zTnZDduFgJhRiqra5Idp65AEHLH4M/8GbsjdDPJrYYaNxhwoZS8VQh0xQ/OkbmBs94 MfD9Gmaoa/9XBgSW4ya3FBvMbZeNv7TVPpCypk04H5QZg9Y2MCkRrj77SfOkjT/ScQjc 6Sj36aXX8dSMMdlnmg3MVk8WVy/fIUBUuugngZ60GnHhi1BfO+nGDDZT7fCU3eJva5jz acPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="bZZl1/WZ"; 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 m8-20020a17090b068800b0025ea4dd9071si8422409pjz.61.2023.06.28.03.06.15; Wed, 28 Jun 2023 03:06:43 -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="bZZl1/WZ"; 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 S234296AbjF1J5k (ORCPT + 99 others); Wed, 28 Jun 2023 05:57:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233514AbjF1Jy1 (ORCPT ); Wed, 28 Jun 2023 05:54:27 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E42330FF; Wed, 28 Jun 2023 02:49:54 -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 1B5746126D; Wed, 28 Jun 2023 09:49:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8557C433C8; Wed, 28 Jun 2023 09:49:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687945793; bh=xbWw7cmsOxiNqVKh5JAAK/jC3NoD/LahpRuwtqAGc04=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bZZl1/WZiWc8fVhnPFkBZX2PNzvyJSk3n7A29f8e22BRjm5NR8mU3eMPTKxoxgwmu 0TTbiimdwFgrz+Bo7R3bQ/45QqBHV0yzpGIDxXS6giYJVvr+gSoiy2qxkS11IsYDFC lW+lCz1kHWlXTku3OjAN5bIz0qTO10aPsYZ5yP9spwHmxGIJlESuXkIEs2ilGOrlTa +UwUAcJ2mnbo7w1tEhCgdvMmfyZEgunuZLHB37lR9BJnfH8fwa5/XktmlGzEjovC3c 8PiPX2esh/3iEIqsVPsK7JhjQn05tG0rHjxFpD9CDJfDRt/bRha0Eaf2A7NxjfQlwV DB4pkP5M9HvEg== From: Maxime Ripard Date: Wed, 28 Jun 2023 11:49:21 +0200 Subject: [PATCH v2 1/3] drivers: base: Add basic devm tests for root devices MIME-Version: 1.0 Message-Id: <20230329-kunit-devm-inconsistencies-test-v2-1-19feb71e864b@kernel.org> References: <20230329-kunit-devm-inconsistencies-test-v2-0-19feb71e864b@kernel.org> In-Reply-To: <20230329-kunit-devm-inconsistencies-test-v2-0-19feb71e864b@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.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4911; i=mripard@kernel.org; h=from:subject:message-id; bh=xbWw7cmsOxiNqVKh5JAAK/jC3NoD/LahpRuwtqAGc04=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDClzmDQkb9WZG7smsUSKHpt8UuuGno2bvp8Fu1XV0WcNm xJ8Lq/uKGVhEONikBVTZIkRNl8Sd2rW6042vnkwc1iZQIYwcHEKwESmJTD8TysOmNJU56rDOj2o zmvxEenVW159aruRL912deP15fpq1YwMn9+sm1R2f5Ukl+bmy1E/ko/NSU01X2Uet4I5Ujen704 xIwA= X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D 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_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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769940506802131924?= X-GMAIL-MSGID: =?utf-8?q?1769940506802131924?= 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. Signed-off-by: Maxime Ripard Reviewed-by: David Gow --- 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);