← Back to Pull requests

Provide hugepage advice only when the THP enabled setting is madvise #27

Open
lexprfuncall opened this pull request with 3 lines diff across 1 files
View on GitHub
lexprfuncall lexprfuncall commented on Aug 11, 2025 20:36 UTC
Author

When THP is enabled system-wide, there is no need to provide hugepage
advice to enable THP for a region of memory. On the other hand, when
the THP defragmentation mode is defer+madvise, hugepage advice can
result in unwanted application stalls to compact and defragment memory
if THP is not immediately available for allocation.

Diff: 3 lines (+2 / -1) across 1 files Reviewer: WenleiHe Review duration: 610648m
All Vouch review confidence checks passed
Residual risk 0.02 = Change Risk (0.18) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.18 · 3 lines diff across 1 files
0.18
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 5 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.083
Real PR Conversation Timeline
10 items
WenleiHe WenleiHe commented on code on Aug 12, 2025 07:15 UTC
Code Review
src/hpa.c:1
@@ -420,6 +420,17 @@ hpa_process_madvise_max_iovec_len(void) {
 	    : opt_process_madvise_max_batch;
 }
 
+/*
+ * Returns true if the promotion to and demotion of hugepages needs to be done
+ * explicitly rather than happening automatically.
+ */
+static bool
+hpa_explicit_hugify_and_dehugfiy(void) {
+	return !(opt_hpa_opts.experimental_thp_always_no_madvise &&
+		init_system_thp_mode == thp_mode_always &&
+		 opt_thp == thp_mode_default);

If opt_thp is thp_mode_always or thp_mode_never, we should not explicitly hugify either?

should this be something like

opt_thp == thp_mode_default && !(opt_hpa_opts.experimental_thp_always_no_madvise && 
init_system_thp_mode == thp_mode_always)

WenleiHe WenleiHe commented on code on Aug 12, 2025 07:17 UTC
Code Review
src/hpa.c:1
@@ -435,7 +446,7 @@ hpa_purge_actual_unlocked(
 		hpdata_t *to_purge = batch[i].hp;
 
 		/* Actually do the purging, now that the lock is dropped. */
-		if (batch[i].dehugify) {
+		if (hpa_explicit_hugify_and_dehugfiy() && batch[i].dehugify) {

could be a separate flag/PR, but i thought for experiments we also wanted to allow directly purging without dehugify (regardless of init_system_thp_mode)?

WenleiHe WenleiHe commented on code on Aug 12, 2025 07:25 UTC
Code Review
src/hpa.c:1
@@ -435,7 +446,7 @@ hpa_purge_actual_unlocked(
 		hpdata_t *to_purge = batch[i].hp;
 
 		/* Actually do the purging, now that the lock is dropped. */
-		if (batch[i].dehugify) {
+		if (hpa_explicit_hugify_and_dehugfiy() && batch[i].dehugify) {
guangli-dai guangli-dai commented on code on Aug 14, 2025 23:29 UTC
Code Review
include/jemalloc/internal/hpa_opts.h:1
@@ -59,6 +59,12 @@ struct hpa_shard_opts_s {
 	 * Maximum number of hugepages to purge on each purging attempt.
 	 */
 	ssize_t experimental_max_purge_nhp;
+
+	/*
+	 * Whether to skip advising memory regions with MADV_{NO,}HUGEPAGE if
+	 * the transparent hugepages enabled value is set to always.
+	 */
+	bool experimental_thp_always_no_madvise;

The default value of this should be added in the HPA_SHARD_OPTS_DEFAULT below since it is used to initialize this struct.

guangli-dai guangli-dai commented on code on Aug 14, 2025 23:35 UTC
Code Review
src/ctl.c:1
@@ -106,6 +106,7 @@ CTL_PROTO(opt_hpa_hugify_delay_ms)
 CTL_PROTO(opt_hpa_hugify_sync)
 CTL_PROTO(opt_hpa_min_purge_interval_ms)
 CTL_PROTO(opt_experimental_hpa_max_purge_nhp)
+CTL_PROTO(opt_experimental_thp_always_no_madvise)

Under what scenarios do we still want to madvise hugepage when we have thp:always?

Besides, we still madvise MADV_DONTNEED even with this flag and we have already removed dehugifying calls, so maybe opt_experimental_thp_always_no_madv_hugepage is more appropriate?

lexprfuncall lexprfuncall commented on code on Aug 15, 2025 02:03 UTC
Code Review
src/ctl.c:1
@@ -106,6 +106,7 @@ CTL_PROTO(opt_hpa_hugify_delay_ms)
 CTL_PROTO(opt_hpa_hugify_sync)
 CTL_PROTO(opt_hpa_min_purge_interval_ms)
 CTL_PROTO(opt_experimental_hpa_max_purge_nhp)
+CTL_PROTO(opt_experimental_thp_always_no_madvise)

Well, if the user sets thp:{always,never} then the extent hooks will always advise regions it allocates as MADV_{,NO}HUGEPAGE. What does this have to do with HPA? As far as I can tell, nothing, because when HPA allocates memory, it does not get memory from the extent hooks. Instead, it calls pages_map directly. (That might make sense to change if we get rid of PAC but that is a change for another day.)

As for your second question, I think the flag name could use improvement and might be better named experimental_hpa_(something) and not mention "thp" since that is easily confused with the thp option. I could also pull opt_thp == default out from the conjunction that says whether to suppress the use of madvise when hugifying.

So, how about the following follow-up changes?

1) Rename the flag to experimental_hpa_no_explicit_hugify (not mentioning MADV_HUGEPAGE avoids excluding MADV_COLLAPSE)
2) Remove the check for opt_thp == default
3) Rename the predicate hpa_explicit_hugify since we do not explicitly dehugify now that my other PR landed

WDYT?

guangli-dai guangli-dai commented on code on Aug 15, 2025 20:13 UTC
Code Review
src/ctl.c:1
@@ -106,6 +106,7 @@ CTL_PROTO(opt_hpa_hugify_delay_ms)
 CTL_PROTO(opt_hpa_hugify_sync)
 CTL_PROTO(opt_hpa_min_purge_interval_ms)
 CTL_PROTO(opt_experimental_hpa_max_purge_nhp)
+CTL_PROTO(opt_experimental_thp_always_no_madvise)

Discussed offline. While the change is 1. no perf impact on systems setting kernel thp to madvise and 2. the perf impact should be a win for kernel thp always/never, there is not a high necessity in maintaining a separate runtime options here. We originally wanted to have this option mainly to benefit testing and rollout, but it is a burden not worthy, so let's also remove the opt_experimental_thp_always_no_madvise.

Besides, I also agree to remove the check for opt_thp == default. For the long term, this option controls thp usage in pac and should not be used in HPA. Once we have HPA well-prepared, all traffic that wants to use thp should go through HPA instead of setting opt_thp.

WenleiHe WenleiHe commented on code on Aug 22, 2025 04:22 UTC
Code Review
src/ctl.c:1
@@ -106,6 +106,7 @@ CTL_PROTO(opt_hpa_hugify_delay_ms)
 CTL_PROTO(opt_hpa_hugify_sync)
 CTL_PROTO(opt_hpa_min_purge_interval_ms)
 CTL_PROTO(opt_experimental_hpa_max_purge_nhp)
+CTL_PROTO(opt_experimental_thp_always_no_madvise)

Under what scenarios do we still want to madvise hugepage when we have thp:always?

I thought we at least want a switch for now for experimentation purpose?

We originally wanted to have this option mainly to benefit testing and rollout, but it is a burden not worthy, so let's also remove the opt_experimental_thp_always_no_madvise.

We discussed that we want to improve hugificaiton heuristic to be more compatible with kernel thp heuristic (i.e. taking pressure into account along with others), wouldn't we want to experiment explicit madvise on top of thp=always for our hugification heuristic improvements, compare that against not requesting hugification at all.

lexprfuncall lexprfuncall commented on code on Aug 22, 2025 06:16 UTC
Code Review
src/ctl.c:1
@@ -106,6 +106,7 @@ CTL_PROTO(opt_hpa_hugify_delay_ms)
 CTL_PROTO(opt_hpa_hugify_sync)
 CTL_PROTO(opt_hpa_min_purge_interval_ms)
 CTL_PROTO(opt_experimental_hpa_max_purge_nhp)
+CTL_PROTO(opt_experimental_thp_always_no_madvise)

I thought we at least want a switch for now for experimentation purpose?

That was the original plan and it's what I suggested in a follow-up comment above. However, the feedback I got afterward was to disable the advice when THP is enabled system-wide since, as far as we could tell, with that setting the advice is a no-op.

That said, further research revealed that the advice does have a surprising effect when the defrag setting is defer+madvise but none of the systems we care about use that setting, at least at the moment.

We discussed that we want to improve hugificaiton heuristic to be more compatible with kernel thp heuristic (i.e. taking pressure into account along with others), wouldn't we want to experiment explicit madvise on top of thp=always for our hugification heuristic improvements, compare that against not requesting hugification at all.

Yes, I think we do want to adjust the heuristics to be more in line with the current behavior of the kernel. However, the no-op nature of MADV_HUGEPAGE when THP is enabled system-wide suggests it is unlikely to be a factor in getting there.

What the current data tells us is that when configured to hugify solely by advising, there is a high percentage of full pageslabs that are never advised. There could be a bug, or bad thresholds, or it could be that we are not budgeting enough foreground or background explicit hugification work. When we get to the bottom of that issue and observe the system with a higher percentage of advised memory, I think the specific mechanisms to use to promote hugification will become clear.

Reviewers
Assignees
lexprfuncall lexprfuncall
Labels
risk: low confidence: 89.3% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 18.3% (LOW) · Higher = larger blast radius
18.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 89.3% · Lower = rushed or shallow review
89.3%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 2% · Safe to merge
2%