From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 8F177A0540 for ; Thu, 7 Jul 2022 09:56:38 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8A4BC415D7; Thu, 7 Jul 2022 09:56:38 +0200 (CEST) Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) by mails.dpdk.org (Postfix) with ESMTP id 3B4C840A7B for ; Thu, 7 Jul 2022 09:56:37 +0200 (CEST) Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 1C5EB3F6F8 for ; Thu, 7 Jul 2022 07:56:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1657180597; bh=2hJngufuJWX6wNZHspbq+R4Ay1VvwMhl3JjeqQXOCuw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=C+Uw9K9mrUHP856uNE/LIPUtcD4uxAgonWTJJN/yVWfDCuBUEO7RfDFU6eJ4eyQjM lAljqQ73Zcb1lXOvoccxYp1fRPdZDUudKMjHu4DHoNK/LG8+OiMKQkD17ac6+prEhm HFqykct2e9ZtM4+ijobEWSZWd/hVAPcMZv9zOwIlFfwZYeZ4SgCpRgIiEoGsmcr3ti DKN+rYIpS9qAUaXl0wTEpGnE3qDsffzv8jVrlYfdP6lWe4YUXYoGqnOwR5bNEuv2Wy LUa3ixKAUgn29KGzrQ/NUyyqJWjtU1UdHLO0ZnvRIZxTMpmVTPS85V5y5fhKNsBXFW GtjGovIvMC1Dw== Received: by mail-ej1-f72.google.com with SMTP id sg40-20020a170907a42800b00722faf0aacbso4435300ejc.3 for ; Thu, 07 Jul 2022 00:56:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2hJngufuJWX6wNZHspbq+R4Ay1VvwMhl3JjeqQXOCuw=; b=Viuwmf3la4Zky9g651Y8Qdfwr/qoef4FiC2vqqKtoC9rzN2zzL1W6q+698Gp1duj33 FAsskNR5nTThznuGwHP7OF7gmHBMQXQGVGlJY6wdw0EcGBEtx0kRHlLJBDRNOXDPkZfA SgJkYaBqPWEeQ4+z1pEeYxm2blnQkix7nZQcX8gzgTBuFSycln+feYqaJKWJqm+pH9gv daILHlQGln/1mV9bY0IlZngAqvaLfF8rbTY7gnRwPhAxfN5oCPt2+7loNTlarL19/5Wi XJksraCFwR1vEo0tuhzKJDr7VOGXCwoX0QXP5T/AukCSAfdCGhP0HqxIAV0ZNKs4wjZW 1wbw== X-Gm-Message-State: AJIora8H5jNjzcrM4oD/Q1Ad3Eu82RMG7CJpWatSGlei1L+/MU1lXILT yp7g5Ts94+F8nF52PQcmTT+ePTHiTppYDLFM4vIRc/Tz9jMWoQqQtebvosRNqQtLPrU+3BkyukO 2WU3JP3NA2O956qvTdI8JFZLq X-Received: by 2002:a17:906:9be0:b0:72a:bb96:581 with SMTP id de32-20020a1709069be000b0072abb960581mr21449120ejc.681.1657180596505; Thu, 07 Jul 2022 00:56:36 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ssihiZuNEHAmBeiYhAnaHlolseAwavHdie4OGFlWLKZhd/+uyO7wSVW4syWYMBUDaC517H3A== X-Received: by 2002:a17:906:9be0:b0:72a:bb96:581 with SMTP id de32-20020a1709069be000b0072abb960581mr21449104ejc.681.1657180596235; Thu, 07 Jul 2022 00:56:36 -0700 (PDT) Received: from Keschdeichel.fritz.box ([2a02:6d40:3a4f:7b00:c19b:938e:7c97:afe2]) by smtp.gmail.com with ESMTPSA id s16-20020a170906455000b00722bc0aa9e3sm12194041ejq.162.2022.07.07.00.56.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 00:56:35 -0700 (PDT) From: christian.ehrhardt@canonical.com To: Luc Pelletier Cc: Konstantin Ananyev , dpdk stable Subject: patch 'eal/x86: fix unaligned access for small memcpy' has been queued to stable release 19.11.13 Date: Thu, 7 Jul 2022 09:54:20 +0200 Message-Id: <20220707075522.194223-25-christian.ehrhardt@canonical.com> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220707075522.194223-1-christian.ehrhardt@canonical.com> References: <20220707075522.194223-1-christian.ehrhardt@canonical.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Hi, FYI, your patch has been queued to stable release 19.11.13 Note it hasn't been pushed to http://dpdk.org/browse/dpdk-stable yet. It will be pushed if I get no objections before 07/09/22. So please shout if anyone has objections. Also note that after the patch there's a diff of the upstream commit vs the patch applied to the branch. This will indicate if there was any rebasing needed to apply to the stable branch. If there were code changes for rebasing (ie: not only metadata diffs), please double check that the rebase was correctly done. Queued patches are on a temporary branch at: https://github.com/cpaelzer/dpdk-stable-queue This queued commit can be viewed at: https://github.com/cpaelzer/dpdk-stable-queue/commit/f584d5fb260ac4d0c50acfc5a9460070861b21c9 Thanks. Christian Ehrhardt --- >From f584d5fb260ac4d0c50acfc5a9460070861b21c9 Mon Sep 17 00:00:00 2001 From: Luc Pelletier Date: Fri, 25 Feb 2022 11:38:05 -0500 Subject: [PATCH] eal/x86: fix unaligned access for small memcpy [ upstream commit 00901e4d1a9ee7c7b43d0a3592683f0a420a331d ] Calls to rte_memcpy for 1 < n < 16 could result in unaligned loads/stores, which is undefined behaviour according to the C standard, and strict aliasing violations. The code was changed to use a packed structure that allows aliasing (using the __may_alias__ attribute) to perform the load/store operations. This results in code that has the same performance as the original code and that is also C standards-compliant. Fixes: af75078fece3 ("first public release") Signed-off-by: Luc Pelletier Acked-by: Konstantin Ananyev Tested-by: Konstantin Ananyev --- .../common/include/arch/x86/rte_memcpy.h | 133 +++++++----------- lib/librte_eal/common/include/rte_common.h | 5 + 2 files changed, 56 insertions(+), 82 deletions(-) diff --git a/lib/librte_eal/common/include/arch/x86/rte_memcpy.h b/lib/librte_eal/common/include/arch/x86/rte_memcpy.h index f1751dd41c..8c2e243d9e 100644 --- a/lib/librte_eal/common/include/arch/x86/rte_memcpy.h +++ b/lib/librte_eal/common/include/arch/x86/rte_memcpy.h @@ -45,6 +45,52 @@ extern "C" { static __rte_always_inline void * rte_memcpy(void *dst, const void *src, size_t n); +/** + * Copy bytes from one location to another, + * locations should not overlap. + * Use with n <= 15. + */ +static __rte_always_inline void * +rte_mov15_or_less(void *dst, const void *src, size_t n) +{ + /** + * Use the following structs to avoid violating C standard + * alignment requirements and to avoid strict aliasing bugs + */ + struct rte_uint64_alias { + uint64_t val; + } __rte_packed __rte_may_alias; + struct rte_uint32_alias { + uint32_t val; + } __rte_packed __rte_may_alias; + struct rte_uint16_alias { + uint16_t val; + } __rte_packed __rte_may_alias; + + void *ret = dst; + if (n & 8) { + ((struct rte_uint64_alias *)dst)->val = + ((const struct rte_uint64_alias *)src)->val; + src = (const uint64_t *)src + 1; + dst = (uint64_t *)dst + 1; + } + if (n & 4) { + ((struct rte_uint32_alias *)dst)->val = + ((const struct rte_uint32_alias *)src)->val; + src = (const uint32_t *)src + 1; + dst = (uint32_t *)dst + 1; + } + if (n & 2) { + ((struct rte_uint16_alias *)dst)->val = + ((const struct rte_uint16_alias *)src)->val; + src = (const uint16_t *)src + 1; + dst = (uint16_t *)dst + 1; + } + if (n & 1) + *(uint8_t *)dst = *(const uint8_t *)src; + return ret; +} + #if defined RTE_MACHINE_CPUFLAG_AVX512F && defined RTE_MEMCPY_AVX512 #define ALIGNMENT_MASK 0x3F @@ -171,8 +217,6 @@ rte_mov512blocks(uint8_t *dst, const uint8_t *src, size_t n) static __rte_always_inline void * rte_memcpy_generic(void *dst, const void *src, size_t n) { - uintptr_t dstu = (uintptr_t)dst; - uintptr_t srcu = (uintptr_t)src; void *ret = dst; size_t dstofss; size_t bits; @@ -181,24 +225,7 @@ rte_memcpy_generic(void *dst, const void *src, size_t n) * Copy less than 16 bytes */ if (n < 16) { - if (n & 0x01) { - *(uint8_t *)dstu = *(const uint8_t *)srcu; - srcu = (uintptr_t)((const uint8_t *)srcu + 1); - dstu = (uintptr_t)((uint8_t *)dstu + 1); - } - if (n & 0x02) { - *(uint16_t *)dstu = *(const uint16_t *)srcu; - srcu = (uintptr_t)((const uint16_t *)srcu + 1); - dstu = (uintptr_t)((uint16_t *)dstu + 1); - } - if (n & 0x04) { - *(uint32_t *)dstu = *(const uint32_t *)srcu; - srcu = (uintptr_t)((const uint32_t *)srcu + 1); - dstu = (uintptr_t)((uint32_t *)dstu + 1); - } - if (n & 0x08) - *(uint64_t *)dstu = *(const uint64_t *)srcu; - return ret; + return rte_mov15_or_less(dst, src, n); } /** @@ -379,8 +406,6 @@ rte_mov128blocks(uint8_t *dst, const uint8_t *src, size_t n) static __rte_always_inline void * rte_memcpy_generic(void *dst, const void *src, size_t n) { - uintptr_t dstu = (uintptr_t)dst; - uintptr_t srcu = (uintptr_t)src; void *ret = dst; size_t dstofss; size_t bits; @@ -389,25 +414,7 @@ rte_memcpy_generic(void *dst, const void *src, size_t n) * Copy less than 16 bytes */ if (n < 16) { - if (n & 0x01) { - *(uint8_t *)dstu = *(const uint8_t *)srcu; - srcu = (uintptr_t)((const uint8_t *)srcu + 1); - dstu = (uintptr_t)((uint8_t *)dstu + 1); - } - if (n & 0x02) { - *(uint16_t *)dstu = *(const uint16_t *)srcu; - srcu = (uintptr_t)((const uint16_t *)srcu + 1); - dstu = (uintptr_t)((uint16_t *)dstu + 1); - } - if (n & 0x04) { - *(uint32_t *)dstu = *(const uint32_t *)srcu; - srcu = (uintptr_t)((const uint32_t *)srcu + 1); - dstu = (uintptr_t)((uint32_t *)dstu + 1); - } - if (n & 0x08) { - *(uint64_t *)dstu = *(const uint64_t *)srcu; - } - return ret; + return rte_mov15_or_less(dst, src, n); } /** @@ -672,8 +679,6 @@ static __rte_always_inline void * rte_memcpy_generic(void *dst, const void *src, size_t n) { __m128i xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8; - uintptr_t dstu = (uintptr_t)dst; - uintptr_t srcu = (uintptr_t)src; void *ret = dst; size_t dstofss; size_t srcofs; @@ -682,25 +687,7 @@ rte_memcpy_generic(void *dst, const void *src, size_t n) * Copy less than 16 bytes */ if (n < 16) { - if (n & 0x01) { - *(uint8_t *)dstu = *(const uint8_t *)srcu; - srcu = (uintptr_t)((const uint8_t *)srcu + 1); - dstu = (uintptr_t)((uint8_t *)dstu + 1); - } - if (n & 0x02) { - *(uint16_t *)dstu = *(const uint16_t *)srcu; - srcu = (uintptr_t)((const uint16_t *)srcu + 1); - dstu = (uintptr_t)((uint16_t *)dstu + 1); - } - if (n & 0x04) { - *(uint32_t *)dstu = *(const uint32_t *)srcu; - srcu = (uintptr_t)((const uint32_t *)srcu + 1); - dstu = (uintptr_t)((uint32_t *)dstu + 1); - } - if (n & 0x08) { - *(uint64_t *)dstu = *(const uint64_t *)srcu; - } - return ret; + return rte_mov15_or_less(dst, src, n); } /** @@ -818,27 +805,9 @@ rte_memcpy_aligned(void *dst, const void *src, size_t n) { void *ret = dst; - /* Copy size <= 16 bytes */ + /* Copy size < 16 bytes */ if (n < 16) { - if (n & 0x01) { - *(uint8_t *)dst = *(const uint8_t *)src; - src = (const uint8_t *)src + 1; - dst = (uint8_t *)dst + 1; - } - if (n & 0x02) { - *(uint16_t *)dst = *(const uint16_t *)src; - src = (const uint16_t *)src + 1; - dst = (uint16_t *)dst + 1; - } - if (n & 0x04) { - *(uint32_t *)dst = *(const uint32_t *)src; - src = (const uint32_t *)src + 1; - dst = (uint32_t *)dst + 1; - } - if (n & 0x08) - *(uint64_t *)dst = *(const uint64_t *)src; - - return ret; + return rte_mov15_or_less(dst, src, n); } /* Copy 16 <= size <= 32 bytes */ diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h index fe7539af26..fc938eadcd 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -68,6 +68,11 @@ typedef uint16_t unaligned_uint16_t; */ #define __rte_packed __attribute__((__packed__)) +/** + * Macro to mark a type that is not subject to type-based aliasing rules + */ +#define __rte_may_alias __attribute__((__may_alias__)) + /******* Macro to mark functions and fields scheduled for removal *****/ #define __rte_deprecated __attribute__((__deprecated__)) -- 2.37.0 --- Diff of the applied patch vs upstream commit (please double-check if non-empty: --- --- - 2022-07-07 09:54:12.279469605 +0200 +++ 0025-eal-x86-fix-unaligned-access-for-small-memcpy.patch 2022-07-07 09:54:10.837823826 +0200 @@ -1 +1 @@ -From 00901e4d1a9ee7c7b43d0a3592683f0a420a331d Mon Sep 17 00:00:00 2001 +From f584d5fb260ac4d0c50acfc5a9460070861b21c9 Mon Sep 17 00:00:00 2001 @@ -5,0 +6,2 @@ +[ upstream commit 00901e4d1a9ee7c7b43d0a3592683f0a420a331d ] + @@ -16 +17,0 @@ -Cc: stable@dpdk.org @@ -22,2 +23,2 @@ - lib/eal/include/rte_common.h | 5 ++ - lib/eal/x86/include/rte_memcpy.h | 133 ++++++++++++------------------- + .../common/include/arch/x86/rte_memcpy.h | 133 +++++++----------- + lib/librte_eal/common/include/rte_common.h | 5 + @@ -26,20 +27,4 @@ -diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h -index d56a7570c0..a96cc2a138 100644 ---- a/lib/eal/include/rte_common.h -+++ b/lib/eal/include/rte_common.h -@@ -85,6 +85,11 @@ typedef uint16_t unaligned_uint16_t; - */ - #define __rte_packed __attribute__((__packed__)) - -+/** -+ * Macro to mark a type that is not subject to type-based aliasing rules -+ */ -+#define __rte_may_alias __attribute__((__may_alias__)) -+ - /******* Macro to mark functions and fields scheduled for removal *****/ - #define __rte_deprecated __attribute__((__deprecated__)) - #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg))) -diff --git a/lib/eal/x86/include/rte_memcpy.h b/lib/eal/x86/include/rte_memcpy.h -index 1b6c6e585f..18aa4e43a7 100644 ---- a/lib/eal/x86/include/rte_memcpy.h -+++ b/lib/eal/x86/include/rte_memcpy.h +diff --git a/lib/librte_eal/common/include/arch/x86/rte_memcpy.h b/lib/librte_eal/common/include/arch/x86/rte_memcpy.h +index f1751dd41c..8c2e243d9e 100644 +--- a/lib/librte_eal/common/include/arch/x86/rte_memcpy.h ++++ b/lib/librte_eal/common/include/arch/x86/rte_memcpy.h @@ -96 +81 @@ - #if defined __AVX512F__ && defined RTE_MEMCPY_AVX512 + #if defined RTE_MACHINE_CPUFLAG_AVX512F && defined RTE_MEMCPY_AVX512 @@ -235,0 +221,16 @@ +diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h +index fe7539af26..fc938eadcd 100644 +--- a/lib/librte_eal/common/include/rte_common.h ++++ b/lib/librte_eal/common/include/rte_common.h +@@ -68,6 +68,11 @@ typedef uint16_t unaligned_uint16_t; + */ + #define __rte_packed __attribute__((__packed__)) + ++/** ++ * Macro to mark a type that is not subject to type-based aliasing rules ++ */ ++#define __rte_may_alias __attribute__((__may_alias__)) ++ + /******* Macro to mark functions and fields scheduled for removal *****/ + #define __rte_deprecated __attribute__((__deprecated__)) +