Subject: bug#51345: dd with conv=fsync sometimes returns when its writes are still cached

From: Pádraig Brady <[email-masked]>
Date: Sat, 23 Oct 2021 13:55:19 +0100

On 23/10/2021 09:18, Sworddragon wrote:
> On Knoppix 9.1 with the Linux Kernel 5.10.10-64 x86_64 and GNU Coreutils
> 8.32 I wanted to overwrite my USB Thumb Drive a few times with random data
> via "dd if=/dev/random of=/dev/sdb bs=1M conv=fsync". While it usually
> takes ~2+ minutes to perform this action dd returned once after less than
> 60 seconds which made me a bit curious. On a later attempt dd returned with
> this command after ~1 minute again but the LED on my USB Thumb Drive was
> still blinking and an immediated executed sync on the terminal blocked for
> ~1 minute and once it returned the LED on my USB Thumb Drive also stopped
> blinking.
> 
> Knoppix 9.1 was booted as a live system from a DVD and the only another
> persistent storage attached to it was an internal HDD which was already
> overwritten the same way via a past booting session.
> 
> 
> Unfortunately Linux is not my main operating system anymore so I randomly
> encountered this issue on a nearly 1 year old distribution which might be
> slightly outdated. But the issue is pretty severe as it can lead to
> significant data loss so it is worth at least reporting it (and having the
> "bad" behavior to always call sync after dd nonetheless will probably now
> continue to stay for quite a while).
> 

Well we're relying on the kernel here to not return from fync()
until appropriate.  You could try running the following immediately after,
to see if it also returns quickly:

   blockdev --flushbufs /dev/sdb

Note a subsequent `sync /dev/sdb` would probably not be effective,
since that would only consider data written by theh sync process
(which would be nothing).

cheers,
Pádraig