iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iommu/amd: Convert from atomic_t to refcount_t on pasid_state->count
@ 2021-07-19  8:32 Xiyu Yang via iommu
  2021-07-22 19:34 ` Suthikulpanit, Suravee via iommu
  2021-07-26 11:47 ` Joerg Roedel
  0 siblings, 2 replies; 3+ messages in thread
From: Xiyu Yang via iommu @ 2021-07-19  8:32 UTC (permalink / raw)
  To: Joerg Roedel, Suravee Suthikulpanit, Will Deacon, iommu, linux-kernel
  Cc: Xin Tan, yuanxzhang, Xiyu Yang

refcount_t type and corresponding API can protect refcounters from
accidental underflow and overflow and further use-after-free situations.

Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
---
 drivers/iommu/amd/iommu_v2.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/amd/iommu_v2.c b/drivers/iommu/amd/iommu_v2.c
index f8d4ad421e07..a9e568276c99 100644
--- a/drivers/iommu/amd/iommu_v2.c
+++ b/drivers/iommu/amd/iommu_v2.c
@@ -6,6 +6,7 @@
 
 #define pr_fmt(fmt)     "AMD-Vi: " fmt
 
+#include <linux/refcount.h>
 #include <linux/mmu_notifier.h>
 #include <linux/amd-iommu.h>
 #include <linux/mm_types.h>
@@ -33,7 +34,7 @@ struct pri_queue {
 
 struct pasid_state {
 	struct list_head list;			/* For global state-list */
-	atomic_t count;				/* Reference count */
+	refcount_t count;				/* Reference count */
 	unsigned mmu_notifier_count;		/* Counting nested mmu_notifier
 						   calls */
 	struct mm_struct *mm;			/* mm_struct for the faults */
@@ -242,7 +243,7 @@ static struct pasid_state *get_pasid_state(struct device_state *dev_state,
 
 	ret = *ptr;
 	if (ret)
-		atomic_inc(&ret->count);
+		refcount_inc(&ret->count);
 
 out_unlock:
 	spin_unlock_irqrestore(&dev_state->lock, flags);
@@ -257,14 +258,14 @@ static void free_pasid_state(struct pasid_state *pasid_state)
 
 static void put_pasid_state(struct pasid_state *pasid_state)
 {
-	if (atomic_dec_and_test(&pasid_state->count))
+	if (refcount_dec_and_test(&pasid_state->count))
 		wake_up(&pasid_state->wq);
 }
 
 static void put_pasid_state_wait(struct pasid_state *pasid_state)
 {
-	atomic_dec(&pasid_state->count);
-	wait_event(pasid_state->wq, !atomic_read(&pasid_state->count));
+	refcount_dec(&pasid_state->count);
+	wait_event(pasid_state->wq, !refcount_read(&pasid_state->count));
 	free_pasid_state(pasid_state);
 }
 
@@ -624,7 +625,7 @@ int amd_iommu_bind_pasid(struct pci_dev *pdev, u32 pasid,
 		goto out;
 
 
-	atomic_set(&pasid_state->count, 1);
+	refcount_set(&pasid_state->count, 1);
 	init_waitqueue_head(&pasid_state->wq);
 	spin_lock_init(&pasid_state->lock);
 
-- 
2.7.4

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iommu/amd: Convert from atomic_t to refcount_t on pasid_state->count
  2021-07-19  8:32 [PATCH] iommu/amd: Convert from atomic_t to refcount_t on pasid_state->count Xiyu Yang via iommu
@ 2021-07-22 19:34 ` Suthikulpanit, Suravee via iommu
  2021-07-26 11:47 ` Joerg Roedel
  1 sibling, 0 replies; 3+ messages in thread
From: Suthikulpanit, Suravee via iommu @ 2021-07-22 19:34 UTC (permalink / raw)
  To: Xiyu Yang, Joerg Roedel, Will Deacon, iommu, linux-kernel
  Cc: Xin Tan, yuanxzhang



On 7/19/2021 3:32 AM, Xiyu Yang wrote:
> refcount_t type and corresponding API can protect refcounters from
> accidental underflow and overflow and further use-after-free situations.
> 
> Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>

Thanks,

Reviewed-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iommu/amd: Convert from atomic_t to refcount_t on pasid_state->count
  2021-07-19  8:32 [PATCH] iommu/amd: Convert from atomic_t to refcount_t on pasid_state->count Xiyu Yang via iommu
  2021-07-22 19:34 ` Suthikulpanit, Suravee via iommu
@ 2021-07-26 11:47 ` Joerg Roedel
  1 sibling, 0 replies; 3+ messages in thread
From: Joerg Roedel @ 2021-07-26 11:47 UTC (permalink / raw)
  To: Xiyu Yang; +Cc: Xin Tan, yuanxzhang, linux-kernel, iommu, Will Deacon

On Mon, Jul 19, 2021 at 04:32:58PM +0800, Xiyu Yang wrote:
>  drivers/iommu/amd/iommu_v2.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)

Applied, thanks.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-07-26 11:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-19  8:32 [PATCH] iommu/amd: Convert from atomic_t to refcount_t on pasid_state->count Xiyu Yang via iommu
2021-07-22 19:34 ` Suthikulpanit, Suravee via iommu
2021-07-26 11:47 ` Joerg Roedel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).