Clang expects 8-byte alignment for some 64-bit atomic operations
in some 32-bit targets. Native instruction lock cmpxchg8b (for x86)
should only require 4-byte alignment.
As a result laterst mariadb fails to build with clang on 32bit/x86 architecture because it
can not find the missing symbol
undefined reference to `__atomic_fetch_or_8'
here is a shortened testcase that can reproduce the problem with clang
Thank you. Starting with MariaDB Server 10.4, the my_atomic wrappers should not be used in C++ code, but the C++11 std::atomic should be used instead.
I am curious about the claim that the Intel Pentium lock cmpxchg8b (infamous of the f00f bug) would only require 4-byte alignment. Is that really so? The external data bus of the Pentium is 64 bits wide. How would a misaligned lock cmpxchg8b work in a multiprocessor environment? I found a claim that the Pentium 75 would support multiprocessing with up to 2 CPUs.
Also, the suggested patch is checking for the preprocessor symbol _GNUC. All clang versions that I have used (I think, between versions 4 and 14) predefine GNUC_ as 4. So, the patch would affect both gcc and clang.
If I understood the patch correctly, it would explicitly declare 8-byte alignment for the 64-bit atomic types.
As far as I can tell by executing the following:
git grep 'my_atomic_.*long'
the macros are unused in MariaDB Server 10.4 and later. In MariaDB Server 10.2 and 10.3 (which are still not EOL) some of those macros are invoked by InnoDB. I do not think that it is necessary to touch those old versions, unless someone can demonstrate a failure.
I think that the easiest fix of this is to remove the unused macros for 64-bit atomics.
Marko Mäkelä
added a comment - Thank you. Starting with MariaDB Server 10.4, the my_atomic wrappers should not be used in C++ code, but the C++11 std::atomic should be used instead.
I am curious about the claim that the Intel Pentium lock cmpxchg8b (infamous of the f00f bug ) would only require 4-byte alignment. Is that really so? The external data bus of the Pentium is 64 bits wide. How would a misaligned lock cmpxchg8b work in a multiprocessor environment? I found a claim that the Pentium 75 would support multiprocessing with up to 2 CPUs.
Also, the suggested patch is checking for the preprocessor symbol _ GNUC . All clang versions that I have used (I think, between versions 4 and 14) predefine GNUC _ as 4. So, the patch would affect both gcc and clang .
If I understood the patch correctly, it would explicitly declare 8-byte alignment for the 64-bit atomic types.
As far as I can tell by executing the following:
git grep 'my_atomic_.*long'
the macros are unused in MariaDB Server 10.4 and later. In MariaDB Server 10.2 and 10.3 (which are still not EOL) some of those macros are invoked by InnoDB. I do not think that it is necessary to touch those old versions, unless someone can demonstrate a failure.
I think that the easiest fix of this is to remove the unused macros for 64-bit atomics.
The 64-bit atomic wrappers that these macros refer to are only being invoked by static member functions of class PFS_atomic, defined in the header file storage/perfschema/pfs_atomic.h. That file should be removed and the PFS_atomic replaced with something based on C++11 std::atomic, perhaps Atomic_relaxed. That should allow all matches of
The 32-bit atomic wrappers are being used much more, and also they could be replaced with std::atomic in C++ code (but not C). That should be done in a separate ticket.
Marko Mäkelä
added a comment - The following definitions became unused in MDEV-17441 :
diff --git a/include/my_atomic.h b/include/my_atomic.h
index 81da9e35cf9..25ae772ebc3 100644
--- a/include/my_atomic.h
+++ b/include/my_atomic.h
@@ -115,22 +115,6 @@
#include "atomic/gcc_builtins.h"
#endif
-#if SIZEOF_LONG == 4
-#define my_atomic_addlong(A,B) my_atomic_add32((int32*) (A), (B))
-#define my_atomic_loadlong(A) my_atomic_load32((int32*) (A))
-#define my_atomic_loadlong_explicit(A,O) my_atomic_load32_explicit((int32*) (A), (O))
-#define my_atomic_storelong(A,B) my_atomic_store32((int32*) (A), (B))
-#define my_atomic_faslong(A,B) my_atomic_fas32((int32*) (A), (B))
-#define my_atomic_caslong(A,B,C) my_atomic_cas32((int32*) (A), (int32*) (B), (C))
-#else
-#define my_atomic_addlong(A,B) my_atomic_add64((int64*) (A), (B))
-#define my_atomic_loadlong(A) my_atomic_load64((int64*) (A))
-#define my_atomic_loadlong_explicit(A,O) my_atomic_load64_explicit((int64*) (A), (O))
-#define my_atomic_storelong(A,B) my_atomic_store64((int64*) (A), (B))
-#define my_atomic_faslong(A,B) my_atomic_fas64((int64*) (A), (B))
-#define my_atomic_caslong(A,B,C) my_atomic_cas64((int64*) (A), (int64*) (B), (C))
-#endif
-
#ifndef MY_MEMORY_ORDER_SEQ_CST
#define MY_MEMORY_ORDER_RELAXED
#define MY_MEMORY_ORDER_CONSUME
The 64-bit atomic wrappers that these macros refer to are only being invoked by static member functions of class PFS_atomic , defined in the header file storage/perfschema/pfs_atomic.h . That file should be removed and the PFS_atomic replaced with something based on C++11 std::atomic , perhaps Atomic_relaxed . That should allow all matches of
git grep 'my_atomic.*64'
to be removed.
khem , can you please submit a GitHub pull request for replacing the PFS_atomic and removing all 64-bit my_atomic functions?
The 32-bit atomic wrappers are being used much more, and also they could be replaced with std::atomic in C++ code (but not C). That should be done in a separate ticket.
Note: 64-bit atomic read-modify-write operations would map to loops around the venerable lock cmpxchg8b instruction that is known for the Pentium F00F bug and the reason why Windows XP would require some tweaks to run on the 80486. Therefore, I do not think that 64-bit read-modify-writes are possible on IA-32 without specifying at least -march=i586. Loads and stores seem to be possible by using some floating point instructions, such as fildll and fistpll, which I encountered the other day while diagnosing MDEV-33972.
Marko Mäkelä
added a comment - Note: 64-bit atomic read-modify-write operations would map to loops around the venerable lock cmpxchg8b instruction that is known for the Pentium F00F bug and the reason why Windows XP would require some tweaks to run on the 80486 . Therefore, I do not think that 64-bit read-modify-writes are possible on IA-32 without specifying at least -march=i586 . Loads and stores seem to be possible by using some floating point instructions, such as fildll and fistpll , which I encountered the other day while diagnosing MDEV-33972 .
The problem that is described in the Description is related to split locks, that is, using atomic operations on unaligned data, in the worst case spanning two cache lines. I might have expected the ABI to require uint64_t* parameters to point to aligned data, but x86 was traditionally very relaxed about this. Basically only some SIMD instructions really require strict alignment.
Marko Mäkelä
added a comment - - edited The problem that is described in the Description is related to split locks , that is, using atomic operations on unaligned data, in the worst case spanning two cache lines. I might have expected the ABI to require uint64_t* parameters to point to aligned data, but x86 was traditionally very relaxed about this. Basically only some SIMD instructions really require strict alignment.
MOV can't be used with LOCK, so I guess atomic::store/load might be broken for unaligned uint64_t.
More interesting insights are in godbolt.org/z/K8sf5hf6P
Clang/gcc/icc use XCHG without LOCK
MSVC 17 uses movq xmm, which seems the most interesting and promising
Nikita Malyavin
added a comment - - edited According to https://www.felixcloutier.com/x86/lock#:~:text=If%20the%20LOCK%20prefix%20is%20used%20with%20an%20instruction%20not%20listed
MOV can't be used with LOCK, so I guess atomic::store/load might be broken for unaligned uint64_t .
More interesting insights are in godbolt.org/z/K8sf5hf6P
Clang/gcc/icc use XCHG without LOCK
MSVC 17 uses movq xmm, which seems the most interesting and promising
People
Nikita Malyavin
KHEM RAJ
Votes:
0Vote for this issue
Watchers:
4Start watching this issue
Dates
Created:
Updated:
Resolved:
Git Integration
Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.
{"report":{"fcp":777.7000002861023,"ttfb":220.30000019073486,"pageVisibility":"visible","entityId":109157,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":1,"journeyId":"9a1c968c-ab78-46e6-aa53-1982fef1c0e0","navigationType":0,"readyForUser":857.5,"redirectCount":0,"resourceLoadedEnd":524,"resourceLoadedStart":227.80000019073486,"resourceTiming":[{"duration":12.199999809265137,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":227.80000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":227.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":240,"responseStart":0,"secureConnectionStart":0},{"duration":12.199999809265137,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":228.2000002861023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":228.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":240.40000009536743,"responseStart":0,"secureConnectionStart":0},{"duration":81.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":228.40000009536743,"connectEnd":228.40000009536743,"connectStart":228.40000009536743,"domainLookupEnd":228.40000009536743,"domainLookupStart":228.40000009536743,"fetchStart":228.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":241.7000002861023,"responseEnd":310.2000002861023,"responseStart":276.7000002861023,"secureConnectionStart":228.40000009536743},{"duration":90.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":228.5,"connectEnd":228.5,"connectStart":228.5,"domainLookupEnd":228.5,"domainLookupStart":228.5,"fetchStart":228.5,"redirectEnd":0,"redirectStart":0,"requestStart":242.30000019073486,"responseEnd":319.09999990463257,"responseStart":280.30000019073486,"secureConnectionStart":228.5},{"duration":63.299999713897705,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":228.7000002861023,"connectEnd":228.7000002861023,"connectStart":228.7000002861023,"domainLookupEnd":228.7000002861023,"domainLookupStart":228.7000002861023,"fetchStart":228.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":245.59999990463257,"responseEnd":292,"responseStart":291.09999990463257,"secureConnectionStart":228.7000002861023},{"duration":68.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":228.90000009536743,"connectEnd":228.90000009536743,"connectStart":228.90000009536743,"domainLookupEnd":228.90000009536743,"domainLookupStart":228.90000009536743,"fetchStart":228.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":246.09999990463257,"responseEnd":297.09999990463257,"responseStart":296.40000009536743,"secureConnectionStart":228.90000009536743},{"duration":66.10000038146973,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":229.09999990463257,"connectEnd":229.09999990463257,"connectStart":229.09999990463257,"domainLookupEnd":229.09999990463257,"domainLookupStart":229.09999990463257,"fetchStart":229.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":246.2000002861023,"responseEnd":295.2000002861023,"responseStart":294.30000019073486,"secureConnectionStart":229.09999990463257},{"duration":16.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":229.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":229.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":245.80000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":66.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":229.40000009536743,"connectEnd":229.40000009536743,"connectStart":229.40000009536743,"domainLookupEnd":229.40000009536743,"domainLookupStart":229.40000009536743,"fetchStart":229.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":248.2000002861023,"responseEnd":296.30000019073486,"responseStart":295.40000009536743,"secureConnectionStart":229.40000009536743},{"duration":17,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":229.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":229.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":246.5,"responseStart":0,"secureConnectionStart":0},{"duration":50.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":229.59999990463257,"connectEnd":262.90000009536743,"connectStart":262.90000009536743,"domainLookupEnd":262.90000009536743,"domainLookupStart":262.90000009536743,"fetchStart":229.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":263.2000002861023,"responseEnd":280,"responseStart":276,"secureConnectionStart":262.90000009536743},{"duration":240.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":242.09999990463257,"connectEnd":242.09999990463257,"connectStart":242.09999990463257,"domainLookupEnd":242.09999990463257,"domainLookupStart":242.09999990463257,"fetchStart":242.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":396,"responseEnd":482.90000009536743,"responseStart":481.2000002861023,"secureConnectionStart":242.09999990463257},{"duration":263.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":260.09999990463257,"connectEnd":260.09999990463257,"connectStart":260.09999990463257,"domainLookupEnd":260.09999990463257,"domainLookupStart":260.09999990463257,"fetchStart":260.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":510.2000002861023,"responseEnd":524,"responseStart":522.7000002861023,"secureConnectionStart":260.09999990463257},{"duration":231.7999997138977,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":508.2000002861023,"connectEnd":508.2000002861023,"connectStart":508.2000002861023,"domainLookupEnd":508.2000002861023,"domainLookupStart":508.2000002861023,"fetchStart":508.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":700.7000002861023,"responseEnd":740,"responseStart":738.9000000953674,"secureConnectionStart":508.2000002861023},{"duration":179.80000019073486,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":754.0999999046326,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":754.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":933.9000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":225.09999990463257,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":777.4000000953674,"connectEnd":777.4000000953674,"connectStart":777.4000000953674,"domainLookupEnd":777.4000000953674,"domainLookupStart":777.4000000953674,"fetchStart":777.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":964.5999999046326,"responseEnd":1002.5,"responseStart":1001.7000002861023,"secureConnectionStart":777.4000000953674}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":35,"responseStart":220,"responseEnd":254,"domLoading":224,"domInteractive":937,"domContentLoadedEventStart":937,"domContentLoadedEventEnd":989,"domComplete":1306,"loadEventStart":1306,"loadEventEnd":1307,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":898.7000002861023},{"name":"bigPipe.sidebar-id.end","time":899.4000000953674},{"name":"bigPipe.activity-panel-pipe-id.start","time":899.5999999046326},{"name":"bigPipe.activity-panel-pipe-id.end","time":902.4000000953674},{"name":"activityTabFullyLoaded","time":1016.5}],"measures":[],"correlationId":"45eba066bab93f","effectiveType":"4g","downlink":9,"rtt":0,"serverDuration":117,"dbReadsTimeInMs":18,"dbConnsTimeInMs":27,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
I have attached a patch which fixes it.