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); From patchwork Wed Jun 28 09:49:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 113763 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp8805387vqr; Wed, 28 Jun 2023 03:08:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ69RVsVXOPKq5ksE3OHSoO9YvIxjIE4PUhVd8Uh4sVoW6uDT2+a3/PnpzQEyuJsfVxHhy0L X-Received: by 2002:a05:6a20:12cc:b0:126:d0e2:3fb4 with SMTP id v12-20020a056a2012cc00b00126d0e23fb4mr12220586pzg.56.1687946897539; Wed, 28 Jun 2023 03:08:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687946897; cv=none; d=google.com; s=arc-20160816; b=XTdy3Vx+Kah63vZ+K/NDueB1hZGQ7itBgGZXrWTsM+LiH/YGy/L8RKVWxO+AL7iYOn rv2+drXV6rILcltZn+K3B7R6XsjXUij90TZoB8xKfIR7+yTjuyUWrcs3Hx/piWGMwKAN mtwVLbdZnfwKkIntqOy4pSuZoGMex54yW7cuEzMmqLWip4F/DEfFT2WXur79bQDA+COj lyeyVeo4LEe5xgPSoZnZ28SYe7KXY+Uc4iPR7VlRzyYGkhxbsYw5CHgjaQ9McxkT7Pc5 Z30KBFRQuG3PvFpQC/AqJJdNj3aeUK593qaGTFDwo3MQ0eRU+6AfNLaP7eSr6EkpnCIn e3yg== 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=h8mLRh+CtthRbVEVI0SERMc9Ha62v1R77Im9LmBKJCg=; fh=L/ZJaf37Ro1R9cy1Bn48Hmf1sYUdjUQuFU8816ag5K8=; b=tZZunnPz5onJ+u+899CgjvMCGL19z94fnnMBWP8bxHt7oLwxY7EJmUpdQwNbpxztRY uSD2QO6wzMN1/mbV9Bs4/N6n+KsWxi4/obyBGl1giGn1HPVnXmSy2Gdssu7wvb71kvC6 FBAFdo2RzvPe1ogy4YEL7PMOYPFYXjN2nVRF6bI3M/pZKleKtLXNAHYNf/1xpmQAdGgW nsdcAu4hiaU8UujMDgKvhRkn6cJTZqFlS19w5KGp+fKj9ZuA1JF6Fz/vn48qKW6zBCIe ZsvPp7KhB5QSuc/Wia67A/W+wvKf9qJryAKfY8TEIL49XEcaVSANAE9lngrDCY5KlDfa 7Xgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dzxTK2gY; 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 bs190-20020a6328c7000000b0054f81c81d98si8645978pgb.194.2023.06.28.03.07.48; Wed, 28 Jun 2023 03:08:17 -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=dzxTK2gY; 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 S233436AbjF1JyS (ORCPT + 99 others); Wed, 28 Jun 2023 05:54:18 -0400 Received: from dfw.source.kernel.org ([139.178.84.217]:36840 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232225AbjF1Jt6 (ORCPT ); Wed, 28 Jun 2023 05:49:58 -0400 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 CFC16612A1; Wed, 28 Jun 2023 09:49:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A299BC433C8; Wed, 28 Jun 2023 09:49:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687945797; bh=rZcqaoz/CBdF7GrtJ5iSysEDcLNhugfhSPfKQxyhYBo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dzxTK2gY0pUHneftUazB97SJMeTvijaJCQNQ6oHRRZNgcmc7BD1n7LJOguGtPZJxC c6bJlRAD8BNEGjUs2H0M3cRqHoHz2shCMndFokSXBJA6oRrdi/7wcMgUTKoT7DJXZ9 JHU5pm/nuVIIASG6NGhSOD0tw60DikNSc41p7EGOXFbN5WSmxDbXQszj9zlB5YKh65 Zv3oziHJSeG6VBAgT22a3qBwTxEwdu64ArcrVHt9xW+9NzteQ5mc5eq7VR3/6Hb1df YAOkJRureIjaVNLdTble5nwY2ZlHdLVARNcF9su6Dzeto1+E0FpB9L2mhdopPVdSQ+ NvDbLPcyM9Fyw== From: Maxime Ripard Date: Wed, 28 Jun 2023 11:49:22 +0200 Subject: [PATCH v2 2/3] drivers: base: Add basic devm tests for platform devices MIME-Version: 1.0 Message-Id: <20230329-kunit-devm-inconsistencies-test-v2-2-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=7457; i=mripard@kernel.org; h=from:subject:message-id; bh=rZcqaoz/CBdF7GrtJ5iSysEDcLNhugfhSPfKQxyhYBo=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDClzmDR0Jl9Yy6doWt7D/15tWmZXhn9QbE3Ycn3d1crbH loZdwh0lLIwiHExyIopssQImy+JOzXrdScb3zyYOaxMIEMYuDgFYCJFMgz/46RjBRw+pxmcEHKq bWI63uoVd/76uWlpDtHOTS/mn/T5ysgw80f4xImLrjLpcT1ZnH+p+hmXQIvPVLsXuQovS9Z4K9/ gBwA= X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D 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?1769940606290139762?= X-GMAIL-MSGID: =?utf-8?q?1769940606290139762?= Platform devices show some inconsistencies with how devm resources are released when the device has been probed and when it hasn't. Let's add a few tests to exercise thos paths and odd cases. Signed-off-by: Maxime Ripard Reviewed-by: David Gow --- 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 Wed Jun 28 09:49:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 113760 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp8804840vqr; Wed, 28 Jun 2023 03:07:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4qVp0EQ/0Ei8Rs7PvpbIk2V/wX3SWP4bwcMgUl4Qk9+zWLQOcw6v+UOH4YhifbLj8Zp/JH X-Received: by 2002:a17:902:ecd1:b0:1b0:6e16:b92c with SMTP id a17-20020a170902ecd100b001b06e16b92cmr14131732plh.54.1687946844255; Wed, 28 Jun 2023 03:07:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687946844; cv=none; d=google.com; s=arc-20160816; b=PcWeQnTaTJbemvb33cgnHWFTPoG3QUXo0hcG+60LjxviKxMZ7+BrbIWU/9H7Ihqs5i J3SRwFPx1/0nI+BFH1fXB6FVffuamIN3JzpZfr+xbdPhShiUtwoKGNZrCIKNj7J/g3Nz Jb41N7BJYbG5vmPU0CUKFx0Bue/yxDqnXwGK/8dP0l371NWQyWnrV8Eo+u7KqYV1sOxr dDfK8AYzFJB7Z2ABn74hKHHY7+kLE/oOm+2h50Pft3JQA6MIBBUJ2ghci3L3VecazHSC oRSuA4Oml8jVzX4a0xzu6m1Mt714yst269nGlLMxCnk6dQT5Irw5mkXnEC3Z3SEZ+Sky nOpg== 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=YJdHezsIb9Zs/i2rnLNGa7wU723TAKA2+hJD7YGLQMA=; fh=ecJFm7QT1PMAwV6imH2uyLFSpQA9koFc54msGfaixMY=; b=fKsY+0JCDR1RdxgT2Y1k+9e/Ki3bocNbzoVuGYsjxsMak7UqoHoOZ0nsE7uwyv6IBZ WWr90LwdkUObVw7+iXONIv9+Af3UYMeZtm3Zsdvpm1VDDmWu4et1M9vDNbHamm5qMQEC 3ewxufEFHtAW2aH0NXhxt+dKNVrVwE98Zhem5+GVTZoB/CBMw+jdxmEAtrKISuoC4kgA jq9LqvPS3gng562j3CxJWXVnujIEta2BSj1ypKTveNX4l4BZkI8YdlcaHeSnIxbeKplL naCZ7LJKnLoHfq9EI466JkICWo52/5WzZprfqgJlUSAOu4g2yjufeSVhCgC4FimQPXT8 O++w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BWkUgE7g; 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 w12-20020a170902d10c00b001b533facaa6si5535746plw.313.2023.06.28.03.06.57; Wed, 28 Jun 2023 03:07:24 -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=BWkUgE7g; 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 S233506AbjF1Jy0 (ORCPT + 99 others); Wed, 28 Jun 2023 05:54:26 -0400 Received: from dfw.source.kernel.org ([139.178.84.217]:36890 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232236AbjF1JuB (ORCPT ); Wed, 28 Jun 2023 05:50:01 -0400 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 CF0EE6126D; Wed, 28 Jun 2023 09:50:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9CF13C433C0; Wed, 28 Jun 2023 09:49:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687945800; bh=wHy/4Bdn8bLqInIOtsbVUy9HeffaJhdQru3m0BWjVgw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BWkUgE7gZGIMcrVg4STv/B5lPWx6Q8LyAoYbws0CSfCsql+bBDYdawWGK2XhmpBeC 3NvH9Qv74cYPjC+asPQ3krfts5qn3C7LZYVRGVdHGFaGO7GdSQmH+hfhKB56A3qgD4 rRd/g93Rxsf5ti8lgPiq9rjcuAtigVVAqhyrlKKEcf0T7e2F743EjQwGeo+bPkXkPY n2tEOow3lYJ/Tr3Tgu68XW+2U48h6bpvpjGQUwRX+yOVSS3c8yAM8jlgvUV2Czauhv C6o8lclOBYraXDRZR45mVnE2yiHbIPq7H1HzStHYeG5v/6wDIwbVa1imO/tNyHZj0v 6GKfP/FzqBL8w== From: Maxime Ripard Date: Wed, 28 Jun 2023 11:49:23 +0200 Subject: [PATCH v2 3/3] drivers: base: Free devm resources when unregistering a device MIME-Version: 1.0 Message-Id: <20230329-kunit-devm-inconsistencies-test-v2-3-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 , Maxime Ripard X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3285; i=mripard@kernel.org; h=from:subject:message-id; bh=kkD1/1T/bjGcKyT9fGwaBiGFMGIVAdeHxZ21NAuyjKQ=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDClzmDQeKW9juC9gfatjn+zP1Tc23Pvrmamu8vlh4Z0Fz 0MeOSxL6ShlYRDjYpAVU2SJETZfEndq1utONr55MHNYmUCGMHBxCsBEHjUzMnQyzcqpElvTKrI5 b+bcuqw+yyvfLRbvNI6x/MYZ685Xu4aR4b5PXZ3fitalx1w82WN83qfUTXv818QscdauFV7nTaf N5gQA X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D 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?1769940550573495209?= X-GMAIL-MSGID: =?utf-8?q?1769940550573495209?= 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 use-after-free concerns. It's not clear whether those concerns are legitimate though, but I would expect drivers not to register new resources in their device-managed actions. Fixes: a525a3ddeaca ("driver core: free devres in device_release") Signed-off-by: Maxime Ripard Signed-off-by: David Gow --- 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);