linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@infradead.org>
To: linux-kernel@vger.kernel.org
Cc: Matthew Wilcox <mawilcox@microsoft.com>,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net,
	linux-nilfs@vger.kernel.org, linux-btrfs@vger.kernel.org,
	linux-xfs@vger.kernel.org, linux-usb@vger.kernel.org,
	Bjorn Andersson <bjorn.andersson@linaro.org>,
	Stefano Stabellini <sstabellini@kernel.org>,
	iommu@lists.linux-foundation.org,
	linux-remoteproc@vger.kernel.org, linux-s390@vger.kernel.org,
	intel-gfx@lists.freedesktop.org, cgroups@vger.kernel.org,
	linux-sh@vger.kernel.org, David Howells <dhowells@redhat.com>
Subject: [PATCH v6 96/99] dma-debug: Convert to XArray
Date: Wed, 17 Jan 2018 12:22:00 -0800	[thread overview]
Message-ID: <20180117202203.19756-97-willy@infradead.org> (raw)
In-Reply-To: <20180117202203.19756-1-willy@infradead.org>

From: Matthew Wilcox <mawilcox@microsoft.com>

This is an unusual way to use the xarray tags.  If any other users
come up, we can add an xas_get_tags() / xas_set_tags() API, but until
then I don't want to encourage this kind of abuse.

Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
---
 lib/dma-debug.c | 105 +++++++++++++++++++++++++-------------------------------
 1 file changed, 46 insertions(+), 59 deletions(-)

diff --git a/lib/dma-debug.c b/lib/dma-debug.c
index fb4af570ce04..965b3837d060 100644
--- a/lib/dma-debug.c
+++ b/lib/dma-debug.c
@@ -22,7 +22,6 @@
 #include <linux/dma-mapping.h>
 #include <linux/sched/task.h>
 #include <linux/stacktrace.h>
-#include <linux/radix-tree.h>
 #include <linux/dma-debug.h>
 #include <linux/spinlock.h>
 #include <linux/vmalloc.h>
@@ -30,6 +29,7 @@
 #include <linux/uaccess.h>
 #include <linux/export.h>
 #include <linux/device.h>
+#include <linux/xarray.h>
 #include <linux/types.h>
 #include <linux/sched.h>
 #include <linux/ctype.h>
@@ -465,9 +465,8 @@ EXPORT_SYMBOL(debug_dma_dump_mappings);
  * At any time debug_dma_assert_idle() can be called to trigger a
  * warning if any cachelines in the given page are in the active set.
  */
-static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
-static DEFINE_SPINLOCK(radix_lock);
-#define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
+static DEFINE_XARRAY_FLAGS(dma_active_cacheline, XA_FLAGS_LOCK_IRQ);
+#define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << XA_MAX_TAGS) - 1)
 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
 #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
 
@@ -477,37 +476,40 @@ static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
 		(entry->offset >> L1_CACHE_SHIFT);
 }
 
-static int active_cacheline_read_overlap(phys_addr_t cln)
+static unsigned int active_cacheline_read_overlap(struct xa_state *xas)
 {
-	int overlap = 0, i;
+	unsigned int tags = 0;
+	xa_tag_t tag;
 
-	for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
-		if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
-			overlap |= 1 << i;
-	return overlap;
+	for (tag = 0; tag < XA_MAX_TAGS; tag++)
+		if (xas_get_tag(xas, tag))
+			tags |= 1U << tag;
+
+	return tags;
 }
 
-static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
+static int active_cacheline_set_overlap(struct xa_state *xas, int overlap)
 {
-	int i;
+	xa_tag_t tag;
 
 	if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
 		return overlap;
 
-	for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
-		if (overlap & 1 << i)
-			radix_tree_tag_set(&dma_active_cacheline, cln, i);
+	for (tag = 0; tag < XA_MAX_TAGS; tag++) {
+		if (overlap & (1U << tag))
+			xas_set_tag(xas, tag);
 		else
-			radix_tree_tag_clear(&dma_active_cacheline, cln, i);
+			xas_clear_tag(xas, tag);
+	}
 
 	return overlap;
 }
 
