]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
cxl/test: Add check after kzalloc() memory in alloc_mock_res()
authorDave Jiang <dave.jiang@intel.com>
Thu, 11 Jun 2026 23:03:05 +0000 (16:03 -0700)
committerDave Jiang <dave.jiang@intel.com>
Fri, 12 Jun 2026 01:08:34 +0000 (18:08 -0700)
alloc_mock_res() calls kzalloc() without checking the return value.
Add scope based resource management to deal with the allocated memory
cleanly.

Reported-by: sashiko-bot
Fixes: 67dcdd4d3b83 ("tools/testing/cxl: Introduce a mocked-up CXL port hierarchy")
Reviewed-by: Alison Schofield <alison.schofield@intel.com>
Link: https://patch.msgid.link/20260611230305.197390-1-dave.jiang@intel.com
Signed-off-by: Dave Jiang <dave.jiang@intel.com>
tools/testing/cxl/test/cxl.c

index 7351fb87c7ab742ca22f2b636bdead48aabc4214..9a0faf70e9b133edb6ea39da00dbf1da1894ff79 100644 (file)
@@ -433,12 +433,16 @@ static void depopulate_all_mock_resources(void)
 
 static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
 {
-       struct cxl_mock_res *res = kzalloc(sizeof(*res), GFP_KERNEL);
        struct genpool_data_align data = {
                .align = align,
        };
        unsigned long phys;
 
+       struct cxl_mock_res *res __free(kfree) = kzalloc(sizeof(*res),
+                                                        GFP_KERNEL);
+       if (!res)
+               return NULL;
+
        INIT_LIST_HEAD(&res->list);
        phys = gen_pool_alloc_algo(cxl_mock_pool, size,
                                   gen_pool_first_fit_align, &data);
@@ -453,7 +457,7 @@ static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
        list_add(&res->list, &mock_res);
        mutex_unlock(&mock_res_lock);
 
-       return res;
+       return no_free_ptr(res);
 }
 
 /* Only update CFMWS0 as this is used by the auto region. */