From patchwork Mon Aug 26 09:23:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13777466 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 9B22EC5321E for ; Mon, 26 Aug 2024 09:24:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14A086B049A; Mon, 26 Aug 2024 05:24:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0FA296B049B; Mon, 26 Aug 2024 05:24:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F2B116B049C; Mon, 26 Aug 2024 05:24:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D50AD6B049A for ; Mon, 26 Aug 2024 05:24:08 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5CCBB140E55 for ; Mon, 26 Aug 2024 09:24:08 +0000 (UTC) X-FDA: 82493860176.29.FD7DEF0 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf08.hostedemail.com (Postfix) with ESMTP id C4210160007 for ; Mon, 26 Aug 2024 09:24:06 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Qfz/8P+7"; spf=pass (imf08.hostedemail.com: domain of brauner@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=brauner@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724664161; 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=+mQb8S0/XzqcB2VYcaWGRKwP81OD7RYivkLgoljRBFc=; b=ghU9MnjbgTbHI2XSasmbA5hS3DO3IhnHlvH2AMswp4UddGoUyxXNOQ/tk8Dz/o0YZsU2oQ X3x9A0FKHzwFwa+TQUElWkn4QIe36VMdMKE0X/wtE5TQqtqeHQH3WDBHqoAsgUd4HjHijH OIJ4thmx3136MCrlmEjjF3o1b0S5/OA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724664161; a=rsa-sha256; cv=none; b=yXzRQpDwO27Pclw0DXqz2jBqDui+yKpQeqN3O6Dftaq35s9m0YBI8KAEpty1TCq45rkTS/ VAVsQuyqiDUnhkGXai7Yo1qPLsZ+mZ6BM61LHoevSzvyzfn6jCTQzNt+bFyz957Hovvy2f GAdeHdYn1UuV/5L4Ep4hB+9KHGFHxNw= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Qfz/8P+7"; spf=pass (imf08.hostedemail.com: domain of brauner@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=brauner@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 25887A41FDD; Mon, 26 Aug 2024 09:23:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0807DC58110; Mon, 26 Aug 2024 09:24:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724664245; bh=fkYKsC7/wC58unCs2WZDpheCT4ZpD+FVODWXgDaEHLA=; h=From:To:Cc:Subject:Date:From; b=Qfz/8P+78G6BjTWFg0X3pL7K4dCRw/5tGbBDhEkBuoGEmtQce45TlTr5BC3rvMiqI ckPK/ciFtmxzL2va2zJt+WFTYaszH4S5CoeUHofJ/OYVrv4ndOZfvMxczSlL8KSBcr HInuQbZTiLVex4NHN5hOrsI32ywKTexrjtz/4c3Lopeg2c5XzEy/AG/QAewsa5WyRH H5JQ2k3HLz5JT7HI5xUpKJuTbkQbD7sM0PwrUFVU/h9IaTIB0iifSskdW6rK84Zs0n oQfyCtLvXWZGcSZTPrizNQ7CfSZQB6UfwWzTRwOHImtrtR6ZYoFXgoZg5ExYepVeCJ 0OdBRXt+4IpkQ== From: Christian Brauner To: Vlastimil Babka , linux-mm@kvack.org Cc: Christian Brauner , Jens Axboe , "Paul E. McKenney" , Roman Gushchin , Linus Torvalds Subject: [PATCH] [POC/RFE]: Avoid silently growing struct file due to SLAB_TYPESAFE_BY_RCU Date: Mon, 26 Aug 2024 11:23:40 +0200 Message-ID: <20240826-benahm-sorgenfrei-245a41a9acb4@brauner> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2351; i=brauner@kernel.org; h=from:subject:message-id; bh=fkYKsC7/wC58unCs2WZDpheCT4ZpD+FVODWXgDaEHLA=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaSd8Vx9frrL95YfS/kcn7bM8VjV93lfRb77EufeJRn3d lvz8sw/3FHKwiDGxSArpsji0G4SLrecp2KzUaYGzBxWJpAhDFycAjARxzeMDF+qjmwskJ4cWZt7 zDbuzmPZ5nOFDvJ6N812LWOa6H8pPpThv5fXjpoPdmpWCVsXBC06XK/B9Vb9gdGxqHvLApO3hyt 94gcA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 X-Stat-Signature: 1e5qtu5pbwhopd8fmguc6xir4hamyeex X-Rspamd-Queue-Id: C4210160007 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724664246-915238 X-HE-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XbqtBbMYnN7uDxA6so84liILYVViCyUhOwEg6BCR+2mDva6+4RtSpJZ/bbJ7UsNE/EhFFxmnBs9pDmGYARK+I2L3mNvTUeoXHNbSlG+1Jd7p2RiuO+9ySiHPU7HWETXc1MrOMyxOX5jWcu/Ht9bI0MV74hevfJHiLqP/4yV3+2P1rJK3ZGAr+STnyrRFQeVR/unPoOupvEBtDcTLkBBoaegOALh4lCzedVt+CTpCYmmPFUVcTlr3Zv0qHEoAOSWhojw== 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: I've managed to shrink struct file to 192 bytes aka three cachelines. What I wasn't aware of is that SLAB_TYPESAFE_BY_RCU silently grows the struct by another cacheline to accomodate the freelist pointer as pointed out by Vlastimil. It's not too bad because we mostly care about "runtime" size but it's still unfortunate that we're wasting a whole additional cacheline for no good reason. So I would like to find a way to avoid that. Below you see a hack because adding a parameter to kmem_cache_create() seemed to require me to fiddle with mm/slub.c and I'd rather not unless I'm told how. For the POC I'm using the offset into the union at the end of struct file so that SLUB places the freelist pointer into the union during kmem_cache_free(). I'm just looking for ideas from mm to figure out an API where we can say "use that as the freelist pointer in the struct" or at least a reason why that's just not possible. Sketched-by: Christian Brauner --- fs/file_table.c | 3 ++- include/linux/fs.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/fs/file_table.c b/fs/file_table.c index 694199a1a966..7a5fac7d4bbd 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -514,7 +514,8 @@ EXPORT_SYMBOL(__fput_sync); void __init files_init(void) { - filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0, + filp_cachep = kmem_cache_create("filp", + offsetof(struct file, __f_slab_rcu), 0, SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, NULL); percpu_counter_init(&nr_files, 0, GFP_KERNEL); diff --git a/include/linux/fs.h b/include/linux/fs.h index 36bb8456eb38..6c3664d624a0 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1010,6 +1010,7 @@ static inline int ra_has_index(struct file_ra_state *ra, pgoff_t index) * @f_task_work: task work entry point * @f_llist: work queue entrypoint * @f_ra: file's readahead state + * @f_slab_rcu: freelist pointer for SLAB_TYPESAFE_BY_RCU */ struct file { atomic_long_t f_count; @@ -1041,6 +1042,7 @@ struct file { struct callback_head f_task_work; struct llist_node f_llist; struct file_ra_state f_ra; + void *__f_slab_rcu; }; /* --- cacheline 3 boundary (192 bytes) --- */ } __randomize_layout