From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58B8D30594F for ; Thu, 25 Jun 2026 06:51:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782370275; cv=none; b=HiP1OQTEXwT1fNdCJ9zh/tEuYybBBLaOXBQht7UUf0xnnGVpzUKFI3ht55VtVl8FUQsFyS4dufgbtQlr4Hb/Nm18JBem0aUuN3VAd/X/BAnNdLqCht+/r8+C8H6dtKss6x/G0ykm2EqKW3AGLg2CvncRS0zZ3QRnuJj6w6JZkpo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782370275; c=relaxed/simple; bh=0aJq5AvCNmx/tVs4NpCm2sPY9d8D1RCNmj0pah4uK3E=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=irfuJbv1dK265prvh7UOBj6qG3tBa6wbOzazYM5LgZ5oFQlg91rm6YraGkNgE7OUFqptAb3z4W58LErFyk0JA0SbN7wLrFJ8wzBrtXGP2lTMCHQ6/ROkCGIct1nzGT/LDB/ArsuL4KPEtTVG/EYI5533TGlgB9YFOca9etSlnAQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=NWug5JXx; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="NWug5JXx" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-49263703c6eso9371215e9.0 for ; Wed, 24 Jun 2026 23:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1782370273; x=1782975073; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=mSaR1flX2hmelJOtLCUzSJ8gbOzzDK8LPhDuawsgajU=; b=NWug5JXxOvUm1UzjquT4qey6Rpul8uVvw5TkcE5bG/m/immr3zGzcMMlY01yibbamT Y+KFtBckTwFGPEXAcjDyoOHerdlM+rlfBvRw185vFSFY8rINfRIhRo18FFWA5/3k8zMS jgmCReGfu0SnzN97U9BNVuqjt7xXUjcaJjIbWTzLhRWewhKfljqh0TMuwVYh39+em3IJ sfAXvT6+V8VFF5NWbCLtOXf8ulgaN5V/t9hhgsaCOMa5XXVIjDAE7YHlEh/c6ChSzhkg 3U+4OI7GTWD8NAFcq1tP1AlwcKaMLHkcfCvurfh+GgKi9DpbmEVP11tq67HsNqP032j5 TT8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782370273; x=1782975073; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=mSaR1flX2hmelJOtLCUzSJ8gbOzzDK8LPhDuawsgajU=; b=o6X+fwL3pISEcxaXj8Ja+lDP3eytXlGLn+jdd9vG9Pi9MO7tIBVcpzO4UXqaSCIVH4 5FLkLNPCaTuKiE3L0U9mZd49/rMwXwmMgCdFBBB9UhP92MoZ29JovTDK0Htf2x9tuxXk shs15hxr5e4PVbZXcWY2aHMGpXuJ3vjeTZhaviKwLJNMZScDtyXDON4qghdadBbx9YiT K4v+uaDk4VN1KeIsGRlUcH1D43nLzbNZfoMSwPUVO7GZudtkqRxYd84ikhe7yv5TQUxZ huNleslp6UrNdbqgOHS2XYGWkE5lFyDhyZDBDMBLbFNld2zT10yG5meEMYEzTduQW3/p uYDg== X-Forwarded-Encrypted: i=1; AFNElJ8uPYQZc7UuRri0UMIStX5dgaXcc0yP6mkPFsi0dzqPJZ1xjtg9ZFs4uXz152O+xgElPk1zhZYZH+77YS4=@vger.kernel.org X-Gm-Message-State: AOJu0YxCokNXv7eWLo6PovrnLrDOXVJ5Tmp8NYvSthzGr7x7BHsm+Nd5 HZf4O8Z4SpUkpfxFn+cMwBxmqU2afxPCshe1GDt1jsROefXmowTGTmAdY2p7OQVVXkE= X-Gm-Gg: AfdE7cnhd40TYuqceU/TThC5qD7TprIlZf6oI3b6e4VV9EtK/D/Gw+35KMcLobXXei+ Vq7k8btVX9zyFusJ5AkTr9YTVouQAytXUhYIgY2i1pK7J2pgRDmqZaZN9grwUynDblEeatZdWlQ UhqFT8NHFqaIThH8FEzF0T3ShGfUemW4Zall5lVTAG50bB1DkQ6uOFMfYWUQZNgNRhwpWCSuvIA 9twW+xoUTMUp+5cTsA8vGQWbqvNIPel/oThFo4vNw5x2Oe1CpG25Ynz8I1IiMn5BmApWlWLl+3J IYYiu+PuAbXo+iZc2PJUtw/q+fJt/WDEQJ/PgZzoX03dcKXHgSk4OZCVd6ZBRT4zLmZR3Cbm46L E5vVELwtm5chVB86WmPj+qYSj5RY2PaoE+ejuf+7DkKrL5OKc0EZT+kmMOsOnS/ABCJcxcIaQhN apnqqOkuyKwAAp4xf0654OOGcpFBqqmoXUtAXR2oY= X-Received: by 2002:a05:600c:1d1b:b0:492:4caa:e2f4 with SMTP id 5b1f17b1804b1-492668ad885mr14185765e9.36.1782370272737; Wed, 24 Jun 2026 23:51:12 -0700 (PDT) Received: from ?IPV6:2403:580d:fda1::299? (2403-580d-fda1--299.ip6.aussiebb.net. [2403:580d:fda1::299]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-139d91006bcsm5372158c88.13.2026.06.24.23.51.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Jun 2026 23:51:11 -0700 (PDT) Message-ID: <68a6def7-20a7-4bce-bb79-aca32c48b7f7@suse.com> Date: Thu, 25 Jun 2026 16:21:07 +0930 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] btrfs: wait for ordered extents before buffered write fallback in direct IO To: "Zhou, Yun" , clm@fb.com, dsterba@suse.com Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org References: <20260625021456.724803-1-yun.zhou@windriver.com> Content-Language: en-US From: Qu Wenruo Autocrypt: addr=wqu@suse.com; keydata= xsBNBFnVga8BCACyhFP3ExcTIuB73jDIBA/vSoYcTyysFQzPvez64TUSCv1SgXEByR7fju3o 8RfaWuHCnkkea5luuTZMqfgTXrun2dqNVYDNOV6RIVrc4YuG20yhC1epnV55fJCThqij0MRL 1NxPKXIlEdHvN0Kov3CtWA+R1iNN0RCeVun7rmOrrjBK573aWC5sgP7YsBOLK79H3tmUtz6b 9Imuj0ZyEsa76Xg9PX9Hn2myKj1hfWGS+5og9Va4hrwQC8ipjXik6NKR5GDV+hOZkktU81G5 gkQtGB9jOAYRs86QG/b7PtIlbd3+pppT0gaS+wvwMs8cuNG+Pu6KO1oC4jgdseFLu7NpABEB AAHNGFF1IFdlbnJ1byA8d3F1QHN1c2UuY29tPsLAlAQTAQgAPgIbAwULCQgHAgYVCAkKCwIE FgIDAQIeAQIXgBYhBC3fcuWlpVuonapC4cI9kfOhJf6oBQJnEXVgBQkQ/lqxAAoJEMI9kfOh Jf6o+jIH/2KhFmyOw4XWAYbnnijuYqb/obGae8HhcJO2KIGcxbsinK+KQFTSZnkFxnbsQ+VY fvtWBHGt8WfHcNmfjdejmy9si2jyy8smQV2jiB60a8iqQXGmsrkuR+AM2V360oEbMF3gVvim 2VSX2IiW9KERuhifjseNV1HLk0SHw5NnXiWh1THTqtvFFY+CwnLN2GqiMaSLF6gATW05/sEd V17MdI1z4+WSk7D57FlLjp50F3ow2WJtXwG8yG8d6S40dytZpH9iFuk12Sbg7lrtQxPPOIEU rpmZLfCNJJoZj603613w/M8EiZw6MohzikTWcFc55RLYJPBWQ+9puZtx1DopW2jOwE0EWdWB rwEIAKpT62HgSzL9zwGe+WIUCMB+nOEjXAfvoUPUwk+YCEDcOdfkkM5FyBoJs8TCEuPXGXBO Cl5P5B8OYYnkHkGWutAVlUTV8KESOIm/KJIA7jJA+Ss9VhMjtePfgWexw+P8itFRSRrrwyUf E+0WcAevblUi45LjWWZgpg3A80tHP0iToOZ5MbdYk7YFBE29cDSleskfV80ZKxFv6koQocq0 vXzTfHvXNDELAuH7Ms/WJcdUzmPyBf3Oq6mKBBH8J6XZc9LjjNZwNbyvsHSrV5bgmu/THX2n g/3be+iqf6OggCiy3I1NSMJ5KtR0q2H2Nx2Vqb1fYPOID8McMV9Ll6rh8S8AEQEAAcLAfAQY AQgAJgIbDBYhBC3fcuWlpVuonapC4cI9kfOhJf6oBQJnEXWBBQkQ/lrSAAoJEMI9kfOhJf6o cakH+QHwDszsoYvmrNq36MFGgvAHRjdlrHRBa4A1V1kzd4kOUokongcrOOgHY9yfglcvZqlJ qfa4l+1oxs1BvCi29psteQTtw+memmcGruKi+YHD7793zNCMtAtYidDmQ2pWaLfqSaryjlzR /3tBWMyvIeWZKURnZbBzWRREB7iWxEbZ014B3gICqZPDRwwitHpH8Om3eZr7ygZck6bBa4MU o1XgbZcspyCGqu1xF/bMAY2iCDcq6ULKQceuKkbeQ8qxvt9hVxJC2W3lHq8dlK1pkHPDg9wO JoAXek8MF37R8gpLoGWl41FIUb3hFiu3zhDDvslYM4BmzI18QgQTQnotJH8= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 在 2026/6/25 16:16, Zhou, Yun 写道: > > > On 6/25/26 13:17, Qu Wenruo wrote: >> >> 在 2026/6/25 14:43, Qu Wenruo 写道: >>> >>> >>> 在 2026/6/25 11:44, Yun Zhou 写道: >>>> When btrfs_direct_write() falls back to buffered IO after a failed DIO >>>> attempt, it may race with the asynchronous completion of DIO ordered >>>> extents.  This leads to a BUG_ON in insert_ordered_extent() due to >>>> overlapping ordered extents in the per-inode rb-tree. >>>> >>>> The race sequence is: >>>>   1. DIO creates an ordered extent via btrfs_dio_iomap_begin() >>>>   2. Page fault occurs (nofault=true), no bio is submitted >>>> (submitted=0) >>>>   3. btrfs_dio_iomap_end() truncates and finishes the OE asynchronously >>>>      via btrfs_finish_ordered_extent() which queues work >>>>   4. iomap returns 0, retry logic faults in pages and retries DIO >>>>   5. Second DIO attempt also fails, code reaches buffered: label >>>>   6. btrfs_buffered_write() dirties pages for the same range >>> >>> btrfs_buffered_write() >>> |- copy_one_range() >>>     |- lock_and_cleanup_extent_if_needed() >>>        |- btrfs_start_ordered_extent() >>> >>> So your explanation doesn't makes sense. As if there is the direct IO oe >>> remaining, we will wait for that OE to complete. >>> >>> There is still something missing. >>> >>>>   7. btrfs_fdatawrite_range() triggers writeback >>>>   8. run_delalloc_nocow() -> fallback_to_cow() -> cow_file_range() >>>>      tries to insert a new ordered extent for the same file offset >>>>   9. The DIO ordered extent hasn't been removed from the rb-tree yet >>>>      (btrfs_finish_ordered_io running async in workqueue) -> BUG_ON >>>> >>>> Fix this by waiting for any pending ordered extents in the target range >>>> before starting the buffered write. >>>> >>>> Reported-by: syzbot+ba2afde329fc27e3f22e@syzkaller.appspotmail.com >>>> Closes: https://syzkaller.appspot.com/bug?extid=ba2afde329fc27e3f22e >>>> Fixes: acf9ed3a6c00 ("btrfs: retry faulting in the pages after a zero >>>> sized short direct write") >> >> And the fixes tag is also incorrect. >> >> Without that commit, we will directly fallback to buffered write without >> retry faulting in the pages. >> >> So by your explanation it will trigger the same problem, with or without >> that commit. > > Yes, my previous analysis does seem inaccurate. Commit acf9ed3a6c00 (which > added retries) merely amplified the window for the issue to occur, but > the problem has actually existed since ff66fe666233 ('btrfs: fix > incorrect buffered IO fallback for append direct writes'), which > introduced i_size revert on DIO short writes, causing > lock_and_cleanup_extent_if_need() to skip the OE check (since start_pos > >= reverted i_size). I will correct the commit message and the Fixes > tag in v2. That's right, and I have already sent out a fix based on that isize revert: https://lore.kernel.org/linux-btrfs/aaabe0f2bee07800fb0490d24fab5cd162e5241d.1782366056.git.wqu@suse.com/ > > Thanks, > Yun