From nobody Mon Dec 15 19:10:28 2025 Received: from polaris.svanheule.net (polaris.svanheule.net [84.16.241.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F6A831280B for ; Wed, 29 Oct 2025 08:13:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.241.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761725584; cv=none; b=lnGVz7AAlmtYk4BdfjBQo6Dpp+VoK70LyB7f9iWbP7f+/tOEZjad1DL2XoIzHXi3tmnAi6aawIRtq9gcf/TNIeSC6evnPcijxOAiUP/95Un1EBC1LKyJ3YtAaQBo3ZgpmI6rKMz8S1aFoMMJmboFb6kPcxl2L0bwwJeN5KxR3sw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761725584; c=relaxed/simple; bh=uW1f+VyML3jkjaRTgFCWvm9Cy/jWTOSqwBw8hEONYdM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GB9o6xI5be4SdMm80pEYKMCheDjZPVHoPOjMdguB0yQ+vqDeFxZ50xlOXFu5aA0XpILD3urJN7k5D1epl2ITtpjB54O7dz8LFM7cF1JlK4kFO2ZEUI6aPOYNlqI95N1SoSoTvlpOdqEhzByDPVFi/3y/5+vcBd0iwL4VQ+Wba1w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=svanheule.net; spf=pass smtp.mailfrom=svanheule.net; dkim=pass (2048-bit key) header.d=svanheule.net header.i=@svanheule.net header.b=D71oeyFk; arc=none smtp.client-ip=84.16.241.116 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=svanheule.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=svanheule.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=svanheule.net header.i=@svanheule.net header.b="D71oeyFk" Received: from terra.vega.svanheule.net (2a02-1812-162c-8f00-1e2d-b404-3319-eba8.ip6.access.telenet.be [IPv6:2a02:1812:162c:8f00:1e2d:b404:3319:eba8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sander@svanheule.net) by polaris.svanheule.net (Postfix) with ESMTPSA id 47941690568; Wed, 29 Oct 2025 09:12:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1761725575; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dqSjmY/zxBiXWdPxezzMnXfs6xXJS1rNM+yXl18h57M=; b=D71oeyFkEjKn0jtgA0GSx/yMud60IPodSD+hCn8rWAAUuhqJ1VSogtCc53aXZvqFaOBbOB pq6E0xB0hoG+6nwiPvHjTus2TuZDEK0a/2M7EzpXRFgaTSkqALuI+7ecdjDzwF29lhKBye CXfO/CMPaRVy8sRx/IJ7hOxoKbapacgg5AG5H9OOgKeG12RSpg2vBuIgHV+zOzRmWM/dbD IrYa1LxnehylpGzt5drYlX11pUE+hf8JZQBvbEyrPwJ3ZujS+cqRFcVQFBdTAcMPXKilh8 6/7/ILQm7Ucn1H5zTUH10Tnfsg5eCK3F6g6L/PhQ7wU85xfa4POEAa2AioiJMg== From: Sander Vanheule To: Mark Brown , linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , "Rafael J . Wysocki" , Danilo Krummrich , Sander Vanheule Subject: [PATCH v6 1/2] regmap: add flat cache with sparse validity Date: Wed, 29 Oct 2025 09:12:47 +0100 Message-ID: <20251029081248.52607-2-sander@svanheule.net> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251029081248.52607-1-sander@svanheule.net> References: <20251029081248.52607-1-sander@svanheule.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The flat regcache will always assume the data in the cache is valid. Since the cache is preferred over hardware access, this may shadow the actual state of the device. Add a new containing cache structure with the flat data table and a bitmap indicating cache validity. REGCACHE_FLAT will still behave as before, as the validity is ignored. Define new cache type REGCACHE_FLAT_S: a flat cache with sparse validity. The sparse validity is used to determine if a hardware access should occur to initialize the cache on the fly, vs. at regmap init for REGCACHE_FLAT. Contrary to REGCACHE_FLAT, this allows us to implement regcache_ops.drop. Signed-off-by: Sander Vanheule --- Changes since v5: - Use kzalloc() to zero-initialize cache - Drop short-lived cache_valid variable - Drop unused initial value for cache pointer in init - Update regcache_type comment to document the difference between the flat caches Changes since v4: - Add REGCACHE_FLAT_S to remaining KUnit test config tables - Shuffle code to make the patches cleaner Changes since v3: - Define new flat-sparse ops instead of modifying existing ones - Update KUnit tests - Allocate cache as flexible array to limit performance impact --- drivers/base/regmap/internal.h | 1 + drivers/base/regmap/regcache-flat.c | 102 +++++++++++++++++++++++++--- drivers/base/regmap/regcache.c | 1 + drivers/base/regmap/regmap-kunit.c | 22 ++++++ include/linux/regmap.h | 17 +++-- 5 files changed, 126 insertions(+), 17 deletions(-) diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h index 6f31240ee4a9..8d19a1414d5b 100644 --- a/drivers/base/regmap/internal.h +++ b/drivers/base/regmap/internal.h @@ -288,6 +288,7 @@ enum regmap_endian regmap_get_val_endian(struct device = *dev, const struct regmap_bus *bus, const struct regmap_config *config); =20 +extern struct regcache_ops regcache_flat_sparse_ops; extern struct regcache_ops regcache_rbtree_ops; extern struct regcache_ops regcache_maple_ops; extern struct regcache_ops regcache_flat_ops; diff --git a/drivers/base/regmap/regcache-flat.c b/drivers/base/regmap/regc= ache-flat.c index f36d3618b67c..86f7679175b1 100644 --- a/drivers/base/regmap/regcache-flat.c +++ b/drivers/base/regmap/regcache-flat.c @@ -6,7 +6,11 @@ // // Author: Mark Brown =20 +#include +#include #include +#include +#include #include #include =20 @@ -18,34 +22,62 @@ static inline unsigned int regcache_flat_get_index(cons= t struct regmap *map, return regcache_get_index_by_order(map, reg); } =20 +struct regcache_flat_data { + unsigned long *valid; + unsigned int data[]; +}; + static int regcache_flat_init(struct regmap *map) { int i; - unsigned int *cache; + size_t cache_data_size; + unsigned int cache_size; + struct regcache_flat_data *cache; =20 if (!map || map->reg_stride_order < 0 || !map->max_register_is_set) return -EINVAL; =20 - map->cache =3D kcalloc(regcache_flat_get_index(map, map->max_register) - + 1, sizeof(unsigned int), map->alloc_flags); - if (!map->cache) + cache_size =3D regcache_flat_get_index(map, map->max_register) + 1; + cache_data_size =3D struct_size(cache, data, cache_size); + + if (cache_data_size =3D=3D SIZE_MAX) { + dev_err(map->dev, "cannot allocate regmap cache"); return -ENOMEM; + } =20 - cache =3D map->cache; + cache =3D kzalloc(cache_data_size, map->alloc_flags); + if (!cache) + return -ENOMEM; + + cache->valid =3D bitmap_zalloc(cache_size, map->alloc_flags); + if (!cache->valid) + goto err_free; + + map->cache =3D cache; =20 for (i =3D 0; i < map->num_reg_defaults; i++) { unsigned int reg =3D map->reg_defaults[i].reg; unsigned int index =3D regcache_flat_get_index(map, reg); =20 - cache[index] =3D map->reg_defaults[i].def; + cache->data[index] =3D map->reg_defaults[i].def; + __set_bit(index, cache->valid); } =20 return 0; + +err_free: + kfree(cache); + return -ENOMEM; } =20 static int regcache_flat_exit(struct regmap *map) { - kfree(map->cache); + struct regcache_flat_data *cache =3D map->cache; + + if (cache) + bitmap_free(cache->valid); + + kfree(cache); map->cache =3D NULL; =20 return 0; @@ -54,10 +86,24 @@ static int regcache_flat_exit(struct regmap *map) static int regcache_flat_read(struct regmap *map, unsigned int reg, unsigned int *value) { - unsigned int *cache =3D map->cache; + struct regcache_flat_data *cache =3D map->cache; unsigned int index =3D regcache_flat_get_index(map, reg); =20 - *value =3D cache[index]; + *value =3D cache->data[index]; + + return 0; +} + +static int regcache_flat_sparse_read(struct regmap *map, + unsigned int reg, unsigned int *value) +{ + struct regcache_flat_data *cache =3D map->cache; + unsigned int index =3D regcache_flat_get_index(map, reg); + + if (unlikely(!test_bit(index, cache->valid))) + return -ENOENT; + + *value =3D cache->data[index]; =20 return 0; } @@ -65,10 +111,34 @@ static int regcache_flat_read(struct regmap *map, static int regcache_flat_write(struct regmap *map, unsigned int reg, unsigned int value) { - unsigned int *cache =3D map->cache; + struct regcache_flat_data *cache =3D map->cache; unsigned int index =3D regcache_flat_get_index(map, reg); =20 - cache[index] =3D value; + cache->data[index] =3D value; + + return 0; +} + +static int regcache_flat_sparse_write(struct regmap *map, unsigned int reg, + unsigned int value) +{ + struct regcache_flat_data *cache =3D map->cache; + unsigned int index =3D regcache_flat_get_index(map, reg); + + cache->data[index] =3D value; + __set_bit(index, cache->valid); + + return 0; +} + +static int regcache_flat_drop(struct regmap *map, unsigned int min, + unsigned int max) +{ + struct regcache_flat_data *cache =3D map->cache; + unsigned int bitmap_min =3D regcache_flat_get_index(map, min); + unsigned int bitmap_max =3D regcache_flat_get_index(map, max); + + bitmap_clear(cache->valid, bitmap_min, bitmap_max + 1 - bitmap_min); =20 return 0; } @@ -81,3 +151,13 @@ struct regcache_ops regcache_flat_ops =3D { .read =3D regcache_flat_read, .write =3D regcache_flat_write, }; + +struct regcache_ops regcache_flat_sparse_ops =3D { + .type =3D REGCACHE_FLAT_S, + .name =3D "flat-sparse", + .init =3D regcache_flat_init, + .exit =3D regcache_flat_exit, + .read =3D regcache_flat_sparse_read, + .write =3D regcache_flat_sparse_write, + .drop =3D regcache_flat_drop, +}; diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c index c7650fa434ad..0392f5525cf3 100644 --- a/drivers/base/regmap/regcache.c +++ b/drivers/base/regmap/regcache.c @@ -16,6 +16,7 @@ #include "internal.h" =20 static const struct regcache_ops *cache_types[] =3D { + ®cache_flat_sparse_ops, ®cache_rbtree_ops, ®cache_maple_ops, ®cache_flat_ops, diff --git a/drivers/base/regmap/regmap-kunit.c b/drivers/base/regmap/regma= p-kunit.c index 95c5bf2a78ee..f6fc5ed016da 100644 --- a/drivers/base/regmap/regmap-kunit.c +++ b/drivers/base/regmap/regmap-kunit.c @@ -54,6 +54,8 @@ static const char *regcache_type_name(enum regcache_type = type) return "none"; case REGCACHE_FLAT: return "flat"; + case REGCACHE_FLAT_S: + return "flat-sparse"; case REGCACHE_RBTREE: return "rbtree"; case REGCACHE_MAPLE: @@ -93,6 +95,8 @@ static const struct regmap_test_param regcache_types_list= [] =3D { { .cache =3D REGCACHE_NONE, .fast_io =3D true }, { .cache =3D REGCACHE_FLAT }, { .cache =3D REGCACHE_FLAT, .fast_io =3D true }, + { .cache =3D REGCACHE_FLAT_S }, + { .cache =3D REGCACHE_FLAT_S, .fast_io =3D true }, { .cache =3D REGCACHE_RBTREE }, { .cache =3D REGCACHE_RBTREE, .fast_io =3D true }, { .cache =3D REGCACHE_MAPLE }, @@ -104,6 +108,8 @@ KUNIT_ARRAY_PARAM(regcache_types, regcache_types_list, = param_to_desc); static const struct regmap_test_param real_cache_types_only_list[] =3D { { .cache =3D REGCACHE_FLAT }, { .cache =3D REGCACHE_FLAT, .fast_io =3D true }, + { .cache =3D REGCACHE_FLAT_S }, + { .cache =3D REGCACHE_FLAT_S, .fast_io =3D true }, { .cache =3D REGCACHE_RBTREE }, { .cache =3D REGCACHE_RBTREE, .fast_io =3D true }, { .cache =3D REGCACHE_MAPLE }, @@ -119,6 +125,12 @@ static const struct regmap_test_param real_cache_types= _list[] =3D { { .cache =3D REGCACHE_FLAT, .from_reg =3D 0x2002 }, { .cache =3D REGCACHE_FLAT, .from_reg =3D 0x2003 }, { .cache =3D REGCACHE_FLAT, .from_reg =3D 0x2004 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0, .fast_io =3D true }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0x2001 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0x2002 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0x2003 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0x2004 }, { .cache =3D REGCACHE_RBTREE, .from_reg =3D 0 }, { .cache =3D REGCACHE_RBTREE, .from_reg =3D 0, .fast_io =3D true }, { .cache =3D REGCACHE_RBTREE, .from_reg =3D 0x2001 }, @@ -136,6 +148,12 @@ static const struct regmap_test_param real_cache_types= _list[] =3D { KUNIT_ARRAY_PARAM(real_cache_types, real_cache_types_list, param_to_desc); =20 static const struct regmap_test_param sparse_cache_types_list[] =3D { + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0, .fast_io =3D true }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0x2001 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0x2002 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0x2003 }, + { .cache =3D REGCACHE_FLAT_S, .from_reg =3D 0x2004 }, { .cache =3D REGCACHE_RBTREE, .from_reg =3D 0 }, { .cache =3D REGCACHE_RBTREE, .from_reg =3D 0, .fast_io =3D true }, { .cache =3D REGCACHE_RBTREE, .from_reg =3D 0x2001 }, @@ -1597,6 +1615,8 @@ static const struct regmap_test_param raw_types_list[= ] =3D { { .cache =3D REGCACHE_NONE, .val_endian =3D REGMAP_ENDIAN_BIG }, { .cache =3D REGCACHE_FLAT, .val_endian =3D REGMAP_ENDIAN_LITTLE }, { .cache =3D REGCACHE_FLAT, .val_endian =3D REGMAP_ENDIAN_BIG }, + { .cache =3D REGCACHE_FLAT_S, .val_endian =3D REGMAP_ENDIAN_LITTLE }, + { .cache =3D REGCACHE_FLAT_S, .val_endian =3D REGMAP_ENDIAN_BIG }, { .cache =3D REGCACHE_RBTREE, .val_endian =3D REGMAP_ENDIAN_LITTLE }, { .cache =3D REGCACHE_RBTREE, .val_endian =3D REGMAP_ENDIAN_BIG }, { .cache =3D REGCACHE_MAPLE, .val_endian =3D REGMAP_ENDIAN_LITTLE }, @@ -1608,6 +1628,8 @@ KUNIT_ARRAY_PARAM(raw_test_types, raw_types_list, par= am_to_desc); static const struct regmap_test_param raw_cache_types_list[] =3D { { .cache =3D REGCACHE_FLAT, .val_endian =3D REGMAP_ENDIAN_LITTLE }, { .cache =3D REGCACHE_FLAT, .val_endian =3D REGMAP_ENDIAN_BIG }, + { .cache =3D REGCACHE_FLAT_S, .val_endian =3D REGMAP_ENDIAN_LITTLE }, + { .cache =3D REGCACHE_FLAT_S, .val_endian =3D REGMAP_ENDIAN_BIG }, { .cache =3D REGCACHE_RBTREE, .val_endian =3D REGMAP_ENDIAN_LITTLE }, { .cache =3D REGCACHE_RBTREE, .val_endian =3D REGMAP_ENDIAN_BIG }, { .cache =3D REGCACHE_MAPLE, .val_endian =3D REGMAP_ENDIAN_LITTLE }, diff --git a/include/linux/regmap.h b/include/linux/regmap.h index 4e1ac1fbcec4..17bed25dc4e3 100644 --- a/include/linux/regmap.h +++ b/include/linux/regmap.h @@ -55,18 +55,23 @@ struct sdw_slave; #define REGMAP_DOWNSHIFT(s) (s) =20 /* - * The supported cache types, the default is no cache. Any new caches - * should usually use the maple tree cache unless they specifically - * require that there are never any allocations at runtime and can't - * provide defaults in which case they should use the flat cache. The - * rbtree cache *may* have some performance advantage for very low end - * systems that make heavy use of cache syncs but is mainly legacy. + * The supported cache types, the default is no cache. Any new caches sho= uld + * usually use the maple tree cache unless they specifically require that = there + * are never any allocations at runtime in which case they should use the = sparse + * flat cache. The rbtree cache *may* have some performance advantage for= very + * low end systems that make heavy use of cache syncs but is mainly legacy. + * These caches are sparse and entries will be initialized from hardware i= f no + * default has been provided. + * The non-sparse flat cache is provided for compatibility with existing u= sers + * and will zero-initialize cache entries for which no defaults are provid= ed. + * New users should use the sparse flat cache. */ enum regcache_type { REGCACHE_NONE, REGCACHE_RBTREE, REGCACHE_FLAT, REGCACHE_MAPLE, + REGCACHE_FLAT_S, }; =20 /** --=20 2.51.0 From nobody Mon Dec 15 19:10:28 2025 Received: from polaris.svanheule.net (polaris.svanheule.net [84.16.241.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77C7F31327F for ; Wed, 29 Oct 2025 08:13:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.241.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761725585; cv=none; b=AViSRLE4/9C3j7cOlKtKFEwF8UuKQXROzWeMAO/56kfkIEnAcO6dlfrsWOvDoJP1TqQ4F6HqcUoOrwRcgSQMBGjetMp6vD154n5TpfvRTvnZBCaAdra+SacLDLnuEwVGO6Fes8BQOdp2nRx0+YXHUOu2X2fFJUrxns5bbTgODD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761725585; c=relaxed/simple; bh=wnpYbp7ZN08ucpXdNVQLca6PgWG1rE3nJo5xqyRiywU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jN+sDJj1Oe4+Ggu47iWODa1B+N835kKfMtYVEkc81puKfeYbZptzgjkRl8UnNSwXQ56nSKrQa1+F7CDBvWxv/G7Uqj3QtFdmkHtYk8JvyQZ9KrV4lutolveJhyNQBnA5E0AoJDX7DmC+qlAF8X1sesZ/TzDG8BbLN+9uSkugqx4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=svanheule.net; spf=pass smtp.mailfrom=svanheule.net; dkim=pass (2048-bit key) header.d=svanheule.net header.i=@svanheule.net header.b=NCCAic5x; arc=none smtp.client-ip=84.16.241.116 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=svanheule.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=svanheule.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=svanheule.net header.i=@svanheule.net header.b="NCCAic5x" Received: from terra.vega.svanheule.net (2a02-1812-162c-8f00-1e2d-b404-3319-eba8.ip6.access.telenet.be [IPv6:2a02:1812:162c:8f00:1e2d:b404:3319:eba8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sander@svanheule.net) by polaris.svanheule.net (Postfix) with ESMTPSA id 9790F690569; Wed, 29 Oct 2025 09:12:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1761725575; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rJP9P3q1GKK/L+3ZNzKe5LsfQCgQWs0cyhA4ENcB7rE=; b=NCCAic5xQqeB8wlvxNW7UDdrfgpBSt+uq6B6ymV+PizOyPTmBv/KmjL6p3JWgr8xCRiOmD u1ZpQgzHeHG0lfK4iPTUs3EiE1xkYXCi+Zw0ZbJNeNna9Av+Sp25r9KT2n3mBn3/ZWng/r x6hzOdCJIc4pampcxYdFdhwIa1zL59gbTxOlnO0qvc0k5OEGSmixvXnhvx1orfcK0w2iXr W4b4me/fwqV/h8VoGpzE+145rsdz9NILc+BHuODvjBuHwBtTd6urWfoz69xmzLAeuVAWeM VlULqBfhrIUFTa+oJ7GFgIjjYK1OJ7S87Lok3tS9tUaCOeIAxi/T215x/vqPVw== From: Sander Vanheule To: Mark Brown , linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , "Rafael J . Wysocki" , Danilo Krummrich , Sander Vanheule Subject: [PATCH v6 2/2] regmap: warn users about uninitialized flat cache Date: Wed, 29 Oct 2025 09:12:48 +0100 Message-ID: <20251029081248.52607-3-sander@svanheule.net> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251029081248.52607-1-sander@svanheule.net> References: <20251029081248.52607-1-sander@svanheule.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The standard flat cache did not contain any validity info, so the cache was always considered to be entirely valid. Multiple mechanisms exist to initialize the cache on regmap init (defaults, raw defaults, HW init), but not all drivers are using one of these. As a result, their implementation might currently depend on the zero-initialized cache or contain other workarounds. When reading an uninitialized value from the flat cache, warn the user, but maintain the current behavior. This will allow developers to switch to a sparse (flat) cache independently. Signed-off-by: Sander Vanheule --- Changes since v4: - Use one line for the log message - Use dev_warn_once() with test_bit() to avoid log flooding with offset register spaces Changes since v3: - New patch: emit a warning for the flat cache on suspicious registers --- drivers/base/regmap/regcache-flat.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/drivers/base/regmap/regcache-flat.c b/drivers/base/regmap/regc= ache-flat.c index 86f7679175b1..3b9235bb8313 100644 --- a/drivers/base/regmap/regcache-flat.c +++ b/drivers/base/regmap/regcache-flat.c @@ -89,6 +89,11 @@ static int regcache_flat_read(struct regmap *map, struct regcache_flat_data *cache =3D map->cache; unsigned int index =3D regcache_flat_get_index(map, reg); =20 + /* legacy behavior: ignore validity, but warn the user */ + if (unlikely(!test_bit(index, cache->valid))) + dev_warn_once(map->dev, + "using zero-initialized flat cache, this may cause unexpected behavior"= ); + *value =3D cache->data[index]; =20 return 0; @@ -114,17 +119,6 @@ static int regcache_flat_write(struct regmap *map, uns= igned int reg, struct regcache_flat_data *cache =3D map->cache; unsigned int index =3D regcache_flat_get_index(map, reg); =20 - cache->data[index] =3D value; - - return 0; -} - -static int regcache_flat_sparse_write(struct regmap *map, unsigned int reg, - unsigned int value) -{ - struct regcache_flat_data *cache =3D map->cache; - unsigned int index =3D regcache_flat_get_index(map, reg); - cache->data[index] =3D value; __set_bit(index, cache->valid); =20 @@ -158,6 +152,6 @@ struct regcache_ops regcache_flat_sparse_ops =3D { .init =3D regcache_flat_init, .exit =3D regcache_flat_exit, .read =3D regcache_flat_sparse_read, - .write =3D regcache_flat_sparse_write, + .write =3D regcache_flat_write, .drop =3D regcache_flat_drop, }; --=20 2.51.0