Re: [PATCH] io_uring: extract the function that checks the legitimacy of sq/cq entries

From: Jens Axboe
Date: Thu Mar 14 2024 - 15:29:30 EST


On 3/12/24 1:44 PM, Xin Wang wrote:
> In the io_uring_create function, the sq_entries and cq_entries passed
> in by the user are examined. The checking logic is the same for both, so
> the common code can be extracted for reuse.

Looks fine to me, though not sure how helpful it really is, it's not
like it's a lot of code and it's easy enough to read as it is. However,
a few minor comments:

> O_RDWR | O_CLOEXEC, NULL);
> }
>
> +static bool io_validate_entries(unsigned int *entries, unsigned int max_entries, __u32 flags)

Line too long, please break list other functions. Also needs a better
name, probably io_validate_ring_entries() would be better.

> +{
> + if (!(*entries))
> + return false;
> + if (*entries > max_entries) {
> + if (!(flags & IORING_SETUP_CLAMP))
> + return false;
> + *entries = max_entries;
> + }
> + return true;
> +}

And I don't know why you use parens for the first *entries check, but
then not for the next? Should be consistent, at least.

> @@ -3854,13 +3861,8 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
> * to a power-of-two, if it isn't already. We do NOT impose
> * any cq vs sq ring sizing.
> */
> - if (!p->cq_entries)
> + if (!io_validate_entries(&(p->cq_entries), IORING_MAX_CQ_ENTRIES, p->flags))

Again not sure what these parens are doing here?

--
Jens Axboe