Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1494649pxb; Fri, 13 Nov 2020 14:26:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJzYmnAjkLqHNxI1tL4GqMu6DFr5ofEbWU0LG1+zdQxrb0uTWbGKbbkWOiRBdyLm3RfZMFpU X-Received: by 2002:a17:906:ad85:: with SMTP id la5mr4385553ejb.423.1605306360360; Fri, 13 Nov 2020 14:26:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605306360; cv=none; d=google.com; s=arc-20160816; b=nNwzlmBZI8XhjFh5Ybs199I5tJsBaVbtglq2S7zCqHQQSPk3/FQyhEvzDo1QhEainw KsKp7c4p4iot/VokOpDKu+vdyu/G6Eh83TvAdcR+fDK+r1k3503DXReBfVd6Yl2QWllS YIFA8qY/QYF6s2RzhRGj89x+vh0G26ws9KjYiNTk+fhfAYLwCr+aLRsExyEGWY4UIdIl zRWDlAFXMwNfRD6KRG2+CyDmsrW9+d78yTC1nONcU4orrHl/W/crOgi5PBlI6g2DthMP 3h6ypNTVIAO7gKvGJHKgq2Li6Sham0sQxDAH3hm+iTS4hmBpwrM1oGaJNQwdV+/wrQ1/ o1bQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=VblPcwxF/Hme6lihpSqzEG9568rY0eTc9RDR6WgTVWw=; b=OtD1/JOg1r36kNDmXtOpl/mZQK59xx1bVH4rJolMzGnImOjuE6owUS523NO2tDxLdH ZwdCLfgpjYqoCJ1TrxSHGrwen/SXbgU10kNONYAv2GnaT6pdlVpXUAzMyYH7VVa0hhen BnU2xJPqKKBLJ0xIUbQ36Zg97zr7T/JaFui79vHKMTzdKlwoR5WFokA05Pzgv7+etA8d qpTBhG8Qj/lY9gSQw0uFbLGpeKvMdW6nTslClELkSTSNtOkP7linHHNTlY5dmP164qRG ZpphFupFk193LGLyUqM37z74fEHLoLYGbPWDnMqbCne8hjpiwh2nkYzwDSo1k1aSg9Uq 8ELg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Wogiwfd7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce10si8450736edb.515.2020.11.13.14.25.37; Fri, 13 Nov 2020 14:26:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Wogiwfd7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726606AbgKMWVb (ORCPT + 99 others); Fri, 13 Nov 2020 17:21:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726807AbgKMWU7 (ORCPT ); Fri, 13 Nov 2020 17:20:59 -0500 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B044C0617A6 for ; Fri, 13 Nov 2020 14:20:59 -0800 (PST) Received: by mail-qk1-x74a.google.com with SMTP id x2so7552893qkd.23 for ; Fri, 13 Nov 2020 14:20:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=VblPcwxF/Hme6lihpSqzEG9568rY0eTc9RDR6WgTVWw=; b=Wogiwfd7B8TITgy/dJX2SuNVBCGj1sMmA/wWfqUVUW625r77eKdpvQaDaSu3aBij18 zEhjdHmrCW/gZ7A+s95KqIfE8DNCf76WA+Nww0tJUkPRyTX4kRL6suTfbHi2qlyVafam rV+hV43GvnYUfMMgdDZbuxRdthx5R+R5/Y2EHztggMs7SRvCIai8cg3H8bvmqhAi17Nt 36le6MqSx5RpWtq05dKx2kbxkwESQg8uMTwlplCYYGRgGySvTwMQEijRKAHcWAwock2d 3rNzJuPXD5wPMUIWUOa1SBHN9tz+5KvOK3ae7A6N6hVkTe9bRJuMUVjKXK9evatpgUtX DvbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=VblPcwxF/Hme6lihpSqzEG9568rY0eTc9RDR6WgTVWw=; b=fIQIgelveruxrWBJ/FSBsB+G61Qqf8HqNmYoaQQd+IRVP93RPbpWXjJL1OAFXwL6+O cXLK+iOpknX1qCuaxI8owBSiybChR2KwqRZupJIu3lEYrRICh5GxhZg7VbChGe8RxzYp lGiA2awHoxEpRh95n7Wf8BQNotM7/rJCXfWLvVtpKlnt8z9uwfvNxRykrsq7VfyijjRm qLWWUXvEAiQdYD8yFTIw7pJC/3AyZhl6NyXTmn2G0JQNc+jzrXHt0MPOCCNce5c3Lhha K13/vRnmH+18dCge7Wmn6Hf1Hi28A4REeoBRXIOebFXd8/AYG0i/rZXH38pJo/sppkJm 8W4w== X-Gm-Message-State: AOAM5311BGVyb4QPJ1yGQAj09EStuXbGp+/A+i9814KXhqgvG8R5wK5A RWvxJsfce4gKqbXo8/YBywBxyaWhNM52KEPv Sender: "andreyknvl via sendgmr" X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a0c:9e53:: with SMTP id z19mr4596536qve.23.1605306058182; Fri, 13 Nov 2020 14:20:58 -0800 (PST) Date: Fri, 13 Nov 2020 23:20:08 +0100 In-Reply-To: Message-Id: <6f0a1e72783ddac000ac08e7315b1d7c0ca4ec51.1605305978.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.299.gdc1121823c-goog Subject: [PATCH mm v3 18/19] kasan, mm: allow cache merging with no metadata From: Andrey Konovalov To: Andrew Morton Cc: Catalin Marinas , Will Deacon , Vincenzo Frascino , Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov , Vincenzo Frascino Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The reason cache merging is disabled with KASAN is because KASAN puts its metadata right after the allocated object. When the merged caches have slightly different sizes, the metadata ends up in different places, which KASAN doesn't support. It might be possible to adjust the metadata allocation algorithm and make it friendly to the cache merging code. Instead this change takes a simpler approach and allows merging caches when no metadata is present. Which is the case for hardware tag-based KASAN with kasan.mode=prod. Co-developed-by: Vincenzo Frascino Signed-off-by: Vincenzo Frascino Signed-off-by: Andrey Konovalov Link: https://linux-review.googlesource.com/id/Ia114847dfb2244f297d2cb82d592bf6a07455dba --- include/linux/kasan.h | 21 +++++++++++++++++++-- mm/kasan/common.c | 11 +++++++++++ mm/slab_common.c | 3 ++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 16cf53eac29b..173a8e81d001 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -81,17 +81,30 @@ struct kasan_cache { }; #ifdef CONFIG_KASAN_HW_TAGS + DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled); + static __always_inline bool kasan_enabled(void) { return static_branch_likely(&kasan_flag_enabled); } -#else + +#else /* CONFIG_KASAN_HW_TAGS */ + static inline bool kasan_enabled(void) { return true; } -#endif + +#endif /* CONFIG_KASAN_HW_TAGS */ + +slab_flags_t __kasan_never_merge(void); +static __always_inline slab_flags_t kasan_never_merge(void) +{ + if (kasan_enabled()) + return __kasan_never_merge(); + return 0; +} void __kasan_unpoison_range(const void *addr, size_t size); static __always_inline void kasan_unpoison_range(const void *addr, size_t size) @@ -238,6 +251,10 @@ static inline bool kasan_enabled(void) { return false; } +static inline slab_flags_t kasan_never_merge(void) +{ + return 0; +} static inline void kasan_unpoison_range(const void *address, size_t size) {} static inline void kasan_alloc_pages(struct page *page, unsigned int order) {} static inline void kasan_free_pages(struct page *page, unsigned int order) {} diff --git a/mm/kasan/common.c b/mm/kasan/common.c index cf874243efab..a5a4dcb1254d 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -87,6 +87,17 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark) } #endif /* CONFIG_KASAN_STACK */ +/* + * Only allow cache merging when stack collection is disabled and no metadata + * is present. + */ +slab_flags_t __kasan_never_merge(void) +{ + if (kasan_stack_collection_enabled()) + return SLAB_KASAN; + return 0; +} + void __kasan_alloc_pages(struct page *page, unsigned int order) { u8 tag; diff --git a/mm/slab_common.c b/mm/slab_common.c index 0b5ae1819a8b..075b23ce94ec 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -54,7 +55,7 @@ static DECLARE_WORK(slab_caches_to_rcu_destroy_work, */ #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \ - SLAB_FAILSLAB | SLAB_KASAN) + SLAB_FAILSLAB | kasan_never_merge()) #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \ SLAB_CACHE_DMA32 | SLAB_ACCOUNT) -- 2.29.2.299.gdc1121823c-goog