Skip to content

Commit c4e490c

Browse files
John Sperbecktorvalds
John Sperbeck
authored andcommitted
mm/slab.c: fix SLAB freelist randomization duplicate entries
This patch fixes a bug in the freelist randomization code. When a high random number is used, the freelist will contain duplicate entries. It will result in different allocations sharing the same chunk. It will result in odd behaviours and crashes. It should be uncommon but it depends on the machines. We saw it happening more often on some machines (every few hours of running tests). Fixes: c7ce4f6 ("mm: SLAB freelist randomization") Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: John Sperbeck <[email protected]> Signed-off-by: Thomas Garnier <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: David Rientjes <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b09ab05 commit c4e490c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

mm/slab.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,7 +2457,6 @@ union freelist_init_state {
24572457
unsigned int pos;
24582458
unsigned int *list;
24592459
unsigned int count;
2460-
unsigned int rand;
24612460
};
24622461
struct rnd_state rnd_state;
24632462
};
@@ -2483,8 +2482,7 @@ static bool freelist_state_initialize(union freelist_init_state *state,
24832482
} else {
24842483
state->list = cachep->random_seq;
24852484
state->count = count;
2486-
state->pos = 0;
2487-
state->rand = rand;
2485+
state->pos = rand % count;
24882486
ret = true;
24892487
}
24902488
return ret;
@@ -2493,7 +2491,9 @@ static bool freelist_state_initialize(union freelist_init_state *state,
24932491
/* Get the next entry on the list and randomize it using a random shift */
24942492
static freelist_idx_t next_random_slot(union freelist_init_state *state)
24952493
{
2496-
return (state->list[state->pos++] + state->rand) % state->count;
2494+
if (state->pos >= state->count)
2495+
state->pos = 0;
2496+
return state->list[state->pos++];
24972497
}
24982498

24992499
/* Swap two freelist entries */

0 commit comments

Comments
 (0)