Skip to content

Check MADV_DONTNEED (for jemalloc), maybe broken under qemu#15590

Merged
alexey-milovidov merged 6 commits intoClickHouse:masterfrom
azat:check-MADV_DONTNEED-for-jemalloc
Oct 5, 2020
Merged

Check MADV_DONTNEED (for jemalloc), maybe broken under qemu#15590
alexey-milovidov merged 6 commits intoClickHouse:masterfrom
azat:check-MADV_DONTNEED-for-jemalloc

Conversation

@azat
Copy link
Copy Markdown
Member

@azat azat commented Oct 3, 2020

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Check MADV_DONTNEED (for jemalloc), maybe broken under qemu

Detailed description / Documentation draft:
jemalloc relies on working MADV_DONTNEED (that fact that after
madvise(MADV_DONTNEED) returns success, after subsequent access to those
pages they will be zeroed).
However qemu does not support this, yet 1, and you can get very tricky
assert if you will run clickhouse-server under qemu:

<jemalloc>: ../contrib/jemalloc/src/extent.c:1195: Failed assertion: "p[i] == 0"

But after this patch you will get pretty error:

$ qemu-x86_64-static programs/clickhouse
MADV_DONTNEED does not zeroed page. jemalloc will be broken

P.S. I also checked clickhouse with patched qemu, that returns ENOSYS in the linux-user layer, and jemalloc assert no more triggered.

Refs: https://gist.github.com/azat/12ba2c825b710653ece34dba7f926ece

Details

HEAD:

  • a139a594bfc6b7709c8640aec0e68c0673fcdcaa (before strlen fix)
  • 152922998bc80cde5a44dd84d70a001cd5feb05a (after strlen fix)
  • 0a9268cb20673e774355d7d200374aa6c41ef339 (w/ bugprone-macro-parentheses)

@robot-clickhouse robot-clickhouse added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 3, 2020
@azat azat marked this pull request as draft October 3, 2020 22:58
@azat azat force-pushed the check-MADV_DONTNEED-for-jemalloc branch from f3551dd to 3301dde Compare October 3, 2020 23:03
@azat azat marked this pull request as ready for review October 3, 2020 23:03
@azat azat force-pushed the check-MADV_DONTNEED-for-jemalloc branch 2 times, most recently from 46c2ac2 to a139a59 Compare October 4, 2020 00:01
@alexey-milovidov alexey-milovidov self-assigned this Oct 4, 2020
azat added 3 commits October 4, 2020 11:20
jemalloc relies on working MADV_DONTNEED (that fact that after
madvise(MADV_DONTNEED) returns success, after subsequent access to those
pages they will be zeroed).
However qemu does not support this, yet [1], and you can get very tricky
assert if you will run clickhouse-server under qemu:

    <jemalloc>: ../contrib/jemalloc/src/extent.c:1195: Failed assertion: "p[i] == 0"

  [1]: https://patchwork.kernel.org/patch/10576637/

But after this patch you will get pretty error:

  $ qemu-x86_64-static programs/clickhouse
  MADV_DONTNEED does not zeroed page. jemalloc will be broken
@azat azat force-pushed the check-MADV_DONTNEED-for-jemalloc branch 3 times, most recently from 261d3e2 to 0a9268c Compare October 4, 2020 11:34
@azat azat force-pushed the check-MADV_DONTNEED-for-jemalloc branch from 0a9268c to 9e3ff34 Compare October 4, 2020 14:38
}
}
/// Macros to avoid using strlen(), since it may fail if SSE is not supported.
#define writeError(data) \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an idiom to write do {} while (0) to avoid issues when someone will write something like if (...) writeError(data);. I'll correct it just in case...

@alexey-milovidov alexey-milovidov merged commit 4d427e8 into ClickHouse:master Oct 5, 2020
@abyss7
Copy link
Copy Markdown
Contributor

abyss7 commented Oct 5, 2020

Do I understand correctly that this PR was intentionally merged in a broken state?

@azat
Copy link
Copy Markdown
Member Author

azat commented Oct 5, 2020

@abyss7 what is broken here? Oh I see, some changes after breaks it, and has been fixed shortly in 674d8d4

@azat azat deleted the check-MADV_DONTNEED-for-jemalloc branch October 5, 2020 20:19
azat added a commit to azat/ClickHouse that referenced this pull request Dec 16, 2020
azat added a commit to azat/ClickHouse that referenced this pull request Dec 19, 2020
qemu does not support MADV_DONTNEED, and by not support it simply ignore
it (i.e. return 0 -- no error).

This issue has been "fixed" in ClickHouse#15590, however it just
terminates the process, and completely breaks clickhouse under qemu
(see also ClickHouse#15174).

But there is no need in such strong protection, we can stop using
madvise in jemalloc if MADV_DONTNEED does not work properly.
And this is what ClickHouse#18169 was tried to do (by override madvise), however
this will break sanitizers, at least TSAN and UBSAN.
The problem there is that sanitizers initialization code uses madvise
(and there is no way to turn this off with TSAN_OPTIONS) and overwritten
madvise function will have sanitizers traits (__tsan_func_entry), while
TSAN is not ready for this, and eventually it SIGSEGV.
Interesting thing is that in the recent clang-12, madvise was replaced
with direct syscall [1].

  [1]: llvm/llvm-project@9f8c403

But it is better to make clickhouse compatible with clang < 12 too, so
instead of override madvise completely, the runtime check was moved into
the jemalloc code [2].

  [2]: ClickHouse/jemalloc#1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants