2021-02-06 03:57:22

by Matteo Croce

[permalink] [raw]
Subject: [PATCH 0/5] block: add a sequence number to disks

From: Matteo Croce <[email protected]>

With this series a monotonically increasing number is added to disks,
precisely in the genhd struct, and it's exported in sysfs and uevent.

This helps the userspace correlate events for devices that reuse the
same device, like loop.

The first patch is the core one, the 2..4 expose the information in
different ways, while the last one increase the sequence number for
loop devices at every attach.

# udevadm monitor -kp |grep -e ^DEVNAME -e ^DISKSEQ &
[1] 523
# losetup -fP 3part
[ 3698.615848] loop0: detected capacity change from 16384 to 0
DEVNAME=/dev/loop0
DISKSEQ=13
[ 3698.647189] loop0: p1 p2 p3
DEVNAME=/dev/loop0
DISKSEQ=13
DEVNAME=/dev/loop0p1
DISKSEQ=13
DEVNAME=/dev/loop0p2
DISKSEQ=13
DEVNAME=/dev/loop0p3
DISKSEQ=13
# losetup -fP 2part
[ 3705.170766] loop1: detected capacity change from 40960 to 0
DEVNAME=/dev/loop1
DISKSEQ=14
[ 3705.247280] loop1: p1 p2
DEVNAME=/dev/loop1
DISKSEQ=14
DEVNAME=/dev/loop1p1
DISKSEQ=14
DEVNAME=/dev/loop1p2
DISKSEQ=14
# ./getdiskseq /dev/loop*
/dev/loop0: 13
/dev/loop0p1: 13
/dev/loop0p2: 13
/dev/loop0p3: 13
/dev/loop1: 14
/dev/loop1p1: 14
/dev/loop1p2: 14
/dev/loop2: 5
/dev/loop3: 6
/dev/loop-control: Function not implemented
# grep . /sys/class/block/*/diskseq
/sys/class/block/loop0/diskseq:13
/sys/class/block/loop1/diskseq:14
/sys/class/block/loop2/diskseq:5
/sys/class/block/loop3/diskseq:6
/sys/class/block/ram0/diskseq:1
/sys/class/block/ram1/diskseq:2
/sys/class/block/vda/diskseq:7

If merged, this feature will immediately used by the userspace:
https://github.com/systemd/systemd/issues/17469#issuecomment-762919781

Matteo Croce (5):
block: add disk sequence number
block: add ioctl to read the disk sequence number
block: refactor sysfs code
block: export diskseq in sysfs
loop: increment sequence number

Documentation/ABI/testing/sysfs-block | 12 ++++++++
block/genhd.c | 43 ++++++++++++++++++++++++---
block/ioctl.c | 2 ++
drivers/block/loop.c | 3 ++
include/linux/genhd.h | 2 ++
include/uapi/linux/fs.h | 1 +
6 files changed, 59 insertions(+), 4 deletions(-)

--
2.29.2


2021-02-06 04:12:40

by Matteo Croce

[permalink] [raw]
Subject: [PATCH 4/5] block: export diskseq in sysfs

From: Matteo Croce <[email protected]>

Add a new sysfs handle to export the new diskseq value.
Place it in <sysfs>/block/<disk>/diskseq and document it.

Signed-off-by: Matteo Croce <[email protected]>
---
Documentation/ABI/testing/sysfs-block | 12 ++++++++++++
block/genhd.c | 11 +++++++++++
2 files changed, 23 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-block b/Documentation/ABI/testing/sysfs-block
index e34cdeeeb9d4..a0ed87386639 100644
--- a/Documentation/ABI/testing/sysfs-block
+++ b/Documentation/ABI/testing/sysfs-block
@@ -28,6 +28,18 @@ Description:
For more details refer Documentation/admin-guide/iostats.rst


