linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: Charan Teja Kalla <quic_charante@quicinc.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@redhat.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mm: Prevent derefencing NULL ptr in pfn_section_valid()
Date: Tue, 2 Jul 2024 19:45:38 -0400	[thread overview]
Message-ID: <3b4c72ff-7894-4772-a918-7e20d00fac1b@redhat.com> (raw)
In-Reply-To: <bf0d9278-2b57-079e-cca5-21163e187ea3@quicinc.com>


On 7/1/24 09:50, Charan Teja Kalla wrote:
> Hi Waiman,
>
> On 6/26/2024 5:46 AM, Waiman Long wrote:
>> Commit 5ec8e8ea8b77 ("mm/sparsemem: fix race in accessing
>> memory_section->usage") changed pfn_section_valid() to add a READ_ONCE()
>> call around "ms->usage" to fix a race with section_deactivate() where
>> ms->usage can be cleared.  The READ_ONCE() call, by itself, is not enough
>> to prevent NULL pointer dereference.  We need to check its value before
>> dereferencing it.
> I am unable to see a scenario where ms->usage will be NULL when
> pfn_section_valid() is called:
>
> 1) In pfn_valid, valid_section() check ensures that pfn_section_valid()
> is not called as the section is marked as invalid.
>
> 2) In pfn_to_online_page, online_section() check ensures that
> pfn_section_valid() is not called.
>
> and in the update path, we do:
>       kfree_rcu(ms->usage, rcu);
>       WRITE_ONCE(ms->usage, NULL);
>
> Could you help me in understanding about what I am missing here, please?
>
With the below timing sequence:

      CPU 0                                      CPU 1
      -----                                      -----
                                         if (!valid_section(ms))
                                             return 0;
  ms->section_mem_map &=
     ~SECTION_HAS_MEM_MAP                <interrupt>

  WRITE_ONCE(ms->usage, NULL);
                                         READ_ONCE(ms->usage)->subsection_map


In the time gap between valid_section() check and accessing ms->usage,
it may have been cleared leading to dereferencing a NULL pointer. That is
why it will be prudent to do a NULL check first.

Regards,
Longman



      reply	other threads:[~2024-07-02 23:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-26  0:16 Waiman Long
2024-07-01 13:50 ` Charan Teja Kalla
2024-07-02 23:45   ` Waiman Long [this message]

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=3b4c72ff-7894-4772-a918-7e20d00fac1b@redhat.com \
    --to=longman@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=quic_charante@quicinc.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