Add a runtime device tree overlay interface for device need dts file.
With it its possible to use device tree overlays without having to use
a per-platform overlay manager.
Add a new device by command:
$ cat test.dtbo > /sys/class/spi_master/spi0/overlay_fdto
Remove the device by command:
$ echo remove > /sys/class/spi_master/spi0/overlay_fdto
Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
---
drivers/spi/Kconfig | 2 ++
drivers/spi/spi-mockup.c | 48 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+)
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index e0f0fa2746ad..4b7c84ddb367 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -1161,6 +1161,8 @@ config SPI_TLE62X0
config SPI_MOCKUP
tristate "SPI controller Testing Driver"
depends on OF
+ select OF_EARLY_FLATTREE
+ select OF_RESOLVE
select BPF_EVENTS
help
This enables SPI controller testing driver, which provides a way to
diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c
index 7a93b194ee53..404ad821bf6a 100644
--- a/drivers/spi/spi-mockup.c
+++ b/drivers/spi/spi-mockup.c
@@ -21,6 +21,9 @@
struct mockup_spi {
struct mutex lock;
struct spi_device *devs[MOCKUP_CHIPSELECT_MAX];
+
+ void *fdto;
+ int ovcs_id;
};
static struct spi_master *to_spi_master(struct device *dev)
@@ -145,9 +148,53 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
}
static DEVICE_ATTR_WO(delete_device);
+static ssize_t
+overlay_fdto_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct spi_master *master = to_spi_master(dev);
+ struct mockup_spi *mock = spi_master_get_devdata(master);
+ int ret;
+
+ mutex_lock(&mock->lock);
+
+ if (strncmp(buf, "remove\n", count) == 0) {
+ if (mock->ovcs_id < 0) {
+ ret = -ENOENT;
+ goto out_unlock;
+ }
+ of_overlay_remove(&mock->ovcs_id);
+ kfree(mock->fdto);
+ mock->ovcs_id = -1;
+ mock->fdto = NULL;
+ } else {
+ if (mock->ovcs_id >= 0) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
+ mock->fdto = kmemdup(buf, count, GFP_KERNEL);
+ if (!mock->fdto) {
+ ret = -ENOMEM;
+ goto out_unlock;
+ }
+ ret = of_overlay_fdt_apply(mock->fdto, count, &mock->ovcs_id);
+ if (ret < 0)
+ goto out_unlock;
+ }
+
+ mutex_unlock(&mock->lock);
+ return count;
+
+out_unlock:
+ mutex_unlock(&mock->lock);
+ return ret;
+}
+static DEVICE_ATTR_WO(overlay_fdto);
+
static struct attribute *spi_mockup_attrs[] = {
&dev_attr_new_device.attr,
&dev_attr_delete_device.attr,
+ &dev_attr_overlay_fdto.attr,
NULL
};
ATTRIBUTE_GROUPS(spi_mockup);
@@ -227,6 +274,7 @@ static int spi_mockup_probe(struct platform_device *pdev)
mock = spi_master_get_devdata(master);
mutex_init(&mock->lock);
+ mock->ovcs_id = -1;
ret = spi_register_master(master);
if (ret) {
--
2.34.1
On Fri, Aug 26, 2022 at 02:43:40PM +0000, Wei Yongjun wrote: > Add a runtime device tree overlay interface for device need dts file. > With it its possible to use device tree overlays without having to use > a per-platform overlay manager. Why would an entirely virtual device like this need to appear in DT? DT is supposed to be a hardware description and this clearly isn't hardware, nor is it something we're providing to a VM.
On 8/30/22 05:27, Mark Brown wrote: > On Fri, Aug 26, 2022 at 02:43:40PM +0000, Wei Yongjun wrote: > >> Add a runtime device tree overlay interface for device need dts file. >> With it its possible to use device tree overlays without having to use >> a per-platform overlay manager. > > Why would an entirely virtual device like this need to appear in > DT? DT is supposed to be a hardware description and this clearly > isn't hardware, nor is it something we're providing to a VM. Good point. Patch 3 seems to not be needed. -Frank
On 2022/8/31 3:24, Frank Rowand wrote: > On 8/30/22 05:27, Mark Brown wrote: >> On Fri, Aug 26, 2022 at 02:43:40PM +0000, Wei Yongjun wrote: >> >>> Add a runtime device tree overlay interface for device need dts file. >>> With it its possible to use device tree overlays without having to use >>> a per-platform overlay manager. >> >> Why would an entirely virtual device like this need to appear in >> DT? DT is supposed to be a hardware description and this clearly >> isn't hardware, nor is it something we're providing to a VM. > > Good point. Patch 3 seems to not be needed. Yes, will drop it in next version. Thanks, Wei Yongjun
comment inline below, plus adding to cc: and to: list
On 8/26/22 09:43, Wei Yongjun wrote:
> Add a runtime device tree overlay interface for device need dts file.
> With it its possible to use device tree overlays without having to use
> a per-platform overlay manager.
Why is an overlay needed? The documentation in patch 4 shows providing
a dtb as an argument to the qemu-system-x86_64 command, which should be
sufficient to supply the appropriate dtb.
-Frank
>
> Add a new device by command:
> $ cat test.dtbo > /sys/class/spi_master/spi0/overlay_fdto
>
> Remove the device by command:
> $ echo remove > /sys/class/spi_master/spi0/overlay_fdto
>
> Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
> ---
> drivers/spi/Kconfig | 2 ++
> drivers/spi/spi-mockup.c | 48 ++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 50 insertions(+)
>
> diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
> index e0f0fa2746ad..4b7c84ddb367 100644
> --- a/drivers/spi/Kconfig
> +++ b/drivers/spi/Kconfig
> @@ -1161,6 +1161,8 @@ config SPI_TLE62X0
> config SPI_MOCKUP
> tristate "SPI controller Testing Driver"
> depends on OF
> + select OF_EARLY_FLATTREE
> + select OF_RESOLVE
> select BPF_EVENTS
> help
> This enables SPI controller testing driver, which provides a way to
> diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c
> index 7a93b194ee53..404ad821bf6a 100644
> --- a/drivers/spi/spi-mockup.c
> +++ b/drivers/spi/spi-mockup.c
> @@ -21,6 +21,9 @@
> struct mockup_spi {
> struct mutex lock;
> struct spi_device *devs[MOCKUP_CHIPSELECT_MAX];
> +
> + void *fdto;
> + int ovcs_id;
> };
>
> static struct spi_master *to_spi_master(struct device *dev)
> @@ -145,9 +148,53 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
> }
> static DEVICE_ATTR_WO(delete_device);
>
> +static ssize_t
> +overlay_fdto_store(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct spi_master *master = to_spi_master(dev);
> + struct mockup_spi *mock = spi_master_get_devdata(master);
> + int ret;
> +
> + mutex_lock(&mock->lock);
> +
> + if (strncmp(buf, "remove\n", count) == 0) {
> + if (mock->ovcs_id < 0) {
> + ret = -ENOENT;
> + goto out_unlock;
> + }
> + of_overlay_remove(&mock->ovcs_id);
> + kfree(mock->fdto);
> + mock->ovcs_id = -1;
> + mock->fdto = NULL;
> + } else {
> + if (mock->ovcs_id >= 0) {
> + ret = -EINVAL;
> + goto out_unlock;
> + }
> + mock->fdto = kmemdup(buf, count, GFP_KERNEL);
> + if (!mock->fdto) {
> + ret = -ENOMEM;
> + goto out_unlock;
> + }
> + ret = of_overlay_fdt_apply(mock->fdto, count, &mock->ovcs_id);
> + if (ret < 0)
> + goto out_unlock;
> + }
> +
> + mutex_unlock(&mock->lock);
> + return count;
> +
> +out_unlock:
> + mutex_unlock(&mock->lock);
> + return ret;
> +}
> +static DEVICE_ATTR_WO(overlay_fdto);
> +
> static struct attribute *spi_mockup_attrs[] = {
> &dev_attr_new_device.attr,
> &dev_attr_delete_device.attr,
> + &dev_attr_overlay_fdto.attr,
> NULL
> };
> ATTRIBUTE_GROUPS(spi_mockup);
> @@ -227,6 +274,7 @@ static int spi_mockup_probe(struct platform_device *pdev)
>
> mock = spi_master_get_devdata(master);
> mutex_init(&mock->lock);
> + mock->ovcs_id = -1;
>
> ret = spi_register_master(master);
> if (ret) {
On 2022/8/30 5:29, Frank Rowand wrote:
> comment inline below, plus adding to cc: and to: list
>
> On 8/26/22 09:43, Wei Yongjun wrote:
>> Add a runtime device tree overlay interface for device need dts file.
>> With it its possible to use device tree overlays without having to use
>> a per-platform overlay manager.
>
> Why is an overlay needed? The documentation in patch 4 shows providing
> a dtb as an argument to the qemu-system-x86_64 command, which should be
> sufficient to supply the appropriate dtb.
Fist, we are aimed to build a device driver regression testsuit. Without
overlay, lots of the dtb fragments must be combined to a very big one, it
may different to maintainence. overlay makes easily to attach/detech as
the device's requirement.
Seconds, overlay dts may be used to found or verification issues related
to of_node_put or others, such as patches like:
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/?qt=grep&q=node_put
many of them may found by static check tool, which may leak of tesing.
The documentation I post does not use overlay dts, but when overlay
dts[1] is used, we can found the of node leak:
$ python3 -m kddv.cmds.mock -b spi -d mchp23k256
create spi device mchp23k256 success!
$ ls /sys/bus/spi/drivers/mchp23k256/spi0.0/mtd/
mtd0 mtd0ro
$ python3 -m kddv.cmds.mock -b spi -d mchp23k256 -r
[ 233.508565] Deleting MTD partitions on "spi0.0":
[ 233.516784] OF: ERROR: memory leak, expected refcount 1 instead of 2,
of_node_get()/of_node_put() unbalanced - destroy cset entry: attach
overlay node /spi/spi-sram@0
remove spi device mchp23k256 success!
Not sure it is worth to fix issues like this, but at least, we can
tesing them now.
-[1]---------------------------------------------------------
/dts-v1/;
/plugin/;
&{/spi} {
spi-sram@0 {
#address-cells = <1>;
#size-cells = <1>;
compatible = "microchip,mchp23k256";
reg = <0>;
spi-max-frequency = <20000000>;
};
};
-------------------------------------------------------
Regards,
Wei Yongjun
>
> -Frank
>
>>
>> Add a new device by command:
>> $ cat test.dtbo > /sys/class/spi_master/spi0/overlay_fdto
>>
>> Remove the device by command:
>> $ echo remove > /sys/class/spi_master/spi0/overlay_fdto
>>
>> Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
>> ---
>> drivers/spi/Kconfig | 2 ++
>> drivers/spi/spi-mockup.c | 48 ++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 50 insertions(+)
>>
>> diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
>> index e0f0fa2746ad..4b7c84ddb367 100644
>> --- a/drivers/spi/Kconfig
>> +++ b/drivers/spi/Kconfig
>> @@ -1161,6 +1161,8 @@ config SPI_TLE62X0
>> config SPI_MOCKUP
>> tristate "SPI controller Testing Driver"
>> depends on OF
>> + select OF_EARLY_FLATTREE
>> + select OF_RESOLVE
>> select BPF_EVENTS
>> help
>> This enables SPI controller testing driver, which provides a way to
>> diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c
>> index 7a93b194ee53..404ad821bf6a 100644
>> --- a/drivers/spi/spi-mockup.c
>> +++ b/drivers/spi/spi-mockup.c
>> @@ -21,6 +21,9 @@
>> struct mockup_spi {
>> struct mutex lock;
>> struct spi_device *devs[MOCKUP_CHIPSELECT_MAX];
>> +
>> + void *fdto;
>> + int ovcs_id;
>> };
>>
>> static struct spi_master *to_spi_master(struct device *dev)
>> @@ -145,9 +148,53 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
>> }
>> static DEVICE_ATTR_WO(delete_device);
>>
>> +static ssize_t
>> +overlay_fdto_store(struct device *dev, struct device_attribute *attr,
>> + const char *buf, size_t count)
>> +{
>> + struct spi_master *master = to_spi_master(dev);
>> + struct mockup_spi *mock = spi_master_get_devdata(master);
>> + int ret;
>> +
>> + mutex_lock(&mock->lock);
>> +
>> + if (strncmp(buf, "remove\n", count) == 0) {
>> + if (mock->ovcs_id < 0) {
>> + ret = -ENOENT;
>> + goto out_unlock;
>> + }
>> + of_overlay_remove(&mock->ovcs_id);
>> + kfree(mock->fdto);
>> + mock->ovcs_id = -1;
>> + mock->fdto = NULL;
>> + } else {
>> + if (mock->ovcs_id >= 0) {
>> + ret = -EINVAL;
>> + goto out_unlock;
>> + }
>> + mock->fdto = kmemdup(buf, count, GFP_KERNEL);
>> + if (!mock->fdto) {
>> + ret = -ENOMEM;
>> + goto out_unlock;
>> + }
>> + ret = of_overlay_fdt_apply(mock->fdto, count, &mock->ovcs_id);
>> + if (ret < 0)
>> + goto out_unlock;
>> + }
>> +
>> + mutex_unlock(&mock->lock);
>> + return count;
>> +
>> +out_unlock:
>> + mutex_unlock(&mock->lock);
>> + return ret;
>> +}
>> +static DEVICE_ATTR_WO(overlay_fdto);
>> +
>> static struct attribute *spi_mockup_attrs[] = {
>> &dev_attr_new_device.attr,
>> &dev_attr_delete_device.attr,
>> + &dev_attr_overlay_fdto.attr,
>> NULL
>> };
>> ATTRIBUTE_GROUPS(spi_mockup);
>> @@ -227,6 +274,7 @@ static int spi_mockup_probe(struct platform_device *pdev)
>>
>> mock = spi_master_get_devdata(master);
>> mutex_init(&mock->lock);
>> + mock->ovcs_id = -1;
>>
>> ret = spi_register_master(master);
>> if (ret) {
>
© 2016 - 2026 Red Hat, Inc.