Skip to content

Commit c7940c8

Browse files
KapJ1coHDanilo Krummrich
authored andcommitted
gpu: nova-core: fix stack overflow in GSP memory allocation
The `Cmdq::new` function was allocating a `PteArray` struct on the stack and was causing a stack overflow with 8216 bytes. Modify the `PteArray` to calculate and write the Page Table Entries directly into the coherent DMA buffer one-by-one. This reduces the stack usage quite a lot. Reported-by: Gary Guo <gary@garyguo.net> Closes: https://rust-for-linux.zulipchat.com/#narrow/channel/509436-Nova/topic/.60Cmdq.3A.3Anew.60.20uses.20excessive.20stack.20size/near/570375549 Link: https://lore.kernel.org/rust-for-linux/CANiq72mAQxbRJZDnik3Qmd4phvFwPA01O2jwaaXRh_T+2=L-qA@mail.gmail.com/ Fixes: f38b4f1 ("gpu: nova-core: Create initial Gsp") Acked-by: Alexandre Courbot <acourbot@nvidia.com> Signed-off-by: Tim Kovalenko <tim.kovalenko@proton.me> Link: https://patch.msgid.link/20260309-drm-rust-next-v4-4-4ef485b19a4c@proton.me [ * Use PteArray::entry() in LogBuffer::new(), * Add TODO comment to use IoView projections once available, * Add PTE_ARRAY_SIZE constant to avoid duplication. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 4da879a commit c7940c8

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

drivers/gpu/nova-core/gsp.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,12 @@ struct PteArray<const NUM_ENTRIES: usize>([u64; NUM_ENTRIES]);
4747
unsafe impl<const NUM_ENTRIES: usize> AsBytes for PteArray<NUM_ENTRIES> {}
4848

4949
impl<const NUM_PAGES: usize> PteArray<NUM_PAGES> {
50-
/// Creates a new page table array mapping `NUM_PAGES` GSP pages starting at address `start`.
51-
fn new(start: DmaAddress) -> Result<Self> {
52-
let mut ptes = [0u64; NUM_PAGES];
53-
for (i, pte) in ptes.iter_mut().enumerate() {
54-
*pte = start
55-
.checked_add(num::usize_as_u64(i) << GSP_PAGE_SHIFT)
56-
.ok_or(EOVERFLOW)?;
57-
}
58-
59-
Ok(Self(ptes))
50+
/// Returns the page table entry for `index`, for a mapping starting at `start`.
51+
// TODO: Replace with `IoView` projection once available.
52+
fn entry(start: DmaAddress, index: usize) -> Result<u64> {
53+
start
54+
.checked_add(num::usize_as_u64(index) << GSP_PAGE_SHIFT)
55+
.ok_or(EOVERFLOW)
6056
}
6157
}
6258

@@ -86,16 +82,22 @@ impl LogBuffer {
8682
NUM_PAGES * GSP_PAGE_SIZE,
8783
GFP_KERNEL | __GFP_ZERO,
8884
)?);
89-
let ptes = PteArray::<NUM_PAGES>::new(obj.0.dma_handle())?;
85+
86+
let start_addr = obj.0.dma_handle();
9087

9188
// SAFETY: `obj` has just been created and we are its sole user.
92-
unsafe {
93-
// Copy the self-mapping PTE at the expected location.
89+
let pte_region = unsafe {
9490
obj.0
95-
.as_slice_mut(size_of::<u64>(), size_of_val(&ptes))?
96-
.copy_from_slice(ptes.as_bytes())
91+
.as_slice_mut(size_of::<u64>(), NUM_PAGES * size_of::<u64>())?
9792
};
9893

94+
// Write values one by one to avoid an on-stack instance of `PteArray`.
95+
for (i, chunk) in pte_region.chunks_exact_mut(size_of::<u64>()).enumerate() {
96+
let pte_value = PteArray::<0>::entry(start_addr, i)?;
97+
98+
chunk.copy_from_slice(&pte_value.to_ne_bytes());
99+
}
100+
99101
Ok(obj)
100102
}
101103
}

drivers/gpu/nova-core/gsp/cmdq.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ struct Msgq {
159159
#[repr(C)]
160160
struct GspMem {
161161
/// Self-mapping page table entries.
162-
ptes: PteArray<{ GSP_PAGE_SIZE / size_of::<u64>() }>,
162+
ptes: PteArray<{ Self::PTE_ARRAY_SIZE }>,
163163
/// CPU queue: the driver writes commands here, and the GSP reads them. It also contains the
164164
/// write and read pointers that the CPU updates.
165165
///
@@ -172,6 +172,10 @@ struct GspMem {
172172
gspq: Msgq,
173173
}
174174

175+
impl GspMem {
176+
const PTE_ARRAY_SIZE: usize = GSP_PAGE_SIZE / size_of::<u64>();
177+
}
178+
175179
// SAFETY: These structs don't meet the no-padding requirements of AsBytes but
176180
// that is not a problem because they are not used outside the kernel.
177181
unsafe impl AsBytes for GspMem {}
@@ -201,7 +205,13 @@ impl DmaGspMem {
201205

202206
let gsp_mem =
203207
CoherentAllocation::<GspMem>::alloc_coherent(dev, 1, GFP_KERNEL | __GFP_ZERO)?;
204-
dma_write!(gsp_mem, [0]?.ptes, PteArray::new(gsp_mem.dma_handle())?);
208+
209+
let start = gsp_mem.dma_handle();
210+
// Write values one by one to avoid an on-stack instance of `PteArray`.
211+
for i in 0..GspMem::PTE_ARRAY_SIZE {
212+
dma_write!(gsp_mem, [0]?.ptes.0[i], PteArray::<0>::entry(start, i)?);
213+
}
214+
205215
dma_write!(
206216
gsp_mem,
207217
[0]?.cpuq.tx,

0 commit comments

Comments
 (0)