2023-11-01 14:31:02

by Ben Wolsieffer

[permalink] [raw]
Subject: [PATCH] regmap: prevent noinc writes from clobbering cache

Currently, noinc writes are cached as if they were standard incrementing
writes, overwriting unrelated register values in the cache. Instead, we
want to cache the last value written to the register, as is done in the
accelerated noinc handler (regmap_noinc_readwrite).

Fixes: cdf6b11daa77 ("regmap: Add regmap_noinc_write API")
Signed-off-by: Ben Wolsieffer <[email protected]>
---
drivers/base/regmap/regmap.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 234a84ecde8b..ea6157747199 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -1620,17 +1620,19 @@ static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
}

if (!map->cache_bypass && map->format.parse_val) {
- unsigned int ival;
+ unsigned int ival, offset;
int val_bytes = map->format.val_bytes;
- for (i = 0; i < val_len / val_bytes; i++) {
- ival = map->format.parse_val(val + (i * val_bytes));
- ret = regcache_write(map,
- reg + regmap_get_offset(map, i),
- ival);
+
+ /* Cache the last written value for noinc writes */
+ i = noinc ? val_len - val_bytes : 0;
+ for (; i < val_len; i += val_bytes) {
+ ival = map->format.parse_val(val + i);
+ offset = noinc ? 0 : regmap_get_offset(map, i / val_bytes);
+ ret = regcache_write(map, reg + offset, ival);
if (ret) {
dev_err(map->dev,
"Error in caching of register: %x ret: %d\n",
- reg + regmap_get_offset(map, i), ret);
+ reg + offset, ret);
return ret;
}
}
--
2.42.0


2023-11-01 17:07:00

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH] regmap: prevent noinc writes from clobbering cache

On Wed, Nov 01, 2023 at 10:29:27AM -0400, Ben Wolsieffer wrote:
> Currently, noinc writes are cached as if they were standard incrementing
> writes, overwriting unrelated register values in the cache. Instead, we
> want to cache the last value written to the register, as is done in the
> accelerated noinc handler (regmap_noinc_readwrite).

Could you please add a kunit test for this?


Attachments:
(No filename) (393.00 B)
signature.asc (499.00 B)
Download all attachments

2023-11-01 18:07:18

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH] regmap: prevent noinc writes from clobbering cache

On Wed, Nov 01, 2023 at 10:29:27AM -0400, Ben Wolsieffer wrote:
> Currently, noinc writes are cached as if they were standard incrementing
> writes, overwriting unrelated register values in the cache. Instead, we
> want to cache the last value written to the register, as is done in the
> accelerated noinc handler (regmap_noinc_readwrite).
>
> Fixes: cdf6b11daa77 ("regmap: Add regmap_noinc_write API")
> Signed-off-by: Ben Wolsieffer <[email protected]>
> ---
> drivers/base/regmap/regmap.c | 16 +++++++++-------
> 1 file changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
> index 234a84ecde8b..ea6157747199 100644
> --- a/drivers/base/regmap/regmap.c
> +++ b/drivers/base/regmap/regmap.c
> @@ -1620,17 +1620,19 @@ static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
> }
>
> if (!map->cache_bypass && map->format.parse_val) {
> - unsigned int ival;
> + unsigned int ival, offset;
> int val_bytes = map->format.val_bytes;
> - for (i = 0; i < val_len / val_bytes; i++) {
> - ival = map->format.parse_val(val + (i * val_bytes));
> - ret = regcache_write(map,
> - reg + regmap_get_offset(map, i),
> - ival);
> +
> + /* Cache the last written value for noinc writes */
> + i = noinc ? val_len - val_bytes : 0;
> + for (; i < val_len; i += val_bytes) {
> + ival = map->format.parse_val(val + i);
> + offset = noinc ? 0 : regmap_get_offset(map, i / val_bytes);
> + ret = regcache_write(map, reg + offset, ival);
> if (ret) {
> dev_err(map->dev,
> "Error in caching of register: %x ret: %d\n",
> - reg + regmap_get_offset(map, i), ret);
> + reg + offset, ret);
> return ret;
> }
> }
> --
> 2.42.0
>

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him
a patch that has triggered this response. He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created. Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- You have marked a patch with a "Fixes:" tag for a commit that is in an
older released kernel, yet you do not have a cc: stable line in the
signed-off-by area at all, which means that the patch will not be
applied to any older kernel releases. To properly fix this, please
follow the documented rules in the
Documentation/process/stable-kernel-rules.rst file for how to resolve
this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot

2023-11-01 20:20:26

by Ben Wolsieffer

[permalink] [raw]
Subject: Re: [PATCH] regmap: prevent noinc writes from clobbering cache

Hi Mark,

On Wed, Nov 01, 2023 at 05:05:39PM +0000, Mark Brown wrote:
> On Wed, Nov 01, 2023 at 10:29:27AM -0400, Ben Wolsieffer wrote:
> > Currently, noinc writes are cached as if they were standard incrementing
> > writes, overwriting unrelated register values in the cache. Instead, we
> > want to cache the last value written to the register, as is done in the
> > accelerated noinc handler (regmap_noinc_readwrite).
>
> Could you please add a kunit test for this?

I started to look into this, but it is not currently possible to test
noinc behavior with regmap_[raw_]ram. The same bulk write operation is
used by both incrementing and non-incrementing writes, and the difference
in behavior is due to how the hardware handles the bulk write to a
particular register.

To test this behavior, regmap_raw_ram (raw because it supports bulk
writes) would have to be told that certain of its registers should
implement noinc semantics.

Is this something I should implement?

Thanks, Ben

2023-11-01 20:25:02

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH] regmap: prevent noinc writes from clobbering cache

On Wed, Nov 01, 2023 at 04:20:08PM -0400, Ben Wolsieffer wrote:
> On Wed, Nov 01, 2023 at 05:05:39PM +0000, Mark Brown wrote:

> > Could you please add a kunit test for this?

> I started to look into this, but it is not currently possible to test
> noinc behavior with regmap_[raw_]ram. The same bulk write operation is
> used by both incrementing and non-incrementing writes, and the difference
> in behavior is due to how the hardware handles the bulk write to a
> particular register.

> To test this behavior, regmap_raw_ram (raw because it supports bulk
> writes) would have to be told that certain of its registers should
> implement noinc semantics.

> Is this something I should implement?

That would be great, yes - I've just been implementing features in those
map types on an as needed basis as I've been writing tests and I didn't
get round to covering noinc yet.


Attachments:
(No filename) (898.00 B)
signature.asc (499.00 B)
Download all attachments

2023-11-02 11:52:52

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH] regmap: prevent noinc writes from clobbering cache

On Wed, 01 Nov 2023 10:29:27 -0400, Ben Wolsieffer wrote:
> Currently, noinc writes are cached as if they were standard incrementing
> writes, overwriting unrelated register values in the cache. Instead, we
> want to cache the last value written to the register, as is done in the
> accelerated noinc handler (regmap_noinc_readwrite).
>
>

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-next

Thanks!

[1/1] regmap: prevent noinc writes from clobbering cache
commit: 984a4afdc87a1fc226fd657b1cd8255c13d3fc1a

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark