block: add IOC_PR_READ_KEYS and IOC_PR_READ_RESERVATION ioctls
Subsystems (3)
v3 cover block: add IOC_PR_READ_KEYS and IOC_PR_READ_RESERVATION ioctls
v3:
- Use checked_mul_overflow(), struct_size(), etc to avoid duplicating size calculations [Christoph]
- Don't use __free() from cleanup.h [Christoph, Krzysztof]
- Drop one-time use num_copy_keys local variable [Christoph]
- Rename inout local variable to read_keys [Christoph]
v2:
- Fix num_keys validation in patches 1-3 [Hannes]
- Declare local variables at beginning of scope [Hannes]
This series exposes struct pr_ops pr_read_keys() and pr_read_reservations() to userspace as ioctls, making it possible to list registered reservation keys and report the current reservation on a block device.
The new ioctls are needed by applications or cluster managers that rely on inspecting the PR state. This is something that has been possible with SCSI- and NVME-specific commands but not with the PR ioctls. I hope to move QEMU from SG_IO to PR ioctls so that NVMe host block devices can be supported alongside SCSI devices without protocol-specific commands.
These ioctls will also make troubleshooting possible with the blkpr(8) util-linux tool, for which I have prepared a separate patch series.
Stefan Hajnoczi (4):
scsi: sd: reject invalid pr_read_keys() num_keys values
nvme: reject invalid pr_read_keys() num_keys values
block: add IOC_PR_READ_KEYS ioctl
block: add IOC_PR_READ_RESERVATION ioctl
include/uapi/linux/pr.h | 14 +++++++ block/ioctl.c | 84 +++++++++++++++++++++++++++++++++++++++++ drivers/nvme/host/pr.c | 6 ++- drivers/scsi/sd.c | 12 +++++- 4 files changed, 114 insertions(+), 2 deletions(-)
Signature · 1 line
2.52.0
PATCH v3 1/4 scsi: sd: reject invalid pr_read_keys() num_keys values
The pr_read_keys() interface has a u32 num_keys parameter. The SCSI PERSISTENT RESERVE IN command has a maximum READ KEYS service action size of 65536 bytes. Reject num_keys values that are too large to fit into the SCSI command.
This will become important when pr_read_keys() is exposed to untrusted userspace via an <linux/pr.h> ioctl.
--- drivers/scsi/sd.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/scsi/sd.c +11 -1
@@ -1974,9 +1974,19 @@ static int sd_pr_read_keys(struct block_device *bdev, struct pr_keys *keys_info) {int result, i, data_offset, num_copy_keys;u32 num_keys = keys_info->num_keys; -int data_len = num_keys *8 +8 ; +int data_len;u8 *data; +/* +* Each reservation key takes 8 bytes and there is an 8-byte header +* before the reservation key list. The total size must fit into the +* 16-bit ALLOCATION LENGTH field. +*/ +if (check_mul_overflow (num_keys,8 , &data_len) || +check_add_overflow (data_len,8 , &data_len) || + data_len >USHRT_MAX ) +return -EINVAL ; + data =kzalloc (data_len,GFP_KERNEL );if (!data)return -ENOMEM ;
Signature · 1 line
2.52.0
Re: [PATCH v3 1/4] scsi: sd: reject invalid pr_read_keys() num_keys values
drivers/scsi/sd.c static int sd_pr_read_keys(struct block_device *bdev, struct pr_keys *keys_info) · line 1986
Quoted 7 lines · Stefan Hajnoczi
drivers/scsi/sd.c +7
-0+/* +* Each reservation key takes 8 bytes and there is an 8-byte header +* before the reservation key list. The total size must fit into the +* 16-bit ALLOCATION LENGTH field. +*/ + if (check_mul_overflow(num_keys, 8, &data_len) || + check_add_overflow(data_len, 8, &data_len) ||
Using data_len for the throw away key size is a little confusing, but then again I guess compared to all the surrounding code that's harmless :)
So:
Re: [PATCH v3 1/4] scsi: sd: reject invalid pr_read_keys() num_keys values
In reply to Stefan HajnocziQuoted 5 lines · Stefan Hajnoczi
The
pr_read_keys()interface has a u32num_keysparameter. The SCSI PERSISTENT RESERVE IN command has a maximum READ KEYS service action size of 65536 bytes. Rejectnum_keysvalues that are too large to fit into the SCSI command.
Quoted 8 lines
This will become important when
pr_read_keys()is exposed to untrusted userspace via an <linux/pr.h> ioctl.Signed-off-by Stefan Hajnoczi <[email protected]>--- drivers/scsi/sd.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
PATCH v3 2/4 nvme: reject invalid pr_read_keys() num_keys values
In reply to Stefan HajnocziThe pr_read_keys() interface has a u32 num_keys parameter. The NVMe Reservation Report command has a u32 maximum length. Reject num_keys values that are too large to fit.
This will become important when pr_read_keys() is exposed to untrusted userspace via an <linux/pr.h> ioctl.
--- drivers/nvme/host/pr.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/nvme/host/pr.c +5 -1
@@ -228,7 +228,8 @@ static int nvme_pr_resv_report(struct block_device *bdev, void *data,static int nvme_pr_read_keys (struct block_device *bdev,struct pr_keys *keys_info) { -u32 rse_len, num_keys = keys_info->num_keys; +size_t rse_len; +u32 num_keys = keys_info->num_keys;struct nvme_reservation_status_ext *rse;int ret, i;bool eds;
@@ -238,6 +239,9 @@ static int nvme_pr_read_keys(struct block_device *bdev,* enough to get enough keys to fill the return keys buffer. */ rse_len =struct_size (rse, regctl_eds, num_keys); +if (rse_len >U32_MAX ) +return -EINVAL ; + rse =kzalloc (rse_len,GFP_KERNEL );if (!rse)return -ENOMEM ;
Signature · 1 line
2.52.0
Re: [PATCH v3 2/4] nvme: reject invalid pr_read_keys() num_keys values
Looks good:
Re: [PATCH v3 2/4] nvme: reject invalid pr_read_keys() num_keys values
In reply to Stefan HajnocziQuoted 4 lines · Stefan Hajnoczi
The
pr_read_keys()interface has a u32num_keysparameter. The NVMe Reservation Report command has a u32 maximum length. Rejectnum_keysvalues that are too large to fit.
Quoted 8 lines
This will become important when
pr_read_keys()is exposed to untrusted userspace via an <linux/pr.h> ioctl.Signed-off-by Stefan Hajnoczi <[email protected]>--- drivers/nvme/host/pr.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
PATCH v3 3/4 block: add IOC_PR_READ_KEYS ioctl
In reply to Stefan HajnocziAdd a Persistent Reservations ioctl to read the list of currently registered reservation keys. This calls the pr_ops->read_keys() function that was previously added in commit c787f1baa503 ("block: Add PR callouts for read keys and reservation") but was only used by the in-kernel SCSI target so far.
The IOC_PR_READ_KEYS ioctl is necessary so that userspace applications that rely on Persistent Reservations ioctls have a way of inspecting the current state. Cluster managers and validation tests need this functionality.
--- include/uapi/linux/pr.h | 7 ++++++ block/ioctl.c | 56 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+)
include/uapi/linux/pr.h +7 -0
@@ -56,6 +56,12 @@ struct pr_clear {__u32 __pad; }; +struct pr_read_keys { +__u32 generation; +__u32 num_keys; +__u64 keys_ptr; +}; +#define PR_FL_IGNORE_KEY (1 << 0)/* ignore existing key */ #define IOC_PR_REGISTER _IOW('p', 200, struct pr_registration)
@@ -64,5 +70,6 @@ struct pr_clear {#define IOC_PR_PREEMPT _IOW('p', 203, struct pr_preempt)#define IOC_PR_PREEMPT_ABORT _IOW('p', 204, struct pr_preempt)#define IOC_PR_CLEAR _IOW('p', 205, struct pr_clear) +#define IOC_PR_READ_KEYS _IOWR('p', 206, struct pr_read_keys)#endif /* _UAPI_PR_H */
block/ioctl.c +56 -0
68 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v3 3/4] block: add IOC_PR_READ_KEYS ioctl
Looks good:
PATCH v3 4/4 block: add IOC_PR_READ_RESERVATION ioctl
In reply to Stefan HajnocziAdd a Persistent Reservations ioctl to read the current reservation. This calls the pr_ops->read_reservation() function that was previously added in commit c787f1baa503 ("block: Add PR callouts for read keys and reservation") but was only used by the in-kernel SCSI target so far.
The IOC_PR_READ_RESERVATION ioctl is necessary so that userspace applications that rely on Persistent Reservations ioctls have a way of inspecting the current state. Cluster managers and validation tests need this functionality.
--- include/uapi/linux/pr.h | 7 +++++++ block/ioctl.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+)
include/uapi/linux/pr.h +7 -0
@@ -62,6 +62,12 @@ struct pr_read_keys {__u64 keys_ptr; }; +struct pr_read_reservation { +__u64 key; +__u32 generation; +__u32 type; +}; +#define PR_FL_IGNORE_KEY (1 << 0)/* ignore existing key */ #define IOC_PR_REGISTER _IOW('p', 200, struct pr_registration)
@@ -71,5 +77,6 @@ struct pr_read_keys {#define IOC_PR_PREEMPT_ABORT _IOW('p', 204, struct pr_preempt)#define IOC_PR_CLEAR _IOW('p', 205, struct pr_clear)#define IOC_PR_READ_KEYS _IOWR('p', 206, struct pr_read_keys) +#define IOC_PR_READ_RESERVATION _IOR('p', 207, struct pr_read_reservation)#endif /* _UAPI_PR_H */
block/ioctl.c +28 -0
@@ -477,6 +477,32 @@ static int blkdev_pr_read_keys(struct block_device *bdev, blk_mode_t mode,return ret; } +static int blkdev_pr_read_reservation (struct block_device *bdev, +blk_mode_t mode,struct pr_read_reservation __user *arg) +{ +const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; +struct pr_held_reservation rsv = {}; +struct pr_read_reservation out = {}; +int ret; + +if (!blkdev_pr_allowed (bdev, mode)) +return -EPERM ; +if (!ops || !ops->pr_read_reservation) +return -EOPNOTSUPP ; + + ret = ops->pr_read_reservation (bdev, &rsv); +if (ret) +return ret; + + out.key = rsv.key; + out.generation = rsv.generation; + out.type = rsv.type; + +if (copy_to_user (arg, &out,sizeof (out))) +return -EFAULT ; +return 0 ; +} +static int blkdev_flushbuf (struct block_device *bdev,unsigned cmd,unsigned long arg) {
@@ -700,6 +726,8 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,return blkdev_pr_clear (bdev, mode, argp);case IOC_PR_READ_KEYS :return blkdev_pr_read_keys (bdev, mode, argp); +case IOC_PR_READ_RESERVATION : +return blkdev_pr_read_reservation (bdev, mode, argp);default :return blk_get_meta_cap (bdev, cmd, argp); }
Signature · 1 line
2.52.0
Stefan,
Quoted 4 lines
This series exposes struct
pr_opspr_read_keys()andpr_read_reservations()to userspace as ioctls, making it possible to list registered reservation keys and report the current reservation on a block device.
Looks OK to me.
Signature · 1 line
Martin K. Petersen
Quoted 11 lines · Stefan Hajnoczi
v3:
- Usechecked_mul_overflow(),struct_size(), etc to avoid duplicating size calculations [Christoph]
- Don't use__free()fromcleanup.h[Christoph, Krzysztof]
- Drop one-time usenum_copy_keyslocal variable [Christoph]
- Rename inout local variable toread_keys[Christoph]v2:
- Fixnum_keysvalidation in patches 1-3 [Hannes]
- Declare local variables at beginning of scope [Hannes][...]
Applied, thanks!
[1/4] scsi: sd: reject invalid pr_read_keys() num_keys values
commit: d832d9366b072e76b94344c0532b7067536b3ef9
[2/4] nvme: reject invalid pr_read_keys() num_keys values
commit: d7d07c1995913f23fe6140fd8d7323c8b923680a
[3/4] block: add IOC_PR_READ_KEYS ioctl
commit: 51f31451b34d1c5d8f16d1dc6ef481d0b49441ee
[4/4] block: add IOC_PR_READ_RESERVATION ioctl
commit: e78d75d1fa447ce2b66799f1ccdcee61a4951a79Best regards,
Signature · 1 line
Jens Axboe