+What: /sys/block/<disk>/diskseq
+Date: February 2021
+Contact: Matteo Croce <[email protected]>
+Description:
+ The /sys/block/<disk>/diskseq files reports the disk
+ sequence number, which is a monotonically increasing
+ number assigned to every drive.
+ Some devices, like the loop device, refresh such number
+ every time the backing file is changed.
+ The value type is 64 bit unsigned.
+
+
What: /sys/block/<disk>/<part>/stat
Date: February 2008
Contact: Jerome Marchand <[email protected]>
diff --git a/block/genhd.c b/block/genhd.c
index a59a35cf452c..1aedd4fab6f3 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -1975,6 +1975,7 @@ static void disk_check_events(struct disk_events *ev,
* events_async : list of events which can be detected w/o polling
* (always empty, only for backwards compatibility)
* events_poll_msecs : polling interval, 0: disable, -1: system default
+ * diskseq : disk sequence number, since boot
*/
static ssize_t __disk_events_show(unsigned int events, char *buf)
{
@@ -2045,16 +2046,26 @@ static ssize_t disk_events_poll_msecs_store(struct device *dev,
return count;
}

+static ssize_t diskseq_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct gendisk *disk = dev_to_disk(dev);
+
+ return sprintf(buf, "%llu\n", disk->diskseq);
+}
+
static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
static const DEVICE_ATTR(events_poll_msecs, 0644,
disk_events_poll_msecs_show,
disk_events_poll_msecs_store);
+static const DEVICE_ATTR(diskseq, 0444, diskseq_show, NULL);

static const struct attribute *disk_sysfs_attrs[] = {
&dev_attr_events.attr,
&dev_attr_events_async.attr,
&dev_attr_events_poll_msecs.attr,
+ &dev_attr_diskseq.attr,
NULL,
};

--
2.29.2

2021-02-08 21:43:01

by Lennart Poettering

[permalink] [raw]
Subject: Re: [PATCH 0/5] block: add a sequence number to disks

On Sa, 06.02.21 01:08, Matteo Croce ([email protected]) wrote:

> From: Matteo Croce <[email protected]>
>
> With this series a monotonically increasing number is added to disks,
> precisely in the genhd struct, and it's exported in sysfs and uevent.
>
> This helps the userspace correlate events for devices that reuse the
> same device, like loop.
>
> The first patch is the core one, the 2..4 expose the information in
> different ways, while the last one increase the sequence number for
> loop devices at every attach.

Patch set looks excellent to me. This would be great to have for the
systems project, as it would allow us to fix some major races around
loop device allocation, that are relatively easily triggered on loaded
systems.

Lennart

2021-02-23 15:51:54

by Matteo Croce

[permalink] [raw]
Subject: Re: [PATCH 0/5] block: add a sequence number to disks

On Sat, Feb 6, 2021 at 1:09 AM Matteo Croce <[email protected]> wrote:
>
> From: Matteo Croce <[email protected]>
>
> With this series a monotonically increasing number is added to disks,
> precisely in the genhd struct, and it's exported in sysfs and uevent.
>
> This helps the userspace correlate events for devices that reuse the
> same device, like loop.
>
> The first patch is the core one, the 2..4 expose the information in
> different ways, while the last one increase the sequence number for
> loop devices at every attach.
>
> # udevadm monitor -kp |grep -e ^DEVNAME -e ^DISKSEQ &
> [1] 523
> # losetup -fP 3part
> [ 3698.615848] loop0: detected capacity change from 16384 to 0
> DEVNAME=/dev/loop0
> DISKSEQ=13
> [ 3698.647189] loop0: p1 p2 p3
> DEVNAME=/dev/loop0
> DISKSEQ=13
> DEVNAME=/dev/loop0p1
> DISKSEQ=13
> DEVNAME=/dev/loop0p2
> DISKSEQ=13
> DEVNAME=/dev/loop0p3
> DISKSEQ=13
> # losetup -fP 2part
> [ 3705.170766] loop1: detected capacity change from 40960 to 0
> DEVNAME=/dev/loop1
> DISKSEQ=14
> [ 3705.247280] loop1: p1 p2
> DEVNAME=/dev/loop1
> DISKSEQ=14
> DEVNAME=/dev/loop1p1
> DISKSEQ=14
> DEVNAME=/dev/loop1p2
> DISKSEQ=14
> # ./getdiskseq /dev/loop*
> /dev/loop0: 13
> /dev/loop0p1: 13
> /dev/loop0p2: 13
> /dev/loop0p3: 13
> /dev/loop1: 14
> /dev/loop1p1: 14
> /dev/loop1p2: 14
> /dev/loop2: 5
> /dev/loop3: 6
> /dev/loop-control: Function not implemented
> # grep . /sys/class/block/*/diskseq
> /sys/class/block/loop0/diskseq:13
> /sys/class/block/loop1/diskseq:14
> /sys/class/block/loop2/diskseq:5
> /sys/class/block/loop3/diskseq:6
> /sys/class/block/ram0/diskseq:1
> /sys/class/block/ram1/diskseq:2
> /sys/class/block/vda/diskseq:7
>
> If merged, this feature will immediately used by the userspace:
> https://github.com/systemd/systemd/issues/17469#issuecomment-762919781
>
> Matteo Croce (5):
> block: add disk sequence number
> block: add ioctl to read the disk sequence number
> block: refactor sysfs code
> block: export diskseq in sysfs
> loop: increment sequence number
>
> Documentation/ABI/testing/sysfs-block | 12 ++++++++
> block/genhd.c | 43 ++++++++++++++++++++++++---
> block/ioctl.c | 2 ++
> drivers/block/loop.c | 3 ++
> include/linux/genhd.h | 2 ++
> include/uapi/linux/fs.h | 1 +
> 6 files changed, 59 insertions(+), 4 deletions(-)
>
> --
> 2.29.2
>

Hi,

Did anyone have a chance to look at this series?

Ideas or suggestions?

Regards,


--
per aspera ad upstream