Re: [PATCH v2 3/4] block: add IOC_PR_READ_KEYS ioctl

Stefan Hajnoczi
2025-12-01T16:33:17Z

[-- Attachment #1: Type: text/plain, Size: 1707 bytes --]

On Mon, Dec 01, 2025 at 07:40:16AM +0100, Christoph Hellwig wrote:
> On Thu, Nov 27, 2025 at 10:54:23AM -0500, Stefan Hajnoczi wrote:
> > +static int blkdev_pr_read_keys(struct block_device *bdev, blk_mode_t mode,
> > + struct pr_read_keys __user *arg)
> > +{
> > + const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
> > + struct pr_keys *keys_info __free(kfree) = NULL;
>
> Please avoid the use of the __free mess and write readable and maintainable
> code instead.

Okay.

> > + struct pr_read_keys inout;
>
> Inout is not a very good variable name, as it doesn't really have much
> of meaning.

It's the ioctl argument. I will change it to read_keys in the next
revision. I'm not sure if that's any better, but it reminds us which
struct this is.

> > + if (copy_from_user(&inout, arg, sizeof(inout)))
> > + return -EFAULT;
> > +
> > + /*
> > + * 64-bit hosts could handle more keys than 32-bit hosts, but this
> > + * limit is more than enough in practice.
> > + */
> > + if (inout.num_keys > (U32_MAX - sizeof(*keys_info)) /
> > + sizeof(keys_info->keys[0]))
> > + return -EINVAL;
> > +
> > + keys_info_len = struct_size(keys_info, keys, inout.num_keys);
>
> Do the size check on the calculate len here?

Yes, that's better. Checking SIZE_MAX also gets rid of the 32-bit vs
64-bit host comment.

> > + return ret;
> > +
> > + /* Copy out individual keys */
> > + keys_ptr = u64_to_user_ptr(inout.keys_ptr);
> > + num_copy_keys = min(inout.num_keys, keys_info->num_keys);
> > + keys_copy_len = num_copy_keys * sizeof(keys_info->keys[0]);
>
> num_copy_keys is only used once, so maybe drop it?

Will fix.

Stefan

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 484 bytes --]