Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp2856849rwe; Mon, 29 Aug 2022 01:09:38 -0700 (PDT) X-Google-Smtp-Source: AA6agR5XQ5BWL3dOLhtKl84LeJkxOc9pU1C+ZUhvya3ipM8FQTsjEMHnPbh8TnAJybDzMsE2x/m2 X-Received: by 2002:a17:906:845c:b0:73d:831e:b73d with SMTP id e28-20020a170906845c00b0073d831eb73dmr12290446ejy.687.1661760574542; Mon, 29 Aug 2022 01:09:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661760574; cv=none; d=google.com; s=arc-20160816; b=iHySPH8vhpNL98cOFBRFiQXB0P6LmD9E80zgeAJCtBXncgdOLpVmm5n7WwniMiPU/5 SSemepDeQcnCqYythWYS6lbKqCKMCsgjLD2V08t8iDtXbtJUQpIID12B+k1VTjsHIacy 9lgxU5fGL5sDyzrmSu0nLiW+phl8Cx46A7BBtVO+b3tq2NOrJFDD1MHw4DcjfQlKWdvG ycvHax87qdmG3ISU9WEQc/U/6pv84f2HXd45dIxHlTBOwjn4zEHM0OsW0hxCrQ+y8HaK luvhv0M/km859EPpf9R2C03mx0n0bAk2v3YkadylK/dd+tcO00oOsn649sUToRS2vGbe UOGw== 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=V0aftn1iiZ20B6TeIbgAsAO9UO8+GfrOumdHC2vtiGU=; b=bCqr0B7dRNa5kstfLYfzDbRDbOPu+qNAYSuzruX1nNgXosq5Nsdl3Q8k7T0m0DCyZV iqck9c3wzbObgKJOGzSHWFpLqtXNvhrwsQtSMxbmqBqf01QbNQdvZQ3b9NmCkxqJm8cY bp0DQj8hpCyDMnG2zKctvcXjm9xhFJ7y9HFhwWEyV4xxhLUiasYnta7nnLzvwYcenGml aeSWzLgXxIShqGBKiIbvzgZE7wPO4NWf8VA1suyRcGdIrvMkA56YeoAv4j+41/uebAq9 yq00Vv95TTW5AANYiBErQ6xHI3byXNfB0NMZJWFRtWLwmGa7WCjOn1sh7y8bLJO22G6N wbkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Nl10dyxy; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id go37-20020a1709070da500b0073d7097ac8asi7397230ejc.538.2022.08.29.01.09.09; Mon, 29 Aug 2022 01:09:34 -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=@intel.com header.s=Intel header.b=Nl10dyxy; 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 S229729AbiH2H4A (ORCPT + 99 others); Mon, 29 Aug 2022 03:56:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229704AbiH2Hzx (ORCPT ); Mon, 29 Aug 2022 03:55:53 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DB5F2715A for ; Mon, 29 Aug 2022 00:55:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661759752; x=1693295752; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PBPokcQTu2e2E10a31KPFVj+zRJDYVfS2hjU6JguMhY=; b=Nl10dyxyLtE0jo3rqj2TbCDYXCBvUa5DS+KNI+0zm2tfP4RW/nhBremG U7L3vBYIlbO5pXAp3mUfzQBqCYSsj/XKteRprohci0gzDR5KdAdQsdIVC ARa5f5WW+WHSY5g/z3MoRlW35H0VbQk/52zyCq1iD9OHdRgtQw5Si47YH o+gGEL7/8aIDZGRTYxhv9WVzTUNYCKVNDRqQslODQyxqtKnKAzEpl17rO MH3rghksXB98DykdxcEe4flSAv0VhnE+PNsOz7RsE+nrDYIedYWBRKrHL o/87Oiwr5wHhP7o0tWzR4gNvXKZk5PFSfyn8z+TcRhxbFaNkqBcyIiKjL g==; X-IronPort-AV: E=McAfee;i="6500,9779,10453"; a="381140126" X-IronPort-AV: E=Sophos;i="5.93,272,1654585200"; d="scan'208";a="381140126" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Aug 2022 00:55:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,272,1654585200"; d="scan'208";a="672283569" Received: from shbuild999.sh.intel.com ([10.239.147.181]) by fmsmga008.fm.intel.com with ESMTP; 29 Aug 2022 00:55:49 -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>, Dmitry Vyukov Cc: Dave Hansen , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Feng Tang Subject: [PATCH v4 2/4] mm/slub: only zero the requested size of buffer for kzalloc Date: Mon, 29 Aug 2022 15:56:16 +0800 Message-Id: <20220829075618.69069-3-feng.tang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220829075618.69069-1-feng.tang@intel.com> References: <20220829075618.69069-1-feng.tang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 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 | 6 +++--- mm/slab.h | 9 +++++++-- mm/slub.c | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index a5486ff8362a..73ecaa7066e1 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -3253,7 +3253,7 @@ slab_alloc_node(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; } @@ -3506,13 +3506,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 65023f000d42..1c773195cfcd 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -720,12 +720,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 @@ -736,7 +741,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 d8bab650ed99..936b7be0642a 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3360,7 +3360,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; } @@ -3817,11 +3817,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; } -- 2.34.1