Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * lib/test_parman.c - Test module for parman
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * modification, are permitted provided that the following conditions are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *    notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *    notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *    documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * 3. Neither the names of the copyright holders nor the names of its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *    contributors may be used to endorse or promote products derived from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *    this software without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * Alternatively, this software may be distributed under the terms of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * GNU General Public License ("GPL") version 2 as published by the Free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * POSSIBILITY OF SUCH DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <linux/parman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define TEST_PARMAN_PRIO_SHIFT 7 /* defines number of prios for testing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define TEST_PARMAN_PRIO_COUNT BIT(TEST_PARMAN_PRIO_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define TEST_PARMAN_PRIO_MASK (TEST_PARMAN_PRIO_COUNT - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define TEST_PARMAN_ITEM_SHIFT 13 /* defines a total number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 				   * of items for testing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				   */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define TEST_PARMAN_ITEM_COUNT BIT(TEST_PARMAN_ITEM_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define TEST_PARMAN_ITEM_MASK (TEST_PARMAN_ITEM_COUNT - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define TEST_PARMAN_BASE_SHIFT 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define TEST_PARMAN_BASE_COUNT BIT(TEST_PARMAN_BASE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define TEST_PARMAN_RESIZE_STEP_SHIFT 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define TEST_PARMAN_RESIZE_STEP_COUNT BIT(TEST_PARMAN_RESIZE_STEP_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define TEST_PARMAN_BULK_MAX_SHIFT (2 + TEST_PARMAN_RESIZE_STEP_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define TEST_PARMAN_BULK_MAX_COUNT BIT(TEST_PARMAN_BULK_MAX_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define TEST_PARMAN_BULK_MAX_MASK (TEST_PARMAN_BULK_MAX_COUNT - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define TEST_PARMAN_RUN_BUDGET (TEST_PARMAN_ITEM_COUNT * 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) struct test_parman_prio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct parman_prio parman_prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	unsigned long priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) struct test_parman_item {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct parman_item parman_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct test_parman_prio *prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	bool used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) struct test_parman {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct parman *parman;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct test_parman_item **prio_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	unsigned long prio_array_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct test_parman_prio prios[TEST_PARMAN_PRIO_COUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct test_parman_item items[TEST_PARMAN_ITEM_COUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct rnd_state rnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	unsigned long run_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	unsigned long bulk_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	bool bulk_noop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	unsigned int used_items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define ITEM_PTRS_SIZE(count) (sizeof(struct test_parman_item *) * (count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static int test_parman_resize(void *priv, unsigned long new_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct test_parman *test_parman = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct test_parman_item **prio_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	unsigned long old_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	prio_array = krealloc(test_parman->prio_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			      ITEM_PTRS_SIZE(new_count), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (new_count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (!prio_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	old_count = test_parman->prio_array_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (new_count > old_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		memset(&prio_array[old_count], 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		       ITEM_PTRS_SIZE(new_count - old_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	test_parman->prio_array = prio_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	test_parman->prio_array_limit = new_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static void test_parman_move(void *priv, unsigned long from_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			     unsigned long to_index, unsigned long count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct test_parman *test_parman = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct test_parman_item **prio_array = test_parman->prio_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	memmove(&prio_array[to_index], &prio_array[from_index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		ITEM_PTRS_SIZE(count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	memset(&prio_array[from_index], 0, ITEM_PTRS_SIZE(count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static const struct parman_ops test_parman_lsort_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.base_count	= TEST_PARMAN_BASE_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	.resize_step	= TEST_PARMAN_RESIZE_STEP_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	.resize		= test_parman_resize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	.move		= test_parman_move,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	.algo		= PARMAN_ALGO_TYPE_LSORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void test_parman_rnd_init(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	prandom_seed_state(&test_parman->rnd, 3141592653589793238ULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static u32 test_parman_rnd_get(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return prandom_u32_state(&test_parman->rnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static unsigned long test_parman_priority_gen(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	unsigned long priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	priority = test_parman_rnd_get(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (priority == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		struct test_parman_prio *prio = &test_parman->prios[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (prio->priority == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (prio->priority == priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void test_parman_prios_init(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		struct test_parman_prio *prio = &test_parman->prios[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		/* Assign random uniqueue priority to each prio structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		prio->priority = test_parman_priority_gen(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		parman_prio_init(test_parman->parman, &prio->parman_prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				 prio->priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static void test_parman_prios_fini(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		struct test_parman_prio *prio = &test_parman->prios[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		parman_prio_fini(&prio->parman_prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static void test_parman_items_init(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	for (i = 0; i < TEST_PARMAN_ITEM_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		struct test_parman_item *item = &test_parman->items[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		unsigned int prio_index = test_parman_rnd_get(test_parman) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 					  TEST_PARMAN_PRIO_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		/* Assign random prio to each item structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		item->prio = &test_parman->prios[prio_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static void test_parman_items_fini(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	for (i = 0; i < TEST_PARMAN_ITEM_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		struct test_parman_item *item = &test_parman->items[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (!item->used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		parman_item_remove(test_parman->parman,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 				   &item->prio->parman_prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				   &item->parman_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static struct test_parman *test_parman_create(const struct parman_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct test_parman *test_parman;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	test_parman = kzalloc(sizeof(*test_parman), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (!test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	err = test_parman_resize(test_parman, TEST_PARMAN_BASE_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		goto err_resize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	test_parman->parman = parman_create(ops, test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (!test_parman->parman) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		goto err_parman_create;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	test_parman_rnd_init(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	test_parman_prios_init(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	test_parman_items_init(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	test_parman->run_budget = TEST_PARMAN_RUN_BUDGET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	return test_parman;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) err_parman_create:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	test_parman_resize(test_parman, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) err_resize:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	kfree(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static void test_parman_destroy(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	test_parman_items_fini(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	test_parman_prios_fini(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	parman_destroy(test_parman->parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	test_parman_resize(test_parman, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	kfree(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static bool test_parman_run_check_budgets(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (test_parman->run_budget-- == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (test_parman->bulk_budget-- != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	test_parman->bulk_budget = test_parman_rnd_get(test_parman) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 				   TEST_PARMAN_BULK_MAX_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	test_parman->bulk_noop = test_parman_rnd_get(test_parman) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int test_parman_run(struct test_parman *test_parman)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	unsigned int i = test_parman_rnd_get(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	while (test_parman_run_check_budgets(test_parman)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		unsigned int item_index = i++ & TEST_PARMAN_ITEM_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		struct test_parman_item *item = &test_parman->items[item_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		if (test_parman->bulk_noop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		if (!item->used) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			err = parman_item_add(test_parman->parman,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 					      &item->prio->parman_prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 					      &item->parman_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			test_parman->prio_array[item->parman_item.index] = item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			test_parman->used_items++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			test_parman->prio_array[item->parman_item.index] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			parman_item_remove(test_parman->parman,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 					   &item->prio->parman_prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 					   &item->parman_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			test_parman->used_items--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		item->used = !item->used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int test_parman_check_array(struct test_parman *test_parman,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				   bool gaps_allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	unsigned int last_unused_items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	unsigned long last_priority = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	unsigned int used_items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (test_parman->prio_array_limit < TEST_PARMAN_BASE_COUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		pr_err("Array limit is lower than the base count (%lu < %lu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		       test_parman->prio_array_limit, TEST_PARMAN_BASE_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	for (i = 0; i < test_parman->prio_array_limit; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		struct test_parman_item *item = test_parman->prio_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (!item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			last_unused_items++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		if (last_unused_items && !gaps_allowed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			pr_err("Gap found in array even though they are forbidden\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		last_unused_items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		used_items++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (item->prio->priority < last_priority) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			pr_err("Item belongs under higher priority then the last one (current: %lu, previous: %lu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			       item->prio->priority, last_priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		last_priority = item->prio->priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		if (item->parman_item.index != i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			pr_err("Item has different index in compare to where it actually is (%lu != %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			       item->parman_item.index, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (used_items != test_parman->used_items) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		pr_err("Number of used items in array does not match (%u != %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		       used_items, test_parman->used_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (last_unused_items >= TEST_PARMAN_RESIZE_STEP_COUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		pr_err("Number of unused item at the end of array is bigger than resize step (%u >= %lu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		       last_unused_items, TEST_PARMAN_RESIZE_STEP_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	pr_info("Priority array check successful\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int test_parman_lsort(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	struct test_parman *test_parman;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	test_parman = test_parman_create(&test_parman_lsort_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (IS_ERR(test_parman))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return PTR_ERR(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	err = test_parman_run(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	err = test_parman_check_array(test_parman, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	test_parman_destroy(test_parman);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static int __init test_parman_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	return test_parman_lsort();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void __exit test_parman_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) module_init(test_parman_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) module_exit(test_parman_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) MODULE_LICENSE("Dual BSD/GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) MODULE_DESCRIPTION("Test module for parman");