drbd/bsc-1206791-05-prandom_u32_max.patch
heming zhao 4e5f7cbf78 Accepting request 1056192 from home:hmzhao:branches:network:ha-clustering:Factory
- drbd: fix build error against kernel v6.1.1 (bsc#1206791)
  * update bsc-1201335_06-bdi.patch commit log (no code change)
    + bsc-1201335_06-bdi.patch
  * update bsc-1202600_02-dax-introduce-DAX_RECOVERY_WRITE-dax-access-mode.patch (no code change)
    + bsc-1202600_02-dax-introduce-DAX_RECOVERY_WRITE-dax-access-mode.patch
  * using upstream patch to replace exist patch
    - bsc-1204596_02-drbd-stop-using-bdevname-in-drbd_report_io_error.patch
    + bsc-1204596_02-drbd-remove-usage-of-bdevname.patch
  * add new patches
    + bsc-1206791-01-drbd-add-comments-explaining-removal-of-bdi-congesti.patch
    + bsc-1206791-02-drbd-fix-static-analysis-warnings.patch
    + bsc-1206791-03-drbd-fix-warning-about-initializing-multiple-struct-.patch
    + bsc-1206791-04-blk_queue_split__no_present.patch
    + bsc-1206791-05-prandom_u32_max.patch
    + bsc-1206791-06-write_zeroes__no_capable.patch
    + bsc-1206791-07-drbd-fix-use-after-free-bugs-in-get_initial_state.patch
    + bsc-1206791-08-lib-lru_cache-Fixed-array-overflow-caused-by-incorre.patch
    + bsc-1206791-09-pmem-use-fs_dax_get_by_bdev-instead-of-dax_get_by_ho.patch

OBS-URL: https://build.opensuse.org/request/show/1056192
OBS-URL: https://build.opensuse.org/package/show/network:ha-clustering:Factory/drbd?expand=0&rev=142
2023-01-05 13:24:33 +00:00

117 lines
3.3 KiB
Diff

by heming.zhao@suse.com on 20220105
This is SUSE special patch, and this patch cocci codes were partly
copied from upstream kernel commit 81895a65ec63ee1daec3255dc1a06675d2fbe915
("treewide: use prandom_u32_max() when possible, part 1").
Because I don't know cocci syntax, so in drbd_state.c this patch
directly modifies prandom_u32() to prandom_u32_max(U32_MAX). All
other cases use cocci to modify.
---
diff -Nupr a/drbd/drbd-kernel-compat/cocci/prandom_u32__no_present.cocci b/drbd/drbd-kernel-compat/cocci/prandom_u32__no_present.cocci
--- a/drbd/drbd-kernel-compat/cocci/prandom_u32__no_present.cocci 2023-01-04 23:15:30.435899748 +0800
+++ b/drbd/drbd-kernel-compat/cocci/prandom_u32__no_present.cocci 2023-01-05 08:58:23.177198823 +0800
@@ -1,3 +1,76 @@
-@@ @@
-- prandom_u32()
-+ random32()
+@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)
diff -Nupr a/drbd/drbd_state.c b/drbd/drbd_state.c
--- a/drbd/drbd_state.c 2023-01-05 09:00:01.252434773 +0800
+++ b/drbd/drbd_state.c 2023-01-05 09:02:47.519078927 +0800
@@ -4488,7 +4488,7 @@ change_cluster_wide_state(bool (*change)
}
do
- reply->tid = prandom_u32();
+ reply->tid = prandom_u32_max(U32_MAX);
while (!reply->tid);
request.tid = cpu_to_be32(reply->tid);
@@ -4716,7 +4716,7 @@ retry:
*reply = (struct twopc_reply) { 0 };
do
- reply->tid = prandom_u32();
+ reply->tid = prandom_u32_max(U32_MAX);
while (!reply->tid);
request.tid = cpu_to_be32(reply->tid);