Skip to contentSkip to conversation

rcu_sync_dtor() warning question

DISCUSSION Paul E. McKenney 4 participants 9 messages Jul 26, 2024 – Jul 29, 2024 1 message not in the archive RCU

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20240726-ansonsten-piste-c4f04d4909fc@brauner

Paul E. McKenney

author maintainer
#1 lore raw
Quoted 39 lines · Christian Brauner

Hey,

I could use some help with understanding a bug related to rcu that was reported today. It first seems to have shown up on the 25th of July:

https://syzkaller.appspot.com/bug?extid=20d7e439f76bbbd863a7

We seem to be hitting the WARN_ON_ONCE() in:

void rcu_sync_dtor(struct rcu_sync *rsp)

{
        int gp_state;

        WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);

from destroy_super_work() which gets called when a superblock is really freed.

If the superblock has been visible in userspace we do it via call_rcu():

static void destroy_super_work(struct work_struct *work)

{
        struct super_block *s = container_of(work, struct super_block,
                                                        destroy_work);
        fsnotify_sb_free(s);
        security_sb_free(s);
        put_user_ns(s->s_user_ns);
        kfree(s->s_subtype);
        for (int i = 0; i < SB_FREEZE_LEVELS; i++)
                percpu_free_rwsem(&s->s_writers.rw_sem[i]);
        kfree(s);
}

static void destroy_super_rcu(struct rcu_head *head)

{
        struct super_block *s = container_of(head, struct super_block, rcu);
        INIT_WORK(&s->destroy_work, destroy_super_work);
        schedule_work(&s->destroy_work);
}
Quoted 3 lines

And I'm really confused because I don't understand the details for sync rcu enough to come up with a clear problem statement even. Could someone please explain what the WARN_ON_ONCE() is about?

If I am not too confused (and Oleg will correct me if I am), this is checking a use-after-free error. A given rcu_sync structure normally transitions from GP_IDLE->GP_ENTER->GP_PASSED->GP_EXIT->GP_IDLE, with possible side-trips from the GP_EXIT state through GP_REPLAY and back to GP_EXIT in special cases such as during early boot.

One way that this can happen is calling percpu_free_rwsem() without having previously done the percpu_up_write() needed to match an earlier percpu_down_write(). I don't see any other way that this can happen right off-hand, but someone might have added something, or I might be suffering a failure of imagination.

Quoted 4 lines

(I had been wondering if this is related to commit 7180f8d91fcb ("vfs:
add rcu-based find_inode variants for iget ops") and the fix in commit 5bc9ad78c2f8 ("vfs: handle __wait_on_freeing_inode() and evict() race") but I don't see how.)

I don't see how either, unless it introduced some timing change that made the scenario above more probable.

Quoted 1 line

Thanks for taking a look!

Hope this helps! If not, you know where to find me. ;-)

Thanx, Paul

#2 lore raw
Quoted 45 lines · Paul E. McKenney
Quoted 39 lines · Christian Brauner

Hey,

I could use some help with understanding a bug related to rcu that was reported today. It first seems to have shown up on the 25th of July:

https://syzkaller.appspot.com/bug?extid=20d7e439f76bbbd863a7

We seem to be hitting the WARN_ON_ONCE() in:

void rcu_sync_dtor(struct rcu_sync *rsp)

{
        int gp_state;

        WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);

from destroy_super_work() which gets called when a superblock is really freed.

If the superblock has been visible in userspace we do it via call_rcu():

static void destroy_super_work(struct work_struct *work)

{
        struct super_block *s = container_of(work, struct super_block,
                                                        destroy_work);
        fsnotify_sb_free(s);
        security_sb_free(s);
        put_user_ns(s->s_user_ns);
        kfree(s->s_subtype);
        for (int i = 0; i < SB_FREEZE_LEVELS; i++)
                percpu_free_rwsem(&s->s_writers.rw_sem[i]);
        kfree(s);
}

static void destroy_super_rcu(struct rcu_head *head)

{
        struct super_block *s = container_of(head, struct super_block, rcu);
        INIT_WORK(&s->destroy_work, destroy_super_work);
        schedule_work(&s->destroy_work);
}
Quoted 3 lines

And I'm really confused because I don't understand the details for sync rcu enough to come up with a clear problem statement even. Could someone please explain what the WARN_ON_ONCE() is about?

Quoted 6 lines

If I am not too confused (and Oleg will correct me if I am), this is checking a use-after-free error. A given rcu_sync structure normally transitions from GP_IDLE->GP_ENTER->GP_PASSED->GP_EXIT->GP_IDLE, with possible side-trips from the GP_EXIT state through GP_REPLAY and back to GP_EXIT in special cases such as during early boot.

use-after-free? In that case I have a candidate for a culprit.

Code prior to any of my changes was doing the following in iget_locked():

        spin_lock(&inode_hash_lock);
        inode = find_inode_fast(sb, head, ino);
        spin_unlock(&inode_hash_lock);
        if (inode) {
                if (IS_ERR(inode))
                        return NULL;
                wait_on_inode(inode);
                if (unlikely(inode_unhashed(inode))) {
                        iput(inode);
                        goto again;
                }
                return inode;
        }

My patch removed the spinlock acquire and made it significantly more likely for the code to end up doing the wait_on_inode + inode_unhashed combo when racing against inode teardown.

Now that you bring up use-after-free I'm not particularly confident the stock code is correct.

For example evict_inodes() -> evict() can mess with the inode and result in the iput() call in iget_locked(), which then will invoke evict() again. And I'm not particularly confident the routine + everything it calls is idempotent.

That's from a quick poke around, maybe I missed something.

syzkaller claims to have a reproducer. Trivial usage in my debug vm does not result in anything, so I may need to grab their entire setup to reproduce.

I'm going to look into it.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
#3 lore raw
Quoted 83 lines · Mateusz Guzik
Quoted 45 lines · Paul E. McKenney
Quoted 39 lines · Christian Brauner

Hey,

I could use some help with understanding a bug related to rcu that was reported today. It first seems to have shown up on the 25th of July:

https://syzkaller.appspot.com/bug?extid=20d7e439f76bbbd863a7

We seem to be hitting the WARN_ON_ONCE() in:

void rcu_sync_dtor(struct rcu_sync *rsp)

{
        int gp_state;

        WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);

from destroy_super_work() which gets called when a superblock is really freed.

If the superblock has been visible in userspace we do it via call_rcu():

static void destroy_super_work(struct work_struct *work)

{
        struct super_block *s = container_of(work, struct super_block,
                                                        destroy_work);
        fsnotify_sb_free(s);
        security_sb_free(s);
        put_user_ns(s->s_user_ns);
        kfree(s->s_subtype);
        for (int i = 0; i < SB_FREEZE_LEVELS; i++)
                percpu_free_rwsem(&s->s_writers.rw_sem[i]);
        kfree(s);
}

static void destroy_super_rcu(struct rcu_head *head)

{
        struct super_block *s = container_of(head, struct super_block, rcu);
        INIT_WORK(&s->destroy_work, destroy_super_work);
        schedule_work(&s->destroy_work);
}
Quoted 3 lines

And I'm really confused because I don't understand the details for sync rcu enough to come up with a clear problem statement even. Could someone please explain what the WARN_ON_ONCE() is about?

Quoted 6 lines

If I am not too confused (and Oleg will correct me if I am), this is checking a use-after-free error. A given rcu_sync structure normally transitions from GP_IDLE->GP_ENTER->GP_PASSED->GP_EXIT->GP_IDLE, with possible side-trips from the GP_EXIT state through GP_REPLAY and back to GP_EXIT in special cases such as during early boot.

use-after-free? In that case I have a candidate for a culprit.

Code prior to any of my changes was doing the following in iget_locked():

        spin_lock(&inode_hash_lock);
        inode = find_inode_fast(sb, head, ino);
        spin_unlock(&inode_hash_lock);
        if (inode) {
                if (IS_ERR(inode))
                        return NULL;
                wait_on_inode(inode);
                if (unlikely(inode_unhashed(inode))) {
                        iput(inode);
                        goto again;
                }
                return inode;
        }

My patch removed the spinlock acquire and made it significantly more likely for the code to end up doing the wait_on_inode + inode_unhashed combo when racing against inode teardown.

Now that you bring up use-after-free I'm not particularly confident the stock code is correct.

For example evict_inodes() -> evict() can mess with the inode and result in the iput() call in iget_locked(), which then will invoke evict() again. And I'm not particularly confident the routine + everything it calls is idempotent.

Quoted 7 lines

That's from a quick poke around, maybe I missed something.

syzkaller claims to have a reproducer. Trivial usage in my debug vm does not result in anything, so I may need to grab their entire setup to reproduce.

I'm going to look into it.

Welp.

syzbot did the bisect, it's not any of the above, instead:

commit b62e71be2110d8b52bf5faf3c3ed7ca1a0c113a5

Author: Chao Yu <[email protected]>
Date:   Sun Apr 23 15:49:15 2023 +0000

f2fs: support errors=remount-ro|continue|panic mountoption

https://lore.kernel.org/linux-fsdevel/[email protected]/T/#m90c03813e12e5cdff1eeada8f9ab581d5f039c76

That said, the stuff I mentioned still looks highly suspicious so I have to something to investigate regardless.

I think this thread can be abandoned. :)

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
author maintainer
#4 lore raw
Quoted 100 lines · Mateusz Guzik
Quoted 83 lines · Mateusz Guzik
Quoted 45 lines · Paul E. McKenney
Quoted 39 lines · Christian Brauner

