From patchwork Wed Nov 23 12:31:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Tang X-Patchwork-Id: 24947 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2759618wrr; Wed, 23 Nov 2022 04:36:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf5v7j2Dy4dmVVGKSwvnJOAH49d7IpwLLYSiBFddlAxBvLABozo28psWITathHC3eRZRtjAj X-Received: by 2002:a17:907:c20d:b0:7b8:882d:43fc with SMTP id ti13-20020a170907c20d00b007b8882d43fcmr6404836ejc.0.1669207001695; Wed, 23 Nov 2022 04:36:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669207001; cv=none; d=google.com; s=arc-20160816; b=RZ0uko/KRkSC9KsC32Q8o7yvb7HIs/X5KrunDrjfXKt1CE3DEovcV9O61iGNTCdsjA 8hb2qaGpG0Kd/iBgzszIVT1lmUPB7/fsEzGvftA55tSFM31lfuYeGczFLOzns0y5FTSK shn3Eojvp49aVrzfCSglRAbl5tKru3raG0yb6LIkmwdqck4kq7XUuuGR9bXPWV9T1mCh M1BxOU40IeaOItPV9jaRoXw2mmMsamGaM6g++5FJcq9fXywcSe43jBD6WbKgfMh85s7p jvTZQMt7/SAb5lfRcggtCMnZyEDrREuil2fA3qIBSMZtkz1j6RvJu+rr22vMgQr6obHo orbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=CJmzxhK0F+EQBwqW32oQ/vEbzIh81a1UF0l3egqzynI=; b=MkT4t+fakCDVC1qvVoAFbg8p8OzBA3/pgvo9Fw2y8zOlMaoYbFcOewgi9lbHDMwcFL qSEfXTCEr8ptNVVkz/mOuR15Mm37xLcyx83FKQ0idbF8Cza3nZMsO8fg6kKn6/nSdyRd 6RL1BEYECkOe7+gLuPbEkaVuDIdDHqpCLQPl60PqHSTzMQ0nZ9N3Uu78GNYECXUQM6A0 Fwl7rsKJyJJXZjA4JHnlnp8Q9DLDsF1TipuH5WRNfKB7F/mrI7jBTagZTGB53HKwv6Zx XQ3YdXFRRNJmbebFg/Oojt5ErXgOS3g3reTv4F3MTpAA8CDgkdJfonXUK0LwQJECTdN9 ruWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=flTN7wX6; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ga4-20020a1709070c0400b007ade20fc40csi15777242ejc.810.2022.11.23.04.36.16; Wed, 23 Nov 2022 04:36:41 -0800 (PST) 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=@intel.com header.s=Intel header.b=flTN7wX6; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236766AbiKWMfE (ORCPT + 99 others); Wed, 23 Nov 2022 07:35:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235612AbiKWMfB (ORCPT ); Wed, 23 Nov 2022 07:35:01 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A534DEBD for ; Wed, 23 Nov 2022 04:35:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1669206901; x=1700742901; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=32aAwApD/Py6ob5i40TYQ76jp/UicXBWzicek/tVfb0=; b=flTN7wX6/StGouVIAT1r6wDjNg34KVw+HaYf50587lPCkKomMB5rSbcI j5pQbxtOAJKxllGBtIiTaoeubBlA4tGvMQY0a38H5rllx5LGpHVmZbLWw MlEcWdY1qc83CKLyd+fl1DAGg2fMrAS0JPtNgliMdQADsKnjdpAydH6D8 Aw/bwLJiH5XufAUSTaval5r7AuHRc+KabG+ZkjOCMi9/LsfT5AJT6JYy2 xgTMG01AHoCK8si/02168JCdrT6CwoxX8Ga1ANTY52W5In943czumGwp1 HqjMQL5H55jfag4pZ060yup1J3+ZZO2DqdwierW5OheS4OU9kMGYEqYlA w==; X-IronPort-AV: E=McAfee;i="6500,9779,10539"; a="340935416" X-IronPort-AV: E=Sophos;i="5.96,187,1665471600"; d="scan'208";a="340935416" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Nov 2022 04:35:00 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10539"; a="705349428" X-IronPort-AV: E=Sophos;i="5.96,187,1665471600"; d="scan'208";a="705349428" Received: from feng-clx.sh.intel.com ([10.238.200.228]) by fmsmga008.fm.intel.com with ESMTP; 23 Nov 2022 04:34:57 -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 v2 -next 1/2] mm/slb: add is_kmalloc_cache() helper function Date: Wed, 23 Nov 2022 20:31:58 +0800 Message-Id: <20221123123159.2325763-1-feng.tang@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750290401123126260?= X-GMAIL-MSGID: =?utf-8?q?1750290401123126260?= commit 6edf2576a6cc ("mm/slub: enable debugging memory wasting of kmalloc") introduces 'SLAB_KMALLOC' bit specifying whether a kmem_cache is a kmalloc cache for slab/slub (slob doesn't have dedicated kmalloc caches). Add a helper inline function for other components like kasan to simplify code. Signed-off-by: Feng Tang Acked-by: Vlastimil Babka Acked-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- changlog: since v1: * don't use macro for the helper (Andrew Morton) * place the inline function in mm/slb.h to solve data structure definition issue (Vlastimil Babka) mm/slab.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mm/slab.h b/mm/slab.h index e3b3231af742..0d72fd62751a 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -325,6 +325,14 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size, } #endif +static inline bool is_kmalloc_cache(struct kmem_cache *s) +{ +#ifndef CONFIG_SLOB + return (s->flags & SLAB_KMALLOC); +#else + return false; +#endif +} /* Legal flag mask for kmem_cache_create(), for various configurations */ #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \ From patchwork Wed Nov 23 12:31:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Tang X-Patchwork-Id: 24948 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2760316wrr; Wed, 23 Nov 2022 04:38:10 -0800 (PST) X-Google-Smtp-Source: AA0mqf7+0V/JTmjrVopLmB3TEccsKIrOHnxVKJS5SaJYVq6ok5MQOEwomeDwksTfUgJQRae6hDwz X-Received: by 2002:aa7:d74c:0:b0:461:b952:8932 with SMTP id a12-20020aa7d74c000000b00461b9528932mr17008243eds.104.1669207090038; Wed, 23 Nov 2022 04:38:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669207090; cv=none; d=google.com; s=arc-20160816; b=R0Wjv0YqLB2wtmzkJyWgnKvpTE/5d+X7lvBgEeSC42dnqfJH3giCkxCqNHpaiYrvLY l0z3xUB46MWmrBdXqWe2Q0HYb5RgPFJKk4fGViQpbCAI1uhHTxGTB6fJQMYE2T4FGPa4 afouOv9yojcHoy/N12Gn+EcbCSSRsRMIxhAYOZyfPUa4/ELrlVlWldjSjjvEz0hRnoTV +hrk315JJhixNJmaTDy4rzYW5Ssx57do6ybP4EjRirpPifqcV1NygyfTPEaORTOB/uuS REAubOiavRJ2IV5w0aUd1sVyRbYMYXetRxXY9wxhRsTNv7kqrBVoym1StzgXOV6lqcoz 7+vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DKpDEUuzQP3JS6vSn7fniF3BoC56bwIDZKe8r0jOrQM=; b=wnnSBiMhEk3brUhPjgskM2AI84XJOoJft8RvWM+Mnyl+mdrMGToR5nScRbizhb9zxo FzsXWAtYsDvgErHJpdJvy6pyWsaHHAO3O0sCmW0IETSBuu+QuxipYxFf1jPYtS0Lu0HG xTDPA60BYNPgj7djkX+2FGBRWUeDVyb4daI7NZKvlLdZdkrfmpbWIpPX/fsgGLRpN+dn vm7id5qLgeLusbVWWZpKI+jbNg2GTsvCMoU8y547A3WpXcoLjoMs7/vtd+pRW5Rea3uH 9LFzi/0p+eapMgBZImukH2+pFSasqXq5VCu5bTO6mq8fLvjlXyEtaQPl07I9XdqWOUog qgrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jqDNFf4d; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw17-20020a1709066a1100b007ae6ba5f344si132976ejc.53.2022.11.23.04.37.46; Wed, 23 Nov 2022 04:38:10 -0800 (PST) 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=@intel.com header.s=Intel header.b=jqDNFf4d; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237114AbiKWMfH (ORCPT + 99 others); Wed, 23 Nov 2022 07:35:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237017AbiKWMfF (ORCPT ); Wed, 23 Nov 2022 07:35:05 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D3B2DEBD for ; Wed, 23 Nov 2022 04:35:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1669206904; x=1700742904; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/ZAAjGfYIjdVR+5X0uaKgifD9for1Ic1lKNhQ5iJ4HU=; b=jqDNFf4dwMJqhc6DZDnmxAcQEXdprc8Rk8G0g+lAeDB5NqXj/EVCn46z lY5sRaJGv569DBUBE3Dg2aVprfQ57rjhI5n9XNc/jumlzvM+X9TTlk01b pYbeQLmFxyToyWaUhn2mnWKf3/wt/jeERZwPwtGtsoF6thTxbafpVJwwl oIRlLqUo2xsC400Tk1iHUVgNlXPjg+dDSAa77Xf1bAa4VwephcgTIKEby BVvEc3ApscTn4dRKFLF3pPWUUp7DOzhQ/GxkPmovKQDRIYykO08nQRhoU rxXvs1z+wf8xf1fj385AocCjhgMJipe5KcLqd+STZns2LrnFGvNDWoak2 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10539"; a="340935429" X-IronPort-AV: E=Sophos;i="5.96,187,1665471600"; d="scan'208";a="340935429" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Nov 2022 04:35:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10539"; a="705349470" X-IronPort-AV: E=Sophos;i="5.96,187,1665471600"; d="scan'208";a="705349470" Received: from feng-clx.sh.intel.com ([10.238.200.228]) by fmsmga008.fm.intel.com with ESMTP; 23 Nov 2022 04:35:00 -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 v2 -next 2/2] mm/kasan: simplify and refine kasan_cache code Date: Wed, 23 Nov 2022 20:31:59 +0800 Message-Id: <20221123123159.2325763-2-feng.tang@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221123123159.2325763-1-feng.tang@intel.com> References: <20221123123159.2325763-1-feng.tang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750290493559474684?= X-GMAIL-MSGID: =?utf-8?q?1750290493559474684?= 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 --- Changlog: 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 dff604912687..0ff382f79f80 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) { @@ -252,7 +236,6 @@ static inline void kasan_poison_pages(struct page *page, unsigned int order, bool init) {} static inline bool kasan_unpoison_pages(struct page *page, unsigned int order, bool init) { 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) {} @@ -303,6 +286,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 f0ffad6a3365..39f7f1f95de2 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 f9c68a9dac04..4e7cdada4bbb 100644 --- a/include/linux/slub_def.h +++ b/include/linux/slub_def.h @@ -132,7 +132,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 1f30080a7a4c..6e265beefc27 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 8276022f0da4..a5480d67f391 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -663,7 +663,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;