misc: fastrpc: Add missing bug fixes
v5 cover misc: fastrpc: Add missing bug fixes
Add missing bug fixes in memory areas. This patch series fixes multiple memory handling issues in the FastRPC driver, primarily around the Audio PD remote heap.
The Audio PD uses a reserved memory-region that is shared between HLOS and the DSP. Allocating and freeing this memory from userspace is unsafe, as the kernel cannot reliably determine when the DSP has finished using the buffers.
To address this, the entire reserved memory-region for the Audio PD is now fully assigned to the DSP during remoteproc boot-up, and its lifetime is tied to the rpmsg channel.
Patch [v4]: https://lore.kernel.org/all/[email protected]/
Change in v5:
- Split reserved-memory handling into separate patches
Change in v4:
- Fail Audio PD static process creation when no reserved memory-region is present, instead of silently proceeding
Change in v3:
- Adjusted the order of the series, placing NULL check changes that are not bug fixes at the end
- Modified the commit message to describe the bug background in detail
- Switch buf->list_lock back to fl->lock
- Add locking to the operation of audio_init_mem
Changes in v2:
- Remove the if check outside fastrpc_buf_free
- Store the spinlock pointer in the struct fastrpc_buf instead
- Allocate entire reserved memory to audio PD through remote heap
Ekansh Gupta (3):
misc: fastrpc: Fix initial memory allocation for Audio PD memory pool
misc: fastrpc: Remove buffer from list prior to unmap operation
misc: fastrpc: Allow fastrpc_buf_free() to accept NULL
Jianping Li (2):
misc: fastrpc: Fail Audio PD init when reserved memory is missing
misc: fastrpc: Allocate entire reserved memory for Audio PD in probe
drivers/misc/fastrpc.c | 135 ++++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 64 deletions(-)
Signature · 1 line
2.43.0
PATCH v5 1/5 misc: fastrpc: Fix initial memory allocation for Audio PD memory pool
From: Ekansh Gupta <[email protected]>
The initial buffer allocated for the Audio PD memory pool is never added to the pool because pageslen is set to 0. As a result, the buffer is not registered with Audio PD and is never used, causing a memory leak. Audio PD immediately falls back to allocating memory from the remote heap since the pool starts out empty.
Fix this by setting pageslen to 1 so that the initially allocated buffer is correctly registered and becomes part of the Audio PD memory pool.
--- drivers/misc/fastrpc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/misc/fastrpc.c +4 -4
@@ -1324,7 +1324,9 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, err =PTR_ERR (name); goto err; } - + inbuf.client_id = fl->client_id; + inbuf.namelen = init.namelen; + inbuf.pageslen =0 ;if (!fl->cctx->remote_heap) { err =fastrpc_remote_heap_alloc (fl, fl->sctx->dev, init.memlen, &fl->cctx->remote_heap);
@@ -1347,12 +1349,10 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, goto err_map; } scm_done = true; + inbuf.pageslen =1 ; } } - inbuf.client_id = fl->client_id; - inbuf.namelen = init.namelen; - inbuf.pageslen =0 ; fl->pd =USER_PD ; args[0 ].ptr = (u64)(uintptr_t)&inbuf;
Signature · 1 line
2.43.0
Re: [PATCH v5 1/5] misc: fastrpc: Fix initial memory allocation for Audio PD memory pool
Quoted 8 lines · Jianping Li
From: Ekansh Gupta <[email protected]>
The initial buffer allocated for the Audio PD memory pool is never added to the pool because pageslen is set to 0. As a result, the buffer is not registered with Audio PD and is never used, causing a memory leak. Audio PD immediately falls back to allocating memory from the remote heap since the pool starts out empty.
Quoted 7 lines
Fix this by setting pageslen to 1 so that the initially allocated buffer is correctly registered and becomes part of the Audio PD memory pool.
Fixes 0871561055e66 ("misc: fastrpc: Add support for audiopd")Co-developed-by Ekansh Gupta <[email protected]>Signed-off-by Ekansh Gupta <[email protected]>
If it's From:Ekansh, it can't be CDB: Ekansh. How can Ekansh co-develop the patch with himself?
Quoted 37 lines
Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)drivers/misc/fastrpc.c +4
-4@@ -1324,7 +1324,9 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, err =PTR_ERR (name); goto err; } - + inbuf.client_id = fl->client_id; + inbuf.namelen = init.namelen; + inbuf.pageslen =0 ;if (!fl->cctx->remote_heap) { err =fastrpc_remote_heap_alloc (fl, fl->sctx->dev, init.memlen, &fl->cctx->remote_heap);@@ -1347,12 +1349,10 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, goto err_map; } scm_done = true; + inbuf.pageslen =1 ; } } - inbuf.client_id = fl->client_id; - inbuf.namelen = init.namelen; - inbuf.pageslen =0 ; fl->pd =USER_PD ; args[0 ].ptr = (u64)(uintptr_t)&inbuf;Signature · 1 line
2.43.0
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v5 1/5] misc: fastrpc: Fix initial memory allocation for Audio PD memory pool
Quoted 12 lines · Dmitry Baryshkov
Quoted 11 lines · Jianping Li
From: Ekansh Gupta <[email protected]>
The initial buffer allocated for the Audio PD memory pool is never added to the pool because pageslen is set to 0. As a result, the buffer is not registered with Audio PD and is never used, causing a memory leak. Audio PD immediately falls back to allocating memory from the remote heap since the pool starts out empty.
Fix this by setting pageslen to 1 so that the initially allocated buffer is correctly registered and becomes part of the Audio PD memory pool.
Quoted 6 lines
Quoted 4 lines
Fixes 0871561055e66 ("misc: fastrpc: Add support for audiopd")Co-developed-by Ekansh Gupta <[email protected]>Signed-off-by Ekansh Gupta <[email protected]>If it's From:Ekansh, it can't be CDB: Ekansh. How can Ekansh co-develop the patch with himself?
I will delete CBD:Ekansh.
Quoted 38 lines
Quoted 38 lines
Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)drivers/misc/fastrpc.c +4
-4@@ -1324,7 +1324,9 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, err =PTR_ERR (name); goto err; } - + inbuf.client_id = fl->client_id; + inbuf.namelen = init.namelen; + inbuf.pageslen =0 ;if (!fl->cctx->remote_heap) { err =fastrpc_remote_heap_alloc (fl, fl->sctx->dev, init.memlen, &fl->cctx->remote_heap);@@ -1347,12 +1349,10 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, goto err_map; } scm_done = true; + inbuf.pageslen =1 ; } } - inbuf.client_id = fl->client_id; - inbuf.namelen = init.namelen; - inbuf.pageslen =0 ; fl->pd =USER_PD ; args[0 ].ptr = (u64)(uintptr_t)&inbuf;Signature · 1 line
2.43.0
PATCH v5 2/5 misc: fastrpc: Remove buffer from list prior to unmap operation
In reply to Jianping LiFrom: Ekansh Gupta <[email protected]>
fastrpc_req_munmap_impl() is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry before calling unmap operation.
--- drivers/misc/fastrpc.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/misc/fastrpc.c +15 -6
@@ -1863,9 +1863,6 @@ static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf * &args[0]);if (!err) {dev_dbg (dev,"unmmap\tpt 0x%09lx OK\n" , buf->raddr); -spin_lock (&fl->lock); -list_del (&buf->node); -spin_unlock (&fl->lock);fastrpc_buf_free (buf); }else {dev_err (dev,"unmmap\tpt 0x%09lx ERROR\n" , buf->raddr);
@@ -1879,6 +1876,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)struct fastrpc_buf *buf =NULL , *iter, *b;struct fastrpc_req_munmap req;struct device *dev = fl->sctx->dev; +int err;if (copy_from_user (&req, argp,sizeof (req)))return -EFAULT ;
@@ -1886,6 +1884,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)spin_lock (&fl->lock);list_for_each_entry_safe (iter, b, &fl->mmaps, node) {if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) { +list_del (&iter->node); buf = iter;break ; }
@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)return -EINVAL ; } -return fastrpc_req_munmap_impl (fl, buf); + err =fastrpc_req_munmap_impl (fl, buf); +if (err) { +spin_lock (&fl->lock); +list_add_tail (&buf->node, &fl->mmaps); +spin_unlock (&fl->lock); + } + +return err; }static int fastrpc_req_mmap (struct fastrpc_user *fl,char __user *argp)
@@ -1989,14 +1995,17 @@ static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)if (copy_to_user ((void __user *)argp, &req,sizeof (req))) { err = -EFAULT ; - goto err_assign; + goto err_copy; }dev_dbg (dev,"mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n" , buf->raddr, buf->size);return 0 ; - +err_copy: +spin_lock (&fl->lock); +list_del (&buf->node); +spin_unlock (&fl->lock); err_assign:fastrpc_req_munmap_impl (fl, buf);
Signature · 1 line
2.43.0
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 6 lines · Jianping Li
From: Ekansh Gupta <[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry before
How can it remove the entry from the list?
Quoted 42 lines
calling unmap operation.
Fixes 2419e55e532de ("misc: fastrpc: add mmap/unmap support")Co-developed-by Ekansh Gupta <[email protected]>Signed-off-by Ekansh Gupta <[email protected]>Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)drivers/misc/fastrpc.c +2
-3@@ -1863,9 +1863,6 @@ static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf * &args[0]);if (!err) {dev_dbg (dev,"unmmap\tpt 0x%09lx OK\n" , buf->raddr); -spin_lock (&fl->lock); -list_del (&buf->node); -spin_unlock (&fl->lock);fastrpc_buf_free (buf); }else {dev_err (dev,"unmmap\tpt 0x%09lx ERROR\n" , buf->raddr);@@ -1879,6 +1876,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)struct fastrpc_buf *buf =NULL , *iter, *b;struct fastrpc_req_munmap req;struct device *dev = fl->sctx->dev; +int err;if (copy_from_user (&req, argp,sizeof (req)))return -EFAULT ;@@ -1886,6 +1884,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)spin_lock (&fl->lock);list_for_each_entry_safe (iter, b, &fl->mmaps, node) {if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) { +list_del (&iter->node); buf = iter;break ; }
drivers/misc/fastrpc.c static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1905
Quoted 10 lines
drivers/misc/fastrpc.c +6
-1@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)return -EINVAL ; } -return fastrpc_req_munmap_impl (fl, buf); + err =fastrpc_req_munmap_impl (fl, buf); +if (err) { +spin_lock (&fl->lock); +list_add_tail (&buf->node, &fl->mmaps); +spin_unlock (&fl->lock); + }
Is it expected that userspace tries to unmap it again? Or why is it being added to the list?
Quoted 12 lines
drivers/misc/fastrpc.c +3
-1+ +return err; }static int fastrpc_req_mmap (struct fastrpc_user *fl,char __user *argp)@@ -1989,14 +1995,17 @@ static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)if (copy_to_user ((void __user *)argp, &req,sizeof (req))) { err = -EFAULT ; - goto err_assign; + goto err_copy; }
drivers/misc/fastrpc.c static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp) · line 2008
Quoted 10 lines
drivers/misc/fastrpc.c +4
-1dev_dbg (dev,"mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n" , buf->raddr, buf->size);return 0 ; - +err_copy: +spin_lock (&fl->lock); +list_del (&buf->node); +spin_unlock (&fl->lock);
This is a separate fix.
Quoted 6 lines
drivers/misc/fastrpc.c +0
-0err_assign:fastrpc_req_munmap_impl (fl, buf);Signature · 1 line
2.43.0
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
Quoted 46 lines
Quoted 46 lines
calling unmap operation.
Fixes 2419e55e532de ("misc: fastrpc: add mmap/unmap support")Co-developed-by Ekansh Gupta<[email protected]>Signed-off-by Ekansh Gupta<[email protected]>Signed-off-by Jianping Li<[email protected]>--- drivers/misc/fastrpc.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)drivers/misc/fastrpc.c +2
-3@@ -1863,9 +1863,6 @@ static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf * &args[0]);if (!err) {dev_dbg (dev,"unmmap\tpt 0x%09lx OK\n" , buf->raddr); -spin_lock (&fl->lock); -list_del (&buf->node); -spin_unlock (&fl->lock);fastrpc_buf_free (buf); }else {dev_err (dev,"unmmap\tpt 0x%09lx ERROR\n" , buf->raddr);@@ -1879,6 +1876,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)struct fastrpc_buf *buf =NULL , *iter, *b;struct fastrpc_req_munmap req;struct device *dev = fl->sctx->dev; +int err;if (copy_from_user (&req, argp,sizeof (req)))return -EFAULT ;@@ -1886,6 +1884,7 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)spin_lock (&fl->lock);list_for_each_entry_safe (iter, b, &fl->mmaps, node) {if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) { +list_del (&iter->node); buf = iter;break ; }@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)return -EINVAL ; }
Quoted 9 lines
drivers/misc/fastrpc.c static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1905
Quoted 7 lines
drivers/misc/fastrpc.c +6
-1-return fastrpc_req_munmap_impl (fl, buf); + err =fastrpc_req_munmap_impl (fl, buf); +if (err) { +spin_lock (&fl->lock); +list_add_tail (&buf->node, &fl->mmaps); +spin_unlock (&fl->lock); + }Is it expected that userspace tries to unmap it again? Or why is it being added to the list?
User process can call unmap and fastrpc library won't call the unmap again. Fastrpc driver will free up this list during fastrpc user-free.
Quoted 17 lines
Quoted 17 lines
drivers/misc/fastrpc.c +3
-1+ +return err; }static int fastrpc_req_mmap (struct fastrpc_user *fl,char __user *argp)@@ -1989,14 +1995,17 @@ static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)if (copy_to_user ((void __user *)argp, &req,sizeof (req))) { err = -EFAULT ; - goto err_assign; + goto err_copy; }dev_dbg (dev,"mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n" , buf->raddr, buf->size);
Quoted 7 lines
drivers/misc/fastrpc.c static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp) · line 2008
Quoted 6 lines
drivers/misc/fastrpc.c +4
-1return 0 ; - +err_copy: +spin_lock (&fl->lock); +list_del (&buf->node); +spin_unlock (&fl->lock);This is a separate fix.
ACK.
I'll send it as a separate pacth.
Quoted 7 lines
Quoted 7 lines
drivers/misc/fastrpc.c +0
-0err_assign:fastrpc_req_munmap_impl (fl, buf);Signature · 1 line
2.43.0
Greg KH
maintainerRe: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 5 lines · Jianping Li
Quoted 3 lines · Dmitry Baryshkov
Quoted 2 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
multiple threads sharing the same file descriptor is a horrible userspace bug. If you do that, you get what you deserve :)
thanks,
greg k-h
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 9 lines · Greg KH
Quoted 8 lines · Jianping Li
Quoted 7 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry before
Quoted 8 lines
Quoted 2 lines
Quoted 1 line
How can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
multiple threads sharing the same file descriptor is a horrible userspace bug. If you do that, you get what you deserve :)
thanks,
greg k-h
I agree that concurrent unmap on the same buffer from multiple threads is a userspace bug.
However, the fastrpc driver is exposed via ioctl, and any userspace (including any random apk) can map directly and then call multiple unmap.
This patch is not intended to support incorrect userspace usage, but to ensure that the driver remains robust against invalid or racy inputs.
Thanks,
Jianping.
Greg KH
maintainerRe: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 18 lines · Jianping Li
Quoted 16 lines · Greg KH
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
multiple threads sharing the same file descriptor is a horrible userspace bug. If you do that, you get what you deserve :)
thanks,
Quoted 7 lines
Quoted 1 line
greg k-h
I agree that concurrent unmap on the same buffer from multiple threads is a userspace bug.
However, the fastrpc driver is exposed via ioctl, and any userspace (including any random apk) can map directly and then call multiple unmap.
So there is no userspace selinux permissions settings on the fasterpc device node at all?
Quoted 3 lines
This patch is not intended to support incorrect userspace usage, but to ensure that the driver remains robust against invalid or racy inputs.
Isn't control to this device an "admin only" thing?
thanks,
greg k-h
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 27 lines · Greg KH
Quoted 15 lines · Jianping Li
Quoted 14 lines · Greg KH
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
multiple threads sharing the same file descriptor is a horrible userspace bug. If you do that, you get what you deserve :)
Quoted 8 lines
Quoted 3 lines
thanks,
greg k-h
I agree that concurrent unmap on the same buffer from multiple threads is a userspace bug.
However, the fastrpc driver is exposed via ioctl, and any userspace (including any random apk) can map directly and then call multiple unmap.
So there is no userspace selinux permissions settings on the fasterpc device node at all?
Quoted 8 lines
Quoted 3 lines
This patch is not intended to support incorrect userspace usage, but to ensure that the driver remains robust against invalid or racy inputs.
Isn't control to this device an "admin only" thing?
thanks,
greg k-h
Hi Greg,
Thanks for the feedback.
Yes there is sepolicy.
I’ll collect more details around the expected userspace model and access restrictions for this interface, and better justify whether this needs kernel-side handling.
For now, I’ll drop this patch from the current series and revisit it with additional context in a separate submission.
Thanks,
Jianping
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
In reply to Greg KHQuoted 27 lines · Greg KH
Quoted 18 lines · Jianping Li
Quoted 16 lines · Greg KH
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
multiple threads sharing the same file descriptor is a horrible userspace bug. If you do that, you get what you deserve :)
thanks,
Quoted 7 lines
Quoted 1 line
greg k-h
I agree that concurrent unmap on the same buffer from multiple threads is a userspace bug.
However, the fastrpc driver is exposed via ioctl, and any userspace (including any random apk) can map directly and then call multiple unmap.
Quoted 8 lines
So there is no userspace selinux permissions settings on the fasterpc device node at all?
Quoted 3 lines
This patch is not intended to support incorrect userspace usage, but to ensure that the driver remains robust against invalid or racy inputs.
Isn't control to this device an "admin only" thing?
No, it's being used by normal user apps. Also, there are systems without SELinux being enabled / used.
Quoted 4 lines
thanks,
greg k-h
Signature · 2 lines
With best wishes Dmitry
Greg KH
maintainerRe: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 31 lines · Dmitry Baryshkov
Quoted 30 lines · Greg KH
Quoted 18 lines · Jianping Li
Quoted 16 lines · Greg KH
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
multiple threads sharing the same file descriptor is a horrible userspace bug. If you do that, you get what you deserve :)
thanks,
Quoted 7 lines
Quoted 1 line
greg k-h
I agree that concurrent unmap on the same buffer from multiple threads is a userspace bug.
However, the fastrpc driver is exposed via ioctl, and any userspace (including any random apk) can map directly and then call multiple unmap.
So there is no userspace selinux permissions settings on the fasterpc device node at all?
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
This patch is not intended to support incorrect userspace usage, but to ensure that the driver remains robust against invalid or racy inputs.
Isn't control to this device an "admin only" thing?
No, it's being used by normal user apps. Also, there are systems without SELinux being enabled / used.
Oh wow, this driver is going to be even more of a problem than I assumed. Someone needs to throw an LLM at this and fix all the bugs as soon as possible then...
good luck!
greg k-h
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
In reply to Jianping LiQuoted 5 lines · Jianping Li
Quoted 3 lines · Dmitry Baryshkov
Quoted 2 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
=> commit message
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/misc/fastrpc.c +0
-1@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)return -EINVAL ; } -return fastrpc_req_munmap_impl (fl, buf);Quoted 8 lines
drivers/misc/fastrpc.c static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1905
Quoted 6 lines
drivers/misc/fastrpc.c +6
-0+ err =fastrpc_req_munmap_impl (fl, buf); +if (err) { +spin_lock (&fl->lock); +list_add_tail (&buf->node, &fl->mmaps); +spin_unlock (&fl->lock); + }Is it expected that userspace tries to unmap it again? Or why is it being added to the list?
Quoted 1 line
User process can call unmap and fastrpc library won't call the unmap again.
In the other email you wrote that the driver can be used by random apps. So... what happens if userspace unmaps it again? What if the userspace _doesn't_ unmap it (although you've just readded it back)?
Quoted 1 line
Fastrpc driver will free up this list during fastrpc user-free.
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 25 lines · Dmitry Baryshkov
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
=> commit message
Quoted 12 lines
Quoted 12 lines
drivers/misc/fastrpc.c static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1904
Quoted 10 lines
drivers/misc/fastrpc.c +6
-1@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)return -EINVAL ; } -return fastrpc_req_munmap_impl (fl, buf); + err =fastrpc_req_munmap_impl (fl, buf); +if (err) { +spin_lock (&fl->lock); +list_add_tail (&buf->node, &fl->mmaps); +spin_unlock (&fl->lock); + }Is it expected that userspace tries to unmap it again? Or why is it being added to the list?
Quoted 4 lines
Quoted 1 line
User process can call unmap and fastrpc library won't call the unmap again.
In the other email you wrote that the driver can be used by random apps. So... what happens if userspace unmaps it again? What if the userspace _doesn't_ unmap it (although you've just readded it back)?
If the same buf is unmapped again, because it has already been added back to the list, the unmap logic will be executed again. If userspace no longer performs unmap, the driver will not unmap it proactively.
The Fastrpc driver will free up this list during fastrpc user-free.
Quoted 2 lines
Quoted 2 lines
Fastrpc driver will free up this list during fastrpc user-free.
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 28 lines · Jianping Li
Quoted 25 lines · Dmitry Baryshkov
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
=> commit message
Quoted 12 lines
Quoted 12 lines
drivers/misc/fastrpc.c static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1904
Quoted 10 lines
drivers/misc/fastrpc.c +6
-1@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)return -EINVAL ; } -return fastrpc_req_munmap_impl (fl, buf); + err =fastrpc_req_munmap_impl (fl, buf); +if (err) { +spin_lock (&fl->lock); +list_add_tail (&buf->node, &fl->mmaps); +spin_unlock (&fl->lock); + }Is it expected that userspace tries to unmap it again? Or why is it being added to the list?
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
User process can call unmap and fastrpc library won't call the unmap again.
In the other email you wrote that the driver can be used by random apps. So... what happens if userspace unmaps it again? What if the userspace _doesn't_ unmap it (although you've just readded it back)?
If the same buf is unmapped again, because it has already been added back to the list, the unmap logic will be executed again. If userspace no longer performs unmap, the driver will not unmap it proactively.
The Fastrpc driver will free up this list during fastrpc user-free.
It will free the list. But what happens with the memory mapping?
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Fastrpc driver will free up this list during fastrpc user-free.
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 28 lines · Dmitry Baryshkov
Quoted 27 lines · Jianping Li
Quoted 25 lines · Dmitry Baryshkov
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
=> commit message
Quoted 12 lines
Quoted 12 lines
drivers/misc/fastrpc.c static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1904
Quoted 10 lines
drivers/misc/fastrpc.c +6
-1@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)return -EINVAL ; } -return fastrpc_req_munmap_impl (fl, buf); + err =fastrpc_req_munmap_impl (fl, buf); +if (err) { +spin_lock (&fl->lock); +list_add_tail (&buf->node, &fl->mmaps); +spin_unlock (&fl->lock); + }Is it expected that userspace tries to unmap it again? Or why is it being added to the list?
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
User process can call unmap and fastrpc library won't call the unmap again.
In the other email you wrote that the driver can be used by random apps. So... what happens if userspace unmaps it again? What if the userspace _doesn't_ unmap it (although you've just readded it back)?
If the same buf is unmapped again, because it has already been added back to the list, the unmap logic will be executed again. If userspace no longer performs unmap, the driver will not unmap it proactively.
The Fastrpc driver will free up this list during fastrpc user-free.It will free the list. But what happens with the memory mapping?
When device release is called, DSP side PD is also cleaned up, which includes cleaning up of DSP side mappings
Before kref_put of fl, we call DSP process release, where DSP PD is cleaned up.
After calling this, we go ahead and do buf_free of the list
Thanks,
Jianping.
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Fastrpc driver will free up this list during fastrpc user-free.
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 37 lines · Jianping Li
Quoted 35 lines · Dmitry Baryshkov
Quoted 34 lines · Jianping Li
Quoted 25 lines · Dmitry Baryshkov
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
=> commit message
Quoted 12 lines
Quoted 12 lines
Review static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1904
Quoted 10 lines
(quoted diff) +6
-1@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) return -EINVAL; } - return fastrpc_req_munmap_impl(fl, buf); + err = fastrpc_req_munmap_impl(fl, buf); + if (err) { + spin_lock(&fl->lock); + list_add_tail(&buf->node, &fl->mmaps); + spin_unlock(&fl->lock); + }Is it expected that userspace tries to unmap it again? Or why is it being added to the list?
Quoted 4 lines
Quoted 1 line
User process can call unmap and fastrpc library won't call the unmap again.
In the other email you wrote that the driver can be used by random apps. So... what happens if userspace unmaps it again? What if the userspace _doesn't_ unmap it (although you've just readded it back)?
If the same buf is unmapped again, because it has already been added back to the list, the unmap logic will be executed again. If userspace no longer performs unmap, the driver will not unmap it proactively.
The Fastrpc driver will free up this list during fastrpc user-free.
Quoted 7 lines
Quoted 1 line
It will free the list. But what happens with the memory mapping?
When device release is called, DSP side PD is also cleaned up, which includes cleaning up of DSP side mappings
Before
kref_putof fl, we call DSP process release, where DSP PD is cleaned up.After calling this, we go ahead and do
buf_freeof the list
Okay, capture this discussion in the commit message.
Quoted 8 lines
Thanks,
Jianping.Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Fastrpc driver will free up this list during fastrpc user-free.
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v5 2/5] misc: fastrpc: Remove buffer from list prior to unmap operation
Quoted 36 lines · Dmitry Baryshkov
Quoted 35 lines · Jianping Li
Quoted 34 lines · Dmitry Baryshkov
Quoted 33 lines · Jianping Li
Quoted 25 lines · Dmitry Baryshkov
Quoted 10 lines · Jianping Li
Quoted 8 lines · Dmitry Baryshkov
Quoted 6 lines · Jianping Li
From: Ekansh Gupta<[email protected]>
fastrpc_req_munmap_impl()is called to unmap any buffer. The buffer is getting removed from the list after it is unmapped from DSP. This can create potential race conditions if any other thread removes the entry from list while unmap operation is ongoing. Remove the entry beforeHow can it remove the entry from the list?
Multiple threads sharing the same file descriptor may invoke unmap concurrently.
=> commit message
Quoted 12 lines
Quoted 12 lines
Review static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) · line 1904
Quoted 10 lines
(quoted diff) +6
-1@@ -1898,7 +1897,14 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) return -EINVAL; } - return fastrpc_req_munmap_impl(fl, buf); + err = fastrpc_req_munmap_impl(fl, buf); + if (err) { + spin_lock(&fl->lock); + list_add_tail(&buf->node, &fl->mmaps); + spin_unlock(&fl->lock); + }Is it expected that userspace tries to unmap it again? Or why is it being added to the list?
Quoted 4 lines
Quoted 1 line
User process can call unmap and fastrpc library won't call the unmap again.
In the other email you wrote that the driver can be used by random apps. So... what happens if userspace unmaps it again? What if the userspace _doesn't_ unmap it (although you've just readded it back)?
If the same buf is unmapped again, because it has already been added back to the list, the unmap logic will be executed again. If userspace no longer performs unmap, the driver will not unmap it proactively.
The Fastrpc driver will free up this list during fastrpc user-free.
Quoted 7 lines
Quoted 6 lines
Quoted 1 line
It will free the list. But what happens with the memory mapping?
When device release is called, DSP side PD is also cleaned up, which includes cleaning up of DSP side mappings
Before
kref_putof fl, we call DSP process release, where DSP PD is cleaned up.After calling this, we go ahead and do
buf_freeof the listOkay, capture this discussion in the commit message.
Got it, thanks.
I'll update the commit message in the next version patch.
Quoted 6 lines
Quoted 6 lines
Thanks,
Jianping.Quoted 2 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Fastrpc driver will free up this list during fastrpc user-free.
PATCH v5 3/5 misc: fastrpc: Fail Audio PD init when reserved memory is missing
In reply to Jianping LiAudio PD static process creation assumes that a reserved-memory region is defined in DT and exposed via cctx->remote_heap.
If reserved-memory is missing or incomplete, the driver may pass invalid address/size information to the DSP, leading to undefined behavior or crashes.
Add explicit validation for remote_heap presence and size before sending the memory to DSP, and fail early if the configuration is invalid.
--- drivers/misc/fastrpc.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/misc/fastrpc.c +6 -0
@@ -1363,6 +1363,12 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, args[1 ].length = inbuf.namelen; args[1 ].fd =-1 ; +if (!fl->cctx->remote_heap || + !fl->cctx->remote_heap->dma_addr || + !fl->cctx->remote_heap->size) { + err = -ENOMEM ; + goto err; + } pages[0 ].addr = fl->cctx->remote_heap->dma_addr; pages[0 ].size = fl->cctx->remote_heap->size;
Signature · 1 line
2.43.0
Re: [PATCH v5 3/5] misc: fastrpc: Fail Audio PD init when reserved memory is missing
Quoted 23 lines · Jianping Li
Audio PD static process creation assumes that a reserved-memory region is defined in DT and exposed via
cctx->remote_heap.If reserved-memory is missing or incomplete, the driver may pass invalid address/size information to the DSP, leading to undefined behavior or crashes.
Add explicit validation for
remote_heappresence and size before sending the memory to DSP, and fail early if the configuration is invalid.Fixes 0871561055e66 ("misc: fastrpc: Add support for audiopd")Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/misc/fastrpc.c +0
-0
drivers/misc/fastrpc.c static int fastrpc_init_create_static_process(struct fastrpc_user *fl, · line 1368
Quoted 6 lines
drivers/misc/fastrpc.c +3
-0@@ -1363,6 +1363,12 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, args[1 ].length = inbuf.namelen; args[1 ].fd =-1 ; +if (!fl->cctx->remote_heap || + !fl->cctx->remote_heap->dma_addr || + !fl->cctx->remote_heap->size) {
I guess that !dma_addr || !size should fail much earlier than here.
Quoted 9 lines
drivers/misc/fastrpc.c +3
-0+ err = -ENOMEM ; + goto err; + } pages[0 ].addr = fl->cctx->remote_heap->dma_addr; pages[0 ].size = fl->cctx->remote_heap->size;Signature · 1 line
2.43.0
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v5 3/5] misc: fastrpc: Fail Audio PD init when reserved memory is missing
Quoted 27 lines · Dmitry Baryshkov
Quoted 26 lines · Jianping Li
Audio PD static process creation assumes that a reserved-memory region is defined in DT and exposed via
cctx->remote_heap.If reserved-memory is missing or incomplete, the driver may pass invalid address/size information to the DSP, leading to undefined behavior or crashes.
Add explicit validation for
remote_heappresence and size before sending the memory to DSP, and fail early if the configuration is invalid.Fixes 0871561055e66 ("misc: fastrpc: Add support for audiopd")Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/misc/fastrpc.c +0
-0@@ -1363,6 +1363,12 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, args[1 ].length = inbuf.namelen; args[1 ].fd =-1 ;
Quoted 4 lines
drivers/misc/fastrpc.c static int fastrpc_init_create_static_process(struct fastrpc_user *fl, · line 1368
Quoted 3 lines
drivers/misc/fastrpc.c +3
-0+if (!fl->cctx->remote_heap || + !fl->cctx->remote_heap->dma_addr || + !fl->cctx->remote_heap->size) {I guess that !dma_addr || !size should fail much earlier than here.
ACK
Quoted 10 lines
Quoted 10 lines
drivers/misc/fastrpc.c +3
-0+ err = -ENOMEM ; + goto err; + } pages[0 ].addr = fl->cctx->remote_heap->dma_addr; pages[0 ].size = fl->cctx->remote_heap->size;Signature · 1 line
2.43.0
Re: [PATCH v5 3/5] misc: fastrpc: Fail Audio PD init when reserved memory is missing
In reply to Jianping LiQuoted 23 lines · Jianping Li
Audio PD static process creation assumes that a reserved-memory region is defined in DT and exposed via
cctx->remote_heap.If reserved-memory is missing or incomplete, the driver may pass invalid address/size information to the DSP, leading to undefined behavior or crashes.
Add explicit validation for
remote_heappresence and size before sending the memory to DSP, and fail early if the configuration is invalid.Fixes 0871561055e66 ("misc: fastrpc: Add support for audiopd")Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/misc/fastrpc.c +0
-0
drivers/misc/fastrpc.c static int fastrpc_init_create_static_process(struct fastrpc_user *fl, · line 1370
Quoted 8 lines
drivers/misc/fastrpc.c +5
-0@@ -1363,6 +1363,12 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, args[1 ].length = inbuf.namelen; args[1 ].fd =-1 ; +if (!fl->cctx->remote_heap || + !fl->cctx->remote_heap->dma_addr || + !fl->cctx->remote_heap->size) { + err = -ENOMEM ; + goto err;
Let's maybe add an UERR() with some context
Konrad
Re: [PATCH v5 3/5] misc: fastrpc: Fail Audio PD init when reserved memory is missing
Quoted 27 lines · Konrad Dybcio
Quoted 26 lines · Jianping Li
Audio PD static process creation assumes that a reserved-memory region is defined in DT and exposed via
cctx->remote_heap.If reserved-memory is missing or incomplete, the driver may pass invalid address/size information to the DSP, leading to undefined behavior or crashes.
Add explicit validation for
remote_heappresence and size before sending the memory to DSP, and fail early if the configuration is invalid.Fixes 0871561055e66 ("misc: fastrpc: Add support for audiopd")Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/misc/fastrpc.c +0
-0@@ -1363,6 +1363,12 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, args[1 ].length = inbuf.namelen; args[1 ].fd =-1 ;
Quoted 8 lines
drivers/misc/fastrpc.c static int fastrpc_init_create_static_process(struct fastrpc_user *fl, · line 1370
Quoted 5 lines
drivers/misc/fastrpc.c +5
-0+if (!fl->cctx->remote_heap || + !fl->cctx->remote_heap->dma_addr || + !fl->cctx->remote_heap->size) { + err = -ENOMEM ; + goto err;Let's maybe add an
UERR()with some contextKonrad
Planning to move this check at the start of this function as per Dmitry's suggestion. I'll also add a dev_dbg log there.
Thanks,
Jianping.
PATCH v5 4/5 misc: fastrpc: Allocate entire reserved memory for Audio PD in probe
In reply to Jianping LiAllocating and freeing Audio PD memory from userspace is unsafe because the kernel cannot reliably determine when the DSP has finished using the memory. Userspace may free buffers while they are still in use by the DSP, and remote free requests cannot be safely trusted.
Allocate the entire Audio PD reserved-memory region upfront during rpmsg probe and tie its lifetime to the rpmsg channel. This avoids userspace- controlled alloc/free and ensures memory is reclaimed only when the DSP shuts down.
--- drivers/misc/fastrpc.c | 107 +++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 58 deletions(-)
drivers/misc/fastrpc.c +49 -58
@@ -276,6 +276,8 @@ struct fastrpc_channel_ctx {struct kref refcount;/* Flag if dsp attributes are cached */ bool valid_attributes; +/* Flag if audio PD init mem was allocated */ +bool audio_init_mem;u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES ];struct fastrpc_device *secure_fdevice;struct fastrpc_device *fdevice;
@@ -1295,15 +1297,16 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl,struct fastrpc_init_create_static init;struct fastrpc_invoke_args *args;struct fastrpc_phy_page pages[1 ]; +struct fastrpc_channel_ctx *cctx = fl->cctx;char *name;int err; -bool scm_done = false;struct {int client_id;u32 namelen;u32 pageslen; } inbuf;u32 sc; +unsigned long flags; args =kzalloc_objs (*args,FASTRPC_CREATE_STATIC_PROCESS_NARGS );if (!args)
@@ -1327,31 +1330,6 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, inbuf.client_id = fl->client_id; inbuf.namelen = init.namelen; inbuf.pageslen =0 ; -if (!fl->cctx->remote_heap) { - err =fastrpc_remote_heap_alloc (fl, fl->sctx->dev, init.memlen, - &fl->cctx->remote_heap); -if (err) - goto err_name; - -/* Map if we have any heap VMIDs associated with this ADSP Static Process. */ -if (fl->cctx->vmcount) { -u64 src_perms =BIT (QCOM_SCM_VMID_HLOS ); - - err =qcom_scm_assign_mem (fl->cctx->remote_heap->dma_addr, - (u64 )fl->cctx->remote_heap->size, - &src_perms, - fl->cctx->vmperms, fl->cctx->vmcount); -if (err) { -dev_err (fl->sctx->dev, -"Failed to assign memory with dma_addr %pad size 0x%llx err %d\n" , - &fl->cctx->remote_heap->dma_addr, - fl->cctx->remote_heap->size, err); - goto err_map; - } - scm_done = true; - inbuf.pageslen =1 ; - } - } fl->pd =USER_PD ;
@@ -1363,14 +1341,24 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, args[1 ].length = inbuf.namelen; args[1 ].fd =-1 ; -if (!fl->cctx->remote_heap || - !fl->cctx->remote_heap->dma_addr || - !fl->cctx->remote_heap->size) { - err = -ENOMEM ; - goto err; +spin_lock_irqsave (&cctx->lock, flags); +if (!fl->cctx->audio_init_mem) { +if (!fl->cctx->remote_heap || + !fl->cctx->remote_heap->dma_addr || + !fl->cctx->remote_heap->size) { +spin_unlock_irqrestore (&cctx->lock, flags); + err = -ENOMEM ; + goto err; + } + pages[0 ].addr = fl->cctx->remote_heap->dma_addr; + pages[0 ].size = fl->cctx->remote_heap->size; + fl->cctx->audio_init_mem = true; + inbuf.pageslen =1 ; + }else { + pages[0 ].addr =0 ; + pages[0 ].size =0 ; } - pages[0 ].addr = fl->cctx->remote_heap->dma_addr; - pages[0 ].size = fl->cctx->remote_heap->size; +spin_unlock_irqrestore (&cctx->lock, flags); args[2 ].ptr = (u64 )(uintptr_t ) pages; args[2 ].length =sizeof (*pages);
@@ -1388,27 +1376,7 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl,return 0 ; err_invoke: -if (fl->cctx->vmcount && scm_done) { -u64 src_perms =0 ; -struct qcom_scm_vmperm dst_perms; -u32 i; - -for (i =0 ; i < fl->cctx->vmcount; i++) - src_perms |=BIT (fl->cctx->vmperms[i].vmid); - - dst_perms.vmid =QCOM_SCM_VMID_HLOS ; - dst_perms.perm =QCOM_SCM_PERM_RWX ; - err =qcom_scm_assign_mem (fl->cctx->remote_heap->dma_addr, - (u64 )fl->cctx->remote_heap->size, - &src_perms, &dst_perms,1 ); -if (err) -dev_err (fl->sctx->dev,"Failed to assign memory dma_addr %pad size 0x%llx err %d\n" , - &fl->cctx->remote_heap->dma_addr, fl->cctx->remote_heap->size, err); - } -err_map: -fastrpc_buf_free (fl->cctx->remote_heap); - fl->cctx->remote_heap =NULL ; -err_name: + fl->cctx->audio_init_mem = false;kfree (name); err:kfree (args);
@@ -2397,12 +2365,21 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) } } -if (domain_id ==SDSP_DOMAIN_ID ) { +if (domain_id ==SDSP_DOMAIN_ID || domain_id ==ADSP_DOMAIN_ID ) {struct resource res;u64 src_perms; err =of_reserved_mem_region_to_resource (rdev->of_node,0 , &res);if (!err) { +if (domain_id ==ADSP_DOMAIN_ID ) { + data->remote_heap = +kzalloc_obj (*data->remote_heap,GFP_KERNEL ); +if (!data->remote_heap) +return -ENOMEM ; + + data->remote_heap->dma_addr = res.start; + data->remote_heap->size =resource_size (&res); + } src_perms =BIT (QCOM_SCM_VMID_HLOS ); err = qcom_scm_assign_mem(res.start, resource_size(&res), &src_perms,
@@ -2410,7 +2387,6 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)if (err) goto err_free_data; } - } secure_dsp = !(of_property_read_bool (rdev->of_node,"qcom,non-secure-domain" ));
@@ -2491,6 +2467,7 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)struct fastrpc_buf *buf, *b;struct fastrpc_user *user;unsigned long flags; +int err;/* No invocations past this point */ spin_lock_irqsave (&cctx->lock, flags);
@@ -2508,8 +2485,22 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)list_for_each_entry_safe (buf, b, &cctx->invoke_interrupted_mmaps, node)list_del (&buf->node); -if (cctx->remote_heap) -fastrpc_buf_free (cctx->remote_heap); +if (cctx->remote_heap && cctx->vmcount) { +u64 src_perms =0 ; +struct qcom_scm_vmperm dst_perms; + +for (u32 i =0 ; i < cctx->vmcount; i++) + src_perms |=BIT (cctx->vmperms[i].vmid); + + dst_perms.vmid =QCOM_SCM_VMID_HLOS ; + dst_perms.perm =QCOM_SCM_PERM_RWX ; + + err =qcom_scm_assign_mem (cctx->remote_heap->dma_addr, + cctx->remote_heap->size, &src_perms, + &dst_perms,1 ); +if (!err) +kfree (cctx->remote_heap); + }of_platform_depopulate (&rpdev->dev);
Signature · 1 line
2.43.0
Re: [PATCH v5 4/5] misc: fastrpc: Allocate entire reserved memory for Audio PD in probe
Quoted 9 lines · Jianping Li
Allocating and freeing Audio PD memory from userspace is unsafe because the kernel cannot reliably determine when the DSP has finished using the memory. Userspace may free buffers while they are still in use by the DSP, and remote free requests cannot be safely trusted.
Allocate the entire Audio PD reserved-memory region upfront during rpmsg probe and tie its lifetime to the rpmsg channel. This avoids userspace- controlled alloc/free and ensures memory is reclaimed only when the DSP shuts down.
So, is this a bugfix or not? Is it possible to make the kernel misbehave without this patch being applied?
Quoted 6 lines
Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 107 +++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 58 deletions(-)
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v5 4/5] misc: fastrpc: Allocate entire reserved memory for Audio PD in probe
Quoted 6 lines · Dmitry Baryshkov
Quoted 5 lines · Jianping Li
Allocating and freeing Audio PD memory from userspace is unsafe because the kernel cannot reliably determine when the DSP has finished using the memory. Userspace may free buffers while they are still in use by the DSP, and remote free requests cannot be safely trusted.
Quoted 6 lines
Quoted 4 lines
Allocate the entire Audio PD reserved-memory region upfront during rpmsg probe and tie its lifetime to the rpmsg channel. This avoids userspace- controlled alloc/free and ensures memory is reclaimed only when the DSP shuts down.
So, is this a bugfix or not? Is it possible to make the kernel misbehave without this patch being applied?
Yes, this is a bug fix.
Because currently after the audio PD requests to grow the heap,
the current kernel does not support shrinking the heap, which will all cause memory leaks.
The current modification is to allocate in advance and release uniformly, which will avoid this error.
Thanks,
Jianping.
Quoted 6 lines
Quoted 6 lines
Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 107 +++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 58 deletions(-)
Re: [PATCH v5 4/5] misc: fastrpc: Allocate entire reserved memory for Audio PD in probe
Quoted 8 lines · Jianping Li
Quoted 6 lines · Dmitry Baryshkov
Quoted 5 lines · Jianping Li
Allocating and freeing Audio PD memory from userspace is unsafe because the kernel cannot reliably determine when the DSP has finished using the memory. Userspace may free buffers while they are still in use by the DSP, and remote free requests cannot be safely trusted.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Allocate the entire Audio PD reserved-memory region upfront during rpmsg probe and tie its lifetime to the rpmsg channel. This avoids userspace- controlled alloc/free and ensures memory is reclaimed only when the DSP shuts down.
So, is this a bugfix or not? Is it possible to make the kernel misbehave without this patch being applied?
Yes, this is a bug fix.
The tag, cc:stable, clear description as the bugfix? How would anybody guess if the patch is to be backported to earlier kernels or not?
Quoted 14 lines
Because currently after the audio PD requests to grow the heap,
the current kernel does not support shrinking the heap, which will all cause memory leaks.The current modification is to allocate in advance and release uniformly, which will avoid this error.
Thanks,
Jianping.Quoted 6 lines
Quoted 6 lines
Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 107 +++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 58 deletions(-)
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v5 4/5] misc: fastrpc: Allocate entire reserved memory for Audio PD in probe
Quoted 12 lines · Dmitry Baryshkov
Quoted 11 lines · Jianping Li
Quoted 10 lines · Dmitry Baryshkov
Quoted 9 lines · Jianping Li
Allocating and freeing Audio PD memory from userspace is unsafe because the kernel cannot reliably determine when the DSP has finished using the memory. Userspace may free buffers while they are still in use by the DSP, and remote free requests cannot be safely trusted.
Allocate the entire Audio PD reserved-memory region upfront during rpmsg probe and tie its lifetime to the rpmsg channel. This avoids userspace- controlled alloc/free and ensures memory is reclaimed only when the DSP shuts down.
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines
So, is this a bugfix or not? Is it possible to make the kernel misbehave without this patch being applied?
Yes, this is a bug fix.
The tag, cc:stable, clear description as the bugfix? How would anybody guess if the patch is to be backported to earlier kernels or not?
When userspace repeatedly grows the heap, the kernel does not support shrinking it, leading to unbounded memory usage. This can eventually cause memory leak.
I will mention this point in the commit message.
Thanks,
Jianping.
Quoted 14 lines
Quoted 14 lines
Because currently after the audio PD requests to grow the heap,
the current kernel does not support shrinking the heap, which will all cause memory leaks.The current modification is to allocate in advance and release uniformly, which will avoid this error.
Thanks,
Jianping.Quoted 5 lines
Quoted 5 lines
Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 107 +++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 58 deletions(-)
PATCH v5 5/5 misc: fastrpc: Allow fastrpc_buf_free() to accept NULL
In reply to Jianping LiFrom: Ekansh Gupta <[email protected]>
Make fastrpc_buf_free() a no-op when passed a NULL pointer, allowing callers to avoid open-coded NULL checks.
--- drivers/misc/fastrpc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/misc/fastrpc.c +5 -4
@@ -416,6 +416,9 @@ static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,static void fastrpc_buf_free (struct fastrpc_buf *buf) { +if (!buf) +return ; +dma_free_coherent (buf->dev, buf->size, buf->virt,fastrpc_ipa_to_dma_addr (buf->fl->cctx, buf->dma_addr));kfree (buf);
@@ -512,8 +515,7 @@ static void fastrpc_context_free(struct kref *ref)for (i =0 ; i < ctx->nbufs; i++)fastrpc_map_put (ctx->maps[i]); -if (ctx->buf) -fastrpc_buf_free (ctx->buf); +fastrpc_buf_free (ctx->buf);spin_lock_irqsave (&cctx->lock, flags);idr_remove (&cctx->ctx_idr, ctx->ctxid >>4 );
@@ -1564,8 +1566,7 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)list_del (&fl->user);spin_unlock_irqrestore (&cctx->lock, flags); -if (fl->init_mem) -fastrpc_buf_free (fl->init_mem); +fastrpc_buf_free (fl->init_mem);list_for_each_entry_safe (ctx, n, &fl->pending, node) {list_del (&ctx->node);
Signature · 1 line
2.43.0
Re: [PATCH v5 5/5] misc: fastrpc: Allow fastrpc_buf_free() to accept NULL
Quoted 5 lines · Jianping Li
From: Ekansh Gupta <[email protected]>
Make
fastrpc_buf_free()a no-op when passed a NULL pointer, allowing callers to avoid open-coded NULL checks.
Quoted 7 lines
Co-developed-by Ekansh Gupta <[email protected]>Signed-off-by Ekansh Gupta <[email protected]>Signed-off-by Jianping Li <[email protected]>--- drivers/misc/fastrpc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 2 lines
With best wishes Dmitry