drm/amdgpu: secure userq lifecycle by its kref
Subsystems (2)
cover drm/amdgpu: secure userq lifecycle by its kref
A struct kref is embedded in user queue, which manages the lifecycle of a user queue. However, several code paths access user queues without hoding the kref of a user queue, especially from the doorbell XArray.
These accesses can race with the queue destruction process and result in use-after-free bugs.
To fix this issue, this commit:
1) Introduces a new helper amdgpu_lookup_queue_by_doorbell, which looks up a user queue with locking and hold its kref during access.
2) Implement asynchronous userq destruction routine, because the last put of a queue kref may be placed in a code path where can not sleep or conflict locking with the destruction process.
3) Hold kref during access the user queues
4) Keep the userq manager alive as long as its queues, to avoid UAF issues.
This seires passed amd_basic tests in igt tests
Zhu Lingshan (10):
drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell drm/amdgpu: keep the userq manager alive as long as its queues drm/amdgpu/gfx11: hold userq refs in private fault worker drm/amdgpu/gfx12: hold userq refs in private fault worker drm/amdgpu: implement asynchronous userq destruction routine drm/amdgpu: hold userq kref in MES reset
drm/amdgpu: hold userq kref during isolation scheduling drm/amdgpu: hold userq kref during suspend and resume drm/amdgpu: free userq by kref_put when fails to create drm/amdgpu: take queue kref in userq_create to avoid UAF
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 214 +++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 18 ++ drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 46 ++--- 5 files changed, 246 insertions(+), 46 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/10 drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
This commit introduces a new helperamdgpu_lookup_queue_by_doorbell which helps
look up a user queue with the given doorbell id
in a xarray.
This function takes a kref of the user space queue.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30 -0
@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Signature · 1 line
2.53.0
Christian König
maintainerRe: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 6 lines · Zhu Lingshan
This commit introduces a new helper
amdgpu_lookup_queue_by_doorbellwhich helps
look up a user queue with the given doorbell id
in a xarray.This function takes a kref of the user space queue.
Well absolutely clear NAK to the whole approach.
This is the nonsense Sunil and I have worked quite hard to remove and we certainly shouldn't repeat such mistakes.
When the userq needs to be used from interrupt context we need to hold the xa_lock_irqsave() or otherwise we don't have any guarantee that the userq, userq_mgr or associated fpriv went out of scope.
Grabbing references from this side would obviously result in circle dependencies.
Regards,
Christian.
Quoted 61 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Re: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 7 lines · Christian König
Quoted 6 lines · Zhu Lingshan
This commit introduces a new helper
amdgpu_lookup_queue_by_doorbellwhich helps
look up a user queue with the given doorbell id
in a xarray.This function takes a kref of the user space queue.
Hello Christian
Thanks for your comments.
Quoted 5 lines
Well absolutely clear NAK to the whole approach.
This is the nonsense Sunil and I have worked quite hard to remove and we certainly shouldn't repeat such mistakes.
When the userq needs to be used from interrupt context we need to hold the
xa_lock_irqsave()or otherwise we don't have any guarantee that the userq,userq_mgror associated fpriv went out of scope.
Holding the spin lock by xa_lock_irqsave() can surely avoid racing with the destruction process, however, it does not apply to all scenarios, for example, you can not hold spin lock in mes_userq_reset_queue(), because it calls either amdgpu_mes_reset_queue_mmio or amdgpu_mes_reset_queue_mmio, both of them acquire the MES mutex through amdgpu_mes_lock.
Another thing, out of the topic is, holding xa_lock does not guarantee fpriv/userq_mgr alive, for example, when drm_device->unplugged is true, all amdgpu teardown paths in amdgpu_drm_release are skipped, and the fpriv/userq_mgr is freed, no matter whether holding the xa spin lock.
So IMHO since we have userq->kref, lets use it to maintain the lifecycle of the queues.
Quoted 2 lines
Grabbing references from this side would obviously result in circle dependencies.
I am not sure, we should use the lock/unlock and kref_put/get in pairs in sequence, can you name some circle dependencies or AB-BA lockings as examples?
Thanks
Lingshan
Quoted 64 lines
Regards,
Christian.Quoted 60 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Christian König
maintainerRe: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 14 lines · Lingshan
Quoted 7 lines · Christian König
Quoted 6 lines · Zhu Lingshan
This commit introduces a new helper
amdgpu_lookup_queue_by_doorbellwhich helps
look up a user queue with the given doorbell id
in a xarray.This function takes a kref of the user space queue.
Hello Christian
Thanks for your comments.
Quoted 8 lines
Quoted 5 lines
Well absolutely clear NAK to the whole approach.
This is the nonsense Sunil and I have worked quite hard to remove and we certainly shouldn't repeat such mistakes.
When the userq needs to be used from interrupt context we need to hold the
xa_lock_irqsave()or otherwise we don't have any guarantee that the userq,userq_mgror associated fpriv went out of scope.Holding the spin lock by
xa_lock_irqsave()can surely avoid racing with the destruction process, however, it does not apply to all scenarios, for example, you can not hold spin lock inmes_userq_reset_queue(), because it calls eitheramdgpu_mes_reset_queue_mmiooramdgpu_mes_reset_queue_mmio, both of them acquire the MES mutex throughamdgpu_mes_lock.
Yeah which is exactly the reason why mes_userq_reset_queue() should *NOT* be called from non IOCTL context.
Quoted 2 lines
Another thing, out of the topic is, holding
xa_lockdoes not guarantee fpriv/userq_mgr alive, for example, whendrm_device->unpluggedis true, all amdgpu teardown paths inamdgpu_drm_releaseare skipped, and the fpriv/userq_mgr is freed, no matter whether holding the xa spin lock.
That would clearly be a massive bug. Those objects still need to be cleaned up independent of device hot plug.
Quoted 5 lines
So IMHO since we have
userq->kref, lets use it to maintain the lifecycle of the queues.Quoted 1 line
Grabbing references from this side would obviously result in circle dependencies.
I am not sure, we should use the lock/unlock and kref_put/get in pairs in sequence, can you name some circle dependencies or AB-BA lockings as examples?
That is not AB-BA locking, but circle dependencies. E.g. A reference B, B referencing C, C referencing A again.
Regards,
Christian.
Quoted 67 lines
Thanks
LingshanQuoted 63 lines
Regards,
Christian.Quoted 60 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Re: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 14 lines · Christian König
Quoted 13 lines · Lingshan
Quoted 7 lines · Christian König
Quoted 6 lines · Zhu Lingshan
This commit introduces a new helper
amdgpu_lookup_queue_by_doorbellwhich helps
look up a user queue with the given doorbell id
in a xarray.This function takes a kref of the user space queue.
Hello Christian
Thanks for your comments.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Well absolutely clear NAK to the whole approach.
This is the nonsense Sunil and I have worked quite hard to remove and we certainly shouldn't repeat such mistakes.
When the userq needs to be used from interrupt context we need to hold the
xa_lock_irqsave()or otherwise we don't have any guarantee that the userq,userq_mgror associated fpriv went out of scope.Holding the spin lock by
xa_lock_irqsave()can surely avoid racing with the destruction process, however, it does not apply to all scenarios, for example, you can not hold spin lock inmes_userq_reset_queue(), because it calls eitheramdgpu_mes_reset_queue_mmiooramdgpu_mes_reset_queue_mmio, both of them acquire the MES mutex throughamdgpu_mes_lock.Yeah which is exactly the reason why
mes_userq_reset_queue()should *NOT* be called from non IOCTL context.
I think it is not about whether called from IOCTL, it is a common racing we should fix, and holding a kref is a low haning fruit.
Quoted 4 lines
Quoted 3 lines
Another thing, out of the topic is, holding
xa_lockdoes not guarantee fpriv/userq_mgr alive, for example, whendrm_device->unpluggedis true, all amdgpu teardown paths inamdgpu_drm_releaseare skipped, and the fpriv/userq_mgr is freed, no matter whether holding the xa spin lock.That would clearly be a massive bug. Those objects still need to be cleaned up independent of device hot plug.
I agree, when unplugged == true, means can not access any HW registers, so this bug deserve another series to fix.
Quoted 6 lines
Quoted 5 lines
So IMHO since we have
userq->kref, lets use it to maintain the lifecycle of the queues.Quoted 1 line
Grabbing references from this side would obviously result in circle dependencies.
I am not sure, we should use the lock/unlock and kref_put/get in pairs in sequence, can you name some circle dependencies or AB-BA lockings as examples?
That is not AB-BA locking, but circle dependencies. E.g. A reference B, B referencing C, C referencing A again.
A kref is an atomic counter, not a lock, that means we can try hold more than one kref in a thread, and a kref not depend on another. As far as I can see, current amdgpu driver does not have such problems, do you see any occurrences?
Thanks
Lingshan
Quoted 70 lines
Regards,
Christian.Quoted 66 lines
Thanks
LingshanQuoted 63 lines
Regards,
Christian.Quoted 60 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Re: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 31 lines · Lingshan
Quoted 14 lines · Christian König
Quoted 13 lines · Lingshan
Quoted 7 lines · Christian König
Quoted 6 lines · Zhu Lingshan
This commit introduces a new helper
amdgpu_lookup_queue_by_doorbellwhich helps
look up a user queue with the given doorbell id
in a xarray.This function takes a kref of the user space queue.
Hello Christian
Thanks for your comments.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Well absolutely clear NAK to the whole approach.
This is the nonsense Sunil and I have worked quite hard to remove and we certainly shouldn't repeat such mistakes.
When the userq needs to be used from interrupt context we need to hold the
xa_lock_irqsave()or otherwise we don't have any guarantee that the userq,userq_mgror associated fpriv went out of scope.Holding the spin lock by
xa_lock_irqsave()can surely avoid racing with the destruction process, however, it does not apply to all scenarios, for example, you can not hold spin lock inmes_userq_reset_queue(), because it calls eitheramdgpu_mes_reset_queue_mmiooramdgpu_mes_reset_queue_mmio, both of them acquire the MES mutex throughamdgpu_mes_lock.Yeah which is exactly the reason why
mes_userq_reset_queue()should *NOT* be called from non IOCTL context.I think it is not about whether called from IOCTL, it is a common racing we should fix, and holding a kref is a low haning fruit.
Quoted 3 lines
Quoted 2 lines
Another thing, out of the topic is, holding
xa_lockdoes not guarantee fpriv/userq_mgr alive, for example, whendrm_device->unpluggedis true, all amdgpu teardown paths inamdgpu_drm_releaseare skipped, and the fpriv/userq_mgr is freed, no matter whether holding the xa spin lock.That would clearly be a massive bug. Those objects still need to be cleaned up independent of device hot plug.
I agree, when unplugged == true, means can not access any HW registers, so this bug deserve another series to fix.
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
So IMHO since we have
userq->kref, lets use it to maintain the lifecycle of the queues.Quoted 1 line
Grabbing references from this side would obviously result in circle dependencies.
I am not sure, we should use the lock/unlock and kref_put/get in pairs in sequence, can you name some circle dependencies or AB-BA lockings as examples?
That is not AB-BA locking, but circle dependencies. E.g. A reference B, B referencing C, C referencing A again.
A kref is an atomic counter, not a lock, that means we can try hold more than one kref in a thread, and a kref not depend on another. As far as I can see, current amdgpu driver does not have such problems, do you see any occurrences?
Hello Christian,
I have not heard back from you for three weeks, I wonder whether you have any further comments for this series, or shall I make any improvements?
Thanks
Lingshan
Quoted 73 lines
Thanks
LingshanQuoted 69 lines
Regards,
Christian.Quoted 66 lines
Thanks
LingshanQuoted 63 lines
Regards,
Christian.Quoted 60 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Christian König
maintainerRe: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 37 lines · Lingshan
Quoted 35 lines · Lingshan
Quoted 14 lines · Christian König
Quoted 13 lines · Lingshan
Quoted 7 lines · Christian König
Quoted 6 lines · Zhu Lingshan
This commit introduces a new helper
amdgpu_lookup_queue_by_doorbellwhich helps
look up a user queue with the given doorbell id
in a xarray.This function takes a kref of the user space queue.
Hello Christian
Thanks for your comments.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Well absolutely clear NAK to the whole approach.
This is the nonsense Sunil and I have worked quite hard to remove and we certainly shouldn't repeat such mistakes.
When the userq needs to be used from interrupt context we need to hold the
xa_lock_irqsave()or otherwise we don't have any guarantee that the userq,userq_mgror associated fpriv went out of scope.Holding the spin lock by
xa_lock_irqsave()can surely avoid racing with the destruction process, however, it does not apply to all scenarios, for example, you can not hold spin lock inmes_userq_reset_queue(), because it calls eitheramdgpu_mes_reset_queue_mmiooramdgpu_mes_reset_queue_mmio, both of them acquire the MES mutex throughamdgpu_mes_lock.Yeah which is exactly the reason why
mes_userq_reset_queue()should *NOT* be called from non IOCTL context.I think it is not about whether called from IOCTL, it is a common racing we should fix, and holding a kref is a low haning fruit.
Quoted 3 lines
Quoted 2 lines
Another thing, out of the topic is, holding
xa_lockdoes not guarantee fpriv/userq_mgr alive, for example, whendrm_device->unpluggedis true, all amdgpu teardown paths inamdgpu_drm_releaseare skipped, and the fpriv/userq_mgr is freed, no matter whether holding the xa spin lock.That would clearly be a massive bug. Those objects still need to be cleaned up independent of device hot plug.
I agree, when unplugged == true, means can not access any HW registers, so this bug deserve another series to fix.
Quoted 4 lines
Quoted 4 lines
So IMHO since we have
userq->kref, lets use it to maintain the lifecycle of the queues.Quoted 1 line
Grabbing references from this side would obviously result in circle dependencies.
I am not sure, we should use the lock/unlock and kref_put/get in pairs in sequence, can you name some circle dependencies or AB-BA lockings as examples?
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
That is not AB-BA locking, but circle dependencies. E.g. A reference B, B referencing C, C referencing A again.
A kref is an atomic counter, not a lock, that means we can try hold more than one kref in a thread, and a kref not depend on another. As far as I can see, current amdgpu driver does not have such problems, do you see any occurrences?
Hello Christian,
I have not heard back from you for three weeks, I wonder whether you have any further comments for this series, or shall I make any improvements?
Well just completely drop that series. As I said this approach is a fundamental no-go from my side.
As far as I can see we have solved the problems at hand and the rules how to handle the user queues should be pretty clear by now.
Regards,
Christian.
Quoted 76 lines
Thanks
LingshanQuoted 72 lines
Thanks
LingshanQuoted 69 lines
Regards,
Christian.Quoted 66 lines
Thanks
LingshanQuoted 63 lines
Regards,
Christian.Quoted 60 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Re: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 41 lines · Christian König
Quoted 40 lines · Lingshan
Quoted 38 lines · Lingshan
Quoted 14 lines · Christian König
Quoted 13 lines · Lingshan
Quoted 7 lines · Christian König
Quoted 6 lines · Zhu Lingshan
This commit introduces a new helper
amdgpu_lookup_queue_by_doorbellwhich helps
look up a user queue with the given doorbell id
in a xarray.This function takes a kref of the user space queue.
Hello Christian
Thanks for your comments.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Well absolutely clear NAK to the whole approach.
This is the nonsense Sunil and I have worked quite hard to remove and we certainly shouldn't repeat such mistakes.
When the userq needs to be used from interrupt context we need to hold the
xa_lock_irqsave()or otherwise we don't have any guarantee that the userq,userq_mgror associated fpriv went out of scope.Holding the spin lock by
xa_lock_irqsave()can surely avoid racing with the destruction process, however, it does not apply to all scenarios, for example, you can not hold spin lock inmes_userq_reset_queue(), because it calls eitheramdgpu_mes_reset_queue_mmiooramdgpu_mes_reset_queue_mmio, both of them acquire the MES mutex throughamdgpu_mes_lock.Yeah which is exactly the reason why
mes_userq_reset_queue()should *NOT* be called from non IOCTL context.I think it is not about whether called from IOCTL, it is a common racing we should fix, and holding a kref is a low haning fruit.
Quoted 3 lines
Quoted 2 lines
Another thing, out of the topic is, holding
xa_lockdoes not guarantee fpriv/userq_mgr alive, for example, whendrm_device->unpluggedis true, all amdgpu teardown paths inamdgpu_drm_releaseare skipped, and the fpriv/userq_mgr is freed, no matter whether holding the xa spin lock.That would clearly be a massive bug. Those objects still need to be cleaned up independent of device hot plug.
I agree, when unplugged == true, means can not access any HW registers, so this bug deserve another series to fix.
Quoted 5 lines
Quoted 4 lines
So IMHO since we have
userq->kref, lets use it to maintain the lifecycle of the queues.Quoted 1 line
Grabbing references from this side would obviously result in circle dependencies.
I am not sure, we should use the lock/unlock and kref_put/get in pairs in sequence, can you name some circle dependencies or AB-BA lockings as examples?
That is not AB-BA locking, but circle dependencies. E.g. A reference B, B referencing C, C referencing A again.
A kref is an atomic counter, not a lock, that means we can try hold more than one kref in a thread, and a kref not depend on another. As far as I can see, current amdgpu driver does not have such problems, do you see any occurrences?
Quoted 7 lines
Quoted 4 lines
Hello Christian,
I have not heard back from you for three weeks, I wonder whether you have any further comments for this series, or shall I make any improvements?
Well just completely drop that series. As I said this approach is a fundamental no-go from my side.
As far as I can see we have solved the problems at hand and the rules how to handle the user queues should be pretty clear by now.
No, there are still some UAF issues. For example, what if the user space free a user queue while hang detector reseting it?
And, do you see any protections in mes_userq_reset_queue?
This series fixes these bugs.
Quoted 79 lines
Regards,
Christian.Quoted 75 lines
Thanks
LingshanQuoted 72 lines
Thanks
LingshanQuoted 69 lines
Regards,
Christian.Quoted 66 lines
Thanks
LingshanQuoted 63 lines
Regards,
Christian.Quoted 60 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Re: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
In reply to Christian KönigQuoted 42 lines · Christian KKKnig
Quoted 41 lines · Lingshan
Quoted 38 lines · Lingshan
Quoted 14 lines · Christian König
Quoted 13 lines · Lingshan
Quoted 7 lines · Christian König
Quoted 6 lines · Zhu Lingshan
This commit introduces a new helper
amdgpu_lookup_queue_by_doorbellwhich helps
look up a user queue with the given doorbell id
in a xarray.This function takes a kref of the user space queue.
Hello Christian
Thanks for your comments.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Well absolutely clear NAK to the whole approach.
This is the nonsense Sunil and I have worked quite hard to remove and we certainly shouldn't repeat such mistakes.
When the userq needs to be used from interrupt context we need to hold the
xa_lock_irqsave()or otherwise we don't have any guarantee that the userq,userq_mgror associated fpriv went out of scope.Holding the spin lock by
xa_lock_irqsave()can surely avoid racing with the destruction process, however, it does not apply to all scenarios, for example, you can not hold spin lock inmes_userq_reset_queue(), because it calls eitheramdgpu_mes_reset_queue_mmiooramdgpu_mes_reset_queue_mmio, both of them acquire the MES mutex throughamdgpu_mes_lock.Yeah which is exactly the reason why
mes_userq_reset_queue()should *NOT* be called from non IOCTL context.I think it is not about whether called from IOCTL, it is a common racing we should fix, and holding a kref is a low haning fruit.
Quoted 3 lines
Quoted 2 lines
Another thing, out of the topic is, holding
xa_lockdoes not guarantee fpriv/userq_mgr alive, for example, whendrm_device->unpluggedis true, all amdgpu teardown paths inamdgpu_drm_releaseare skipped, and the fpriv/userq_mgr is freed, no matter whether holding the xa spin lock.That would clearly be a massive bug. Those objects still need to be cleaned up independent of device hot plug.
I agree, when unplugged == true, means can not access any HW registers, so this bug deserve another series to fix.
Quoted 5 lines
Quoted 4 lines
So IMHO since we have
userq->kref, lets use it to maintain the lifecycle of the queues.Quoted 1 line
Grabbing references from this side would obviously result in circle dependencies.
I am not sure, we should use the lock/unlock and kref_put/get in pairs in sequence, can you name some circle dependencies or AB-BA lockings as examples?
That is not AB-BA locking, but circle dependencies. E.g. A reference B, B referencing C, C referencing A again.
A kref is an atomic counter, not a lock, that means we can try hold more than one kref in a thread, and a kref not depend on another. As far as I can see, current amdgpu driver does not have such problems, do you see any occurrences?
Quoted 9 lines
Quoted 4 lines
Hello Christian,
I have not heard back from you for three weeks, I wonder whether you have any further comments for this series, or shall I make any improvements?
Well just completely drop that series. As I said this approach is a fundamental no-go from my side.
As far as I can see we have solved the problems at hand and the rules how to handle the user queues should be pretty clear by now.
Assuming every kref_get() has a matching kref_put(), and the queue object itself does not participate in a circular ownership chain, I don't immediately see how taking a temporary queue reference would cause correctness issues.
Could you please point us to the historical issue or the corresponding commits that led to removing this approach previously?
Thanks,
Ray
Quoted 80 lines
Regards,
Christian.Quoted 76 lines
Thanks
LingshanQuoted 72 lines
Thanks
LingshanQuoted 69 lines
Regards,
Christian.Quoted 66 lines
Thanks
LingshanQuoted 63 lines
Regards,
Christian.Quoted 60 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Christian König
maintainerRe: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
On 9/22/26 09:55, Huang Rui wrote:
...
Quoted 7 lines
Quoted 2 lines
As far as I can see we have solved the problems at hand and the rules how to handle the user queues should be pretty clear by now.
Assuming every
kref_get()has a matchingkref_put(), and the queue object itself does not participate in a circular ownership chain, I don't immediately see how taking a temporary queue reference would cause correctness issues.
The reference on the user queue object is meant to keep it alive for the duration of IOCTLs, e.g. prevent concurrent destruction from the close IOCTL.
It CAN'T be used to grab references from either atomic, interrupt or worker context since then you can't drop the reference any more without risking destruction from non IOCTL context which in turn causes tons of lifetime problems.
This is just standard refcounting rules to keep objects alive during an IOCTL.
Quoted 2 lines
Could you please point us to the historical issue or the corresponding commits that led to removing this approach previously?
See Sunils original approach, he made exactly the same mistake and I already had to fix that back then.
I don't remember if it was actually committed or if we only discussed that on the mailing list.
Regards,
Christian.
Quoted 3 lines
Thanks,
Ray
Re: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 9 lines · Christian König
On 9/22/26 09:55, Huang Rui wrote:
...Quoted 6 lines
Quoted 2 lines
As far as I can see we have solved the problems at hand and the rules how to handle the user queues should be pretty clear by now.
Assuming every
kref_get()has a matchingkref_put(), and the queue object itself does not participate in a circular ownership chain, I don't immediately see how taking a temporary queue reference would cause correctness issues.The reference on the user queue object is meant to keep it alive for the duration of IOCTLs, e.g. prevent concurrent destruction from the close IOCTL.
why only IOCTL???? Since the kref has been embedded in the struct user queue, it should keep the queue alive during ANY OPERATIONS, Not only IOCTL. Or it will race with the destruction callback in kref_put, and causing UAF problems.
Do you see any kernel documents says that kref should only be used in ioctl contexts?
Quoted 4 lines
It CAN'T be used to grab references from either atomic, interrupt or worker context since then you can't drop the reference any more without risking destruction from non IOCTL context which in turn causes tons of lifetime problems.
This is just standard refcounting rules to keep objects alive during an IOCTL.
Where do you see such standard? It is just a atomic number, not a lock.
Quoted 11 lines
Quoted 2 lines
Could you please point us to the historical issue or the corresponding commits that led to removing this approach previously?
See Sunils original approach, he made exactly the same mistake and I already had to fix that back then.
I don't remember if it was actually committed or if we only discussed that on the mailing list.
Regards,
Christian.Quoted 2 lines
Thanks,
Ray
Christian König
maintainerRe: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 12 lines · Lingshan
Quoted 9 lines · Christian König
On 9/22/26 09:55, Huang Rui wrote:
...Quoted 6 lines
Quoted 2 lines
As far as I can see we have solved the problems at hand and the rules how to handle the user queues should be pretty clear by now.
Assuming every
kref_get()has a matchingkref_put(), and the queue object itself does not participate in a circular ownership chain, I don't immediately see how taking a temporary queue reference would cause correctness issues.The reference on the user queue object is meant to keep it alive for the duration of IOCTLs, e.g. prevent concurrent destruction from the close IOCTL.
Quoted 7 lines
why only IOCTL???? Since the kref has been embedded in the struct user queue, it should keep the queue alive during ANY OPERATIONS, Not only IOCTL. Or it will race with the destruction callback in
kref_put, and causing UAF problems.Do you see any kernel documents says that kref should only be used in ioctl contexts?
We just cycle over the same questions multiple times now. To limit the reference count to the IOCTL is a design decision I made.
We just cycle over the same questions multiple times now. To limit the reference count to the IOCTL is a design decision I made.
I tried to explain the rational behind that design decision multiple times now but it obviously doesn't seem to be understood.
So to stop wasting my time I'm stopping to reply to this any further.
This patch set is rejected.
Regards,
Christian.
Quoted 16 lines
Quoted 4 lines
It CAN'T be used to grab references from either atomic, interrupt or worker context since then you can't drop the reference any more without risking destruction from non IOCTL context which in turn causes tons of lifetime problems.
This is just standard refcounting rules to keep objects alive during an IOCTL.
Where do you see such standard? It is just a atomic number, not a lock.
Quoted 11 lines
Quoted 2 lines
Could you please point us to the historical issue or the corresponding commits that led to removing this approach previously?
See Sunils original approach, he made exactly the same mistake and I already had to fix that back then.
I don't remember if it was actually committed or if we only discussed that on the mailing list.
Regards,
Christian.Quoted 2 lines
Thanks,
Ray
Re: [PATCH 01/10] drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
Quoted 17 lines · Christian König
Quoted 10 lines · Lingshan
Quoted 8 lines · Christian König
On 9/22/26 09:55, Huang Rui wrote:
...Quoted 6 lines
Quoted 2 lines
As far as I can see we have solved the problems at hand and the rules how to handle the user queues should be pretty clear by now.
Assuming every
kref_get()has a matchingkref_put(), and the queue object itself does not participate in a circular ownership chain, I don't immediately see how taking a temporary queue reference would cause correctness issues.Quoted 6 lines
Quoted 1 line
The reference on the user queue object is meant to keep it alive for the duration of IOCTLs, e.g. prevent concurrent destruction from the close IOCTL.
why only IOCTL???? Since the kref has been embedded in the struct user queue, it should keep the queue alive during ANY OPERATIONS, Not only IOCTL. Or it will race with the destruction callback in
kref_put, and causing UAF problems.Do you see any kernel documents says that kref should only be used in ioctl contexts?
We just cycle over the same questions multiple times now. To limit the reference count to the IOCTL is a design decision I made.
Quoted 7 lines
We just cycle over the same questions multiple times now. To limit the reference count to the IOCTL is a design decision I made.
I tried to explain the rational behind that design decision multiple times now but it obviously doesn't seem to be understood.
So to stop wasting my time I'm stopping to reply to this any further.
This patch set is rejected.
This design is wrong, see kernel documents core-api/kref.rst:
krefs allow you to add reference counters to your objects. If you have objects that are used in multiple places and passed around, and you don't have refcounts, your code is almost certainly broken. If you want refcounts, krefs are the way to go.
Quoted 19 lines
Regards,
Christian.Quoted 15 lines
Quoted 3 lines
It CAN'T be used to grab references from either atomic, interrupt or worker context since then you can't drop the reference any more without risking destruction from non IOCTL context which in turn causes tons of lifetime problems.
This is just standard refcounting rules to keep objects alive during an IOCTL.
Where do you see such standard? It is just a atomic number, not a lock.
Quoted 11 lines
Quoted 2 lines
Could you please point us to the historical issue or the corresponding commits that led to removing this approach previously?
See Sunils original approach, he made exactly the same mistake and I already had to fix that back then.
I don't remember if it was actually committed or if we only discussed that on the mailing list.
Regards,
Christian.Quoted 2 lines
Thanks,
Ray
PATCH 2/10 drm/amdgpu: keep the userq manager alive as long as its queues
In reply to Zhu LingshanThe life cycle of a user queue is managed by its kref. However when destroy a userq manager, the kref_put of its queues in amdgpu_userq_mgr_fini may not be the last put, therefore the queues could be still alive after the userq manager has been destroyed, resulting in userq->userq_mgr use-after-free issues.
This commit fixes this problem by introduce a new counter refs representing for the number of its queues, and only free the userq_manager when refs == 0
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 9 +++++++ 2 files changed, 39 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30 -0
@@ -27,6 +27,7 @@#include <linux/pm_runtime.h> #include <linux/overflow.h> #include <drm/drm_drv.h> +#include <linux/wait_bit.h> #include "amdgpu.h" #include "amdgpu_reset.h"
@@ -533,6 +534,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,return r; } +static void amdgpu_userq_mgr_inc_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +atomic_inc (&uq_mgr->refs); +} + +static void amdgpu_userq_mgr_dec_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +if (atomic_dec_and_test (&uq_mgr->refs)) +wake_up_var (&uq_mgr->refs); +} +static int amdgpu_userq_destroy (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue) {
@@ -594,6 +606,8 @@ static void amdgpu_userq_kref_destroy(struct kref *kref) r =amdgpu_userq_destroy (uq_mgr, queue);if (r)drm_file_err (uq_mgr->file,"Failed to destroy usermode queue %d\n" , r); + +amdgpu_userq_mgr_dec_refs (uq_mgr); }struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid)
@@ -707,6 +721,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->xcp_id = (fpriv->xcp_id !=AMDGPU_XCP_NO_PARTITION ) ? fpriv->xcp_id :0 ; queue->userq_mgr = uq_mgr; +amdgpu_userq_mgr_inc_refs (uq_mgr);INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work);
@@ -819,6 +834,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) free_queue:trace_amdgpu_userq_create_end (queue, r);kfree (queue); +amdgpu_userq_mgr_dec_refs (uq_mgr); err_pm_runtime:pm_runtime_put_autosuspend (adev_to_drm (adev)->dev);return r;
@@ -1331,6 +1347,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f {mutex_init (&userq_mgr->userq_mutex);xa_init_flags (&userq_mgr->userq_xa,XA_FLAGS_ALLOC ); +atomic_set (&userq_mgr->refs,0 ); userq_mgr->adev = adev; userq_mgr->file = file_priv; userq_mgr->proc_ctx_allocated = false;
@@ -1380,6 +1397,19 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)amdgpu_userq_put (queue); } +/* +* The above amdgpu_userq_put() may not be the last put +* of the kref of a user queue, therefore there could +* be some queues still alive even when the userq manager +* has been destroyed. This wait_evet() blocks +* amdgpu_userq_mgr_fini(), so keep userq_mgr alive +* while any queues holding it. +* +* This prevents queue->userq_mgr use-after-free issues. +*/ +wait_var_event (&userq_mgr->refs, + !atomic_read_acquire (&userq_mgr->refs)); +xa_destroy (&userq_mgr->userq_xa);/*
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +9 -0
@@ -126,6 +126,15 @@ struct amdgpu_userq_mgr {*/ struct xarray userq_xa;struct mutex userq_mutex; + +/** +* @refs: +* +* Each queue increases this counter when join this manager, +* and decreases it when leave this manager. +*/ +atomic_t refs; +struct amdgpu_device *adev;struct delayed_work resume_work;struct drm_file *file;
Signature · 1 line
2.53.0
Christian König
maintainerRe: [PATCH 02/10] drm/amdgpu: keep the userq manager alive as long as its queues
Quoted 11 lines · Zhu Lingshan
The life cycle of a user queue is managed by its kref. However when destroy a userq manager, the
kref_putof its queues inamdgpu_userq_mgr_finimay not be the last put, therefore the queues could be still alive after the userq manager has been destroyed, resulting inuserq->userq_mgruse-after-free issues.This commit fixes this problem by introduce a new counter refs representing for the number of its queues, and only free the
userq_managerwhen refs == 0
Clear NAK to that one as well, this is just nonsense.
Christian.
Quoted 110 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 9 +++++++ 2 files changed, 39 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -27,6 +27,7 @@#include <linux/pm_runtime.h> #include <linux/overflow.h> #include <drm/drm_drv.h> +#include <linux/wait_bit.h> #include "amdgpu.h" #include "amdgpu_reset.h" @@ -533,6 +534,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,return r; } +static void amdgpu_userq_mgr_inc_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +atomic_inc (&uq_mgr->refs); +} + +static void amdgpu_userq_mgr_dec_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +if (atomic_dec_and_test (&uq_mgr->refs)) +wake_up_var (&uq_mgr->refs); +} +static int amdgpu_userq_destroy (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue) {@@ -594,6 +606,8 @@ static void amdgpu_userq_kref_destroy(struct kref *kref) r =amdgpu_userq_destroy (uq_mgr, queue);if (r)drm_file_err (uq_mgr->file,"Failed to destroy usermode queue %d\n" , r); + +amdgpu_userq_mgr_dec_refs (uq_mgr); }struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid)@@ -707,6 +721,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->xcp_id = (fpriv->xcp_id !=AMDGPU_XCP_NO_PARTITION ) ? fpriv->xcp_id :0 ; queue->userq_mgr = uq_mgr; +amdgpu_userq_mgr_inc_refs (uq_mgr);INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work);@@ -819,6 +834,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) free_queue:trace_amdgpu_userq_create_end (queue, r);kfree (queue); +amdgpu_userq_mgr_dec_refs (uq_mgr); err_pm_runtime:pm_runtime_put_autosuspend (adev_to_drm (adev)->dev);return r;@@ -1331,6 +1347,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f {mutex_init (&userq_mgr->userq_mutex);xa_init_flags (&userq_mgr->userq_xa,XA_FLAGS_ALLOC ); +atomic_set (&userq_mgr->refs,0 ); userq_mgr->adev = adev; userq_mgr->file = file_priv; userq_mgr->proc_ctx_allocated = false;@@ -1380,6 +1397,19 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)amdgpu_userq_put (queue); } +/* +* The above amdgpu_userq_put() may not be the last put +* of the kref of a user queue, therefore there could +* be some queues still alive even when the userq manager +* has been destroyed. This wait_evet() blocks +* amdgpu_userq_mgr_fini(), so keep userq_mgr alive +* while any queues holding it. +* +* This prevents queue->userq_mgr use-after-free issues. +*/ +wait_var_event (&userq_mgr->refs, + !atomic_read_acquire (&userq_mgr->refs)); +xa_destroy (&userq_mgr->userq_xa);/* drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +9
-0@@ -126,6 +126,15 @@ struct amdgpu_userq_mgr {*/ struct xarray userq_xa;struct mutex userq_mutex; + +/** +* @refs: +* +* Each queue increases this counter when join this manager, +* and decreases it when leave this manager. +*/ +atomic_t refs; +struct amdgpu_device *adev;struct delayed_work resume_work;struct drm_file *file;
Re: [PATCH 02/10] drm/amdgpu: keep the userq manager alive as long as its queues
Quoted 9 lines · Christian König
Quoted 8 lines · Zhu Lingshan
The life cycle of a user queue is managed by its kref. However when destroy a userq manager, the
kref_putof its queues inamdgpu_userq_mgr_finimay not be the last put, therefore the queues could be still alive after the userq manager has been destroyed, resulting inuserq->userq_mgruse-after-free issues.
Quoted 4 lines
Quoted 3 lines
This commit fixes this problem by introduce a new counter refs representing for the number of its queues, and only free the
userq_managerwhen refs == 0Clear NAK to that one as well, this is just nonsense.
It could be better to have some explanations.
I am not sure how to guarantee the put_kref in amdgpu_userq_mgr_fini is the last put and result in kref == 0, if not the last one, there can be userq->userq_mgr UAF bugs.
Thanks
Lingshan
Quoted 112 lines
Christian.
Quoted 109 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 9 +++++++ 2 files changed, 39 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -27,6 +27,7 @@#include <linux/pm_runtime.h> #include <linux/overflow.h> #include <drm/drm_drv.h> +#include <linux/wait_bit.h> #include "amdgpu.h" #include "amdgpu_reset.h" @@ -533,6 +534,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,return r; } +static void amdgpu_userq_mgr_inc_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +atomic_inc (&uq_mgr->refs); +} + +static void amdgpu_userq_mgr_dec_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +if (atomic_dec_and_test (&uq_mgr->refs)) +wake_up_var (&uq_mgr->refs); +} +static int amdgpu_userq_destroy (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue) {@@ -594,6 +606,8 @@ static void amdgpu_userq_kref_destroy(struct kref *kref) r =amdgpu_userq_destroy (uq_mgr, queue);if (r)drm_file_err (uq_mgr->file,"Failed to destroy usermode queue %d\n" , r); + +amdgpu_userq_mgr_dec_refs (uq_mgr); }struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid)@@ -707,6 +721,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->xcp_id = (fpriv->xcp_id !=AMDGPU_XCP_NO_PARTITION ) ? fpriv->xcp_id :0 ; queue->userq_mgr = uq_mgr; +amdgpu_userq_mgr_inc_refs (uq_mgr);INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work);@@ -819,6 +834,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) free_queue:trace_amdgpu_userq_create_end (queue, r);kfree (queue); +amdgpu_userq_mgr_dec_refs (uq_mgr); err_pm_runtime:pm_runtime_put_autosuspend (adev_to_drm (adev)->dev);return r;@@ -1331,6 +1347,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f {mutex_init (&userq_mgr->userq_mutex);xa_init_flags (&userq_mgr->userq_xa,XA_FLAGS_ALLOC ); +atomic_set (&userq_mgr->refs,0 ); userq_mgr->adev = adev; userq_mgr->file = file_priv; userq_mgr->proc_ctx_allocated = false;@@ -1380,6 +1397,19 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)amdgpu_userq_put (queue); } +/* +* The above amdgpu_userq_put() may not be the last put +* of the kref of a user queue, therefore there could +* be some queues still alive even when the userq manager +* has been destroyed. This wait_evet() blocks +* amdgpu_userq_mgr_fini(), so keep userq_mgr alive +* while any queues holding it. +* +* This prevents queue->userq_mgr use-after-free issues. +*/ +wait_var_event (&userq_mgr->refs, + !atomic_read_acquire (&userq_mgr->refs)); +xa_destroy (&userq_mgr->userq_xa);/* drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +9
-0@@ -126,6 +126,15 @@ struct amdgpu_userq_mgr {*/ struct xarray userq_xa;struct mutex userq_mutex; + +/** +* @refs: +* +* Each queue increases this counter when join this manager, +* and decreases it when leave this manager. +*/ +atomic_t refs; +struct amdgpu_device *adev;struct delayed_work resume_work;struct drm_file *file;
Christian König
maintainerRe: [PATCH 02/10] drm/amdgpu: keep the userq manager alive as long as its queues
Quoted 14 lines · Lingshan
Quoted 12 lines · Christian König
Quoted 11 lines · Zhu Lingshan
The life cycle of a user queue is managed by its kref. However when destroy a userq manager, the
kref_putof its queues inamdgpu_userq_mgr_finimay not be the last put, therefore the queues could be still alive after the userq manager has been destroyed, resulting inuserq->userq_mgruse-after-free issues.This commit fixes this problem by introduce a new counter refs representing for the number of its queues, and only free the
userq_managerwhen refs == 0
Quoted 7 lines
Quoted 1 line
Clear NAK to that one as well, this is just nonsense.
It could be better to have some explanations.
I am not sure how to guarantee the
put_krefinamdgpu_userq_mgr_finiis the last put and result in kref == 0, if not the last one, there can beuserq->userq_mgrUAF bugs.
The rules are actually pretty simple:
The reference is for keeping the userq alive while IOCTLs happen. And IOCTL can only happen while the file and therefor the fpriv, userq_mgr etc... are still alive.
What can potentially be is that we also need to grab a reference from a work item, but in this case the fpriv/userq_mgr cleanup functions just need to cancel and wait for the work to finish.
There should *never* be a reference grabbed from interrupt context, explicitely because releasing that reference is also not possible from interrupt context. Instead xa_lock_irqsave() needs to be used to make sure that the userq stays alive while the interrupt processing happens.
Regards,
Christian.
Quoted 115 lines
Thanks
LingshanQuoted 111 lines
Christian.
Quoted 109 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 9 +++++++ 2 files changed, 39 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -27,6 +27,7 @@#include <linux/pm_runtime.h> #include <linux/overflow.h> #include <drm/drm_drv.h> +#include <linux/wait_bit.h> #include "amdgpu.h" #include "amdgpu_reset.h" @@ -533,6 +534,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,return r; } +static void amdgpu_userq_mgr_inc_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +atomic_inc (&uq_mgr->refs); +} + +static void amdgpu_userq_mgr_dec_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +if (atomic_dec_and_test (&uq_mgr->refs)) +wake_up_var (&uq_mgr->refs); +} +static int amdgpu_userq_destroy (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue) {@@ -594,6 +606,8 @@ static void amdgpu_userq_kref_destroy(struct kref *kref) r =amdgpu_userq_destroy (uq_mgr, queue);if (r)drm_file_err (uq_mgr->file,"Failed to destroy usermode queue %d\n" , r); + +amdgpu_userq_mgr_dec_refs (uq_mgr); }struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid)@@ -707,6 +721,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->xcp_id = (fpriv->xcp_id !=AMDGPU_XCP_NO_PARTITION ) ? fpriv->xcp_id :0 ; queue->userq_mgr = uq_mgr; +amdgpu_userq_mgr_inc_refs (uq_mgr);INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work);@@ -819,6 +834,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) free_queue:trace_amdgpu_userq_create_end (queue, r);kfree (queue); +amdgpu_userq_mgr_dec_refs (uq_mgr); err_pm_runtime:pm_runtime_put_autosuspend (adev_to_drm (adev)->dev);return r;@@ -1331,6 +1347,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f {mutex_init (&userq_mgr->userq_mutex);xa_init_flags (&userq_mgr->userq_xa,XA_FLAGS_ALLOC ); +atomic_set (&userq_mgr->refs,0 ); userq_mgr->adev = adev; userq_mgr->file = file_priv; userq_mgr->proc_ctx_allocated = false;@@ -1380,6 +1397,19 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)amdgpu_userq_put (queue); } +/* +* The above amdgpu_userq_put() may not be the last put +* of the kref of a user queue, therefore there could +* be some queues still alive even when the userq manager +* has been destroyed. This wait_evet() blocks +* amdgpu_userq_mgr_fini(), so keep userq_mgr alive +* while any queues holding it. +* +* This prevents queue->userq_mgr use-after-free issues. +*/ +wait_var_event (&userq_mgr->refs, + !atomic_read_acquire (&userq_mgr->refs)); +xa_destroy (&userq_mgr->userq_xa);/* drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +9
-0@@ -126,6 +126,15 @@ struct amdgpu_userq_mgr {*/ struct xarray userq_xa;struct mutex userq_mutex; + +/** +* @refs: +* +* Each queue increases this counter when join this manager, +* and decreases it when leave this manager. +*/ +atomic_t refs; +struct amdgpu_device *adev;struct delayed_work resume_work;struct drm_file *file;
Re: [PATCH 02/10] drm/amdgpu: keep the userq manager alive as long as its queues
Quoted 21 lines · Christian König
Quoted 14 lines · Lingshan
Quoted 12 lines · Christian König
Quoted 11 lines · Zhu Lingshan
The life cycle of a user queue is managed by its kref. However when destroy a userq manager, the
kref_putof its queues inamdgpu_userq_mgr_finimay not be the last put, therefore the queues could be still alive after the userq manager has been destroyed, resulting inuserq->userq_mgruse-after-free issues.This commit fixes this problem by introduce a new counter refs representing for the number of its queues, and only free the
userq_managerwhen refs == 0Quoted 6 lines
Quoted 1 line
Clear NAK to that one as well, this is just nonsense.
It could be better to have some explanations.
I am not sure how to guarantee the
put_krefinamdgpu_userq_mgr_finiis the last put and result in kref == 0, if not the last one, there can beuserq->userq_mgrUAF bugs.
Quoted 7 lines
The rules are actually pretty simple:
The reference is for keeping the userq alive while IOCTLs happen. And IOCTL can only happen while the file and therefor the fpriv,
userq_mgretc... are still alive.What can potentially be is that we also need to grab a reference from a work item, but in this case the fpriv/userq_mgr cleanup functions just need to cancel and wait for the work to finish.
There should *never* be a reference grabbed from interrupt context, explicitely because releasing that reference is also not possible from interrupt context. Instead
xa_lock_irqsave()needs to be used to make sure that the userq stays alive while the interrupt processing happens.
To protect a user queue from unexpected free and use-after-free, we either use xa_lock or the kref.
Please note, currently the xa lock can work only because the user queue destruction routine also acquires the xa lock, and this is not always true.
However in some code paths, none of kref or xa_lock are employed, for example, in mes_userq_reset_queue, its just xa_for_each, no locking, no kref. That is the problem this series intends to fix (UAF issues, racing with the destroyer).
And I believe we should choose kref rather than xa lock, the reasons:
1) in some code paths, like mes_userq_reset_queue, amdgpu_userq_stop_sched_for_enforce_isolation and amdgpu_userq_start_sched_for_enforce_isolation, they all acquire a mutex, may sleep, so can not use xa lock in such routines
2) xa lock is for the XArray, it serializes the access to the queues, means when lock the xarray, only one thread can process one queue, it is a performance degradation.
3) xa lock is a spin lock, so don't hold it for long time while processing a queue.
4) since the struct kref is embedded in user queue, it introduces a new destruction process of the queue, which is invoked when kref == 0,
so we should take kref in every routine that process the queue to avoid UAF.
So this patch is a prepare for the following patches, after fixing the user queue UAF issues, there would be more code paths get and put the kref to protect the queues, therefore, the put in amdgpu_userq_mgr_fini may not be the last put, and amdgpu_userq_mgr_fini destroys the userq manager, resulting in userq->userq_mgr use-after-free, that is why we need this improvement.
Thanks
Lingshan
Quoted 118 lines
Regards,
Christian.Quoted 114 lines
Thanks
LingshanQuoted 111 lines
Christian.
Quoted 109 lines
Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 9 +++++++ 2 files changed, 39 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30
-0@@ -27,6 +27,7 @@#include <linux/pm_runtime.h> #include <linux/overflow.h> #include <drm/drm_drv.h> +#include <linux/wait_bit.h> #include "amdgpu.h" #include "amdgpu_reset.h" @@ -533,6 +534,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,return r; } +static void amdgpu_userq_mgr_inc_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +atomic_inc (&uq_mgr->refs); +} + +static void amdgpu_userq_mgr_dec_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +if (atomic_dec_and_test (&uq_mgr->refs)) +wake_up_var (&uq_mgr->refs); +} +static int amdgpu_userq_destroy (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue) {@@ -594,6 +606,8 @@ static void amdgpu_userq_kref_destroy(struct kref *kref) r =amdgpu_userq_destroy (uq_mgr, queue);if (r)drm_file_err (uq_mgr->file,"Failed to destroy usermode queue %d\n" , r); + +amdgpu_userq_mgr_dec_refs (uq_mgr); }struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid)@@ -707,6 +721,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->xcp_id = (fpriv->xcp_id !=AMDGPU_XCP_NO_PARTITION ) ? fpriv->xcp_id :0 ; queue->userq_mgr = uq_mgr; +amdgpu_userq_mgr_inc_refs (uq_mgr);INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work);@@ -819,6 +834,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) free_queue:trace_amdgpu_userq_create_end (queue, r);kfree (queue); +amdgpu_userq_mgr_dec_refs (uq_mgr); err_pm_runtime:pm_runtime_put_autosuspend (adev_to_drm (adev)->dev);return r;@@ -1331,6 +1347,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f {mutex_init (&userq_mgr->userq_mutex);xa_init_flags (&userq_mgr->userq_xa,XA_FLAGS_ALLOC ); +atomic_set (&userq_mgr->refs,0 ); userq_mgr->adev = adev; userq_mgr->file = file_priv; userq_mgr->proc_ctx_allocated = false;@@ -1380,6 +1397,19 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)amdgpu_userq_put (queue); } +/* +* The above amdgpu_userq_put() may not be the last put +* of the kref of a user queue, therefore there could +* be some queues still alive even when the userq manager +* has been destroyed. This wait_evet() blocks +* amdgpu_userq_mgr_fini(), so keep userq_mgr alive +* while any queues holding it. +* +* This prevents queue->userq_mgr use-after-free issues. +*/ +wait_var_event (&userq_mgr->refs, + !atomic_read_acquire (&userq_mgr->refs)); +xa_destroy (&userq_mgr->userq_xa);/* drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +9
-0@@ -126,6 +126,15 @@ struct amdgpu_userq_mgr {*/ struct xarray userq_xa;struct mutex userq_mutex; + +/** +* @refs: +* +* Each queue increases this counter when join this manager, +* and decreases it when leave this manager. +*/ +atomic_t refs; +struct amdgpu_device *adev;struct delayed_work resume_work;struct drm_file *file;
PATCH 3/10 drm/amdgpu/gfx11: hold userq refs in private fault worker
In reply to Zhu LingshanThe GFX11 user queue private fault woker loads the relevnt user queue from the userq doorbell xarray. However it does not hold the spin_lock of the xarray when walking the xarray, and does not increase the kref of the user queue, so it races with queue destruction path and may run into an use-after-free userq problem.
This commit fixes this UAF problem by utilizing amdgpu_lookup_queue_by_doorbell helper, which properly hoding the xarray spin lock and the kref of the user queue.
--- drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +5 -2
@@ -6733,9 +6733,12 @@ static void gfx_v11_0_userq_priv_fault_work(struct work_struct *work) doorbell = (db_ctrl &CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET_MASK ) >>CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT ; - q =xa_load (&adev->userq_doorbell_xa, doorbell); -if (q) + q =amdgpu_lookup_queue_by_doorbell (&adev->userq_doorbell_xa, + doorbell); +if (q) {amdgpu_userq_start_hang_detect_work (q); +amdgpu_userq_put (q); + } } }
Signature · 1 line
2.53.0
Christian König
maintainerRe: [PATCH 03/10] drm/amdgpu/gfx11: hold userq refs in private fault worker
Quoted 24 lines · Zhu Lingshan
The GFX11 user queue private fault woker loads the relevnt user queue from the userq doorbell xarray. However it does not hold the
spin_lockof the xarray when walking the xarray, and does not increase the kref of the user queue, so it races with queue destruction path and may run into an use-after-free userq problem.This commit fixes this UAF problem by utilizing
amdgpu_lookup_queue_by_doorbellhelper, which properly hoding the xarray spin lock and the kref of the user queue.Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0
-0@@ -6733,9 +6733,12 @@ static void gfx_v11_0_userq_priv_fault_work(struct work_struct *work)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static void gfx_v11_0_userq_priv_fault_work(struct work_struct *work) · line 6741
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +5
-2doorbell = (db_ctrl &CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET_MASK ) >>CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT ; - q =xa_load (&adev->userq_doorbell_xa, doorbell); -if (q) + q =amdgpu_lookup_queue_by_doorbell (&adev->userq_doorbell_xa, + doorbell); +if (q) {amdgpu_userq_start_hang_detect_work (q); +amdgpu_userq_put (q); + }
This must just lock the XA irqsave, this code has been suggested like that before and been removed because it doesn't work correctly.
Please check the git history before suggesting bringing back a buggy approach.
Christian.
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0
-0} }
Re: [PATCH 03/10] drm/amdgpu/gfx11: hold userq refs in private fault worker
Quoted 25 lines · Christian König
Quoted 24 lines · Zhu Lingshan
The GFX11 user queue private fault woker loads the relevnt user queue from the userq doorbell xarray. However it does not hold the
spin_lockof the xarray when walking the xarray, and does not increase the kref of the user queue, so it races with queue destruction path and may run into an use-after-free userq problem.This commit fixes this UAF problem by utilizing
amdgpu_lookup_queue_by_doorbellhelper, which properly hoding the xarray spin lock and the kref of the user queue.Signed-off-by Zhu Lingshan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0
-0@@ -6733,9 +6733,12 @@ static void gfx_v11_0_userq_priv_fault_work(struct work_struct *work)
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static void gfx_v11_0_userq_priv_fault_work(struct work_struct *work) · line 6741
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +5
-2doorbell = (db_ctrl &CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET_MASK ) >>CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT ; - q =xa_load (&adev->userq_doorbell_xa, doorbell); -if (q) + q =amdgpu_lookup_queue_by_doorbell (&adev->userq_doorbell_xa, + doorbell); +if (q) {amdgpu_userq_start_hang_detect_work (q); +amdgpu_userq_put (q); + }This must just lock the XA irqsave, this code has been suggested like that before and been removed because it doesn't work correctly.\
The xa spin lock is held in the helper amdgpu_lookup_queue_by_doorbell().
Please see patch 1:
38 +amdgpu_lookup_queue_by_doorbell(struct xarray *xa, u32 doorbell)
39 +{
40 + struct amdgpu_usermode_queue *queue;
41 + unsigned long flags;
42 +
43 + xa_lock_irqsave(xa, flags);
Thanks
Lingshan
Quoted 8 lines
Please check the git history before suggesting bringing back a buggy approach.
Christian.
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0
-0} }
PATCH 4/10 drm/amdgpu/gfx12: hold userq refs in private fault worker
In reply to Zhu LingshanThe GFX12 user queue private fault woker loads the relevnt user queue from the userq doorbell xarray. However it does not hold the spin_lock of the xarray when walking the xarray, and does not increase the kref of the user queue, so it races with queue destruction path and may run into an use-after-free userq problem.
This commit fixes this UAF problem by utilizing amdgpu_lookup_queue_by_doorbell helper, which properly hoding the xarray spin lock and the kref of the user queue.
--- drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +5 -2
@@ -5070,9 +5070,12 @@ static void gfx_v12_0_userq_priv_fault_work(struct work_struct *work) doorbell = (db_ctrl &CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET_MASK ) >>CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT ; - q =xa_load (&adev->userq_doorbell_xa, doorbell); -if (q) + q =amdgpu_lookup_queue_by_doorbell (&adev->userq_doorbell_xa, + doorbell); +if (q) {amdgpu_userq_start_hang_detect_work (q); +amdgpu_userq_put (q); + } } }
Signature · 1 line
2.53.0
PATCH 5/10 drm/amdgpu: implement asynchronous userq destruction routine
In reply to Zhu LingshanThe last kref of a user queue may be released
from a context where can not sleep or
require an alrady hold lock(reset_domain->sem, reset_mutext, etc) in the same context.
This commit helps resovle these issues by refactoring the user queue destruction function to a deferred work item, so it does not conflict with the caller context.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 15 ++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 7 +++++++ 2 files changed, 19 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12 -3
@@ -596,11 +596,11 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_quereturn r; } -static void amdgpu_userq_kref_destroy (struct kref *kref) +static void amdgpu_userq_destroy_worker (struct work_struct *work) {int r;struct amdgpu_usermode_queue *queue = - container_of(kref, struct amdgpu_usermode_queue, refcount); + container_of(work, struct amdgpu_usermode_queue, destroy_work);struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; r =amdgpu_userq_destroy (uq_mgr, queue);
@@ -610,6 +610,14 @@ static void amdgpu_userq_kref_destroy(struct kref *kref)amdgpu_userq_mgr_dec_refs (uq_mgr); } +static void amdgpu_userq_kref_release (struct kref *kref) +{ +struct amdgpu_usermode_queue *queue = + container_of(kref, struct amdgpu_usermode_queue, refcount); + +WARN_ON_ONCE (!queue_work (system_unbound_wq, &queue->destroy_work)); +} +struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid) {struct amdgpu_usermode_queue *queue;
@@ -656,7 +664,7 @@ amdgpu_lookup_queue_by_doorbell(struct xarray *xa, u32 doorbell)void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue) -kref_put (&queue->refcount, amdgpu_userq_kref_destroy); +kref_put (&queue->refcount, amdgpu_userq_kref_release); } static int amdgpu_userq_priority_permit(struct drm_file *filp,
@@ -724,6 +732,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)amdgpu_userq_mgr_inc_refs (uq_mgr);INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work); +INIT_WORK (&queue->destroy_work, amdgpu_userq_destroy_worker); r =amdgpu_userq_fence_driver_alloc (adev, &queue->fence_drv);if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7 -0
@@ -87,6 +87,13 @@ struct amdgpu_usermode_queue {* Delayed work which runs when userq_fences time out. */ struct delayed_work hang_detect_work; + +/** +* @destroy_work: +* +* Work struct used to destroy the queue when kref == 0 +*/ +struct work_struct destroy_work;struct kref refcount;union {
Signature · 1 line
2.53.0
PATCH 6/10 drm/amdgpu: hold userq kref in MES reset
In reply to Zhu LingshanCurrent mes_userq_reset_queue() walks the user queue doorbell xarray without holding its spin lock and does not take a queue->kref when if finds a queue. So the queue could be unexpectedly deconstructed and casuing an use-after-free problem.
This commit fixes this problem by using the helper amdgpu_lookup_queue_by_doorbell() to properly acquire the spin lock of the xarray, and hold its kref during processing the queue.
--- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 46 +++++++++++----------- 1 file changed, 23 insertions(+), 23 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +23 -23
@@ -244,30 +244,30 @@ int mes_userq_reset_queue(struct amdgpu_device *adev, {struct amdgpu_usermode_queue *uq;bool use_mmio = adev->gfx.mec.use_mmio_for_reset; -unsigned long uq_id; -int r; +int r =0 ; -xa_for_each (&adev->userq_doorbell_xa, uq_id, uq) { -if (uq->queue_type == queue_type) { -if (uq == guilty_uq) -continue ; -if (uq->doorbell_index == db) { - uq->state =AMDGPU_USERQ_STATE_HUNG ; -if (use_mmio) - r =amdgpu_mes_reset_queue_mmio (adev, queue_type,0 ,1 , pipe, queue,0 ); -else - r =amdgpu_mes_reset_user_queue (adev, queue_type, db,0 ); -if (r) -return r; - r =mes_userq_unmap (uq); -if (r) -return r; -amdgpu_userq_fence_driver_force_completion (uq); -break ; - } - } - } -return 0 ; + uq =amdgpu_lookup_queue_by_doorbell (&adev->userq_doorbell_xa, db); +if (!uq) +return 0 ; + +if (uq == guilty_uq || uq->queue_type != queue_type) + goto put_queue; + + uq->state =AMDGPU_USERQ_STATE_HUNG ; +if (use_mmio) + r =amdgpu_mes_reset_queue_mmio (adev, queue_type,0 ,1 , pipe, queue,0 ); +else + r =amdgpu_mes_reset_user_queue (adev, queue_type, db,0 ); +if (r) + goto put_queue; + + r =mes_userq_unmap (uq); +if (!r) +amdgpu_userq_fence_driver_force_completion (uq); + +put_queue: +amdgpu_userq_put (uq); +return r; } static int mes_userq_create_ctx_space(struct amdgpu_userq_mgr *uq_mgr,
Signature · 1 line
2.53.0
PATCH 7/10 drm/amdgpu: hold userq kref during isolation scheduling
In reply to Zhu LingshanThe enforce-isolation stop and start handlers iterate the doorbell XArray, but process a user queue without holding its kref. Therefore, a concurrent queue destruction process can free a queue before these paths finish their work, causing use-after-free problems.
This commit fixes this issue by introducing a new helper amdgpu_userq_xa_find() which finds a queue from a XArray and hold its kref, and employ this helper in the enforce-isolation stop and start handlers.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 72 +++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +68 -4
@@ -661,6 +661,54 @@ amdgpu_lookup_queue_by_doorbell(struct xarray *xa, u32 doorbell)return queue; } +/** +* amdgpu_userq_xa_find - search the XArray for a queue +* @xa: user queue XArray +* @index: first index to search, updated by every iteration of the search +* +* Return: a queue which has the lowest index that is at least @index, +* or NULL when no queue was found. +* +* The caller must release the kref of a queue with amdgpu_userq_put() after use. +*/ +static struct amdgpu_usermode_queue * +amdgpu_userq_xa_find (struct xarray *xa,unsigned long *index) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_find (xa, index,ULONG_MAX ,XA_PRESENT ); +while (queue) { +/* +* If found a queue but failed to get a kref, +* it means the queue is in destruction process, +* so skip it by continuing the loop. +* +* If get a kref of the queue, break the loop and return it. +*/ +if (kref_get_unless_zero (&queue->refcount)) +break ; + +/* +* If the index is ULONG_MAX, we have reached the end of the XArray. +* Break the loop and return NULL because ULONG_MAX + 1 = 0, +* which is the start of the XArray. +*/ +if (*index ==ULONG_MAX ) { + queue =NULL ; +break ; + } + + (*index)++; + queue =xa_find (xa, index,ULONG_MAX ,XA_PRESENT ); + } + +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)
@@ -1496,7 +1544,7 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue;struct amdgpu_userq_mgr *uqm; -unsigned long queue_id; +unsigned long queue_id =0 ;int ret =0 , r;/* only need to stop gfx/compute */
@@ -1506,7 +1554,8 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,if (adev->userq_halt_for_enforce_isolation)dev_warn (adev->dev,"userq scheduling already stopped!\n" ); adev->userq_halt_for_enforce_isolation = true; -xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, &queue_id); +while (queue) { uqm = queue->userq_mgr;cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex);
@@ -1518,6 +1567,13 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, ret = r; }mutex_unlock (&uqm->userq_mutex); +amdgpu_userq_put (queue); + +if (queue_id ==ULONG_MAX ) +break ; + queue_id++; + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, + &queue_id); }return ret;
@@ -1529,7 +1585,7 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue;struct amdgpu_userq_mgr *uqm; -unsigned long queue_id; +unsigned long queue_id =0 ;int ret =0 , r;/* only need to stop gfx/compute */
@@ -1541,7 +1597,8 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, adev->userq_halt_for_enforce_isolation = false; -xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, &queue_id); +while (queue) { uqm = queue->userq_mgr;mutex_lock (&uqm->userq_mutex); if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
@@ -1552,6 +1609,13 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, ret = r; }mutex_unlock (&uqm->userq_mutex); +amdgpu_userq_put (queue); + +if (queue_id ==ULONG_MAX ) +break ; + queue_id++; + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, + &queue_id); }return ret;
Signature · 1 line
2.53.0
PATCH 8/10 drm/amdgpu: hold userq kref during suspend and resume
In reply to Zhu LingshanThe userq suspend and resume handlers walks the doorbell XArray but process a user queuewithout holding its kref.
Therefore, a concurrent queue destruction process can free a queue before these handlers finish their work, causing an use-after-free problem.
This commit fixes this problem by using amdgpu_userq_xa_find helper which properly holds the kref of a queue in a loop of searching for queues.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 35 +++++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +29 -6
@@ -1493,23 +1493,35 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue;struct amdgpu_userq_mgr *uqm; -unsigned long queue_id; +unsigned long queue_id =0 ;int r;if (!ip_mask)return 0 ; -xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, &queue_id); +while (queue) { uqm = queue->userq_mgr;cancel_delayed_work_sync (&uqm->resume_work); -guard (mutex)(&uqm->userq_mutex); +mutex_lock (&uqm->userq_mutex);if (adev->in_s0ix) r =amdgpu_userq_preempt_helper (queue);else r =amdgpu_userq_unmap_helper (queue); +mutex_unlock (&uqm->userq_mutex); +amdgpu_userq_put (queue); +if (r)return r; + +if (queue_id ==ULONG_MAX ) +break ; + + queue_id++; + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, + &queue_id); } +return 0 ; }
@@ -1518,21 +1530,32 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue;struct amdgpu_userq_mgr *uqm; -unsigned long queue_id; +unsigned long queue_id =0 ;int r;if (!ip_mask)return 0 ; -xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, &queue_id); +while (queue) { uqm = queue->userq_mgr; -guard (mutex)(&uqm->userq_mutex); +mutex_lock (&uqm->userq_mutex);if (adev->in_s0ix) r =amdgpu_userq_restore_helper (queue);else r =amdgpu_userq_map_helper (queue); +mutex_unlock (&uqm->userq_mutex); +amdgpu_userq_put (queue); +if (r)return r; + +if (queue_id ==ULONG_MAX ) +break ; + + queue_id++; + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, + &queue_id); }return 0 ;
Signature · 1 line
2.53.0
PATCH 9/10 drm/amdgpu: free userq by kref_put when fails to create
In reply to Zhu LingshanIn amdgpu_userq_create(), once the user queue is published to userq_doorbell_xa, other threads could access the queue from the XArray. But the queue may fails to map the queue and kfree(queue), this causes the accessor use-after-free issue.
This commit fixes this issue by properly
get and put the kref of a queue to maintain
the lifecycle of a user queue.
There are some minor improvements in this commit:
1) Early detach the doorbell of a queue when fails to map a queue, because the map helper set the queue state to HUNG, and the asynchronous post reset helper amdgpu_userq_post_reset can find the queue from the XArray and remaps the queue again, which causes a queue leaking because the user space already receive an error code for the queue
from amdgpu_userq_create()
2) Use xa_cmpxchg_irq in amdgpu_userq_detach_doorbell(). Once fails to map a queue, the queue destruction process is asynchronous (a delayed worker), and a new created queue could re-use the doorbell, xa_cmpxchg_irq compares the doorbell which to be deleted with the doorbell which @index points to, so it does not detach the doorbell of the new created queue.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15 -5
@@ -424,7 +424,9 @@ static void amdgpu_userq_detach_doorbell(struct amdgpu_usermode_queue *queue)struct amdgpu_device *adev = queue->userq_mgr->adev;down_read (&adev->reset_domain->sem); -xa_erase_irq (&adev->userq_doorbell_xa, queue->doorbell_index); +/* It doesn't alloc any memory here, so pass 0 to gfp */ +xa_cmpxchg_irq (&adev->userq_doorbell_xa, queue->doorbell_index, + queue,NULL ,0 );up_read (&adev->reset_domain->sem); }
@@ -836,6 +838,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)if (r) goto clean_mqd; +/* +* Once the queue has been published to doorbell_xa, +* it could be accessed by other threads, so it has to be +* destroyed through kref put. The destroy work decreases +* userq_count, so we have to increase it here. +*/ +atomic_inc (&uq_mgr->userq_count[queue->queue_type]); +amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr);/* don't map the queue if scheduling is halted */
@@ -851,12 +861,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)if (r) {drm_file_err (uq_mgr->file,"Failed to map Queue\n" );trace_amdgpu_userq_create_end (queue, r); +amdgpu_userq_detach_doorbell (queue);mutex_unlock (&uq_mgr->userq_mutex); - goto erase_doorbell; +amdgpu_userq_put (queue); + +return r; } } -atomic_inc (&uq_mgr->userq_count[queue->queue_type]);mutex_unlock (&uq_mgr->userq_mutex); r = xa_alloc(&uq_mgr->userq_xa, &qid, queue,
@@ -877,8 +889,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) args->out.queue_id = qid;return 0 ; -erase_doorbell: -xa_erase_irq (&adev->userq_doorbell_xa, index); clean_mqd: uq_funcs->mqd_destroy (queue); clean_doorbell_bo:
Signature · 1 line
2.53.0
PATCH 10/10 drm/amdgpu: take queue kref in userq_create to avoid UAF
In reply to Zhu LingshanIn amdgpu_userq_create(), once a newly created user queue has been assigned a qid and published to userq_xa, a concurrent AMDGPU_USERQ_OP_FREE ioctl can free the queue, resulting in use-after-free issues in amdgpu_userq_create.
This is surely a user space bug, but kernel should not crash. This commit fixes this issue by taking an additional kref before xa_alloc()
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11 -1
@@ -871,22 +871,32 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)mutex_unlock (&uq_mgr->userq_mutex); +/* +* A concurrent AMDGPU_USERQ_OP_FREE ioctl can free the queue once +* xa_alloc() publishes it and assigne a qid to it. +* Take a kref to avoid use-after-free issues. +*/ +kref_get (&queue->refcount); r =xa_alloc (&uq_mgr->userq_xa, &qid, queue,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL );if (r) {/* -* This drops the last reference which should take care of +* This drops the last two references which should take care of * all cleanup. */ trace_amdgpu_userq_create_end (queue, r);amdgpu_userq_put (queue); +amdgpu_userq_put (queue); +return r; }amdgpu_debugfs_userq_init (filp, queue, qid);trace_amdgpu_userq_create_end (queue,0 ); args->out.queue_id = qid; +amdgpu_userq_put (queue); +return 0 ; clean_mqd:
Signature · 1 line
2.53.0