]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
spi: spi-mem: avoid mutating op template in spi_mem_supports_op()
authorSanthosh Kumar K <s-k6@ti.com>
Wed, 27 May 2026 17:37:36 +0000 (23:07 +0530)
committerMark Brown <broonie@kernel.org>
Thu, 28 May 2026 12:49:00 +0000 (13:49 +0100)
spi_mem_supports_op() accepts a const struct spi_mem_op pointer but
casts away const internally to call spi_mem_adjust_op_freq(). This
mutates the caller's op template, which causes stale max_freq values
when callers reuse persistent templates - subsequent calls won't
re-apply the device frequency cap since spi_mem_adjust_op_freq()
skips non-zero values.

Fix by operating on a stack-local copy instead.

Fixes: a4f8e70d75dd ("spi: spi-mem: add spi_mem_adjust_op_freq() in spi_mem_supports_op()")
Cc: Tianyu Xu <xtydtc@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Santhosh Kumar K <s-k6@ti.com>
Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://patch.msgid.link/20260527173736.2243004-1-s-k6@ti.com
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/spi/spi-mem.c

index a09371a075d2ec23328774807ed6855653bb3320..93266848c6dfe8728e3046bf605f9b8c11374da2 100644 (file)
@@ -279,13 +279,20 @@ static bool spi_mem_internal_supports_op(struct spi_mem *mem,
  */
 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
 {
-       /* Make sure the operation frequency is correct before going futher */
-       spi_mem_adjust_op_freq(mem, (struct spi_mem_op *)op);
+       struct spi_mem_op eval_op = *op;
+
+       /*
+        * Work on a local copy; this is a pure capability check and must
+        * not modify the caller's op. Stored templates with max_freq == 0
+        * must remain unset so their frequency is always re-capped to the
+        * current device maximum at execution time.
+        */
+       spi_mem_adjust_op_freq(mem, &eval_op);
 
-       if (spi_mem_check_op(op))
+       if (spi_mem_check_op(&eval_op))
                return false;
 
-       return spi_mem_internal_supports_op(mem, op);
+       return spi_mem_internal_supports_op(mem, &eval_op);
 }
 EXPORT_SYMBOL_GPL(spi_mem_supports_op);