From patchwork Sun Sep 3 11:31:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mateusz Guzik X-Patchwork-Id: 13373231 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 A7905C71153 for ; Sun, 3 Sep 2023 11:31:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 957718D0022; Sun, 3 Sep 2023 07:31:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9073F8D0002; Sun, 3 Sep 2023 07:31:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F7368D0022; Sun, 3 Sep 2023 07:31:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6E2C88D0002 for ; Sun, 3 Sep 2023 07:31:39 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3C9AF160408 for ; Sun, 3 Sep 2023 11:31:39 +0000 (UTC) X-FDA: 81195071118.06.EA0E9C1 Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) by imf11.hostedemail.com (Postfix) with ESMTP id A24294000E for ; Sun, 3 Sep 2023 11:31:37 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="o/kVinOb"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of mjguzik@gmail.com designates 209.85.167.181 as permitted sender) smtp.mailfrom=mjguzik@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693740697; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=Gdbz24+Ofcek+qE5y+j7DeCzOp7bVX1dEEjejCTpHxE=; b=dbEvNK3Q5ZJOk5cwh9uzckLimNi32oPMy0J0baGnNu+nbupQCohGDy8++E2eVWe+eBxZv3 ZyoVopnaayhDeuh9lEs+QR9ohtsNrcgKPXyUKckmNwtL29N1eBCdG0VjTwfcz1MFIzMNOb Zti9Ry0SzOsbUzTIIB2w02mNHodTP4E= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="o/kVinOb"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of mjguzik@gmail.com designates 209.85.167.181 as permitted sender) smtp.mailfrom=mjguzik@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693740697; a=rsa-sha256; cv=none; b=CyKBFv/UMXNYPTrFTQGP/9RJ2akLMxx0+IcBOpfHAM4hDJmBNIz+FkffLgWCt4CDXjOuze GJGnEKM7Vxwbc9zxEvAgNTILUvSC3HQ8+Lr4vxhuCaUL+RIKbHnz8cbQwGDmyJDSp9MCSq Ih+GJVrxtR0P4cKdY3WxC9J/Up/rAr4= Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3a7d4030621so234576b6e.3 for ; Sun, 03 Sep 2023 04:31:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693740697; x=1694345497; darn=kvack.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=Gdbz24+Ofcek+qE5y+j7DeCzOp7bVX1dEEjejCTpHxE=; b=o/kVinObPTCAjrKtr3vmmte6QJ4aOm4c2sTw9WxJ2oDnX2ab4pJmY7vR7rEuvcTOIX fwHxlAbp8wEKk4wFzpj2QdKLdCr0Z4b3Q946GNSqGzaPJyICVQEHXqXi6mnu0SNhAyOR 1mewN3I6joF7efRxIKl1xJo7P5ucqaEXZIhWPtw5i3TK3nwHiivKshzqHBrT/tFRgMjK RtHEbjctvyMATwWyymccRE6sWD0DVnypXm9TmEAXQ3id0oMbEv/81qAcZtdhAaBsZQrr hz525GF2IYhb/CNkitqm82Mbx5mOVw5Bkgtw1D0wocol1ovlidxnSD8hMP+qbA5dpHmv sDYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693740697; x=1694345497; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Gdbz24+Ofcek+qE5y+j7DeCzOp7bVX1dEEjejCTpHxE=; b=E8852sPRBqGV0xyR53TfiwBfecAqN+GcRz22Rq21VCsu1gjF0Mj2lfrmxS96Cq0Uc/ he22BAt7pKjeCd57tEkMaK2SCFgHEgN2iN/6Zn59KjJ5PKSW5cUrAMV5Qkl8n0yXS+bH 92ToIsO0lDgGyK3+WEgfRXRuPO33JvPK8VFST1dbuHMd9aDS5Kb36lebi1qkB5XNgeyC 54zUneTJP6u7YwiXXHO/49EIVXj5850zHA4uBkZ+pGWJ6arGOAdZxit6isJcaglmO5Oc 1YpywIc6Jst7e+YTTkEz2evZTY9zbMycU4oDDBE5ARCDnTDqT8UoF3u0GLgKRBq89RhC QSEQ== X-Gm-Message-State: AOJu0YzfD07oU4Le6UNur4z3EZFqm6YLTPNQl2+0j4dzlQ7D9TPHaTgk s84iotRlg8l++uIfYiZ1gLAVa2IMC9Tdbj8HKGG56osSs08= X-Google-Smtp-Source: AGHT+IFiMC+Lbe3PKB9/VBZMUPT4Y01nC0G1OzOpBqw36ks+wGfAY0BYak2gGJKvPuZycEuTlpRVLfNzmkn2hAX2Juo= X-Received: by 2002:a05:6870:6125:b0:196:45b7:9385 with SMTP id s37-20020a056870612500b0019645b79385mr7453077oae.27.1693740696713; Sun, 03 Sep 2023 04:31:36 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a8a:60c:0:b0:4f0:1250:dd51 with HTTP; Sun, 3 Sep 2023 04:31:36 -0700 (PDT) From: Mateusz Guzik Date: Sun, 3 Sep 2023 13:31:36 +0200 Message-ID: Subject: Unintentional 24 byte hole in mm_struct reducing objs_per_slab To: linux-mm X-Rspam-User: X-Stat-Signature: n9fierdj3e4tyt3c7fatg7j5iw3bw81u X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A24294000E X-HE-Tag: 1693740697-971231 X-HE-Meta: U2FsdGVkX18bmKvqVgUr61q7ji3NSHp6G38dkkkh5r0hL6SdeLOxqIXdkNia8z6itHUYRU6bY9A2vpOyHAjvXc6YvVc/9lvJIML2jjyd/3nqfSwTo8YajjTSABdFvTDLIO9dNBST8NbCSpHYnM0fAJpUaVNI8o/lt5y5vDkUi1AtRREZV3iXppVHOpT2qbwfEppQ2zAjZ7s/CDxsaPX8Yj7keI2/mnAtM0tD31sV/FWZjzrzo/BLSVyd87jcprq6hfwvOPQsUsBVMwv7tb3LYoOb7t2X3muP6nlQq0SR6dbOHOyAaZnoUfPe4UTnmyYVHlSpQM6/9okIXqnNz1nMYUqRjXJJQ5YbfrVWyyCrRmUwzuQGZhoCl7m03CU+BsKcOY3rNZCIitzGim8KcnRHyTlpA5BwJ8acrZALa340hzphlJFC0Dk9F3tpXFjYRabbB4xp/7IHUWmJAbgIlh49RVEdsh5WiLw6QaHoJf9bmvprVATM+caCXe2wzhTzX/EtPzHjNX0CQ3vtOCNXf4iMBW0dLfu63IJFWorUCvn8fQHl1jcLdMIjxIUU/86PZFlUDQD9IVDLPbycmT7svm7rYOL2tXr3Mo7gYQhPe+2C/Pge9RRZvNR9MYh6Q1Njdz6wDfYv1u98Mo2e7Y2hbRNioygx0pUaOeBOeI3VUwCitflEVW+IEMeeu22GsPS/WbzbamETl58PXTMjgLYxWhtdyd3zPu+Q/vByLDoPlXuZhgZyp3i+Pgpwi6lYK4n9s0zOobgAdPmBkwc3WXI1vEqBff/AX/ivmbMhdERStJdFOdrWtbg6PUvT0aPIwDCwnN5ALJs6dtZ3w2bdwsmhdO64ajhGa/VuKxLubxn01kayNbkULQ1OZA6IpIA5vkcKTaKos71+J2UyYtaslw9uwAJ58ZtPn6rE7VXaZif+lCtJcMVb/82J4JYjDtSTDYaseMf3lQvgXLpLiI/RSu12Z+U CzL9P53x lU0i+I3H1dxhZtUtgYD1x9g9WXTj1cs0zTjKlojo+9fHHOWdQXfEzivb+FlUcevllgGD+cNM0kGoyLVC4UiQH6evnI5ULZG2fpkaVmKtJgVGmjpKjQ5xdGlvygUOYmxarV9J3oq0uVgN6zWrpkoZwQHgVHfKo5INKO0/gc81ND6Q7JTtA92iId93q0JWH6V+wP5+k3lGVoHweq6DlJrVO4L+87P0eK9yNzh7qf/dNrSQBSgyLcEDb+IsOrN+Vb9M1EUhaMIckZVHR8u1g0aQR75rjmVw0LYU5rYkRFh8OePspfSCAGprF5+RIz1JkgVs4gC9W2jaVyqUfzS7efkEPbIgrsNWJ/Coqo1us5za3fTwZRVemBqGYXKzlrG1X3RsxjsBXJ4ss/v/pvb6wy7zPKOJ6VmvfFfd0c/Np0WcAjEYKQfk= 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: The size is avoidably too big and I wrote a hack to demonstrate it does not have to be. stock: # cat /sys/kernel/slab/mm_struct/slab_size 1408 # cat /sys/kernel/slab/mm_struct/objs_per_slab 23 patched: # cat /sys/kernel/slab/mm_struct/slab_size 1344 # cat /sys/kernel/slab/mm_struct/objs_per_slab 24 At the beginning of the struct there is a field annotated with ____cacheline_aligned_in_smp (aka 64 on x86-64). Apart from making free space up to the next field, this has a side effect of forcing the size of the struct to be a multiple of 64. While normally expected, it is actively harmful for mm_struct. Allocations come from a dedicated slab: mm_size = sizeof(struct mm_struct) + cpumask_size() + mm_cid_size(); mm_cachep = kmem_cache_create_usercopy("mm_struct", mm_size, ARCH_MIN_MMSTRUCT_ALIGN, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, offsetof(struct mm_struct, saved_auxv), sizeof_field(struct mm_struct, saved_auxv), NULL); As in, the actual allocated size is bigger then sizeof(struct mm_struct) and SLAB_HWCACHE_ALIGN already provides the expected round up. Instead, the following hack: .. provides the necessary padding without inducing the sizeof problem. Now the patch looks like crap and a cheap shot failed to produce a way to do it I would be happy with. Additionally I have epsilon interest in arguing how to do it nicely. That is to say I'm reporting this for an interested party to fix in a whatever (non)hacky way they see fit. ptype /o struct mm_struct : stock: /* offset | size */ type = struct mm_struct { /* 0 | 1344 */ struct { /* 0 | 64 */ struct { /* 0 | 4 */ atomic_t mm_count; /* XXX 60-byte padding */ /* total size (bytes): 64 */ }; /* 64 | 16 */ struct maple_tree { [snip] /* 1288 | 32 */ struct { /* 1288 | 16 */ struct list_head { /* 1288 | 8 */ struct list_head *next; /* 1296 | 8 */ struct list_head *prev; /* total size (bytes): 16 */ } list; /* 1304 | 8 */ unsigned long bitmap; /* 1312 | 8 */ struct mem_cgroup *memcg; /* total size (bytes): 32 */ } lru_gen; /* XXX 24-byte padding */ /* total size (bytes): 1344 */ }; /* 1344 | 0 */ unsigned long cpu_bitmap[]; /* total size (bytes): 1344 */ } patched: /* offset | size */ type = struct mm_struct { /* 0 | 1320 */ struct { /* 0 | 4 */ struct { /* 0 | 4 */ atomic_t mm_count; /* total size (bytes): 4 */ }; /* 4 | 60 */ char __padbuf[60]; /* 64 | 16 */ struct maple_tree { [snip] /* 1288 | 32 */ struct { /* 1288 | 16 */ struct list_head { /* 1288 | 8 */ struct list_head *next; /* 1296 | 8 */ struct list_head *prev; /* total size (bytes): 16 */ } list; /* 1304 | 8 */ unsigned long bitmap; /* 1312 | 8 */ struct mem_cgroup *memcg; /* total size (bytes): 32 */ } lru_gen; /* total size (bytes): 1320 */ }; /* 1320 | 0 */ unsigned long cpu_bitmap[]; /* total size (bytes): 1320 */ } Cheers, diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 36c5b43999e6..33de9495a8d3 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -685,7 +685,8 @@ struct mm_struct { * 0, the &struct mm_struct is freed. */ atomic_t mm_count; - } ____cacheline_aligned_in_smp; + }; + char __padbuf[SMP_CACHE_BYTES - __alignof__(atomic_t)]; struct maple_tree mm_mt; #ifdef CONFIG_MMU