From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796133940; b=QH8bW+FfKOhoj9YZ2KjqJZWgfv5iDJV28xXmgHBIK6C8scimcCeozoNFrS3xwPMxjHSi +YfiFwv5TVv0f00iOy7NBWTmC2bGuWbK9dhoM2x7S4f96gdIqxCc8YjU23MBdBTfh5l8b VbuFUOG0oRfL8ZWqAsJye1r9dU9wpFSIt7ufdU9CBhas/ogFqZ/tii18dx8U/V0RsbcC4 tbXWfJwCC33yMym4PRQXXCcc8Kh7jg4c6qhA1Zbl2aB3fmSUjFqAI5UdGVzg5DjmYKoSH vBWQXv4AVznNhI8wbmzlSHTeAwbYpnHFRldO+/9g0w/meK2nPkbuSxIoXa3f946AXCQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796133940; h=DMARC-Filter:Received:DKIM-Signature:Received:Received: DKIM-Signature:X-Google-DKIM-Signature:X-Forwarded-Encrypted: X-Gm-Message-State:X-Gm-Gg:X-Received:X-Received:Received:From:Date: Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Proofpoint-ORIG-GUID:X-Proofpoint-GUID: X-Proofpoint-Spam-Details-Enc:X-Proofpoint-Spam-Info: X-Authority-Analysis:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=SeCSsT26+NolHOBKWLZM2krtQ0yerZH1GLA00cEEvVM=; b=nyxvKgGO91qxsXI9BJo+tC/kh5PjD/4VpSMh+Ee2lbGgEDpxjjHI4zgEVdr8LAoUOh6P RPzE2LCqHUfIl9vInzHAyjdJjtteU1tDw/hLBxZOlPrIrpFoboxcZybMKu9Kg7DCq01KV uiXghwy8YfNcELpScT34vXqwOECr4pQ6KAS+pNu6u7TNmVdYB6ry+RuI3na+GjBAaNo0H mDBRPNSab45l72r/EU1C9g0hp1HMBm44/e43lZP6MSCZd3AQr4PF5jINHb1PA1pnoybyI S6v62wRXMmCB/Ti5apYusJC1vC5zzy/qtBKE+Tl9CAGKnKoT/ni86UrMlZHRgW19q/g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=Pd1nUb2j; dkim=pass header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=SeN08pF2; arc=none smtp.remote-ip=205.220.168.131 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 398AC606A329 for ; Tue, 1 Dec 2026 14:05:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 398AC606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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.a=rsa-sha256 header.s=qcppdkim1 header.b=Pd1nUb2j; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=SeN08pF2 Received: from pps.filterd (m0279864.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B1D1alu006812 for ; Tue, 1 Dec 2026 14:05:39 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= SeCSsT26+NolHOBKWLZM2krtQ0yerZH1GLA00cEEvVM=; b=Pd1nUb2jY3M4iZ02 Gb4EojGpmUOHn7pyUsKI2lA4J+8xTASXqmtRUXBcGYJ5BcJxGE6RW1am7Kbxf3bH +yiuIsAZ702Axhhnm2M1eU00wiU3O3U7XwD9Nux2gzaM4jtWR9H4xr+uF8Z+uX5x c2DLc4h4O9bsdk9ZU7zFUBZtGaeKIlzHfxNPjwEoao+4nwoQZIKSGa/r5I6uqUy1 L26+tLf5qJUwlXGAL27ME1WJrzxz3gE5CZt9gbGhjrDe7fpolqovrM95lr29rLa6 HvqxmVgKw9eIS7PHvb/Nct11LMHYObggaqgWyvhoj7fEBXIOWom3jusMBgYLooqc c05UFw== Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.198]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4j9tng1e6f-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 01 Dec 2026 14:05:35 +0000 (GMT) Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-836caf085e0so150216377b3.1 for ; Tue, 01 Dec 2026 06:05:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1796133934; x=1796738734; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=SeCSsT26+NolHOBKWLZM2krtQ0yerZH1GLA00cEEvVM=; b=SeN08pF233JzeYvsDVBM8sT1Hl0PZ+5P+PUlVIpkX2Y8hyh0TShFpvrUG3AnlY7dQH 5pTZuN+Nc44vUxihghdSE3c2/+SCFOpFpIAKDuEpUFeueU2WA8o5jDpYG0WlqaVibngV ytAg7pwAxuv3t2YHDDSIbTpAXXBPJN13p+lL2P2SV/CEGK/AecUNQc54inZu+RyGdy6y XFQ5lQP7bfuSsXBtgqOLpkTxQhBJXm6Pz4xXE14fMvn21U9xvPZdFZ+lkS+IegUaffgB PEXMtQxJVAMhSAUnEMcekiFXj+A2SEyo+QEnKdcL7cUjax/n2vBErMdR7yQtN0iibNRp LdtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796133934; x=1796738734; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=SeCSsT26+NolHOBKWLZM2krtQ0yerZH1GLA00cEEvVM=; b=p448BqY/6vkBNeGwERoqTwdtkDT5hX60j9jC0Ep2y03ER3qi4kPEQbOBYCUrH7/ZcQ 4wgkqSCRLx9g8dPec3HMVlnd1Un48NUoqHXLn8/FU3qXJoZ1P9r3QhvBRDt5ip2Ao0Kr CXRTI1Mun5GBWurFJsw5oZaegaKE+B8oO15a7b5Jt9id0GxWrfLEFXqREuLvHiw54XbI oQqe3cQa9dBzz7HI0tN5KUNwGxAEa6cmw0juuEq0KF8FhhtK247YGKECbwgmIDSvam4Q +vjh2NQS5Mp8qnUhobGprKrXStQvJ8o90fD93tpU8GkGMZyrUoi1nhGmKU3szrrQ6cFw Q78A== X-Forwarded-Encrypted: i=1; AL+hErG84lyFP6u7PaYvha1rOsa/db5uyxFBcuaS8WybZ1VC2n6/rQ1prOSoyRtNZrQb0UQSfhxEI/bva2uzzPo=@vger.kernel.org X-Gm-Message-State: APuU4Z9Z62uFtvT0R6Y41rihWIjZuOLgbnXVXHTO/5EhQT+6X45eEUL5 rIBGJA/BPqp8AEm19xjKH9DkXEu73Bav/3R4xWebjE4rcf3LC+yhohQEoCrhhEXyB3l85pUF0/l w/fhJyh0B7BKZwo2C6KsI9zjzU1byBLfekRxboAH7eMR5mOkBmlKYHAJQwya6xIfDj5E= X-Gm-Gg: AZFdTZebDRDTt2WEtKaFX+1nPeOQOHSGVdqyEIZ88RV0lpH9f+Qvg12xYdUIa5HkwBx mWK8eo4tAFIHV5PliUR/uSz/Nobp54aQtPQMIQX6tC585N3/F8q0SuCyITf2hVj9k2fDyFq4qbQ YidkI+8nYZQagqT4QWAY9cG2aM3li6P6X+qX7Crm0Qc7ByOcCdXy6VUWorahd/r6S5tJCgLX973 EiHmm7X6fx7pudXIC8iHGlTGxRNdX/unedFNu2obiZwPEQyCoDj+dOhTN3QncoDLVqd+r2GIVSF UNZN0t01kdPw4R3JkvRZPgUvBOyeKm3n2V6NpK6YsEljNWGOC6CFtZylX+b/L1CPaMzTtqvH47c QPGMfXHBoqUy867njy4nie0QY06JnX5bquDiRR7+MN0na6no2+eYQHJ4hrBVAoJinTvlPGYuFYg 5hwuZBTAKH4S4sIiml47U/tj9sCnoXHtPiGRSGPaTANjk6jjiYHN4u6YfWD/AQXJaoE1iS7yt4K /SM2hUebA4Q4H3XBrdzu08FdZ8s X-Received: by 2002:a05:690e:484d:b0:67d:ea8d:e437 with SMTP id 956f58d0204a3-67df4442fe0mr10172412d50.73.1796133933505; Tue, 01 Dec 2026 06:05:33 -0800 (PST) X-Received: by 2002:a05:690e:484d:b0:67d:ea8d:e437 with SMTP id 956f58d0204a3-67df4442fe0mr10172304d50.73.1796133932572; Tue, 01 Dec 2026 06:05:32 -0800 (PST) Received: from QCOM-eG0v1AUPpu.na.qualcomm.com ([2a01:e0a:830:450:2022:3328:5af7:2b49]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bba6a0d65fsm538404065e9.3.2026.12.01.06.05.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 06:05:31 -0800 (PST) From: Loic Poulain Date: Tue, 01 Dec 2026 15:05:25 +0100 Subject: [PATCH v14 2/2] block: implement NVMEM provider 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261201-block-as-nvmem-v14-2-3a07f4ff246c@oss.qualcomm.com> References: <20261201-block-as-nvmem-v14-0-3a07f4ff246c@oss.qualcomm.com> In-Reply-To: <20261201-block-as-nvmem-v14-0-3a07f4ff246c@oss.qualcomm.com> To: Ulf Hansson , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Bjorn Andersson , Konrad Dybcio , Jens Axboe , Johannes Berg , Jeff Johnson , Bartosz Golaszewski , Marcel Holtmann , Luiz Augusto von Dentz , Balakrishna Godavarthi , Rocky Liao , "David S. Miller" , Jakub Kicinski , Paolo Abeni , Simon Horman , Srinivas Kandagatla , Andrew Lunn , Heiner Kallweit , Russell King , Saravana Kannan , Christian Marangi , Eric Dumazet , Abel Vesa , Eric Dumazet Cc: linux-mmc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-block@vger.kernel.org, linux-wireless@vger.kernel.org, ath10k@lists.infradead.org, linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, daniel@makrotopia.org, Loic Poulain , Bartosz Golaszewski , Christoph Hellwig X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: VPxn-UKkMeoTRnCmQdWydzBcjLeHqQDv X-Proofpoint-GUID: VPxn-UKkMeoTRnCmQdWydzBcjLeHqQDv X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAxMDA1NSBTYWx0ZWRfX/WWjKUEjEmoM Pzm/nMC0IP7wbbYkSyyadmte2a+XCWDoI2AeKMkri0qkkzZdTkcSb2+Yl7/CpxYK4VcwmgdZbP4 E3w4380uX7HPIJeB+XWxxu2mBOAn4UxdsRWW7jkxd/lDVYVpzcfx00DqiEDQxxgtJF+DyU+w+tn CZF9fIjyv9FKKKkZ1H2uZo0al5Ng7fVJ8ASsJxgcutjO5X5gilX/E0kd0MuKqr9oKMK+rrXzBX2 WsXKiQB8PgCp4+DXDSvoSqmDxCS9fqyKyHEgZ1UaFIH3uPpLkyyjfTVf2clqK+I8DPnM+aY/TEj fMBkuCQ45ep5UoPAEK87hufGrRhDJHTSLZ3msvkAcn+ndv/XuWhQDcN6ielN7xhxajc+PbpU1UV LOwMXrnOg/8AaOFuhG0ZNptvy0D0vfgLkcaRk5kg1jaJrWv658GABwdBpRgLBYJdmzJXeHsqWlS WOsaZ8NynlvJbx6aRPg== X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAxMDA1NSBTYWx0ZWRfX4ZGODVntAJ/f d8q/6+yc3sspT8E+auHEmaNNC3oqlTYFgqpcAxz7tbkbj7fzKe6k+4tYGfm93wLjokDziy8QvZJ Tdl/pDFiCJufZk4umJCEttCXUN/PzpI= X-Authority-Analysis: v=2.4 cv=O5ysLx9W c=1 sm=1 tr=0 ts=6b0ed42f cx=c_pps a=g1v0Z557R90hA0UpD/5Yag==:117 a=xqWC_Br6kY4A:10 a=IkcTkHD0fZMA:10 a=pUM-KoujPXoA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=DJpcGTmdVt4CTyJn9g5Z:22 a=EUspDBNiAAAA:8 a=VwQbUJbxAAAA:8 a=VT4XjZGOAAAA:8 a=PlhdpwwxGme58_HcCFAA:9 a=QEXdDO2ut3YA:10 a=MFSWADHSvvjO3QEy5MdX:22 a=6CpsfURP9XNmmWg3j1mJ:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-01_02,2026-12-01_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 malwarescore=0 phishscore=0 lowpriorityscore=0 bulkscore=0 priorityscore=1501 adultscore=0 impostorscore=0 spamscore=0 suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612010055 On embedded devices using an eMMC it is common that one or more partitions on the eMMC are used to store MAC addresses and Wi-Fi calibration EEPROM data. Allow referencing the partition in device tree for the kernel and Wi-Fi drivers accessing it via the NVMEM layer. NVMEM is registered for a block device whose OF node describes an NVMEM layout, either via an "nvmem-layout" child or by being a "fixed-layout" node itself (e.g. an eMMC boot partition associated through its mmc-card node). In the latter case the NVMEM core populates the layout device on the block device's own OF node, so that node ends up marked OF_POPULATED_BUS and shared with the layout device for as long as the block device exists. The NVMEM device is a read-only side channel onto a block device that stays fully usable, and it never takes ownership of it. Reads are served from the page cache of the block device, the same way the partition table itself is read (see read_part_sector()), so they are coherent with the other users of that same block device, but the data is only as stable as whatever owns the block device keeps it. Going through the page cache instead of opening the block device is what makes the lifetime of the NVMEM device work out. blk_nvmem_del() runs under disk->open_mutex (drop_partition()) and has to wait, through nvmem_unregister(), for the in-flight readers to drain before the NVMEM device is gone, as it is a child of the block device that is about to be deleted. A reader that opened the block device would take disk->open_mutex itself and deadlock against that wait. A read stuck on dead hardware instead delays the partition removal until the driver times out. Registering a partition happens under the same mutex, and synchronously probes the layout driver, which for all in-tree layouts is device tree parsing plus reads through the same page cache path. Exclusive openers are turned away with -EBUSY instead, a block device described as an NVMEM provider in the device tree is not a filesystem, md or dm member. Openers that do not ask for exclusive access (udev, blkid, dd, and provisioning tools that do not use O_EXCL) keep working and see the same data through the shared page cache. Consumers looking up a cell before the provider shows up get -EPROBE_DEFER from the NVMEM core and retry later. Reviewed-by: Bartosz Golaszewski Reviewed-by: Christoph Hellwig Signed-off-by: Loic Poulain --- MAINTAINERS | 1 + block/Kconfig | 11 +++++ block/Makefile | 1 + block/bdev.c | 7 +++ block/blk-nvmem.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++ block/blk.h | 18 +++++++ block/genhd.c | 4 ++ block/partitions/core.c | 5 ++ include/linux/blk_types.h | 4 ++ 9 files changed, 172 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index c00d3612c9acf4665bf89376877f1ca883cb1b2a..0527aff11c92291aca50a4e716eca0438a583b28 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20323,6 +20323,7 @@ S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/srini/nvmem.git F: Documentation/ABI/stable/sysfs-bus-nvmem F: Documentation/devicetree/bindings/nvmem/ +F: block/blk-nvmem.c F: drivers/nvmem/ F: include/dt-bindings/nvmem/ F: include/linux/nvmem-consumer.h diff --git a/block/Kconfig b/block/Kconfig index 70e4a66d941ff66ecb0ec34f4f7eff7fd1e4be40..4899ad572c71ffdbb62460d57623216254893ddc 100644 --- a/block/Kconfig +++ b/block/Kconfig @@ -209,6 +209,17 @@ config BLK_INLINE_ENCRYPTION_FALLBACK by falling back to the kernel crypto API when inline encryption hardware is not present. +config BLK_NVMEM + bool "Block device NVMEM provider" + depends on OF + depends on NVMEM + help + Allow block devices (or partitions) to act as NVMEM providers, + exposing factory-provisioned data such as MAC addresses or Wi-Fi + calibration blobs to the drivers that consume them. This is + typically used on embedded devices where such data is stored in a + dedicated area of an eMMC, instead of a separate EEPROM or OTP. + source "block/partitions/Kconfig" config BLK_PM diff --git a/block/Makefile b/block/Makefile index e7bd320e3d6971a8abf584f5de42776bb54e8216..826af86edce4e86eceb06c744e740abd0cab139f 100644 --- a/block/Makefile +++ b/block/Makefile @@ -39,3 +39,4 @@ obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o \ blk-crypto-sysfs.o obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o obj-$(CONFIG_BLOCK_HOLDER_DEPRECATED) += holder.o +obj-$(CONFIG_BLK_NVMEM) += blk-nvmem.o diff --git a/block/bdev.c b/block/bdev.c index e0a3dbb06837f0e82016cd531264e7d55cfddc23..730a42c6161afadb0cdcae6f9681f5f84afcfee5 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -976,6 +976,13 @@ int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder, int ret; if (holder) { + /* + * A block device providing NVMEM cells is not a filesystem, + * md or dm member and must not be claimed exclusively. + */ + if (bdev_is_nvmem(bdev)) + return -EBUSY; + mode |= BLK_OPEN_EXCL; ret = bd_prepare_to_claim(bdev, holder, hops); if (ret) diff --git a/block/blk-nvmem.c b/block/blk-nvmem.c new file mode 100644 index 0000000000000000000000000000000000000000..8d29fd338f0b070f30e70aabb80df8be4d4845a3 --- /dev/null +++ b/block/blk-nvmem.c @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * block device NVMEM provider + * + * Copyright (c) 2024 Daniel Golle + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. + * + * Useful on devices using a whole disk or a partition (e.g. an eMMC boot + * partition) to store MAC addresses, Bluetooth addresses or Wi-Fi + * calibration EEPROM data. + * + * The NVMEM device is a read-only side channel onto a block device that stays + * fully usable, and never takes ownership of it. Reads go through the page + * cache of the block device, so they are coherent with the other users of that + * same block device, but the data is only as stable as whatever owns the block + * device keeps it. + */ + +#include +#include +#include +#include +#include + +#include "blk.h" + +static int blk_nvmem_reg_read(void *priv, unsigned int from, + void *val, size_t bytes) +{ + struct block_device *bdev = priv; + struct address_space *mapping = bdev->bd_mapping; + loff_t pos = from; + + /* the device may have shrunk since the NVMEM device was registered */ + if (pos + bytes > bdev_nr_bytes(bdev)) + return -EIO; + + while (bytes) { + struct folio *folio; + size_t offset, len; + + folio = read_mapping_folio(mapping, pos >> PAGE_SHIFT, NULL); + if (IS_ERR(folio)) + return PTR_ERR(folio); + + offset = offset_in_folio(folio, pos); + len = min(bytes, folio_size(folio) - offset); + memcpy_from_folio(val, folio, offset, len); + folio_put(folio); + + val += len; + pos += len; + bytes -= len; + } + + return 0; +} + +void blk_nvmem_add(struct block_device *bdev) +{ + struct device *dev = &bdev->bd_device; + struct device_node *np = dev_of_node(dev); + loff_t size = bdev_nr_bytes(bdev); + struct nvmem_config config = { + .id = NVMEM_DEVID_NONE, + .owner = THIS_MODULE, + .word_size = 1, + .stride = 1, + .read_only = true, + .root_only = true, + .ignore_wp = true, + .dev = dev, + .name = dev_name(dev), + .of_node = np, + .priv = bdev, + .reg_read = blk_nvmem_reg_read, + }; + struct nvmem_device *nvmem; + struct device_node *child; + + /* skip devices which do not have a device tree node */ + if (!np) + return; + + /* + * Only register devices which the device tree describes a layout for, + * either in an "nvmem-layout" child node or on the node itself, as + * of_nvmem_layout_get_container() looks it up. + */ + child = of_get_child_by_name(np, "nvmem-layout"); + if (child) + of_node_put(child); + else if (!of_device_is_compatible(np, "fixed-layout")) + return; + + /* nvmem_config.size is an int, so reject anything that would overflow */ + if (size > INT_MAX) { + dev_warn(dev, "block device too large to be an NVMEM\n"); + return; + } + + config.size = size; + + nvmem = nvmem_register(&config); + if (IS_ERR(nvmem)) { + dev_err(dev, "Failed to register NVMEM device (%pe)\n", nvmem); + return; + } + + WRITE_ONCE(bdev->bd_nvmem, nvmem); +} + +/* + * Relies on GD_DEAD being set before, so that nvmem readers error out + * instead of waiting for the unfreeze. + */ +void blk_nvmem_del(struct block_device *bdev) +{ + nvmem_unregister(bdev->bd_nvmem); + WRITE_ONCE(bdev->bd_nvmem, NULL); +} diff --git a/block/blk.h b/block/blk.h index 105fff157bfbd453ae4cfa993e8971ceb36d916e..94f871c04665937160daa5643e3a15ebc4a14b91 100644 --- a/block/blk.h +++ b/block/blk.h @@ -839,4 +839,22 @@ static inline void blk_debugfs_unlock(struct request_queue *q, memalloc_noio_restore(memflags); } +#ifdef CONFIG_BLK_NVMEM +void blk_nvmem_add(struct block_device *bdev); +void blk_nvmem_del(struct block_device *bdev); + +static inline bool bdev_is_nvmem(struct block_device *bdev) +{ + return READ_ONCE(bdev->bd_nvmem); +} +#else +static inline void blk_nvmem_add(struct block_device *bdev) {} +static inline void blk_nvmem_del(struct block_device *bdev) {} + +static inline bool bdev_is_nvmem(struct block_device *bdev) +{ + return false; +} +#endif + #endif /* BLK_INTERNAL_H */ diff --git a/block/genhd.c b/block/genhd.c index 69204d44adae11eb8bccf5db8d5d5eaa0fa186a8..39adc6f5e44cf736161c67e63602249a32b42751 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -417,6 +417,8 @@ static void add_disk_final(struct gendisk *disk) */ dev_set_uevent_suppress(ddev, 0); disk_uevent(disk, KOBJ_ADD); + + blk_nvmem_add(disk->part0); } blk_apply_bdi_limits(disk->bdi, &disk->queue->limits); @@ -708,6 +710,8 @@ static void __del_gendisk(struct gendisk *disk) disk_del_events(disk); + blk_nvmem_del(disk->part0); + /* * Prevent new openers by unlinked the bdev inode. */ diff --git a/block/partitions/core.c b/block/partitions/core.c index eee954df1ac6ef7dd4f02ad5c9d49aed778bf015..c83227710584a6feb25c8abae3b319cfae9cce5b 100644 --- a/block/partitions/core.c +++ b/block/partitions/core.c @@ -289,6 +289,8 @@ void drop_partition(struct block_device *part) xa_erase(&part->bd_disk->part_tbl, bdev_partno(part)); kobject_put(part->bd_holder_dir); + blk_nvmem_del(part); + device_del(&part->bd_device); put_device(&part->bd_device); } @@ -406,6 +408,9 @@ static struct block_device *add_partition(struct gendisk *disk, int partno, /* suppress uevent if the disk suppresses it */ if (!dev_get_uevent_suppress(ddev)) kobject_uevent(&pdev->kobj, KOBJ_ADD); + + blk_nvmem_add(bdev); + return bdev; out_del: diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index 644b9528f2749f82df4d014adc0149dc42c986a8..6c442546623e31aae3d4e5c7e64b0b1836ef254b 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -15,6 +15,7 @@ struct bio_set; struct bio; struct bio_integrity_payload; +struct nvmem_device; struct page; struct io_context; struct cgroup_subsys_state; @@ -74,6 +75,9 @@ struct block_device { int bd_writers; #ifdef CONFIG_SECURITY void *bd_security; +#endif +#ifdef CONFIG_BLK_NVMEM + struct nvmem_device *bd_nvmem; #endif /* * keep this out-of-line as it's both big and not needed in the fast -- 2.34.1