2

Sadly the issue reported and described here: Pernicious USB-stick stall problem. Reverting workaround fix? and Is "writeback throttling" a solution to the "USB-stick stall problem"? continues to be unresolved in modern Linux distros as of 2024 despite the availability of the BDI interface introduced in Linux 6.2 released in February 2023.

This can be addressed by a simple udev rule invoking a script which sets sane writeback cache values for USB mass storage devices.

1 Answers1

2

A udev rule, /etc/udev/rules.d/99-adjust-writeback-cache.rules:

ACTION=="add", KERNEL=="sd?", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", \
RUN+="/usr/local/lib/adjust-writeback-cache.sh $major $minor"
cat /usr/local/lib/adjust-writeback-cache.sh
#! /bin/bash

devroot=/sys/class/bdi max_bytes=134217728 # must be divisible by 4096 dev=$1:$2

logme="logger --tag basename $0" test "$TERM" = "xterm-256color" && logme=echo

test "$UID" -ne "0" && $logme "Must be run under root" && exit 1 test -z "$2" && $logme "Need two arguments: major minor" && exit 2

$logme "Adjusting writeback cache for the device [$dev] to $max_bytes bytes ..." test ! -d "$devroot/$dev" && $logme "The device [$dev] is not found in $devroot. Bailing out!" && exit 3

echo $max_bytes > "$devroot/$dev/max_bytes" res=cat "$devroot/$dev/max_bytes" test "$res" = "$max_bytes" && $logme "All good!" || $logme "The operation has failed."

Don't forget to make it executable, e.g. sudo chmod 755 /usr/local/lib/adjust-writeback-cache.sh

  • To be honest I feel like every class of storage must have different default values. E.g. here I've set 128MB for removable USB mass storage, for the spinning rust (i.e. HDD) it makes sense to set a value like 512MB and for NVMe we are probably OK with 1GB. The kernel defaults for vm.dirty_bytes and vm.dirty_background_bytes look simply insane for systems with more than 16GB of RAM. – Artem S. Tashkinov Mar 01 '24 at 09:16