Received: by 2002:a05:6358:795:b0:dc:4c66:fc3e with SMTP id n21csp1254948rwj; Sat, 29 Oct 2022 20:08:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6ySvx1HSKFJdLFsLvxFQhKoss1sfUwbwFbfs5TAUdL17/m3bk/UmAH6etLROKes71/ORT/ X-Received: by 2002:a17:906:730d:b0:78e:9ca5:3269 with SMTP id di13-20020a170906730d00b0078e9ca53269mr6464808ejc.366.1667099337152; Sat, 29 Oct 2022 20:08:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667099337; cv=none; d=google.com; s=arc-20160816; b=IBCvt5jQQpTD1litQ8Qzb3TjD6ISNJe6roLaFDsoRwrvlkVRKNJYc7wag4ZqbCqLPC XMpUlDhMv/XkrlRtAVY2YhKeos2Rlv3BCjElXvk3qlr0FwatY+JV7kqVXpuy7jdv4dkm sL77b1Yp8inISJdatnjBI53ge7fTGndEg3SYIyB9+s3jKtCgfJZ2yh2RwkRZhTg68HXb +kU+4WZaDPcn0j6V4iuv8snR7Z7+69/wcjceLpfbYy5xFiPdvr7XdT2zkc2rpemxbH7Z L/dj/QiH0MKuLJyFYSuXgz2eyWvLMWzBIFU4b5VQi8LiekK+2Go/jxgfSdRqOVvN42lA yeyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=ViUX7nS+sMEeCyJpGqlawogkGghwNa0jyE1+CkQUpdc=; b=gghvQ5LIyofPbRKwbn7N8P/MEgh2yqKB4OMDb1Zkg0ndtdD0ydYUkuA/Tx0af3YqBO hTctUl9yHYCfkS2jolsTNJXggXRYkQuuifEMpZHyW7N4CtzlzNJu61na2D98QxAlVyJI eH0WifbGQRQRI+Vkr71IuEOPDDG3uhRmroduj9nu0Y3b5lkSSCDMQxNo4VEM48NqgfXD 4InfanmFnyP9N98qobBGuspuF/ex8nQwg8Eb/wgySnSPEASK683WF3fxNJmX7JWvnpn/ Ad5WfkScidfSbgrf9RtOo5E6w46lgU1dEXJOiEZDlsv/h8YOpZAz9Oh9mNuBTJ1INN+y ZF+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qQ6rDtl8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hs37-20020a1709073ea500b007882926848bsi3631189ejc.818.2022.10.29.20.08.32; Sat, 29 Oct 2022 20:08:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qQ6rDtl8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229691AbiJ3C7f (ORCPT + 99 others); Sat, 29 Oct 2022 22:59:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229549AbiJ3C7e (ORCPT ); Sat, 29 Oct 2022 22:59:34 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57C38C7C for ; Sat, 29 Oct 2022 19:59:32 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id z14so11431549wrn.7 for ; Sat, 29 Oct 2022 19:59:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=ViUX7nS+sMEeCyJpGqlawogkGghwNa0jyE1+CkQUpdc=; b=qQ6rDtl8YogRVAxKyJvgUzMif7w/ZB2i3g7mK+pw6pYRNgxQTUsMyj8nPdMXEeXzLF PXLS4IoVwFNta7RCuAAUYM1Ms2+nJyNo0HafTdH0SA83mBdA4RPJuPtXTKY/5D56dVm3 HTSR+mBo3ZX9sBybbjBm56aRQkVg+vUxgvp8xn9lOrdS6+9rPBME/OLt56ykODryouhU DvDiQIC2IdYP+AOyBKmJLgWIHtjKzfAqJECgL9kMvSaX3dzv4+FwRH/p9XR3/oQPgsYe OEq6CtMEWDtIV8n8lq526UsStYYB1l593S4s+aat/PW1A4HZzfUdkNM6NHqcL6RpkPNk awpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ViUX7nS+sMEeCyJpGqlawogkGghwNa0jyE1+CkQUpdc=; b=am3hP4cRSVHroGvaIWKU7+XyMku9hZa0sgXY9kN5PcbJD203D5WqMlm1kc1QI/6ojM KWPQQV0rldBOWD86zLv6rPWxVNoNCZfp0NoZEmQqmQPxRI3zPKuk9BQ2sltpzQPXvJD+ I1PeSDOUwaOd1peq27lOieJL059zVZ3hMkIPHDdYUj93kw92kLK4DGj9vXL+b6DyYa8s A1GKlHAXp1FS6pZSod/XXSNOTJ2mMNYAvsqZcaNzKw0zxrAp4pjXm8fP/YgXKiMBL3xY oaKaPn8RUtVdg+jffKB369tlYP+CyEVQ847ugXxK02UyfGtqMD0saUWLWmVYW2eCLecl orCg== X-Gm-Message-State: ACrzQf11qtFl3Wela4Zp+n9viqnvpoX7UVfeqwQ7ACn6HQQDURQakEqD 4v1uqQuf2VCKqJ8dOzyfTYjBJQ== X-Received: by 2002:adf:d1c4:0:b0:230:7771:f618 with SMTP id b4-20020adfd1c4000000b002307771f618mr3541746wrd.203.1667098770552; Sat, 29 Oct 2022 19:59:30 -0700 (PDT) Received: from elver.google.com ([2a00:79e0:9c:201:a6f7:9df9:f4cc:97c1]) by smtp.gmail.com with ESMTPSA id bi22-20020a05600c3d9600b003b31c560a0csm3240628wmb.12.2022.10.29.19.59.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Oct 2022 19:59:29 -0700 (PDT) Date: Sun, 30 Oct 2022 03:59:22 +0100 From: Marco Elver To: andrey.konovalov@linux.dev Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: Re: [PATCH] kasan: allow sampling page_alloc allocations for HW_TAGS Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.7 (2022-08-07) X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 27, 2022 at 10:10PM +0200, andrey.konovalov@linux.dev wrote: > From: Andrey Konovalov > > Add a new boot parameter called kasan.page_alloc.sample, which makes > Hardware Tag-Based KASAN tag only every Nth page_alloc allocation. > > As Hardware Tag-Based KASAN is intended to be used in production, its > performance impact is crucial. As page_alloc allocations tend to be big, > tagging and checking all such allocations introduces a significant > slowdown in some testing scenarios. The new flag allows to alleviate > that slowdown. > > Enabling page_alloc sampling has a downside: KASAN will miss bad accesses > to a page_alloc allocation that has not been tagged. > > Signed-off-by: Andrey Konovalov > --- > Documentation/dev-tools/kasan.rst | 4 +++ > include/linux/kasan.h | 7 ++--- > mm/kasan/common.c | 9 +++++-- > mm/kasan/hw_tags.c | 26 +++++++++++++++++++ > mm/kasan/kasan.h | 15 +++++++++++ > mm/page_alloc.c | 43 +++++++++++++++++++++---------- > 6 files changed, 85 insertions(+), 19 deletions(-) > > diff --git a/Documentation/dev-tools/kasan.rst b/Documentation/dev-tools/kasan.rst > index 5c93ab915049..bd97301845ef 100644 > --- a/Documentation/dev-tools/kasan.rst > +++ b/Documentation/dev-tools/kasan.rst > @@ -140,6 +140,10 @@ disabling KASAN altogether or controlling its features: > - ``kasan.vmalloc=off`` or ``=on`` disables or enables tagging of vmalloc > allocations (default: ``on``). > > +- ``kasan.page_alloc.sample=`` makes KASAN tag only Frequency is number of samples per frame (unit time, or if used non-temporally like here, population size). [1] https://en.wikipedia.org/wiki/Systematic_sampling You're using it as an interval, so I'd just replace uses of frequency with "interval" appropriately here and elsewhere. > + every Nth page_alloc allocation, where N is the value of the parameter > + (default: ``1``). > + > Error reports > ~~~~~~~~~~~~~ > > diff --git a/include/linux/kasan.h b/include/linux/kasan.h > index d811b3d7d2a1..d45d45dfd007 100644 > --- a/include/linux/kasan.h > +++ b/include/linux/kasan.h > @@ -120,12 +120,13 @@ static __always_inline void kasan_poison_pages(struct page *page, > __kasan_poison_pages(page, order, init); > } > > -void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init); > -static __always_inline void kasan_unpoison_pages(struct page *page, > +bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init); > +static __always_inline bool kasan_unpoison_pages(struct page *page, > unsigned int order, bool init) > { > if (kasan_enabled()) > - __kasan_unpoison_pages(page, order, init); > + return __kasan_unpoison_pages(page, order, init); > + return false; > } > > void __kasan_cache_create_kmalloc(struct kmem_cache *cache); > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 833bf2cfd2a3..1f30080a7a4c 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -95,19 +95,24 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark) > } > #endif /* CONFIG_KASAN_STACK */ > > -void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init) > +bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init) > { > u8 tag; > unsigned long i; > > if (unlikely(PageHighMem(page))) > - return; > + return false; > + > + if (!kasan_sample_page_alloc()) > + return false; > > tag = kasan_random_tag(); > kasan_unpoison(set_tag(page_address(page), tag), > PAGE_SIZE << order, init); > for (i = 0; i < (1 << order); i++) > page_kasan_tag_set(page + i, tag); > + > + return true; > } > > void __kasan_poison_pages(struct page *page, unsigned int order, bool init) > diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c > index b22c4f461cb0..aa3b5a080297 100644 > --- a/mm/kasan/hw_tags.c > +++ b/mm/kasan/hw_tags.c > @@ -59,6 +59,11 @@ EXPORT_SYMBOL_GPL(kasan_mode); > /* Whether to enable vmalloc tagging. */ > DEFINE_STATIC_KEY_TRUE(kasan_flag_vmalloc); > > +/* Frequency of page_alloc allocation poisoning. */ > +unsigned long kasan_page_alloc_sample = 1; > + > +DEFINE_PER_CPU(unsigned long, kasan_page_alloc_count); > + > /* kasan=off/on */ > static int __init early_kasan_flag(char *arg) > { > @@ -122,6 +127,27 @@ static inline const char *kasan_mode_info(void) > return "sync"; > } > > +/* kasan.page_alloc.sample= */ > +static int __init early_kasan_flag_page_alloc_sample(char *arg) > +{ > + int rv; > + > + if (!arg) > + return -EINVAL; > + > + rv = kstrtoul(arg, 0, &kasan_page_alloc_sample); > + if (rv) > + return rv; > + > + if (!kasan_page_alloc_sample) { > + kasan_page_alloc_sample = 1; > + return -EINVAL; > + } > + > + return 0; > +} > +early_param("kasan.page_alloc.sample", early_kasan_flag_page_alloc_sample); > + > /* > * kasan_init_hw_tags_cpu() is called for each CPU. > * Not marked as __init as a CPU can be hot-plugged after boot. > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h > index abbcc1b0eec5..ee67eb35f4a7 100644 > --- a/mm/kasan/kasan.h > +++ b/mm/kasan/kasan.h > @@ -42,6 +42,9 @@ enum kasan_mode { > > extern enum kasan_mode kasan_mode __ro_after_init; > > +extern unsigned long kasan_page_alloc_sample; > +DECLARE_PER_CPU(unsigned long, kasan_page_alloc_count); > + > static inline bool kasan_vmalloc_enabled(void) > { > return static_branch_likely(&kasan_flag_vmalloc); > @@ -57,6 +60,13 @@ static inline bool kasan_sync_fault_possible(void) > return kasan_mode == KASAN_MODE_SYNC || kasan_mode == KASAN_MODE_ASYMM; > } > > +static inline bool kasan_sample_page_alloc(void) > +{ > + unsigned long *count = this_cpu_ptr(&kasan_page_alloc_count); this_cpu_inc_return() without it, you need to ensure preemption is disabled around here. > + > + return (*count)++ % kasan_page_alloc_sample == 0; Doing '%' is a potentially costly operation if called in a fast-path. We can generate better code with (rename 'count' -> 'skip'): long skip_next = this_cpu_dec_return(kasan_page_alloc_skip); if (skip_next < 0) { this_cpu_write(kasan_page_alloc_skip, kasan_page_alloc_sample - 1); return true; } return false; Important is also to switch the counter to a 'long', otherwise you'd have to pre-initialize all of them to something non-zero to avoid wrap.