Repository navigation
Conversation
|
Is it able to fix uucore side instead of |
| // `n_skip` may not fit in a `usize` on 32-bit targets, and splice(2) | ||
| // rejects a length above `isize::MAX`. Splice at most that many bytes, | ||
| // then read and discard the rest. | ||
| let max = isize::MAX as usize; |
There was a problem hiding this comment.
This restricts performance on 64 bit target. No?
There was a problem hiding this comment.
The isize::MAX cap is gone. Each splice call now asks for at most MAX_ROOTLESS_PIPE_SIZE, and one call can't move more than a pipe buffer anyway (the broker pipe is set to that size). 64-bit doesn't lose anything. It's still one loop of splices, with no read() fallback.
| } | ||
| { | ||
| // `n_skip` may not fit in a `usize` on 32-bit targets, and splice(2) | ||
| // rejects a length above `isize::MAX`. Splice at most that many bytes, |
There was a problem hiding this comment.
Actually, we have MAX_ROOTLESS_PIPE_SIZE we could use at uucore::pipes side.
There was a problem hiding this comment.
Good idea, done. It's the chunk size now, inside uucore::pipes::discard_n_bytes.
| Ok(spliced) if spliced < n => return Ok(n_skip - spliced as u64), | ||
| Ok(spliced) | Err(spliced) => spliced as u64, | ||
| }; | ||
| let read = uucore::io::read_and_discard(curr, n_skip - spliced, SKIP_BUFFER_SIZE)?; |
There was a problem hiding this comment.
on 32-bit the remaining ~2 GiB then goes through read(), could we loop on discard_n_bytes instead?
There was a problem hiding this comment.
Yes. discard_n_bytes already looped, it just took a usize. With a u64 length and 1 MiB chunks it splices the whole skip on 32-bit too, and read() only runs when splice can't be used. I checked it on i686 under qemu. The new test fails on main and passes now.
|
Yes, I moved it there. |
| /// | ||
| /// Each splice asks for at most `MAX_ROOTLESS_PIPE_SIZE` bytes. A call never | ||
| /// moves more than one pipe buffer, and splice rejects a length above | ||
| /// `isize::MAX` (which `n` can exceed on 32-bit targets). |
There was a problem hiding this comment.
I don't think that this is needed.
There was a problem hiding this comment.
Removed in 14d3860, the doc comment on discard_n_bytes is back to the original three lines.
| #[inline] | ||
| pub fn discard_n_bytes(fd: impl AsFd, n: usize) -> Result<usize, usize> { | ||
| pub fn discard_n_bytes(fd: impl AsFd, n: u64) -> Result<u64, u64> { | ||
| let chunk = |discarded: u64| (n - discarded).min(MAX_ROOTLESS_PIPE_SIZE as u64) as usize; |
There was a problem hiding this comment.
It is sad to have such trivial helper just for 2 operations. But I hane no better idea...
There was a problem hiding this comment.
Perhaps we could do that at uucore::pipes::splice instead and restrict this hack to 32bit only to avoid using more CPU on 64 bit target.
There was a problem hiding this comment.
Done in 14d3860, the clamp is in uucore::pipes::splice now and the chunk helper is gone.
I measured it first. With a normal 64 KiB input pipe the 1 MiB chunk changed nothing (65536 splices for a 4 GiB skip, same time). With a 16 MiB input pipe it did 1024 splices instead of 64, so you were right about the extra calls.
I left the clamp on 64-bit too, because 64-bit hits the same EINVAL: head -c 10E file asks splice for more than isize::MAX and falls back to read/write. On i686 the same happens to head -c 3G today. So one min before the syscall fixes od, head and tail in one place.
"before" is db2156a, "after" is 14d3860, "main" is 2a072db.
i686 under qemu-i386
$ od -Ad -tc -j4G -N5 header payload
main : 4294967296 F I N A L
before: 4294967296 D R A F T
after : 4294967296 D R A F T
$ QEMU_STRACE=1 head -c 3G header >/dev/null # 3 GiB > isize::MAX on i686
before: splice(3,0,5,0,-1073741824,0) = -1 errno=22 (Invalid argument)
after : splice(3,0,5,0,2147483647,0) = 5
aarch64 Linux, native
$ python3 big_pipe.py | strace -e trace=splice od -An -c -j1G -N5 # 16 MiB input pipe
before: D R A F T splice calls: 1024 largest: 1048576 bytes
after: D R A F T splice calls: 64 largest: 16777216 bytes
$ strace -f -e trace=splice head -c 10E header | wc -c # 10 EiB > isize::MAX
before: splice(3, NULL, 5, NULL, 11529215046068469760, 0) = -1 EINVAL (Invalid argument) -> 5 bytes
after: splice(3, NULL, 5, NULL, 9223372036854775807, 0) = 5 -> 5 bytes
cargo fmt --check and cargo clippy -p uucore --features pipes -D warnings are clean. test_od, test_head and test_tail pass on aarch64 Linux, except the two tail permission-denied tests, which fail only because my container runs as root.
There was a problem hiding this comment.
pipe size > 1 MiB requires root priv. But it is fine to adopt maximal size. I am wondering why kernel itself cannot cap given length.
There was a problem hiding this comment.
To be honest, I couldn't work this out on my own at first. I only got it after I went through the research with claude. Here is what I found:
Only root can grow a pipe past 1 MiB, so that case is rare.
The kernel does cap it, but only after one check. splice, read and write all go through rw_verify_area, which returns EINVAL when (ssize_t)count < 0:
https://github.com/torvalds/linux/blob/v7.4-rc5/fs/read_write.c#L438-L439
Only after that is the length cut to MAX_RW_COUNT (about 2 GiB):
https://github.com/torvalds/linux/blob/v7.4-rc5/fs/splice.c#L896-L897
The syscall returns ssize_t, so a count it could not report back is rejected instead of clamped. POSIX leaves a count above SSIZE_MAX implementation-defined, and Linux picked EINVAL. read() behaves the same, which is why Rust std clamps every read to isize::MAX (READ_LIMIT in library/std/src/sys/fd/unix.rs). The min in our splice wrapper does the same.
There was a problem hiding this comment.
We sometimes use rustix::io::read by some reason. I think we don't use it with too large buf, but it should be considered.
|
Please squash commits. |
skip_in_file passed n_skip to discard_n_bytes as usize, so on a 32-bit target a 4 GiB skip became 0 and od printed the wrong bytes. discard_n_bytes now takes a u64. splice returns EINVAL for a length above isize::MAX, so the splice wrapper clamps len to it. This also lets head and tail splice counts above 2 GiB on 32-bit (and above 8 EiB on 64-bit) instead of falling back to read/write.
14d3860 to
487222b
Compare
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Fixes #16998
On Linux,
skip_in_filehands the skip todiscard_n_bytesasn_skip as usize. On a 32-bit target a 4 GiB skip turns into 0, so the 5-byteheadergets spliced for 0 bytes,odmoves on topayloadwith the full 4 GiB still to skip, and you getFINALinstead ofDRAFT.Just using
usize::MAXthere wasn't enough. When I traced it under qemu-i386,splicewith a length of0xFFFFFFFFcame back withEINVAL, anddiscard_n_bytesreports that asOk(0), which looks like EOF. So the same wrong output came out. The change:n_skipcapped atisize::MAX;n_skipgoes throughread_and_discard, as in the existingErrbranch;Okstill means the input ended, same as before.On 64-bit nothing changes for skips up to
isize::MAX.Before and after, on i686 with the sparse files from the issue:
Test:
test_skip_past_u32_max_across_inputsbuilds the issue's sparsepayloadand checks the output. It only runs on Linux and Android, since that is the only place the splice path exists. I ran it like this:--target i686-unknown-linux-muslon macOS (linked with rust-lld), then ran the test binary in alinux/386Debian container under qemu-i386 user emulation. On main the new test fails withF I N A LvsD R A F T. With the fix all 130test_odtests pass (1 ignored, unchanged).test_odtests pass, the new one included.test_odtests and 57uu_odunit tests pass.cargo fmt --checkis clean.cargo clippy -D warningsis clean foruu_odand the od tests, both on macOS and with--target i686-unknown-linux-gnu.I haven't run it on a real 32-bit kernel, only under qemu-i386 on a 64-bit one.