From: Karolina Stolarek <karolina.stolarek@intel.com>
To: "Bernatowicz, Marcin" <marcin.bernatowicz@linux.intel.com>
Cc: igt-dev@lists.freedesktop.org
Subject: Re: [igt-dev] [PATCH i-g-t] lib/intel_blt.c: ensure uint64_t result of multiplication
Date: Fri, 20 Oct 2023 08:58:26 +0200 [thread overview]
Message-ID: <34cfc694-8196-27c1-91f9-ab310af4ca31@intel.com> (raw)
In-Reply-To: <966a8a76-cf95-48ed-888f-d3b13417a8b5@linux.intel.com>
On 19.10.2023 16:41, Bernatowicz, Marcin wrote:
> Hi,
>
> On 10/18/2023 11:08 AM, Karolina Stolarek wrote:
>> On 17.10.2023 16:36, Marcin Bernatowicz wrote:
>>> Additionally check for overflow.
>>>
>>> This should allow to exercise large buffers
>>> ex. xe_exercise_blt -W 16384 -H 16384
>>
>> I think it would be good to add a dedicated (sub)test case to test
>> large buffers.
>>
> Maybe, but it's not the purpose of this patch.
Yes, I agree, but your change suggests that we've missed a test case
that should be there. To be clear, this suggestion won't block the patch.
>
>>>
>>> Signed-off-by: Marcin Bernatowicz <marcin.bernatowicz@linux.intel.com>
>>> ---
>>> lib/intel_blt.c | 8 +++++++-
>>> 1 file changed, 7 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/lib/intel_blt.c b/lib/intel_blt.c
>>> index a76c7a404..f46c85e91 100644
>>> --- a/lib/intel_blt.c
>>> +++ b/lib/intel_blt.c
>>> @@ -1607,12 +1607,18 @@ blt_create_object(const struct blt_copy_data
>>> *blt, uint32_t region,
>>> bool create_mapping)
>>> {
>>> struct blt_copy_object *obj;
>>> - uint64_t size = width * height * bpp / 8;
>>> uint32_t stride = tiling == T_LINEAR ? width * 4 : width;
>>> uint32_t handle;
>>> + uint64_t size;
>>> igt_assert_f(blt->driver, "Driver isn't set, have you called
>>> blt_copy_init()?\n");
>>> + igt_assert_f((UINT64_MAX / 8) >= width &&
>>> + (UINT64_MAX / width) >= height &&
>>> + (UINT64_MAX / (width * height)) >= bpp, "Overflow
>>> detected!\n");
>>
>> OK, it took a bit for me to parse it... So, we first check if we have
>> enough space to at least fit width, then compare it against height,
>> and then compare all of this to what's left for bpp. Is that correct?
>
> This one is to check if (width * height * bpp / 8) fits in UINT64_MAX
> and is just a paranoid check.
I understand the purpose, I was just checking if it does it right, one
step at a time...
>
>>
>> But still, width and height is limited by surface_with and
>> surface_height field that are u14 iirc. I don't think that overflow is
>> possible here?
>
> Where do we limit width and height to u14 ? I see uint32_t is used for
> width, height and bpp input params.
True that we don't limit the output.
Looking at this now, I think we should reject widths and heights that
don't fit into u14, because both block copy won't be able to handle it.
Fast copy expect pitches, and these are u16. When preparing the command
submission, we use helper structs which definitions would truncate the
width and height, for example:
dw16 of gen12_block_copy_data_ext:
uint32_t dst_surface_height: BITRANGE(0, 13);
uint32_t dst_surface_width: BITRANGE(14, 27);
dw07 of gen12_fast_copy_data:
uint32_t src_pitch: BITRANGE(0, 15);
So, I think, that while we should ensure there's no overflow, we should
give folks a heads up if they wish to copy bigger buffers (the latter is
more to a note to myself, don't worry). There was a similar issue
connected to display, and the only sensible solution was to use render
copy that has no such limitations.
All the best,
Karolina
>
> The current code overflows uint32_t when width = height = 16384 and bpp
> = 32 => 16384 * 16384 * 32 / 8 => 0 => size == 0 :(
>
> We need (uint64_t) cast to ensure the entire multiplication is done in
> 64-bit arithmetic. >
> --
> marcin
>>
>>> +
>>> + size = (uint64_t)width * height * bpp / 8;
>>
>> Cast takes precedence over *, but this line doesn't trigger any
>> warnings, so I think we don't have to add extra ().
>>
>> All the best,
>> Karolina
>>
>>> +
>>> obj = calloc(1, sizeof(*obj));
>>> obj->size = size;
next prev parent reply other threads:[~2023-10-20 6:58 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-17 14:36 [igt-dev] [PATCH i-g-t] lib/intel_blt.c: ensure uint64_t result of multiplication Marcin Bernatowicz
2023-10-17 15:59 ` [igt-dev] ✗ CI.xeBAT: failure for " Patchwork
2023-10-17 16:00 ` [igt-dev] ✗ Fi.CI.BAT: " Patchwork
2023-10-18 9:08 ` [igt-dev] [PATCH i-g-t] " Karolina Stolarek
2023-10-19 14:41 ` Bernatowicz, Marcin
2023-10-20 6:58 ` Karolina Stolarek [this message]
2023-10-18 15:28 ` Kamil Konieczny
2023-10-19 15:23 ` Bernatowicz, Marcin
2023-10-19 15:27 ` Kamil Konieczny
2023-10-19 15:36 ` Bernatowicz, Marcin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=34cfc694-8196-27c1-91f9-ab310af4ca31@intel.com \
--to=karolina.stolarek@intel.com \
--cc=igt-dev@lists.freedesktop.org \
--cc=marcin.bernatowicz@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox