From mboxrd@z Thu Jan  1 00:00:00 1970
Authentication-Results: passt.top; dmarc=pass (p=none dis=none) header.from=redhat.com
Authentication-Results: passt.top;
	dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=XAzDNQhK;
	dkim-atps=neutral
Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124])
	by passt.top (Postfix) with ESMTPS id 589B85A026F
	for <passt-dev@passt.top>; Thu, 09 Jan 2025 18:17:50 +0100 (CET)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
	s=mimecast20190719; t=1736443069;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=PStHVSbZYhwszjdTFsBjtkdhLLEDQyFFNayiqMbLidM=;
	b=XAzDNQhKiRKVMql4XoZf9m3mInFpNYX3Llrzode6WiH13+0ELEBDWFPtE2ldmdDMh4MRzk
	kO8GxFWrT/QUaYgTnANCGhL9TziBlu1f9OXJx4JgbuR7mi0gD7t2+KvChjTq1/QgyoVNnY
	lV8dB9Wbyg7Goh1Np7MJ2BW9dlLSNsg=
Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com
 [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id
 us-mta-593-yP6j-grvOx6ksJiWktsz1g-1; Thu, 09 Jan 2025 12:17:47 -0500
X-MC-Unique: yP6j-grvOx6ksJiWktsz1g-1
X-Mimecast-MFC-AGG-ID: yP6j-grvOx6ksJiWktsz1g
Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-385e49efd59so464580f8f.0
        for <passt-dev@passt.top>; Thu, 09 Jan 2025 09:17:47 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20230601; t=1736443065; x=1737047865;
        h=content-transfer-encoding:mime-version:organization:references
         :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state
         :from:to:cc:subject:date:message-id:reply-to;
        bh=PStHVSbZYhwszjdTFsBjtkdhLLEDQyFFNayiqMbLidM=;
        b=juLsBXxz3fy7mVSevBUcZY6opuMq/zKWYsqOxTJL9EnVf6QMLDyqHzGuJj+ngxZo3b
         V/dgtj/067mczkWFtPrrhnswQL8OaAVY4+ju05rD9DR5aym27qlHHIoF4HvjmvzQteN6
         XXwkkHpu4EEaBAIPCKImr8tMrTr4fj2N02q698rXoWtS/n9p7+CUtMyxbNv0quKnP0fI
         N0/PmFKLV3PA0HO7yCkF4M3YpIRKITc0oJ6u88Es+PIq3avnx6IuzmsSeMAStMnV7+D+
         dR8WDuoN42h43UklYTCxX2Py+GFdIE1xzU9ZipiG5lSXlbriofv0dGuoUNZMunCkOmg9
         KNdw==
X-Gm-Message-State: AOJu0Yws80rmyy2ol0+Qgq3T/Wq3Z2lF5on2BAkorV5gQ7L1Lj/4Cw3T
	bio1+pM12729829f3UqUNbTEobcyTkaVvlQHHZi8kW13KaJpHntyao++P6BjgphNo9o37Cf3LZr
	8RUXwkoR+E6GGpIsWbDZ6QyVrEvs5OJfZ3UyEHPD38nT9xpEdo5dubE14ng==
X-Gm-Gg: ASbGncs3128X9ANd9Css4XcNNKNg6PjSNb3x5baAhm+a1Wf4/gHnhqeTQUlue2VSoKA
	0njlSPcTXA/W+KycL/sOBHIRSgLC2gZZ0SqOAqX5/VhpMNgqmywdafVeRv03Eu/yIdOzIy+VHiI
	vivPnVNWGz9PPiws4kXk4Z9DHW+QJdACz73AU1/paKkADgMJ7wEL0u5/J+tMnPEN14UvlpKgwXB
	ts+lLThnQ/bYc+UeuaLBzToTiLWkmBdEiA6Wk75tl8PM7TOCA66THwo5aF7Lb1ABVYg8UWH9OxU
	AsSzpmlqpg==
X-Received: by 2002:a05:6000:2ce:b0:385:ee59:44f1 with SMTP id ffacd0b85a97d-38a872dec2amr6602805f8f.20.1736443065614;
        Thu, 09 Jan 2025 09:17:45 -0800 (PST)
X-Google-Smtp-Source: AGHT+IHRav8/EXIKlI3YJzeuvB6OhiQyHxZfl7imV+gbUt/IfB0Im1WiGI3f2Fc2TKf/6bxbwcUjng==
X-Received: by 2002:a05:6000:2ce:b0:385:ee59:44f1 with SMTP id ffacd0b85a97d-38a872dec2amr6602785f8f.20.1736443065246;
        Thu, 09 Jan 2025 09:17:45 -0800 (PST)
Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4])
        by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a8e3840bfsm2366511f8f.39.2025.01.09.09.17.44
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 09 Jan 2025 09:17:44 -0800 (PST)
Date: Thu, 9 Jan 2025 18:17:42 +0100
From: Stefano Brivio <sbrivio@redhat.com>
To: Laurent Vivier <lvivier@redhat.com>
Subject: Re: [PATCH] checksum: fix checksum with odd base address
Message-ID: <20250109181742.4a97d717@elisabeth>
In-Reply-To: <55777e39-4b17-47be-8fff-3dd1faa53e18@redhat.com>
References: <20250109130648.326933-1-lvivier@redhat.com>
	<20250109163642.0a0bfcea@elisabeth>
	<55777e39-4b17-47be-8fff-3dd1faa53e18@redhat.com>
