Re: [RFC][PATCH] fix csum_and_copy_..._user() idiocy. Re: AW: [PATCH] amd64: Fix csum_partial_copy_generic()

From: Thomas Gleixner
Date: Tue Oct 24 2023 - 08:31:12 EST


On Tue, Oct 24 2023 at 05:26, Al Viro wrote:
> On Mon, Oct 23, 2023 at 12:37:58PM +0200, Thomas Gleixner wrote:
>> On Sun, Oct 22 2023 at 20:46, Al Viro wrote:
>> > - return checksum;
>> > + return from64to16 (checksum);
>>
>> from64to16(checksum); all over the place
>
> Umm... Is that about whitespace?

Yes, my parser choked on that :)

>> > /*
>> > - * We report fault by returning 0 csum - impossible in normal case, since
>> > - * we start with 0xffffffff for initial sum.
>> > + * We report fault by returning ~0ULL csum
>> > */
>>
>> There is also a stale comment a few lines further up.
>
> Umm...
> * Returns : r0:r1 = checksum:0 on success or -1:-1 on fault
> perhaps?

Looks good.

>> > +static inline bool wsum_fault_check(__wsum_fault v)
>> > +{
>> > +#if defined(CONFIG_64BIT) || defined(__LITTLE_ENDIAN__)
>> > + return (__force s64)v < 0;
>> > +#else
>> > + return (int)(__force u32)v < 0;
>>
>> Why not __force s32 right away?
>
> Mostly to keep the reader within more familiar cases
> of conversion - u64 to u32 is "throw the upper 32 bits
> away", u32 to s32 - "treat MSB as sign".

Fair enough.

> It's still a nasal demon country, of course - the proper
> solution is
>
> static inline bool wsum_fault_check(__wsum_fault v)
> {
> #if defined(CONFIG_64BIT) || defined(__LITTLE_ENDIAN__)
> return (__force u64)v & (1ULL << 63);
> #else
> return (__force u32)v & (1ULL << 31);
> #endif
> }
>
> Incidentally, in this case we really want a cast to u32
> rather than u64 - gcc is smart enough to figure out that
> checking MSB in 32bit can be done as signed 32bit comparison
> with 0, but bit 31 in 64bit is not special as far as it's
> concerned, even though it's a bit 31 of 32bit register...

Indeed.

>> As the callers just check for != 0 such a partial copy is considered
>> success, no?
>
> Check the callers...
>
> static __always_inline __must_check
> bool csum_and_copy_from_iter_full(void *addr, size_t bytes,
> __wsum *csum, struct iov_iter *i)
> {
> size_t copied = csum_and_copy_from_iter(addr, bytes, csum, i);
> if (likely(copied == bytes))
> return true;

Duh. I think I stared at a caller of csum_and_copy_from_iter_full()
instead...

Thanks,

tglx