From patchwork Wed Jan 4 06:06:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Tang X-Patchwork-Id: 13088189 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 64779C4708D for ; Wed, 4 Jan 2023 06:08:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 056808E0003; Wed, 4 Jan 2023 01:08:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 005938E0001; Wed, 4 Jan 2023 01:08:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0FCF8E0003; Wed, 4 Jan 2023 01:08:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D1FC88E0001 for ; Wed, 4 Jan 2023 01:08:52 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A38E71A02A8 for ; Wed, 4 Jan 2023 06:08:52 +0000 (UTC) X-FDA: 80316088104.12.5157E00 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf10.hostedemail.com (Postfix) with ESMTP id BE3E6C000C for ; Wed, 4 Jan 2023 06:08:50 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SJfKpvYh; spf=pass (imf10.hostedemail.com: domain of feng.tang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672812531; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ICYbAgxo1gxvMoVCz28YjqvVpuUwn4trUmXKW6YLBhQ=; b=Q5X8UZG7HKVUjSzah9qMM+mdC+QcM5YCTRHG1OYZ7VuSE7p6zBkULeiK0VFeZuZh8UY210 kePrk5UrIGQFzOO4TcIjJhcbs50UiKq6xrLAWVM4SET6s/DcNtqbGC0rnT678WefmrmFVC fFphvpgK3BzbhNjWNqHfRUARxdC2rsU= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SJfKpvYh; spf=pass (imf10.hostedemail.com: domain of feng.tang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672812531; a=rsa-sha256; cv=none; b=EDKeFK0AUAfQQaGrM6fHSmNU68uDfk7j3heBeiNPpvveawvmZWYtBDUKjv5ClcuE6Q9Oe+ AksEK3+bzO2jo07FikK3nrfF/WgqWq9bQcRccqiijEAEQFpscQT0H8WmwJmiSLdmmbusDV DlGTFYDsv3w/fTgX6RH0Wq2yYdcqIy8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1672812530; x=1704348530; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yjPAo3gzL+gE/D3nqOFC9+brYvlet3IWm71HhQsK8Fo=; b=SJfKpvYh++XWiwE6vEsDuGnfzmsAxRLBcc2hOVX9UFUdqabbhhBR2gvo vTlT1IWrnmZEtk98eRD1Ny7U+kdW66rUZ3hbOMMc4KBjAxufOBxwTKHue aUHi64fawAFPsvWWCHJ50IIxgb25NxYvgoonD6hdzff1KhlpeYybP1lrL xer3enWWsOQ7IPseKznTcBqxu9o/AYNX5ZFvhIoz8gypuJRKLqP95DLN/ ZP0Os246rajTsENRmI3oCQVXa2pmaB4n34qUFL9Y/jj8/yuRxs1AdmCeR IufTR1TFqS0MG6Doznasg0bLZ/glL9/QI6lYkWexupl2GKCvABG1vu/mX Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10579"; a="323840145" X-IronPort-AV: E=Sophos;i="5.96,299,1665471600"; d="scan'208";a="323840145" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jan 2023 22:08:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10579"; a="779094081" X-IronPort-AV: E=Sophos;i="5.96,299,1665471600"; d="scan'208";a="779094081" Received: from feng-clx.sh.intel.com ([10.238.200.228]) by orsmga004.jf.intel.com with ESMTP; 03 Jan 2023 22:08:45 -0800 From: Feng Tang To: Andrew Morton , Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Vincenzo Frascino Cc: linux-mm@kvack.org, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, Feng Tang Subject: [Patch v3 -mm 2/2] mm/kasan: simplify and refine kasan_cache code Date: Wed, 4 Jan 2023 14:06:05 +0800 Message-Id: <20230104060605.930910-2-feng.tang@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230104060605.930910-1-feng.tang@intel.com> References: <20230104060605.930910-1-feng.tang@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: BE3E6C000C X-Rspam-User: X-Stat-Signature: y7mhft5ng8wz9tx8hthxhzpn4nbfhrz6 X-HE-Tag: 1672812530-702113 X-HE-Meta: U2FsdGVkX18ckCDXc8HLWb96XhhSWQoGvVFHFK1Vd1+ydIti31xK07fKho+VVUvhvSN9RHxMt+Jpahw7RLmiQQYE7dL6JM23AJKnVAmTEDL5tSkVXanZoyQv1eqDydGJmhRG5m12mKPXEv9NUcSoNs6goEZHaWBgPHWnk68uQzYOTaL4bBY5vC5X+MwhGKE6uGhlAMuywrexyIhp6vPjhF01KqG+K0RRL42OfY62bsNwe6hhunmtEtFcmGEoXMltZJdzg5KctOqzp5sK+V2pcXcS6wIXDyBPuq+JPfCa9iWz3o+lagr0KgHjI5IRTs4otEqi87dp3JiYQEQlArn/M8OHGcrViulhwLKFhQhoUIY875lSboW/WeMcmq63lPNuicnGwKewj0cU/P6rciVughrxK/zYUmWEf4+7dMkeWkhJ1oLZGDJqe0ymERXlHFVBFV1fszWId+XknJ/tih7DMJN+D2BTODPx1BqDaSlmkYaEioLJQXhQMZFPWV2EM4l3qTUSK8DilF4DcVyo2fTzEakDjrTvZmYNjnzVXubnU0zbyg/NrRXB1r0ndXoRoA+iIFyGf8lYX/G3ZefwDeYV6p9RDvIH7ZVR8aoKJFRd41KwGi+/jhQmeTFydDS8oncZurzx4ixL+b2Jg4OXRXGZs4FWYv3McnAd/YIH0ZjEGFJgM6mObVxtVtWU/NEomABOiAE5wWzKtcT4lbtB0Za6C4orgZkKTVhS1NbSHddipG5JPTzPoBG7L5wmACWoC7wuPaunEt0wYcc4UTIIUnHUukDOif2PeEOPAdwZNLGQhJ8A/LIZZ3xR3k4JlSc+DnBEp3oqHW8sNOfqQMIhiYtAO3KzLCM2YE34He3gj88bgYlLapqP0m50DHJhmuKCBW5BOIE0v1HhZDy96eXsUci1vAyrF1CXtIz8fJOv9UMelJxe9J2XhHRQBqGWMofkG12hjqI4NRF4hVZPaUUgqfB d6HB8u6J P1kcyQtvhU+15NgUNqpj3ge4mf7Q4yW3ilp9F62yiqw9hcy82sz8hOROciA== 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: struct 'kasan_cache' has a member 'is_kmalloc' indicating whether its host kmem_cache is a kmalloc cache. With newly introduced is_kmalloc_cache() helper, 'is_kmalloc' and its related function can be replaced and removed. Also 'kasan_cache' is only needed by KASAN generic mode, and not by SW/HW tag modes, so refine its protection macro accordingly, suggested by Andrey Konoval. Signed-off-by: Feng Tang Reviewed-by: Andrey Konovalov Acked-by: Vlastimil Babka Acked-by: David Rientjes --- Changlog: Since v2: * Rebase latest -mm tree, fix a conflict with kasan changes * Collect Reviewed-by tag Since v1 * Use CONFIG_KASAN_GENERIC instead of CONFIG_KASAN for 'kasan_cache', as suggested by Andrey Konovalov include/linux/kasan.h | 22 +++++----------------- include/linux/slab_def.h | 2 +- include/linux/slub_def.h | 2 +- mm/kasan/common.c | 9 ++------- mm/slab_common.c | 1 - 5 files changed, 9 insertions(+), 27 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 5ebbaf672009..f7ef70661ce2 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -96,15 +96,6 @@ static inline bool kasan_has_integrated_init(void) } #ifdef CONFIG_KASAN - -struct kasan_cache { -#ifdef CONFIG_KASAN_GENERIC - int alloc_meta_offset; - int free_meta_offset; -#endif - bool is_kmalloc; -}; - void __kasan_unpoison_range(const void *addr, size_t size); static __always_inline void kasan_unpoison_range(const void *addr, size_t size) { @@ -129,13 +120,6 @@ static __always_inline bool kasan_unpoison_pages(struct page *page, return false; } -void __kasan_cache_create_kmalloc(struct kmem_cache *cache); -static __always_inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) -{ - if (kasan_enabled()) - __kasan_cache_create_kmalloc(cache); -} - void __kasan_poison_slab(struct slab *slab); static __always_inline void kasan_poison_slab(struct slab *slab) { @@ -255,7 +239,6 @@ static inline bool kasan_unpoison_pages(struct page *page, unsigned int order, { return false; } -static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {} static inline void kasan_poison_slab(struct slab *slab) {} static inline void kasan_unpoison_object_data(struct kmem_cache *cache, void *object) {} @@ -306,6 +289,11 @@ static inline void kasan_unpoison_task_stack(struct task_struct *task) {} #ifdef CONFIG_KASAN_GENERIC +struct kasan_cache { + int alloc_meta_offset; + int free_meta_offset; +}; + size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object); slab_flags_t kasan_never_merge(void); void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, diff --git a/include/linux/slab_def.h b/include/linux/slab_def.h index 5834bad8ad78..a61e7d55d0d3 100644 --- a/include/linux/slab_def.h +++ b/include/linux/slab_def.h @@ -72,7 +72,7 @@ struct kmem_cache { int obj_offset; #endif /* CONFIG_DEBUG_SLAB */ -#ifdef CONFIG_KASAN +#ifdef CONFIG_KASAN_GENERIC struct kasan_cache kasan_info; #endif diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h index aa0ee1678d29..f6df03f934e5 100644 --- a/include/linux/slub_def.h +++ b/include/linux/slub_def.h @@ -136,7 +136,7 @@ struct kmem_cache { unsigned int *random_seq; #endif -#ifdef CONFIG_KASAN +#ifdef CONFIG_KASAN_GENERIC struct kasan_cache kasan_info; #endif diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 1d0008e1c420..6b8e9c848573 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -122,11 +122,6 @@ void __kasan_poison_pages(struct page *page, unsigned int order, bool init) KASAN_PAGE_FREE, init); } -void __kasan_cache_create_kmalloc(struct kmem_cache *cache) -{ - cache->kasan_info.is_kmalloc = true; -} - void __kasan_poison_slab(struct slab *slab) { struct page *page = slab_page(slab); @@ -326,7 +321,7 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, kasan_unpoison(tagged_object, cache->object_size, init); /* Save alloc info (if possible) for non-kmalloc() allocations. */ - if (kasan_stack_collection_enabled() && !cache->kasan_info.is_kmalloc) + if (kasan_stack_collection_enabled() && !is_kmalloc_cache(cache)) kasan_save_alloc_info(cache, tagged_object, flags); return tagged_object; @@ -372,7 +367,7 @@ static inline void *____kasan_kmalloc(struct kmem_cache *cache, * Save alloc info (if possible) for kmalloc() allocations. * This also rewrites the alloc info when called from kasan_krealloc(). */ - if (kasan_stack_collection_enabled() && cache->kasan_info.is_kmalloc) + if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache)) kasan_save_alloc_info(cache, (void *)object, flags); /* Keep the tag that was set by kasan_slab_alloc(). */ diff --git a/mm/slab_common.c b/mm/slab_common.c index 1cba98acc486..bf4e777cfe90 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -670,7 +670,6 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name, create_boot_cache(s, name, size, flags | SLAB_KMALLOC, useroffset, usersize); - kasan_cache_create_kmalloc(s); list_add(&s->list, &slab_caches); s->refcount = 1; return s;