Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp3211199ybe; Sun, 15 Sep 2019 10:00:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqxHCefRf9rRuTWdPxeKrF0UFGKdlq2FXfc4KQxU7jQj4PCUv4KfBAWI/GI8Srtv99UrRSY9 X-Received: by 2002:aa7:c355:: with SMTP id j21mr57082517edr.210.1568566816597; Sun, 15 Sep 2019 10:00:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568566816; cv=none; d=google.com; s=arc-20160816; b=0+SDwKlgaqwH7cQtuOWIFY+GH7JmKssfc87dtKMV8WbR3ANCQ+HBShYRxDNAhh/Lvm ruZEub1QhEArEk9BPWTGoavHK8p89TGpbu9iQxSqliUW6AoFUtWo/eim9+/Xe5kY2vRO OmkYlAp3IdlJ8cfiPpgGe71+vt3WRDy3dUHJpHT4H1zVODdb/fXOrwD53rRHb2hNyVC6 bC8iymU5vwQWd8Ikx557uC5zq/le3DaPanDf6AXNZkNO+LEG+0bvNu/G2Scbnv07YPyM ME76a3InHHEKObdx9O1eLEULcqY2fQYCP7EIHimmT0QIovGUaAVqXnSXOqBYuyjlza1k nbYQ== 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=nii43oMTaFtktKbnijjugyrLFfb5ZKvbiN8kMj3vNPE=; b=FS4GEXZuYnLCQ2UaHzE2CzP1hUtYvOyOYPqFDV12J4IYJ7Hr7hbunuxmXkZ2OlEEos 2HYFK2U5jObQKlJfmSPVUuZk5NHv9uWzh+QIfhg1lkGhJQPpBskR9EZH1QKcbjgu9GqX 98QHlDRa6x3eIoETC5PTHQ+Jn5QoKJLCfIUiwFcfbs986tXrYxJ46nkvMrJus1BjS8YA 5u7gHI+Vk/44RzRq3RbIqPAO1h/hck96djvrkGezIyg28ot7WGF6nIXLtk7LcolpTkzV Eueu/E8lEommmdV2CYYp1tlgPL5J+gMBslQU4FjjX7FiW6N7EN1Iltjw9Y0JbipUgQ6C V+0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jo+e4APs; 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 bo15si4365503ejb.16.2019.09.15.09.59.52; Sun, 15 Sep 2019 10:00:16 -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=jo+e4APs; 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 S1726665AbfIOQwt (ORCPT + 99 others); Sun, 15 Sep 2019 12:52:49 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:46237 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726372AbfIOQwt (ORCPT ); Sun, 15 Sep 2019 12:52:49 -0400 Received: by mail-pg1-f194.google.com with SMTP id a3so167060pgm.13 for ; Sun, 15 Sep 2019 09:52:48 -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=nii43oMTaFtktKbnijjugyrLFfb5ZKvbiN8kMj3vNPE=; b=jo+e4APs5xDRR8vZnH8rVBXMdzgGhpBwxw1z0mC3zae1JlCpMP62+atbzfzG4DzlJ4 weLCzW1zyxAYbUHZEPNrQtsLGmmxNCJWoa02cfILoRJfidraJ/kiXJgAl7L/E8Q0fs9c Dn/J+0SvCTaHBHWnlM2UnCAVBdLyy6KgR6bwLi7xR2wQwJ/MXLv8ynXcZwlUN4nwfe+s mxH+qYSu1vigjSVG1BrMLQUwD7FpJli522rbBGlbnGrt3xKG30eSKffnD0Y2HkDdyYzk pUuTC/IUU8TzL15qlK7Wzrw6vMLteSA9xTkhgzsInyxQftpd8Cy5PZjQZfwwplgw50+k yv6Q== 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=nii43oMTaFtktKbnijjugyrLFfb5ZKvbiN8kMj3vNPE=; b=Zq91PpKX3VEkJzbS296rsYU0B9asGM2QUcmX60YlCCGPdHXEphtFuPfBfZ85tQnwEY 2gVnZZzIiha8NC5krB4Fa/9QvmcMm0s+GgYGZtQkEa7pcNe7pKS1mOD3C15nY0K6WzKo wDCDrht8uagqJcvP4cRq3eCbV2s5u3FN0Usq8ZrdfrMxgLhhAVxTt0CQ3qEokAYkJ30P aHNMuwJnpbEzF3CeXG+4C6jTe+vwqvk7jCnWBtMOJfmtu697h5E+jB/hS4jf4RJVvXMk GFofWRCM6p5FZOOTEtDq4pvsrpXzR7jJ/s+8VUBa1f35rsKSbOVu8YafVaPpofaiZc8h iWzA== X-Gm-Message-State: APjAAAXhudvkoxQjMIE/tlfhCAJX9TZbyps1Uj3kY4nFCTGVlPCG7vlc jp2Tv+ziuSBb/xXoSgmTEOs= X-Received: by 2002:a62:7c4d:: with SMTP id x74mr7837018pfc.95.1568566368554; Sun, 15 Sep 2019 09:52:48 -0700 (PDT) Received: from localhost.localdomain.localdomain ([2408:823c:c11:160:b8c3:8577:bf2f:3]) by smtp.gmail.com with ESMTPSA id a4sm4383259pgq.6.2019.09.15.09.52.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Sep 2019 09:52:48 -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 v4 4/7] mm, slab: Return ZERO_SIZE_ALLOC for zero sized kmalloc requests Date: Mon, 16 Sep 2019 00:51:15 +0800 Message-Id: <20190915165121.7237-7-lpf.vector@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190915165121.7237-1-lpf.vector@gmail.com> References: <20190915165121.7237-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 --- 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