Skip to content

Commit 27fe733

Browse files
pcctorvalds
authored andcommitted
mm, kasan: use compare-exchange operation to set KASAN page tag
It has been reported that the tag setting operation on newly-allocated pages can cause the page flags to be corrupted when performed concurrently with other flag updates as a result of the use of non-atomic operations. Fix the problem by using a compare-exchange loop to update the tag. Link: https://lkml.kernel.org/r/[email protected] Link: https://linux-review.googlesource.com/id/I456b24a2b9067d93968d43b4bb3351c0cec63101 Fixes: 2813b9c ("kasan, mm, arm64: tag non slab memory allocated via pagealloc") Signed-off-by: Peter Collingbourne <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 09c6304 commit 27fe733

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

include/linux/mm.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,11 +1506,18 @@ static inline u8 page_kasan_tag(const struct page *page)
15061506

15071507
static inline void page_kasan_tag_set(struct page *page, u8 tag)
15081508
{
1509-
if (kasan_enabled()) {
1510-
tag ^= 0xff;
1511-
page->flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
1512-
page->flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
1513-
}
1509+
unsigned long old_flags, flags;
1510+
1511+
if (!kasan_enabled())
1512+
return;
1513+
1514+
tag ^= 0xff;
1515+
old_flags = READ_ONCE(page->flags);
1516+
do {
1517+
flags = old_flags;
1518+
flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
1519+
flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
1520+
} while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags)));
15141521
}
15151522

15161523
static inline void page_kasan_tag_reset(struct page *page)

0 commit comments

Comments
 (0)