* Re: [PATCH] fscrypt: Copy the memcg information to the ciphertext page [not found] ` <Y9a2m8uvmXmCVYvE-8Kz7deQLoS0mYvmMESoHnA@public.gmane.org> @ 2023-01-29 21:26 ` Matthew Wilcox [not found] ` <Y9bkoasmAmtQ2nSV-FZi0V3Vbi30CUdFEqe4BF2D2FQJk+8+b@public.gmane.org> 0 siblings, 1 reply; 5+ messages in thread From: Matthew Wilcox @ 2023-01-29 21:26 UTC (permalink / raw) To: Eric Biggers Cc: Theodore Y . Ts'o, Jaegeuk Kim, linux-fscrypt-u79uwXL29TY76Z2rM5mHXA, linux-fsdevel-u79uwXL29TY76Z2rM5mHXA, linux-ext4-u79uwXL29TY76Z2rM5mHXA, linux-f2fs-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f, stable-u79uwXL29TY76Z2rM5mHXA, cgroups-u79uwXL29TY76Z2rM5mHXA, Tejun Heo On Sun, Jan 29, 2023 at 10:10:35AM -0800, Eric Biggers wrote: > On Sun, Jan 29, 2023 at 12:18:51PM +0000, Matthew Wilcox (Oracle) wrote: > > Both f2fs and ext4 end up passing the ciphertext page to > > wbc_account_cgroup_owner(). At the moment, the ciphertext page appears > > to belong to no cgroup, so it is accounted to the root_mem_cgroup instead > > of whatever cgroup the original page was in. > > > > It's hard to say how far back this is a bug. The crypto code shared > > between ext4 & f2fs was created in May 2015 with commit 0b81d0779072, > > but neither filesystem did anything with memcg_data before then. memcg > > writeback accounting was added to ext4 in July 2015 in commit 001e4a8775f6 > > and it wasn't added to f2fs until January 2018 (commit 578c647879f7). > > What is the actual effect of this bug? > > The bounce pages are short-lived, so surely it doesn't really matter what memory > cgroup they get charged to? Ah, we don't want to charge the _memory_ of the bounce pages to the cgroup. We want to charge the _I/O_ to the cgroup. Looking at the original commits, the effect will be that if you have an unencrypted filesystem, writeback will be throttled according to the cgroup's rules, but if you have an encrypted filesystem, it will escape the cgroup I/O limits. > I guess it's really more about the effect on cgroup writeback? And that's also > the reason why this is a problem here but not e.g. in dm-crypt? I haven't looked at dm-crypt at all, but my assumption is that the fs charges the I/O of the pagecache page to the cgroup, and there's no need to do it again. > > diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c > > index e78be66bbf01..a4e76f96f291 100644 > > --- a/fs/crypto/crypto.c > > +++ b/fs/crypto/crypto.c > > @@ -205,6 +205,9 @@ struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, > > } > > SetPagePrivate(ciphertext_page); > > set_page_private(ciphertext_page, (unsigned long)page); > > +#ifdef CONFIG_MEMCG > > + ciphertext_page->memcg_data = page->memcg_data; > > +#endif > > return ciphertext_page; > > } > > Nothing outside mm/ and include/linux/memcontrol.h does anything with memcg_data > directly. Are you sure this is the right thing to do here? Nope ;-) Happy to hear from people who know more about cgroups than I do. Adding some more ccs. > Also, this patch causes the following: Oops. Clearly memcg_data needs to be set to NULL before we free it. ^ permalink raw reply [flat|nested] 5+ messages in thread
[parent not found: <Y9bkoasmAmtQ2nSV-FZi0V3Vbi30CUdFEqe4BF2D2FQJk+8+b@public.gmane.org>]
* Re: [PATCH] fscrypt: Copy the memcg information to the ciphertext page [not found] ` <Y9bkoasmAmtQ2nSV-FZi0V3Vbi30CUdFEqe4BF2D2FQJk+8+b@public.gmane.org> @ 2023-01-31 21:27 ` Tejun Heo [not found] ` <Y9mH0PCcZoGPryXw-NiLfg/pYEd1N0TnZuCh8vA@public.gmane.org> 0 siblings, 1 reply; 5+ messages in thread From: Tejun Heo @ 2023-01-31 21:27 UTC (permalink / raw) To: Matthew Wilcox Cc: Eric Biggers, Theodore Y . Ts'o, Jaegeuk Kim, linux-fscrypt-u79uwXL29TY76Z2rM5mHXA, linux-fsdevel-u79uwXL29TY76Z2rM5mHXA, linux-ext4-u79uwXL29TY76Z2rM5mHXA, linux-f2fs-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f, stable-u79uwXL29TY76Z2rM5mHXA, cgroups-u79uwXL29TY76Z2rM5mHXA Hello, On Sun, Jan 29, 2023 at 09:26:57PM +0000, Matthew Wilcox wrote: > > > diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c > > > index e78be66bbf01..a4e76f96f291 100644 > > > --- a/fs/crypto/crypto.c > > > +++ b/fs/crypto/crypto.c > > > @@ -205,6 +205,9 @@ struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, > > > } > > > SetPagePrivate(ciphertext_page); > > > set_page_private(ciphertext_page, (unsigned long)page); > > > +#ifdef CONFIG_MEMCG > > > + ciphertext_page->memcg_data = page->memcg_data; > > > +#endif > > > return ciphertext_page; > > > } > > > > Nothing outside mm/ and include/linux/memcontrol.h does anything with memcg_data > > directly. Are you sure this is the right thing to do here? > > Nope ;-) Happy to hear from people who know more about cgroups than I > do. Adding some more ccs. > > > Also, this patch causes the following: > > Oops. Clearly memcg_data needs to be set to NULL before we free it. These can usually be handled by explicitly associating the bio's to the desired cgroups using one of bio_associate_blkg*() or bio_clone_blkg_association(). It is possible to go through memcg ownership too using set_active_memcg() so that the page is owned by the target cgroup; however, the page ownership doesn't directly map to IO ownership as the relationship depends on the type of the page (e.g. IO ownership for pagecache writeback is determined per-inode, not per-page). If the in-flight pages are limited, it probably is better to set bio association directly. Thanks. -- tejun ^ permalink raw reply [flat|nested] 5+ messages in thread
[parent not found: <Y9mH0PCcZoGPryXw-NiLfg/pYEd1N0TnZuCh8vA@public.gmane.org>]
* Re: [PATCH] fscrypt: Copy the memcg information to the ciphertext page [not found] ` <Y9mH0PCcZoGPryXw-NiLfg/pYEd1N0TnZuCh8vA@public.gmane.org> @ 2023-02-01 6:31 ` Eric Biggers 2023-02-02 21:30 ` Tejun Heo 0 siblings, 1 reply; 5+ messages in thread From: Eric Biggers @ 2023-02-01 6:31 UTC (permalink / raw) To: Tejun Heo Cc: Matthew Wilcox, Theodore Y . Ts'o, Jaegeuk Kim, linux-fscrypt-u79uwXL29TY76Z2rM5mHXA, linux-fsdevel-u79uwXL29TY76Z2rM5mHXA, linux-ext4-u79uwXL29TY76Z2rM5mHXA, linux-f2fs-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f, stable-u79uwXL29TY76Z2rM5mHXA, cgroups-u79uwXL29TY76Z2rM5mHXA On Tue, Jan 31, 2023 at 11:27:44AM -1000, Tejun Heo wrote: > Hello, > > On Sun, Jan 29, 2023 at 09:26:57PM +0000, Matthew Wilcox wrote: > > > > diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c > > > > index e78be66bbf01..a4e76f96f291 100644 > > > > --- a/fs/crypto/crypto.c > > > > +++ b/fs/crypto/crypto.c > > > > @@ -205,6 +205,9 @@ struct page *fscrypt_encrypt_pagecache_blocks(struct page *page, > > > > } > > > > SetPagePrivate(ciphertext_page); > > > > set_page_private(ciphertext_page, (unsigned long)page); > > > > +#ifdef CONFIG_MEMCG > > > > + ciphertext_page->memcg_data = page->memcg_data; > > > > +#endif > > > > return ciphertext_page; > > > > } > > > > > > Nothing outside mm/ and include/linux/memcontrol.h does anything with memcg_data > > > directly. Are you sure this is the right thing to do here? > > > > Nope ;-) Happy to hear from people who know more about cgroups than I > > do. Adding some more ccs. > > > > > Also, this patch causes the following: > > > > Oops. Clearly memcg_data needs to be set to NULL before we free it. > > These can usually be handled by explicitly associating the bio's to the > desired cgroups using one of bio_associate_blkg*() or > bio_clone_blkg_association(). Here that already happens in wbc_init_bio(), called from io_submit_init_bio() in fs/ext4/page-io.c. > It is possible to go through memcg ownership > too using set_active_memcg() so that the page is owned by the target cgroup; > however, the page ownership doesn't directly map to IO ownership as the > relationship depends on the type of the page (e.g. IO ownership for > pagecache writeback is determined per-inode, not per-page). If the in-flight > pages are limited, it probably is better to set bio association directly. ext4 also calls wbc_account_cgroup_owner() for each pagecache page that's written out. It seems this is for a different purpose -- it looks like the fs-writeback code is trying to figure out which cgroup "owns" the inode based on which cgroup "owns" most of the pagecache pages? The bug we're discussing here is that when ext4 writes out a pagecache page in an encrypted file, it first encrypts the data into a bounce page, then passes the bounce page (which don't have a memcg) to wbc_account_cgroup_owner(). Maybe the proper fix is to just pass the pagecache page to wbc_account_cgroup_owner() instead? See below for ext4 (a separate patch would be needed for f2fs): diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c index beaec6d81074a..1e4db96a04e63 100644 --- a/fs/ext4/page-io.c +++ b/fs/ext4/page-io.c @@ -409,7 +409,8 @@ static void io_submit_init_bio(struct ext4_io_submit *io, static void io_submit_add_bh(struct ext4_io_submit *io, struct inode *inode, - struct page *page, + struct page *pagecache_page, + struct page *bounce_page, struct buffer_head *bh) { int ret; @@ -421,10 +422,11 @@ static void io_submit_add_bh(struct ext4_io_submit *io, } if (io->io_bio == NULL) io_submit_init_bio(io, bh); - ret = bio_add_page(io->io_bio, page, bh->b_size, bh_offset(bh)); + ret = bio_add_page(io->io_bio, bounce_page ?: pagecache_page, + bh->b_size, bh_offset(bh)); if (ret != bh->b_size) goto submit_and_retry; - wbc_account_cgroup_owner(io->io_wbc, page, bh->b_size); + wbc_account_cgroup_owner(io->io_wbc, pagecache_page, bh->b_size); io->io_next_block++; } @@ -561,8 +563,7 @@ int ext4_bio_write_page(struct ext4_io_submit *io, do { if (!buffer_async_write(bh)) continue; - io_submit_add_bh(io, inode, - bounce_page ? bounce_page : page, bh); + io_submit_add_bh(io, inode, page, bounce_page, bh); } while ((bh = bh->b_this_page) != head); unlock: unlock_page(page); ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] fscrypt: Copy the memcg information to the ciphertext page 2023-02-01 6:31 ` Eric Biggers @ 2023-02-02 21:30 ` Tejun Heo 2023-02-03 1:07 ` Eric Biggers 0 siblings, 1 reply; 5+ messages in thread From: Tejun Heo @ 2023-02-02 21:30 UTC (permalink / raw) To: Eric Biggers Cc: Matthew Wilcox, Theodore Y . Ts'o, Jaegeuk Kim, linux-fscrypt, linux-fsdevel, linux-ext4, linux-f2fs-devel, stable, cgroups Hello, On Tue, Jan 31, 2023 at 10:31:31PM -0800, Eric Biggers wrote: > > These can usually be handled by explicitly associating the bio's to the > > desired cgroups using one of bio_associate_blkg*() or > > bio_clone_blkg_association(). > > Here that already happens in wbc_init_bio(), called from io_submit_init_bio() in > fs/ext4/page-io.c. Yeah, without bouncing, that's usually how writeback IOs are associated with their cgroups. > > It is possible to go through memcg ownership > > too using set_active_memcg() so that the page is owned by the target cgroup; > > however, the page ownership doesn't directly map to IO ownership as the > > relationship depends on the type of the page (e.g. IO ownership for > > pagecache writeback is determined per-inode, not per-page). If the in-flight > > pages are limited, it probably is better to set bio association directly. > > ext4 also calls wbc_account_cgroup_owner() for each pagecache page that's > written out. It seems this is for a different purpose -- it looks like the > fs-writeback code is trying to figure out which cgroup "owns" the inode based on > which cgroup "owns" most of the pagecache pages? Yeah, there's a difference between how memory and IO track cgroup ownership. Memory ownership is per-page but IO ownership is per-inode. This is because splitting writeback IOs of the same inode can perform really badly, so we try to find the majority dirty page owner cgroup of a given inode and associate the whole inode to that cgroup. So, something like md / dm, which gets a bio from filesystem and then bounces it to another bio, would use either bio_clone_blkg_association() to copy the association of the original bio (which probably is set through wbc_init_bio()) or determine the cgroup the bio should belong to somehow and set it explicitly with bio_associate_blkg(). However, here, as the filesystem is the one bouncing I guess it can be simpler. > The bug we're discussing here is that when ext4 writes out a pagecache page in > an encrypted file, it first encrypts the data into a bounce page, then passes > the bounce page (which don't have a memcg) to wbc_account_cgroup_owner(). Maybe > the proper fix is to just pass the pagecache page to wbc_account_cgroup_owner() > instead? See below for ext4 (a separate patch would be needed for f2fs): Yeah, this makes sense to me and is the right thing to do no matter what. wbc_account_cgroup_owner() should be fed the origin page so that the IO can be blamed on the owner of that page. Thanks. -- tejun ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] fscrypt: Copy the memcg information to the ciphertext page 2023-02-02 21:30 ` Tejun Heo @ 2023-02-03 1:07 ` Eric Biggers 0 siblings, 0 replies; 5+ messages in thread From: Eric Biggers @ 2023-02-03 1:07 UTC (permalink / raw) To: Tejun Heo Cc: Matthew Wilcox, Theodore Y . Ts'o, Jaegeuk Kim, linux-fscrypt, linux-fsdevel, linux-ext4, linux-f2fs-devel, stable, cgroups On Thu, Feb 02, 2023 at 11:30:42AM -1000, Tejun Heo wrote: > > The bug we're discussing here is that when ext4 writes out a pagecache page in > > an encrypted file, it first encrypts the data into a bounce page, then passes > > the bounce page (which don't have a memcg) to wbc_account_cgroup_owner(). Maybe > > the proper fix is to just pass the pagecache page to wbc_account_cgroup_owner() > > instead? See below for ext4 (a separate patch would be needed for f2fs): > > Yeah, this makes sense to me and is the right thing to do no matter what. > wbc_account_cgroup_owner() should be fed the origin page so that the IO can > be blamed on the owner of that page. Thanks. These patches fix this for ext4 and f2fs: * https://lore.kernel.org/r/20230203005503.141557-1-ebiggers@kernel.org * https://lore.kernel.org/r/20230203010239.216421-1-ebiggers@kernel.org - Eric ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-02-03 1:07 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20230129121851.2248378-1-willy@infradead.org>
[not found] ` <Y9a2m8uvmXmCVYvE@sol.localdomain>
[not found] ` <Y9a2m8uvmXmCVYvE-8Kz7deQLoS0mYvmMESoHnA@public.gmane.org>
2023-01-29 21:26 ` [PATCH] fscrypt: Copy the memcg information to the ciphertext page Matthew Wilcox
[not found] ` <Y9bkoasmAmtQ2nSV-FZi0V3Vbi30CUdFEqe4BF2D2FQJk+8+b@public.gmane.org>
2023-01-31 21:27 ` Tejun Heo
[not found] ` <Y9mH0PCcZoGPryXw-NiLfg/pYEd1N0TnZuCh8vA@public.gmane.org>
2023-02-01 6:31 ` Eric Biggers
2023-02-02 21:30 ` Tejun Heo
2023-02-03 1:07 ` Eric Biggers
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox