Let me try to pitch the motivation first:
In the case of NUMA-aware shrinker, memory pressure may lead to splitting and freeing subpages within a THP, irrespective of whether the page belongs to the memcg that is under memory pressure. THP sharing between memcgs is not a pre-condition for above to happen.
Let's consider two memcgs: memcg-A and memcg-B. Say memcg-A is under memory pressure that is hitting its limit. If this memory pressure invokes the shrinker (non-memcg-aware) and splits pages from memcg-B queued for deferred splits, then that won't reduce memcg-A's usage. It will reduce memcg-B's usage. Also, why should memcg-A's memory pressure reduce memcg-B's usage.
By making this shrinker memcg-aware, we can invoke respective memcg shrinkers to handle the memory pressure. Furthermore, with this approach we can isolate the THPs of other memcg(s) (not under memory pressure) from premature splits. Isolation aids in reducing performance impact when we have several memcgs on the same machine.
Regarding ifdef ugliness: I get your point and agree with you on that. I think I can do a better job at restricting the ugliness, will post another version.
Thanks,
-Neha Agarwal