LCOV - code coverage report
Current view: top level - lib/kunit - resource.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 18 25 72.0 %
Date: 2023-07-19 18:55:55 Functions: 2 3 66.7 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * KUnit resource API for test managed resources (allocations, etc.).
       4             :  *
       5             :  * Copyright (C) 2022, Google LLC.
       6             :  * Author: Daniel Latypov <dlatypov@google.com>
       7             :  */
       8             : 
       9             : #include <kunit/resource.h>
      10             : #include <kunit/test.h>
      11             : #include <linux/kref.h>
      12             : 
      13             : /*
      14             :  * Used for static resources and when a kunit_resource * has been created by
      15             :  * kunit_alloc_resource().  When an init function is supplied, @data is passed
      16             :  * into the init function; otherwise, we simply set the resource data field to
      17             :  * the data value passed in. Doesn't initialize res->should_kfree.
      18             :  */
      19         349 : int __kunit_add_resource(struct kunit *test,
      20             :                          kunit_resource_init_t init,
      21             :                          kunit_resource_free_t free,
      22             :                          struct kunit_resource *res,
      23             :                          void *data)
      24             : {
      25         349 :         int ret = 0;
      26             :         unsigned long flags;
      27             : 
      28         349 :         res->free = free;
      29         698 :         kref_init(&res->refcount);
      30             : 
      31         349 :         if (init) {
      32         349 :                 ret = init(res, data);
      33         349 :                 if (ret)
      34             :                         return ret;
      35             :         } else {
      36           0 :                 res->data = data;
      37             :         }
      38             : 
      39         349 :         spin_lock_irqsave(&test->lock, flags);
      40         698 :         list_add_tail(&res->node, &test->resources);
      41             :         /* refcount for list is established by kref_init() */
      42         698 :         spin_unlock_irqrestore(&test->lock, flags);
      43             : 
      44         349 :         return ret;
      45             : }
      46             : EXPORT_SYMBOL_GPL(__kunit_add_resource);
      47             : 
      48         349 : void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
      49             : {
      50             :         unsigned long flags;
      51             :         bool was_linked;
      52             : 
      53         349 :         spin_lock_irqsave(&test->lock, flags);
      54         698 :         was_linked = !list_empty(&res->node);
      55         698 :         list_del_init(&res->node);
      56         698 :         spin_unlock_irqrestore(&test->lock, flags);
      57             : 
      58         349 :         if (was_linked)
      59             :                 kunit_put_resource(res);
      60         349 : }
      61             : EXPORT_SYMBOL_GPL(kunit_remove_resource);
      62             : 
      63           0 : int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
      64             :                            void *match_data)
      65             : {
      66           0 :         struct kunit_resource *res = kunit_find_resource(test, match,
      67             :                                                          match_data);
      68             : 
      69           0 :         if (!res)
      70             :                 return -ENOENT;
      71             : 
      72           0 :         kunit_remove_resource(test, res);
      73             : 
      74             :         /* We have a reference also via _find(); drop it. */
      75           0 :         kunit_put_resource(res);
      76             : 
      77           0 :         return 0;
      78             : }
      79             : EXPORT_SYMBOL_GPL(kunit_destroy_resource);

Generated by: LCOV version 1.14