LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Xiyu Yang <xiyuyang19@fudan.edu.cn>
To: Joerg Roedel <joro@8bytes.org>,
	Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>,
	Will Deacon <will@kernel.org>,
	iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org
Cc: yuanxzhang@fudan.edu.cn, Xiyu Yang <xiyuyang19@fudan.edu.cn>,
	Xin Tan <tanxin.ctf@gmail.com>
Subject: [PATCH] iommu/amd: Convert from atomic_t to refcount_t on device_state->count
Date: Mon, 19 Jul 2021 14:00:37 +0800	[thread overview]
Message-ID: <1626674437-56007-1-git-send-email-xiyuyang19@fudan.edu.cn> (raw)

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 | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/iommu/amd/iommu_v2.c b/drivers/iommu/amd/iommu_v2.c
index f8d4ad421e07..15d64f916fe5 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>
@@ -51,7 +52,7 @@ struct pasid_state {
 struct device_state {
 	struct list_head list;
 	u16 devid;
-	atomic_t count;
+	refcount_t count;
 	struct pci_dev *pdev;
 	struct pasid_state **states;
 	struct iommu_domain *domain;
@@ -113,7 +114,7 @@ static struct device_state *get_device_state(u16 devid)
 	spin_lock_irqsave(&state_lock, flags);
 	dev_state = __get_device_state(devid);
 	if (dev_state != NULL)
-		atomic_inc(&dev_state->count);
+		refcount_inc(&dev_state->count);
 	spin_unlock_irqrestore(&state_lock, flags);
 
 	return dev_state;
@@ -144,7 +145,7 @@ static void free_device_state(struct device_state *dev_state)
 
 static void put_device_state(struct device_state *dev_state)
 {
-	if (atomic_dec_and_test(&dev_state->count))
+	if (refcount_dec_and_test(&dev_state->count))
 		wake_up(&dev_state->wq);
 }
 
@@ -765,7 +766,7 @@ int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
 	for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
 		dev_state->pasid_levels += 1;
 
-	atomic_set(&dev_state->count, 1);
+	refcount_set(&dev_state->count, 1);
 	dev_state->max_pasids = pasids;
 
 	ret = -ENOMEM;
@@ -856,7 +857,7 @@ void amd_iommu_free_device(struct pci_dev *pdev)
 	 * Wait until the last reference is dropped before freeing
 	 * the device state.
 	 */
-	wait_event(dev_state->wq, !atomic_read(&dev_state->count));
+	wait_event(dev_state->wq, !refcount_read(&dev_state->count));
 	free_device_state(dev_state);
 }
 EXPORT_SYMBOL(amd_iommu_free_device);
-- 
2.7.4


             reply	other threads:[~2021-07-19  6:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-19  6:00 Xiyu Yang [this message]
2021-07-19  9:07 ` Will Deacon
2021-07-22 19:33 ` Suthikulpanit, Suravee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1626674437-56007-1-git-send-email-xiyuyang19@fudan.edu.cn \
    --to=xiyuyang19@fudan.edu.cn \
    --cc=iommu@lists.linux-foundation.org \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tanxin.ctf@gmail.com \
    --cc=will@kernel.org \
    --cc=yuanxzhang@fudan.edu.cn \
    --subject='Re: [PATCH] iommu/amd: Convert from atomic_t to refcount_t on device_state->count' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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).