Commit Graph

4 Commits

Author SHA1 Message Date
Mika Penttilä 87c39019b8 kunit: Add kunit_add_action() to defer a call until test exit
JIRA: https://issues.redhat.com/browse/RHEL-1349
Upstream Status: v6.5-rc1

commit b9dce8a1ed3efe0f5c0957f4605140f204226a0f
Author:     David Gow <davidgow@google.com>
AuthorDate: Thu May 25 12:21:28 2023 +0800
Commit:     Shuah Khan <skhan@linuxfoundation.org>
CommitDate: Thu May 25 08:52:55 2023 -0600

    Many uses of the KUnit resource system are intended to simply defer
    calling a function until the test exits (be it due to success or
    failure). The existing kunit_alloc_resource() function is often used for
    this, but was awkward to use (requiring passing NULL init functions, etc),
    and returned a resource without incrementing its reference count, which
    -- while okay for this use-case -- could cause problems in others.

    Instead, introduce a simple kunit_add_action() API: a simple function
    (returning nothing, accepting a single void* argument) can be scheduled
    to be called when the test exits. Deferred actions are called in the
    opposite order to that which they were registered.

    This mimics the devres API, devm_add_action(), and also provides
    kunit_remove_action(), to cancel a deferred action, and
    kunit_release_action() to trigger one early.

    This is implemented as a resource under the hood, so the ordering
    between resource cleanup and deferred functions is maintained.

    Reviewed-by: Benjamin Berg <benjamin.berg@intel.com>
    Reviewed-by: Maxime Ripard <maxime@cerno.tech>
    Tested-by: Maxime Ripard <maxime@cerno.tech>
    Signed-off-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Signed-off-by: Mika Penttilä <mpenttil@redhat.com>
2023-10-26 06:54:59 +03:00
Nico Pache 5bd2ae31a6 kunit: Rework kunit_resource allocation policy
commit ad69172ec930075d25e14220841dd96375088d28
Author: David Gow <davidgow@google.com>
Date:   Sat Apr 2 12:35:30 2022 +0800

    kunit: Rework kunit_resource allocation policy

    KUnit's test-managed resources can be created in two ways:
    - Using the kunit_add_resource() family of functions, which accept a
      struct kunit_resource pointer, typically allocated statically or on
      the stack during the test.
    - Using the kunit_alloc_resource() family of functions, which allocate a
      struct kunit_resource using kzalloc() behind the scenes.

    Both of these families of functions accept a 'free' function to be
    called when the resource is finally disposed of.

    At present, KUnit will kfree() the resource if this 'free' function is
    specified, and will not if it is NULL. However, this can lead
    kunit_alloc_resource() to leak memory (if no 'free' function is passed
    in), or kunit_add_resource() to incorrectly kfree() memory which was
    allocated by some other means (on the stack, as part of a larger
    allocation, etc), if a 'free' function is provided.

    Instead, always kfree() if the resource was allocated with
    kunit_alloc_resource(), and never kfree() if it was passed into
    kunit_add_resource() by the user. (If the user of kunit_add_resource()
    wishes the resource be kfree()ed, they can call kfree() on the resource
    from within the 'free' function.

    This is implemented by adding a 'should_free' member to
    struct kunit_resource and setting it appropriately. To facilitate this,
    the various resource add/alloc functions have been refactored somewhat,
    making them all call a __kunit_add_resource() helper after setting the
    'should_free' member appropriately. In the process, all other functions
    have been made static inline functions.

    Signed-off-by: David Gow <davidgow@google.com>
    Tested-by: Daniel Latypov <dlatypov@google.com>
    Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2168378
Signed-off-by: Nico Pache <npache@redhat.com>
2023-04-17 11:47:35 -06:00
Nico Pache 55b2cf99da kunit: Make kunit_remove_resource() idempotent
commit 59729170afcd4900e08997a482467ffda8d88c7f
Author: David Gow <davidgow@google.com>
Date:   Sat Apr 2 12:35:29 2022 +0800

    kunit: Make kunit_remove_resource() idempotent

    The kunit_remove_resource() function is used to unlink a resource from
    the list of resources in the test, making it no longer show up in
    kunit_find_resource().

    However, this could lead to a race condition if two threads called
    kunit_remove_resource() on the same resource at the same time: the
    resource would be removed from the list twice (causing a crash at the
    second list_del()), and the refcount for the resource would be
    decremented twice (instead of once, for the reference held by the
    resource list).

    Fix both problems, the first by using list_del_init(), and the second by
    checking if the resource has already been removed using list_empty(),
    and only decrementing its refcount if it has not.

    Also add a KUnit test for the kunit_remove_resource() function which
    tests this behaviour.

    Reported-by: Daniel Latypov <dlatypov@google.com>
    Signed-off-by: David Gow <davidgow@google.com>
    Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2168378
Signed-off-by: Nico Pache <npache@redhat.com>
2023-04-17 11:47:35 -06:00
Nico Pache 4db05af562 kunit: split resource API impl from test.c into new resource.c
commit cdebea6968faafa955b3cc9196003e7f17f78955
Author: Daniel Latypov <dlatypov@google.com>
Date:   Mon Mar 28 10:41:43 2022 -0700

    kunit: split resource API impl from test.c into new resource.c

    We've split out the declarations from include/kunit/test.h into
    resource.h.
    This patch splits out the definitions as well for consistency.

    A side effect of this is git blame won't properly track history by
    default, users need to run
    $ git blame -L ,1 -C13 lib/kunit/resource.c

    Signed-off-by: Daniel Latypov <dlatypov@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2168378
Signed-off-by: Nico Pache <npache@redhat.com>
2023-04-17 11:47:34 -06:00