Open HuaxinLu opened 3 years ago
@poettering could you comment whether this is a sane thing to do in PID 1 at boot time?
Sure, we can do that. But I am not sure I understand the effect of it in full. Returning the memory to the kernel might slow things down for us if we end up needing it for something else later. It appears to me that we should only call this once pid1 initialization is complete and we are idle (i.e. from an sd_event_add_defer() handler), and probably independently of selinux code, i.e. do this always. and someone needs to do some profiling how much this actually does IRL.
The init process systemd calls selinux_init_load_policy function to load policy when system start. Under the special conditions that the maximum supported policy version of the kernel is lower than the current policy version, the policy downgrade will happen in selinux_mkload_policy function.
During the downgrade, a lot of memory allocations and frees will happen. Due to the memory management, part of the physical memory cannot be memory reclaimed after downgrade, despite the fact that they have been freed. That will cause the system's available memory to decrease because systemd process will not exit.
I suggest that the malloc_trim can be called after selinux_init_load_policy to force memory recovery reclaim.
For example, I test with 4.19 kernel with 3.1 selinux packages:
The memory usage of systemd is large:
After patching code as follow:
The memory usage of systemd can be decreased: