linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Jinjiang Tu <tujinjiang@huawei.com>,
	akpm@linux-foundation.org, shr@devkernel.io, hannes@cmpxchg.org,
	riel@surriel.com, wangkefeng.wang@huawei.com,
	sunnanyong@huawei.com, linux-mm@kvack.org
Subject: Re: [PATCH v3] mm/ksm: fix ksm exec support for prctl
Date: Mon, 18 Mar 2024 11:54:29 +0100	[thread overview]
Message-ID: <8819b03d-7715-4159-863d-f15e94adcc29@redhat.com> (raw)
In-Reply-To: <527963a1-e7e5-78ab-99dd-2677de980eed@huawei.com>


>>>    +static inline int ksm_execve(struct mm_struct *mm)
>>> +{
>>> +    if (test_bit(MMF_VM_MERGE_ANY, &mm->flags))
>>> +        return __ksm_enter(mm);
>>
>> As soon as we did the __ksm_enter(), we have to set MMF_VM_MERGEABLE.
>> I don't think it would be set right now, because:
>>
>> mm_alloc()->mm_init() will initialize the flags using
>>
>>      mm->flags = mmf_init_flags(current->mm->flags);
>>
>> Whereby MMF_INIT_MASK contains only MMF_VM_MERGE_ANY_MASK.
>>
>>
>> So we likely need a set_bit(MMF_VM_MERGEABLE, &mm->flags) here as
>> well. Otherwise ksm_exit() wouldn't clean up, and we might call
>> __ksm_enter() twice.
> 
> __ksm_enter() will set MMF_VM_MERGEABLE when it succeeds.
> 

Ah, indeed!

>>
>>
>> And now I wonder, when would be the right point to call __ksm_enter().
>>
>> __mmput() calls ksm_exit(). But for example, if __bprm_mm_init() fails
>> after __ksm_enter(), we will only call mmdrop(), not cleaning up ...
>> so that looks wrong.
> Yes, I forgot cleanup in error path. ksm_exit() should be called when
> __bprm_mm_init() fails after __ksm_enter().
>>
>> We would have to make sure we call __ksm_enter() only once we know
>> that callers will call mm_put(). that is the case once we return from
>> bprm_mm_init() with success.
>>
>> Maybe move the ksm_execve() to bprm_mm_init(), adding a comment that
>> cleanup will only happen during mm_put(), so it's harder to mess up in
>> the future?
>>
> __ksm_enter() should be called under mmap write lock to avoid race with
> ksmd. So we can't move ksm_execve() to bprm_mm_init().

Makes sense. We have to be careful that this won't silently break in the 
future. Better add some comment as well.

-- 
Cheers,

David / dhildenb



  reply	other threads:[~2024-03-18 10:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-18  9:04 [PATCH v3] mm/ksm: fix ksm exec support for prctl Jinjiang Tu
2024-03-18  8:19 ` Jinjiang Tu
2024-03-18  9:36 ` David Hildenbrand
2024-03-18 10:52   ` Jinjiang Tu
2024-03-18 10:54     ` David Hildenbrand [this message]
2024-03-18  9:59 ` David Hildenbrand
2024-03-18 10:54   ` Jinjiang Tu

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=8819b03d-7715-4159-863d-f15e94adcc29@redhat.com \
    --to=david@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-mm@kvack.org \
    --cc=riel@surriel.com \
    --cc=shr@devkernel.io \
    --cc=sunnanyong@huawei.com \
    --cc=tujinjiang@huawei.com \
    --cc=wangkefeng.wang@huawei.com \
    /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;
as well as URLs for NNTP newsgroup(s).