Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2439675lqo; Mon, 13 May 2024 20:35:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUYNjZXjUMc9HKx897BSbT5c3w0FOSgRyEOMRRak2Ds6PD+Q0pzs3oCIni2rxSGruw13JKuSHjitW6lmdanynv3ttHdRQams8I8mFqIQg== X-Google-Smtp-Source: AGHT+IFUNxAIgc48Avo+vpnr33AzxuBfBUO+MgC2lVuUybVt/8SJOU5XXzJ64s6qJoB+LMUeDrD5 X-Received: by 2002:a05:620a:3711:b0:792:bb55:906d with SMTP id af79cd13be357-792c75ab846mr1458091285a.39.1715657759151; Mon, 13 May 2024 20:35:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715657759; cv=pass; d=google.com; s=arc-20160816; b=Wsq+1LUzbXBU+JRNwP9s7ePe0yHv2nKbHWAhyFrowxHgEqRPnmCvsRfLifIIXJy9CF jQFS3nSeYV0w28o4cMmBJhsSd7dWf5bckfttAl9nU4VtWETQRINc5/GOMm70IxrsR/6I 9wNsdYBA9WnaSSxokaEEiIIqGBdCKYSzfGWdtbGrzJEVEvciZfllgf6iZQTPgXI+YK06 K2eU6Izl6Pr08OdW0hgiFfBsLEiznM46T6/asAVD2gzyumB0DfxzJm/Bv0BVCLD+BeF9 yqcI/R4+aXJIu8qOKlnGAnHtYNikd6qmWj35tYHN3NCqZ/t065QhOsuSm9uUmCC74upV hkQQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:date:to:cc:from:subject:references:in-reply-to :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:dkim-signature; bh=kUzAtG8sMGimZ7l9mBpONOBTAHLwz1dFLoWha547AZc=; fh=29fAtjy3d9q+ttmTblWbp4JolbcfzpHYS3Kj3FZF9A0=; b=BBKVpaSjDoGMEqaC/ZdsBsW0bdpYS4Od5u7A0mLOJFvTi73d40KI9eSTe2PKGczL9K LuVntHo5ns8ZY5e6EjCz6F4b0JZp9ufkNI3BVsggXd4qm9+Qkuwhxd6jEh+VFJlHgQJI vU2cL2fitwisganfMo5ym8FH1Q15HxlVd1lM3AN7DwaTnJFOkGQ/Y2z6esR1EH4t2Rv1 Omm4kLJ9GA0KM10fA1tuDHUR5NCDbm+0Qmdnx3F6zzB9RTqPNEy4pNXFYrTrLrp7/KU4 w/GE4k2svakzlTA8bdjRxt9BwEY0qwsozVRJXkIXH5NVUC9RCMV2wwV6XHoNDj+D8Ew6 vSFg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RWhj5WyH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-178295-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178295-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-792bf33b2b0si1071579385a.438.2024.05.13.20.35.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 20:35:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-178295-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RWhj5WyH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-178295-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178295-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D24411C21395 for ; Tue, 14 May 2024 03:35:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 88416F51B; Tue, 14 May 2024 03:35:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RWhj5WyH" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89DC3AD53; Tue, 14 May 2024 03:35:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715657751; cv=none; b=HPO42QaS7A0t7+1/FRasS7zqC+9U+HcoudA72MBtrEF/JFMjNaJiO+mENpnsvPoI/aZAb+47RQ9GmiHuYhE1XNxdSE52yWtCSbRvQn9MY93RFsp4g5zye5DKLm/L12ZxKJOLC+Z4rhfTir/l6/Krtuw5jK05WPH1pQpWfqqdD6M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715657751; c=relaxed/simple; bh=2g51WnteVbDW6Xw9cHo8bze8u9qoK08M7PEKoTNl//U=; h=Message-ID:Content-Type:MIME-Version:In-Reply-To:References: Subject:From:Cc:To:Date; b=i5QPuNqmTH+TZ7Nbmu5T9mEyb4i31vq4UFthc7biMNPjC/DT2LM5aGD32NFHiGEy9N9PfcySKapVH270nriVUn3qlWNlbzM8gKuPidfKwkufhHeoMkWX6TmMQkZ8cbAugOTm70nyZ+kwEXGDTikA79S+cKUajXUJ24paxRQ3M1o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RWhj5WyH; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4913AC2BD10; Tue, 14 May 2024 03:35:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715657751; bh=2g51WnteVbDW6Xw9cHo8bze8u9qoK08M7PEKoTNl//U=; h=In-Reply-To:References:Subject:From:Cc:To:Date:From; b=RWhj5WyH38Woqr+5eMMlmko6m4W28FqTOEvMn2vnCfh/yJHDfSelrHDdq8V296J2s ZaDecWJebiToXuNeDOHk8hkAqC0FezOcUWj3+1oEtpAevdnjdHYub5zHelCHV+44sP sY8B02t11x5Gt58+t0omv/OQ/jp/9HsU7dCOHyrLxej3fMwMunVBzZpc6m1le3F26m g027xhP7NXsTyu3Uy5iaRfbIQWnwjxHS1vTrUPnzOCbjhqtmU4cmP4ffBK3evUqzSZ p3b4TXgHAQssXm6Nx/ikd6fGoui/EKPwhF4lWm0v5bRa8FnTkj+tje5PB04f8XHZ47 9b53bYw7AT5Qw== Message-ID: <31d4dfdf8bc4b866a2b6d45fc4de0c2d.sboyd@kernel.org> Content-Type: text/plain; charset="utf-8" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable In-Reply-To: <20240422232404.213174-6-sboyd@kernel.org> References: <20240422232404.213174-1-sboyd@kernel.org> <20240422232404.213174-6-sboyd@kernel.org> Subject: Re: [PATCH v4 05/10] platform: Add test managed platform_device/driver APIs From: Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, devicetree@vger.kernel.org, Brendan Higgins , David Gow , Rae Moar , Greg Kroah-Hartman , Rafael J . Wysocki , Rob Herring , Saravana Kannan , Daniel Latypov , Christian Marangi , Krzysztof Kozlowski , Conor Dooley , Maxime Ripard To: Michael Turquette , Stephen Boyd Date: Mon, 13 May 2024 20:35:49 -0700 User-Agent: alot/0.10 Quoting Stephen Boyd (2024-04-22 16:23:58) > diff --git a/drivers/base/test/platform_kunit.c b/drivers/base/test/platf= orm_kunit.c > new file mode 100644 > index 000000000000..54af6db2a6d8 > --- /dev/null > +++ b/drivers/base/test/platform_kunit.c > @@ -0,0 +1,174 @@ [...] > +struct platform_device * > +platform_device_alloc_kunit(struct kunit *test, const char *name, int id) > +{ > + struct platform_device *pdev; > + > + pdev =3D platform_device_alloc(name, id); > + if (!pdev) > + return NULL; > + > + if (kunit_add_action_or_reset(test, (kunit_action_t *)&platform_d= evice_put, pdev)) > + return NULL; > + > + return pdev; > +} > +EXPORT_SYMBOL_GPL(platform_device_alloc_kunit); > + > +static void platform_device_add_kunit_exit(struct kunit_resource *res) > +{ > + struct platform_device *pdev =3D res->data; > + > + platform_device_unregister(pdev); > +} > + > +static bool > +platform_device_alloc_kunit_match(struct kunit *test, > + struct kunit_resource *res, void *match= _data) > +{ > + struct platform_device *pdev =3D match_data; > + > + return res->data =3D=3D pdev; > +} > + > +/** > + * platform_device_add_kunit() - Register a KUnit test managed platform = device > + * @test: test context > + * @pdev: platform device to add > + * > + * Register a test managed platform device. The device is unregistered w= hen the > + * test completes. > + * > + * Return: 0 on success, negative errno on failure. > + */ > +int platform_device_add_kunit(struct kunit *test, struct platform_device= *pdev) > +{ > + struct kunit_resource *res; > + int ret; > + > + ret =3D platform_device_add(pdev); > + if (ret) > + return ret; > + > + res =3D kunit_find_resource(test, platform_device_alloc_kunit_mat= ch, pdev); This doesn't work because platform_device_alloc_kunit() used kunit_add_action_or_reset() which has a chained free routine and data pointer. I've added a test to make sure the platform device is removed from the bus. It's not super great though because when this code fails to find a match it will still remove the device by calling platform_device_unregister() when the test ends. It will follow that up with a call to platform_device_put(), which is the problem as that causes an underflow and operates on an already freed device. I couldn't come up with anything better than searching the platform bus. Maybe if there was a way to allocate the memory or redirect where platform_device_alloc_kunit() got memory from we could hold the device memory around after it should have been freed and make sure the kref for the device kobject is 0. That seems pretty invasive to do though so I'm just going to leave it for now and add this test to make sure it cleans up.