From patchwork Thu May 13 12:44:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 12255765 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-24.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27FA2C433B4 for ; Thu, 13 May 2021 12:44:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B5F2E613D3 for ; Thu, 13 May 2021 12:44:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B5F2E613D3 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 38F6D6B0036; Thu, 13 May 2021 08:44:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 318556B006E; Thu, 13 May 2021 08:44:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1446F6B0070; Thu, 13 May 2021 08:44:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0086.hostedemail.com [216.40.44.86]) by kanga.kvack.org (Postfix) with ESMTP id D63F96B0036 for ; Thu, 13 May 2021 08:44:47 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5F4D9181AEF1F for ; Thu, 13 May 2021 12:44:47 +0000 (UTC) X-FDA: 78136177014.19.BF8BB47 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) by imf04.hostedemail.com (Postfix) with ESMTP id 5E19E3C7 for ; Thu, 13 May 2021 12:44:40 +0000 (UTC) Received: by mail-wr1-f44.google.com with SMTP id d11so26755488wrw.8 for ; Thu, 13 May 2021 05:44:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=hD4dt2Eg4RnvV5fMv2oKGaKTjuE+r6cuvUcfND+PBWY=; b=SESeB8mMhwbp0etWFYXLMhJ9XUZKmkm6VG+5UuiTFNsT9TXOL1cv9S1VkGKPnBGFAc WGWFjUPBEEqqcibuhnmqg9QND5v4/UVxC2hTmqj1ALVZweU+XcwIjiG74KNBZa1RWwEs fz34IqHzdRJX0ChorJTRrA5jM3JRLMEiZDahWbRhxRMpNtRWFxh54ciZCyNLvFFow+rx AtLPSP88hw6yL+8uIRb1Lad9+Bln1du9fkj3vYOEhwzBG7nEtJKXffqbVBSnwb1O7uYD HvzGG+bDMlPjfcMreLouWI+USIE0Ry+uD8wM1+dvP3ptDrJ7Uy6hezldmmqiO3pTqHZu JQpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=hD4dt2Eg4RnvV5fMv2oKGaKTjuE+r6cuvUcfND+PBWY=; b=Htnh6uDTC2JquKbiBOa1a5RbDjl9PIIFzA4HCY0qhIqj2wZykF1+YKP+ILZZIHwPhT yFupdLJcuQCodIcW57VH7ZYVivQmsqGwQn6ZCahJp5M/aIX9QwD5KFnlspexJtIrWjoH 6zulgZZMrX/3LMpZepKNoQWhL2H3N2a/9u8xb5Jgg3mMA5iyWae9QQ1ThtExZd7gYoYi lhVVvfhrKV+BMnMa/mmxGp73Hs77pTrkTcrteUm9gzB9+hDoxEyVe/qiZ1y+Tf2DXhWf fkgXxQzP9fg6vFwzhhB+ObgVPytGKxtjfDEChNVRobrplgUTQttncbefgxih8fQOArno fkmg== X-Gm-Message-State: AOAM531SPBLjEvnLDj29sSdS38O62A27RCwsY6vM99LOslrN+ON55+98 XJaxfVM7NhXmikJHG0Mjh9/CiA== X-Google-Smtp-Source: ABdhPJyt3R7b9kPzU7PC4XgXZ9FfZE63gu+BsAWl+WIRN1Ky7ldG1rtcDAWHhkMz+ooQEc9pZ0I13g== X-Received: by 2002:adf:e944:: with SMTP id m4mr9449755wrn.10.1620909885562; Thu, 13 May 2021 05:44:45 -0700 (PDT) Received: from elver.google.com ([2a00:79e0:15:13:c5e0:2ff3:baa4:b294]) by smtp.gmail.com with ESMTPSA id n12sm2054569wmq.29.2021.05.13.05.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 May 2021 05:44:44 -0700 (PDT) Date: Thu, 13 May 2021 14:44:39 +0200 From: Marco Elver To: Andrew Morton Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>, vbabka@suse.cz, iamjoonsoo.kim@lge.com, rientjes@google.com, penberg@kernel.org, cl@linux.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, glider@google.com Subject: [PATCH] kfence: test: fix for "mm, slub: change run-time assertion in kmalloc_index() to compile-time" Message-ID: References: <20210511173448.GA54466@hyeyoo> <20210512195227.245000695c9014242e9a00e5@linux-foundation.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210512195227.245000695c9014242e9a00e5@linux-foundation.org> User-Agent: Mutt/2.0.5 (2021-01-21) X-Rspamd-Queue-Id: 5E19E3C7 Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=SESeB8mM; spf=pass (imf04.hostedemail.com: domain of elver@google.com designates 209.85.221.44 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam04 X-Stat-Signature: 9j8n71dx3iyobbkoxnaworhzun5u6unc Received-SPF: none (google.com>: No applicable sender policy available) receiver=imf04; identity=mailfrom; envelope-from=""; helo=mail-wr1-f44.google.com; client-ip=209.85.221.44 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620909880-765532 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: Enable using kmalloc_index() in allocator test modules again where the size may be non-constant, while ensuring normal usage always passes a constant size. Split the definition into __kmalloc_index(size, size_is_constant), and a definition of kmalloc_index(s), matching the old kmalloc_index() interface, but that still requires size_is_constant==true. This ensures that normal usage of kmalloc_index() always passes a constant size. While the __-prefix should make it clearer that the function is to be used with care, also rewrite the "Note" to highlight the restriction (and add a hint to kmalloc_slab()). The alternative considered here is to export kmalloc_slab(), but given it is internal to mm/ and not in , we should probably avoid exporting it. Allocator test modules will work just fine by using __kmalloc_index(s, false). Link: https://lkml.kernel.org/r/20210512195227.245000695c9014242e9a00e5@linux-foundation.org Signed-off-by: Marco Elver --- include/linux/slab.h | 15 +++++++++++---- mm/kfence/kfence_test.c | 5 +++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 27d142564557..465c5d9d9630 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -347,10 +347,13 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) * 2 = 129 .. 192 bytes * n = 2^(n-1)+1 .. 2^n * - * Note: there's no need to optimize kmalloc_index because it's evaluated - * in compile-time. + * Note: __kmalloc_index() is compile-time optimized, and not runtime optimized; + * typical usage is via kmalloc_index() and therefore evaluated at compile-time. + * Callers where !size_is_constant should only be test modules, where runtime + * overheads of __kmalloc_index() can be tolerated. Also see kmalloc_slab(). */ -static __always_inline unsigned int kmalloc_index(size_t size) +static __always_inline unsigned int __kmalloc_index(size_t size, + bool size_is_constant) { if (!size) return 0; @@ -386,11 +389,15 @@ static __always_inline unsigned int kmalloc_index(size_t size) if (size <= 16 * 1024 * 1024) return 24; if (size <= 32 * 1024 * 1024) return 25; - BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()"); + if (size_is_constant) + BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()"); + else + BUG(); /* Will never be reached. Needed because the compiler may complain */ return -1; } +#define kmalloc_index(s) __kmalloc_index(s, true) #endif /* !CONFIG_SLOB */ void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __malloc; diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c index 4acf4251ee04..7f24b9bcb2ec 100644 --- a/mm/kfence/kfence_test.c +++ b/mm/kfence/kfence_test.c @@ -197,7 +197,7 @@ static void test_cache_destroy(void) static inline size_t kmalloc_cache_alignment(size_t size) { - return kmalloc_caches[kmalloc_type(GFP_KERNEL)][kmalloc_index(size)]->align; + return kmalloc_caches[kmalloc_type(GFP_KERNEL)][__kmalloc_index(size, false)]->align; } /* Must always inline to match stack trace against caller. */ @@ -267,7 +267,8 @@ static void *test_alloc(struct kunit *test, size_t size, gfp_t gfp, enum allocat if (is_kfence_address(alloc)) { struct page *page = virt_to_head_page(alloc); - struct kmem_cache *s = test_cache ?: kmalloc_caches[kmalloc_type(GFP_KERNEL)][kmalloc_index(size)]; + struct kmem_cache *s = test_cache ?: + kmalloc_caches[kmalloc_type(GFP_KERNEL)][__kmalloc_index(size, false)]; /* * Verify that various helpers return the right values