linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] nvmem: core: fix read buffer in place
@ 2019-03-20 19:32 Jorge Ramirez-Ortiz
  2019-04-04 11:01 ` Srinivas Kandagatla
  0 siblings, 1 reply; 3+ messages in thread
From: Jorge Ramirez-Ortiz @ 2019-03-20 19:32 UTC (permalink / raw)
  To: jorge.ramirez-ortiz, srinivas.kandagatla; +Cc: linux-kernel, niklas.cassel

When the bit_offset in the cell is zero, the pointer to the msb will
not be properly initialized (ie, will still be pointing to the first
byte in the buffer).

This being the case, if there are bits to clear in the msb, those will
be left untouched while the mask will incorrectly clear bit positions
on the first byte.

This commit also makes sure that any byte unused in the cell is
cleared.

Signed-off-by: Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
---
 drivers/nvmem/core.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index f24008b66826..53dc37574b5d 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1166,7 +1166,7 @@ EXPORT_SYMBOL_GPL(nvmem_cell_put);
 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
 {
 	u8 *p, *b;
-	int i, bit_offset = cell->bit_offset;
+	int i, extra, bit_offset = cell->bit_offset;
 
 	p = b = buf;
 	if (bit_offset) {
@@ -1181,11 +1181,16 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
 			p = b;
 			*b++ >>= bit_offset;
 		}
-
-		/* result fits in less bytes */
-		if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
-			*p-- = 0;
+	} else {
+		/* point to the msb */
+		p += cell->bytes - 1;
 	}
+
+	/* result fits in less bytes */
+	extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
+	while (--extra >= 0)
+		*p-- = 0;
+
 	/* clear msb bits if any leftover in the last byte */
 	*p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvmem: core: fix read buffer in place
  2019-03-20 19:32 [PATCH] nvmem: core: fix read buffer in place Jorge Ramirez-Ortiz
@ 2019-04-04 11:01 ` Srinivas Kandagatla
  0 siblings, 0 replies; 3+ messages in thread
From: Srinivas Kandagatla @ 2019-04-04 11:01 UTC (permalink / raw)
  To: Jorge Ramirez-Ortiz; +Cc: linux-kernel, niklas.cassel



On 20/03/2019 19:32, Jorge Ramirez-Ortiz wrote:
> When the bit_offset in the cell is zero, the pointer to the msb will
> not be properly initialized (ie, will still be pointing to the first
> byte in the buffer).
> 
> This being the case, if there are bits to clear in the msb, those will
> be left untouched while the mask will incorrectly clear bit positions
> on the first byte.
> 
> This commit also makes sure that any byte unused in the cell is
> cleared.
> 
> Signed-off-by: Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
Applied Thanks,
Srini

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] nvmem: core: fix read_buffer in place
@ 2019-02-28 14:22 Jorge Ramirez-Ortiz
  0 siblings, 0 replies; 3+ messages in thread
From: Jorge Ramirez-Ortiz @ 2019-02-28 14:22 UTC (permalink / raw)
  To: jorge.ramirez-ortiz, srinivas.kandagatla; +Cc: linux-kernel

All unused/extra bytes in the cell buffer must be zeroed.

Signed-off-by: Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
---
 drivers/nvmem/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index f7301bb4ef3b..65e198adebac 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1162,7 +1162,7 @@ EXPORT_SYMBOL_GPL(nvmem_cell_put);
 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
 {
 	u8 *p, *b;
-	int i, bit_offset = cell->bit_offset;
+	int i, extra, bit_offset = cell->bit_offset;
 
 	p = b = buf;
 	if (bit_offset) {
@@ -1179,7 +1179,8 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
 		}
 
 		/* result fits in less bytes */
-		if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
+		extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
+		while (--extra >= 0)
 			*p-- = 0;
 	}
 	/* clear msb bits if any leftover in the last byte */
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-04-04 11:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-20 19:32 [PATCH] nvmem: core: fix read buffer in place Jorge Ramirez-Ortiz
2019-04-04 11:01 ` Srinivas Kandagatla
  -- strict thread matches above, loose matches on Subject: below --
2019-02-28 14:22 [PATCH] nvmem: core: fix read_buffer " Jorge Ramirez-Ortiz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).