-static void active_cacheline_inc_overlap(phys_addr_t cln)
+static void active_cacheline_inc_overlap(struct xa_state *xas)
 {
-	int overlap = active_cacheline_read_overlap(cln);
+	int overlap = active_cacheline_read_overlap(xas);
 
-	overlap = active_cacheline_set_overlap(cln, ++overlap);
+	overlap = active_cacheline_set_overlap(xas, ++overlap);
 
 	/* If we overflowed the overlap counter then we're potentially
 	 * leaking dma-mappings.  Otherwise, if maps and unmaps are
@@ -517,21 +519,22 @@ static void active_cacheline_inc_overlap(phys_addr_t cln)
 	 */
 	WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
 		  "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
-		  ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
+		  ACTIVE_CACHELINE_MAX_OVERLAP, &xas->xa_index);
 }
 
-static int active_cacheline_dec_overlap(phys_addr_t cln)
+static int active_cacheline_dec_overlap(struct xa_state *xas)
 {
-	int overlap = active_cacheline_read_overlap(cln);
+	int overlap = active_cacheline_read_overlap(xas);
 
-	return active_cacheline_set_overlap(cln, --overlap);
+	return active_cacheline_set_overlap(xas, --overlap);
 }
 
 static int active_cacheline_insert(struct dma_debug_entry *entry)
 {
 	phys_addr_t cln = to_cacheline_number(entry);
+	XA_STATE(xas, &dma_active_cacheline, cln);
 	unsigned long flags;
-	int rc;
+	struct dma_debug_entry *exists;
 
 	/* If the device is not writing memory then we don't have any
 	 * concerns about the cpu consuming stale data.  This mitigates
@@ -540,32 +543,32 @@ static int active_cacheline_insert(struct dma_debug_entry *entry)
 	if (entry->direction == DMA_TO_DEVICE)
 		return 0;
 
-	spin_lock_irqsave(&radix_lock, flags);
-	rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
-	if (rc == -EEXIST)
-		active_cacheline_inc_overlap(cln);
-	spin_unlock_irqrestore(&radix_lock, flags);
+	xas_lock_irqsave(&xas, flags);
+	exists = xas_create(&xas);
+	if (exists)
+		active_cacheline_inc_overlap(&xas);
+	else
+		xas_store(&xas, entry);
+	xas_unlock_irqrestore(&xas, flags);
 
-	return rc;
+	return xas_error(&xas);
 }
 
 static void active_cacheline_remove(struct dma_debug_entry *entry)
 {
 	phys_addr_t cln = to_cacheline_number(entry);
+	XA_STATE(xas, &dma_active_cacheline, cln);
 	unsigned long flags;
 
 	/* ...mirror the insert case */
 	if (entry->direction == DMA_TO_DEVICE)
 		return;
 
-	spin_lock_irqsave(&radix_lock, flags);
-	/* since we are counting overlaps the final put of the
-	 * cacheline will occur when the overlap count is 0.
-	 * active_cacheline_dec_overlap() returns -1 in that case
-	 */
-	if (active_cacheline_dec_overlap(cln) < 0)
-		radix_tree_delete(&dma_active_cacheline, cln);
-	spin_unlock_irqrestore(&radix_lock, flags);
+	xas_lock_irqsave(&xas, flags);
+	xas_load(&xas);
+	if (active_cacheline_dec_overlap(&xas) < 0)
+		xas_store(&xas, NULL);
+	xas_unlock_irqrestore(&xas, flags);
 }
 
 /**
@@ -578,12 +581,8 @@ static void active_cacheline_remove(struct dma_debug_entry *entry)
  */
 void debug_dma_assert_idle(struct page *page)
 {
-	static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
-	struct dma_debug_entry *entry = NULL;
-	void **results = (void **) &ents;
-	unsigned int nents, i;
-	unsigned long flags;
-	phys_addr_t cln;
+	struct dma_debug_entry *entry;
+	unsigned long cln;
 
 	if (dma_debug_disabled())
 		return;
@@ -591,21 +590,9 @@ void debug_dma_assert_idle(struct page *page)
 	if (!page)
 		return;
 
-	cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
-	spin_lock_irqsave(&radix_lock, flags);
-	nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
-				       CACHELINES_PER_PAGE);
-	for (i = 0; i < nents; i++) {
-		phys_addr_t ent_cln = to_cacheline_number(ents[i]);
-
-		if (ent_cln == cln) {
-			entry = ents[i];
-			break;
-		} else if (ent_cln >= cln + CACHELINES_PER_PAGE)
-			break;
-	}
-	spin_unlock_irqrestore(&radix_lock, flags);
-
+	cln = page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
+	entry = xa_find(&dma_active_cacheline, &cln,
+			cln + CACHELINES_PER_PAGE - 1, XA_PRESENT);
 	if (!entry)
 		return;
 