Organization: Red Hat
X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; x86_64-pc-linux-gnu)
MIME-Version: 1.0
X-Mimecast-Spam-Score: 0
X-Mimecast-MFC-PROC-ID: 2NFLS2z4EPZyGSDoZwmkjvsXwIl0P1J9ylATFY-EuFo_1736443067
X-Mimecast-Originator: redhat.com
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Message-ID-Hash: R3R56R6ZZ37PNK4R6G2IULRBNGYKH6YV
X-Message-ID-Hash: R3R56R6ZZ37PNK4R6G2IULRBNGYKH6YV
X-MailFrom: sbrivio@redhat.com
X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header
CC: passt-dev@passt.top, Mike Jones <mike@mjones.io>
X-Mailman-Version: 3.3.8
Precedence: list
List-Id: Development discussion and patches for passt <passt-dev.passt.top>
Archived-At: <https://archives.passt.top/passt-dev/20250109181742.4a97d717@elisabeth/>
Archived-At: <https://passt.top/hyperkitty/list/passt-dev@passt.top/message/R3R56R6ZZ37PNK4R6G2IULRBNGYKH6YV/>
List-Archive: <https://archives.passt.top/passt-dev/>
List-Archive: <https://passt.top/hyperkitty/list/passt-dev@passt.top/>
List-Help: <mailto:passt-dev-request@passt.top?subject=help>
List-Owner: <mailto:passt-dev-owner@passt.top>
List-Post: <mailto:passt-dev@passt.top>
List-Subscribe: <mailto:passt-dev-join@passt.top>
List-Unsubscribe: <mailto:passt-dev-leave@passt.top>

On Thu, 9 Jan 2025 17:47:06 +0100
Laurent Vivier <lvivier@redhat.com> wrote:

> On 09/01/2025 16:36, Stefano Brivio wrote:
> > [Cc'ed Mike who reported this]
> > 
> > On Thu,  9 Jan 2025 14:06:48 +0100
> > Laurent Vivier <lvivier@redhat.com> wrote:
> >   
> >> csum_unfolded() must call csum_avx2() with a 32byte aligned base address.
> >>
> >> To be able to do that if the buffer is not correctly aligned,
> >> it splits the buffers in 2 parts, the second part is 32byte aligned and
> >> can be used with csum_avx2(), the first part is the remaining part, that
> >> is not 32byte aligned and we use sum_16b() to compute the checksum.
> >>
> >> A problem appears if the length of the first part is odd because
> >> the checksum is using 16bit words to do the checksum.
> >>
> >> If the length is odd, when the second part is computed, all words are
> >> shifted by 1 byte, meaning weight of upper and lower byte is swapped.
> >>
> >> For instance a 13 bytes buffer:
> >>
> >> bytes:
> >>
> >> aa AA bb BB cc CC dd DD ee EE ff FF gg
> >>
> >> 16bit words:
> >>
> >> AAaa BBbb CCcc DDdd EEee FFff 00gg
> >>
> >> If we don't split the sequence, the checksum is:
> >>
> >> AAaa + BBbb + CCcc + DDdd + EEee + FFff + 00gg
> >>
> >> If we split the sequence with an even length for the first part:
> >>
> >> (AAaa + BBbb) + (CCcc + DDdd + EEee + FFff + 00gg)
> >>
> >> But if the first part has an odd length:
> >>
> >> (AAaa + BBbb + 00cc) + (ddCC + eeDD + ffEE + ggFF)  
> > 
> > Thanks, this description is really helpful.
> >   
> >> To avoid the problem, do not call csum_avx2() if the first part cannot
> >> have an even length, and compute the checksum of all the buffer using
> >> sum_16b().
> >>
> >> This is slower but it can only happen if the buffer base address is odd,
> >> and this can only happen if the binary is built using '-Os', and that
> >> means we have chosen to prioritize size over speed.  
> > 
> > Reported-by: Mike Jones <mike@mjones.io>
> >   
> >> Link: https://bugs.passt.top/show_bug.cgi?id=108
> >> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> >> ---
> >>   checksum.c | 2 +-
> >>   1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/checksum.c b/checksum.c
> >> index 1c4354d35734..2fd6867cdf75 100644
> >> --- a/checksum.c
> >> +++ b/checksum.c
> >> @@ -452,7 +452,7 @@ uint32_t csum_unfolded(const void *buf, size_t len, uint32_t init)
> >>   	intptr_t align = ROUND_UP((intptr_t)buf, sizeof(__m256i));
> >>   	unsigned int pad = align - (intptr_t)buf;
> >>   
> >> -	if (len < pad)
> >> +	if (pad & 1 || len < pad)  
> > 
> > I'm fine applying this as it is, because the issue is quite nasty and we
> > have this great commit message anyway, but for clarity, could we have a
> > comment mentioning why we're doing this? Something like:
> > 
> >    /* Don't mix sum_16b() and csum_avx2() with odd padding lengths */
> > 
> > (I'm not quite satisfied with it but I find it better than nothing).
> >   
> >>   		pad = len;
> >>   
> >>   	if (pad)  
> 
> Could you update the patch on merge accordingly to your comments?

Ah, yes, sure. I'll just add that line.

-- 
Stefano