From patchwork Fri Mar 1 17:07:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13578807 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 E4D14C5478C for ; Fri, 1 Mar 2024 17:07:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75C37940007; Fri, 1 Mar 2024 12:07:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 70B7D6B009C; Fri, 1 Mar 2024 12:07:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55E99940007; Fri, 1 Mar 2024 12:07:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 41C3E6B009B for ; Fri, 1 Mar 2024 12:07:15 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1A722160408 for ; Fri, 1 Mar 2024 17:07:15 +0000 (UTC) X-FDA: 81849100830.08.F48DBCA Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf01.hostedemail.com (Postfix) with ESMTP id 8AE7340028 for ; Fri, 1 Mar 2024 17:07:12 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=hR4RI3M7; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=o05E46K6; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=hR4RI3M7; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=o05E46K6; dmarc=none; spf=pass (imf01.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709312832; a=rsa-sha256; cv=none; b=rSzDKn5TOTOhuPsS6zeYnBYdGSmhi2PJFnUUb84Cslp9nyyevxwzsKYxsNUBZJpU346+6m jfeg/QXdnr/Nu1UKClliGDvjxTCUZZv4zsVnU6hPoDIsqZGLMN3Xx26itokXFTfd0mmyx4 EFXPM2qAubCBm+m3HexhnHzaqAFYp80= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=hR4RI3M7; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=o05E46K6; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=hR4RI3M7; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=o05E46K6; dmarc=none; spf=pass (imf01.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709312832; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3OiKnePSvssqSsATKZD6FgDSrsGbw6Gz9LH7uaXGsMo=; b=usRWIv3c93sDwlOgNinCbh+08rVUPt6S3qYeEvBFSJds5HL2+fZRM5ugwLrAu73bBdThp4 Cf1mLfdCMrJXxO87imoLK8l+gfW/0s8wAB5BPr1S7WPlpUAIUTpX8KF6rCul+LvY18+bmt CNTp7XGj8LGnA88yQEwFPMCf9v6ungc= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 13FDA207B4; Fri, 1 Mar 2024 17:07:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1709312831; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3OiKnePSvssqSsATKZD6FgDSrsGbw6Gz9LH7uaXGsMo=; b=hR4RI3M7QdmI77qwLuZOBimpV0p2ze0qGZw1B4hIMLU2F/D7t3Gu5VbTAR9gTJgDi6vCeY CHs1xCbyeczWk9Hb3kbBfU7sfuhKG4jF0YdapV00Mtliv7GsO2UuM0v+bywHfHlN1udd4J we+zKQVacDSXAT9F9S8KKuGQXrqXA18= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1709312831; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3OiKnePSvssqSsATKZD6FgDSrsGbw6Gz9LH7uaXGsMo=; b=o05E46K6EN+fy853AkUBuf6HrQD5ZThoOdIKf4tjmcYR9J8tcaU11fvMi6vu+GSL77uZkk Y/9da7wAoHpaIGBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1709312831; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3OiKnePSvssqSsATKZD6FgDSrsGbw6Gz9LH7uaXGsMo=; b=hR4RI3M7QdmI77qwLuZOBimpV0p2ze0qGZw1B4hIMLU2F/D7t3Gu5VbTAR9gTJgDi6vCeY CHs1xCbyeczWk9Hb3kbBfU7sfuhKG4jF0YdapV00Mtliv7GsO2UuM0v+bywHfHlN1udd4J we+zKQVacDSXAT9F9S8KKuGQXrqXA18= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1709312831; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3OiKnePSvssqSsATKZD6FgDSrsGbw6Gz9LH7uaXGsMo=; b=o05E46K6EN+fy853AkUBuf6HrQD5ZThoOdIKf4tjmcYR9J8tcaU11fvMi6vu+GSL77uZkk Y/9da7wAoHpaIGBA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id E618113AA3; Fri, 1 Mar 2024 17:07:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id MKjgNz4L4mUcGQAAD6G6ig (envelope-from ); Fri, 01 Mar 2024 17:07:10 +0000 From: Vlastimil Babka Date: Fri, 01 Mar 2024 18:07:11 +0100 Subject: [PATCH RFC 4/4] UNFINISHED mm, fs: use kmem_cache_charge() in path_openat() MIME-Version: 1.0 Message-Id: <20240301-slab-memcg-v1-4-359328a46596@suse.cz> References: <20240301-slab-memcg-v1-0-359328a46596@suse.cz> In-Reply-To: <20240301-slab-memcg-v1-0-359328a46596@suse.cz> To: Linus Torvalds , Josh Poimboeuf , Jeff Layton , Chuck Lever , Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Alexander Viro , Christian Brauner , Jan Kara Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, Vlastimil Babka X-Mailer: b4 0.13.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 8AE7340028 X-Stat-Signature: fpnwrmxc9ofid818n3mwtftzjayzhkd5 X-HE-Tag: 1709312832-241885 X-HE-Meta: U2FsdGVkX1/HNkatZm3V+cD+tWJKgDCKIH9rQv56RIWZJJJ9SWpmBf42Y4JHIH9w49GTDcOVbEJsQH5reG1ZvLwcWGO/zhtyzzrZ4diYAQKzGD6IC2n8s+DXw10xxlJp/X6G7Jrrl5q/CBPeiT+OyYbf9vIibRgKX46GPK3h/Gr2wVd3BCQLQH3+CHRVL/L1k1NJZ103sGVXd2xbinvtUcFYuDKVf/IxcrTZLmEwvmLo+WqcH1A8TChap8y94PHpGwmdT+pYbMSTZqWFlyfupNvLG45TJTqamP0+oUXNOiB6qOUB2YPPzzxstgWN06yYMRvsdEsftGkrQVIpRWH+xzUUhvIrlpbaLDb6Rp6sE8aZ3X7tmScfwSlOu6LMfvDdbKmoqoyO8lqeHmKjhiYlL9zvCxdngsyKGxU49t0SoBCMA+SmlPtqejq5xtqxAXw28iBE+INsB2RcMKKuDKZ3kTUnWgK6yNa4Td0i0unKgf0HosLvd3iDLhOngz9cCaRhBZGp+GItrUSbhueSBlJj8aGOilCe5qXijHl1IpyYwYH/vKSUS6HSEbrHgl9sdR8wdhPUB+E34PxoHu827fzF5EChilPubSPPduGjMv2i3tmSP7s12qErh1l8rdUYBd7ApAyr2NULlfQN+xhWNaMVq7NK0LRZGj8oR2ngxRgnnxEiFbRMPt/wznmPYsJdQa4gSfSxUANe4EbdSGslXAMd9qryNtMshQEoT1T8dnnHFyzaP84gfoW/naVNFatjbplnRwPeXaSNZphu5zLfdYPc+JqLGZ6WeyULBiNfzxYGqInD9ZMxYorGxIT7qA44oylsgOXLMlJ0i4ZvSbE+q5DAnFIzy6Uzu3tkCSIgDfvENyceeZF2SG1oMXnZy6BMSMLoqzl7bFrwrTAUfH8eA8A2Bk8/0/zIvi11/EO/qHfvgkBM+dk3HmF/wqw9UjyQBd6jJ14wZqIbWj9BbWvrvl2 o8rZK121 fbglOJbD2rx/YvJiHS3hFWAFTDLKShhl9jn8itL/Z5nx1Yf+AcKkx/0kD850aYhQj8wAjrTbJFg06U8oOMsj4TT453kaF4zP4D2Chj1oqGwfZTcilJJmd+A/J70PbWd7JW+WJA16FV+MjWPDHeQcW4oVcmHrqisRALiMzSK9yVyGWP4Bm2/bES2GsnWq45TwXEZ3XAFUfENDD7/c+HsYVbggsG8ZLLh+EgxGio+f4tfrOD+887pYlE5Geq3EGMSJFwynxyJkoFOUnQPEWYugL0X46ObvC9d3jvxH52TCpVUVa/iO6xL7yvIYslqTKb9biBHRSINaN5WD5eRSudVtE3xHeXhT9PaqBPdf4XHExWy4kcmmjedwzn2iLkCXqQ6hxfhy+cY/c2Zt26lfrF43fQHbVkUddtVF4Eny4r7kWpai/4JM= 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: This is just an example of using the kmem_cache_charge() API. I think it's placed in a place that's applicable for Linus's example [1] although he mentions do_dentry_open() - I have followed from strace() showing openat(2) to path_openat() doing the alloc_empty_file(). The idea is that filp_cachep stops being SLAB_ACCOUNT. Allocations that want to be accounted immediately can use GFP_KERNEL_ACCOUNT. I did that in alloc_empty_file_noaccount() (despite the contradictory name but the noaccount refers to something else, right?) as IIUC it's about kernel-internal opens. alloc_empty_file() is now not doing the accounting, so I added kmem_account_file() that calls the new kmem_cache_charge() API. Why is this unfinished: - there are other callers of alloc_empty_file() which I didn't adjust so they simply became memcg-unaccounted. I haven't investigated for which ones it would make also sense to separate the allocation and accounting. Maybe alloc_empty_file() would need to get a parameter to control this. - I don't know how to properly unwind the accounting failure case. It seems like a new case because when we succeed the open, there's no further error path at least in path_openat(). Basically it boils down I'm unfamiliar with VFS so this depends if this approach is deemed useful enough to finish it. [1] https://lore.kernel.org/all/CAHk-=whYOOdM7jWy5jdrAm8LxcgCMFyk2bt8fYYvZzM4U-zAQA@mail.gmail.com/ Not-signed-off-by: Vlastimil Babka --- fs/file_table.c | 9 +++++++-- fs/internal.h | 1 + fs/namei.c | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/fs/file_table.c b/fs/file_table.c index b991f90571b4..6401b6f175ae 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -223,6 +223,11 @@ struct file *alloc_empty_file(int flags, const struct cred *cred) return ERR_PTR(-ENFILE); } +int kmem_account_file(struct file *f) +{ + return kmem_cache_charge(filp_cachep, GFP_KERNEL_ACCOUNT, f); +} + /* * Variant of alloc_empty_file() that doesn't check and modify nr_files. * @@ -234,7 +239,7 @@ struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred) struct file *f; int error; - f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL); + f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL_ACCOUNT); if (unlikely(!f)) return ERR_PTR(-ENOMEM); @@ -468,7 +473,7 @@ void __init files_init(void) { filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0, SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN | - SLAB_PANIC | SLAB_ACCOUNT, NULL); + SLAB_PANIC, NULL); percpu_counter_init(&nr_files, 0, GFP_KERNEL); } diff --git a/fs/internal.h b/fs/internal.h index b67406435fc0..06ada11b71d0 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -96,6 +96,7 @@ extern void chroot_fs_refs(const struct path *, const struct path *); struct file *alloc_empty_file(int flags, const struct cred *cred); struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred); struct file *alloc_empty_backing_file(int flags, const struct cred *cred); +int kmem_account_file(struct file *file); static inline void file_put_write_access(struct file *file) { diff --git a/fs/namei.c b/fs/namei.c index 4e0de939fea1..fcf3f3fcd059 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3799,8 +3799,10 @@ static struct file *path_openat(struct nameidata *nd, terminate_walk(nd); } if (likely(!error)) { - if (likely(file->f_mode & FMODE_OPENED)) + if (likely(file->f_mode & FMODE_OPENED)) { + kmem_account_file(file); return file; + } WARN_ON(1); error = -EINVAL; }