From patchwork Wed May 4 16:45:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 569859 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39926C433EF for ; Wed, 4 May 2022 16:57:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354669AbiEDRB3 (ORCPT ); Wed, 4 May 2022 13:01:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355537AbiEDRAO (ORCPT ); Wed, 4 May 2022 13:00:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D363D4B84F; Wed, 4 May 2022 09:51:49 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 4EEA0B827AA; Wed, 4 May 2022 16:51:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE404C385A5; Wed, 4 May 2022 16:51:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651683104; bh=xM5Gy52mF7BEsiFWmHdJksaZ8bgXdg0D7bEd5jt04ko=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1MksxrBuooV15Z9iogsO6HOfQKTcgzVwEOHQ6u1tb6lSQZZZkEYRHw+538rw1UBmy T1xzZgH4m6oSRKY6MalLVU1p+uxut17ZEgidsmXi8HLmSLFmGykkE6mRZVr17Xv3hH A1sPYYUGMUQ9HENnHvDXvBIfEbr0EGjKhAynQIqk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zqiang , Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Andrew Morton , Linus Torvalds Subject: [PATCH 5.10 108/129] kasan: prevent cpu_quarantine corruption when CPU offline and cache shrink occur at same time Date: Wed, 4 May 2022 18:45:00 +0200 Message-Id: <20220504153029.633200099@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504153021.299025455@linuxfoundation.org> References: <20220504153021.299025455@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Zqiang commit 31fa985b4196f8a66f027672e9bf2b81fea0417c upstream. kasan_quarantine_remove_cache() is called in kmem_cache_shrink()/ destroy(). The kasan_quarantine_remove_cache() call is protected by cpuslock in kmem_cache_destroy() to ensure serialization with kasan_cpu_offline(). However the kasan_quarantine_remove_cache() call is not protected by cpuslock in kmem_cache_shrink(). When a CPU is going offline and cache shrink occurs at same time, the cpu_quarantine may be corrupted by interrupt (per_cpu_remove_cache operation). So add a cpu_quarantine offline flags check in per_cpu_remove_cache(). [akpm@linux-foundation.org: add comment, per Zqiang] Link: https://lkml.kernel.org/r/20220414025925.2423818-1-qiang1.zhang@intel.com Signed-off-by: Zqiang Reviewed-by: Dmitry Vyukov Cc: Andrey Ryabinin Cc: Alexander Potapenko Cc: Andrey Konovalov Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/kasan/quarantine.c | 7 +++++++ 1 file changed, 7 insertions(+) --- a/mm/kasan/quarantine.c +++ b/mm/kasan/quarantine.c @@ -299,6 +299,13 @@ static void per_cpu_remove_cache(void *a struct qlist_head *q; q = this_cpu_ptr(&cpu_quarantine); + /* + * Ensure the ordering between the writing to q->offline and + * per_cpu_remove_cache. Prevent cpu_quarantine from being corrupted + * by interrupt. + */ + if (READ_ONCE(q->offline)) + return; qlist_move_cache(q, &to_free, cache); qlist_free_all(&to_free, cache); }