From: David Hildenbrand <david@redhat.com>
To: Stefan Roesch <shr@devkernel.io>
Cc: kernel-team@fb.com, linux-mm@kvack.org, riel@surriel.com,
mhocko@suse.com, linux-kselftest@vger.kernel.org,
linux-doc@vger.kernel.org, akpm@linux-foundation.org,
hannes@cmpxchg.org, Bagas Sanjaya <bagasdotme@gmail.com>
Subject: Re: [PATCH v4 1/3] mm: add new api to enable ksm per process
Date: Mon, 3 Apr 2023 19:02:36 +0200 [thread overview]
Message-ID: <0235770b-eb37-88e0-9350-a2d9c0cf9c32@redhat.com> (raw)
In-Reply-To: <qvqwpm8ludx2.fsf@dev0134.prn3.facebook.com>
On 03.04.23 17:50, Stefan Roesch wrote:
>> I guess the interpreter could enable it (like a memory allocator could enable it
>> for the whole heap). But I get that it's much easier to enable this per-process,
>> and eventually only when a lot of the same processes are running in that
>> particular environment.
>>
>
> We don't want it to get enabled for all workloads of that interpreter,
> instead we want to be able to select for which workloads we enable KSM.
>
Right.
>
>>> 1. New options for prctl system command
>>> This patch series adds two new options to the prctl system call.
>>> The first one allows to enable KSM at the process level and the second
>>> one to query the setting.
>>> The setting will be inherited by child processes.
>>> With the above setting, KSM can be enabled for the seed process of a
>>> cgroup and all processes in the cgroup will inherit the setting.
>>> 2. Changes to KSM processing
>>> When KSM is enabled at the process level, the KSM code will iterate
>>> over all the VMA's and enable KSM for the eligible VMA's.
>>> When forking a process that has KSM enabled, the setting will be
>>> inherited by the new child process.
>>> In addition when KSM is disabled for a process, KSM will be disabled
>>> for the VMA's where KSM has been enabled.
>>
>> Do we want to make MADV_MERGEABLE/MADV_UNMERGEABLE fail while the new prctl is
>> enabled for a process?
>
> I decided to allow enabling KSM with prctl even when MADV_MERGEABLE,
> this allows more flexibility.
MADV_MERGEABLE will be a nop. But IIUC, MADV_UNMERGEABLE will end up
calling unmerge_ksm_pages() and clear VM_MERGEABLE. But then, the next
KSM scan will merge the pages in there again.
Not sure if that flexibility is worth having.
[...]
>>> @@ -2661,6 +2662,32 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
>>> case PR_SET_VMA:
>>> error = prctl_set_vma(arg2, arg3, arg4, arg5);
>>> break;
>>> +#ifdef CONFIG_KSM
>>> + case PR_SET_MEMORY_MERGE:
>>> + if (!capable(CAP_SYS_RESOURCE))
>>> + return -EPERM;
>>> +
>>> + if (arg2) {
>>> + if (mmap_write_lock_killable(me->mm))
>>> + return -EINTR;
>>> +
>>> + if (!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags))
>>> + error = __ksm_enter(me->mm, MMF_VM_MERGE_ANY);
>>
>> Hm, I think this might be problematic if we alread called __ksm_enter() via
>> madvise(). Maybe we should really consider making MMF_VM_MERGE_ANY set
>> MMF_VM_MERGABLE instead. Like:
>>
>> error = 0;
>> if(test_bit(MMF_VM_MERGEABLE, &me->mm->flags))
>> error = __ksm_enter(me->mm);
>> if (!error)
>> set_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
>>
>
> If we make that change, we would no longer be able to distinguish
> if MMF_VM_MERGEABLE or MMF_VM_MERGE_ANY have been set.
Why would you need that exactly? To cleanup? See below.
>
>>> + mmap_write_unlock(me->mm);
>>> + } else {
>>> + __ksm_exit(me->mm, MMF_VM_MERGE_ANY);
>>
>> Hm, I'd prefer if we really only call __ksm_exit() when we really exit the
>> process. Is there a strong requirement to optimize disabling of KSM or would it
>> be sufficient to clear the MMF_VM_MERGE_ANY flag here?
>>
> Then we still have the mm_slot allocated until the process gets
> terminated.
Which is the same as using MADV_UNMERGEABLE, no?
>
>> Also, I wonder what happens if we have another VMA in that process that has it
>> enabled ..
>>
>> Last but not least, wouldn't we want to do the same thing as MADV_UNMERGEABLE
>> and actually unmerge the KSM pages?
>>
> Do you want to call unmerge for all VMA's?
The question is what clearing MMF_VM_MERGE_ANY is supposed to do. If
it's supposed to disable KSM (like MADV_UNMERGEABLE) would, then I guess
you should go over all VMA's and unmerge.
Also, it depend on how you want to handle VM_MERGABLE with
MMF_VM_MERGE_ANY. If MMF_VM_MERGE_ANY would not set VM_MERGABLE, then
you'd only unmerge where VM_MERGABLE is not set. Otherwise, you'd
unshare everywhere where VM_MERGABLE is set (and clear VM_MERGABLE)
while at it.
Unsharing when clearing MMF_VM_MERGE_ANY might be the right thing to do
IMHO.
I guess the main questions regarding implementation are:
1) Do we want setting MMF_VM_MERGE_ANY to set VM_MERGABLE on all
candidate VMA's (go over all VMA's and set VM_MERGABLE). Then,
clearing MMF_VM_MERGE_ANY would simply unmerge and clear VM_MERGABLE
on all VMA's.
2) Do we want to make MMF_VM_MERGE_ANY imply MMF_VM_MERGABLE. You could
still disable KSM (__ksm_exit()) during clearing MMF_VM_MERGE_ANY
after going over all VMA's (where you might want to unshare already
either way).
I guess the code will end up simpler if you make MMF_VM_MERGE_ANY simply
piggy-back on MMF_VM_MERGABLE + VM_MERGABLE. I might be wrong, of course.
--
Thanks,
David / dhildenb
next prev parent reply other threads:[~2023-04-03 17:02 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-10 18:28 [PATCH v4 0/3] mm: process/cgroup ksm support Stefan Roesch
2023-03-10 18:28 ` [PATCH v4 1/3] mm: add new api to enable ksm per process Stefan Roesch
2023-03-13 16:26 ` Johannes Weiner
2023-04-03 10:37 ` David Hildenbrand
2023-04-03 11:03 ` David Hildenbrand
2023-04-04 16:32 ` Stefan Roesch
2023-04-04 16:43 ` Stefan Roesch
2023-04-05 6:51 ` Christian Borntraeger
2023-04-05 16:04 ` David Hildenbrand
2023-04-03 15:50 ` Stefan Roesch
2023-04-03 17:02 ` David Hildenbrand [this message]
2023-03-10 18:28 ` [PATCH v4 2/3] mm: add new KSM process and sysfs knobs Stefan Roesch
2023-04-05 17:04 ` David Hildenbrand
2023-04-05 21:20 ` Stefan Roesch
2023-04-06 13:23 ` David Hildenbrand
2023-04-06 14:16 ` Johannes Weiner
2023-04-06 14:32 ` David Hildenbrand
2023-03-10 18:28 ` [PATCH v4 3/3] selftests/mm: add new selftests for KSM Stefan Roesch
2023-03-15 20:03 ` [PATCH v4 0/3] mm: process/cgroup ksm support David Hildenbrand
2023-03-15 20:23 ` Mike Kravetz
2023-03-15 21:05 ` Johannes Weiner
2023-03-15 21:19 ` Johannes Weiner
2023-03-15 21:45 ` David Hildenbrand
2023-03-15 21:47 ` David Hildenbrand
2023-03-30 16:19 ` Stefan Roesch
2023-03-28 23:09 ` Andrew Morton
2023-03-30 4:55 ` David Hildenbrand
2023-03-30 14:26 ` Johannes Weiner
2023-03-30 14:40 ` David Hildenbrand
2023-03-30 16:41 ` Stefan Roesch
2023-04-03 9:48 ` David Hildenbrand
2023-04-03 16:34 ` Stefan Roesch
2023-04-03 17:04 ` David Hildenbrand
2023-04-06 16:59 ` Stefan Roesch
2023-04-06 17:10 ` David Hildenbrand
2023-03-30 20:18 ` Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0235770b-eb37-88e0-9350-a2d9c0cf9c32@redhat.com \
--to=david@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=bagasdotme@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=kernel-team@fb.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=riel@surriel.com \
--cc=shr@devkernel.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox