[PATCH v7 11/16] mm/highmem: introduce clear_user_highpages()

Ankur Arora posted 16 patches 2 weeks ago
[PATCH v7 11/16] mm/highmem: introduce clear_user_highpages()
Posted by Ankur Arora 2 weeks ago
Define clear_user_highpages() which clears pages sequentially using
the single page variant.

With !CONFIG_HIGHMEM, pages are contiguous so use the range clearing
primitive clear_user_pages().

Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
---
 include/linux/highmem.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index 6234f316468c..ed609987e24d 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -207,6 +207,24 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
 }
 #endif
 
+#ifndef clear_user_highpages
+static inline void clear_user_highpages(struct page *page, unsigned long vaddr,
+					unsigned int npages)
+{
+	if (!IS_ENABLED(CONFIG_HIGHMEM)) {
+		void *base = page_address(page);
+		clear_user_pages(base, vaddr, page, npages);
+		return;
+	}
+
+	do {
+		clear_user_highpage(page, vaddr);
+		vaddr += PAGE_SIZE;
+		page++;
+	} while (--npages);
+}
+#endif
+
 #ifndef vma_alloc_zeroed_movable_folio
 /**
  * vma_alloc_zeroed_movable_folio - Allocate a zeroed page for a VMA.
-- 
2.43.5
Re: [PATCH v7 11/16] mm/highmem: introduce clear_user_highpages()
Posted by David Hildenbrand 1 week, 2 days ago
On 17.09.25 17:24, Ankur Arora wrote:
> Define clear_user_highpages() which clears pages sequentially using
> the single page variant.
> 
> With !CONFIG_HIGHMEM, pages are contiguous so use the range clearing
> primitive clear_user_pages().
> 
> Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
> ---
>   include/linux/highmem.h | 18 ++++++++++++++++++
>   1 file changed, 18 insertions(+)
> 
> diff --git a/include/linux/highmem.h b/include/linux/highmem.h
> index 6234f316468c..ed609987e24d 100644
> --- a/include/linux/highmem.h
> +++ b/include/linux/highmem.h
> @@ -207,6 +207,24 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
>   }
>   #endif
>   
> +#ifndef clear_user_highpages

Maybe we can add a simple kernel doc that points at the doc of clear_user_pages,
but makes it clear that this for pages that might reside in highmem.

> +static inline void clear_user_highpages(struct page *page, unsigned long vaddr,
> +					unsigned int npages)
> +{
> +	if (!IS_ENABLED(CONFIG_HIGHMEM)) {
> +		clear_user_pages(base, vaddr, page, npages);

Single line should work

		clear_user_pages(page_address(page), vaddr, page, npages);

> +		return;
> +	}
> +
> +	do {
> +		clear_user_highpage(page, vaddr);
> +		vaddr += PAGE_SIZE;
> +		page++;
> +	} while (--npages);
> +}
> +#endif
> +
>   #ifndef vma_alloc_zeroed_movable_folio
>   /**
>    * vma_alloc_zeroed_movable_folio - Allocate a zeroed page for a VMA.


-- 
Cheers

David / dhildenb
Re: [PATCH v7 11/16] mm/highmem: introduce clear_user_highpages()
Posted by Ankur Arora 1 week, 1 day ago
David Hildenbrand <david@redhat.com> writes:

> On 17.09.25 17:24, Ankur Arora wrote:
>> Define clear_user_highpages() which clears pages sequentially using
>> the single page variant.
>> With !CONFIG_HIGHMEM, pages are contiguous so use the range clearing
>> primitive clear_user_pages().
>> Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
>> ---
>>   include/linux/highmem.h | 18 ++++++++++++++++++
>>   1 file changed, 18 insertions(+)
>> diff --git a/include/linux/highmem.h b/include/linux/highmem.h
>> index 6234f316468c..ed609987e24d 100644
>> --- a/include/linux/highmem.h
>> +++ b/include/linux/highmem.h
>> @@ -207,6 +207,24 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
>>   }
>>   #endif
>>   +#ifndef clear_user_highpages
>
> Maybe we can add a simple kernel doc that points at the doc of clear_user_pages,
> but makes it clear that this for pages that might reside in highmem.

Didn't add one because clear_user_highpage() didn't have one. Will add
for both.

>> +static inline void clear_user_highpages(struct page *page, unsigned long vaddr,
>> +					unsigned int npages)
>> +{
>> +	if (!IS_ENABLED(CONFIG_HIGHMEM)) {
>> +		clear_user_pages(base, vaddr, page, npages);
>
> Single line should work
>
> 		clear_user_pages(page_address(page), vaddr, page, npages);

Unfortunately not. The problem is that I'm defining the fallback version
of clear_user_pages() as a macro in the previous patch.

        +#define clear_user_pages(addr, vaddr, pg, npages)	\
        +do {							\
        +	clear_user_page(addr, vaddr, pg);		\
        +	addr += PAGE_SIZE;				\
        +	vaddr += PAGE_SIZE;				\
        +	pg++;						\
        +} while (--npages)


And so using page_address() directly doesn't work because addr needs
to be an lvalue for the addition which page_address(page) isn't.


Thanks

--
ankur
Re: [PATCH v7 11/16] mm/highmem: introduce clear_user_highpages()
Posted by David Hildenbrand 1 week ago
On 23.09.25 22:34, Ankur Arora wrote:
> 
> David Hildenbrand <david@redhat.com> writes:
> 
>> On 17.09.25 17:24, Ankur Arora wrote:
>>> Define clear_user_highpages() which clears pages sequentially using
>>> the single page variant.
>>> With !CONFIG_HIGHMEM, pages are contiguous so use the range clearing
>>> primitive clear_user_pages().
>>> Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
>>> ---
>>>    include/linux/highmem.h | 18 ++++++++++++++++++
>>>    1 file changed, 18 insertions(+)
>>> diff --git a/include/linux/highmem.h b/include/linux/highmem.h
>>> index 6234f316468c..ed609987e24d 100644
>>> --- a/include/linux/highmem.h
>>> +++ b/include/linux/highmem.h
>>> @@ -207,6 +207,24 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
>>>    }
>>>    #endif
>>>    +#ifndef clear_user_highpages
>>
>> Maybe we can add a simple kernel doc that points at the doc of clear_user_pages,
>> but makes it clear that this for pages that might reside in highmem.
> 
> Didn't add one because clear_user_highpage() didn't have one. Will add
> for both.
> 

Doesn't have to be excessive. But even I have to keep reminding myself 
when to use clear_page(), clear_user_page(), clear_user_highpage() ...

>>> +static inline void clear_user_highpages(struct page *page, unsigned long vaddr,
>>> +					unsigned int npages)
>>> +{
>>> +	if (!IS_ENABLED(CONFIG_HIGHMEM)) {
>>> +		clear_user_pages(base, vaddr, page, npages);
>>
>> Single line should work
>>
>> 		clear_user_pages(page_address(page), vaddr, page, npages);
> 
> Unfortunately not. The problem is that I'm defining the fallback version
> of clear_user_pages() as a macro in the previous patch.

Yet another sign that we have to fix that instead. :)

-- 
Cheers

David / dhildenb