mirror of
https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux
synced 2025-11-11 11:24:50 +10:00
Rather than incurring a division or requesting too many random bytes for
the given range, use the prandom_u32_max() function, which only takes
the minimum required bytes from the RNG and avoids divisions. This was
done mechanically with this coccinelle script:
@basic@
expression E;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u64;
@@
(
- ((T)get_random_u32() % (E))
+ prandom_u32_max(E)
|
- ((T)get_random_u32() & ((E) - 1))
+ prandom_u32_max(E * XXX_MAKE_SURE_E_IS_POW2)
|
- ((u64)(E) * get_random_u32() >> 32)
+ prandom_u32_max(E)
|
- ((T)get_random_u32() & ~PAGE_MASK)
+ prandom_u32_max(PAGE_SIZE)
)
@multi_line@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
identifier RAND;
expression E;
@@
- RAND = get_random_u32();
... when != RAND
- RAND %= (E);
+ RAND = prandom_u32_max(E);
// Find a potential literal
@literal_mask@
expression LITERAL;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
position p;
@@
((T)get_random_u32()@p & (LITERAL))
// Add one to the literal.
@script:python add_one@
literal << literal_mask.LITERAL;
RESULT;
@@
value = None
if literal.startswith('0x'):
value = int(literal, 16)
elif literal[0] in '123456789':
value = int(literal, 10)
if value is None:
print("I don't know how to handle %s" % (literal))
cocci.include_match(False)
elif value == 2**32 - 1 or value == 2**31 - 1 or value == 2**24 - 1 or value == 2**16 - 1 or value == 2**8 - 1:
print("Skipping 0x%x for cleanup elsewhere" % (value))
cocci.include_match(False)
elif value & (value + 1) != 0:
print("Skipping 0x%x because it's not a power of two minus one" % (value))
cocci.include_match(False)
elif literal.startswith('0x'):
coccinelle.RESULT = cocci.make_expr("0x%x" % (value + 1))
else:
coccinelle.RESULT = cocci.make_expr("%d" % (value + 1))
// Replace the literal mask with the calculated result.
@plus_one@
expression literal_mask.LITERAL;
position literal_mask.p;
expression add_one.RESULT;
identifier FUNC;
@@
- (FUNC()@p & (LITERAL))
+ prandom_u32_max(RESULT)
@collapse_ret@
type T;
identifier VAR;
expression E;
@@
{
- T VAR;
- VAR = (E);
- return VAR;
+ return E;
}
@drop_var@
type T;
identifier VAR;
@@
{
- T VAR;
... when != VAR
}
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: KP Singh <kpsingh@kernel.org>
Reviewed-by: Jan Kara <jack@suse.cz> # for ext4 and sbitmap
Reviewed-by: Christoph Böhmwalder <christoph.boehmwalder@linbit.com> # for drbd
Acked-by: Jakub Kicinski <kuba@kernel.org>
Acked-by: Heiko Carstens <hca@linux.ibm.com> # for s390
Acked-by: Ulf Hansson <ulf.hansson@linaro.org> # for mmc
Acked-by: Darrick J. Wong <djwong@kernel.org> # for xfs
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
123 lines
3.3 KiB
C
123 lines
3.3 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
#include <kunit/test.h>
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/list_sort.h>
|
|
#include <linux/list.h>
|
|
#include <linux/module.h>
|
|
#include <linux/printk.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/random.h>
|
|
|
|
/*
|
|
* The pattern of set bits in the list length determines which cases
|
|
* are hit in list_sort().
|
|
*/
|
|
#define TEST_LIST_LEN (512+128+2) /* not including head */
|
|
|
|
#define TEST_POISON1 0xDEADBEEF
|
|
#define TEST_POISON2 0xA324354C
|
|
|
|
struct debug_el {
|
|
unsigned int poison1;
|
|
struct list_head list;
|
|
unsigned int poison2;
|
|
int value;
|
|
unsigned int serial;
|
|
};
|
|
|
|
static void check(struct kunit *test, struct debug_el *ela, struct debug_el *elb)
|
|
{
|
|
struct debug_el **elts = test->priv;
|
|
|
|
KUNIT_EXPECT_LT_MSG(test, ela->serial, (unsigned int)TEST_LIST_LEN, "incorrect serial");
|
|
KUNIT_EXPECT_LT_MSG(test, elb->serial, (unsigned int)TEST_LIST_LEN, "incorrect serial");
|
|
|
|
KUNIT_EXPECT_PTR_EQ_MSG(test, elts[ela->serial], ela, "phantom element");
|
|
KUNIT_EXPECT_PTR_EQ_MSG(test, elts[elb->serial], elb, "phantom element");
|
|
|
|
KUNIT_EXPECT_EQ_MSG(test, ela->poison1, TEST_POISON1, "bad poison");
|
|
KUNIT_EXPECT_EQ_MSG(test, ela->poison2, TEST_POISON2, "bad poison");
|
|
|
|
KUNIT_EXPECT_EQ_MSG(test, elb->poison1, TEST_POISON1, "bad poison");
|
|
KUNIT_EXPECT_EQ_MSG(test, elb->poison2, TEST_POISON2, "bad poison");
|
|
}
|
|
|
|
/* `priv` is the test pointer so check() can fail the test if the list is invalid. */
|
|
static int cmp(void *priv, const struct list_head *a, const struct list_head *b)
|
|
{
|
|
struct debug_el *ela, *elb;
|
|
|
|
ela = container_of(a, struct debug_el, list);
|
|
elb = container_of(b, struct debug_el, list);
|
|
|
|
check(priv, ela, elb);
|
|
return ela->value - elb->value;
|
|
}
|
|
|
|
static void list_sort_test(struct kunit *test)
|
|
{
|
|
int i, count = 1;
|
|
struct debug_el *el, **elts;
|
|
struct list_head *cur;
|
|
LIST_HEAD(head);
|
|
|
|
elts = kunit_kcalloc(test, TEST_LIST_LEN, sizeof(*elts), GFP_KERNEL);
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, elts);
|
|
test->priv = elts;
|
|
|
|
for (i = 0; i < TEST_LIST_LEN; i++) {
|
|
el = kunit_kmalloc(test, sizeof(*el), GFP_KERNEL);
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, el);
|
|
|
|
/* force some equivalencies */
|
|
el->value = prandom_u32_max(TEST_LIST_LEN / 3);
|
|
el->serial = i;
|
|
el->poison1 = TEST_POISON1;
|
|
el->poison2 = TEST_POISON2;
|
|
elts[i] = el;
|
|
list_add_tail(&el->list, &head);
|
|
}
|
|
|
|
list_sort(test, &head, cmp);
|
|
|
|
for (cur = head.next; cur->next != &head; cur = cur->next) {
|
|
struct debug_el *el1;
|
|
int cmp_result;
|
|
|
|
KUNIT_ASSERT_PTR_EQ_MSG(test, cur->next->prev, cur,
|
|
"list is corrupted");
|
|
|
|
cmp_result = cmp(test, cur, cur->next);
|
|
KUNIT_ASSERT_LE_MSG(test, cmp_result, 0, "list is not sorted");
|
|
|
|
el = container_of(cur, struct debug_el, list);
|
|
el1 = container_of(cur->next, struct debug_el, list);
|
|
if (cmp_result == 0) {
|
|
KUNIT_ASSERT_LE_MSG(test, el->serial, el1->serial,
|
|
"order of equivalent elements not preserved");
|
|
}
|
|
|
|
check(test, el, el1);
|
|
count++;
|
|
}
|
|
KUNIT_EXPECT_PTR_EQ_MSG(test, head.prev, cur, "list is corrupted");
|
|
|
|
KUNIT_EXPECT_EQ_MSG(test, count, TEST_LIST_LEN,
|
|
"list length changed after sorting!");
|
|
}
|
|
|
|
static struct kunit_case list_sort_cases[] = {
|
|
KUNIT_CASE(list_sort_test),
|
|
{}
|
|
};
|
|
|
|
static struct kunit_suite list_sort_suite = {
|
|
.name = "list_sort",
|
|
.test_cases = list_sort_cases,
|
|
};
|
|
|
|
kunit_test_suites(&list_sort_suite);
|
|
|
|
MODULE_LICENSE("GPL");
|