diff mbox series

[v2,tip/perf/core,3/5] fs: add back RCU-delayed freeing of FMODE_BACKING file

Message ID 20241001225207.2215639-4-andrii@kernel.org (mailing list archive)
State Superseded
Headers show
Series uprobes,mm: speculative lockless VMA-to-uprobe lookup | expand

Commit Message

Andrii Nakryiko Oct. 1, 2024, 10:52 p.m. UTC
6cf41fcfe099 ("backing file: free directly") switched FMODE_BACKING
files to direct freeing as back then there were no use cases requiring
RCU protected access to such files.

Now, with speculative lockless VMA-to-uprobe lookup logic, we do need to
have a guarantee that struct file memory is not going to be freed from
under us during speculative check. So add back RCU-delayed freeing
logic.

We use headless kfree_rcu_mightsleep() variant, as file_free() is only
called for FMODE_BACKING files in might_sleep() context.

Suggested-by: Suren Baghdasaryan <surenb@google.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 fs/file_table.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Christian Brauner Oct. 3, 2024, 9:13 a.m. UTC | #1
On Tue, Oct 01, 2024 at 03:52:05PM GMT, Andrii Nakryiko wrote:
> 6cf41fcfe099 ("backing file: free directly") switched FMODE_BACKING
> files to direct freeing as back then there were no use cases requiring
> RCU protected access to such files.
> 
> Now, with speculative lockless VMA-to-uprobe lookup logic, we do need to
> have a guarantee that struct file memory is not going to be freed from
> under us during speculative check. So add back RCU-delayed freeing
> logic.
> 
> We use headless kfree_rcu_mightsleep() variant, as file_free() is only
> called for FMODE_BACKING files in might_sleep() context.
> 
> Suggested-by: Suren Baghdasaryan <surenb@google.com>
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: Amir Goldstein <amir73il@gmail.com>
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> ---

Reviewed-by: Christian Brauner <brauner@kernel.org>
Christian Brauner Oct. 4, 2024, 8:01 a.m. UTC | #2
On Thu, Oct 03, 2024 at 11:13:54AM GMT, Christian Brauner wrote:
> On Tue, Oct 01, 2024 at 03:52:05PM GMT, Andrii Nakryiko wrote:
> > 6cf41fcfe099 ("backing file: free directly") switched FMODE_BACKING
> > files to direct freeing as back then there were no use cases requiring
> > RCU protected access to such files.
> > 
> > Now, with speculative lockless VMA-to-uprobe lookup logic, we do need to
> > have a guarantee that struct file memory is not going to be freed from
> > under us during speculative check. So add back RCU-delayed freeing
> > logic.
> > 
> > We use headless kfree_rcu_mightsleep() variant, as file_free() is only
> > called for FMODE_BACKING files in might_sleep() context.
> > 
> > Suggested-by: Suren Baghdasaryan <surenb@google.com>
> > Cc: Christian Brauner <brauner@kernel.org>
> > Cc: Amir Goldstein <amir73il@gmail.com>
> > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > ---
> 
> Reviewed-by: Christian Brauner <brauner@kernel.org>

Fwiw, I have another patch series for files that I'm testing that will
require me to switch FMODE_BACKING to a SLAB_TYPSAFE_BY_RCU cache. That
shouldn't matter for your use-case though.
Andrii Nakryiko Oct. 4, 2024, 7:58 p.m. UTC | #3
On Fri, Oct 4, 2024 at 1:01 AM Christian Brauner <brauner@kernel.org> wrote:
>
> On Thu, Oct 03, 2024 at 11:13:54AM GMT, Christian Brauner wrote:
> > On Tue, Oct 01, 2024 at 03:52:05PM GMT, Andrii Nakryiko wrote:
> > > 6cf41fcfe099 ("backing file: free directly") switched FMODE_BACKING
> > > files to direct freeing as back then there were no use cases requiring
> > > RCU protected access to such files.
> > >
> > > Now, with speculative lockless VMA-to-uprobe lookup logic, we do need to
> > > have a guarantee that struct file memory is not going to be freed from
> > > under us during speculative check. So add back RCU-delayed freeing
> > > logic.
> > >
> > > We use headless kfree_rcu_mightsleep() variant, as file_free() is only
> > > called for FMODE_BACKING files in might_sleep() context.
> > >
> > > Suggested-by: Suren Baghdasaryan <surenb@google.com>
> > > Cc: Christian Brauner <brauner@kernel.org>
> > > Cc: Amir Goldstein <amir73il@gmail.com>
> > > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > > ---
> >
> > Reviewed-by: Christian Brauner <brauner@kernel.org>
>
> Fwiw, I have another patch series for files that I'm testing that will
> require me to switch FMODE_BACKING to a SLAB_TYPSAFE_BY_RCU cache. That
> shouldn't matter for your use-case though.

