[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <d048aac6-7b70-44b7-9e92-7277bd49b182@redhat.com>
Date: Thu, 28 Aug 2025 09:57:25 +0200
From: David Hildenbrand <david@...hat.com>
To: Andrew Morton <akpm@...ux-foundation.org>,
Max Kellermann <max.kellermann@...os.com>
Cc: lorenzo.stoakes@...cle.com, ziy@...dia.com,
baolin.wang@...ux.alibaba.com, Liam.Howlett@...cle.com, npache@...hat.com,
ryan.roberts@....com, dev.jain@....com, baohua@...nel.org,
shikemeng@...weicloud.com, kasong@...cent.com, nphamcs@...il.com,
bhe@...hat.com, chrisl@...nel.org, linux-mm@...ck.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2] huge_mm.h: disallow is_huge_zero_folio(NULL)
On 28.08.25 01:53, Andrew Morton wrote:
> On Wed, 27 Aug 2025 17:03:30 +0200 Max Kellermann <max.kellermann@...os.com> wrote:
>
>> Calling is_huge_zero_folio(NULL) should not be legal - it makes no
>> sense, and a different (theoretical) implementation may dereference
>> the pointer. But currently, lacking any explicit documentation, this
>> call is possible.
>>
>> But if somebody really passes NULL, the function should not return
>> true - this isn't the huge zero folio after all! However, if the
>> `huge_zero_folio` hasn't been allocated yet, it's NULL, and
>> is_huge_zero_folio(NULL) just happens to return true, which is a lie.
>>
>> This weird side effect prevented me from reproducing a kernel crash
>> that occurred when the elements of a folio_batch were NULL - since
>> folios_put_refs() skips huge zero folios, this sometimes causes a
>> crash, but sometimes does not. For debugging, it is better to reveal
>> such bugs reliably and not hide them behind random preconditions like
>> "has the huge zero folio already been created?"
>>
>> To improve detection of such bugs, David Hildenbrand suggested adding
>> a VM_WARN_ON_ONCE().
>>
>> ...
>>
>> --- a/include/linux/huge_mm.h
>> +++ b/include/linux/huge_mm.h
>> @@ -2,6 +2,7 @@
>> #ifndef _LINUX_HUGE_MM_H
>> #define _LINUX_HUGE_MM_H
>>
>> +#include <linux/mmdebug.h> // for VM_WARN_ON_ONCE()
>> #include <linux/mm_types.h>
>>
>> #include <linux/fs.h> /* only for vma_is_dax() */
>> @@ -479,6 +480,8 @@ extern unsigned long huge_zero_pfn;
>>
>> static inline bool is_huge_zero_folio(const struct folio *folio)
>> {
>> + VM_WARN_ON_ONCE(folio == NULL);
>> +
>> return READ_ONCE(huge_zero_folio) == folio;
>> }
>
> OK, but it remains the case that we have seen code which calls
> is_huge_zero_folio() prior to the initialization of huge_zero_folio.
>
> Is this a bug? I think so. Should we be checking for recurrences of
> this bug?
As answered elsewhere, this is perfectly fine as the huge zero folio is
allocated on demand only (and only once enabled).
>
>
> Also, sigh. I do dislike seeing VM_WARN_ON_ONCE() in an inline
> function - heaven knows how much bloat that adds. Defconfig
> mm/huge_memory.o (which has three calls) grows by 80 bytes so I guess
> that's livable with.
Common practice to use VM_WARN_ON_ONCE() and friend in inline functions.
Just look at page-flags.h.
If someone complains about kernel image size with CONFIG_DEBUG_VM, they
shopuld reevaluate life choices. :)
--
Cheers
David / dhildenb
Powered by blists - more mailing lists