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

* Re: floppy: buffer_chain_size() misdirected DMA in 2.6.12..5.12 (gone since 3d86739c6343)
  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)
  0 siblings, 0 replies; 2+ messages in thread
From: Denis Efremov (Oracle) @ 2026-11-23 21:15 UTC (permalink / raw)
  To: bouldrenough, linux-block

Hello,

Thank you for the report.

On 21/11/2026 07:00, bouldrenough wrote:
> 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.

I guess what actually happens is a later page gets earlier sectors' data,
and the skipped page is marked complete without ever being filled.

> 
> It only matters where the page cache is below MAX_DMA_ADDRESS, so in
> practice machines with 16MB of RAM or less.

I think this limit applies only to x86, MAX_DMA_ADDRESS is ~0UL on sparc,
powerpc and parisc.

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.

I think it's worth to send the patch to 5.10 LTS branch. Are you interested
in preparing it or will let me do it?

I don't think backporting of 3d86739c6343 ("floppy: always use the track
buffer") is a right choice for 5.10. 5.10 doesn't have memcpy_{to,from}_page
and we should take your patch instead:
https://gist.githubusercontent.com/agmorion/bc94c0f989334e28e2f1b03ec97733a3/raw/a48235cf721a2dc50d2f47115eaf78ba9c181252/floppy-buffer-chain-v5.10.patch

If you'll decide to prepare the patch yourself, you will need to update the
comment /* (rq_for_each_segment is two nested loops ...) */ because it fails
checkpatch.pl check and will need to describe the problem in commit message
with explanation why it's better to use your stable-only fix instead of
backporting 3d86739c6343 (missing prerequisites + backport is larger),
use '[PATCH 5.10.y]' prefix for submission and send it to stable@vger.kernel.org

If we will take it to 5.10 there is a high chance such projects as CIP and
OpenELA also will take it. They still support old kernels.

Let me know if you want to prepare the submission yourself.

> 
> 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,
Denis


^ 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