Stop the NPU driver oopsing when inference is interrupted - #11
Merged
Conversation
Ctrl-C during an rknn run takes the kernel down: Internal error: Oops - BUG: 0 [#1] THUMB2 PC is at dma_buf_release+0xa/0x58 dma_buf_release from __dentry_kill+0x73/0xb6 ... task_work_run from do_exit+0x235/0x570 which is BUG_ON(dmabuf->vmapping_counter) at the top of dma_buf_release(): a dma-buf reached its last reference while the kernel still had it vmapped. RKNPU_MEM_CREATE gives away the buffer it allocates. dma_buf_fd() does not take a reference of its own, so the fd it installs takes over the one from rk_dma_heap_buffer_alloc(), and the driver goes on to attach and dma_buf_vmap() a buffer it holds no reference to. The runtime's own teardown hides this, because RKNPU_MEM_DESTROY unmaps before the fd goes. A killed process never runs it, and do_exit() drops the mapping in exit_mm() before it closes /dev/rknpu in exit_files(). Take a reference for the driver's own use and drop it unconditionally on teardown, so a buffer outlives the mapping the driver has on it however the process ends. Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ctrl-C during an rknn run takes the kernel down with
BUG_ON(dmabuf->vmapping_counter)at the top ofdma_buf_release(): a dma-buf hit its last reference while the kernel still had it vmapped. The register dump agrees,dma_buf_release+0xais exactly thatBUG_ONon this tree, the dma_buf'svmapping_counteris 1 and its attachment list is still non-empty, so nothing had torn the buffer down.RKNPU_MEM_CREATEwithargs.handle == 0(whatlibrknnmrtuses, which is whyrk_dma_heap_cma=sizes the pool) allocates the buffer and then gives its only reference away.dma_buf_fd()installs the file without taking a reference of its own, so the fd takes over the onerk_dma_heap_buffer_alloc()returned, and the driver goes on todma_buf_attach()anddma_buf_vmap()a buffer it holds nothing on. Both teardown paths agree with the create path and skip the put when->owneris set:That is safe only while userspace calls
RKNPU_MEM_DESTROYbefore the last fd and mapping go, which is what the runtime's own teardown does. A killed process never gets there, anddo_exit()tears the address space down inexit_mm()before it closes the fd table inexit_files(), so a buffer the runtime mmap()ed and whose fd it had already closed loses its last reference while the driver still has a kernel mapping on it. Had theBUG_ONnot fired,rknpu_release()would have gone on to vunmap and detach a freeddma_buf.The fix is one
get_dma_buf()after the fd is installed, plus dropping that reference unconditionally inrknpu_mem_destroy_ioctl(),rknpu_release()and the create error path:The count is then one per
rknpu_mem_objecthowever the buffer was obtained, and the buffer outlives the driver's mapping of it no matter how the process ends.->ownerstops deciding anything but is left in place: it is documented inrknpu_mem.h, and deleting a member of a vendor struct is churn whose only effect here would be to make the patch likelier to stop applying.Two smaller things fall out of the same change.
dma_buf_attach()ordma_buf_map_attachment()failing after the fd was installed used to free the buffer out from under an fd userspace had already been given; with our own reference the error path drops ours and the fd keeps its own. And thecopy_to_user()failure path now only vunmaps if something was vmapped, since withoutRKNPU_MEM_KERNEL_MAPPINGtheiosys_mapis still zeroed anddma_buf_vunmap()opens withBUG_ON(iosys_map_is_null(&dmabuf->vmap_ptr)).Nothing to do with #10; the 594 MHz clocking is not involved. Verified that both patches apply in order to
drivers/rknpuat the pinnedKERNEL_REF, andmake checkpasses. Not yet run on hardware, the test is awhile true; do ./yolo_detect ...; doneand a Ctrl-C.Need help on this PR? Tag
@codesmithwith what you need. Autofix is enabled.