Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp657165rwd; Thu, 18 May 2023 01:59:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7nV99HXv3GOlOefUF+E/2f+GjU24d2nP/Cx89D3e9PGfC4gaGoqqwmnLMq5yhqXKp+WeSf X-Received: by 2002:a17:90a:f18c:b0:253:4d77:451e with SMTP id bv12-20020a17090af18c00b002534d77451emr1853307pjb.9.1684400350365; Thu, 18 May 2023 01:59:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684400350; cv=none; d=google.com; s=arc-20160816; b=xmsAub0KR9+s8ORdeF/OcGwi2ujdGXQwtJa2EXeLrJUfgoFHYReNccCvBTQ0NfaM1O h5guIzQsopc0AmzLE0BtLe1j01kR9r6ZEuc7bpUaLJQkFDSBBbgEyBxhGrxS1Xppr6aS VNhC2/pKAxEfWpwGz/ONUlQV0HtkVsnXg9mEyIjiAacXzZqsrFF+qslaf/kVs2rAY3nH qNA+akMN8y7vgO39X2TrLfZcSnz4l8hlnx0yU+PRIbJfrgWzXxOf6iNilbUvlknABG8b /9geL32UJ0MHUk0HUpAH7kBBdbtoWHRDx2HN0+sRCFXnAbX/kJthSj1BM2p3rq0g0jCp mAJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=ktXlgtWlyfQZQ1d8uj3tU7I14o2usUDk+fQoHXPWmDU=; b=ApedufvnLj12ew5c80Yp9+KO4M1ZQ6hV/K/xih3YQyojiQnxSzQ2vQM+rzk3Aq9Bjl x6VitcXzLC6tVlSPa8JR52xl+bEAWMtRkTUN5QKhNQFbqMJUaERwfIv7W3Y9J+RXnI73 q4tFsGleimDyJA0H96Z+sserFiq+DdI9i8l4mhFu8K+rA9xLkeTt2QHnWxr0bgdMCscP PIU7J9LOcS+2Aub5z/gFOTPaFXwkyM688ko+qOxp9Cc0Fy+6VbeT6XFuiMYpjjgKtN1l YKlRKCxHZEpckKtwscR62aLngX2uKvWU8Mgq96agLuzf4NLm+dprfFOvYRKlrGb4I20c W6VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=VgXvSLQJ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 141-20020a630093000000b0053048bd0669si901015pga.445.2023.05.18.01.58.53; Thu, 18 May 2023 01:59:10 -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=@google.com header.s=20221208 header.b=VgXvSLQJ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230285AbjERIj3 (ORCPT + 99 others); Thu, 18 May 2023 04:39:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230187AbjERIjS (ORCPT ); Thu, 18 May 2023 04:39:18 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1884A2722 for ; Thu, 18 May 2023 01:39:09 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5618f3273dbso36744327b3.1 for ; Thu, 18 May 2023 01:39:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684399148; x=1686991148; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ktXlgtWlyfQZQ1d8uj3tU7I14o2usUDk+fQoHXPWmDU=; b=VgXvSLQJQOhsSG6UOczEvxUlPM2ur85ri/n2c3BM88UVbcvRwtybZUbqHQhPphTTJS qEWKuFqmBczy64nT6g8RK9M0ViMel6Cy7xisxqBJxPUigBtu6vz6jkTH0+jiaz9nm4b4 6TFHJYWdYV+NZpu4BoGQPTt3tN5hUpqsm7tVYb7IeAIRVtgwF04QFOM5GjQiJHVbW8Tc eTclL8ikuZG/7d29UbQkFQ12xoBavLfUfepLs9eBVPOhRNUoXKAs20vs//yM+L4MgSkl zdkQaUk8jDtbrHgUZ5Fa3ZQFkZ/vy76GsD+pudEZUIKjppI9j5mPWYRwKR0AwhyXPU7q UYaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684399148; x=1686991148; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ktXlgtWlyfQZQ1d8uj3tU7I14o2usUDk+fQoHXPWmDU=; b=dsMztLcX3KmweHRUuJ0MGFcnUkkYy3gwFE8K/kfYX8f05RYexFpNP7Cl3JvAApSh09 0xG0qW+CSG6zkO+ykIeg7fGQQdnQJCz2KN+jk29/hKnnohkrWaXGvdw5RyxF/hmlGl1y vWVvPNlklOmMVCg68IEV/6e9pBnkGLtePIlm5WDP9B6D4s/hjIkWi9DkyVpYaoKO+/LJ 7GnTW/4PnSH/NePhuMTvM37lqCO++qWjQ5E/iSSLVw6maYqD6cE01I2GeF+7avCSUlUS 0tXPtSWqC8FNv3Ykp+Pupxs3MVPJlFZSpFaOTerD36o0cZbe+p8g527lQNoOz9lWfiA1 QlMA== X-Gm-Message-State: AC+VfDw2USONpdYKaMI/PE8a4Soyjde7iVrKkzj0FHLdoAVKVAIuYZmw 1NT/VSvRL6nvni2Jdz6f0gAj2+ec05QCww== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:c808:0:b0:54f:bb71:c7b3 with SMTP id n8-20020a81c808000000b0054fbb71c7b3mr589835ywi.9.1684399148266; Thu, 18 May 2023 01:39:08 -0700 (PDT) Date: Thu, 18 May 2023 16:38:46 +0800 In-Reply-To: <20230518083849.2631178-1-davidgow@google.com> Mime-Version: 1.0 References: <20230518083849.2631178-1-davidgow@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Message-ID: <20230518083849.2631178-4-davidgow@google.com> Subject: [PATCH v2 4/4] Documentation: kunit: Add usage notes for kunit_add_action() From: David Gow To: Matti Vaittinen , Daniel Latypov , Maxime Ripard , Benjamin Berg , Brendan Higgins , Stephen Boyd , Shuah Khan , Rae Moar Cc: David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Heikki Krogerus , Jonathan Cameron , Sadiya Kazi , Jonathan Corbet , linux-doc@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 Add some basic documentation for kunit_add_action() and related deferred action functions. Signed-off-by: David Gow --- This patch is new in v2. --- Documentation/dev-tools/kunit/usage.rst | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index 46957d1cbcbb..c2f0ed648385 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -615,6 +615,57 @@ For example: KUNIT_ASSERT_STREQ(test, buffer, ""); } +Registering Cleanup Actions +--------------------------- + +If you need to perform some cleanup beyond simple use of ``kunit_kzalloc``, +you can register a cusom "deferred action", which is a cleanup function +run when the test exits (whether cleanly, or via a failed assertion). + +Actions are simple functions with no return value, and a single ``void*`` +context argument, and forfil the same role as "cleanup" functions in Python +and Go tests, "defer" statements in languages which support them, and +(in some cases) destructors in RAII languages. + +These are very useful for unregistering things from global lists, closing +files or other resources, or freeing resources. + +For example: + +.. code-block:: C + + static void cleanup_device(void *ctx) + { + struct device *dev = (struct device *)ctx; + + device_unregister(dev); + } + + void example_device_test(struct kunit *test) + { + struct my_device dev; + + device_register(&dev); + + kunit_add_action(test, &cleanup_device, &dev); + } + +Note that, for functions like device_unregister which only accept a single +pointer-sized argument, it's possible to directly cast that function to +a ``kunit_action_t`` rather than writing a wrapper function, for example: + +.. code-block:: C + + kunit_add_action(test, (kunit_action_t *)&device_unregister, &dev); + +``kunit_add_action`` can fail if, for example, the system is out of memory. +You can use ``kunit_add_action_or_reset`` instead which runs the action +immediately if it cannot be deferred. + +If you need more control over when the cleanup function is called, you +can trigger it early using ``kunit_release_action``, or cancel it entirely +with ``kunit_remove_action``. + Testing Static Functions ------------------------ -- 2.40.1.698.g37aff9b760-goog