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=d3yImjsK; 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 AE9C25A0272 for ; Fri, 10 Jan 2025 23:53:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736549587; 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=Opw++11LiASwzUBG0+rnYTg79yetFLyLmBrbsXUwtEY=; b=d3yImjsKctPIxFGJLaR78MayNhWfcNkaDDQtW5oeDuek+J2/FsXXU+9O+N92TaXXa/ux+F k53fKzQ9JJeddB28m+DHPvS9xmiZ6CCiktRXKsaoVceOqPsF/AT1+gDu8wUitS0my9lHuc MA4DJDQuppIm8QHsW1L0Aigc5VxS1FY= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-353-qTdHqUBhNcS_MCMPrKYDUQ-1; Fri, 10 Jan 2025 17:53:05 -0500 X-MC-Unique: qTdHqUBhNcS_MCMPrKYDUQ-1 X-Mimecast-MFC-AGG-ID: qTdHqUBhNcS_MCMPrKYDUQ Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-43635895374so16216215e9.0 for ; Fri, 10 Jan 2025 14:53:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736549585; x=1737154385; 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=Opw++11LiASwzUBG0+rnYTg79yetFLyLmBrbsXUwtEY=; b=IRti+GhYdpcubA7SlXnJBO1BGURYa/n0XcIcx/QIEePwbkqHYO/f8+US6qAzAefIAR E6BRBWF8lpK66J0YIzdTUF19htOrXhZ6KXpDrTx+8DejG+6Q3p59iD9AXUJtESAOdarI eDbn0Cr1MKwYSEwMpUMKhxcIkxkgke6YdBU9S3ubfa/pOvhsi61HRpXtFfLD5bqs9A8v BjGuhA8qcGHZDmfrxREoGICfCkk/edo7nNbqRaTTqS5qys8jJV4GaVVwpVwx/kqV8SbG 8rV/C6M+17GHTMgAhqwH+fXe6QejCLntYUqTC6quQEIZlI0ykRgUYBzCpYMnBaN6zi8K d1TQ== X-Gm-Message-State: AOJu0Yyk4yLeXoKdXexoQzKsaNJEaXH43bq1HrmlAgVXzn/aiZI7EQU9 ufRs4MeceFl6O3yZs49PtCJsuPRBSS8Mdwn38ob+pGr+KCm1RVSJF/o9NYM4+f8YMz/tra/XuRb HzP3L9Sq3MwvmHG/x0biTSn3Hri5cJYvZm/oJ51WhdScCz+H3YA== X-Gm-Gg: ASbGnctvQVlTR0HTsEawSae9XPFmr2oiWv6NphwKnkl6bvDdZMWUVc2aigXIOnwzcF9 XknNTLDs4Ey4LfC5q0oPJigNW7R9lbLgR+vzOUrZqkHmk1c755lc1TQ5Dxdzromw88bBY73aWW5 rl5aaoBLNqccao1f9wu9WxlmNfpqs0k6Yl7lhlpZyNJeJBVHWDtyCto7X2ALfVNvLR+S7Ogd6XM i4tOMEo+SeYC/h2fuqaSoHE4bmTQslULJTmEpzVawthMkQalxndrsyY6Aodn+PZ9syDJW6poIMo sGRpIbaFaQ== X-Received: by 2002:a5d:5885:0:b0:382:4b69:9ca4 with SMTP id ffacd0b85a97d-38a8b0f832amr7907708f8f.26.1736549584759; Fri, 10 Jan 2025 14:53:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IEQgMPXXS2FRjQnNTWIFcig4ZW2k7zDYsE8MVpK6d/meIekdugNeZW4sgn96OXUyCAlXfXtjA== X-Received: by 2002:a5d:5885:0:b0:382:4b69:9ca4 with SMTP id ffacd0b85a97d-38a8b0f832amr7907696f8f.26.1736549584426; Fri, 10 Jan 2025 14:53:04 -0800 (PST) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a8e38c7aesm5738807f8f.53.2025.01.10.14.53.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2025 14:53:02 -0800 (PST) Date: Fri, 10 Jan 2025 23:52:59 +0100 From: Stefano Brivio To: Laurent Vivier Subject: Re: [PATCH] checksum: fix checksum with odd base address Message-ID: <20250110235259.62419d86@elisabeth> In-Reply-To: <20250109181742.4a97d717@elisabeth> References: <20250109130648.326933-1-lvivier@redhat.com> <20250109163642.0a0bfcea@elisabeth> <55777e39-4b17-47be-8fff-3dd1faa53e18@redhat.com> <20250109181742.4a97d717@elisabeth> 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: ZCBe0x_5JKZCarqG3yrgUooIAfMCib65ppHZINLptwU_1736549585 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: NUQLB5WV43KDLCY45ND47UOVGAL4K2QX X-Message-ID-Hash: NUQLB5WV43KDLCY45ND47UOVGAL4K2QX 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 X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Thu, 9 Jan 2025 18:17:42 +0100 Stefano Brivio wrote: > On Thu, 9 Jan 2025 17:47:06 +0100 > Laurent Vivier 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 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 > > > > > >> Link: https://bugs.passt.top/show_bug.cgi?id=108 > > >> Signed-off-by: Laurent Vivier > > >> --- > > >> 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. Applied. -- Stefano