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 E4B6043F3A; Tue, 30 Apr 2024 12:06:24 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2C09C402E3; Tue, 30 Apr 2024 12:06:00 +0200 (CEST) Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04on2055.outbound.protection.outlook.com [40.107.7.55]) by mails.dpdk.org (Postfix) with ESMTP id 23A0B402BA for ; Tue, 30 Apr 2024 12:05:56 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PnWGvgjBBxEA8m4509+IuQRhgmxrj1nwQBu8BLIva58nUDXWtydGUs1WM42Enkp51o53+fPB2gdAzDBZCEDyS44y0q/Eql8a4iypwc4WiY/Z8/Zz504ir7OOIeFwhEam286TbhquQGcsOqYq8GE6CGz0S1ODMVtMGWLnz5BppvzxR1MO+DBFStDU1rDhXPi4+i9S4RrtLcJoTr9PDYRPIPbm1yh7ZtFqV204wofNP+G29cPg5qUokX0G7kSCjXEi6FqmqEK3cMLLCEt4kt0TFuEoU93ac9RzgvO25d3s9WyvYlUZ3e4Mgzbdjj4tEfps5mZMbTzSoX0jjz5P+5CJpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=AvPWbCvgzyOJyxa/j0ge/ILRgogIdQ9NHvtGx46yEDw=; b=bDeekbL81v3429vh/TW04dH1WnXTv0nYP9xF2X5wQAjoLFClbnYj0NKNKAPk0ixhTUKLv9/aeeWjpq5bJSDdWUTUPUfQw6Phu0Yk3z1j6UKLjMdj8LbjfeQ6wojNgKRQxeoNwPnBWouEBt130trjHaTvbWydEojZaHSd9Zhcb6l8BzjXRI3Q12CwenPGxInxDQKpRyrzENiqKTLWQmtUQJQ8FZq+8VP7T9JrQ6Uhb5cbuZEQ0NuAfXITzixyDcoRPRQ5M8BNGt7SjjBDBoCxOxpfcYwW2oGUoeIxpKG7BQc7rNHmMdujZ79XhkNXzZXqZNjuJHl+h3kKmjHiyFUHtw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 192.176.1.74) smtp.rcpttodomain=dpdk.org smtp.mailfrom=ericsson.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ericsson.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AvPWbCvgzyOJyxa/j0ge/ILRgogIdQ9NHvtGx46yEDw=; b=prVgr1sN1v24nq7JnyQdWBYJvHnmIA8GMfk67wgXvQLXfBpU+zep9Lxh7f9U6ogo9AeJ+89asEu4SVhQDLHDHgpZ0A3mNbn1cAvaYirxr77JtI2+Y9UrbNqOGFY7CFGrOgGHNmoFN2i34fXZ2bG9dkIiOMMOhYJtYqLJiinu1r/RwEosifqXjlDlOuSX8Y0qDais7F4J/4soOSG64/SrqYKUr9hK92zzn6uxs/rRLPBdrTIPSvq/E4ITN58aiYCHBKU6ssTQFnWCchM6IBONkuXSoaxDpTIWSUPO1DXKyZf7VzgFCRIWxDi/dePRFpMAOj+v1AlE5IlrXee3aXDBBw== Received: from AM6P191CA0101.EURP191.PROD.OUTLOOK.COM (2603:10a6:209:8a::42) by AM9PR07MB7859.eurprd07.prod.outlook.com (2603:10a6:20b:302::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.36; Tue, 30 Apr 2024 10:05:54 +0000 Received: from AMS1EPF00000042.eurprd04.prod.outlook.com (2603:10a6:209:8a:cafe::3c) by AM6P191CA0101.outlook.office365.com (2603:10a6:209:8a::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.35 via Frontend Transport; Tue, 30 Apr 2024 10:05:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 192.176.1.74) smtp.mailfrom=ericsson.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ericsson.com; Received-SPF: Pass (protection.outlook.com: domain of ericsson.com designates 192.176.1.74 as permitted sender) receiver=protection.outlook.com; client-ip=192.176.1.74; helo=oa.msg.ericsson.com; pr=C Received: from oa.msg.ericsson.com (192.176.1.74) by AMS1EPF00000042.mail.protection.outlook.com (10.167.16.39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.18 via Frontend Transport; Tue, 30 Apr 2024 10:05:54 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.62) with Microsoft SMTP Server id 15.2.1544.9; Tue, 30 Apr 2024 12:05:53 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id 287821C006A; Tue, 30 Apr 2024 12:05:53 +0200 (CEST) From: =?UTF-8?q?Mattias=20R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?UTF-8?q?Morten=20Br=C3=B8rup?= , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Subject: [RFC v4 3/6] eal: add exactly-once bit access functions Date: Tue, 30 Apr 2024 11:55:20 +0200 Message-ID: <20240430095523.108688-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240430095523.108688-1-mattias.ronnblom@ericsson.com> References: <20240429095138.106849-2-mattias.ronnblom@ericsson.com> <20240430095523.108688-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AMS1EPF00000042:EE_|AM9PR07MB7859:EE_ X-MS-Office365-Filtering-Correlation-Id: 71bfe9dc-cc0a-41af-35e5-08dc68fd1fa0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|82310400014|376005|36860700004|1800799015; X-Microsoft-Antispam-Message-Info: =?utf-8?B?QmduY01neGpja2gvU0R0Rm5RU1dvTWtTTXVNWmw2dUFDWkFnVXg5OGVXejQ1?= =?utf-8?B?YkV5a1BIVk5pQnVLZURvTTMrYjc4WmlCdHFiNW1SSXFLN2RLa1huWUgvSit0?= =?utf-8?B?bHlRZGU0dGVDb0U3QnpKRzI0TmNLZGtPemlsdExuaVRWZWNzVlhueGdhRFM1?= =?utf-8?B?bUZQOGNKZXNvcDd3bzFXQ2hRc2hsM0hCaFROSzRjbnVMVElucVh0QU9yWmZN?= =?utf-8?B?WXpGVjBlS2dvbVU1TGhIYk8wc3o1eE5weXNzZlAwUTdqUHlqcUhnaVlKNXdF?= =?utf-8?B?Tzh0MCtLTEpGVXhUN1ZOS3JJTjR1cFpYQ0ppeDNNdFhyY1lLZ29URGZ4V2tS?= =?utf-8?B?ejdTNUFRZ1AxSURFRllWcWpGdmVMWGM5UC9jckRxb3BOTUhSUTA3cHRIVEwz?= =?utf-8?B?dnRuS3NwZG5qZVlFTjZnRDRXRXIzYWljYlVYNXJZbnhFRzFUaVVoZ1QwdEpy?= =?utf-8?B?eFEzK1oybU5rMWNBZ2JMTVVHMVozYmc5STBZS05IQ1lYcUFWSXc3QnlTdjVs?= =?utf-8?B?cGNlQ0FkcFBYUnBoZmpqd2oxaEtPbTdGTVhyazAwUzdx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ME4yNVdKZ2xIMUg4cExx?= =?utf-8?B?VHA4bm1FQ05ncGR2SnQzM1N1QXJkeXNvZFI1aXVDT1FraTFoQVppbVNINWx3?= =?utf-8?B?MFQxcHMxYzFnc0JNaWdxeS9rY2YxYlY0UXI3bWJiVHRWcW16Sk1zZndCSkRt?= =?utf-8?B?bGdSWTVCamo0QnpxTkFxQkg1KzNTY2VuMW5obEhVMVlseUpoZHdVL3pHQW1u?= =?utf-8?B?Z1hxVDB0cXU4bEk2TUg2ZFJlMisrNlMwbXZ4amM3YXdFRy9WbEloV2wzZEhE?= =?utf-8?B?cFFEQ1NodnVVWjJVeEMrVUxibWt0c0RPelZHTlJic3RJUGFaNStiS2pKZFlN?= =?utf-8?B?RitVQk0vMHFYNHpsaWJ0d2lvSk83eVFIYTZnYTFSSmtLaFpseFJYRnVxWk0v?= =?utf-8?Q?pSI/dJtXsF0Ac?= X-Forefront-Antispam-Report: CIP:192.176.1.74; CTRY:SE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:oa.msg.ericsson.com; PTR:office365.se.ericsson.net; CAT:NONE; SFS:(13230031)(82310400014)(376005)(36860700004)(1800799015); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Apr 2024 10:05:54.5906 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 71bfe9dc-cc0a-41af-35e5-08dc68fd1fa0 X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=92e84ceb-fbfd-47ab-be52-080c6b87953f; Ip=[192.176.1.74]; Helo=[oa.msg.ericsson.com] X-MS-Exchange-CrossTenant-AuthSource: AMS1EPF00000042.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR07MB7859 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Add bit test/set/clear/assign functions which prevents certain compiler optimizations and guarantees that program-level memory loads and/or stores will actually occur. These functions are useful when interacting with memory-mapped hardware devices. The "once" family of functions does not promise atomicity and provides no memory ordering guarantees beyond the C11 relaxed memory model. RFC v3: * Work around lack of C++ support for _Generic (Tyler Retzlaff). Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup Acked-by: Tyler Retzlaff --- lib/eal/include/rte_bitops.h | 195 +++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 9d426f1602..f77bd83e97 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -224,6 +224,177 @@ extern "C" { uint32_t *: __rte_bit_flip32, \ uint64_t *: __rte_bit_flip64)(addr, nr) +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Generic selection macro to test exactly once the value of a bit in + * a 32-bit or 64-bit word. The type of operation depends on the type + * of the @c addr parameter. + * + * This function is guaranteed to result in exactly one memory load + * (e.g., it may not be eliminate or merged by the compiler). + * + * \code{.c} + * rte_bit_once_set(addr, 17); + * if (rte_bit_once_test(addr, 17)) { + * ... + * } + * \endcode + * + * In the above example, rte_bit_once_set() may not be removed by + * the compiler, which would be allowed in case rte_bit_set() and + * rte_bit_test() was used. + * + * \code{.c} + * while (rte_bit_once_test(addr, 17); + * ; + * \endcode + * + * In case rte_bit_test(addr, 17) was used instead, the resulting + * object code could (and in many cases would be) replaced with + * the equivalent to + * \code{.c} + * if (rte_bit_test(addr, 17)) { + * for (;;) // spin forever + * ; + * } + * \endcode + * + * rte_bit_once_test() does not give any guarantees in regards to + * memory ordering or atomicity. + * + * The regular bit set operations (e.g., rte_bit_test()) should be + * preferred over the "once" family of operations (e.g., + * rte_bit_once_test()) if possible, since the latter may prevent + * optimizations crucial for run-time performance. + * + * @param addr + * A pointer to the word to query. + * @param nr + * The index of the bit. + * @return + * Returns true if the bit is set, and false otherwise. + */ + +#define rte_bit_once_test(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_test32, \ + uint64_t *: __rte_bit_once_test64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Set bit in word exactly once. + * + * Set bit specified by @c nr in the word pointed to by @c addr to '1' + * exactly once. + * + * This function is guaranteed to result in exactly one memory load + * and exactly one memory store, *or* an atomic bit set operation. + * + * See rte_bit_test_once32() for more information and uses cases for + * the "once" class of functions. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ + +#define rte_bit_once_set(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_set32, \ + uint64_t *: __rte_bit_once_set64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Clear bit in word exactly once. + * + * Set bit specified by @c nr in the word pointed to by @c addr to '0' + * exactly once. + * + * This function is guaranteed to result in exactly one memory load + * and exactly one memory store, *or* an atomic bit clear operation. + * + * See rte_bit_test_once32() for more information and uses cases for + * the "once" class of functions. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ +#define rte_bit_once_clear(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_clear32, \ + uint64_t *: __rte_bit_once_clear64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Assign a value to bit in a word exactly once. + * + * Set bit specified by @c nr in the word pointed to by @c addr to the + * value indicated by @c value exactly once. + * + * This function is guaranteed to result in exactly one memory load + * and exactly one memory store, *or* an atomic bit clear operation. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param value + * The new value of the bit - true for '1', or false for '0'. + */ +#define rte_bit_once_assign(addr, nr, value) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_assign32, \ + uint64_t *: __rte_bit_once_assign64)(addr, nr, value) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Flip bit in word, reading and writing exactly once. + * + * Change the value of a bit to '0' if '1' or '1' if '0' in a 32-bit + * or 64-bit word. The type of operation depends on the type of the @c + * addr parameter. + * + * This function is guaranteed to result in exactly one memory load + * and exactly one memory store, *or* an atomic bit flip operation. + * + * See rte_bit_test_once32() for more information and uses cases for + * the "once" class of functions. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ +#define rte_bit_once_flip(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_flip32, \ + uint64_t *: __rte_bit_once_flip64)(addr, nr) + #define __RTE_GEN_BIT_TEST(family, fun, qualifier, size) \ __rte_experimental \ static inline bool \ @@ -296,6 +467,18 @@ __RTE_GEN_BIT_CLEAR(, clear,, 64) __RTE_GEN_BIT_ASSIGN(, assign,, 64) __RTE_GEN_BIT_FLIP(, flip,, 64) +__RTE_GEN_BIT_TEST(once_, test, volatile, 32) +__RTE_GEN_BIT_SET(once_, set, volatile, 32) +__RTE_GEN_BIT_CLEAR(once_, clear, volatile, 32) +__RTE_GEN_BIT_ASSIGN(once_, assign, volatile, 32) +__RTE_GEN_BIT_FLIP(once_, flip, volatile, 32) + +__RTE_GEN_BIT_TEST(once_, test, volatile, 64) +__RTE_GEN_BIT_SET(once_, set, volatile, 64) +__RTE_GEN_BIT_CLEAR(once_, clear, volatile, 64) +__RTE_GEN_BIT_ASSIGN(once_, assign, volatile, 64) +__RTE_GEN_BIT_FLIP(once_, flip, volatile, 64) + /*------------------------ 32-bit relaxed operations ------------------------*/ /** @@ -991,6 +1174,12 @@ rte_log2_u64(uint64_t v) #undef rte_bit_assign #undef rte_bit_flip +#undef rte_bit_once_test +#undef rte_bit_once_set +#undef rte_bit_once_clear +#undef rte_bit_once_assign +#undef rte_bit_once_flip + #define __RTE_BIT_OVERLOAD_SZ_2(fun, qualifier, size, arg1_type, arg1_name) \ static inline void \ rte_bit_ ## fun(qualifier uint ## size ## _t *addr, \ @@ -1040,6 +1229,12 @@ __RTE_BIT_OVERLOAD_2(clear,, unsigned int, nr) __RTE_BIT_OVERLOAD_3(assign,, unsigned int, nr, bool, value) __RTE_BIT_OVERLOAD_2(flip,, unsigned int, nr) +__RTE_BIT_OVERLOAD_2R(once_test, const volatile, bool, unsigned int, nr) +__RTE_BIT_OVERLOAD_2(once_set, volatile, unsigned int, nr) +__RTE_BIT_OVERLOAD_2(once_clear, volatile, unsigned int, nr) +__RTE_BIT_OVERLOAD_3(once_assign, volatile, unsigned int, nr, bool, value) +__RTE_BIT_OVERLOAD_2(once_flip, volatile, unsigned int, nr) + #endif #endif /* _RTE_BITOPS_H_ */ -- 2.34.1