From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796167636; b=yaEdVteafTojA8ME2/wjdZJblaJFtY9j5mih0fdSi2+jNMV8rHH26SgLKCY6gvYxsc1H rHu/929OSVf6hIwmAyFFwygesbOHpZDQxdneh1h35J8+f04fFvzi0GrLf/Ucd25Syl2Tg xFfwuZnvUAcGfAVO6qy4OnSTjeJzFPOfunX0YE4KsjTvY3Lo7z/QY1NxjAIZG0wy/RPZk Fr/Bf341FHSi1GWLgJi06Rdzdyrnj+ijOFh5PUxfoY1XEcdg06/RVfYvTcuptSETCqrj6 eDiDuqGtzhHUxYNjVdjN+6UAtNk2lnQE5Dte1bMhmNfnxFrJOBYRqhsdVfcTj3rfe6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796167636; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=XzFq+Sto0F0UJGF0Fcos9VVxTTJDhvunWGHN3uSvo4k=; b=t635QmsrnaFW6hW93SxIkHCMKp1gPQDjEvZWVsQHT3QfN2UHfoV5LFyPoqNBfH3Lmd8k k8eKKY5IRokBbPbjITEiLvajrxgt5hLzNqXa7IrGM+pwQ+4xLhLJ2DdJc1xBROOKjkw7q e9CmZbKfrmdzevHQTIGF/7b2lze7EAc8UmaIQepsOuy5sunx3G0/BuiDDBtLbCuQCscvZ skpwzZ3VpXCo9uCIgfpZqC1SQ2zUWyqiSlobfKLK65beMDDPRpv6g1eWb+qRzTFn5vhky J9Bqx4ZvYtcUAq8tbfDu2H6o1UIiRCcQ/ioMZe9Tuay7/KW/cLEirY3mIdq1Zp+md3A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=brA3D4Bh; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48E47606A337; Tue, 1 Dec 2026 23:27:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=brA3D4Bh Received: by smtp.kernel.org (Postfix) with ESMTPSA id B10D51F0089B; Tue, 1 Dec 2026 23:27:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796167636; bh=XzFq+Sto0F0UJGF0Fcos9VVxTTJDhvunWGHN3uSvo4k=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=brA3D4BhsI1iTyPjQX/H9OUgQ6oglrrtTH+cyvm+mMsFl5TVKPdQYMFkiiZTLEGFn TVKazlinZT4cavumvpd9N5RosmTXXEBFih30MW0wBAGYb3Vj3wJ6avRQD6ocsckXuF CD4fC8mniv2o+yuUd5lgMQbssVUIwzhQ4pqfPwg2cJElYEWtFR1NrHPEU2tvKqzuu/ ZFulhGtvWcPORL801t34Yn2NMSZvN0yRhqejMAxb6dNMxxd3buXJ3QcdXiHgHUirfX 5kMFDjyk3Auo2DGd06TSkWxBCAiZCVco11p+wLWbKk7KRoagG2+NR3Hg/38a3Ft8Bc QNiW7aqtsQOpA== From: srini@kernel.org To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, James Hilliard , stable@vger.kernel.org, Srinivas Kandagatla Subject: [PATCH 04/10] nvmem: drain keepout and cell post-processing accesses Date: Tue, 1 Dec 2026 23:26:51 +0000 Message-ID: <20261201232657.537736-5-srini@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201232657.537736-1-srini@kernel.org> References: <20261201232657.537736-1-srini@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: James Hilliard A provider can unregister while a read is still walking its keepout table or running a cell's post-processing callback. Waiting for the register callback alone does not keep that provider state alive. Take the SRCU read lock once for each register or cell access. Use the same operations pointer for every chunk, including the reads needed for a partial-byte write. Unregister then waits for the entire operation; new accesses fail even if they would only touch a keepout. Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/core.c | 110 ++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index be7c046b703d..81d787f7050a 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -54,32 +54,18 @@ static LIST_HEAD(nvmem_lookup_list); static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); -static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, - void *val, size_t bytes) +static int nvmem_access_chunk(struct nvmem_device *nvmem, + const struct nvmem_operations *ops, + unsigned int offset, void *val, size_t bytes, bool write) { - struct nvmem_operations *ops; - - guard(srcu)(&nvmem->srcu); - ops = srcu_dereference(nvmem->ops, &nvmem->srcu); - if (!ops) - return -ENODEV; - - if (!ops->reg_read) - return -EOPNOTSUPP; - - return ops->reg_read(nvmem->priv, offset, val, bytes); -} - -static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, - void *val, size_t bytes) -{ - struct nvmem_operations *ops; int ret, wr_ok; - guard(srcu)(&nvmem->srcu); - ops = srcu_dereference(nvmem->ops, &nvmem->srcu); - if (!ops) - return -ENODEV; + if (!write) { + if (!ops->reg_read) + return -EOPNOTSUPP; + + return ops->reg_read(nvmem->priv, offset, val, bytes); + } if (!ops->reg_write) return -EOPNOTSUPP; @@ -97,17 +83,20 @@ static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, return wr_ok; } -static int nvmem_access_with_keepouts(struct nvmem_device *nvmem, - unsigned int offset, void *val, - size_t bytes, int write) +static int nvmem_access_locked(struct nvmem_device *nvmem, + const struct nvmem_operations *ops, + unsigned int offset, void *val, size_t bytes, bool write) { - unsigned int end = offset + bytes; unsigned int kend, ksize; const struct nvmem_keepout *keepout = nvmem->keepout; - const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout; + const struct nvmem_keepout *keepoutend; int rc; + if (!nvmem->nkeepout) + return nvmem_access_chunk(nvmem, ops, offset, val, bytes, write); + + keepoutend = keepout + nvmem->nkeepout; /* * Skip all keepouts before the range being accessed. * Keepouts are sorted. @@ -120,11 +109,7 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem, if (offset < keepout->start) { kend = min(end, keepout->start); ksize = kend - offset; - if (write) - rc = __nvmem_reg_write(nvmem, offset, val, ksize); - else - rc = __nvmem_reg_read(nvmem, offset, val, ksize); - + rc = nvmem_access_chunk(nvmem, ops, offset, val, ksize, write); if (rc) return rc; @@ -150,13 +135,8 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem, * If we ran out of keepouts but there's still stuff to do, send it * down directly */ - if (offset < end) { - ksize = end - offset; - if (write) - return __nvmem_reg_write(nvmem, offset, val, ksize); - else - return __nvmem_reg_read(nvmem, offset, val, ksize); - } + if (offset < end) + return nvmem_access_chunk(nvmem, ops, offset, val, end - offset, write); return 0; } @@ -164,19 +144,27 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem, static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, void *val, size_t bytes) { - if (!nvmem->nkeepout) - return __nvmem_reg_read(nvmem, offset, val, bytes); + const struct nvmem_operations *ops; + + guard(srcu)(&nvmem->srcu); + ops = srcu_dereference(nvmem->ops, &nvmem->srcu); + if (!ops) + return -ENODEV; - return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false); + return nvmem_access_locked(nvmem, ops, offset, val, bytes, false); } static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, void *val, size_t bytes) { - if (!nvmem->nkeepout) - return __nvmem_reg_write(nvmem, offset, val, bytes); + const struct nvmem_operations *ops; + + guard(srcu)(&nvmem->srcu); + ops = srcu_dereference(nvmem->ops, &nvmem->srcu); + if (!ops) + return -ENODEV; - return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true); + return nvmem_access_locked(nvmem, ops, offset, val, bytes, true); } #ifdef CONFIG_NVMEM_SYSFS @@ -1650,10 +1638,15 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem, struct nvmem_cell_entry *cell, void *buf, size_t *len, const char *id, int index) { + const struct nvmem_operations *ops; int rc; - rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len); + guard(srcu)(&nvmem->srcu); + ops = srcu_dereference(nvmem->ops, &nvmem->srcu); + if (!ops) + return -ENODEV; + rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->raw_len, false); if (rc) return rc; @@ -1709,6 +1702,7 @@ void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len) EXPORT_SYMBOL_GPL(nvmem_cell_read); static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell, + const struct nvmem_operations *ops, u8 *_buf, int len) { struct nvmem_device *nvmem = cell->nvmem; @@ -1728,7 +1722,7 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell, *b <<= bit_offset; /* setup the first byte with lsb bits from nvmem */ - rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); + rc = nvmem_access_locked(nvmem, ops, cell->offset, &v, 1, false); if (rc) goto err; *b++ |= GENMASK(bit_offset - 1, 0) & v; @@ -1747,8 +1741,8 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell, /* if it's not end on byte boundary */ if ((nbits + bit_offset) % BITS_PER_BYTE) { /* setup the last byte with msb bits from nvmem */ - rc = nvmem_reg_read(nvmem, - cell->offset + cell->bytes - 1, &v, 1); + rc = nvmem_access_locked(nvmem, ops, cell->offset + cell->bytes - 1, + &v, 1, false); if (rc) goto err; *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v; @@ -1764,10 +1758,18 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell, static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len) { struct nvmem_device *nvmem = cell->nvmem; + const struct nvmem_operations *ops; int rc; - if (!nvmem || nvmem->read_only || - (cell->bit_offset == 0 && len != cell->bytes)) + if (!nvmem) + return -EINVAL; + + guard(srcu)(&nvmem->srcu); + ops = srcu_dereference(nvmem->ops, &nvmem->srcu); + if (!ops) + return -ENODEV; + + if (nvmem->read_only || (cell->bit_offset == 0 && len != cell->bytes)) return -EINVAL; /* @@ -1781,12 +1783,12 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si if (cell->bit_offset || cell->nbits) { if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes) return -EINVAL; - buf = nvmem_cell_prepare_write_buffer(cell, buf, len); + buf = nvmem_cell_prepare_write_buffer(cell, ops, buf, len); if (IS_ERR(buf)) return PTR_ERR(buf); } - rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); + rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->bytes, true); /* free the tmp buffer */ if (cell->bit_offset || cell->nbits) -- 2.53.0