Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp939866pxb; Wed, 6 Apr 2022 04:54:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNf6GAHJIDoheJLq+XyoVB1Rx/RDxaszq8Gw32Lkl+owNXMwxh5Z07iXzmSgeBujWRajzu X-Received: by 2002:a05:6a00:e8e:b0:4fa:a52f:59cf with SMTP id bo14-20020a056a000e8e00b004faa52f59cfmr8475485pfb.84.1649246040695; Wed, 06 Apr 2022 04:54:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649246040; cv=none; d=google.com; s=arc-20160816; b=B9Z7pWxz6WPE/AertBVKmeeqsw6MnGtbE8cik2g3BGyXRTbUnsO5mwwUn696TF2+ne swmddI+tOJyJmsTmMTKV/Xh7VO1i8U8pfMIPe9HujPQMORJlBN/2CkVVhM9c326pFsWi m0GkwOb+DImOWwmiELNj+rz+ZSFlj6cu9+pdSPCFfSulhkHAaYVUmEeyZEIiOalBivqd S8WTH/Lrf43NI6dJqzVW1p9jroEkHD8DrQXBUqPiJTuTyq6aJM1PlhCObZKcQ9vw0XoW nw4jk4IaeJk3NW0YYuDa0IFRZCnG7dQL3H7+FurmuVYWSk4by+AGXNvrSDx7TrjYkc2P mLbA== 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; bh=PCt4ww2C2ZBFrKENHmtXEGUYamWuffeTriglnh9iAss=; b=olJMcjVm7R0zEs2Mw5V/QTjQtnHd3VcQlGz1w1z+fmDrbpiHh2fUdSmo+bvMRT2Ol7 o0Krw5RsLN2ILNYfJ/84GHAw+5CPtL5f/31EG2xBljoT3AcYeDYued2Pxe3By5lHkHA/ J+GWx5P4Su2NEGRKPTMmd6cGhTJx/hvt1gUt1cC/MECwm85nF24yib7z6VnI8ASOH9Xk 4FBO5cdIrktO5chVchmORhtABHgzC2KfaV9UY3E31flouF7jlPCQW39Ev2n6UPJmT1Zh vOHMFq319KHb1GOIywBzRAr4NDvur0ygC4PFqhlNjRqel5Wrc54o71rWd97Ty2FVPQqS S8Gg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q12-20020a170902f78c00b00153b2d16537si14351338pln.319.2022.04.06.04.54.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 04:54:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6E963642E37; Wed, 6 Apr 2022 03:14:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1578662AbiDEXYY (ORCPT + 99 others); Tue, 5 Apr 2022 19:24:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443718AbiDEPkL (ORCPT ); Tue, 5 Apr 2022 11:40:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B738814FB99 for ; Tue, 5 Apr 2022 06:58:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5268C61910 for ; Tue, 5 Apr 2022 13:58:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D18DAC385A4; Tue, 5 Apr 2022 13:58:21 +0000 (UTC) From: Catalin Marinas To: Will Deacon , Marc Zyngier , Arnd Bergmann , Greg Kroah-Hartman , Andrew Morton , Linus Torvalds Cc: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 08/10] mm/slab: Allow dynamic kmalloc() minimum alignment Date: Tue, 5 Apr 2022 14:57:56 +0100 Message-Id: <20220405135758.774016-9-catalin.marinas@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220405135758.774016-1-catalin.marinas@arm.com> References: <20220405135758.774016-1-catalin.marinas@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 ARCH_KMALLOC_MINALIGN represents the minimum guaranteed kmalloc() alignment but an architecture may require a larger run-time alignment. Do not create kmalloc caches smaller than arch_kmalloc_minalign(). Signed-off-by: Catalin Marinas Cc: Andrew Morton --- include/linux/slab.h | 2 ++ mm/slab.c | 6 +----- mm/slab.h | 2 ++ mm/slab_common.c | 33 +++++++++++++++++++++++---------- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index d58211bdeceb..2137dba85691 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -332,6 +332,8 @@ enum kmalloc_cache_type { extern struct kmem_cache * kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1]; +unsigned int arch_kmalloc_minalign(void); + /* * Define gfp bits that should not be set for KMALLOC_NORMAL. */ diff --git a/mm/slab.c b/mm/slab.c index b04e40078bdf..4aaeeb9c994d 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -1256,11 +1256,7 @@ void __init kmem_cache_init(void) * Initialize the caches that provide memory for the kmem_cache_node * structures first. Without this, further allocations will bug. */ - kmalloc_caches[KMALLOC_NORMAL][INDEX_NODE] = create_kmalloc_cache( - kmalloc_info[INDEX_NODE].name[KMALLOC_NORMAL], - kmalloc_info[INDEX_NODE].size, - ARCH_KMALLOC_FLAGS, 0, - kmalloc_info[INDEX_NODE].size); + new_kmalloc_cache(INDEX_NODE, KMALLOC_NORMAL, ARCH_KMALLOC_FLAGS); slab_state = PARTIAL_NODE; setup_kmalloc_cache_index_table(); diff --git a/mm/slab.h b/mm/slab.h index fd7ae2024897..e9238406602a 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -283,6 +283,8 @@ int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags); struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size, slab_flags_t flags, unsigned int useroffset, unsigned int usersize); +void __init new_kmalloc_cache(int idx, enum kmalloc_cache_type type, + slab_flags_t flags); extern void create_boot_cache(struct kmem_cache *, const char *name, unsigned int size, slab_flags_t flags, unsigned int useroffset, unsigned int usersize); diff --git a/mm/slab_common.c b/mm/slab_common.c index 6ee64d6208b3..594d8a8a68d0 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -838,9 +838,18 @@ void __init setup_kmalloc_cache_index_table(void) } } -static void __init +unsigned int __weak arch_kmalloc_minalign(void) +{ + return ARCH_KMALLOC_MINALIGN; +} + +void __init new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags) { + unsigned int minalign = arch_kmalloc_minalign(); + unsigned int aligned_size = kmalloc_info[idx].size; + int aligned_idx = idx; + if (type == KMALLOC_RECLAIM) { flags |= SLAB_RECLAIM_ACCOUNT; } else if (IS_ENABLED(CONFIG_MEMCG_KMEM) && (type == KMALLOC_CGROUP)) { @@ -851,10 +860,17 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags) flags |= SLAB_ACCOUNT; } - kmalloc_caches[type][idx] = create_kmalloc_cache( - kmalloc_info[idx].name[type], - kmalloc_info[idx].size, flags, 0, - kmalloc_info[idx].size); + if (minalign > ARCH_KMALLOC_MINALIGN) { + aligned_size = ALIGN(aligned_size, minalign); + aligned_idx = __kmalloc_index(aligned_size, false); + } + + if (!kmalloc_caches[type][aligned_idx]) + kmalloc_caches[type][aligned_idx] = create_kmalloc_cache( + kmalloc_info[aligned_idx].name[type], + aligned_size, flags, 0, aligned_size); + if (idx != aligned_idx) + kmalloc_caches[type][idx] = kmalloc_caches[type][aligned_idx]; /* * If CONFIG_MEMCG_KMEM is enabled, disable cache merging for @@ -904,11 +920,8 @@ void __init create_kmalloc_caches(slab_flags_t flags) struct kmem_cache *s = kmalloc_caches[KMALLOC_NORMAL][i]; if (s) { - kmalloc_caches[KMALLOC_DMA][i] = create_kmalloc_cache( - kmalloc_info[i].name[KMALLOC_DMA], - kmalloc_info[i].size, - SLAB_CACHE_DMA | flags, 0, - kmalloc_info[i].size); + new_kmalloc_cache(i, KMALLOC_DMA, + SLAB_CACHE_DMA | flags); } } #endif