From patchwork Wed Mar 20 19:32:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jorge Ramirez-Ortiz X-Patchwork-Id: 160729 Delivered-To: patch@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp666957jan; Wed, 20 Mar 2019 12:32:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqyYYH1tiWaKdHhuytiGuD45oBuyc/PDIQrZvFCkAHxjm6gGdIwoxzKiG1ePXz5bJmb1+a+z X-Received: by 2002:a17:902:523:: with SMTP id 32mr9401488plf.249.1553110371463; Wed, 20 Mar 2019 12:32:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553110371; cv=none; d=google.com; s=arc-20160816; b=RZSGq1psmcBlawBXfdqLDGovSvKxlQl/AV7qye8G3dH3ij44Nc6jTStJdkylfQ/i8V VlimYaYYxk58iIfUKjuOTuwmJvTu8OQWAfkkTLOEKLLqTLEPSoNaO1DMttm13bfCkqxV pUGz02DO34vgOTv97ULYAYHVKiwM6m29AgNx+lEYtlA5xjqT11Rz8D0NsJfgCCZCxQqU j/rl0lS9VBWndLeqVUtuotUEPqZn2J3qxmwIc+AbG+stvDSrc8Af5kUbBALOpwYCFMxV bcEvy+05hjqSyoezWxfUIlRn2k8F3zXdgztElZsTxvhctB1620i9BDZvFu6ECcCKa2Xt PIyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=ynaleAgMhlwaGg4TQUwydIHUo2lHIkdidB1ei/USpcw=; b=TXNam+xveE/Wtxay+SncMRvxO/VVGDuU0JJejafEohyyshaFoVLnfYRR8BbO6LRJ7j m9QwneKbypgJz9JC4jsM+Y5NRk51B7aY/Vz5bvJ3jliOiQtK5kiInNSIUoupAA1NhFbX 2lP2M/ooROmLMLkYfaYHF7whUu8Z3xHipuzzshCT39X11Tk4qt6fEHrUDocm68X170GS YZdMTKbOrsn9EIM4SMoDbMHuEYn6gKumfM/gYtIgiPVgarS2OddHi71jhQmWUWmgJ1Lp XMVsGrFcbzDi+31YHxHgFKfzmfkCKr1yWoO3iIG95Q3GlmJOvzvkY8k66SJ7URE4EkDF XKyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Uu9oRmEl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f7si2269108pgo.569.2019.03.20.12.32.51; Wed, 20 Mar 2019 12:32:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Uu9oRmEl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726832AbfCTTct (ORCPT + 31 others); Wed, 20 Mar 2019 15:32:49 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:37906 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726067AbfCTTcs (ORCPT ); Wed, 20 Mar 2019 15:32:48 -0400 Received: by mail-wr1-f66.google.com with SMTP id g12so4106791wrm.5 for ; Wed, 20 Mar 2019 12:32:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ynaleAgMhlwaGg4TQUwydIHUo2lHIkdidB1ei/USpcw=; b=Uu9oRmEl+z3eLQb2JZs2y4Me2w0xCyq4aXMzqfXCoBr+W6QG8W6m8kCZlfgYo/UpcL f0lt6ZQwcmrnVP7fV3OEVvtCupy5Mzqupyn04+hc+sN5jstNqveyj8z4xA+ZGXw/yLDs mM5Pw7dbJGbGY62mK1wUClAIrgybSc9vT0DUULfgaV5TzOKmQJcPAhD1uqhj9M52zPI2 i5mBGVCom+oeKR7S2RBPQ2OGgqkeB+6DnPPCehc2k3Tb9hRVhMvsojgfkeFdvEZQ+DYx d2YUGmKsaXzHvHHdb0SZcgK/1lfTJmZMLsdgo13wl6JqtqXkYCWAx6G161Wv2Tplw5O0 bkRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ynaleAgMhlwaGg4TQUwydIHUo2lHIkdidB1ei/USpcw=; b=EsRjyHajCtf1dk9Tym4Da3GQbcPJBZ3sHpoLAEAONAZ2AXy9MHVDIwH0jNzGqaUFcn 9fmsfKrjtFKPSnKoXPi+ufW6ReosyMxCUM4NU0RdoNMxexVXZMeAzCg0o6rx/+/783K5 SeVeZwlOuVjAryHR5RBg66TuzX/t9KQDHUG22LKY02OXq1pjQWlH7USDCoIDxTYLUVYp eXzIl1mGTz9z76jQRPPoC4iq8VFBxP9pCOYBI3N25cgk1ttBufTe9abmg2bUHJ5/EWXW MDEzQpYmfsSeJ9NG+HP5EDrYKHo9a2R30TqKKkPz2ieH2S/SPd/V8aN9KgeqR1XQmr7h xh0g== X-Gm-Message-State: APjAAAWAI2d9XTKxB2tdpQKFVT6kUAquUWNac4lthbHFiIPFK7YdP8iZ j1xRv8VjxTfb1LqlFGFhRMv95AEnK3g= X-Received: by 2002:a5d:4f8b:: with SMTP id d11mr12336222wru.150.1553110366758; Wed, 20 Mar 2019 12:32:46 -0700 (PDT) Received: from localhost.localdomain (52.red-95-121-90.dynamicip.rima-tde.net. [95.121.90.52]) by smtp.gmail.com with ESMTPSA id b10sm3429889wru.92.2019.03.20.12.32.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 20 Mar 2019 12:32:45 -0700 (PDT) From: Jorge Ramirez-Ortiz To: jorge.ramirez-ortiz@linaro.org, srinivas.kandagatla@linaro.org Cc: linux-kernel@vger.kernel.org, niklas.cassel@linaro.org Subject: [PATCH] nvmem: core: fix read buffer in place Date: Wed, 20 Mar 2019 20:32:42 +0100 Message-Id: <20190320193242.20159-1-jorge.ramirez-ortiz@linaro.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- drivers/nvmem/core.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) -- 2.20.1 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); }