Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6508242rdb; Thu, 14 Dec 2023 23:50:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNDTWfl2PPVP4nNzK2fSvHQ3/is9Pi9PdmSEIcDpWBWznh4iTRq6Q4ZDRjHThoX9IbHeW0 X-Received: by 2002:a9d:73c6:0:b0:6da:2ec0:cdc3 with SMTP id m6-20020a9d73c6000000b006da2ec0cdc3mr3924378otk.28.1702626610624; Thu, 14 Dec 2023 23:50:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702626610; cv=none; d=google.com; s=arc-20160816; b=x0RnEaM/reVjc5UbGxEPTy2bAYTIhJC9xjf9H9niWiJ+vBDVjuNPSr22aXFbZYZdFu HzFWIoXx0fHbW5S5RMIviH3ZhKaS+GBj8OYlyIbl6FM8Qa1029tjI+kq/Rt5QMs9HfNz AYD++o1X+Hd6flm+YEb9cTvA2P5Ld1YCcThHr5hr1i4wowEL6yAbvWMsTwpTHo/MOrk7 mjAKJnPDNV/sUQl+ZDYTDpRfi6Tx08IXdrcNni6JgmkelX5UAXJYPycm/CRZulxKnB48 4ITd62LEKxmIB3aDMUF58QoGcGhmxLTbvvzeSPtRPFnD35svlhDOpSgWl2QpoewGIILZ kbiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=Mp/8hWJv2G3IEKZLrg9ZmDkM8WmGCalwoEjWRw4iU4I=; fh=s2ufp5odxBiVI1DugGmR9pnJ96PDI7y2LhWfMqzKBMk=; b=FLt7VE7xilNGfe/2ejLfy+9V9haarQxU+vr2V3Ss3r58UAMS0prjn+dg0pjLK6gr/u wm54TKjk5/RTpKjifAt0vCN07zhcQTw84ywDkTDpCMnvzwKQVyCNLgFyOaABL6esln2F yLWeUjR/lNVFm1uO/Rry7HIosiSPAuuguJVhBm5wfJ7BYptHfGU6oVLOvcmLxBzzjDcQ xo5o0VURzTP2L2N4IyxEBoAXYrdM/dfTYgb1TzPssQO3EMh7jmK5OCWgyKIbdBK1TAtW wzBcvsIXk5vXfO4fC39h3o1h6LPr+Q578N2n/JLTC/2rOEgxnhjJmSB8I7OEUW4ShIes txjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="l/RaSu98"; spf=pass (google.com: domain of linux-kernel+bounces-534-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-534-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id n128-20020a632786000000b005b11ce9c97esi12375834pgn.353.2023.12.14.23.50.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 23:50:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-534-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="l/RaSu98"; spf=pass (google.com: domain of linux-kernel+bounces-534-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-534-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id EEAC6284641 for ; Fri, 15 Dec 2023 07:39:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0C3D7101C3; Fri, 15 Dec 2023 07:39:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="l/RaSu98" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48D3D12E6E for ; Fri, 15 Dec 2023 07:39:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-db402e6f61dso234514276.3 for ; Thu, 14 Dec 2023 23:39:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702625953; x=1703230753; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Mp/8hWJv2G3IEKZLrg9ZmDkM8WmGCalwoEjWRw4iU4I=; b=l/RaSu98NWmILQWuM6rpl6zMyxbpbwNz5KIo9kTWfntjLzb2z3FiQlmQAufrErANHT dDzaj3j7hVnfNg5wnniuA4g2980iXazQa/BeoTi4LPn8KYKVDCtyVxqy1UvgE+kKvwq1 /QgqK/pi+sODqBCWmYRmR+dIkrPDWf0bHqoYn4pYa6hVNCRevcnmfBHgmSoFJzRVJpKM s0Gq1ICunwOB0rDco4o3b56lRR79dtn0SbQ4BQU4PoJLjSWqMaAdLcSbNyv6JK2gz6NE UixTZ816jrYR73ON6JMT1BJJEVkCt9z2SOE8ypGbKC02wKDvGMOXj22xfW/3hoZ5xKui 4Zpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702625953; x=1703230753; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Mp/8hWJv2G3IEKZLrg9ZmDkM8WmGCalwoEjWRw4iU4I=; b=EpQVeg0KwonK+LuZV3t+MvfAQit+XSoZeU+pNvXAOC3yaQrJLtgQVgFrsiOXIdsxS7 uYhx0urrIn4Xa/Lls/akA8OmWuofoe3rju5W+/Byhh9sdjbvCa1ssyGwHlzV+6P7/gZ/ XbV0hYUa+QkFGI2pzUrsLc12nnxXEaV5W3oCftTczsaxyPNwOty4yRXx3BUBjMB/I25P t0cmdM4tfARTwz165+hWMsx4tnWAcyDlx5cXPBnfyJdsVot6f4FYNGHkjhHycY1TzhBc u6Ap/ofcuWxAkE12Jnt9/FLqC0rlWsntvmhKHzwtS00NXQsBTQiosen32GphpeFzISnJ ieVQ== X-Gm-Message-State: AOJu0YwhAwA3OzJcLEilB6iugHhluoUZXA9ZVG481Ra5PmYHp19udX0e anZSNram5KEtYYS0es9TjMjnmM+cERlMBw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:a122:0:b0:dbc:35fe:5e81 with SMTP id z31-20020a25a122000000b00dbc35fe5e81mr102923ybh.12.1702625953359; Thu, 14 Dec 2023 23:39:13 -0800 (PST) Date: Fri, 15 Dec 2023 15:39:07 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAJsCfGUC/2XO0Q6CIBTG8VdxXEcDDqh01Xu01hSOyippUqzmf PfQK1qX3xm//5hJwMlhIIdiJhNGF5wf05C7gpihGXukzqZNBBPAKl7T62t0z0v7CrRpuTYSO1l bQ9L7x4Sde2+t0zntwYWnnz5bOvL1ulW4YCqrRE4ZLUExKC20hsOx976/4d74O1kzUeQ0/0AUi aJWmimrWxDqj0JGucwpJFqhbkRdWWCy/qHLsnwBjE6oShkBAAA= X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> Subject: [PATCH v4 0/5] kunit: Add helpers for creating test-managed devices From: davidgow@google.com To: Rae Moar , Brendan Higgins , Greg Kroah-Hartman , Matti Vaittinen , Stephen Boyd , Shuah Khan , Jonathan Corbet , Kees Cook , Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai , Maxime Ripard Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-sound@vger.kernel.org, David Gow , Maxime Ripard Content-Type: text/plain; charset="utf-8" KUnit tests often need to provide a struct device, and thus far have mostly been using root_device_register() or platform devices to create a 'fake device' for use with, e.g., code which uses device-managed resources. This has several disadvantages, including not being designed for test use, scattering files in sysfs, and requiring manual teardown on test exit, which may not always be possible in case of failure. Instead, introduce a set of helper functions which allow devices (internally a struct kunit_device) to be created and managed by KUnit -- i.e., they will be automatically unregistered on test exit. These helpers can either use a user-provided struct device_driver, or have one automatically created and managed by KUnit. In both cases, the device lives on a new kunit_bus. This is a follow-up to a previous proposal here: https://lore.kernel.org/linux-kselftest/20230325043104.3761770-1-davidgow@google.com/ (The kunit_defer() function in the first patch there has since been merged as the 'deferred actions' feature.) My intention is to take this whole series in via the kselftest/kunit branch, but I'm equally okay with splitting up the later patches which use this to go via the various subsystem trees in case there are merge conflicts. Cheers, -- David Signed-off-by: David Gow --- Changes in v4: - Update tags, fix a missing Signed-off-by. - Link to v3: https://lore.kernel.org/r/20231214-kunit_bus-v3-0-7e9a287d3048@google.com Changes in v3: - Port the DRM tests to these new helpers (Thanks, Maxime!) - Include the lib/kunit/device-impl.h file, which was missing from the previous revision. - Fix a use-after-free bug in kunit_device_driver_test, which resulted in memory corruption on some clang-built UML builds. - The 'test_state' is now allocated with kunit_kzalloc(), not on the stack, as the stack will be gone when cleanup occurs. - Link to v2: https://lore.kernel.org/r/20231208-kunit_bus-v2-0-e95905d9b325@google.com Changes in v2: - Simplify device/driver/bus matching, removing the no-longer-required kunit_bus_match function. (Thanks, Greg) - The return values are both more consistent (kunit_device_register now returns an explicit error pointer, rather than failing the test), and better documented. - Add some basic documentation to the implementations as well as the headers. The documentation in the headers is still more complete, and is now properly compiled into the HTML documentation (under dev-tools/kunit/api/resources.html). (Thanks, Matti) - Moved the internal-only kunit_bus_init() function to a private header, lib/kunit/device-impl.h to avoid polluting the public headers, and match other internal-only headers. (Thanks, Greg) - Alphabetise KUnit includes in other test modules. (Thanks, Amadeusz.) - Several code cleanups, particularly around error handling and allocation. (Thanks Greg, Maxime) - Several const-correctness and casting improvements. (Thanks, Greg) - Added a new test to verify KUnit cleanup triggers device cleanup. (Thanks, Maxime). - Improved the user-specified device test to verify that probe/remove hooks are called correctly. (Thanks, Maxime). - The overflow test no-longer needlessly calls kunit_device_unregister(). - Several other minor cleanups and documentation improvements, which hopefully make this a bit clearer and more robust. - Link to v1: https://lore.kernel.org/r/20231205-kunit_bus-v1-0-635036d3bc13@google.com --- David Gow (4): kunit: Add APIs for managing devices fortify: test: Use kunit_device overflow: Replace fake root_device with kunit_device ASoC: topology: Replace fake root_device with kunit_device in tests Maxime Ripard (1): drm/tests: Switch to kunit devices Documentation/dev-tools/kunit/api/resource.rst | 9 ++ Documentation/dev-tools/kunit/usage.rst | 50 +++++++ drivers/gpu/drm/tests/drm_kunit_helpers.c | 66 +-------- include/kunit/device.h | 80 +++++++++++ lib/fortify_kunit.c | 5 +- lib/kunit/Makefile | 3 +- lib/kunit/device-impl.h | 17 +++ lib/kunit/device.c | 181 +++++++++++++++++++++++++ lib/kunit/kunit-test.c | 134 +++++++++++++++++- lib/kunit/test.c | 3 + lib/overflow_kunit.c | 5 +- sound/soc/soc-topology-test.c | 10 +- 12 files changed, 485 insertions(+), 78 deletions(-) --- base-commit: b285ba6f8cc1b2bfece0b4350fdb92c8780bc698 change-id: 20230718-kunit_bus-ab19c4ef48dc Best regards, -- David Gow