On Thu, May 16, 2024 at 07:50:52PM +0800, Kairui Song wrote: > > > On 2024/4/18 22:20, Peng Zhang wrote: > > >> From: ZhangPeng <zhangpeng362@xxxxxxxxxx> > > >> > > >> Since commit f1a7941243c1 ("mm: convert mm's rss stats into > > >> percpu_counter"), the rss_stats have converted into percpu_counter, > > >> which convert the error margin from (nr_threads * 64) to approximately > > >> (nr_cpus ^ 2). However, the new percpu allocation in mm_init() causes a > > >> performance regression on fork/exec/shell. Even after commit > > >> 14ef95be6f55 > > >> ("kernel/fork: group allocation/free of per-cpu counters for mm > > >> struct"), > > >> the performance of fork/exec/shell is still poor compared to previous > > >> kernel versions. > > >> > > >> To mitigate performance regression, we delay the allocation of percpu > > >> memory for rss_stats. Therefore, we convert mm's rss stats to use > > >> percpu_counter atomic mode. For single-thread processes, rss_stat is in > > >> atomic mode, which reduces the memory consumption and performance > > >> regression caused by using percpu. For multiple-thread processes, > > >> rss_stat is switched to the percpu mode to reduce the error margin. > > >> We convert rss_stats from atomic mode to percpu mode only when the > > >> second thread is created. > > I've a patch series that is earlier than commit f1a7941243c1 ("mm: > convert mm's rss stats into > percpu_counter"): > > https://lwn.net/ml/linux-kernel/20220728204511.56348-1-ryncsn@xxxxxxxxx/ > > Instead of a per-mm-per-cpu cache, it used only one global per-cpu > cache, and flush it on schedule. Or, if the arch supports, flush and > fetch it use mm bitmap as an optimization (like tlb shootdown). > I just spotted this thread. I have a rather long rant to write about the entire ordeal, but don't have the time at the moment. I do have time to make some remarks though. Rolling with a centralized counter and only distributing per-cpu upon creation of a thread is something which was discussed last time and which I was considering doing. Then life got it in the way and in the meantime I managed to conclude it's a questionable idea anyway. The state prior to the counters moving to per-cpu was not that great to begin with, with quite a few serialization points. As far as allocating stuff goes one example is mm_alloc_cid, with the following: mm->pcpu_cid = alloc_percpu(struct mm_cid); Converting the code to avoid per-cpu rss counters in the common case or the above patchset only damage-control the state back to what it was, don't do anything to push things further. Another note is that unfortunately userspace is increasingly multithreaded for no good reason, see the Rust ecosystem as an example. All that to say is that the multithreaded case is what has to get faster, as a side effect possibly obsoleting both approaches proposed above. I concede if there is nobody wiling to commit to doing the work in the foreseeable future then indeed a damage-controlling solution should land. On that note in check_mm there is this loop: for (i = 0; i < NR_MM_COUNTERS; i++) { long x = percpu_counter_sum(&mm->rss_stat[i]); This avoidably walks all cpus 4 times with a preemption and lock trip for each round. Instead one can observe all modifications are supposed to have already stopped and that this is allocated in a banch. A routine, say percpu_counter_sum_many_unsafe, could do one iteration without any locks or interrupt play and return an array. This should be markedly faster and I perhaps will hack it up. A part of The Real Solution(tm) would make counter allocations scale (including mcid, not just rss) or dodge them (while maintaining the per-cpu distribution, see below for one idea), but that boils down to balancing scalability versus total memory usage. It is trivial to just slap together a per-cpu cache of these allocations and have the problem go away for benchmarking purposes, while being probably being too memory hungry for actual usage. I was pondering an allocator with caches per some number of cores (say 4 or 8). Microbenchmarks aside I suspect real workloads would not suffer from contention at this kind of granularity. This would trivially reduce memory usage compared to per-cpu caching. I suspect things like mm_struct, task_struct, task stacks and similar would be fine with it. Suppose mm_struct is allocated from a more coarse grained allocator than per-cpu. Total number of cached objects would be lower than it is now. That would also mean these allocated but not currently used mms could hold on to other stuff, for example per-cpu rss and mcid counters. Then should someone fork or exit, alloc/free_percpu would be avoided for most cases. This would scale better and be faster single-threaded than the current state. (believe it or not this is not the actual long rant I have in mind) I can't commit to work on the Real Solution though. In the meantime I can submit percpu_counter_sum_many_unsafe as described above if Denis likes the idea.