Repository navigation
accel/ane: h16: check admission before importing the HWX and account the copy - #3194
Conversation
…the copy H16_LOAD copied its four sections and the whole HWX, up to ANE_H16_HWX_MAX, before it checked whether the file could load at all. A LOAD on a file that already had a program allocated, copied, validated and hashed a full HWX only to fail with -EBUSY afterwards, any number of LOADs could do that at once on one file, and none of that memory was charged to the caller. Admit the LOAD first, under the file and node locks. A file that has a program, or another LOAD in flight, now fails with -EBUSY before anything is read, and a program lost across system sleep is released there, as before. The admitted LOAD marks the file loading, then reads its sections and HWX with no lock of ours held, as before, so a file has at most one import in flight. Charge the copies to the caller's memory cgroup: kvmalloc() with __GFP_ACCOUNT and copy_from_user() replace vmemdup_user() for the HWX, and kmalloc() with __GFP_ACCOUNT replaces memdup_user() for the sections. A loaded program's HWX snapshot stays charged to the cgroup that loaded it for the program's lifetime. ANE_H16_HWX_MAX stays at 256 MiB plus 1 MiB: the validator accepts one segment of up to 256 MiB, and the cap is that segment plus room for the load commands. Signed-off-by: Dj <[email protected]>
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
h16-load-admission.py loads one H16G or H17 HWX on one open file of the accel node and checks that an oversized LOAD fails with EINVAL; that the valid LOAD grows the caller's cgroup kernel memory by at least the HWX size; and that a second LOAD fails with EBUSY, both one whose pointers all fault, so admission comes before any copy, and a valid one, which leaves kernel memory unchanged. Run it in a cgroup of its own (systemd-run --scope) so the kernel-memory numbers are the test's only. Signed-off-by: Dj <[email protected]>
|
Verdict at Blockers: none Checked:
Evidence
|
|
af175d2 J713 (M4 Air, T8132/H16, batched 0-diff merge onto main) PASS: ANE bound (/dev/accel/accel0, H16 T8132 27.2 26B123 J713 accepted), MobileNet-v2 top-1 16/16 PASS pre-sleep (p50 0.332 ms vs 0.322 ms fleet85) and post-s2idle 1/1 (p50 0.334 ms); h16-load-admission.py on MobileNet HWX (8.58 MiB) PASS 5/5 (EINVAL oversized, +16 MiB cgroup charge on LOAD, EBUSY on faulting 2nd LOAD vs EFAULT on fleet85, EBUSY/+0 on valid 2nd LOAD, -16 MiB on close; 48 KiB add128.hwx passes admission EBUSY/EINVAL but sits below 16K-page per-CPU memcg batch), 0 ANE errors, Enforcing, 0 failed; fleet85 restored. |
|
J714s (MacBook Pro 14" M5 Pro, G17S B1, H17 ANE) row at
|
The charge check compared the cgroup's kernel memory before and after a valid LOAD. With a small HWX such as add-128 (48 KiB) that reads +0 on every kernel: memory cgroups batch charges per CPU, and memory.stat is flushed lazily. Measure a 64 MiB LOAD of zero bytes instead, read from a private anonymous mapping that was never written, so that only the kernel's copy is charged. The import fails validation, and the cgroup's memory.peak, reset for the test's file descriptor, shows whether the copy was charged while the kernel held it. A second such LOAD on the busy file must fail with EBUSY without raising the peak, since admission now comes before the copy. Signed-off-by: DjDeveloperr <[email protected]>
There was a problem hiding this comment.
Verdict: MERGE at c2addc86 (once cancelled KUnit CI checks are rerun green)
Blockers: none (code and two-generation hardware rows are ready; rerun the 12 cancelled KUnit checks before merge).
Checked:
drivers/accel/ane/ane_h16_accel.c:39-185: verifiedane_h16_load_admit()checksf->loadingandf->program(releasing sleep-lost programs) underf->lockanda->lock, setsf->loading = true, and drops both locks before copying user memory; verified every error/success path resetsf->loading = falseunderf->lockwith balanced locking and cleanup ofsnapshot/aux.drivers/accel/ane/ane_h16_accel.c:22andane_h16_hwx.c:304: verified both section copies (kmalloc) and HWX snapshots (kvmalloc) charge the caller's memory cgroup viaGFP_USER | __GFP_ACCOUNT | __GFP_NOWARNand free cleanly oncopy_from_userfault.ane_h16_accel_test.c,ane_h16_hwx_test.c, andh16-load-admission.py: verified KUnit and selftest coverage.- Verified hardware rows on both J713 (M4/H16, MobileNet-v2 16/16 + post-s2idle + selftest 5/5) and J714s (M5/H17, add-128 16/16 + post-sleep + selftest 5/5).
Evidence
- Diff reviewed at
c2addc86(all 6 files, 622 lines). - CI at review time: 7 passed, 0 failed, 12 cancelled.
|
Merge-train status at |
|
Merging under the 6-point rule (Ryan 09:29, permanent), at |
What:
ANE_H16_LOADnow checks whether the file can load before it reads anything from userspace, and the kernel's copies of the HWX and the four sections are charged to the caller's memory cgroup. Before, a LOAD copied, validated and hashed the whole HWX (up to 257 MiB) first, and only then failed with -EBUSY if the file already had a program. Any number of LOADs could do that at once on one file, and none of that memory was charged to the caller.Tested: J713 (M4, H16): MobileNet-v2 16/16 before and after s2idle, and
h16-load-admission.py5/5: the LOAD charge shows in the cgroup, a second LOAD with faulting pointers gets EBUSY (stock gives EFAULT), and the charge is returned on close; 0 ANE errors. J714s (M5, H17): add-128 16/16 exact, the same EBUSY-vs-EFAULT result, and the ANE resumes after deep sleep with 0 ANE errors or warnings. Both rows ran at af175d2. Its kernel commit is identical to this head's; only the selftest has changed since. J714s then ran this head's selftest on the same kernel and passed: 64.0 MiB charged and EBUSY. Stock fails it: nothing charged, and EFAULT. CI KUnitane-bootpassed 282/282 at af175d2, including the three new cases. checkpatch--strictis clean, and this head merges cleanly with #2876 (git merge-tree).Risk: two-way door · every Mac whose ANE uses the H16 backend: the M3 family (H15), the M4 family (H16) and the M5 family (H17)
Not yet: nothing
Details
LOAD now runs in three steps:
ane_h16_load_admit()), under the file lock and the node lock as before. A file that has a program from this firmware boot, or that has another LOAD in flight, fails with -EBUSY. An O_NONBLOCK LOAD during system sleep fails with -EAGAIN. A program lost across system sleep is released here, before the copy instead of after it. An admitted LOAD marks the fileloading.mmap()of the node takes the file and node locks under the process mmap lock, so holding the file lock across a user fault would invert that order.loadingis cleared on every path.The copies are now charged to the caller's memory cgroup:
kvmalloc(GFP_USER | __GFP_ACCOUNT | __GFP_NOWARN)pluscopy_from_user()replacesvmemdup_user()for the HWX.kmalloc(GFP_USER | __GFP_ACCOUNT | __GFP_NOWARN)pluscopy_from_user()replacesmemdup_user()for the sections.These are the flags those helpers use, plus
__GFP_ACCOUNT. A loaded program keeps its HWX snapshot, so the charge stays with the cgroup that loaded it for the program's lifetime.The size cap stays at 256 MiB plus 1 MiB (
ANE_H16_HWX_MAX). The validator accepts a single segment of up to 256 MiB, and the cap is one such segment plus room for the load commands. A lower cap would refuse containers the validator accepts, and we have no measured model sizes to pick a lower value from.There is no device-wide cap on imports in flight. With one, a single client that stalls its imports would block every other client's LOAD. Instead, each import is charged to its caller's cgroup, and each file has at most one in flight.
What userspace can see: only which error comes back from a LOAD that fails anyway. A LOAD on a busy file now returns -EBUSY even when its pointers are bad, where it used to return -EFAULT or -EINVAL. The documented behavior, -EBUSY for a second LOAD, is unchanged.
How this composes with #2876: #2876 changes EXECUTE (it pins buffers before locking) and the validator. This PR changes the order of LOAD and its copy helpers. The hunks are separate, and
git merge-treeof this head with #2876's headcd4666bbd23eis clean. In the merged tree, both LOAD and EXECUTE read user memory with no lock of ours held. Either PR can merge first.New KUnit cases:
ane-h16-accel/ane_h16_load_admission_test:ane-h16-accel/ane_h16_load_size_test:ANE_H16_HWX_MAX + 1bytes and one of 31 bytes fail with -EINVAL after the sections are read;ane-h16-hwx/hwx_import_user_test:New selftest,
tools/testing/selftests/drivers/ane/h16-load-admission.py(H16G or H17). Run it in its own cgroup (systemd-run --scope -p MemoryAccounting=yes). On one open file of the node, it checks that:memory.peakrose by at least 48 MiB while the kernel held its copy;The 64 MiB come from a private mapping that was never written. Userspace reads them from the zero page, so only the kernel's copy is charged. A small HWX can't show the charge, because memory cgroups batch charges per CPU.
Thanks to erikwb for the report and the analysis.