Hey,

I could use some help with understanding a bug related to rcu that was reported today. It first seems to have shown up on the 25th of July:

https://syzkaller.appspot.com/bug?extid=20d7e439f76bbbd863a7

We seem to be hitting the WARN_ON_ONCE() in:

void rcu_sync_dtor(struct rcu_sync *rsp)

{
        int gp_state;

        WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);

from destroy_super_work() which gets called when a superblock is really freed.

If the superblock has been visible in userspace we do it via call_rcu():

static void destroy_super_work(struct work_struct *work)

{
        struct super_block *s = container_of(work, struct super_block,
                                                        destroy_work);
        fsnotify_sb_free(s);
        security_sb_free(s);
        put_user_ns(s->s_user_ns);
        kfree(s->s_subtype);
        for (int i = 0; i < SB_FREEZE_LEVELS; i++)
                percpu_free_rwsem(&s->s_writers.rw_sem[i]);
        kfree(s);
}

static void destroy_super_rcu(struct rcu_head *head)

{
        struct super_block *s = container_of(head, struct super_block, rcu);
        INIT_WORK(&s->destroy_work, destroy_super_work);
        schedule_work(&s->destroy_work);
}
Quoted 3 lines

And I'm really confused because I don't understand the details for sync rcu enough to come up with a clear problem statement even. Could someone please explain what the WARN_ON_ONCE() is about?

Quoted 6 lines

If I am not too confused (and Oleg will correct me if I am), this is checking a use-after-free error. A given rcu_sync structure normally transitions from GP_IDLE->GP_ENTER->GP_PASSED->GP_EXIT->GP_IDLE, with possible side-trips from the GP_EXIT state through GP_REPLAY and back to GP_EXIT in special cases such as during early boot.

use-after-free? In that case I have a candidate for a culprit.

Code prior to any of my changes was doing the following in iget_locked():

        spin_lock(&inode_hash_lock);
        inode = find_inode_fast(sb, head, ino);
        spin_unlock(&inode_hash_lock);
        if (inode) {
                if (IS_ERR(inode))
                        return NULL;
                wait_on_inode(inode);
                if (unlikely(inode_unhashed(inode))) {
                        iput(inode);
                        goto again;
                }
                return inode;
        }

My patch removed the spinlock acquire and made it significantly more likely for the code to end up doing the wait_on_inode + inode_unhashed combo when racing against inode teardown.

Now that you bring up use-after-free I'm not particularly confident the stock code is correct.

For example evict_inodes() -> evict() can mess with the inode and result in the iput() call in iget_locked(), which then will invoke evict() again. And I'm not particularly confident the routine + everything it calls is idempotent.