-- 
2.15.1

  parent reply	other threads:[~2018-01-17 20:22 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-17 20:20 [PATCH v6 00/99] XArray version 6 Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 01/99] xarray: Add the xa_lock to the radix_tree_root Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 02/99] page cache: Use xa_lock Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 03/99] xarray: Replace exceptional entries Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 04/99] xarray: Change definition of sibling entries Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 05/99] xarray: Add definition of struct xarray Matthew Wilcox
2018-01-24  8:45   ` Paul Bolle
2018-01-17 20:20 ` [PATCH v6 06/99] xarray: Define struct xa_node Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 07/99] xarray: Add documentation Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 08/99] xarray: Add xa_load Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 09/99] xarray: Add xa_get_tag, xa_set_tag and xa_clear_tag Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 10/99] xarray: Add xa_store Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 11/99] xarray: Add xa_cmpxchg and xa_insert Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 12/99] xarray: Add xa_for_each Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 13/99] xarray: Add xa_extract Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 14/99] xarray: Add xa_destroy Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 15/99] xarray: Add xas_next and xas_prev Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 16/99] xarray: Add xas_create_range Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 17/99] xarray: Add MAINTAINERS entry Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 18/99] xarray: Add ability to store errno values Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 19/99] idr: Convert to XArray Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 20/99] ida: " Matthew Wilcox
2018-01-17 21:17   ` John Paul Adrian Glaubitz
2018-01-17 20:20 ` [PATCH v6 21/99] xarray: Add xa_reserve and xa_release Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 22/99] page cache: Convert hole search to XArray Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 23/99] page cache: Add page_cache_range_empty function Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 24/99] page cache: Add and replace pages using the XArray Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 25/99] page cache: Convert page deletion to XArray Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 26/99] page cache: Convert page cache lookups " Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 27/99] page cache: Convert delete_batch " Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 28/99] page cache: Remove stray radix comment Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 29/99] page cache: Convert filemap_range_has_page to XArray Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 30/99] mm: Convert page-writeback " Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 31/99] mm: Convert workingset " Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 32/99] mm: Convert truncate " Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 33/99] mm: Convert add_to_swap_cache " Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 34/99] mm: Convert delete_from_swap_cache " Matthew Wilcox
2018-01-17 20:20 ` [PATCH v6 35/99] mm: Convert __do_page_cache_readahead " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 36/99] mm: Convert page migration " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 37/99] mm: Convert huge_memory " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 38/99] mm: Convert collapse_shmem " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 39/99] mm: Convert khugepaged_scan_shmem " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 40/99] pagevec: Use xa_tag_t Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 41/99] shmem: Convert replace to XArray Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 42/99] shmem: Convert shmem_confirm_swap " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 43/99] shmem: Convert find_swap_entry " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 44/99] shmem: Convert shmem_tag_pins " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 45/99] shmem: Convert shmem_wait_for_pins " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 46/99] shmem: Convert shmem_add_to_page_cache " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 47/99] shmem: Convert shmem_alloc_hugepage " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 48/99] shmem: Convert shmem_free_swap " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 49/99] shmem: Convert shmem_partial_swap_usage " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 50/99] shmem: Comment fixups Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 51/99] btrfs: Convert page cache to XArray Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 52/99] fs: Convert buffer " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 53/99] fs: Convert writeback " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 54/99] nilfs2: Convert " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 55/99] f2fs: " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 56/99] lustre: " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 57/99] dax: Convert dax_unlock_mapping_entry " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 58/99] dax: Convert lock_slot " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 59/99] dax: More XArray conversion Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 60/99] dax: Convert __dax_invalidate_mapping_entry to XArray Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 61/99] dax: Convert dax_writeback_one " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 62/99] dax: Convert dax_insert_pfn_mkwrite " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 63/99] dax: Convert dax_insert_mapping_entry " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 64/99] dax: Convert grab_mapping_entry " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 65/99] dax: Fix sparse warning Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 66/99] page cache: Finish XArray conversion Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 67/99] mm: Convert cgroup writeback to XArray Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 68/99] vmalloc: Convert " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 69/99] brd: " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 70/99] xfs: Convert m_perag_tree " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 71/99] xfs: Convert pag_ici_root " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 72/99] xfs: Convert xfs dquot " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 73/99] xfs: Convert mru cache " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 74/99] usb: Convert xhci-mem " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 75/99] md: Convert raid5-cache " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 76/99] irqdomain: Convert " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 77/99] fscache: " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 78/99] sh: intc: " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 79/99] blk-cgroup: " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 80/99] blk-ioc: " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 81/99] i915: Convert handles_vma " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 82/99] s390: Convert gmap " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 83/99] hwspinlock: Convert " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 84/99] btrfs: Convert fs_roots_radix " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 85/99] btrfs: Remove unused spinlock Matthew Wilcox
2018-01-18 14:22   ` David Sterba
2018-01-17 20:21 ` [PATCH v6 86/99] btrfs: Convert reada_zones to XArray Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 87/99] btrfs: Convert reada_extents " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 88/99] btrfs: Convert reada_tree " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 89/99] btrfs: Convert buffer_radix " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 90/99] btrfs: Convert delayed_nodes_tree " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 91/99] btrfs: Convert name_cache " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 92/99] f2fs: Convert pids radix tree " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 93/99] f2fs: Convert ino_root " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 94/99] f2fs: Convert extent_tree_root " Matthew Wilcox
2018-01-17 20:21 ` [PATCH v6 95/99] f2fs: Convert gclist.iroot " Matthew Wilcox
2018-01-17 20:22 ` Matthew Wilcox [this message]
2018-01-17 20:22 ` [PATCH v6 97/99] xen: Convert pvcalls-back " Matthew Wilcox
2018-01-17 20:22 ` [PATCH v6 98/99] qrtr: Convert " Matthew Wilcox
2018-01-17 20:22 ` [PATCH v6 99/99] null_blk: " Matthew Wilcox
2018-01-18 16:07 ` [PATCH v6 00/99] XArray version 6 David Sterba
2018-01-18 16:48   ` Matthew Wilcox
2018-01-18 16:56     ` David Sterba
2018-01-18 17:02       ` Matthew Wilcox

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=20180117202203.19756-97-willy@infradead.org \
    --to=willy@infradead.org \
    --cc=bjorn.andersson@linaro.org \
    --cc=cgroups@vger.kernel.org \
    --cc=dhowells@redhat.com \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=iommu@lists.linux-foundation.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-nilfs@vger.kernel.org \
    --cc=linux-remoteproc@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=mawilcox@microsoft.com \
    --cc=sstabellini@kernel.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).