mmc: mmc_test: use kzalloc_flex

Simplifies allocations by using a flexible array member in these structs.

Add __counted_by to get extra runtime analysis.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
This commit is contained in:
Rosen Penev 2026-03-26 19:58:49 -07:00 committed by Ulf Hansson
parent 59b0efd867
commit c3126dccfd

View File

@ -51,12 +51,12 @@ struct mmc_test_pages {
/** /**
* struct mmc_test_mem - allocated memory. * struct mmc_test_mem - allocated memory.
* @arr: array of allocations
* @cnt: number of allocations * @cnt: number of allocations
* @arr: array of allocations
*/ */
struct mmc_test_mem { struct mmc_test_mem {
struct mmc_test_pages *arr;
unsigned int cnt; unsigned int cnt;
struct mmc_test_pages arr[] __counted_by(cnt);
}; };
/** /**
@ -135,21 +135,22 @@ struct mmc_test_dbgfs_file {
* struct mmc_test_card - test information. * struct mmc_test_card - test information.
* @card: card under test * @card: card under test
* @scratch: transfer buffer * @scratch: transfer buffer
* @buffer: transfer buffer
* @highmem: buffer for highmem tests * @highmem: buffer for highmem tests
* @area: information for performance tests * @area: information for performance tests
* @gr: pointer to results of current testcase * @gr: pointer to results of current testcase
* @buffer: transfer buffer
*/ */
struct mmc_test_card { struct mmc_test_card {
struct mmc_card *card; struct mmc_card *card;
u8 scratch[BUFFER_SIZE]; u8 scratch[BUFFER_SIZE];
u8 *buffer;
#ifdef CONFIG_HIGHMEM #ifdef CONFIG_HIGHMEM
struct page *highmem; struct page *highmem;
#endif #endif
struct mmc_test_area area; struct mmc_test_area area;
struct mmc_test_general_result *gr; struct mmc_test_general_result *gr;
u8 buffer[];
}; };
enum mmc_test_prep_media { enum mmc_test_prep_media {
@ -315,7 +316,6 @@ static void mmc_test_free_mem(struct mmc_test_mem *mem)
while (mem->cnt--) while (mem->cnt--)
__free_pages(mem->arr[mem->cnt].page, __free_pages(mem->arr[mem->cnt].page,
mem->arr[mem->cnt].order); mem->arr[mem->cnt].order);
kfree(mem->arr);
kfree(mem); kfree(mem);
} }
@ -348,14 +348,10 @@ static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
if (max_segs > max_page_cnt) if (max_segs > max_page_cnt)
max_segs = max_page_cnt; max_segs = max_page_cnt;
mem = kzalloc_obj(*mem); mem = kzalloc_flex(*mem, arr, max_segs);
if (!mem) if (!mem)
return NULL; return NULL;
mem->arr = kzalloc_objs(*mem->arr, max_segs);
if (!mem->arr)
goto out_free;
while (max_page_cnt) { while (max_page_cnt) {
struct page *page; struct page *page;
unsigned int order; unsigned int order;
@ -3099,7 +3095,7 @@ static ssize_t mtf_test_write(struct file *file, const char __user *buf,
if (ret) if (ret)
return ret; return ret;
test = kzalloc_obj(*test); test = kzalloc_flex(*test, buffer, BUFFER_SIZE);
if (!test) if (!test)
return -ENOMEM; return -ENOMEM;
@ -3111,7 +3107,6 @@ static ssize_t mtf_test_write(struct file *file, const char __user *buf,
test->card = card; test->card = card;
test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
#ifdef CONFIG_HIGHMEM #ifdef CONFIG_HIGHMEM
test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER); test->highmem = alloc_pages(GFP_KERNEL | __GFP_HIGHMEM, BUFFER_ORDER);
if (!test->highmem) { if (!test->highmem) {
@ -3120,17 +3115,14 @@ static ssize_t mtf_test_write(struct file *file, const char __user *buf,
} }
#endif #endif
if (test->buffer) {
mutex_lock(&mmc_test_lock); mutex_lock(&mmc_test_lock);
mmc_test_run(test, testcase); mmc_test_run(test, testcase);
mutex_unlock(&mmc_test_lock); mutex_unlock(&mmc_test_lock);
}
#ifdef CONFIG_HIGHMEM #ifdef CONFIG_HIGHMEM
__free_pages(test->highmem, BUFFER_ORDER); __free_pages(test->highmem, BUFFER_ORDER);
free_test_buffer: free_test_buffer:
#endif #endif
kfree(test->buffer);
kfree(test); kfree(test);
return count; return count;