From patchwork Thu Jan 13 03:14:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12712183 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15FABC433FE for ; Thu, 13 Jan 2022 03:14:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2EEF66B0072; Wed, 12 Jan 2022 22:14:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 29E336B0073; Wed, 12 Jan 2022 22:14:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 164E26B0074; Wed, 12 Jan 2022 22:14:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0064.hostedemail.com [216.40.44.64]) by kanga.kvack.org (Postfix) with ESMTP id 06DF26B0072 for ; Wed, 12 Jan 2022 22:14:45 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id B8C3B7C5B0 for ; Thu, 13 Jan 2022 03:14:44 +0000 (UTC) X-FDA: 79023796488.07.C22D8F6 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf24.hostedemail.com (Postfix) with ESMTP id 498FB180002 for ; Thu, 13 Jan 2022 03:14:44 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id s13-20020a252c0d000000b00611786a6925so8778022ybs.8 for ; Wed, 12 Jan 2022 19:14:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=Vn5oKbnGZSK29/TeD2jdsTWSsU6BDg6nOy220ST8fyE=; b=ZAVJQmK76ka/sB9vdoEgbpDzHHvQ9eaRaksI3ggjA9gVsDwXmYzobW1WITyq+eXbJ7 d0EPMyljiyJTxGq4RZ4LdlWDk5U/f27wU+O+CI+bpJrKu6DWTwONrF4XF/dI+5spLRJJ FMJOwro7nKdAhO0EPrCEagaEgRODbulo/AfI8Ip2y+/40eZyHd3uS8EaKOxk8dtDUxAT EdYGdxK08jPgIaXE8gb2qJwaJnY8ZcAPgbA8nqk43dlGBVnXfgXaKu/nuTKvwyqMv0+e r72JnMbrVNDCxFAdcNs+NJGc4m0I5192QGjfFQAX4xji6GWCU1dSEzfNXjyE5IsgG45s KBXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=Vn5oKbnGZSK29/TeD2jdsTWSsU6BDg6nOy220ST8fyE=; b=nk/LgC4w8n6aOXiYPgiyd4YmR0LotowsgfWJcDSJ+0JDsooP94LN6Atd7DPZ7+pUW3 ljrkNoJPAH1J4VvJTVdrcVd1TCDfl8OhJjHCrrDkAeuJtJBZ7mvSOYGX4+e9nsNt+9gk i+FrTEST6yfEeFP9T6LJr0pq2lcnY2adrv6WFYoGs7FAOgYnbiXE07jvUBSBHQon6thp NzNF1QzLD3UIiHUg6tYLGKAt8T5wbi+QA/pzI8BnmUssMWbPnccRSH4vPW+gfMeMiPoh 0wVmLKXEJ8qenITfPjq1SxfdX86DcsaBCs5NErOwBsX3CveSmPMLi0TrP1KSW7e8Vsa5 +/Kw== X-Gm-Message-State: AOAM531o+Hg6uu1TodU28ZwcyAL9Ys5QXIovtTzeb8MOqqXXMn9cxh2N +kNzPzIFyYwJGZgZ1JzoRu23kvY= X-Google-Smtp-Source: ABdhPJwo+5zEuBBP9DSC2b29Tuo0F0jw0Ox/T3uvpDRiQLRasor571HDpoxgJoDoibMecszhSTXgi4E= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:55e2:d4be:752f:9807]) (user=pcc job=sendgmr) by 2002:a05:6902:725:: with SMTP id l5mr3323683ybt.575.1642043683429; Wed, 12 Jan 2022 19:14:43 -0800 (PST) Date: Wed, 12 Jan 2022 19:14:34 -0800 Message-Id: <20220113031434.464992-1-pcc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH] mm: use compare-exchange operation to set KASAN page tag From: Peter Collingbourne To: Andrey Konovalov , Andrew Morton Cc: Peter Collingbourne , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org X-Rspamd-Queue-Id: 498FB180002 X-Stat-Signature: ztx9t4m9fftrj1zg1jrkyguh7mknzcm4 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ZAVJQmK7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3I5nfYQMKCOgZMMQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--pcc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3I5nfYQMKCOgZMMQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--pcc.bounces.google.com X-Rspamd-Server: rspam02 X-HE-Tag: 1642043684-6324 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Signed-off-by: Peter Collingbourne Link: https://linux-review.googlesource.com/id/I456b24a2b9067d93968d43b4bb3351c0cec63101 Fixes: 2813b9c02962 ("kasan, mm, arm64: tag non slab memory allocated via pagealloc") Cc: stable@vger.kernel.org --- include/linux/mm.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index c768a7c81b0b..b544b0a9f537 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1531,11 +1531,17 @@ static inline u8 page_kasan_tag(const struct page *page) static inline void page_kasan_tag_set(struct page *page, u8 tag) { - if (kasan_enabled()) { - tag ^= 0xff; - page->flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT); - page->flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT; - } + unsigned long old_flags, flags; + + if (!kasan_enabled()) + return; + + tag ^= 0xff; + do { + old_flags = flags = page->flags; + flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT); + flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT; + } while (unlikely(cmpxchg(&page->flags, old_flags, flags) != old_flags)); } static inline void page_kasan_tag_reset(struct page *page)