From mboxrd@z Thu Jan 1 00:00:00 1970 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 D1F35C30658 for ; Tue, 2 Jul 2024 19:31:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 663046B008A; Tue, 2 Jul 2024 15:31:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EC036B0095; Tue, 2 Jul 2024 15:31:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48D4B6B0096; Tue, 2 Jul 2024 15:31:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 25C6F6B008A for ; Tue, 2 Jul 2024 15:31:04 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C8CCFC052F for ; Tue, 2 Jul 2024 19:31:03 +0000 (UTC) X-FDA: 82295805606.22.87D03B0 Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by imf19.hostedemail.com (Postfix) with ESMTP id 84CFF1A0023 for ; Tue, 2 Jul 2024 19:31:00 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Q1FEnjiQ; spf=pass (imf19.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719948649; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=J0SU+FVYux8dBxFA9n9nO3CUHybdv+MQ9BO4fGoXttc=; b=dJv670MmZzenY+ivEdrQUX442XvpzYaorTe43j29AWHuatycHjcHQFgWKDhH+L7iGLDbN6 KTwZajynEPxeRlEOf8dICvK7F/4ftjbpjgxmXml7DkMpoRr3RLxTUuTlepOfiOa/Q+ZFz+ ijlxPe+rvHPNIT5iXwq9vontj1iicIE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Q1FEnjiQ; spf=pass (imf19.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719948649; a=rsa-sha256; cv=none; b=Eiiag3YyoSJI0L86GkzTgm5uUwokvv7BHSmbIzaVjMzPm7mIGCHejwx0LWN7vQiUgauwbK l3c9AN+cQwFr/HJoqeFHGziT4hAKHp3B6+K5uuxFBSKoP418+VE/f+TdjqOLPRchjcNX+x XUyCM4RKQYtePVbh3IW3vt09M9MfuH0= X-Envelope-To: vbabka@suse.cz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1719948658; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=J0SU+FVYux8dBxFA9n9nO3CUHybdv+MQ9BO4fGoXttc=; b=Q1FEnjiQv7ICJdNXiNOvRIFyl5PIS1nWD0FR4+Ix0eD4pchJvlXx0E7f6w/9SzdkcZolPL wzwFQDwqxDoLZsuqWPJqMRt0iRYG7+5QuHv2rh3KcXJ3NPnYkPj+E4veQzUr4sLl3+ZDLn IESUTdLYgogZjOcMNea13n4RYil/wt8= X-Envelope-To: linux-mm@kvack.org X-Envelope-To: rientjes@google.com X-Envelope-To: cl@linux.com X-Envelope-To: 42.hyeyoo@gmail.com X-Envelope-To: keescook@chromium.org X-Envelope-To: aliceryhl@google.com X-Envelope-To: boqun.feng@gmail.com X-Envelope-To: rust-for-linux@vger.kernel.org X-Envelope-To: linux-kernel@vger.kernel.org X-Envelope-To: patches@lists.linux.dev Date: Tue, 2 Jul 2024 19:30:53 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Vlastimil Babka Cc: linux-mm@kvack.org, David Rientjes , Christoph Lameter , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Kees Cook , Alice Ryhl , Boqun Feng , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH] mm, slab: extend kmalloc() alignment for non power-of-two sizes Message-ID: References: <20240702155800.166503-2-vbabka@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240702155800.166503-2-vbabka@suse.cz> X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 84CFF1A0023 X-Stat-Signature: d8p5ykkap9f9n99tphghgq617mszkpns X-HE-Tag: 1719948660-806195 X-HE-Meta: U2FsdGVkX1+adz904uT4I5DW3950D02uXLo34YBs/engQHHLn+GP50tGj5u8MLeA6y4ZQ6DW+62HbUSbGS4ga18na5VqMRFpilH4GFfm29sb8YGay3P0KLYrdCHJYvIiaftNaq7iqJdivy+y4AICvNJIuiU+qrNcPgFClCZLR2c3KREBPrlWMcwJJ+aqdLPb0IStjrS0XlWMYUfq5WbyqllxnrS6BqMcGgfJZzKVxLuELUGi5Q7B9vkdaqMN/wsPSd7sMNY4G+29mmCfms76TwD8j0aeD0iEmvTzrm5AUCOnWQvA70grubHnITUDjTbDil05EBgSfOVL1yqBIBjBgDMARSPD1jYO+m4uFRFtkCbPEIwzO0xmBpGfO9DApwnwLr1AMenM7BQY1MZVCwaFAkma5C69wCc3xW4EWInHREqKlAykIHt2Xjkm0Wxi6Rq5gWJn9SWHx+ocrF8Cp2SqujVBt8kda8JKf4dpXUbFqgut+LZGHKYhE6QcSZs+TTVduevr7EGlkeV+ndCPou/gG8NGiCu31r9o3nEOL18Bc1yoJKyZLLb2ntzdDJHV5/TZv7E2CVMHRc+xx0fIfD+yhdAdUUbAMZX4LRRd4t2YuhMO+xxf7ClG/rLarZtVspF1o9qtLejfVwxtpsU0LhFvzdYuh/BupWNW2JiWALC3DWPxL8+B8Ra0fFYDaL24cxqe3kuUajXBXbAc2OgHFOHzQxGH0H+ijKdjbm8+iY/rfDVB07YKFTjlLfyA/R5wVKTvoWUl7cw4bDFFtSfvN5/1N2B125jfUtFF8LM9KXGvq1rP3HtmRduFpc/PkY9fGtLguy4Q/qiecYRAvj7lq9iJKfZqoA7u9MBNtQ6KSMj5xfgQz9qzjuLlr3qhzVaPiBkECoeIzu1q0uFiR0RzObR0iZWGzgsLDV5++nvwzPRuwrTlP92rSAHtk0UzDNDhBOvdoev+1ubbSL7CKUTrBRJ 5uINafra v1wRT8TjwzLB8GIpSmD6USNmTo2j+N6PGWdhJDmsAxMmtW2a3e481h9RIUO2b2U837OenHbUQtkYJBDLw4jvEuTERuSdqD8Z1Xr/kbPH9ygt7OSl+id7HolvzxNxC2TY/L3ge8K/EUt6ncrWLla5wogEgZ6aGh0e8oas40LEe7Zwu9QLELAjJme33KOV54jDJW7xFq0M/x/bYxHH07i/rWamKRTmXzJRWr9GKoyEIQNA4kgXMpA/o8r+SqNQQVNRSoxfY1iqF4ndmIxps7cGmoxJA448dsUMXyUrOiG/bmHWx8YduOqyHahuNLQ== 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: List-Subscribe: List-Unsubscribe: On Tue, Jul 02, 2024 at 05:58:01PM +0200, Vlastimil Babka wrote: > Slab allocators have been guaranteeing natural alignment for > power-of-two sizes since commit 59bb47985c1d ("mm, sl[aou]b: guarantee > natural alignment for kmalloc(power-of-two)"), while any other sizes are > aligned only to ARCH_KMALLOC_MINALIGN bytes. > > Rust's allocator API specifies size and alignment per allocation, which > have to satisfy the following rules, per Alice Ryhl [1]: > > 1. The alignment is a power of two. > 2. The size is non-zero. > 3. When you round up the size to the next multiple of the alignment, > then it must not overflow the signed type isize / ssize_t. > > In order to map this to kmalloc()'s guarantees, some requested > allocation sizes have to be enlarged to the next power-of-two size [2]. > For example, an allocation of size 96 and alignment of 32 will be > enlarged to an allocation of size 128, because the existing kmalloc-96 > bucket doesn't guarantee alignent above ARCH_KMALLOC_MINALIGN. Without > slab debugging active, the layout of the kmalloc-96 slabs however > naturally aligns the objects to 32 bytes, so extending the size to 128 > bytes is wasteful. > > To improve the situation we can extend the kmalloc() alignment > guarantees in a way that > > 1) doesn't change the current slab layout (and thus does not increase > internal fragmentation) when slab debugging is not active > 2) reduces waste in the Rust allocator use case > 3) is a superset of the current guarantee for power-of-two sizes. > > The extended guarantee is that alignment is at least the largest > power-of-two divisor of the requested size. For power-of-two sizes the > largest divisor is the size itself, but let's keep this case documented > separately for clarity. > > For current kmalloc size buckets, it means kmalloc-96 will guarantee > alignment of 32 bytes and kmalloc-196 will guarantee 64 bytes. > > This covers the rules 1 and 2 above of Rust's API as long as the size is > a multiple of the alignment. The Rust layer should now only need to > round up the size to the next multiple if it isn't, while enforcing the > rule 3. > > Implementation-wise, this changes the alignment calculation in > create_boot_cache(). While at it also do the calulation only for caches > with the SLAB_KMALLOC flag, because the function is also used to create > the initial kmem_cache and kmem_cache_node caches, where no alignment > guarantee is necessary. > > Link: https://lore.kernel.org/all/CAH5fLggjrbdUuT-H-5vbQfMazjRDpp2%2Bk3%3DYhPyS17ezEqxwcw@mail.gmail.com/ [1] > Link: https://lore.kernel.org/all/CAH5fLghsZRemYUwVvhk77o6y1foqnCeDzW4WZv6ScEWna2+_jw@mail.gmail.com/ [2] > Signed-off-by: Vlastimil Babka Hello Vlastimil, the idea and the implementation makes total sense to me. Do you have an estimate for the memory overhead it will typically introduce? I don't think it will be too large though and actually can be compensated by potential performance gains due to a better memory alignment. What do you think? Thanks!