Quoted 7 lines

That's from a quick poke around, maybe I missed something.

syzkaller claims to have a reproducer. Trivial usage in my debug vm does not result in anything, so I may need to grab their entire setup to reproduce.

I'm going to look into it.

Welp.

syzbot did the bisect, it's not any of the above, instead:

commit b62e71be2110d8b52bf5faf3c3ed7ca1a0c113a5

Author: Chao Yu <[email protected]>
Date:   Sun Apr 23 15:49:15 2023 +0000
Quoted 8 lines

f2fs: support errors=remount-ro|continue|panic mountoption

https://lore.kernel.org/linux-fsdevel/[email protected]/T/#m90c03813e12e5cdff1eeada8f9ab581d5f039c76

That said, the stuff I mentioned still looks highly suspicious so I have to something to investigate regardless.

I think this thread can be abandoned. :)

Thank you for digging into this one!

Thanx, Paul

#5 lore raw
In reply to Mateusz Guzik

Sorry for late reply.

You do not need my help, I know nothing about fs ;) but just in case...

Quoted 9 lines · Mateusz Guzik

Welp.

syzbot did the bisect, it's not any of the above, instead:

commit b62e71be2110d8b52bf5faf3c3ed7ca1a0c113a5

Author: Chao Yu <[email protected]>
Date:   Sun Apr 23 15:49:15 2023 +0000
Quoted 6 lines

f2fs: support errors=remount-ro|continue|panic mountoption

https://lore.kernel.org/linux-fsdevel/[email protected]/T/#m90c03813e12e5cdff1eeada8f9ab581d5f039c76

That said, the stuff I mentioned still looks highly suspicious so I have to something to investigate regardless.

Did you see the patch from Hillf ?
https://lore.kernel.org/all/[email protected]/
it seems to fix the problem...

Of course I don't understand this patch, but afaics SB_RDONLY can confuse thaw_super_locked(). If sb_rdonly() is true, thaw_super_locked() assumes that freeze_super() didn't call sb_wait_write() -> percpu_down_write().

So in this case thaw_super_locked() just clears sb->s_writers.frozen and goes to the "out_deactivate" label bypassing sb_freeze_unlock().

Oleg.

#6 lore raw
Quoted 16 lines · Oleg Nesterov

Sorry for late reply.

You do not need my help, I know nothing about fs ;) but just in case...

Quoted 11 lines · Mateusz Guzik

Welp.

syzbot did the bisect, it's not any of the above, instead:

commit b62e71be2110d8b52bf5faf3c3ed7ca1a0c113a5

Author: Chao Yu <[email protected]>
Date:   Sun Apr 23 15:49:15 2023 +0000

f2fs: support errors=remount-ro|continue|panic mountoption

Quoted 8 lines
Quoted 4 lines

https://lore.kernel.org/linux-fsdevel/[email protected]/T/#m90c03813e12e5cdff1eeada8f9ab581d5f039c76

That said, the stuff I mentioned still looks highly suspicious so I have to something to investigate regardless.

Did you see the patch from Hillf ?
https://lore.kernel.org/all/[email protected]/
it seems to fix the problem...

I only skimmed the bisect while I was entertaining a toddler during a trainride. :)

Quoted 7 lines

Of course I don't understand this patch, but afaics SB_RDONLY can confuse thaw_super_locked(). If sb_rdonly() is true, thaw_super_locked() assumes that freeze_super() didn't call sb_wait_write() -> percpu_down_write().

So in this case thaw_super_locked() just clears sb->s_writers.frozen and goes to the "out_deactivate" label bypassing sb_freeze_unlock().

So the thing is that remounting a superblock read-only directly without going through the VFS first is pretty risky and is likely to cause trouble. The order of operations to transition rw->ro:

(1) Check that the filsystem is unfrozen. If not, fail with EBUSY.
(2) Mark all mounts of that filesystem as read-only. Fail with EBUSY if there are any active writers on any of the mounts.
(3) Call into ->reconfigure() method of the filesystem so it can do it's internal thing to mount read-only.

