Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp4559913ybe; Mon, 16 Sep 2019 14:27:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqz4apOWrUgaRRdvDfND48Y5e9nvVlzTABVG7uc9VTv6bdi+17WOvBTbwXC7NgydLqam/vYh X-Received: by 2002:a05:6402:7d1:: with SMTP id u17mr1441271edy.132.1568669278673; Mon, 16 Sep 2019 14:27:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568669278; cv=none; d=google.com; s=arc-20160816; b=IArIDCWXRCDb3e0uhxsshdk48PBkEpxSJqx6CuUkNRN1nucdWdBYGnc6HCiDo1SzZ6 UogB/6jEjnXu4BCf5gHRPMvZbpoYBC2bGlOnfU/w3PsoKH52CYy2e0oDfUayIB4PRW2d QLNA/uhGPE4AbIyA40EVt4JIsdGHqt4dTFChAicDaLDoLN4bdqccQtu7KJcYWRI6Ot5Y he56GmgcKBhf4Z43vWTnRbamCHu3tTdCsHBSGi3uJ536tgy/b2S3k0W78ETsV60yOp1v FS0zTq6+gSXASAPo3D9gBEeHUdH3EideD6Gqtsyx2CKk3Yq/5TryBty9bnpGvtq0EGDD i4cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QbVF1PSqvP2xnQhufiGK8AJvI4iylUVt8uZ1yhy2wwc=; b=p8wLuacGR6evXG2CYjLCkn9Hd6PEu2alW3d4R/fr/TYpTmn+u/rRHzn+ICY/eLjTkL pSGpePRWX7UW1XBBpUrl4IIo82mW/LeobE49udM43AN3poMvoSJIZ3QUWSgdENOO/I8O ntMcikZzTRyhCxexO/q1l9BnNq45DheNhV4LZHdssrQr7Q8k8nKwU/V1ShJTD/Q/gNSU xUtRPMlr4/LEfl1Wnp+oRmv+LTeVyxcwwVssIBeDzCORuCMWUd89V6KHCUnyIUQId0AE qtF55QOEOTSDDw2kBG20Z/zawe/VdkVpfjzU1PGaExwcFGj7g+/unZdLPQgvP2Yq6Y4l A2aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YR9U5b2A; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v27si72396ejb.182.2019.09.16.14.27.35; Mon, 16 Sep 2019 14:27:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YR9U5b2A; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732719AbfIPOrF (ORCPT + 99 others); Mon, 16 Sep 2019 10:47:05 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:42075 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732374AbfIPOrF (ORCPT ); Mon, 16 Sep 2019 10:47:05 -0400 Received: by mail-pl1-f194.google.com with SMTP id e5so5967965pls.9 for ; Mon, 16 Sep 2019 07:47:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QbVF1PSqvP2xnQhufiGK8AJvI4iylUVt8uZ1yhy2wwc=; b=YR9U5b2Al2uZzdBiRyBtWDbXpGBtWrT4PMKL+x1AZt3LYMS+m3xzN98uaYTyO+CFUF 7IhKvR0RKbfMl53lB1Ph16DPrOSZmSqW+QKgSAysJpNWZ2zntkD2HbA5vaUegHGgsgAZ oJ+A1++3S4ceNlCIng74lZeCWfICtAWZCEdhaZQwsZKVTrdPCKh0WuGIjZhjI4aE+o5m +BxDxp98Qw4Z9Bnmonbf1saR7UI/vHeAMNjYTplEWhlTdWKZdRVyuv56bI+tizq7Sit9 GdxJfk7bGuackN22jFCiFIs8FrfIzZC0RtxbQiBgucqMNmxnwNsiCOCCLWLgf4vxn6CR 44lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QbVF1PSqvP2xnQhufiGK8AJvI4iylUVt8uZ1yhy2wwc=; b=jOdVKXNynXSWBZHRCMcBvEUCzlC3HeEoik3vNBhjqY5vPS3fijXz7A9JETTOVWCws+ FGJ9UttYYyV1ZJ6jcGO24CbFNudVEee1AlQ9Rv6gtZNLqvyMd0ctRfKC9cJxegLT2Btm 3gZoxFbRyUaySF/G1xq0y8FpuLnwUUpIpp3NBbwMWFqx9Unt65wFVeUf6dNx2SbsdTLz Zx7mHjGHTRCoculS3lzBoKRpeRHTm+EWAlqsCPpvtlnhz9ag3JuG4p6GLrNwkSPgPKn/ bD0sm2EhWo1tuFxBbLhZXitAyX6w49BM/QJmkMAROrSyQ7OZL57z9rkRiHgjL3Lf2t7P tf0g== X-Gm-Message-State: APjAAAUn4xsak5KMciRiVJSuRo9gArmo8OB3bhpYsmaHAiUJMIG3aRr0 q6hIZa4ND6QH8T2TIqnLDbQ= X-Received: by 2002:a17:902:a418:: with SMTP id p24mr161368plq.312.1568645225140; Mon, 16 Sep 2019 07:47:05 -0700 (PDT) Received: from localhost.localdomain.localdomain ([2408:823c:c11:160:b8c3:8577:bf2f:3]) by smtp.gmail.com with ESMTPSA id d190sm15036004pgc.25.2019.09.16.07.46.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2019 07:47:04 -0700 (PDT) From: Pengfei Li To: akpm@linux-foundation.org Cc: vbabka@suse.cz, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, guro@fb.com, Pengfei Li Subject: [PATCH v5 4/7] mm, slab: Return ZERO_SIZE_ALLOC for zero sized kmalloc requests Date: Mon, 16 Sep 2019 22:45:55 +0800 Message-Id: <20190916144558.27282-5-lpf.vector@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190916144558.27282-1-lpf.vector@gmail.com> References: <20190916144558.27282-1-lpf.vector@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a preparation patch, just replace 0 with ZERO_SIZE_ALLOC as the return value of zero sized requests. Signed-off-by: Pengfei Li Acked-by: David Rientjes --- include/linux/slab.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index e773e5764b7b..1f05f68f2c3e 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -121,14 +121,20 @@ #define SLAB_DEACTIVATED ((slab_flags_t __force)0x10000000U) /* - * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. + * ZERO_SIZE_ALLOC will be returned by kmalloc_index() if it was zero sized + * requests. * + * After that, ZERO_SIZE_PTR will be returned by the function that called + * kmalloc_index(). + * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault. * * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can. * Both make kfree a no-op. */ -#define ZERO_SIZE_PTR ((void *)16) +#define ZERO_SIZE_ALLOC (UINT_MAX) + +#define ZERO_SIZE_PTR ((void *)16) #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \ (unsigned long)ZERO_SIZE_PTR) @@ -350,7 +356,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) static __always_inline unsigned int kmalloc_index(size_t size) { if (!size) - return 0; + return ZERO_SIZE_ALLOC; if (size <= KMALLOC_MIN_SIZE) return KMALLOC_SHIFT_LOW; @@ -546,7 +552,7 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags) #ifndef CONFIG_SLOB index = kmalloc_index(size); - if (!index) + if (index == ZERO_SIZE_ALLOC) return ZERO_SIZE_PTR; return kmem_cache_alloc_trace( @@ -564,7 +570,7 @@ static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) size <= KMALLOC_MAX_CACHE_SIZE) { unsigned int i = kmalloc_index(size); - if (!i) + if (i == ZERO_SIZE_ALLOC) return ZERO_SIZE_PTR; return kmem_cache_alloc_node_trace( -- 2.21.0