From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 739DBC98313 for ; Sat, 17 Jan 2026 07:15:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2484F10E043; Sat, 17 Jan 2026 07:15:57 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="kGIWvolx"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) by gabe.freedesktop.org (Postfix) with ESMTPS id 89F3A10E029 for ; Sat, 17 Jan 2026 07:15:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1768634155; x=1800170155; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lDtJdj/+kb7ZKGksudYy+MRDy8T4x73kzyPvzSURCXU=; b=kGIWvolxEXvLuhvFJvm25/+Y9LX1FaiTq7eZTbYZtLmbdWYHHs4Il9if j/Qb3zk1M1JM1l1uumf9wIXSpdwwm3yJjNyy4nG7m0WvyNc11XBRY69ET +DhoHFKDICHoXE+DBMm9JBulQdu7nEmvEWu8UvgITckjf0Gdo948wdA9+ 2ZKZA8OsNo3cfotGn2BNxL5ujkrkZO1BsSVC2c62e3DIX/67ue89RSZuA B7hHmWTC3fLzSqe4BMhF9NO9QjDdr/CRdMs7HI92IXvx85Cmf4ca+cGn5 tYrY38kTaehatM7kO73pjCXyvQBi2nruymYvfGKGYS9ms8FWn/nxbvt4/ Q==; X-CSE-ConnectionGUID: 2ZrDpXspR+usSRz2JpsMuQ== X-CSE-MsgGUID: dLiUgagdShmav2AR4Dz68g== X-IronPort-AV: E=McAfee;i="6800,10657,11673"; a="73786309" X-IronPort-AV: E=Sophos;i="6.21,233,1763452800"; d="scan'208";a="73786309" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jan 2026 23:15:55 -0800 X-CSE-ConnectionGUID: IxSaJwmTQaCf48T1UFOnIg== X-CSE-MsgGUID: qbmgP5n4Tgui1awDx+vdbA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,233,1763452800"; d="scan'208";a="205842657" Received: from dut6304bmgfrd.fm.intel.com ([10.36.21.42]) by fmviesa009.fm.intel.com with ESMTP; 16 Jan 2026 23:15:54 -0800 From: Xin Wang 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 Subject: [PATCH 2/4] lib/intel_device_info: make device info cache thread-safe Date: Sat, 17 Jan 2026 07:14:57 +0000 Message-ID: <20260117071459.892532-3-x.wang@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260117071459.892532-1-x.wang@intel.com> References: <20260117071459.892532-1-x.wang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: igt-dev@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development mailing list for IGT GPU Tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: igt-dev-bounces@lists.freedesktop.org Sender: "igt-dev" 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 --- 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 #include /* 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