mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: George Spelvin <lkml@sdf.org>
To: linux-kernel@vger.kernel.org, lkml@sdf.org
Cc: Akinobu Mita <akinobu.mita@gmail.com>, Anton Blanchard <anton@samba.org>
Subject: [RFC PATCH v1 02/50] lib/fault-inject.c: Fix off-by-one error in probability
Date: Wed, 4 Dec 2019 00:25:58 -0500	[thread overview]
Message-ID: <202003281643.02SGh6ii026355@sdf.org> (raw)

This is the combination of two fixes.  First, use prandom_u32_max() for
efficiency:
-	if (attr->probability <= prandom_u32() % 100)
+	if (attr->probability <= prandom_u32_max(100))
 		return false

And then a bug-fix:
-	if (attr->probability <= prandom_u32_max(100))
+	if (attr->probability < prandom_u32_max(100))
 		return false

Before, probability = 1 would succeed 2% of the time and 99 would
succeed 100% of the time.  (0% was caught by an earlier test.)

Signed-off-by: George Spelvin <lkml@sdf.org>
Cc: Akinobu Mita <akinobu.mita@gmail.com>
Cc: Anton Blanchard <anton@samba.org>
---
 lib/fault-inject.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/fault-inject.c b/lib/fault-inject.c
index 8186ca84910bc..e20151fa5515e 100644
--- a/lib/fault-inject.c
+++ b/lib/fault-inject.c
@@ -134,7 +134,7 @@ bool should_fail(struct fault_attr *attr, ssize_t size)
 			return false;
 	}
 
-	if (attr->probability <= prandom_u32() % 100)
+	if (attr->probability < prandom_u32_max(100))
 		return false;
 
 	if (!fail_stacktrace(attr))
-- 
2.26.0


                 reply	other threads:[~2020-03-28 16:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202003281643.02SGh6ii026355@sdf.org \
    --to=lkml@sdf.org \
    --cc=akinobu.mita@gmail.com \
    --cc=anton@samba.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®