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 BFF3243F65; Thu, 2 May 2024 08:07:53 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CEC93402E1; Thu, 2 May 2024 08:07:35 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2046.outbound.protection.outlook.com [40.107.20.46]) by mails.dpdk.org (Postfix) with ESMTP id 41E61402C5 for ; Thu, 2 May 2024 08:07:32 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WmwSslY2LfGBsbjmSEGE60YpyXYIqnjkXb54s7Qcztqx0K7vOGA/n4S1rsjP56tZXAh2nYPZsnCSt0dedkX74PRlE5B38sBUWTkc7TBtHLp1pbAkJPA9TMIztxrOHfG+W6QH5oby/BW2wc7rRvrgrKw2EYSUHew+T4AIn6C1emQlC25p14HwmAeOggdvmoc0SnKwrUbkZIN0brF2FZNae2oE5gbWNKAYVcrG9ZQee1AgMRfx0287rzMw4Zfhpsg3HrUzccP0AMehXQ19UlK3ntjBpnjbonVAV1dDgN1Si9BICyrjJ16XuTZmnkObZuxA8iK9MMCtOZI5xMJoLu1exQ== 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=zmUfRKzoqHSFuLBu20WiDhlgo8uRunsquRSfzUxzP2M=; b=KWKKMrHP6XtwQ56Jr61NzqtfGtzDOuFKwyKgLcGEeg3jvszl1s2CJCOwT03j16ghy0o25DImu6FTn/OQGd+tg4xDRgxYg96BenoIhKNQzI9hOfOZZSNEFsCPYGFmfRQ8APjEm3cCWBMGEJC3Nqi3k4fEqwy2SjVZAzrsyWZZQTtA8I/crisl4VYrl/LrQjeelk7qg1tv7o6dWyCWYnEOSKKlRGKKxRyp3DaeN2mfzY0GXkQCFCSykExXOBOdxvkt/qj6l2/GGgCdJwwk9jPF2PYdwUaocN0PN88ElHqcXNP91afEg8CzkpmOcb2+8WhRFYn3poxr77OGRbU5AfhOog== 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=zmUfRKzoqHSFuLBu20WiDhlgo8uRunsquRSfzUxzP2M=; b=SQdbOeOZZKxYYBV4yJRbpK4/c5dTMbSt1ocERu+PQgR2kfc0RqoHcy3XT6w79OC3rJHoNUT312LGf3YnvnkiF+il/T3DbxocVWtiS9Ndeolj99km2w1utzJpbZer84zyqLvcMjdLddnjIcFgxJnqT+5XFcUFzTdykRMF8z8ZgoRddiDODiUAnO9zRCju8PpZWb23JvyjWuuUO5gz1IDQ8f4KOztsFuLLZCW+rjw9eUJ30ETZx5zL8IrZDI8d1Efa3/Tryx+BDiMd8D5d6Xxz0BmZps+185oA1/bBIMut/Qn1JytFYb/ziXeV6kiaCR8HDzXEOSVkpbQRjdFO7BULgQ== Received: from DUZPR01CA0073.eurprd01.prod.exchangelabs.com (2603:10a6:10:3c2::11) by AS5PR07MB9820.eurprd07.prod.outlook.com (2603:10a6:20b:651::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.26; Thu, 2 May 2024 06:07:31 +0000 Received: from DB5PEPF00014B9E.eurprd02.prod.outlook.com (2603:10a6:10:3c2:cafe::82) by DUZPR01CA0073.outlook.office365.com (2603:10a6:10:3c2::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.29 via Frontend Transport; Thu, 2 May 2024 06:07:30 +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 DB5PEPF00014B9E.mail.protection.outlook.com (10.167.8.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.18 via Frontend Transport; Thu, 2 May 2024 06:07:30 +0000 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.64) with Microsoft SMTP Server id 15.2.1544.9; Thu, 2 May 2024 08:07:30 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id E20C5380061; Thu, 2 May 2024 08:07:29 +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 v6 3/6] eal: add exactly-once bit access functions Date: Thu, 2 May 2024 07:57:03 +0200 Message-ID: <20240502055706.112443-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240502055706.112443-1-mattias.ronnblom@ericsson.com> References: <20240430120810.108928-2-mattias.ronnblom@ericsson.com> <20240502055706.112443-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: DB5PEPF00014B9E:EE_|AS5PR07MB9820:EE_ X-MS-Office365-Filtering-Correlation-Id: 6a0b8216-8e2b-4f67-0ea7-08dc6a6e2698 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|376005|36860700004|1800799015; X-Microsoft-Antispam-Message-Info: =?utf-8?B?aTVhUnhSQ0t1aDB3WUJYQjVZNFVRUkROUHkrQ1AvVWxtSldkSE5qOGF0S29L?= =?utf-8?B?cW9idFpBSTI2clVFUHQvSmx0V1dDZTlPS1RHVytFQ2lES25QWUI5UHNqRDdw?= =?utf-8?B?U1RrV1lodFUzV1QwZlUyYWFIdjlEc243MnRjTE8xandzMkhvN0JtTGpFUWxS?= =?utf-8?B?eGFRNkJIK3diYkFwU2s4Sk5kakRRS3F5b3NvaUVFaGhWYWRGblRuc3V5ZXRX?= =?utf-8?B?UHlRWGNPczNQcmx1R2FqWHNzOXdCRlo0STV0N2pweDdTRWxxL3pFWHBybFRr?= =?utf-8?B?Wnc0K1NCSjVyWmdKd1dRRWZRWDdSTUFHSzhFV29ncllIVDV5Z0FhR2JzaStq?= =?utf-8?B?RlYxNFNSdVo2UjdLcXVtM29YYXFiNjlXSU1sZW1jbG43SXpQRk8zeGlaczlj?= =?utf-8?B?OE5jTnVYaG4rZWpGb1ZyMHhPczliQUt5Q2U5ajZnMDN2S25vNjBsMFpTS2dl?= =?utf-8?B?b3ZjR01BTXR4L3JrTE1xYTRTdmt3QkZmVzRJbG5STUV6VTBMcDJHM0tKYVVp?= =?utf-8?B?c210KzdIM25PRmU0VVQreG1NZlRIeDcyQlpvSENXVDNKYlRxd2tDeWNmY3VL?= =?utf-8?B?K0NTeHFnZDBMUTU0SU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VBbms3NytpbjMrRDRybU5NSUQ5bFRQQ25Oc3g3N2MyTUtjall6WjVsOWxx?= =?utf-8?B?N1FqYUIrSCt3QTdGaDRtR2FGOFBqTndFMmJIN0FSWDZhcU1xanBTdStuclhH?= =?utf-8?B?ak9uV28wL09YVGxXUjA0MFM2RWQ1Q0NVMDFhSnJ1Rk1rb3pDQ3lHUkJuZ3F3?= =?utf-8?B?bHI2QW1vc1NDWG9mRkhVNHR0R01YSmZvNzVRa0hKNUphNW1tU0cwb0RoZ1JB?= =?utf-8?B?akdBQ09HOUZibHVKdmRCNzV0QTM1aXF2U2YyTFNyTWhoU0dVY1ZVSEFWWWZ5?= =?utf-8?B?RVJ6TXdxQUxKdUo5RE5FUE96S0txVXlmaDNEb092czFDMDNFSTVjYVRIWndk?= =?utf-8?Q?TJqS54mNcA6Cr?= 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)(376005)(36860700004)(1800799015); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 May 2024 06:07:30.5432 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6a0b8216-8e2b-4f67-0ea7-08dc6a6e2698 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: DB5PEPF00014B9E.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS5PR07MB9820 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 test/set/clear/assign/flip 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 v6: * Have rte_bit_once_test() accept const-marked bitsets. 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 | 197 +++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 3297133e22..caec4f36bb 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -226,6 +226,179 @@ 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, \ + const uint32_t *: __rte_bit_once_test32, \ + uint64_t *: __rte_bit_once_test64, \ + const 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 \ @@ -298,6 +471,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 ------------------------*/ /** @@ -993,6 +1178,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, \ @@ -1042,6 +1233,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