From patchwork Fri Nov 18 03:51:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13047695 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D0A1C4167B for ; Fri, 18 Nov 2022 03:52:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232532AbiKRDwF (ORCPT ); Thu, 17 Nov 2022 22:52:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240484AbiKRDwD (ORCPT ); Thu, 17 Nov 2022 22:52:03 -0500 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD95D8F3F8 for ; Thu, 17 Nov 2022 19:52:02 -0800 (PST) Received: by mail-pf1-x42a.google.com with SMTP id y203so3727225pfb.4 for ; Thu, 17 Nov 2022 19:52:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Kl2HCmuKXCRtuCV8keQkppjI9HHVusIDw26V+KNW/hE=; b=TvL+I/WOxW0UWgdWvgJ0gvY2Fk0cjOtxylk/v1UA6dttCvpI1kE/XpZJv57lTN1ZSQ 9zfsGIldon1ArmWFGGhT0hzWaukocjI+WLMq+JWyItCq99MGxbLnN/hq2lmyIomVrFrf KpZuajZ/wA7ltdKsh4/x05FY/9udPWBqve9qc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Kl2HCmuKXCRtuCV8keQkppjI9HHVusIDw26V+KNW/hE=; b=uAMXhGCwCkLtNJTjwGTlVY9GsNm1+0dEJc423e/uAhxvUE3Q6IiIlH1IjawgFRgLDd 6tk/G5rRFIzTx/PfZ3ccmgDpr1QfpikB/x1xJ4egBXmORsKMihEuiKL9SWvs2cdEwStC MzRY6/gVYMeXCoPUm6vq2cmoZi4v9TOaCs9EQ1Xh2vOFmy/jDRPXRFigbByQCUS447/N q3qms76vg97BhsyMF2AFxImqFq/XR9EBQPnGVeuUq+SYPmOYEw8EgEpArn3tLMVfiJOp OR7Dl5c5ChOTRp9mZPjDs2zPIozuGC3SpQARNKnGgkENlf9xB9RxXEZa/c5LCiVuPSzI Mc/g== X-Gm-Message-State: ANoB5pmbOWTQxHfEhxDPI8MMkvvgn+Lokv5HSGDdboWjyl/hyXNQ8OOZ UxKvj4uDMZVNUE7fos5H5+pylg== X-Google-Smtp-Source: AA0mqf7HS6tS+6S6c48a5+nH45KT6BsUA5aJzuSM7l/pjNwX3dKMq7yLshBvt6nVve0qTN8OxxufbA== X-Received: by 2002:a63:42c5:0:b0:460:9fe9:d325 with SMTP id p188-20020a6342c5000000b004609fe9d325mr4873891pga.608.1668743522250; Thu, 17 Nov 2022 19:52:02 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id d8-20020a170902cec800b00186e2b3e12fsm2254854plg.261.2022.11.17.19.52.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 19:52:01 -0800 (PST) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2 1/2] slab: Clean up SLOB vs kmalloc() definition Date: Thu, 17 Nov 2022 19:51:58 -0800 Message-Id: <20221118035200.1269184-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221118034713.gonna.754-kees@kernel.org> References: <20221118034713.gonna.754-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2026; h=from:subject; bh=XCqnPjfv4o/NKArWFRHDgYWfDTlT2kWs5IGt+A3wTyQ=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjdwFfTA7j/LHUkXVUp6HAaWlDXQgvh/IdCZT/aouq MnYGyIOJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY3cBXwAKCRCJcvTf3G3AJp8VEA CnjcKb2iCpqmX0eLs3YrJn7gR2VmSD2hbc20QVLE5MxIZJBJgcap3GTy5i1qgCFw2KASeJLxX0fboz KIcE1Mw81hfNEkXWK6vKZ5xFg/pYKmvV6RV7AyQC947FQXvZIi0BaGFsNkS1H1zkCCkAeVMfN80mI0 1dX8ervJ42PpR9KAL7ySYtqYc5nKiffXK78XUMp0GBTZPg5qaurwfYUORKYh3QgkS5uq37EH6B5l0T uHDTLHL8dx2eDshTmfH5c+aw49XVD8zMUwoFc1A1jeD4exIqQHuOahoU0ZSyyjQW1LRXIQTuCzVJC/ o+tEaCPzfAVUa/qfEBHzHbRQW7rMwSguwAz0QAb+bdJLKrN7MR/LtheapmaVMGQjpjJU3Z3DPEt/wi Aq0IbtyxN6RooERZkVULRpRfS9DRzUv6YnmAXOhC0nXVnSVkHSGjOHyjnRfRDxizmrGvl2J0ZORyGE ehH2e6SDBf8yXy3C2gujb+9Xemv/Z2BxHV4BzI46EpD2x9xHZdgIoffQYFanYzr3R8BADb8voGurr+ PSgtL/PqvYl1cj6TsrtAgXAMpDc4VyT7rSMYo6rpO9XUYPhy9iH8YvnGWQ/R5/YwaqcOYjZ2Qq5KpN h6BaSq7PorQjdZiIwkoCEbhD9KEgyIBjAavfzOHXAo+MmXKGPmUKxMabAB2w== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org As already done for kmalloc_node(), clean up the #ifdef usage in the definition of kmalloc() so that the SLOB-only version is an entirely separate and much more readable function. Cc: Vlastimil Babka Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org Signed-off-by: Kees Cook Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- include/linux/slab.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 1c670c16c737..9033937c758e 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -558,15 +558,15 @@ void *kmalloc_large_node(size_t size, gfp_t flags, int node) __assume_page_align * Try really hard to succeed the allocation but fail * eventually. */ +#ifndef CONFIG_SLOB static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags) { if (__builtin_constant_p(size)) { -#ifndef CONFIG_SLOB unsigned int index; -#endif + if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large(size, flags); -#ifndef CONFIG_SLOB + index = kmalloc_index(size); if (!index) @@ -575,10 +575,18 @@ static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags) return kmalloc_trace( kmalloc_caches[kmalloc_type(flags)][index], flags, size); -#endif } return __kmalloc(size, flags); } +#else +static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags) +{ + if (__builtin_constant_p(size) && size > KMALLOC_MAX_CACHE_SIZE) + return kmalloc_large(size, flags); + + return __kmalloc(size, flags); +} +#endif #ifndef CONFIG_SLOB static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp_t flags, int node) From patchwork Fri Nov 18 03:51:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13047693 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32E48C433FE for ; Fri, 18 Nov 2022 03:52:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240825AbiKRDwE (ORCPT ); Thu, 17 Nov 2022 22:52:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232659AbiKRDwD (ORCPT ); Thu, 17 Nov 2022 22:52:03 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BF058F3ED for ; Thu, 17 Nov 2022 19:52:02 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id q9so3739524pfg.5 for ; Thu, 17 Nov 2022 19:52:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=13POlA7CELUjT6ZjE2YlVT7xlxRT3lU0xqTeymXfKuQ=; b=mln/mgTD/8ZiYnrfEL5sJdItSGA3CsUZXe0Rbw9VmgWwP7++319wUWFlXAOZhmdkE+ Q1A8F59QiRmh/VDbUqCYA2L/XIIyIXNDrRkekGA+mdDaZnqnT7Iacfw8H/f5UlHKWNFL zWFpNrCeB4pUtxOC9D7mAQUfGrSn4vB3tbBI4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=13POlA7CELUjT6ZjE2YlVT7xlxRT3lU0xqTeymXfKuQ=; b=ULhYtcD/kxGr4R+eiUxtUQfhhfO2tUUmv6ouFaJroxFaixgV08sswM5BEkgszBRugD 4L1klVw15YTXIZJ9YO6UD9jcx+1mIkEi0unU10P/bz5hlYOtH4Qsk4bH29ueneE5FSGk 6coQwLIVgDLr9hMuJ8lEJFsV2b/TH0UqL73krf2+8dXMpCb62GDWKwcyy/tjw7nKUMjT SdBho6hJVlXMo9YOnS3FsexZVgbUi1jBpvy4OGbpUaFBXBYYqh6C+EwEjunCJsFM2iGD mIghb9ilj3rcFB0ysy/FQ42oMpj+Y+m+9uC78FXalccrmRZisul+X4HASFhKETNuNyK/ S+Mw== X-Gm-Message-State: ANoB5pkr6xfIEj4E9t8qpqpBM2R3Nu9PNEzeTHb87G/5tmqHC4QpitkQ BpX9o9uNykjLJpwOmjcAL+8sxQ== X-Google-Smtp-Source: AA0mqf6N+OlYszLxbtmqYFwWBrZKb2ySfrwYomT8syL2s6d6kloyN/S01vclXkdw4lKt9+nfwwJ+dw== X-Received: by 2002:a65:6847:0:b0:476:f43e:a86 with SMTP id q7-20020a656847000000b00476f43e0a86mr4988332pgt.363.1668743522032; Thu, 17 Nov 2022 19:52:02 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id u18-20020a170902e5d200b00186748fe6ccsm2276693plf.214.2022.11.17.19.52.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 19:52:01 -0800 (PST) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2 2/2] slab: Remove special-casing of const 0 size allocations Date: Thu, 17 Nov 2022 19:51:59 -0800 Message-Id: <20221118035200.1269184-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221118034713.gonna.754-kees@kernel.org> References: <20221118034713.gonna.754-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2371; h=from:subject; bh=roYcJKurZ1Ad0yqcM7lIDF6nN9ThBNp448RHSIPN1YQ=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjdwFfZxDK7dz/ZV+GaTBVSw8vUtlA5Z9lYg4dYZxh Ty2FDZaJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY3cBXwAKCRCJcvTf3G3AJqB3D/ 92I8BImb3l8xp7T7C/K708vzPl/8cg+RmpOK6O8J9tqppE4B5M+SHEFIwWoiPjeSunOrFQ1DwDx9iZ u69XcPWH0rvSq6XgAS4hKKx3hUEvm7SfHhU/tMqqLMduePCXfFRifcSHXyzlATJyq8Wouii8HIaWo4 wpwfW8/jeErjZCLwxoOQFfOGSdrPISOLAvnII7IHQyoHd/w0i9HLKgRHtB8xe4z9u/B0X33zioYBJN J62lW7fZXdQJey/y170LtBc8LAW/Tw7fwE8hxD0AsbzzOxgYShr8FnHhwwcu0EC4j4GnYBuTey8frV EySb8ats1/DK6YD0YCsS5AQB+/IUERCstiwsPOzLUZgZd8bdSusAxZhGH4iBRRof6wHWXGNrjAVwhn 3Snjp2O9GaBhJWiCAuNsthWjbSRE5KabMfNcSbunYrPfQXvBCXcdsUNxPVAILnJG2RgVexDJXdP1+S 95JSp0z6rrSxXhOefnmqdca5dura/qA18ttumMHL/6w2vykG0fx4VLfMizyQgi3/yTu9ykaTsw77+6 /VGws3PmhsMC0P+WacnTRQRezboPQxcBN65EO5en1ch1MUhYl6E+/yKf9e6POwUMv6e8aO56MI3us9 C/kaUdRHt4xCbf/FUTiy6plT2ZYVnNHudbi4m84b46NIo/ancm1AFn8JpH1A== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org Passing a constant-0 size allocation into kmalloc() or kmalloc_node() does not need to be a fast-path operation, so the static return value can be removed entirely. This is in preparation for making sure that all paths through the inlines result in a full extern function call, where __alloc_size() hints will actually be seen[1] by GCC. (A constant return value of 0 means the "0" allocation size won't be propagated by the inline.) [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503 Cc: Vlastimil Babka Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org Signed-off-by: Kees Cook Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- include/linux/slab.h | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 9033937c758e..84be05208418 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -561,17 +561,13 @@ void *kmalloc_large_node(size_t size, gfp_t flags, int node) __assume_page_align #ifndef CONFIG_SLOB static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags) { - if (__builtin_constant_p(size)) { + if (__builtin_constant_p(size) && size) { unsigned int index; if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large(size, flags); index = kmalloc_index(size); - - if (!index) - return ZERO_SIZE_PTR; - return kmalloc_trace( kmalloc_caches[kmalloc_type(flags)][index], flags, size); @@ -591,17 +587,13 @@ static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags) #ifndef CONFIG_SLOB static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp_t flags, int node) { - if (__builtin_constant_p(size)) { + if (__builtin_constant_p(size) && size) { unsigned int index; if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large_node(size, flags, node); index = kmalloc_index(size); - - if (!index) - return ZERO_SIZE_PTR; - return kmalloc_node_trace( kmalloc_caches[kmalloc_type(flags)][index], flags, node, size);