Misuse of mapping_set_folio_order_range()
While reviewing an unrelated change, Sashiko seems to have found a bug in ext4:
Quoted 55 lines
--- Patch [9]: [PATCH v6 09/12] filemap: Remove checks in
mapping_set_folio_order_range()--- commitc0859bbb9afcf450bfdee34a144cba981c50e605
Author: Matthew Wilcox (Oracle) <[email protected]>filemap: Remove checks in
mapping_set_folio_order_range()This commit removes the maximum order checks in
mapping_set_folio_order_range()to allow its use in hugetlb.Quoted 5 lines
include/linux/pagemap.h +0
-0@@ -405,29 +405,23 @@ static inline size_t mapping_max_folio_size_supported(void)[ ... ]
Quoted 10 lines
static inline void mapping_set_folio_order_range(struct
address_space*mapping,include/linux/pagemap.h +2
-5- unsigned int min, - unsigned int max) + unsigned int min, unsigned int max) { -if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) +if (!IS_ENABLED (CONFIG_LARGE_FOLIO ))return ; - -if (min >MAX_PAGECACHE_ORDER )include/linux/pagemap.h
Quoted 10 lines
include/linux/pagemap.h +0
-5- min =MAX_PAGECACHE_ORDER ; - -if (max >MAX_PAGECACHE_ORDER ) - max =MAX_PAGECACHE_ORDER ; -if (max < min) max = min; mapping->flags = (mapping->flags & ~AS_FOLIO_ORDER_MASK ) | (min <<AS_FOLIO_ORDER_MIN ) | (max <<AS_FOLIO_ORDER_MAX );This is a pre-existing issue, but can this non-atomic read-modify-write of
mapping->flagscause lost updates to concurrently set atomic bit flags?While the documentation above this function states it should not be called while the inode is active,
ext4_change_inode_journal_flag()dynamically invokes it on an active inode during theEXT4_IOC_SETFLAGSioctl:fs/ext4/inode.c:ext4_change_inode_journal_flag() { ... ext4_set_inode_mapping_order(inode); ... }If a privileged user toggles the ext4 journal flag, could this non-atomic update race with
mm_take_all_locks()(which can be triggered by userfaultfd or KVM) modifyingAS_MM_ALL_LOCKSvia atomic bitops?
Quoted 5 lines
Since they do not share any locks (
ext4_change_inode_journal_flag()holdsinode_lockandinvalidate_lock, whilemm_take_all_locks()holdsmmap_lockandi_mmap_rwsem), losing theAS_MM_ALL_LOCKSbit could causevm_unlock_mapping()to skip unlockingmapping->i_mmap_rwsem, resulting in a permanent deadlock.
Making mapping_set_folio_order_range() atomic would be a pain -- it'd involve a cmpxchg() loop which would be utterly useless for most callers. Can you figure out how to fix this in ext4?
Quoted 64 lines · Matthew Wilcox
While reviewing an unrelated change, Sashiko seems to have found a bug in ext4:
Quoted 55 lines
--- Patch [9]: [PATCH v6 09/12] filemap: Remove checks in
mapping_set_folio_order_range()--- commitc0859bbb9afcf450bfdee34a144cba981c50e605
Author: Matthew Wilcox (Oracle) <[email protected]>filemap: Remove checks in
mapping_set_folio_order_range()This commit removes the maximum order checks in
mapping_set_folio_order_range()to allow its use in hugetlb.Quoted 5 lines
include/linux/pagemap.h +0
-0@@ -405,29 +405,23 @@ static inline size_t mapping_max_folio_size_supported(void)[ ... ]
Quoted 10 lines
static inline void mapping_set_folio_order_range(struct
address_space*mapping,include/linux/pagemap.h +2
-5- unsigned int min, - unsigned int max) + unsigned int min, unsigned int max) { -if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) +if (!IS_ENABLED (CONFIG_LARGE_FOLIO ))return ; - -if (min >MAX_PAGECACHE_ORDER )include/linux/pagemap.h
Quoted 10 lines
include/linux/pagemap.h +0
-5- min =MAX_PAGECACHE_ORDER ; - -if (max >MAX_PAGECACHE_ORDER ) - max =MAX_PAGECACHE_ORDER ; -if (max < min) max = min; mapping->flags = (mapping->flags & ~AS_FOLIO_ORDER_MASK ) | (min <<AS_FOLIO_ORDER_MIN ) | (max <<AS_FOLIO_ORDER_MAX );This is a pre-existing issue, but can this non-atomic read-modify-write of
mapping->flagscause lost updates to concurrently set atomic bit flags?While the documentation above this function states it should not be called while the inode is active,
ext4_change_inode_journal_flag()dynamically invokes it on an active inode during theEXT4_IOC_SETFLAGSioctl:fs/ext4/inode.c:ext4_change_inode_journal_flag() { ... ext4_set_inode_mapping_order(inode); ... }If a privileged user toggles the ext4 journal flag, could this non-atomic update race with
mm_take_all_locks()(which can be triggered by userfaultfd or KVM) modifyingAS_MM_ALL_LOCKSvia atomic bitops?Quoted 5 lines
Since they do not share any locks (
ext4_change_inode_journal_flag()holdsinode_lockandinvalidate_lock, whilemm_take_all_locks()holdsmmap_lockandi_mmap_rwsem), losing theAS_MM_ALL_LOCKSbit could causevm_unlock_mapping()to skip unlockingmapping->i_mmap_rwsem, resulting in a permanent deadlock.
Quoted 3 lines
Making
mapping_set_folio_order_range()atomic would be a pain -- it'd involve acmpxchg()loop which would be utterly useless for most callers. Can you figure out how to fix this in ext4?
What we need to do in ext4_change_inode_journal_flag() is that we want to disable / enable large folios when enabling / disabling data journalling for an inode (because writeback of large folios would strain the journal too much when journalling data). We are actually pretty careful in ext4_change_inode_journal_flag() - we lock the inode_lock, invalidate_lock, writeout everything, prune all the page cache, make sure to synchronize with writeback path and then we switch adress_space_operations and allowed folio orders.
As far as I can tell AS_MM_ALL_LOCKS flags bit manipulation is the only thing we can realistically race with. But I don't see how we could avoid that race from ext4 :-|. Maybe we could have mapping_set_folio_order_range_atomic() helper to avoid the pointless overhead for everybody?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR