• Filipe Manana's avatar
    btrfs: stop extent map shrinker if reschedule is needed · b3ebb9b7
    Filipe Manana authored
    The extent map shrinker can be called in a variety of contexts where we
    are under memory pressure, and of them is when a task is trying to
    allocate memory. For this reason the shrinker is typically called with a
    value of struct shrink_control::nr_to_scan that is much smaller than what
    we return in the nr_cached_objects callback of struct super_operations
    (fs/btrfs/super.c:btrfs_nr_cached_objects()), so that the shrinker does
    not take a long time and cause high latencies. However we can still take
    a lot of time in the shrinker even for a limited amount of nr_to_scan:
    
    1) When traversing the red black tree that tracks open inodes in a root,
       as for example with millions of open inodes we get a deep tree which
       takes time searching for an inode;
    
    2) Iterating over the extent map tree, which is a red black tree, of an
       inode when doing the rb_next() calls and when removing an extent map
       from the tree, since often that requires rebalancing the red black
       tree;
    
    3) When trying to write lock an inode's extent map tree we may wait for a
       significant amount of time, because there's either another task about
       to do IO and searching for an extent map in the tree or inserting an
       extent map in the tree, and we can have thousands or even millions of
       extent maps for an inode. Furthermore, there can be concurrent calls
       to the shrinker so the lock might be busy simply because there is
       already another task shrinking extent maps for the same inode;
    
    4) We often reschedule if we need to, which further increases latency.
    
    So improve on this by stopping the extent map shrinking code whenever we
    need to reschedule and make it skip an inode if we can't immediately lock
    its extent map tree.
    Reported-by: default avatarMikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
    Reported-by: default avatarAndrea Gelmini <andrea.gelmini@gmail.com>
    Link: https://lore.kernel.org/linux-btrfs/CABXGCsMmmb36ym8hVNGTiU8yfUS_cGvoUmGCcBrGWq9OxTrs+A@mail.gmail.com/Reviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
    Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    b3ebb9b7
extent_map.c 34 KB