From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FBC4361640 for ; Mon, 23 Feb 2026 10:58:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771844284; cv=none; b=HNNwedve9dhHaF45uaPii/l4SEXAJVgpBv9ykszwxV3Nw5EaZjvwgLFPytra06vqYNH4EmgtqiSF0PrMHq1M1c/PcX9FmHSy1Gky6/uwNriQGpgj2p84QQ1fnGliVvjYhBYFFJ9Ho6A8S/0h0QOi79N/KxH0bZEGemuzczh++Fs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771844284; c=relaxed/simple; bh=zVasZljh3xnjiCR3b/CBSwA5o+gxDn0uCcEsXA6fYTM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=STSTPf5INhLflZzomqNdScS+Naz+iXscwVjBwiHrRpGBSCTaf1+GtXxr4260qJshgHosFcuV7AxFpTClyKZMsHf3nAUVbvvyKR1Um+LcFkW1c2Dx9F6zvMhLoVm8EYSae7JvaY1Es/sPbMQt3rKeVACBnbO9cTQ1cCBqZtCQZJ8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=jkRYZdRL; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=KZoND2k2; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="jkRYZdRL"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="KZoND2k2" Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 61N9s4DO2001244 for ; Mon, 23 Feb 2026 10:58:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= 1NEpixV2Iw6Z3EhLtd6F7csj4AhOgi1LKFmxR1LHcUo=; b=jkRYZdRLTFxpAmQ9 SfR2WQR1L5azqThsKMfsLRtGG0BagStFQixWjUyJsW0qfZAYq7jaytnLPYZxxaYS S/74RIm54nXTGPUAN2+B+sn3345rREe99Bss+xGoWZvEYPKBOIdykWqd7HwjKe8K Q3qrxbM0K5nrT5KBC/2QMC/dx4TweCxbmmJOXkZsrEeLT5fyMXzvNFwty420qxuU 7isl4fn97YXCKIKMq+hURgAtrcS30gxnt4+esxxIw2vOov3UUD68NJTZDODtGcik IuTSvnMJ/gT4cMWNjuaqWlt6PiWfgIKuLvrFYMKQTZT0c1ynxAl38cjT+TzMmDWK SuqhTg== Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4cf5wavfbc-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Mon, 23 Feb 2026 10:58:02 +0000 (GMT) Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-8cb5359e9d3so4263409685a.2 for ; Mon, 23 Feb 2026 02:58:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1771844281; x=1772449081; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1NEpixV2Iw6Z3EhLtd6F7csj4AhOgi1LKFmxR1LHcUo=; b=KZoND2k2zd/6rJZobw7Rw3BLqgbToM9lYHURczU33v/joVmOq2dTAtkAO2aED2IiRj lBQ/6uIeY1233GATo+zy6Nrbrou6c5ofgRMXAWGL0VkdjBrudXfKBEHZj1FL/KWIUqGN 09hbx9/k+wKAEBSdg4lijyWuczf2Gi/9i232ZASg8LISMGv4iVBZDtWwjPxMG0cpJmqU HXiQomcxTfK0n4J97dYw/UKfXOohnLISzaaXk0CMjT6xW0ffHAWzwHB/lxts6OzGUS1Q d0EiHfiHLCLP3vSxiEUb/HVM2IZyYwgatYjChOQAVYT8lGDiOcF/dUqwNvBVCg6Udy1a TYYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771844281; x=1772449081; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=1NEpixV2Iw6Z3EhLtd6F7csj4AhOgi1LKFmxR1LHcUo=; b=L1cX/XeGFlw/jW9fQXI7vblsMGb5kumIeSSHK8ZLR/tGmdK/Z16QuoOPMEtkrVJzuG RNa/3SZnqJ0eOMoRrN5eUm9veFROO9piNHCz5o6g4P8JrU1Ali/S7mztm3MFe7J8XZRY k//f7Cz1b0gUpbnoaUzlLzpMBk+1jU8tQorEWf3uKF2EOTNc4hrQ4ZgZEzaHsktrEU4x TF75Hle8W4RzJ7YxV+a5VVqOwTckWF/5iAs1r2x5zHy2bqSAjnpWxwsJ1jygKCuhGk88 2r2MBpuA23R+eRBVA+CXsMABY2eyO7O92Aogus3sowWAGjZw6mCIl6n2XZti/hN0G01t 9K0w== X-Gm-Message-State: AOJu0YxF1nCFz8Vj8d002JSD7M7XkMosyQffDMNcRhsDRzXwzTaJPJ3P gunjuc639E3KWlI4BRPYysGM+o0QTBgCtLYksAIRM9Ernk9zCOPgPTUDxC+P7e1wL53eUXD65pO dttyGGMyxCcU32/H0ZWKlv0zbfpoXA7Wg+snv+y1nALQ4XXxwgPVETjgTh0bnJOU8qw7f5ja5gJ 0= X-Gm-Gg: AZuq6aISLTEoZt5ZYkKboPqrdxTxygWGV+OBE/WA6jrFBx9ZqS15JCEfMu/pwNkUBm9 klt4T58y15yKxl7ZmyH2TncNuwXhef46Rc34WszrDvQGmS/4VpfTYMMwZU77WoRfyVc6UlbC86Z IBigC7w1KFxUQkExRbovpxTvmCN+EPK0VY8M2YjZ/j6xNqiZyb1GarvxkPGPB9eYxVqWuM5J0e/ y8WSea+hUUU7+tnDNJTMjgcq1Ac+22R+cvYBrnzOpoPixQUQbTuk0gD7q8hMEYL+tdcYf0U4+Gt tQyvpG1rkbx6NOcvobjG7mFYcylPaZVBsMiGiSc60GG/9gMaTMtakhZp4MCp9FkkP1BngnzZdtz 4kQihH73b8/YNzP3leEaWf4nEnEfb4uVrcKkFHkQKXHhZDUJvrkZQ X-Received: by 2002:a05:620a:3943:b0:8b2:2066:ffca with SMTP id af79cd13be357-8cb8cab0d39mr888794185a.82.1771844281337; Mon, 23 Feb 2026 02:58:01 -0800 (PST) X-Received: by 2002:a05:620a:3943:b0:8b2:2066:ffca with SMTP id af79cd13be357-8cb8cab0d39mr888792685a.82.1771844280881; Mon, 23 Feb 2026 02:58:00 -0800 (PST) Received: from brgl-qcom.local ([2a01:cb1d:dc:7e00:bd71:422c:5e83:8b37]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43970c00c18sm18120002f8f.14.2026.02.23.02.58.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Feb 2026 02:58:00 -0800 (PST) From: Bartosz Golaszewski Date: Mon, 23 Feb 2026 11:57:08 +0100 Subject: [PATCH v2 7/7] nvmem: synchronize nvmem device unregistering with SRCU Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260223-nvmem-unbind-v2-7-0df33a933dca@oss.qualcomm.com> References: <20260223-nvmem-unbind-v2-0-0df33a933dca@oss.qualcomm.com> In-Reply-To: <20260223-nvmem-unbind-v2-0-0df33a933dca@oss.qualcomm.com> To: Srinivas Kandagatla , Bartosz Golaszewski Cc: linux-kernel@vger.kernel.org, brgl@kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=10275; i=bartosz.golaszewski@oss.qualcomm.com; h=from:subject:message-id; bh=zVasZljh3xnjiCR3b/CBSwA5o+gxDn0uCcEsXA6fYTM=; b=owEBbQKS/ZANAwAKAQWdLsv/NoTDAcsmYgBpnDKu55K0CBFP7YdFwNXYyVZlptvkOMJfmLEkn G7fIhOG71KJAjMEAAEKAB0WIQSR5RMt5bVGHXuiZfwFnS7L/zaEwwUCaZwyrgAKCRAFnS7L/zaE w5EgD/9zRULbDPORWxSqPirTumCsUN4T8j1S96kJZxcu29+2HfYPZhaNfwxBxxJDj09gNDbpel/ eiZ8xGIRjpLbbktBVoV76fxSIQKliVSK7aQVS94+mr8aSjriI0D2W9JoIcFqnqVfMljguS0LWqH bWHT03BfuBgAyA6MsQHAMvdoepr0EePhMoMp8q0tNFFDueLqpvsaUTA/FjLm+jaAJ77G5doNVg+ VRD/0kf2B/mN6K8G+o1C667MvMmkRcfva8rhONfT278I7SXHgy0F5fe1tfFbtu830pnS4ISkMPc CORYaOziX9IXQyTpEJQQrfu1BxHDkIns9NSWCyqwx3ZUgR3mJyWoSH6wpODV+vRXphhHOx9MD7o j9I0BEHoNxx8vYtEauEoH1E31y8WXVLbB34w00/P/agCX+5T5eGvyhhouwertp4P6FFF6LfOVk9 XR7+NRuoFHAcMeluHZDxxR3+0X3qjhpIYAbRJhb4bgsGe7BnRH2JhZHwUUBEOX3olTGu1iiwf3d a//LBVbqnjRH6dWPV3cAzOSytcGV3uTeReyM/bhqW/ZWBVZd8wIuLbTKdkAKSZp8HVnnRgMkbHe srtn+ypob82RsGtwAUDNsqScyFiLejCWwyGABhA1GXf+yhc69g27MYqTn2H2oRjXyZLi68S7ELQ HEqZlc5dwbA87xw== X-Developer-Key: i=bartosz.golaszewski@oss.qualcomm.com; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 X-Authority-Analysis: v=2.4 cv=KJVXzVFo c=1 sm=1 tr=0 ts=699c32ba cx=c_pps a=HLyN3IcIa5EE8TELMZ618Q==:117 a=xqWC_Br6kY4A:10 a=IkcTkHD0fZMA:10 a=HzLeVaNsDn8A:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=yOCtJkima9RkubShWh1s:22 a=EUspDBNiAAAA:8 a=ZcM4VTEW66qICm1TBvkA:9 a=QEXdDO2ut3YA:10 a=bTQJ7kPSJx9SKPbeHEYW:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMjIzMDA5NCBTYWx0ZWRfXyO4wRmElutOa jCb3Zex/1cxeKC/XiZr7a2bTRRz5DhTgP8F58pCNny0ELEszjD5pFcQywX9MLGFinDVLUChZxVs 169IQtyrrv+hhB4+XIVZ3g96oZbQ/RpH/LnwQBEnxC3zKko6oIWaBS9gqKG3lVJFElsqjbxWRCL taymJCzX/HdmcZSVi423ScD+j7KC+4+gZiTn5Z4FFyPTuqYEpMYCyDtPxsLq5ZqKHbja2YShsa7 nGdhQyaWt/S3yn+nyZVen1I0JQznvGDveheGooam3rvsW0s5Byj7AT/h/3ot85vIXXa+e4Z/rUy WE84xSTkI3aaIhLGtfvDvOVrfNmALVfPMumOPlPT34gQl0YVcBsuS/3KRLeTcCp+JUN9Bs9VwmO Gt1y0BcEbA4p+QjAa4Zeulooxb/YpUWqAjRMLOOweo/W0QE3GJwa1hPe+4feqF8tuwx7Uu2Tl9V Wt99yU9La0Lwz/F1ALA== X-Proofpoint-ORIG-GUID: Q2TNlLvQHJlD28Q8WrSFy-D-cCLai1qW X-Proofpoint-GUID: Q2TNlLvQHJlD28Q8WrSFy-D-cCLai1qW X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-02-23_02,2026-02-20_04,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 phishscore=0 bulkscore=0 lowpriorityscore=0 priorityscore=1501 clxscore=1015 spamscore=0 malwarescore=0 adultscore=0 suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2602130000 definitions=main-2602230094 With the provider-owned data split out into a separate structure, we can now protect it with SRCU. Protect all dereferences of nvmem->impl with an SRCU read lock. Synchronize SRCU in nvmem_unregister() after setting the implementation pointer to NULL. This has the effect of numbing down the device after nvmem_unregister() returns - it will no longer accept any consumer calls and return -ENODEV. The actual device will live on for as long as there are references to it but we will no longer reach into the consumer's memory which may be gone by this time. The change has the added benefit of dropping the - now redundant - reference counting with kref. We are left with a single release() function depending on the kobject reference counting provided by struct device. Nvmem cell entries are destroyed in .release() now as they may be still dereferenced via the nvmem_cell handles after nvmem_release(). The actual calls will still go through SRCU and fail with -ENODEV if the provider is gone. Signed-off-by: Bartosz Golaszewski --- drivers/nvmem/core.c | 140 ++++++++++++++++++++++++++-------------------- drivers/nvmem/internals.h | 5 +- 2 files changed, 82 insertions(+), 63 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 9ff97330682975ef724b542fee6089fa7cd5414a..f80a1ec501d574cb0c3114c6341fd0f05d00a543 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -57,7 +56,12 @@ static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, void *val, size_t bytes) { - struct nvmem_impl *impl = nvmem->impl; + struct nvmem_impl *impl; + + guard(srcu)(&nvmem->srcu); + impl = rcu_dereference(nvmem->impl); + if (!impl) + return -ENODEV; if (!impl->reg_read) return -EOPNOTSUPP; @@ -68,9 +72,14 @@ static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, void *val, size_t bytes) { - struct nvmem_impl *impl = nvmem->impl; + struct nvmem_impl *impl; int ret, written; + guard(srcu)(&nvmem->srcu); + impl = rcu_dereference(nvmem->impl); + if (!impl) + return -ENODEV; + if (!impl->reg_write) return -EOPNOTSUPP; @@ -289,10 +298,14 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem) { - struct nvmem_impl *impl = nvmem->impl; - + struct nvmem_impl *impl; umode_t mode = 0400; + guard(srcu)(&nvmem->srcu); + impl = rcu_dereference(nvmem->impl); + if (!impl) + return 0; + if (!nvmem->root_only) mode |= 0044; @@ -333,7 +346,12 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj, { struct device *dev = kobj_to_dev(kobj); struct nvmem_device *nvmem = to_nvmem_device(dev); - struct nvmem_impl *impl = nvmem->impl; + struct nvmem_impl *impl; + + guard(srcu)(&nvmem->srcu); + impl = rcu_dereference(nvmem->impl); + if (!impl) + return 0; /* * If the device has no .reg_write operation, do not allow @@ -460,10 +478,9 @@ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, return 0; } -static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, - const struct nvmem_config *config) +static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem) { - if (config->compat) + if (nvmem->flags & FLAG_COMPAT) device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); } @@ -530,31 +547,12 @@ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, { return -ENOSYS; } -static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, - const struct nvmem_config *config) +static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem) { } #endif /* CONFIG_NVMEM_SYSFS */ -static void nvmem_release(struct device *dev) -{ - struct nvmem_device *nvmem = to_nvmem_device(dev); - - ida_free(&nvmem_ida, nvmem->id); - gpiod_put(nvmem->wp_gpio); - kfree(nvmem->impl); - kfree(nvmem); -} - -static const struct device_type nvmem_provider_type = { - .release = nvmem_release, -}; - -static const struct bus_type nvmem_bus_type = { - .name = "nvmem", -}; - static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell) { blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell); @@ -573,6 +571,26 @@ static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) nvmem_cell_entry_drop(cell); } +static void nvmem_release(struct device *dev) +{ + struct nvmem_device *nvmem = to_nvmem_device(dev); + + gpiod_put(nvmem->wp_gpio); + nvmem_sysfs_remove_compat(nvmem); + nvmem_device_remove_all_cells(nvmem); + ida_free(&nvmem_ida, nvmem->id); + cleanup_srcu_struct(&nvmem->srcu); + kfree(nvmem); +} + +static const struct device_type nvmem_provider_type = { + .release = nvmem_release, +}; + +static const struct bus_type nvmem_bus_type = { + .name = "nvmem", +}; + static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell) { scoped_guard(mutex, &nvmem_mutex) @@ -948,7 +966,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) goto err_put_device; } - kref_init(&nvmem->refcnt); INIT_LIST_HEAD(&nvmem->cells); nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info; @@ -956,7 +973,12 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) impl->reg_read = config->reg_read; impl->reg_write = config->reg_write; - nvmem->impl = impl; + rval = init_srcu_struct(&nvmem->srcu); + if (rval) + goto err_put_device; + + rcu_assign_pointer(nvmem->impl, impl); + nvmem->owner = config->owner; if (!nvmem->owner && config->dev->driver) nvmem->owner = config->dev->driver->owner; @@ -1011,24 +1033,24 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (config->cells) { rval = nvmem_add_cells(nvmem, config->cells, config->ncells); if (rval) - goto err_remove_cells; + goto err_put_device; } if (config->add_legacy_fixed_of_cells) { rval = nvmem_add_cells_from_legacy_of(nvmem); if (rval) - goto err_remove_cells; + goto err_put_device; } rval = nvmem_add_cells_from_fixed_layout(nvmem); if (rval) - goto err_remove_cells; + goto err_put_device; dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); rval = device_add(&nvmem->dev); if (rval) - goto err_remove_cells; + goto err_put_device; rval = nvmem_populate_layout(nvmem); if (rval) @@ -1050,33 +1072,14 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) #endif err_remove_dev: device_del(&nvmem->dev); -err_remove_cells: - nvmem_device_remove_all_cells(nvmem); - if (config->compat) - nvmem_sysfs_remove_compat(nvmem, config); err_put_device: put_device(&nvmem->dev); + kfree(impl); return ERR_PTR(rval); } EXPORT_SYMBOL_GPL(nvmem_register); -static void nvmem_device_release(struct kref *kref) -{ - struct nvmem_device *nvmem; - - nvmem = container_of(kref, struct nvmem_device, refcnt); - - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); - - if (nvmem->flags & FLAG_COMPAT) - device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); - - nvmem_device_remove_all_cells(nvmem); - nvmem_destroy_layout(nvmem); - device_unregister(&nvmem->dev); -} - /** * nvmem_unregister() - Unregister previously registered nvmem device * @@ -1084,8 +1087,17 @@ static void nvmem_device_release(struct kref *kref) */ void nvmem_unregister(struct nvmem_device *nvmem) { - if (nvmem) - kref_put(&nvmem->refcnt, nvmem_device_release); + struct nvmem_impl *impl; + + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); + + impl = rcu_replace_pointer(nvmem->impl, NULL, true); + synchronize_srcu(&nvmem->srcu); + + nvmem_destroy_layout(nvmem); + kfree(impl); + + device_unregister(&nvmem->dev); } EXPORT_SYMBOL_GPL(nvmem_unregister); @@ -1146,8 +1158,6 @@ static struct nvmem_device *__nvmem_device_get(void *data, return ERR_PTR(-EINVAL); } - kref_get(&nvmem->refcnt); - return nvmem; } @@ -1263,9 +1273,8 @@ EXPORT_SYMBOL_GPL(devm_nvmem_device_put); */ void nvmem_device_put(struct nvmem_device *nvmem) { - put_device(&nvmem->dev); module_put(nvmem->owner); - kref_put(&nvmem->refcnt, nvmem_device_release); + put_device(&nvmem->dev); } EXPORT_SYMBOL_GPL(nvmem_device_put); @@ -1652,6 +1661,15 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem, { int rc; + /* + * Take the SRCU read lock earlier. It will be taken again in + * nvmem_reg_read() but that's alright, they can be nested. If + * nvmem_reg_read() returns -ENODEV, we'll return right way. If it + * succeeds, we need to stay within the SRCU read-critical section + * until we're done calling cell->read_post_process(). + */ + guard(srcu)(&nvmem->srcu); + rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len); if (rc) diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index 05197074799ff3e2a6720f6552878a9e1354a5c3..5afb1297a93a38e399085391130c4df99f64af16 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -6,6 +6,7 @@ #include #include #include +#include /* * Holds data owned by the provider of the nvmem implementation. This goes @@ -20,11 +21,11 @@ struct nvmem_impl { struct nvmem_device { struct module *owner; struct device dev; - struct nvmem_impl *impl; + struct nvmem_impl __rcu *impl; + struct srcu_struct srcu; int stride; int word_size; int id; - struct kref refcnt; size_t size; bool read_only; bool root_only; -- 2.47.3