linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Mark Brown <broonie@kernel.org>
To: "Liam R. Howlett" <Liam.Howlett@oracle.com>
Cc: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>,
	 maple-tree@lists.infradead.org, linux-mm@kvack.org,
	 linux-kernel@vger.kernel.org, Mark Brown <broonie@kernel.org>
Subject: [PATCH 5/5] regmap: Don't double lock maple cache when using a regmap provided lock
Date: Thu, 22 Aug 2024 20:13:39 +0100	[thread overview]
Message-ID: <20240822-b4-regmap-maple-nolock-v1-5-d5e6dbae3396@kernel.org> (raw)
In-Reply-To: <20240822-b4-regmap-maple-nolock-v1-0-d5e6dbae3396@kernel.org>

It is not possible to use the maple tree without holding a lock of some
kind, by default the maple tree incorporates a lock but it does have
support for registering an external lock which will be asserted against
instead. At present regmap uses the maple tree's internal lock which is
unfortunate since there is also regmap level locking above the cache which
protects the cache data structure and things like read/modify/write
operations.

Let's reduce the overhead here by telling the maple tree about the regmap
level lock for cases where regmap does it's own locking. We also support
external and custom locking for regmap, neither of which will benefit from
this, but this will cover the vast majority of users.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 drivers/base/regmap/internal.h       | 12 ++++++++++++
 drivers/base/regmap/regcache-maple.c |  9 +++++++++
 drivers/base/regmap/regmap.c         |  4 ++++
 3 files changed, 25 insertions(+)

diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h
index 83acccdc1008..a5fe052a70ce 100644
--- a/drivers/base/regmap/internal.h
+++ b/drivers/base/regmap/internal.h
@@ -59,6 +59,9 @@ struct regmap {
 			unsigned long raw_spinlock_flags;
 		};
 	};
+#ifdef CONFIG_LOCKDEP
+	struct lockdep_map *lockdep;
+#endif
 	regmap_lock lock;
 	regmap_unlock unlock;
 	void *lock_arg; /* This is passed to lock/unlock functions */
@@ -344,4 +347,13 @@ struct regmap *__regmap_init_raw_ram(struct device *dev,
 #define regmap_init_raw_ram(dev, config, data)				\
 	__regmap_lockdep_wrapper(__regmap_init_raw_ram, #dev, dev, config, data)
 
+#ifdef CONFIG_LOCKDEP
+static inline void regmap_set_lockdep(struct regmap *m, struct lockdep_map *l)
+{
+	m->lockdep = l;
+}
+#else
+#define regmap_set_lockdep(m, l)
+#endif
+
 #endif
diff --git a/drivers/base/regmap/regcache-maple.c b/drivers/base/regmap/regcache-maple.c
index d2de3eba1646..1247ff3ae397 100644
--- a/drivers/base/regmap/regcache-maple.c
+++ b/drivers/base/regmap/regcache-maple.c
@@ -365,7 +365,16 @@ static int regcache_maple_init(struct regmap *map)
 		return -ENOMEM;
 	map->cache = mt;
 
+#ifdef CONFIG_LOCKDEP
+	if (map->lockdep) {
+		mt_init_flags(mt, MT_FLAGS_LOCK_EXTERN);
+		mt_set_external_lock_dep_map(mt, map->lockdep);
+	} else {
+		mt_init(mt);
+	}
+#else
 	mt_init(mt);
+#endif
 
 	if (!map->num_reg_defaults)
 		return 0;
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 9ed842d17642..f66b5ef56cf8 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -729,12 +729,15 @@ struct regmap *__regmap_init(struct device *dev,
 				map->unlock = regmap_unlock_raw_spinlock;
 				lockdep_set_class_and_name(&map->raw_spinlock,
 							   lock_key, lock_name);
+				regmap_set_lockdep(map,
+						   &map->raw_spinlock.dep_map);
 			} else {
 				spin_lock_init(&map->spinlock);
 				map->lock = regmap_lock_spinlock;
 				map->unlock = regmap_unlock_spinlock;
 				lockdep_set_class_and_name(&map->spinlock,
 							   lock_key, lock_name);
+				regmap_set_lockdep(map, &map->spinlock.dep_map);
 			}
 		} else {
 			mutex_init(&map->mutex);
@@ -743,6 +746,7 @@ struct regmap *__regmap_init(struct device *dev,
 			map->can_sleep = true;
 			lockdep_set_class_and_name(&map->mutex,
 						   lock_key, lock_name);
+			regmap_set_lockdep(map, &map->mutex.dep_map);
 		}
 		map->lock_arg = map;
 	}

-- 
2.39.2



  parent reply	other threads:[~2024-08-25 18:31 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-22 19:13 [PATCH 0/5] regmap: Improve lock handling with maple tree Mark Brown
2024-08-22 19:13 ` [PATCH 1/5] maple_tree: Allow external locks to be configured with their map Mark Brown
2024-08-22 19:21   ` Matthew Wilcox
2024-08-22 19:48     ` Mark Brown
2024-08-22 19:55       ` Matthew Wilcox
2024-08-22 20:45         ` Mark Brown
2024-08-22 19:13 ` [PATCH 2/5] regmap: Hold the regmap lock when allocating and freeing the cache Mark Brown
     [not found]   ` <CGME20240828100239eucas1p2afc0d3088c66468061baf81c5676882a@eucas1p2.samsung.com>
2024-08-28 10:02     ` Marek Szyprowski
2024-08-28 11:32       ` Mark Brown
2024-08-22 19:13 ` [PATCH 3/5] regmap: Use locking during kunit tests Mark Brown
2024-08-22 19:13 ` [PATCH 4/5] regmap: Wrap maple tree locking Mark Brown
2024-08-22 19:13 ` Mark Brown [this message]
2024-08-23 22:57 ` (subset) [PATCH 0/5] regmap: Improve lock handling with maple tree Mark Brown

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=20240822-b4-regmap-maple-nolock-v1-5-d5e6dbae3396@kernel.org \
    --to=broonie@kernel.org \
    --cc=Liam.Howlett@oracle.com \
    --cc=cristian.ciocaltea@collabora.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=maple-tree@lists.infradead.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).