From: Chris Mason mapping_set_folio_order_range() updates mapping->flags with a plain load and store, assuming that nobody can modify the flags concurrently. However, set_blocksize() and ext4_change_inode_journal_flag() call it on active inodes, and none of their locks protects from concurrent mm_take_all_locks(): set_blocksize() mm_take_all_locks() =============== =================== load mapping->flags test_and_set_bit(AS_MM_ALL_LOCKS) down_write(i_mmap_rwsem) store mapping->flags mm_drop_all_locks() bit is clear, no up_write() Consequently, we can lose concurrent setting/clearing of AS_MM_ALL_LOCKS, resulting in i_mmap_rwsem staying write-locked forever or being released by another task. Let's fix it by updating the order bits atomically, and document what callers on an active inode still have to ensure. This has medium urgency, as an unsuspecting admin can trigger it, leaving i_mmap_rwsem locked forever. Fixes: 3c20917120ce ("block/bdev: enable large folio support for large logical block sizes") Fixes: 58fd191f99f3 ("ext4: make data=journal support large block size") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: adjust comment and description ] Signed-off-by: David Hildenbrand (Arm) --- include/linux/pagemap.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 73af18a373670..0363ce1b1e5ab 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -411,8 +411,8 @@ static inline size_t mapping_max_folio_size_supported(void) * something the core cannot know). * Do not tune it based on, eg, i_size. * - * Context: This should not be called while the inode is active as it - * is non-atomic. + * Context: Safe to be called while the inode is active. However, the page + * cache must be empty and must be kept empty while setting the order range. */ static inline void mapping_set_folio_order_range(struct address_space *mapping, unsigned int min, @@ -430,8 +430,8 @@ static inline void mapping_set_folio_order_range(struct address_space *mapping, if (max < min) max = min; - mapping->flags = (mapping->flags & ~AS_FOLIO_ORDER_MASK) | - (min << AS_FOLIO_ORDER_MIN) | (max << AS_FOLIO_ORDER_MAX); + set_mask_bits(&mapping->flags, AS_FOLIO_ORDER_MASK, + (min << AS_FOLIO_ORDER_MIN) | (max << AS_FOLIO_ORDER_MAX)); } static inline void mapping_set_folio_min_order(struct address_space *mapping, @@ -448,8 +448,7 @@ static inline void mapping_set_folio_min_order(struct address_space *mapping, * indicate that the VFS can use large folios to cache the contents of * the file. * - * Context: This should not be called while the inode is active as it - * is non-atomic. + * Context: See mapping_set_folio_order_range(). */ static inline void mapping_set_large_folios(struct address_space *mapping) { -- 2.56.1