Skip to contentSkip to conversation

sched: fix data race in RED random number generation

PATCH Applied Stephen Hemminger 2 participants 2 messages Oct 10 – Oct 15

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched: fix data race in RED random number generation

The RED scheduler used its own random number generator which was weak and buggy. Change standard rte_rand() which has better randomness and is thread safe.

There is no prior deprecation notice for relatedd symbols. They were never meant to be part of the API: rte_fast_rand() is the internal generator of the dropper and the two variables are its state, all exposed only because the drop test is inline in rte_red.h.

Fixes de3cfa2c9823 ("sched: initial import")
---
 doc/guides/rel_notes/release_26_11.rst |  3 +++
 lib/sched/rte_red.c                    |  9 +--------
 lib/sched/rte_red.h                    | 26 ++------------------------
 3 files changed, 6 insertions(+), 32 deletions(-)
doc/guides/rel_notes/release_26_11.rst +3 -0
index 88c06025b0..d4a3a7e7f2 100644
@@ -226,6 +226,9 @@ Removed Items
   The only remaining method for detecting LLDP packets is by using
   the mbuf packet type in conjunction with the ``enable_lldp`` devarg.
 
+* sched: Removed the private random number generator used by RED: the
+  inline function ``rte_fast_rand()``.
+
 
 API Changes
 -----------
lib/sched/rte_red.c +1 -8
index d7534d0bee..6d20afa9dc 100644
@@ -5,14 +5,9 @@
 #include <math.h>
 #include <eal_export.h>
 #include "rte_red.h"
-#include <rte_random.h>
 #include <rte_common.h>
 
-static int rte_red_init_done = 0;     /**< Flag to indicate that global initialisation is done */
-RTE_EXPORT_SYMBOL(rte_red_rand_val)
-uint32_t rte_red_rand_val = 0;        /**< Random value cache */
-RTE_EXPORT_SYMBOL(rte_red_rand_seed)
-uint32_t rte_red_rand_seed = 0;       /**< Seed for random number generation */
+static int rte_red_init_done;         /**< Flag to indicate that global initialisation is done */
 
 /**
  * table[i] = log2(1-Wq) * Scale * -1
@@ -116,8 +111,6 @@ rte_red_config_init(struct rte_red_config *red_cfg,
 	 *  Initialize the RED module if not already done
 	 */
 	if (!rte_red_init_done) {
-		rte_red_rand_seed = rte_rand();
-		rte_red_rand_val = rte_fast_rand();
 		__rte_red_init_tables();
 		rte_red_init_done = 1;
 	}
lib/sched/rte_red.h +2 -24
index 3b90cc46a9..ad11941ed5 100644
@@ -15,6 +15,7 @@
 #include <rte_debug.h>
 #include <rte_cycles.h>
 #include <rte_branch_prediction.h>
+#include <rte_random.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -34,8 +35,6 @@ extern "C" {
 /**
  * Externs
  */
-extern uint32_t rte_red_rand_val;
-extern uint32_t rte_red_rand_seed;
 extern uint16_t rte_red_log2_1_minus_Wq[RTE_RED_WQ_LOG2_NUM];
 extern uint16_t rte_red_pow2_frac_inv[16];
 
@@ -102,23 +101,6 @@ rte_red_config_init(struct rte_red_config *red_cfg,
 	const uint16_t max_th,
 	const uint16_t maxp_inv);
 
-/**
- * @brief Generate random number for RED
- *
- * Implementation based on:
- * http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/
- *
- * 10 bit shift has been found through empirical tests (was 16).
- *
- * @return Random number between 0 and (2^22 - 1)
- */
-static inline uint32_t
-rte_fast_rand(void)
-{
-	rte_red_rand_seed = (214013 * rte_red_rand_seed) + 2531011;
-	return rte_red_rand_seed >> 10;
-}
-
 /**
  * @brief calculate factor to scale average queue size when queue
  *        becomes empty
@@ -293,12 +275,8 @@ __rte_red_drop(const struct rte_red_config *red_cfg, struct rte_red *red)
 
 	pa_den = red_cfg->pa_const - pa_num_count;
 
-	/* If drop, generate and save random number to be used next time */
-	if (unlikely((rte_red_rand_val % pa_den) < pa_num)) {
-		rte_red_rand_val = rte_fast_rand();
-
+	if (unlikely((rte_rand() % pa_den) < pa_num))
 		return 1;
-	}
 
 	/* No drop */
 	return 0;
Signature · 1 line
2.53.0
#1 lore raw

10/10/2026 03:59, Stephen Hemminger:

Quoted 11 lines

The RED scheduler used its own random number generator which was weak and buggy. Change standard rte_rand() which has better randomness and is thread safe.

There is no prior deprecation notice for relatedd symbols. They were never meant to be part of the API: rte_fast_rand() is the internal generator of the dropper and the two variables are its state, all exposed only because the drop test is inline in rte_red.h.

Fixes de3cfa2c9823 ("sched: initial import")

Applied, thanks.