From patchwork Wed Jul 27 07:10:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Tang X-Patchwork-Id: 12930194 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 A3D7CC3F6B0 for ; Wed, 27 Jul 2022 07:10:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 289366B0087; Wed, 27 Jul 2022 03:10:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 20EAF6B0088; Wed, 27 Jul 2022 03:10:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 087C96B0089; Wed, 27 Jul 2022 03:10:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EF10F6B0087 for ; Wed, 27 Jul 2022 03:10:47 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CD71F1A0B89 for ; Wed, 27 Jul 2022 07:10:47 +0000 (UTC) X-FDA: 79732007334.19.C626589 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf05.hostedemail.com (Postfix) with ESMTP id 115C81000A4 for ; Wed, 27 Jul 2022 07:10:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1658905847; x=1690441847; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=69y2qTj7SDB6fNmDi4fGWqVS2niql+kdyFzN631S5jw=; b=IjR3je4GK0jVSdT9ZXIkwhiObI8hveplZ/T9IBkb53QZ7Yg/1XgzQ9cd L7V5HoxdV2NKRXa7AFIvJ7aWph1o3xbQa8+6S6+s53aj/47sImv3zKXnO fHDQ3oYKyx7EATOCzWenS/M6owAWv4fW6YwcQjw5VCsQW0rmm140RRtqs S4i26QXNoiMgeap8vvEt9rb7MSh9OeG4kLH2Q59bhQM9odpQ14G1hiag+ 0SAP4PYnSrlov1V/2S8q15ED6DWF/m5r+HAit0xGxccJ4wIb7nQT2tAO9 gVLbEpfaCivDct6SkZumQ8TMZhi1kFJQlIa0YFOxNs3M0Y1BTFFgoORje g==; X-IronPort-AV: E=McAfee;i="6400,9594,10420"; a="275038605" X-IronPort-AV: E=Sophos;i="5.93,195,1654585200"; d="scan'208";a="275038605" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2022 00:10:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,195,1654585200"; d="scan'208";a="550737930" Received: from shbuild999.sh.intel.com ([10.239.146.138]) by orsmga003.jf.intel.com with ESMTP; 27 Jul 2022 00:10:42 -0700 From: Feng Tang To: Andrew Morton , Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Hansen , Robin Murphy , John Garry , Kefeng Wang , Feng Tang Subject: [PATCH v3 2/3] mm/slub: only zero the requested size of buffer for kzalloc Date: Wed, 27 Jul 2022 15:10:41 +0800 Message-Id: <20220727071042.8796-3-feng.tang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220727071042.8796-1-feng.tang@intel.com> References: <20220727071042.8796-1-feng.tang@intel.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=IjR3je4G; spf=pass (imf05.hostedemail.com: domain of feng.tang@intel.com designates 134.134.136.20 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=1658905847; a=rsa-sha256; cv=none; b=Ou3aW+uFFp9uSSfZJBdKpTh0ABvRnK1hnPOw5t3zKeLyuc/7oyjboclAfE9hdDr6vWMbPr GL/2VfbWzIjkL8QVNIT9QFBfj2YU/qgtHipoyCPsJ8w+vKw9fL1GF5/RPu23oy3IkXVlOi wtbSQME2RuPWV03ghFC3cg7JEzGThP4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658905847; 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=fEfWdfPrOT3/+EdhGveCrcjlPAQNbmw4do6zASsPOg0=; b=eiwE1o0KpogGSFLMxxYBxhwBqvWi13518uvaGKdWigbCJLD3BNNK6wHWLKr3RzJss9uae3 aut3G2MlnNqBZgTIvjBLbibyRBQZQPgHQ478orC5D4WYbOoYysDKTKLrDvfEiaKDQWodbl YgHf2JtwfGQVPrlZIeY7w3NtJuWfLqY= Authentication-Results: imf05.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=IjR3je4G; spf=pass (imf05.hostedemail.com: domain of feng.tang@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 115C81000A4 X-Stat-Signature: f4grye1kx14dcw7zmy8c3pmnzagbzgdn X-HE-Tag: 1658905846-819125 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: kzalloc/kmalloc will round up the request size to a fixed size (mostly power of 2), so the allocated memory could be more than requested. Currently kzalloc family APIs will zero all the allocated memory. To detect out-of-bound usage of the extra allocated memory, only zero the requested part, so that sanity check could be added to the extra space later. For kzalloc users who will call ksize() later and utilize this extra space, please be aware that the space is not zeroed any more. Signed-off-by: Feng Tang --- mm/slab.c | 8 ++++---- mm/slab.h | 9 +++++++-- mm/slub.c | 6 +++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index 5e73e2d80222..771f7c57d3ef 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -3236,7 +3236,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid, size_t orig_ init = slab_want_init_on_alloc(flags, cachep); out_hooks: - slab_post_alloc_hook(cachep, objcg, flags, 1, &ptr, init); + slab_post_alloc_hook(cachep, objcg, flags, 1, &ptr, init, 0); return ptr; } @@ -3299,7 +3299,7 @@ slab_alloc(struct kmem_cache *cachep, struct list_lru *lru, gfp_t flags, init = slab_want_init_on_alloc(flags, cachep); out: - slab_post_alloc_hook(cachep, objcg, flags, 1, &objp, init); + slab_post_alloc_hook(cachep, objcg, flags, 1, &objp, init, 0); return objp; } @@ -3546,13 +3546,13 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, * Done outside of the IRQ disabled section. */ slab_post_alloc_hook(s, objcg, flags, size, p, - slab_want_init_on_alloc(flags, s)); + slab_want_init_on_alloc(flags, s), 0); /* FIXME: Trace call missing. Christoph would like a bulk variant */ return size; error: local_irq_enable(); cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_); - slab_post_alloc_hook(s, objcg, flags, i, p, false); + slab_post_alloc_hook(s, objcg, flags, i, p, false, 0); kmem_cache_free_bulk(s, i, p); return 0; } diff --git a/mm/slab.h b/mm/slab.h index 4ec82bec15ec..7b53868efe6d 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -710,12 +710,17 @@ static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, static inline void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, gfp_t flags, - size_t size, void **p, bool init) + size_t size, void **p, bool init, + unsigned int orig_size) { size_t i; flags &= gfp_allowed_mask; + /* If original request size(kmalloc) is not set, use object_size */ + if (!orig_size) + orig_size = s->object_size; + /* * As memory initialization might be integrated into KASAN, * kasan_slab_alloc and initialization memset must be @@ -726,7 +731,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, for (i = 0; i < size; i++) { p[i] = kasan_slab_alloc(s, p[i], flags, init); if (p[i] && init && !kasan_has_integrated_init()) - memset(p[i], 0, s->object_size); + memset(p[i], 0, orig_size); kmemleak_alloc_recursive(p[i], s->object_size, 1, s->flags, flags); } diff --git a/mm/slub.c b/mm/slub.c index 2e046cc10b84..946919066a4b 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3285,7 +3285,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l init = slab_want_init_on_alloc(gfpflags, s); out: - slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init); + slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init, orig_size); return object; } @@ -3778,11 +3778,11 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, * Done outside of the IRQ disabled fastpath loop. */ slab_post_alloc_hook(s, objcg, flags, size, p, - slab_want_init_on_alloc(flags, s)); + slab_want_init_on_alloc(flags, s), 0); return i; error: slub_put_cpu_ptr(s->cpu_slab); - slab_post_alloc_hook(s, objcg, flags, i, p, false); + slab_post_alloc_hook(s, objcg, flags, i, p, false, 0); kmem_cache_free_bulk(s, i, p); return 0; }