Correct, we assume SLAB_TYPESAFE_BY_RCU semantics for the common case
anyways. But hopefully my change won't cause major merge conflicts
with your patch set.
Christian Brauner Oct. 9, 2024, 10:35 a.m. UTC | #4
On Fri, Oct 04, 2024 at 12:58:00PM GMT, Andrii Nakryiko wrote:
> On Fri, Oct 4, 2024 at 1:01 AM Christian Brauner <brauner@kernel.org> wrote:
> >
> > On Thu, Oct 03, 2024 at 11:13:54AM GMT, Christian Brauner wrote:
> > > On Tue, Oct 01, 2024 at 03:52:05PM GMT, Andrii Nakryiko wrote:
> > > > 6cf41fcfe099 ("backing file: free directly") switched FMODE_BACKING
> > > > files to direct freeing as back then there were no use cases requiring
> > > > RCU protected access to such files.
> > > >
> > > > Now, with speculative lockless VMA-to-uprobe lookup logic, we do need to
> > > > have a guarantee that struct file memory is not going to be freed from
> > > > under us during speculative check. So add back RCU-delayed freeing
> > > > logic.
> > > >
> > > > We use headless kfree_rcu_mightsleep() variant, as file_free() is only
> > > > called for FMODE_BACKING files in might_sleep() context.
> > > >
> > > > Suggested-by: Suren Baghdasaryan <surenb@google.com>
> > > > Cc: Christian Brauner <brauner@kernel.org>
> > > > Cc: Amir Goldstein <amir73il@gmail.com>
> > > > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > > > ---
> > >
> > > Reviewed-by: Christian Brauner <brauner@kernel.org>
> >
> > Fwiw, I have another patch series for files that I'm testing that will
> > require me to switch FMODE_BACKING to a SLAB_TYPSAFE_BY_RCU cache. That
> > shouldn't matter for your use-case though.
> 
> Correct, we assume SLAB_TYPESAFE_BY_RCU semantics for the common case
> anyways. But hopefully my change won't cause major merge conflicts
> with your patch set.

Please drop this patch and pull the following tag which adds
SLAB_TYPE_SAFE_BY_RCU protection for FMODE_BACKING files aligning them
with regular files lifetime (even though not needed). The branch the tag
is based on is stable and won't change anymore:

git pull -S git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git tags/vfs-6.13.for-bpf.file
Andrii Nakryiko Oct. 9, 2024, 7:37 p.m. UTC | #5
On Wed, Oct 9, 2024 at 3:36 AM Christian Brauner <brauner@kernel.org> wrote:
>
> On Fri, Oct 04, 2024 at 12:58:00PM GMT, Andrii Nakryiko wrote:
> > On Fri, Oct 4, 2024 at 1:01 AM Christian Brauner <brauner@kernel.org> wrote:
> > >
> > > On Thu, Oct 03, 2024 at 11:13:54AM GMT, Christian Brauner wrote:
> > > > On Tue, Oct 01, 2024 at 03:52:05PM GMT, Andrii Nakryiko wrote:
> > > > > 6cf41fcfe099 ("backing file: free directly") switched FMODE_BACKING
> > > > > files to direct freeing as back then there were no use cases requiring
> > > > > RCU protected access to such files.
> > > > >
> > > > > Now, with speculative lockless VMA-to-uprobe lookup logic, we do need to
> > > > > have a guarantee that struct file memory is not going to be freed from
> > > > > under us during speculative check. So add back RCU-delayed freeing
> > > > > logic.
> > > > >
> > > > > We use headless kfree_rcu_mightsleep() variant, as file_free() is only
> > > > > called for FMODE_BACKING files in might_sleep() context.
> > > > >
> > > > > Suggested-by: Suren Baghdasaryan <surenb@google.com>
> > > > > Cc: Christian Brauner <brauner@kernel.org>
> > > > > Cc: Amir Goldstein <amir73il@gmail.com>
> > > > > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > > > > ---
> > > >
> > > > Reviewed-by: Christian Brauner <brauner@kernel.org>
> > >
> > > Fwiw, I have another patch series for files that I'm testing that will
> > > require me to switch FMODE_BACKING to a SLAB_TYPSAFE_BY_RCU cache. That
> > > shouldn't matter for your use-case though.
> >
> > Correct, we assume SLAB_TYPESAFE_BY_RCU semantics for the common case
> > anyways. But hopefully my change won't cause major merge conflicts
> > with your patch set.
>
> Please drop this patch and pull the following tag which adds
> SLAB_TYPE_SAFE_BY_RCU protection for FMODE_BACKING files aligning them
> with regular files lifetime (even though not needed). The branch the tag
> is based on is stable and won't change anymore:
>
> git pull -S git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git tags/vfs-6.13.for-bpf.file

Ok, will drop. It will on Peter to pull this tag into tip/perf/core,
but I'll mention all this in the cover letter (and will pull locally
for testing, of course). Thanks.
diff mbox series

Patch

diff --git a/fs/file_table.c b/fs/file_table.c
index ca7843dde56d..257691d358ee 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -68,7 +68,7 @@  static inline void file_free(struct file *f)
 	put_cred(f->f_cred);
 	if (unlikely(f->f_mode & FMODE_BACKING)) {
 		path_put(backing_file_user_path(f));
-		kfree(backing_file(f));
+		kfree_rcu_mightsleep(backing_file(f));
 	} else {
 		kmem_cache_free(filp_cachep, f);
 	}