From patchwork Tue Nov 5 02:27:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 13862274 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DFBCD1CA15 for ; Tue, 5 Nov 2024 02:28:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC1DD6B009E; Mon, 4 Nov 2024 21:28:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A70E96B00A0; Mon, 4 Nov 2024 21:28:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9389A6B00A1; Mon, 4 Nov 2024 21:28:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 766126B009E for ; Mon, 4 Nov 2024 21:28:02 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 291C040CF1 for ; Tue, 5 Nov 2024 02:28:02 +0000 (UTC) X-FDA: 82750456068.14.9091077 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf25.hostedemail.com (Postfix) with ESMTP id E401CA000D for ; Tue, 5 Nov 2024 02:27:36 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JYzGDOAI; spf=pass (imf25.hostedemail.com: domain of koichiro.den@gmail.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=koichiro.den@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730773544; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=3W30/6d3+CvWP0OuyYGi+x0OLlLYD4V/Fui7cKummCc=; b=04QQioWWZpHFMyfKRMUw90IhPMk7nxkzNAk+yVII4Pe07kt//nps/y4NtAu7c4hFgZcbc1 Jk9hlA+ahgmTTiesXMquhM9nwKT3mX301dlcMvvwtyYZioR/SLmLh4C1GW5SSfstFXOTxj I4kn1LikAn5outnBgmumbbkV155rsPs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730773544; a=rsa-sha256; cv=none; b=TG64Mk66o2Cm7I1UYdkJSaB+rGO+T+Jpu/QPUO9s/IJKn0e5KgIe5BUJRA3CNzmTgVZiSx V44eECM1Zl/LGWvgOMffDPLUFpmJJ80x/FrTrTZ0Y1Vtyp2XxpaohUKGXhXByR2Q0a1cNA y8fS0OXC3I6DnqsnjoI1XwS1OrmDLTg= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JYzGDOAI; spf=pass (imf25.hostedemail.com: domain of koichiro.den@gmail.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=koichiro.den@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-723db2798caso1466591b3a.0 for ; Mon, 04 Nov 2024 18:28:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730773679; x=1731378479; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3W30/6d3+CvWP0OuyYGi+x0OLlLYD4V/Fui7cKummCc=; b=JYzGDOAIdYugZ4D/g26oow5iw0Xp5uxrqx+JkEEHDu4yI8u/c+zEhgNjv0Yn35Rl7n 87iRQpJwO+iBfn6QEwYwLAyGm0XJw1+0s76HktjFktx6Cr8ly/5FojqeGV2kmGmSv+xM YEKCvBRLTw1jwotyIoHoLuUdChC12f+ukemHXZEkYVtfhhjNbLSX/790mIh/Aggw6TRv ONexRLPih+YtTc7TVLaa9oCJ7Og3e/O4IwvpLjAvNR9w+2ryN+aKuEP85CxlQRL8fgsu m7SlJyRKGDcU7ZQ43TkHgTQDwwr1rPpnOwC/HZMTI3kCL015FCCDB6Jx6rx51IVaO666 IlHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730773679; x=1731378479; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3W30/6d3+CvWP0OuyYGi+x0OLlLYD4V/Fui7cKummCc=; b=WEZR+8KEaWiBA0uKwReppKrsNC53y0nNRfau5zDty6uScj4aGoYGXUV55gWaKk97ZA fmOF5DjX+K6HET3Gmi2YLG43QNQCzp3zydPpPc6BZPxBg/aLD1TeEKAm6Xf4wmy00T7Y 6eukU9yR15YXr5QiUIk0nhIl4/yF3ssyYHaosAuItahKq0if5dI+Vj0njLdav0Xap6cS oYiVZyxJvsOkNjZPyNARuNfylsMiW+JvO6h8o6l8pxmyUaUiEjf4Dw6S3ShK+DJGb9zS dscCHi/i8lQTt+bTNluEus+ihZa5dGClyOaO/vCI51JQVRomF5D9DmlcweQFVJLLNvIx igjQ== X-Forwarded-Encrypted: i=1; AJvYcCUHiLHqnGn+ZJroam8Iy4FOUU+bQ3GgzvW3aAmL0P/Uj9z1jchaGOIe8taNzB2S+sf5s6wSGKDgiw==@kvack.org X-Gm-Message-State: AOJu0Yz9PEpxyvP7gwqVUJPwSh/T7kAuyovOPQCsHin/AzTMdTtdRKAW K09Lzk8fHy8Y3ERF/ckoJVYwGE5381uBPgcixl3wTGTr8ng0hTg0 X-Google-Smtp-Source: AGHT+IEBfwZMMO7o+N9Ba2wkkaEAOh/cpsr7ZZW+06lkYCrZztGf4fqTtpOEsMgOKCnQLNdqdcWSUw== X-Received: by 2002:a05:6a00:4644:b0:71e:7636:3323 with SMTP id d2e1a72fcca58-720ab3babdemr28407776b3a.7.1730773678850; Mon, 04 Nov 2024 18:27:58 -0800 (PST) Received: from z790sl.. ([240f:74:7be:1:54ee:9253:695b:4125]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc2f17fasm8402127b3a.180.2024.11.04.18.27.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Nov 2024 18:27:58 -0800 (PST) From: Koichiro Den To: vbabka@suse.cz Cc: cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, kees@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, koichiro.den@gmail.com Subject: [PATCH v2] mm/slab: fix warning caused by duplicate kmem_cache creation in kmem_buckets_create Date: Tue, 5 Nov 2024 11:27:47 +0900 Message-ID: <20241105022747.2819151-1-koichiro.den@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E401CA000D X-Stat-Signature: cob5jo7g85eg8isxxd38aepmmak854hh X-HE-Tag: 1730773656-685818 X-HE-Meta: U2FsdGVkX1+8D3kE18KvWUNxupxjWa1o7v55tP+oL91IC970gpzr8n99jCZlmBwrJAjOqf3X5BnW2519fcWVmifrKh85FJPKDK0t0I/9xy3V5uDtR9MXBYlkvPuaSnjTKTGjkLY9gtF1o3lAczpIDR8HlaW6ZCY61Et/vJNwRdChksbR6k1CMk6c0FioLeZaVq45OLJnOxRjLroRfbkwTOyfNJPh/4i8YDhVgyd7N4LLmHNvVS6n0JdZJS3Z9Id4ZM/iAx3BV8mSYAM8TSahsXuQnMeuFUj3qKzCZUwcgZtm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UnTHD4YUpBgFG8FIX+w+nCIFkWLYmuA0wXsPpo/zAYsmMSSTiBrLkGWl4m1u7tbyBXDGOj7ApFYpRCbbuPbU0EEOUyVpxv/IJY3VMVdHYE5UBsGYaurm48XvbDy1zRBSOhzpK1PcpH3lRNH2HyEj4VxZU/kGliCcoiQQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Commit b035f5a6d852 ("mm: slab: reduce the kmalloc() minimum alignment if DMA bouncing possible") reduced ARCH_KMALLOC_MINALIGN to 8 on arm64. However, with KASAN_HW_TAGS enabled, arch_slab_minalign() becomes 16. This causes kmalloc_caches[*][8] to be aliased to kmalloc_caches[*][16], resulting in kmem_buckets_create() attempting to create a kmem_cache for size 16 twice. This duplication triggers warnings on boot: [ 2.325108] ------------[ cut here ]------------ [ 2.325135] kmem_cache of name 'memdup_user-16' already exists [ 2.325783] WARNING: CPU: 0 PID: 1 at mm/slab_common.c:107 __kmem_cache_create_args+0xb8/0x3b0 [ 2.327957] Modules linked in: [ 2.328550] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.12.0-rc5mm-unstable-arm64+ #12 [ 2.328683] Hardware name: QEMU QEMU Virtual Machine, BIOS 2024.02-2 03/11/2024 [ 2.328790] pstate: 61000009 (nZCv daif -PAN -UAO -TCO +DIT -SSBS BTYPE=--) [ 2.328911] pc : __kmem_cache_create_args+0xb8/0x3b0 [ 2.328930] lr : __kmem_cache_create_args+0xb8/0x3b0 [ 2.328942] sp : ffff800083d6fc50 [ 2.328961] x29: ffff800083d6fc50 x28: f2ff0000c1674410 x27: ffff8000820b0598 [ 2.329061] x26: 000000007fffffff x25: 0000000000000010 x24: 0000000000002000 [ 2.329101] x23: ffff800083d6fce8 x22: ffff8000832222e8 x21: ffff800083222388 [ 2.329118] x20: f2ff0000c1674410 x19: f5ff0000c16364c0 x18: ffff800083d80030 [ 2.329135] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000 [ 2.329152] x14: 0000000000000000 x13: 0a73747369786520 x12: 79646165726c6120 [ 2.329169] x11: 656820747563205b x10: 2d2d2d2d2d2d2d2d x9 : 0000000000000000 [ 2.329194] x8 : 0000000000000000 x7 : 0000000000000000 x6 : 0000000000000000 [ 2.329210] x5 : 0000000000000000 x4 : 0000000000000000 x3 : 0000000000000000 [ 2.329226] x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000000 [ 2.329291] Call trace: [ 2.329407] __kmem_cache_create_args+0xb8/0x3b0 [ 2.329499] kmem_buckets_create+0xfc/0x320 [ 2.329526] init_user_buckets+0x34/0x78 [ 2.329540] do_one_initcall+0x64/0x3c8 [ 2.329550] kernel_init_freeable+0x26c/0x578 [ 2.329562] kernel_init+0x3c/0x258 [ 2.329574] ret_from_fork+0x10/0x20 [ 2.329698] ---[ end trace 0000000000000000 ]--- [ 2.403704] ------------[ cut here ]------------ [ 2.404716] kmem_cache of name 'msg_msg-16' already exists [ 2.404801] WARNING: CPU: 2 PID: 1 at mm/slab_common.c:107 __kmem_cache_create_args+0xb8/0x3b0 [ 2.404842] Modules linked in: [ 2.404971] CPU: 2 UID: 0 PID: 1 Comm: swapper/0 Tainted: G W 6.12.0-rc5mm-unstable-arm64+ #12 [ 2.405026] Tainted: [W]=WARN [ 2.405043] Hardware name: QEMU QEMU Virtual Machine, BIOS 2024.02-2 03/11/2024 [ 2.405057] pstate: 60400009 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) [ 2.405079] pc : __kmem_cache_create_args+0xb8/0x3b0 [ 2.405100] lr : __kmem_cache_create_args+0xb8/0x3b0 [ 2.405111] sp : ffff800083d6fc50 [ 2.405115] x29: ffff800083d6fc50 x28: fbff0000c1674410 x27: ffff8000820b0598 [ 2.405135] x26: 000000000000ffd0 x25: 0000000000000010 x24: 0000000000006000 [ 2.405153] x23: ffff800083d6fce8 x22: ffff8000832222e8 x21: ffff800083222388 [ 2.405169] x20: fbff0000c1674410 x19: fdff0000c163d6c0 x18: ffff800083d80030 [ 2.405185] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000 [ 2.405201] x14: 0000000000000000 x13: 0a73747369786520 x12: 79646165726c6120 [ 2.405217] x11: 656820747563205b x10: 2d2d2d2d2d2d2d2d x9 : 0000000000000000 [ 2.405233] x8 : 0000000000000000 x7 : 0000000000000000 x6 : 0000000000000000 [ 2.405248] x5 : 0000000000000000 x4 : 0000000000000000 x3 : 0000000000000000 [ 2.405271] x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000000 [ 2.405287] Call trace: [ 2.405293] __kmem_cache_create_args+0xb8/0x3b0 [ 2.405305] kmem_buckets_create+0xfc/0x320 [ 2.405315] init_msg_buckets+0x34/0x78 [ 2.405326] do_one_initcall+0x64/0x3c8 [ 2.405337] kernel_init_freeable+0x26c/0x578 [ 2.405348] kernel_init+0x3c/0x258 [ 2.405360] ret_from_fork+0x10/0x20 [ 2.405370] ---[ end trace 0000000000000000 ]--- To address this, alias kmem_cache for sizes smaller than min alignment to the aligned sized kmem_cache, as done with the default system kmalloc bucket. Cc: # 6.11.x Fixes: b32801d1255b ("mm/slab: Introduce kmem_buckets_create() and family") Signed-off-by: Koichiro Den Reviewed-by: Catalin Marinas Tested-by: Catalin Marinas --- Changes in v2: - Simplify by just reusing calculated aligned size stored in the default kmalloc_caches --- mm/slab_common.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index 3d26c257ed8b..db6ffe53c23e 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -380,8 +380,11 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, unsigned int usersize, void (*ctor)(void *)) { + unsigned long mask = 0; + unsigned int idx; kmem_buckets *b; - int idx; + + BUILD_BUG_ON(ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]) > BITS_PER_LONG); /* * When the separate buckets API is not built in, just return @@ -403,7 +406,7 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++) { char *short_size, *cache_name; unsigned int cache_useroffset, cache_usersize; - unsigned int size; + unsigned int size, aligned_idx; if (!kmalloc_caches[KMALLOC_NORMAL][idx]) continue; @@ -416,10 +419,6 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, if (WARN_ON(!short_size)) goto fail; - cache_name = kasprintf(GFP_KERNEL, "%s-%s", name, short_size + 1); - if (WARN_ON(!cache_name)) - goto fail; - if (useroffset >= size) { cache_useroffset = 0; cache_usersize = 0; @@ -427,18 +426,29 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, cache_useroffset = useroffset; cache_usersize = min(size - cache_useroffset, usersize); } - (*b)[idx] = kmem_cache_create_usercopy(cache_name, size, - 0, flags, cache_useroffset, - cache_usersize, ctor); - kfree(cache_name); - if (WARN_ON(!(*b)[idx])) - goto fail; + + aligned_idx = __kmalloc_index(size, false); + if (!(*b)[aligned_idx]) { + cache_name = kasprintf(GFP_KERNEL, "%s-%s", name, short_size + 1); + if (WARN_ON(!cache_name)) + goto fail; + (*b)[aligned_idx] = kmem_cache_create_usercopy(cache_name, size, + 0, flags, cache_useroffset, + cache_usersize, ctor); + if (WARN_ON(!(*b)[aligned_idx])) { + kfree(cache_name); + goto fail; + } + set_bit(aligned_idx, &mask); + } + if (idx != aligned_idx) + (*b)[idx] = (*b)[aligned_idx]; } return b; fail: - for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++) + for_each_set_bit(idx, &mask, ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL])) kmem_cache_destroy((*b)[idx]); kmem_cache_free(kmem_buckets_cache, b);