All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xin Wang <x.wang@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: matthew.d.roper@intel.com, zbigniew.kempczynski@intel.com,
	kamil.konieczny@linux.intel.com, ravi.kumar.vodapalli@intel.com,
	Xin Wang <x.wang@intel.com>
Subject: [PATCH v12 2/4] lib/intel_device_info: make device info cache thread-safe
Date: Sat, 17 Jan 2026 23:48:18 +0000	[thread overview]
Message-ID: <20260117234821.929209-3-x.wang@intel.com> (raw)
In-Reply-To: <20260117234821.929209-1-x.wang@intel.com>

Replace the per-thread single-entry cache in intel_get_device_info() with a
process-wide cache keyed by devid.

Use igt_map + pthread mutex to store a copy of struct intel_device_info per
device id, and initialize/teardown the cache via igt_constructor/igt_destructor.

Also link igt_map.c into libigt_chipset to provide the map implementation.

Signed-off-by: Xin Wang <x.wang@intel.com>
---
 lib/intel_device_info.c | 91 +++++++++++++++++++++++++++++++++++++----
 lib/meson.build         |  1 +
 2 files changed, 83 insertions(+), 9 deletions(-)

diff --git a/lib/intel_device_info.c b/lib/intel_device_info.c
index 89fa6788f..e97a8caa0 100644
--- a/lib/intel_device_info.c
+++ b/lib/intel_device_info.c
@@ -1,7 +1,10 @@
 #include "intel_chipset.h"
 #include "pciids.h"
 #include "i915_pciids_local.h"
+#include "igt_core.h"
+#include "igt_map.h"
 
+#include <pthread.h>
 #include <strings.h> /* ffs() */
 
 static const struct intel_device_info intel_generic_info = {
@@ -688,6 +691,53 @@ static const struct pci_id_match intel_device_match[] = {
 
 #undef INTEL_PCI_ID_INIT
 
+static struct intel_device_cache {
+	pthread_mutex_t cache_mutex;
+	struct igt_map *map;
+} dev;
+
+static void intel_device_info_cache_init(void)
+{
+	pthread_mutex_init(&dev.cache_mutex, NULL);
+	dev.map = igt_map_create(igt_map_hash_32, igt_map_equal_32);
+}
+
+static void free_device_info(struct igt_map_entry *entry)
+{
+	free(entry->data);
+	free((void *)entry->key);
+}
+
+static void intel_device_info_cache_fini(void)
+{
+	pthread_mutex_lock(&dev.cache_mutex);
+	igt_map_destroy(dev.map, free_device_info);
+	pthread_mutex_unlock(&dev.cache_mutex);
+	pthread_mutex_destroy(&dev.cache_mutex);
+}
+
+static struct intel_device_info *intel_device_info_cache_get(uint16_t devid)
+{
+	struct intel_device_info *info;
+	uint32_t key = devid;
+
+	pthread_mutex_lock(&dev.cache_mutex);
+	info = igt_map_search(dev.map, &key);
+	pthread_mutex_unlock(&dev.cache_mutex);
+
+	return info;
+}
+
+igt_constructor
+{
+	intel_device_info_cache_init();
+}
+
+igt_destructor
+{
+	intel_device_info_cache_fini();
+}
+
 /**
  * intel_get_device_info:
  * @devid: pci device id
@@ -699,24 +749,47 @@ static const struct pci_id_match intel_device_match[] = {
  */
 const struct intel_device_info *intel_get_device_info(uint16_t devid)
 {
-	static __thread const struct intel_device_info *cache = &intel_generic_info;
-	static __thread uint16_t cached_devid;
+	struct intel_device_info *info;
+	struct intel_device_info *new_info = NULL;
+	uint32_t *new_key = NULL;
+	uint32_t key_val = devid;
 	int i;
 
-	if (cached_devid == devid)
-		goto out;
+	info = intel_device_info_cache_get(devid);
+	if (info)
+		return info;
 
-	/* XXX Presort table and bsearch! */
 	for (i = 0; intel_device_match[i].device_id != PCI_MATCH_ANY; i++) {
 		if (devid == intel_device_match[i].device_id)
 			break;
 	}
 
-	cached_devid = devid;
-	cache = (void *)intel_device_match[i].match_data;
+	new_key = malloc(sizeof(*new_key));
+	new_info = malloc(sizeof(*new_info));
+	if (!new_key || !new_info) {
+		free(new_key);
+		free(new_info);
+		return &intel_generic_info;
+	}
+
+	*new_key = devid;
+	memcpy(new_info, (const void *)intel_device_match[i].match_data, sizeof(*new_info));
+
+	/* Another thread may have inserted while we were allocating. */
+	pthread_mutex_lock(&dev.cache_mutex);
+	info = igt_map_search(dev.map, &key_val);
+	if (!info) {
+		igt_map_insert(dev.map, new_key, new_info);
+		info = new_info;
+		new_key = NULL;
+		new_info = NULL;
+	}
+	pthread_mutex_unlock(&dev.cache_mutex);
+
+	free(new_info);
+	free(new_key);
 
-out:
-	return cache;
+	return info;
 }
 
 /**
diff --git a/lib/meson.build b/lib/meson.build
index 1a569ba52..d41540799 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -317,6 +317,7 @@ igt_deps = [ lib_igt ] + lib_deps
 lin_igt_chipset_build = static_library('igt_chipset',
                                        ['intel_chipset.c',
 					'intel_device_info.c',
+					'igt_map.c',
 					'intel_cmds_info.c'],
                                        include_directories : inc)
 
-- 
2.43.0


  parent reply	other threads:[~2026-01-17 23:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-17 23:48 [PATCH v12 0/4] lib/intel_device_info: get the xe .graphics_rel from GMD_ID Xin Wang
2026-01-17 23:48 ` [PATCH v12 1/4] lib/igt_core: add igt_destructor helper macro Xin Wang
2026-01-17 23:48 ` Xin Wang [this message]
2026-01-17 23:48 ` [PATCH v12 3/4] lib/intel_device_info: allow xe_query to override graphics version Xin Wang
2026-01-17 23:48 ` [PATCH v12 4/4] lib/intel_device_info: remove the graphcs_rel from xe2+ devices Xin Wang
2026-01-18  0:40 ` ✓ Xe.CI.BAT: success for lib/intel_device_info: get the xe .graphics_rel from GMD_ID Patchwork
2026-01-18  0:52 ` ✓ i915.CI.BAT: " Patchwork
2026-01-18  1:47 ` ✗ Xe.CI.Full: failure " Patchwork
2026-01-18  2:56 ` ✓ i915.CI.Full: success " Patchwork

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=20260117234821.929209-3-x.wang@intel.com \
    --to=x.wang@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=kamil.konieczny@linux.intel.com \
    --cc=matthew.d.roper@intel.com \
    --cc=ravi.kumar.vodapalli@intel.com \
    --cc=zbigniew.kempczynski@intel.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.