Skip to content

Fix operator precedence and integer width in nv_dma_buf_mmap#1072

Open
maycuatroi1 wants to merge 1 commit intoNVIDIA:mainfrom
maycuatroi1:fix/dmabuf-mmap-operator-precedence
Open

Fix operator precedence and integer width in nv_dma_buf_mmap#1072
maycuatroi1 wants to merge 1 commit intoNVIDIA:mainfrom
maycuatroi1:fix/dmabuf-mmap-operator-precedence

Conversation

@maycuatroi1
Copy link

@maycuatroi1 maycuatroi1 commented Mar 19, 2026

Summary

Fix two bugs in nv_dma_buf_mmap() (kernel-open/nvidia/nv-dmabuf.c):

1. Operator precedence error (line 1344)

The outer for-loop condition:

for (; i < (priv->num_objects && (addr < vma->vm_end)); i++)

evaluates && first, producing a boolean 0 or 1. The loop runs at most once (i < 1), regardless of num_objects.

For multi-object DMA-buf exports, only the first object's ranges are mapped. The function returns success despite the incomplete mapping. Accessing the unmapped portion of the VMA causes SIGBUS.

Fix:

for (; (i < priv->num_objects) && (addr < vma->vm_end); i++)

2. Integer truncation in total_skip_size (line 1233)

total_skip_size is NvU32 but accumulates NvU64 range sizes. For DMA-bufs larger than 4 GB the offset wraps, causing incorrect range lookups in the page-mapping loop.

Fix: widen from NvU32 to NvU64.

Verification

Operator precedence bug confirmed with standalone C reproduction:

num_objects  buggy_iters     correct_iters   match?
----------  -----------     -------------   ------
1            1               1               OK
5            1               5               BUG!
100          1               100             BUG!

Integer truncation confirmed:

range[0]=0x100000000  NvU32=0x00000000  NvU64=0x0000000100000000  OVERFLOW!

Test plan

  • Verify multi-object DMA-buf export + mmap maps all objects correctly
  • Verify DMA-buf exports with total size > 4 GB compute correct offsets
  • Verify single-object DMA-buf mmap behavior is unchanged

Fix two bugs in nv_dma_buf_mmap():

1. Operator precedence error in the outer for-loop condition at line 1344.
   The expression `i < (priv->num_objects && (addr < vma->vm_end))` evaluates
   the `&&` first, producing a boolean (0 or 1). This means the loop body
   executes at most once (when i == 0), regardless of num_objects.

   For multi-object DMA-buf exports, only the first object's ranges are
   mapped. The function returns success despite the incomplete mapping.

   Fix: `(i < priv->num_objects) && (addr < vma->vm_end)`

2. Integer truncation in `total_skip_size` (NvU32) which accumulates NvU64
   range sizes. For DMA-bufs larger than 4GB, the offset calculation wraps
   around, causing incorrect range lookups.

   Fix: widen `total_skip_size` from NvU32 to NvU64.
@CLAassistant
Copy link

CLAassistant commented Mar 19, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants