From patchwork Mon Nov 14 16:45:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 19918 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2250150wru; Mon, 14 Nov 2022 08:57:57 -0800 (PST) X-Google-Smtp-Source: AA0mqf5fOvF5+ck6rfPv1h27FQiBacOFSvFBE9Jf3etOaY6yO11tpQdiS8o2z4XfZtLoBOaqiaRP X-Received: by 2002:a17:906:824e:b0:7ae:77ef:d048 with SMTP id f14-20020a170906824e00b007ae77efd048mr11234601ejx.740.1668445077077; Mon, 14 Nov 2022 08:57:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668445077; cv=none; d=google.com; s=arc-20160816; b=TExvBn9xD+m6RSbLEuHDgDOU9Xlhz5k8b2bKeieV7XZ3h1RUeSf5I3tsZhi8X1HtH5 Y3q5R7Kf3qRbfIXa1izD0x6LhSP5arzRW2UyRFSAWEXSlNDcd4NMBQJ0rxEfRJqoWUdL p5jdGIdZbxQm54EJPKuXSBZLJ9FlNdNhBT8sr+IJ1EsLU8sdK+mCs3bl0Hk85RWZW/kN W5IsSctGwtHr/6qfKbhVhhx0SRxsLn8sw9ALab/2GAdz9eu9omOosGxvdarJnUTLO260 hydRIwPqvmNa2V0kbI54rakSibScl5PNKhsHf84wT8LfvcrsQsjj2+1uH+7gEaX+7zt9 xNUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GUOLjwe2IeIpzWXOEF9lexW8mdcNzGTdZsyQ38BdoPo=; b=D2XJ3MnyTaCcNhmAZOofBki0yd0jhE1JN1hF1Geg4ROA0KnhkdpTvWMQBYR/57RNhq 4d28tiagyRtsv0cN9QobMyYEatDEk59lYQ5OmSO1kw1TrYNnFXTVFIu0yNimTIKP59cG /XvH0ISwaLhBPogO90XaPw+UaZSqciGItKfFI/n+KPfkT8J6DwMlUY3FJesDQzDBl4Is JKnTAQL17xfiMtvlKxF3Ry4kzj8dKTPiY6waZSoI0PzbQn5gDkTiZWR2NoLbKLGu2s/+ 0e/nwMMyd9+s2pmbV03EC9nPkrWFnAYe+GAXpgXJ98OliVtXydyw9NyP69LIZK1FJa95 8VAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b="IZwW/pXt"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ho33-20020a1709070ea100b007af1002db2bsi1582700ejc.641.2022.11.14.08.57.32; Mon, 14 Nov 2022 08:57:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b="IZwW/pXt"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238019AbiKNQrS (ORCPT + 99 others); Mon, 14 Nov 2022 11:47:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238100AbiKNQrK (ORCPT ); Mon, 14 Nov 2022 11:47:10 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D825A2F028; Mon, 14 Nov 2022 08:47:09 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 74137612DF; Mon, 14 Nov 2022 16:47:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 883ECC433C1; Mon, 14 Nov 2022 16:47:05 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="IZwW/pXt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1668444425; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GUOLjwe2IeIpzWXOEF9lexW8mdcNzGTdZsyQ38BdoPo=; b=IZwW/pXtgQjRmiQ0PtVQPt2pepd0WzFJDYnD9391y9kx/P1IoPzFa7R23Ul6Y1I6NyMf54 720Ds0WvTdN9pJBmGYz6ky6KfVM3twh/wLmfzS137SqlUCUm/EnhwjnExPBVNyJaC0r/yz hUqcqQRMMcMIQm2imAIC1tMqnYFTlUk= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 04ea1518 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 14 Nov 2022 16:47:04 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org, patches@lists.linux.dev Cc: "Jason A. Donenfeld" , Kees Cook , Greg Kroah-Hartman , Jakub Kicinski , Russell King , Catalin Marinas , Thomas Bogendoerfer , Heiko Carstens , Herbert Xu , =?utf-8?q?Christoph_B=C3=B6hmwalder?= , Jani Nikula , Jason Gunthorpe , Sakari Ailus , "Martin K . Petersen" , Theodore Ts'o , Andreas Dilger , Jaegeuk Kim , Richard Weinberger , "Darrick J . Wong" , SeongJae Park , Thomas Gleixner , Andrew Morton , Michael Ellerman , Helge Deller , netdev@vger.kernel.org, linux-crypto@vger.kernel.org, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mmc@vger.kernel.org, linux-parisc@vger.kernel.org Subject: [PATCH v2 2/3] treewide: use get_random_u32_{above,below}() instead of manual loop Date: Mon, 14 Nov 2022 17:45:57 +0100 Message-Id: <20221114164558.1180362-3-Jason@zx2c4.com> In-Reply-To: <20221114164558.1180362-1-Jason@zx2c4.com> References: <20221114164558.1180362-1-Jason@zx2c4.com> MIME-Version: 1.0 X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749491465504023332?= X-GMAIL-MSGID: =?utf-8?q?1749491465504023332?= These cases were done with this Coccinelle: @@ expression E; identifier I; @@ - do { ... when != I - I = get_random_u32(); ... when != I - } while (I > E); + I = get_random_u32_below(E + 1); @@ expression E; identifier I; @@ - do { ... when != I - I = get_random_u32(); ... when != I - } while (I >= E); + I = get_random_u32_below(E); @@ expression E; identifier I; @@ - do { ... when != I - I = get_random_u32(); ... when != I - } while (I < E); + I = get_random_u32_above(E - 1); @@ expression E; identifier I; @@ - do { ... when != I - I = get_random_u32(); ... when != I - } while (I <= E); + I = get_random_u32_above(E); @@ identifier I; @@ - do { ... when != I - I = get_random_u32(); ... when != I - } while (!I); + I = get_random_u32_above(0); @@ identifier I; @@ - do { ... when != I - I = get_random_u32(); ... when != I - } while (I == 0); + I = get_random_u32_above(0); @@ expression E; @@ - E + 1 + get_random_u32_below(U32_MAX - E) + get_random_u32_above(E) Reviewed-by: Greg Kroah-Hartman Signed-off-by: Jason A. Donenfeld --- fs/ext4/mmp.c | 8 +------- lib/test_fprobe.c | 5 +---- lib/test_kprobes.c | 5 +---- net/ipv6/output_core.c | 8 +------- net/vmw_vsock/af_vsock.c | 3 +-- 5 files changed, 5 insertions(+), 24 deletions(-) diff --git a/fs/ext4/mmp.c b/fs/ext4/mmp.c index 588cb09c5291..4681fff6665f 100644 --- a/fs/ext4/mmp.c +++ b/fs/ext4/mmp.c @@ -262,13 +262,7 @@ void ext4_stop_mmpd(struct ext4_sb_info *sbi) */ static unsigned int mmp_new_seq(void) { - u32 new_seq; - - do { - new_seq = get_random_u32(); - } while (new_seq > EXT4_MMP_SEQ_MAX); - - return new_seq; + return get_random_u32_below(EXT4_MMP_SEQ_MAX + 1); } /* diff --git a/lib/test_fprobe.c b/lib/test_fprobe.c index e0381b3ec410..1fb56cf5e5ce 100644 --- a/lib/test_fprobe.c +++ b/lib/test_fprobe.c @@ -144,10 +144,7 @@ static unsigned long get_ftrace_location(void *func) static int fprobe_test_init(struct kunit *test) { - do { - rand1 = get_random_u32(); - } while (rand1 <= div_factor); - + rand1 = get_random_u32_above(div_factor); target = fprobe_selftest_target; target2 = fprobe_selftest_target2; target_ip = get_ftrace_location(target); diff --git a/lib/test_kprobes.c b/lib/test_kprobes.c index eeb1d728d974..1c95e5719802 100644 --- a/lib/test_kprobes.c +++ b/lib/test_kprobes.c @@ -339,10 +339,7 @@ static int kprobes_test_init(struct kunit *test) stacktrace_target = kprobe_stacktrace_target; internal_target = kprobe_stacktrace_internal_target; stacktrace_driver = kprobe_stacktrace_driver; - - do { - rand1 = get_random_u32(); - } while (rand1 <= div_factor); + rand1 = get_random_u32_above(div_factor); return 0; } diff --git a/net/ipv6/output_core.c b/net/ipv6/output_core.c index 2685c3f15e9d..b5205311f372 100644 --- a/net/ipv6/output_core.c +++ b/net/ipv6/output_core.c @@ -15,13 +15,7 @@ static u32 __ipv6_select_ident(struct net *net, const struct in6_addr *dst, const struct in6_addr *src) { - u32 id; - - do { - id = get_random_u32(); - } while (!id); - - return id; + return get_random_u32_above(0); } /* This function exists only for tap drivers that must support broken diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index ff38c5a4d174..d593d5b6d4b1 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -626,8 +626,7 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, struct sockaddr_vm new_addr; if (!port) - port = LAST_RESERVED_PORT + 1 + - get_random_u32_below(U32_MAX - LAST_RESERVED_PORT); + port = get_random_u32_above(LAST_RESERVED_PORT); vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);