Skip to content

Commit 7f434e1

Browse files
GeHao01994tehcaster
authored andcommitted
slab: Fix obj_ext mistakenly considered NULL due to race condition
If two competing threads enter alloc_slab_obj_exts(), and the one that allocates the vector wins the cmpxchg(), the other thread that failed allocation mistakenly assumes that slab->obj_exts is still empty due to its own allocation failure. This will then trigger warnings with CONFIG_MEM_ALLOC_PROFILING_DEBUG checks in the subsequent free path. Therefore, let's check the result of cmpxchg() to see if marking the allocation as failed was successful. If it wasn't, check whether the winning side has succeeded its allocation (it might have been also marking it as failed) and if yes, return success. Suggested-by: Harry Yoo <[email protected]> Fixes: f7381b9 ("slab: mark slab->obj_exts allocation failures unconditionally") Cc: <[email protected]> Signed-off-by: Hao Ge <[email protected]> Link: https://patch.msgid.link/[email protected] Reviewed-by: Suren Baghdasaryan <[email protected]> Reviewed-by: Harry Yoo <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent eecd7cb commit 7f434e1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

mm/slub.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,9 +2052,9 @@ static inline void mark_objexts_empty(struct slabobj_ext *obj_exts)
20522052
}
20532053
}
20542054

2055-
static inline void mark_failed_objexts_alloc(struct slab *slab)
2055+
static inline bool mark_failed_objexts_alloc(struct slab *slab)
20562056
{
2057-
cmpxchg(&slab->obj_exts, 0, OBJEXTS_ALLOC_FAIL);
2057+
return cmpxchg(&slab->obj_exts, 0, OBJEXTS_ALLOC_FAIL) == 0;
20582058
}
20592059

20602060
static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
@@ -2076,7 +2076,7 @@ static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
20762076
#else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */
20772077

20782078
static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) {}
2079-
static inline void mark_failed_objexts_alloc(struct slab *slab) {}
2079+
static inline bool mark_failed_objexts_alloc(struct slab *slab) { return false; }
20802080
static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
20812081
struct slabobj_ext *vec, unsigned int objects) {}
20822082

@@ -2124,8 +2124,14 @@ int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,
21242124
slab_nid(slab));
21252125
}
21262126
if (!vec) {
2127-
/* Mark vectors which failed to allocate */
2128-
mark_failed_objexts_alloc(slab);
2127+
/*
2128+
* Try to mark vectors which failed to allocate.
2129+
* If this operation fails, there may be a racing process
2130+
* that has already completed the allocation.
2131+
*/
2132+
if (!mark_failed_objexts_alloc(slab) &&
2133+
slab_obj_exts(slab))
2134+
return 0;
21292135

21302136
return -ENOMEM;
21312137
}

0 commit comments

Comments
 (0)