Linux block layer
 help / color / mirror / Atom feed
* floppy: buffer_chain_size() misdirected DMA in 2.6.12..5.12 (gone since 3d86739c6343)
@ 2026-11-21  3:00 bouldrenough
  2026-11-23 21:15 ` Denis Efremov (Oracle)
  0 siblings, 1 reply; 2+ messages in thread
From: bouldrenough @ 2026-11-21  3:00 UTC (permalink / raw)
  To: linux-block; +Cc: efremov

Hello,

This is a report for the record, not a patch for mainline: the code is
already gone. It may help anyone still maintaining or using older kernels
on low-memory machines.

The bug was found, diagnosed and fixed by Claude, Anthropic's AI model
(Claude Opus 5.5, in Claude Code), while building a Linux distribution
for a real 386SX/20 with 16MB of RAM (Linux 3.7.10). I am the owner of
the machine and am sending this on its behalf; the analysis below is
Claude's, and I have reviewed it.

In drivers/block/floppy.c, buffer_chain_size() decides how much of a
request can be DMAed straight from its pages:

rq_for_each_segment(bv, current_req, iter) {
if (page_address(bv.bv_page) + bv.bv_offset != base + size)
break;
size += bv.bv_len;
}

rq_for_each_segment() is two nested loops (bios, then segments), so the
break only leaves the inner one. If the next bio's first page happens to
sit right after the run so far, it is counted as contiguous, and
make_raw_rw_request() DMAs across the gap. On a write, the floppy gets
another page's data, silently; on a read, the DMA would land in memory
outside the request.

It only matters where the page cache is below MAX_DMA_ADDRESS, so in
practice machines with 16MB of RAM or less. On the 386 it misplaced 4KB
blocks of dd-to-/dev/fd0 writes about one write in four, and on every
write under memory pressure. A debug patch logging each write just before
setup_DMA() showed, for one request:

  DEC pos450 chain14 segs ac5400+3072 ac2000+4096 ac6000+4096
  W c12 h1 s1 n14 DIR phys ac5400 -> sectors 456-463 held 464-471

(all pages still under writeback and at the right page-cache index).

The same loop is in 2.6.12 (written out as rq_for_each_bio +
bio_for_each_segment) through 5.12. Commit 3d86739c6343 ("floppy: always
use the track buffer", 5.13) removed the direct path as a cleanup and
with it this bug.

The fix for affected versions is to leave both loops:

- break;
+ goto out;
 ...
+out:
  return size >> 9;

It was tested on the real 386 with 3.7.10; patches for 2.6.32 to 5.12 and
the full write-up, with a test method, are at:
https://gist.github.com/agmorion/bc94c0f989334e28e2f1b03ec97733a3

Thanks,
bouldrenough@gmail.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23 21:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  3:00 floppy: buffer_chain_size() misdirected DMA in 2.6.12..5.12 (gone since 3d86739c6343) bouldrenough
2026-11-23 21:15 ` Denis Efremov (Oracle)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox