Re:[PATCH v2] bcache: enhancing the security of dirty data writeback

"Zhou Jifeng" <[email protected]> Wed, 13 Aug 2025 15:11:48 +0800
Newsgroups org.kernel.vger.linux-bcache,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
On Wed, 13 Aug 2025 at 10:14, Zhou Jifeng <[email protected]> wrote:
>
> There is a potential data consistency risk in bcache's writeback mode:when
> the application calls fsync, bcache returns success after completing the
> log write, persisting the cache disk data, and persisting the HDD internal
> cache. However, at this point, the actual application data may still be in
> a dirty state and remain stuck in the cache disk. when these data are
> subsequently written back to the HDD asynchronously through REQ_OP_WRITE,
> there is no forced refresh mechanism to ensure physical placement on the
> disk, and there may be no power-off protection measures, which poses a risk
> of data loss. This mechanism may cause the application to misjudge that the
> data has been persisted, which is different from the actual storage state,
> and also violates the semantic agreement that fsync should ensure data
> persistence.
>
> This patch aims to enhance the reliability of dirty data writeback through
> PREFLUSH, ensuring that the dirty data mark in the cache device is cleared
> only after the dirty data is written to the disk. Double triggering
> conditions for PREFLUSH:
> 1、When the cumulative number of dirty bkeys written back reaches the
> threshold(Dynamic control parameters:/sys/block/bcache0/bcache/
> flush_interval, the default value is 20000. by increasing the value of this
> parameter, the impact of flush on performance can be reduced.)
> 2、When the interval since the last refresh exceeds 30 seconds
> If any of the conditions are met, the system will send a PREFLUSH command
> to the backend HDD, and clear the corresponding dirty bkey mark only after
> confirming that the PREFLUSH is executed successfully.
>
> Signed-off-by: Zhou Jifeng <[email protected]>
> ---
> v1 -> v2: Make revisions according to the review comments.
>
>
> [snipped]
>
>
> diff --git a/drivers/md/bcache/bcache_ondisk.h b/drivers/md/bcache/bcache_ondisk.h
> index 6620a7f8fffc..df5800838e40 100644
> --- a/drivers/md/bcache/bcache_ondisk.h
> +++ b/drivers/md/bcache/bcache_ondisk.h
> @@ -294,6 +294,10 @@ BITMASK(BDEV_CACHE_MODE,           struct cache_sb, flags, 0, 4);
>  #define CACHE_MODE_WRITEBACK           1U
>  #define CACHE_MODE_WRITEAROUND         2U
>  #define CACHE_MODE_NONE                        3U
> +BITMASK(BDEV_WRITEBACK_FLUSH,          struct cache_sb, flags, 4, 1);
> +#define WRITEBACK_FLUSH_OFF            0U
> +#define WRITEBACK_FLUSH_ON             1U
> +
>  BITMASK(BDEV_STATE,                    struct cache_sb, flags, 61, 2);
>  #define BDEV_STATE_NONE                        0U
>  #define BDEV_STATE_CLEAN               1U

Sorry, the inspection was not thorough enough. This is an invalid code. It was left over
from the past and was forgotten to be deleted.