Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp75449rwd; Wed, 24 May 2023 14:25:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7xh6cDipqlSG2MmuTLQ52df2b9D3eBbKg1/w5dCXGwc0SqNGsVk43iBxUwGkgFCuOFOW58 X-Received: by 2002:a17:90a:b38f:b0:255:a8fb:a72a with SMTP id e15-20020a17090ab38f00b00255a8fba72amr6785624pjr.32.1684963527160; Wed, 24 May 2023 14:25:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684963527; cv=none; d=google.com; s=arc-20160816; b=W/iRRQlM07EocI65B+K4I21/bizpp852/d+rQv+yABsuMAzm0VJJ+M73CCiK+fV0in fQ4fBuym1OKToPrDHq+K4o1oPN7RHJUb3jcisbV+TsOLMxDLibWU1ccw+/gYIopneVll aVg9gkWGVxJAoEhWIb50oJ1CZibjTMj6vaYNnR8TDsFcSBbrlkestfqGrFZztUfBuQni zxR6mVa4ifA2PNwtyY50X0GcDmsNgLsZEBYWgDQPYZHjoQUGVYay+8XHUzCSqvq8JMdq FAfBkdBOTBp8DKaJJzZVbdj+FgaGQQT46CCBySzaemQscSjNXbTaFjdaX4ZrVnja35G7 9Glw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=T8Qg2K654YRlKmz23V8WpWN+Y6cHXvK/b2p7jnK6OQs=; b=QM6zOpz1NfgSlc+ihUS96/o9eeTjfI2oVto5hlgx/RQYoX2vCIM90AoxqVn4sdVmWt 1ZfYk2WZwjie85uvZoQcAXKmaVG6yLbVaXKLADPnZPsdgUdCfRnN3AxcpAxd8NUzbYss sIYJAlRh6H86qxe4OSuIJn69DEUL2BGLh8KksABBpMW6TD/YkWG4HmkjofWTsbozttO6 TG27VX1DtPnEsgYs+6osGkbYpZNZDFj6dZ835YO7LM+mevlJK+84i70kmqh7YzjEm4cJ p56naJy4IOgoalBCKHS3I3K+zGCRoinIoHcgWTnbBXjWFbDzA7KOd91K5X+Y9dNTiLEK 2pMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=t5sPqCEe; 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 q34-20020a17090a4fa500b00237155f2303si1997908pjh.136.2023.05.24.14.25.05; Wed, 24 May 2023 14:25:27 -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=t5sPqCEe; 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 S229792AbjEXVXG (ORCPT + 99 others); Wed, 24 May 2023 17:23:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229604AbjEXVXE (ORCPT ); Wed, 24 May 2023 17:23:04 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44A6D12E for ; Wed, 24 May 2023 14:23:03 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-4d9b2045e1cso1002a12.1 for ; Wed, 24 May 2023 14:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684963382; x=1687555382; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=T8Qg2K654YRlKmz23V8WpWN+Y6cHXvK/b2p7jnK6OQs=; b=t5sPqCEeZFf9HQQ4ntEgMZTuOcPaVIMzfbEpscsappcH0P0d2ASkDmPicxu7wnamVk fP7ckKxxzZ8Oa9e8Y9KxlAfJjjPM4n8ovg48npe8rUJGnj7kJB9mv+7kiXrASjqNkq5S HPROKIUIYb5aqkZrRAAnNwuIAEmtdGTHM/cDqQd8FPOPOnje75XJUaV+Q09uhuVLt1T9 PD5ShFEzQOAIiyfSv9ZpYIXNf+CmJFUy+EMFb0xxxyEyumo7jL/Awq1K/4H3SO2dKs3p 1dM9K37a6Go1bNHi7BE+tQSRYg8qJUjyB/Lpfxuo3TZAxAHFUUP0hfPsR+l7dFF/rLuc CWQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684963382; x=1687555382; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T8Qg2K654YRlKmz23V8WpWN+Y6cHXvK/b2p7jnK6OQs=; b=MPAH5tfkb9qd5q87RXlQPLMhP+8cxccOFf56oMM4PMbEjx00aAyieFPjW9SRIRtOVI Hs3a5QoQRFzY85B/kHD1Lyafh+2ZSRwzMmQ3deqZiW9c2/NveoGkfuo255Y68SUVr2H4 7NTgYcKGrT0nKC24AbDnDSn5BAMayxTdav6I2lm5tYsB/1BwE3yjeuqTYknmhD7sTagi xA3TPOxc3ozL/RGJHHdlGYq4XvIbwo1+jjDjX56+MQe75AtpV4l/6OB06TqNSjT55gqe tgGYOWos3zK+qgKRJihwCPmU44C5a+qRBAyG7O8P26O+8loKRWbUfl8esVYBB6ExFmbA h5QA== X-Gm-Message-State: AC+VfDzQNreV+2L+3YSfZ2272Q1ugTdAk3hFmqdTEat8Oi7C+oWyVzr0 S8nCS+3K0ViyvawaUMdx3uVKEnn1cTJI1urHkSfPFg== X-Received: by 2002:a50:9317:0:b0:506:b280:4993 with SMTP id m23-20020a509317000000b00506b2804993mr48726eda.2.1684963381628; Wed, 24 May 2023 14:23:01 -0700 (PDT) MIME-Version: 1.0 References: <20230518083849.2631178-1-davidgow@google.com> <20230518083849.2631178-4-davidgow@google.com> In-Reply-To: <20230518083849.2631178-4-davidgow@google.com> From: Rae Moar Date: Wed, 24 May 2023 17:22:49 -0400 Message-ID: Subject: Re: [PATCH v2 4/4] Documentation: kunit: Add usage notes for kunit_add_action() To: David Gow Cc: Matti Vaittinen , Daniel Latypov , Maxime Ripard , Benjamin Berg , Brendan Higgins , Stephen Boyd , Shuah Khan , 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" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_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 On Thu, May 18, 2023 at 4:39=E2=80=AFAM David Gow wro= te: > > Add some basic documentation for kunit_add_action() and related > deferred action functions. Hi David! This looks good to me. Just two typos below. Thanks! Reviewed-by: Rae Moar > > 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 Looks like a typo here: "custom" > +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 Pyt= hon Another small typo here as Bagas noted. > +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, closin= g > +files or other resources, or freeing resources. > + > +For example: > + > +.. code-block:: C > + > + static void cleanup_device(void *ctx) > + { > + struct device *dev =3D (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 sing= le > +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 memo= ry. > +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 entire= ly > +with ``kunit_remove_action``. > + > > Testing Static Functions > ------------------------ > -- > 2.40.1.698.g37aff9b760-goog >