From nobody Sun Feb 8 06:21:50 2026 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 B37562F28E3 for ; Wed, 22 Oct 2025 20:04:57 +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=1761163499; cv=none; b=hiYvgc2Ay2I04f9uXNY8OLW3JQFhJLTnG/4WSEDfp6LRWn9oIVTQjV7yL888qSannQqGYwkwiRpS3pccmDYFM9Ex8D2PTLVHyidiSrEHl2m1ma8Dl2hbMBrChycJt9s/qdNxZFKdEwKpscXDvjaAgJYXSanYHfM2qm7285da55Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761163499; c=relaxed/simple; bh=vArYOlHSw3dHtGB+jYnhJ5JVoTIgXSpWmJm6vATTd0Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mgzI0MK0neSltDkx4XHImFak25broksRXsEcZ7irUZKg1pwgOaWlhvhscVm9S2fwZIvWLYkxGAyKht1J1vt+EtNB/iIF7NX5xwYjcxQ5LY89CfP7VUBs4nQ9CL4yofuvUKsRXJOyz4DJpUPjl7ohgOq1v9rLrpXCegPq5SXmpyo= 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=KELzKlql; 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="KELzKlql" 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 77ADA68B9A0; Wed, 22 Oct 2025 22:04:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1761163489; 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=bFza/X3iPKHOe33ggUnPmTHTUIBGIsqXCELjzJk8XUI=; b=KELzKlqlV12u6YMu85rOQpxxOjFgHUuR+WQ+4TTL949Vfqz9HDeaA7HZuqrrQh27sea/J8 vLniNDdy6iYvdkmJBFwgmrCM8lXKeI21NOc0+ijeUl8+tvCaIjXBpSP2A34c4QqGo7wpl2 qnvWjbHF533fTu9EFgO0uv6sGBS/ve1/la+7Myc4qbgSfeB+2UzoAKq5Wru4A66ex0d8N5 2Oc0lMbkPJoJpUXQ9B9MKeELiB3fbfRmoqu4smEoI58N9vfmRzXUT2UAse/QnSvZLrkxu9 NYYBUGXBe+huH/eV9yKl5Fus7bgnl/7OOORm54XCuBoLgyIGFSVjGaIwU29E/w== From: Sander Vanheule To: Mark Brown , linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , "Rafael J . Wysocki" , Danilo Krummrich , Sander Vanheule Subject: [PATCH v4 1/2] regmap: add flat cache with sparse validity Date: Wed, 22 Oct 2025 22:04:07 +0200 Message-ID: <20251022200408.63027-2-sander@svanheule.net> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251022200408.63027-1-sander@svanheule.net> References: <20251022200408.63027-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 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 | 103 +++++++++++++++++++++++++--- drivers/base/regmap/regcache.c | 1 + drivers/base/regmap/regmap-kunit.c | 14 ++++ include/linux/regmap.h | 1 + 5 files changed, 109 insertions(+), 11 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..52ce125a74e1 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,63 @@ 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; + unsigned int cache_size; + struct regcache_flat_data *cache =3D NULL; + unsigned long *cache_valid =3D NULL; =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; + + size_t 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 kmalloc(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; + + cache->valid =3D cache_valid; + 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 +87,10 @@ 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]; =20 return 0; } @@ -65,10 +98,48 @@ 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); + + cache->data[index] =3D value; + + 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]; + + 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); =20 - cache[index] =3D value; + 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 +152,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..70ecb3e46472 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: @@ -119,6 +121,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 +144,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 }, diff --git a/include/linux/regmap.h b/include/linux/regmap.h index 4e1ac1fbcec4..a5987f1a1a76 100644 --- a/include/linux/regmap.h +++ b/include/linux/regmap.h @@ -67,6 +67,7 @@ enum regcache_type { REGCACHE_RBTREE, REGCACHE_FLAT, REGCACHE_MAPLE, + REGCACHE_FLAT_S, }; =20 /** --=20 2.51.0 From nobody Sun Feb 8 06:21:50 2026 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 B36C92D0C97 for ; Wed, 22 Oct 2025 20:04:57 +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=1761163499; cv=none; b=OEd+p/LK/ONFvIt3qAdX+frgwmZuU67tvfcLtxWhxWv/eiY926gzOdKfjKc1dJs3QnFcYxg0pRqpPHWPAkI2U13yLwhYWn8W2J2Sd8jfcrWzgZ7S11YlxxQzG2TJchq1pEDNTamKQfN1gUwpCrwPwUrfGdPO1+g1uJ9b7E9/PX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761163499; c=relaxed/simple; bh=pKHq4tL/vSPCuT4H2cv/OCDdlZ8gLd53YfR8eqd9WNQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eq3isBHF6p5SuzAIU06XwFmDF+N4Se8LjS4hNgYbL2Y/qaqUJcxZ/0qdl6Gx447hOpWxUWDm+oPkp8/GMILrlYm6Old7czTjxGndI/oOkxVjcdR3apirJLl02380cKRrN1S+HzFgBxqWtjytYOUQAlagy6REOkzBJaabDHDFNBo= 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=t5Rkj8cm; 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="t5Rkj8cm" 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 BB21D68B9A1; Wed, 22 Oct 2025 22:04:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svanheule.net; s=mail1707; t=1761163489; 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=sOjs6gmHYhxAucDQWaCaiSvU+FT5q72t/PAw72NC2IY=; b=t5Rkj8cmzSccGreeVgIpstkrRV8lW/XZvqkKkzBzCu13W3hpT0u80n5vmfoaMEKdNeEmPg vHbB1goEpXdYZO3QgLqeOuxoKrlJ+SbYM0324sb7eRVNY2zO9CHjCyN0rM6sc3zy5CUoRw 0H9Y2LNA1rn2su2E+j7oxzKB1gQ1GiWtMp54d3zP4R/Nux4GYXZlobDkKeCrRxbX8bpR14 S3NYSnTQqM9aFeanhAuHKvO42QO15fHyb3fyRSSRUS25SsGUwHM63VBGSo8s4kvT+2+Ulb 91O9Oth/wA2AE1y5qaEIHf2Pzjj4LF932WQDhuUftr/qkfZiThpTvnvL5F34kg== From: Sander Vanheule To: Mark Brown , linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , "Rafael J . Wysocki" , Danilo Krummrich , Sander Vanheule Subject: [PATCH v4 2/2] regmap: warn users about uninitialized flat cache Date: Wed, 22 Oct 2025 22:04:08 +0200 Message-ID: <20251022200408.63027-3-sander@svanheule.net> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251022200408.63027-1-sander@svanheule.net> References: <20251022200408.63027-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 --- I've decided to use dev_warn() with __test_and_set_bit() to avoid log flooding from e.g. regmap_update_bits() deciding not to write an updated value. This way the warning is only issued once per invalid register, which will hopefully be a bit more obvious than a single log entry from a dev_warn_once(). Changes since v3: - New patch: emit a warning for the flat cache on suspicious registers --- drivers/base/regmap/regcache-flat.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/drivers/base/regmap/regcache-flat.c b/drivers/base/regmap/regc= ache-flat.c index 52ce125a74e1..8802713e0cc6 100644 --- a/drivers/base/regmap/regcache-flat.c +++ b/drivers/base/regmap/regcache-flat.c @@ -90,18 +90,13 @@ 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 - *value =3D cache->data[index]; - - return 0; -} - -static int regcache_flat_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); + /* legacy behavior: ignore validity, but warn the user once per reg */ + if (unlikely(!__test_and_set_bit(index, cache->valid))) + dev_warn(map->dev, + "using zero-initialized flat cache, " + "this may cause unexpected behavior"); =20 - cache->data[index] =3D value; + *value =3D cache->data[index]; =20 return 0; } @@ -120,7 +115,7 @@ static int regcache_flat_sparse_read(struct regmap *map, return 0; } =20 -static int regcache_flat_sparse_write(struct regmap *map, unsigned int reg, +static int regcache_flat_write(struct regmap *map, unsigned int reg, unsigned int value) { struct regcache_flat_data *cache =3D map->cache; @@ -159,6 +154,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