2020-11-03 09:27:34 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _LINUX_HIGHMEM_INTERNAL_H
|
|
|
|
#define _LINUX_HIGHMEM_INTERNAL_H
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Outside of CONFIG_HIGHMEM to support X86 32bit iomap_atomic() cruft.
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_KMAP_LOCAL
|
|
|
|
void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot);
|
|
|
|
void *__kmap_local_page_prot(struct page *page, pgprot_t prot);
|
2022-07-06 11:15:19 +00:00
|
|
|
void kunmap_local_indexed(const void *vaddr);
|
2020-11-18 19:48:43 +00:00
|
|
|
void kmap_local_fork(struct task_struct *tsk);
|
|
|
|
void __kmap_local_sched_out(void);
|
|
|
|
void __kmap_local_sched_in(void);
|
|
|
|
static inline void kmap_assert_nomap(void)
|
|
|
|
{
|
|
|
|
DEBUG_LOCKS_WARN_ON(current->kmap_ctrl.idx);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline void kmap_local_fork(struct task_struct *tsk) { }
|
|
|
|
static inline void kmap_assert_nomap(void) { }
|
2020-11-03 09:27:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIGHMEM
|
|
|
|
#include <asm/highmem.h>
|
|
|
|
|
|
|
|
#ifndef ARCH_HAS_KMAP_FLUSH_TLB
|
|
|
|
static inline void kmap_flush_tlb(unsigned long addr) { }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef kmap_prot
|
|
|
|
#define kmap_prot PAGE_KERNEL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *kmap_high(struct page *page);
|
|
|
|
void kunmap_high(struct page *page);
|
|
|
|
void __kmap_flush_unused(void);
|
|
|
|
struct page *__kmap_to_page(void *addr);
|
|
|
|
|
|
|
|
static inline void *kmap(struct page *page)
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
might_sleep();
|
|
|
|
if (!PageHighMem(page))
|
|
|
|
addr = page_address(page);
|
|
|
|
else
|
|
|
|
addr = kmap_high(page);
|
|
|
|
kmap_flush_tlb((unsigned long)addr);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kunmap(struct page *page)
|
|
|
|
{
|
|
|
|
might_sleep();
|
|
|
|
if (!PageHighMem(page))
|
|
|
|
return;
|
|
|
|
kunmap_high(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct page *kmap_to_page(void *addr)
|
|
|
|
{
|
|
|
|
return __kmap_to_page(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kmap_flush_unused(void)
|
|
|
|
{
|
|
|
|
__kmap_flush_unused();
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:48:44 +00:00
|
|
|
static inline void *kmap_local_page(struct page *page)
|
|
|
|
{
|
|
|
|
return __kmap_local_page_prot(page, kmap_prot);
|
|
|
|
}
|
|
|
|
|
2020-12-30 15:21:39 +00:00
|
|
|
static inline void *kmap_local_folio(struct folio *folio, size_t offset)
|
|
|
|
{
|
|
|
|
struct page *page = folio_page(folio, offset / PAGE_SIZE);
|
|
|
|
return __kmap_local_page_prot(page, kmap_prot) + offset % PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:48:44 +00:00
|
|
|
static inline void *kmap_local_page_prot(struct page *page, pgprot_t prot)
|
|
|
|
{
|
|
|
|
return __kmap_local_page_prot(page, prot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *kmap_local_pfn(unsigned long pfn)
|
|
|
|
{
|
|
|
|
return __kmap_local_pfn_prot(pfn, kmap_prot);
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:15:19 +00:00
|
|
|
static inline void __kunmap_local(const void *vaddr)
|
2020-11-18 19:48:44 +00:00
|
|
|
{
|
|
|
|
kunmap_local_indexed(vaddr);
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:27:34 +00:00
|
|
|
static inline void *kmap_atomic_prot(struct page *page, pgprot_t prot)
|
|
|
|
{
|
2021-09-08 02:56:09 +00:00
|
|
|
if (IS_ENABLED(CONFIG_PREEMPT_RT))
|
|
|
|
migrate_disable();
|
|
|
|
else
|
|
|
|
preempt_disable();
|
|
|
|
|
2020-11-03 09:27:34 +00:00
|
|
|
pagefault_disable();
|
|
|
|
return __kmap_local_page_prot(page, prot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *kmap_atomic(struct page *page)
|
|
|
|
{
|
|
|
|
return kmap_atomic_prot(page, kmap_prot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *kmap_atomic_pfn(unsigned long pfn)
|
|
|
|
{
|
2021-09-08 02:56:09 +00:00
|
|
|
if (IS_ENABLED(CONFIG_PREEMPT_RT))
|
|
|
|
migrate_disable();
|
|
|
|
else
|
|
|
|
preempt_disable();
|
|
|
|
|
2020-11-03 09:27:34 +00:00
|
|
|
pagefault_disable();
|
|
|
|
return __kmap_local_pfn_prot(pfn, kmap_prot);
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:15:19 +00:00
|
|
|
static inline void __kunmap_atomic(const void *addr)
|
2020-11-03 09:27:34 +00:00
|
|
|
{
|
|
|
|
kunmap_local_indexed(addr);
|
|
|
|
pagefault_enable();
|
2021-09-08 02:56:09 +00:00
|
|
|
if (IS_ENABLED(CONFIG_PREEMPT_RT))
|
|
|
|
migrate_enable();
|
|
|
|
else
|
|
|
|
preempt_enable();
|
2020-11-03 09:27:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:37:11 +00:00
|
|
|
unsigned long __nr_free_highpages(void);
|
mm/highmem: reimplement totalhigh_pages() by walking zones
Patch series "mm/highmem: don't track highmem pages manually".
Let's remove highmem special-casing from adjust_managed_page_count(), to
result in less confusion why memblock manually adjusts totalram_pages, and
__free_pages_core() only adjusts the zone's managed pages -- what about
the highmem pages that adjust_managed_page_count() updates?
Now, we only maintain totalram_pages and a zone's managed pages
independent of highmem support. We can derive the number of highmem pages
simply by looking at the relevant zone's managed pages. I don't think
there is any particular fast path that needs a maximum-efficient
totalhigh_pages() implementation.
Note that highmem memory is currently initialized using
free_highmem_page()->free_reserved_page(), not __free_pages_core(). In
the future we might want to also use __free_pages_core() to initialize
highmem memory, to make that less special, and consider moving
totalram_pages updates into __free_pages_core() [1], so we can just use
adjust_managed_page_count() in there as well.
Booting a simple kernel in QEMU reveals no highmem accounting change:
Before:
Memory: 3095448K/3145208K available (14802K kernel code, 2073K rwdata,
5000K rodata, 740K init, 556K bss, 49760K reserved, 0K cma-reserved,
2244488K highmem)
After:
Memory: 3095276K/3145208K available (14802K kernel code, 2073K rwdata,
5000K rodata, 740K init, 556K bss, 49932K reserved, 0K cma-reserved,
2244488K highmem)
[1] https://lkml.kernel.org/r/20240601133402.2675-1-richard.weiyang@gmail.com
This patch (of 2):
Can we get rid of the highmem ifdef in adjust_managed_page_count()?
Likely yes: we don't have that many totalhigh_pages() users, and they all
don't seem to be very performance critical.
So let's implement totalhigh_pages() like nr_free_highpages(), collecting
information from all zones. This is now similar to what we do in
si_meminfo_node() to collect the per-node highmem page count.
In the common case (single node, 3-4 zones), we really shouldn't care. We
could optimize a bit further (only walk ZONE_HIGHMEM and ZONE_MOVABLE if
required), but there doesn't seem a real need for that.
[david@redhat.com: fix build bot complaint]
Link: https://lkml.kernel.org/r/b57e5bc4-eb72-40e3-add4-57dfa6e03df6@redhat.com
Link: https://lkml.kernel.org/r/20240607083711.62833-1-david@redhat.com
Link: https://lkml.kernel.org/r/20240607083711.62833-2-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-07 08:37:10 +00:00
|
|
|
unsigned long __totalhigh_pages(void);
|
2020-11-03 09:27:34 +00:00
|
|
|
|
2024-06-07 08:37:11 +00:00
|
|
|
static inline unsigned long nr_free_highpages(void)
|
2020-11-03 09:27:34 +00:00
|
|
|
{
|
|
|
|
return __nr_free_highpages();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long totalhigh_pages(void)
|
|
|
|
{
|
mm/highmem: reimplement totalhigh_pages() by walking zones
Patch series "mm/highmem: don't track highmem pages manually".
Let's remove highmem special-casing from adjust_managed_page_count(), to
result in less confusion why memblock manually adjusts totalram_pages, and
__free_pages_core() only adjusts the zone's managed pages -- what about
the highmem pages that adjust_managed_page_count() updates?
Now, we only maintain totalram_pages and a zone's managed pages
independent of highmem support. We can derive the number of highmem pages
simply by looking at the relevant zone's managed pages. I don't think
there is any particular fast path that needs a maximum-efficient
totalhigh_pages() implementation.
Note that highmem memory is currently initialized using
free_highmem_page()->free_reserved_page(), not __free_pages_core(). In
the future we might want to also use __free_pages_core() to initialize
highmem memory, to make that less special, and consider moving
totalram_pages updates into __free_pages_core() [1], so we can just use
adjust_managed_page_count() in there as well.
Booting a simple kernel in QEMU reveals no highmem accounting change:
Before:
Memory: 3095448K/3145208K available (14802K kernel code, 2073K rwdata,
5000K rodata, 740K init, 556K bss, 49760K reserved, 0K cma-reserved,
2244488K highmem)
After:
Memory: 3095276K/3145208K available (14802K kernel code, 2073K rwdata,
5000K rodata, 740K init, 556K bss, 49932K reserved, 0K cma-reserved,
2244488K highmem)
[1] https://lkml.kernel.org/r/20240601133402.2675-1-richard.weiyang@gmail.com
This patch (of 2):
Can we get rid of the highmem ifdef in adjust_managed_page_count()?
Likely yes: we don't have that many totalhigh_pages() users, and they all
don't seem to be very performance critical.
So let's implement totalhigh_pages() like nr_free_highpages(), collecting
information from all zones. This is now similar to what we do in
si_meminfo_node() to collect the per-node highmem page count.
In the common case (single node, 3-4 zones), we really shouldn't care. We
could optimize a bit further (only walk ZONE_HIGHMEM and ZONE_MOVABLE if
required), but there doesn't seem a real need for that.
[david@redhat.com: fix build bot complaint]
Link: https://lkml.kernel.org/r/b57e5bc4-eb72-40e3-add4-57dfa6e03df6@redhat.com
Link: https://lkml.kernel.org/r/20240607083711.62833-1-david@redhat.com
Link: https://lkml.kernel.org/r/20240607083711.62833-2-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-07 08:37:10 +00:00
|
|
|
return __totalhigh_pages();
|
2020-11-03 09:27:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 23:15:27 +00:00
|
|
|
static inline bool is_kmap_addr(const void *x)
|
|
|
|
{
|
|
|
|
unsigned long addr = (unsigned long)x;
|
2023-02-04 04:06:32 +00:00
|
|
|
|
|
|
|
return (addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP)) ||
|
|
|
|
(addr >= __fix_to_virt(FIX_KMAP_END) &&
|
|
|
|
addr < __fix_to_virt(FIX_KMAP_BEGIN));
|
2022-01-10 23:15:27 +00:00
|
|
|
}
|
2020-11-03 09:27:34 +00:00
|
|
|
#else /* CONFIG_HIGHMEM */
|
|
|
|
|
|
|
|
static inline struct page *kmap_to_page(void *addr)
|
|
|
|
{
|
|
|
|
return virt_to_page(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *kmap(struct page *page)
|
|
|
|
{
|
|
|
|
might_sleep();
|
|
|
|
return page_address(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kunmap_high(struct page *page) { }
|
|
|
|
static inline void kmap_flush_unused(void) { }
|
|
|
|
|
|
|
|
static inline void kunmap(struct page *page)
|
|
|
|
{
|
|
|
|
#ifdef ARCH_HAS_FLUSH_ON_KUNMAP
|
|
|
|
kunmap_flush_on_unmap(page_address(page));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:48:44 +00:00
|
|
|
static inline void *kmap_local_page(struct page *page)
|
|
|
|
{
|
|
|
|
return page_address(page);
|
|
|
|
}
|
|
|
|
|
2020-12-30 15:21:39 +00:00
|
|
|
static inline void *kmap_local_folio(struct folio *folio, size_t offset)
|
|
|
|
{
|
|
|
|
return page_address(&folio->page) + offset;
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:48:44 +00:00
|
|
|
static inline void *kmap_local_page_prot(struct page *page, pgprot_t prot)
|
|
|
|
{
|
|
|
|
return kmap_local_page(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *kmap_local_pfn(unsigned long pfn)
|
|
|
|
{
|
|
|
|
return kmap_local_page(pfn_to_page(pfn));
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:15:19 +00:00
|
|
|
static inline void __kunmap_local(const void *addr)
|
2020-11-18 19:48:44 +00:00
|
|
|
{
|
|
|
|
#ifdef ARCH_HAS_FLUSH_ON_KUNMAP
|
2023-01-26 20:07:27 +00:00
|
|
|
kunmap_flush_on_unmap(PTR_ALIGN_DOWN(addr, PAGE_SIZE));
|
2020-11-18 19:48:44 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:27:34 +00:00
|
|
|
static inline void *kmap_atomic(struct page *page)
|
|
|
|
{
|
2021-09-08 02:56:09 +00:00
|
|
|
if (IS_ENABLED(CONFIG_PREEMPT_RT))
|
|
|
|
migrate_disable();
|
|
|
|
else
|
|
|
|
preempt_disable();
|
2020-11-03 09:27:34 +00:00
|
|
|
pagefault_disable();
|
|
|
|
return page_address(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *kmap_atomic_prot(struct page *page, pgprot_t prot)
|
|
|
|
{
|
|
|
|
return kmap_atomic(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *kmap_atomic_pfn(unsigned long pfn)
|
|
|
|
{
|
|
|
|
return kmap_atomic(pfn_to_page(pfn));
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:15:19 +00:00
|
|
|
static inline void __kunmap_atomic(const void *addr)
|
2020-11-03 09:27:34 +00:00
|
|
|
{
|
|
|
|
#ifdef ARCH_HAS_FLUSH_ON_KUNMAP
|
2023-01-26 20:07:27 +00:00
|
|
|
kunmap_flush_on_unmap(PTR_ALIGN_DOWN(addr, PAGE_SIZE));
|
2020-11-03 09:27:34 +00:00
|
|
|
#endif
|
|
|
|
pagefault_enable();
|
2021-09-08 02:56:09 +00:00
|
|
|
if (IS_ENABLED(CONFIG_PREEMPT_RT))
|
|
|
|
migrate_enable();
|
|
|
|
else
|
|
|
|
preempt_enable();
|
2020-11-03 09:27:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:37:11 +00:00
|
|
|
static inline unsigned long nr_free_highpages(void) { return 0; }
|
|
|
|
static inline unsigned long totalhigh_pages(void) { return 0; }
|
2020-11-03 09:27:34 +00:00
|
|
|
|
2022-01-10 23:15:27 +00:00
|
|
|
static inline bool is_kmap_addr(const void *x)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:27:34 +00:00
|
|
|
#endif /* CONFIG_HIGHMEM */
|
|
|
|
|
2022-05-13 23:48:55 +00:00
|
|
|
/**
|
|
|
|
* kunmap_atomic - Unmap the virtual address mapped by kmap_atomic() - deprecated!
|
|
|
|
* @__addr: Virtual address to be unmapped
|
|
|
|
*
|
|
|
|
* Unmaps an address previously mapped by kmap_atomic() and re-enables
|
|
|
|
* pagefaults. Depending on PREEMP_RT configuration, re-enables also
|
|
|
|
* migration and preemption. Users should not count on these side effects.
|
|
|
|
*
|
|
|
|
* Mappings should be unmapped in the reverse order that they were mapped.
|
|
|
|
* See kmap_local_page() for details on nesting.
|
|
|
|
*
|
|
|
|
* @__addr can be any address within the mapped page, so there is no need
|
|
|
|
* to subtract any offset that has been added. In contrast to kunmap(),
|
|
|
|
* this function takes the address returned from kmap_atomic(), not the
|
|
|
|
* page passed to it. The compiler will warn you if you pass the page.
|
2020-11-03 09:27:34 +00:00
|
|
|
*/
|
|
|
|
#define kunmap_atomic(__addr) \
|
|
|
|
do { \
|
|
|
|
BUILD_BUG_ON(__same_type((__addr), struct page *)); \
|
|
|
|
__kunmap_atomic(__addr); \
|
|
|
|
} while (0)
|
|
|
|
|
2022-03-22 21:47:58 +00:00
|
|
|
/**
|
|
|
|
* kunmap_local - Unmap a page mapped via kmap_local_page().
|
|
|
|
* @__addr: An address within the page mapped
|
|
|
|
*
|
|
|
|
* @__addr can be any address within the mapped page. Commonly it is the
|
|
|
|
* address return from kmap_local_page(), but it can also include offsets.
|
|
|
|
*
|
|
|
|
* Unmapping should be done in the reverse order of the mapping. See
|
|
|
|
* kmap_local_page() for details.
|
|
|
|
*/
|
2020-11-18 19:48:44 +00:00
|
|
|
#define kunmap_local(__addr) \
|
|
|
|
do { \
|
|
|
|
BUILD_BUG_ON(__same_type((__addr), struct page *)); \
|
|
|
|
__kunmap_local(__addr); \
|
|
|
|
} while (0)
|
|
|
|
|
2020-11-03 09:27:34 +00:00
|
|
|
#endif
|