Skip to content

Commit 90b7f02

Browse files
pccsmb49
authored andcommitted
mm, kasan: use compare-exchange operation to set KASAN page tag
BugLink: https://bugs.launchpad.net/bugs/1964422 commit 27fe733 upstream. 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]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Kamal Mostafa <[email protected]> Signed-off-by: Stefan Bader <[email protected]>
1 parent 3b9ffcc commit 90b7f02

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
@@ -1507,11 +1507,18 @@ static inline u8 page_kasan_tag(const struct page *page)
15071507

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

15171524
static inline void page_kasan_tag_reset(struct page *page)

0 commit comments

Comments
 (0)