Re: [PATCH 08/32] mm/khugepaged: flush file after unlocking folios in collapse_file()

From: David Hildenbrand (Arm)

Date: Wed Dec 02 2026 - 16:39:46 EST


On 12/2/26 22:32, Gregory Price (Meta) wrote:
> On 2026-11-29 11:06 +0100, David Hildenbrand (Arm) wrote:
>> From: Chris Mason <mason@xxxxxxxxxx>
>>
>> collapse_file() calls filemap_flush() when it encounters a dirty folio in
>> a file that is not open for writing. At that point, source folios earlier
>> in the range are still locked. The writable-open check is racy: a writer
>> can open the file after the check.
>>
>> On ext4, the flush may need a journal handle, while EXT4_IOC_MOVE_EXT
>> takes a journal handle before locking folios, and a journal commit blocks
>> new handles while waiting for existing handles to be released.
>>
>> Consequently, we can deadlock: collapse_file() waits for a handle, the
>> commit waits for extent movement to finish, and extent movement waits for
>> a source folio lock.
>>
>> Let's fix it by deferring filemap_flush() until rollback has unlocked all
>> source folios.
>>
>> This has medium urgency, as it can deadlock ext4 in some scenarios.
>>
>> Fixes: 75f360696ce9 ("mm/thp: flush file for !is_shmem PageDirty() case in collapse_file()")
>> Cc: stable@xxxxxxxxxxxxxxx
>> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
>> [ david: rework code; simplify comments and description ]
>> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>
> I wasn't actually able to reproduce this deadlock, but i was able to
> confirm the dangerous ordering in a contrived test at least. Were you
> able to produce the deadlock yourself?

If my memory serves me right, yes, all things involving ext4 were reproduced.

--
Cheers,

David