All of that requires sb->s_umount semaphore to be held. But f2fs seems to transition rw->ro without holding sb->s_umount in various callsites of:

f2fs_stop_checkpoint()
-> f2fs_handle_critical_error()

So that would mean freeze_super() would've indeed acquired percpu_down_write(SB_FREEZE_WRITE+SB_FREEZE_PAGEFAULT+SB_FREEZE_FS) but thaw_super() would skip calling sb_freeze_unlock(SB_FREEZE_FS+SB_FREEZE_PAGEFAULT+SB_FREEZE_WRITE) and destroying the superblock during umount with an imbalance that gets noticed in rcu_sync_dtor().

So afaict this would mean that we never called rcu_sync_exit() and thus never set GP_EXIT and notice that imbalance during rcu_sync_dtor().

#7 lore raw
Quoted 5 lines · Christian Brauner
Quoted 3 lines · Oleg Nesterov

So in this case thaw_super_locked() just clears sb->s_writers.frozen and goes to the "out_deactivate" label bypassing sb_freeze_unlock().

...

Quoted 9 lines

So that would mean freeze_super() would've indeed acquired percpu_down_write(SB_FREEZE_WRITE+SB_FREEZE_PAGEFAULT+SB_FREEZE_FS) but thaw_super() would skip calling sb_freeze_unlock(SB_FREEZE_FS+SB_FREEZE_PAGEFAULT+SB_FREEZE_WRITE) and destroying the superblock during umount with an imbalance that gets noticed in rcu_sync_dtor().

So afaict this would mean that we never called rcu_sync_exit() and thus never set GP_EXIT and notice that imbalance during rcu_sync_dtor().

Yes, yes, this is what I meant.

Oleg.

#8 lore raw
In reply to Mateusz Guzik
Quoted 100 lines · Mateusz Guzik
Quoted 83 lines · Mateusz Guzik
Quoted 45 lines · Paul E. McKenney
Quoted 39 lines · Christian Brauner

Hey,

I could use some help with understanding a bug related to rcu that was reported today. It first seems to have shown up on the 25th of July:

https://syzkaller.appspot.com/bug?extid=20d7e439f76bbbd863a7

We seem to be hitting the WARN_ON_ONCE() in:

void rcu_sync_dtor(struct rcu_sync *rsp)

{
        int gp_state;

        WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);

from destroy_super_work() which gets called when a superblock is really freed.

If the superblock has been visible in userspace we do it via call_rcu():

static void destroy_super_work(struct work_struct *work)

{
        struct super_block *s = container_of(work, struct super_block,
                                                        destroy_work);
        fsnotify_sb_free(s);
        security_sb_free(s);
        put_user_ns(s->s_user_ns);
        kfree(s->s_subtype);
        for (int i = 0; i < SB_FREEZE_LEVELS; i++)
                percpu_free_rwsem(&s->s_writers.rw_sem[i]);
        kfree(s);
}

static void destroy_super_rcu(struct rcu_head *head)

{
        struct super_block *s = container_of(head, struct super_block, rcu);
        INIT_WORK(&s->destroy_work, destroy_super_work);
        schedule_work(&s->destroy_work);
}
Quoted 3 lines

And I'm really confused because I don't understand the details for sync rcu enough to come up with a clear problem statement even. Could someone please explain what the WARN_ON_ONCE() is about?

Quoted 6 lines

If I am not too confused (and Oleg will correct me if I am), this is checking a use-after-free error. A given rcu_sync structure normally transitions from GP_IDLE->GP_ENTER->GP_PASSED->GP_EXIT->GP_IDLE, with possible side-trips from the GP_EXIT state through GP_REPLAY and back to GP_EXIT in special cases such as during early boot.

use-after-free? In that case I have a candidate for a culprit.

Code prior to any of my changes was doing the following in iget_locked():

        spin_lock(&inode_hash_lock);
        inode = find_inode_fast(sb, head, ino);
        spin_unlock(&inode_hash_lock);
        if (inode) {
                if (IS_ERR(inode))
                        return NULL;
                wait_on_inode(inode);
                if (unlikely(inode_unhashed(inode))) {
                        iput(inode);
                        goto again;
                }
                return inode;
        }

My patch removed the spinlock acquire and made it significantly more likely for the code to end up doing the wait_on_inode + inode_unhashed combo when racing against inode teardown.

Now that you bring up use-after-free I'm not particularly confident the stock code is correct.

For example evict_inodes() -> evict() can mess with the inode and result in the iput() call in iget_locked(), which then will invoke evict() again. And I'm not particularly confident the routine + everything it calls is idempotent.

Quoted 7 lines

That's from a quick poke around, maybe I missed something.

syzkaller claims to have a reproducer. Trivial usage in my debug vm does not result in anything, so I may need to grab their entire setup to reproduce.

I'm going to look into it.

Welp.

syzbot did the bisect, it's not any of the above, instead:

commit b62e71be2110d8b52bf5faf3c3ed7ca1a0c113a5

Author: Chao Yu <[email protected]>
Date:   Sun Apr 23 15:49:15 2023 +0000
Quoted 6 lines

f2fs: support errors=remount-ro|continue|panic mountoption

https://lore.kernel.org/linux-fsdevel/[email protected]/T/#m90c03813e12e5cdff1eeada8f9ab581d5f039c76

That said, the stuff I mentioned still looks highly suspicious so I have to something to investigate regardless.

Thanks for looking into this!

#9 lore raw
In reply to Paul E. McKenney
Quoted 50 lines · Paul E. McKenney
Quoted 39 lines · Christian Brauner

Hey,

I could use some help with understanding a bug related to rcu that was reported today. It first seems to have shown up on the 25th of July:

https://syzkaller.appspot.com/bug?extid=20d7e439f76bbbd863a7

We seem to be hitting the WARN_ON_ONCE() in:

void rcu_sync_dtor(struct rcu_sync *rsp)

{
        int gp_state;

        WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);

from destroy_super_work() which gets called when a superblock is really freed.

If the superblock has been visible in userspace we do it via call_rcu():

static void destroy_super_work(struct work_struct *work)

{
        struct super_block *s = container_of(work, struct super_block,
                                                        destroy_work);
        fsnotify_sb_free(s);
        security_sb_free(s);
        put_user_ns(s->s_user_ns);
        kfree(s->s_subtype);
        for (int i = 0; i < SB_FREEZE_LEVELS; i++)
                percpu_free_rwsem(&s->s_writers.rw_sem[i]);
        kfree(s);
}

static void destroy_super_rcu(struct rcu_head *head)

{
        struct super_block *s = container_of(head, struct super_block, rcu);
        INIT_WORK(&s->destroy_work, destroy_super_work);
        schedule_work(&s->destroy_work);
}
Quoted 3 lines

And I'm really confused because I don't understand the details for sync rcu enough to come up with a clear problem statement even. Could someone please explain what the WARN_ON_ONCE() is about?

If I am not too confused (and Oleg will correct me if I am), this is checking a use-after-free error. A given rcu_sync structure normally transitions from GP_IDLE->GP_ENTER->GP_PASSED->GP_EXIT->GP_IDLE, with possible side-trips from the GP_EXIT state through GP_REPLAY and back to GP_EXIT in special cases such as during early boot.

Quoted 5 lines

One way that this can happen is calling percpu_free_rwsem() without having previously done the percpu_up_write() needed to match an earlier percpu_down_write(). I don't see any other way that this can happen right off-hand, but someone might have added something, or I might be suffering a failure of imagination.

And you were spot on of course!

Quoted 6 lines
Quoted 5 lines

(I had been wondering if this is related to commit 7180f8d91fcb ("vfs:
add rcu-based find_inode variants for iget ops") and the fix in commit 5bc9ad78c2f8 ("vfs: handle __wait_on_freeing_inode() and evict() race") but I don't see how.)

Quoted 6 lines

I don't see how either, unless it introduced some timing change that made the scenario above more probable.

Quoted 1 line

Thanks for taking a look!

Hope this helps! If not, you know where to find me. ;-)

Thank you for taking the time, Paul. That was indeed helpful!