|
|
Subscribe / Log in / New account

Corner case

Corner case

Posted Nov 12, 2025 16:35 UTC (Wed) by abatters (✭ supporter ✭, #6932)
Parent article: The intersection of unstable pages and direct I/O

I use a lot of direct I/O code that writes to a raw partition (e.g. /dev/sdb1). One interesting corner case is if you have a single page involved in two non-overlapping direct I/Os simultaneously at different offsets. The I/Os won't conflict with each other logically, but if they add these restrictions, the kernel may incorrectly detect a conflict based on page granularity.

Example:
4k buffer (one page)
write #1: 2k @ offset 0 written to sector 100
write #2: 2k @ offset 2k written to sector 200


to post comments

Corner case

Posted Nov 12, 2025 17:42 UTC (Wed) by farnz (subscriber, #17727) [Link]

My understanding is that your corner case as shown would not trigger conflict detection, because the buffer is not changed by the write. The problem would come with two reads to different parts of the same page (at sector alignment, on a system where sectors are smaller than pages), where page granularity would detect two places changing the same page.

This is a fixable problem if it's a common false positive - once page granularity detection says "two direct I/O reads to same page", you'd add in a check for overlap there.

Corner case

Posted Nov 12, 2025 20:02 UTC (Wed) by kreijack (guest, #43513) [Link] (2 responses)

> One interesting corner case is if you have a single page involved in two non-overlapping direct I/Os simultaneously at different offsets. The I/Os won't conflict with each other logically, but if they add these restrictions, the kernel may incorrectly detect a conflict based on page granularity.

My be that I am misunderstanding something, but direct-I/O have some requirements. One of these is that the read/write size shall be multiple of the page size, and the offset must be multiple of the page size. So under direct-io is not possible to update a portion of a page, because you have to write (at least) the full-page.

Corner case

Posted Nov 12, 2025 20:20 UTC (Wed) by abatters (✭ supporter ✭, #6932) [Link]

The alignment requirements have been relaxed over the years. See the O_DIRECT section under:

https://www.man7.org/linux/man-pages/man2/open.2.html#NOTES

Corner case

Posted Nov 12, 2025 20:23 UTC (Wed) by koverstreet (✭ supporter ✭, #4296) [Link]

It's important to note that there are different alignment requirements for the buffer - "dma alignment" - and the file offset, and they both have to be checked for (via statx).

We recently ran into an application (rocksdb) that didn't work on bcachefs in largebs mode because of this - it was only checking dma alignment, not offset.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds