mirror of
https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux
synced 2025-09-14 00:29:50 +10:00
Patch series "New page table range API", v6. This patchset changes the API used by the MM to set up page table entries. The four APIs are: set_ptes(mm, addr, ptep, pte, nr) update_mmu_cache_range(vma, addr, ptep, nr) flush_dcache_folio(folio) flush_icache_pages(vma, page, nr) flush_dcache_folio() isn't technically new, but no architecture implemented it, so I've done that for them. The old APIs remain around but are mostly implemented by calling the new interfaces. The new APIs are based around setting up N page table entries at once. The N entries belong to the same PMD, the same folio and the same VMA, so ptep++ is a legitimate operation, and locking is taken care of for you. Some architectures can do a better job of it than just a loop, but I have hesitated to make too deep a change to architectures I don't understand well. One thing I have changed in every architecture is that PG_arch_1 is now a per-folio bit instead of a per-page bit when used for dcache clean/dirty tracking. This was something that would have to happen eventually, and it makes sense to do it now rather than iterate over every page involved in a cache flush and figure out if it needs to happen. The point of all this is better performance, and Fengwei Yin has measured improvement on x86. I suspect you'll see improvement on your architecture too. Try the new will-it-scale test mentioned here: https://lore.kernel.org/linux-mm/20230206140639.538867-5-fengwei.yin@intel.com/ You'll need to run it on an XFS filesystem and have CONFIG_TRANSPARENT_HUGEPAGE set. This patchset is the basis for much of the anonymous large folio work being done by Ryan, so it's received quite a lot of testing over the last few months. This patch (of 38): Determine if a value lies within a range more efficiently (subtraction + comparison vs two comparisons and an AND). It also has useful (under some circumstances) behaviour if the range exceeds the maximum value of the type. Convert all the conflicting definitions of in_range() within the kernel; some can use the generic definition while others need their own definition. Link: https://lkml.kernel.org/r/20230802151406.3735276-1-willy@infradead.org Link: https://lkml.kernel.org/r/20230802151406.3735276-2-willy@infradead.org Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
197 lines
5.8 KiB
C
197 lines
5.8 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef _LINUX_MINMAX_H
|
|
#define _LINUX_MINMAX_H
|
|
|
|
#include <linux/const.h>
|
|
#include <linux/types.h>
|
|
|
|
/*
|
|
* min()/max()/clamp() macros must accomplish three things:
|
|
*
|
|
* - avoid multiple evaluations of the arguments (so side-effects like
|
|
* "x++" happen only once) when non-constant.
|
|
* - perform strict type-checking (to generate warnings instead of
|
|
* nasty runtime surprises). See the "unnecessary" pointer comparison
|
|
* in __typecheck().
|
|
* - retain result as a constant expressions when called with only
|
|
* constant expressions (to avoid tripping VLA warnings in stack
|
|
* allocation usage).
|
|
*/
|
|
#define __typecheck(x, y) \
|
|
(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
|
|
|
|
#define __no_side_effects(x, y) \
|
|
(__is_constexpr(x) && __is_constexpr(y))
|
|
|
|
#define __safe_cmp(x, y) \
|
|
(__typecheck(x, y) && __no_side_effects(x, y))
|
|
|
|
#define __cmp(x, y, op) ((x) op (y) ? (x) : (y))
|
|
|
|
#define __cmp_once(x, y, unique_x, unique_y, op) ({ \
|
|
typeof(x) unique_x = (x); \
|
|
typeof(y) unique_y = (y); \
|
|
__cmp(unique_x, unique_y, op); })
|
|
|
|
#define __careful_cmp(x, y, op) \
|
|
__builtin_choose_expr(__safe_cmp(x, y), \
|
|
__cmp(x, y, op), \
|
|
__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
|
|
|
|
#define __clamp(val, lo, hi) \
|
|
((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
|
|
|
|
#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \
|
|
typeof(val) unique_val = (val); \
|
|
typeof(lo) unique_lo = (lo); \
|
|
typeof(hi) unique_hi = (hi); \
|
|
__clamp(unique_val, unique_lo, unique_hi); })
|
|
|
|
#define __clamp_input_check(lo, hi) \
|
|
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
|
|
__is_constexpr((lo) > (hi)), (lo) > (hi), false)))
|
|
|
|
#define __careful_clamp(val, lo, hi) ({ \
|
|
__clamp_input_check(lo, hi) + \
|
|
__builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \
|
|
__typecheck(hi, lo) && __is_constexpr(val) && \
|
|
__is_constexpr(lo) && __is_constexpr(hi), \
|
|
__clamp(val, lo, hi), \
|
|
__clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
|
|
__UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); })
|
|
|
|
/**
|
|
* min - return minimum of two values of the same or compatible types
|
|
* @x: first value
|
|
* @y: second value
|
|
*/
|
|
#define min(x, y) __careful_cmp(x, y, <)
|
|
|
|
/**
|
|
* max - return maximum of two values of the same or compatible types
|
|
* @x: first value
|
|
* @y: second value
|
|
*/
|
|
#define max(x, y) __careful_cmp(x, y, >)
|
|
|
|
/**
|
|
* min3 - return minimum of three values
|
|
* @x: first value
|
|
* @y: second value
|
|
* @z: third value
|
|
*/
|
|
#define min3(x, y, z) min((typeof(x))min(x, y), z)
|
|
|
|
/**
|
|
* max3 - return maximum of three values
|
|
* @x: first value
|
|
* @y: second value
|
|
* @z: third value
|
|
*/
|
|
#define max3(x, y, z) max((typeof(x))max(x, y), z)
|
|
|
|
/**
|
|
* min_not_zero - return the minimum that is _not_ zero, unless both are zero
|
|
* @x: value1
|
|
* @y: value2
|
|
*/
|
|
#define min_not_zero(x, y) ({ \
|
|
typeof(x) __x = (x); \
|
|
typeof(y) __y = (y); \
|
|
__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
|
|
|
|
/**
|
|
* clamp - return a value clamped to a given range with strict typechecking
|
|
* @val: current value
|
|
* @lo: lowest allowable value
|
|
* @hi: highest allowable value
|
|
*
|
|
* This macro does strict typechecking of @lo/@hi to make sure they are of the
|
|
* same type as @val. See the unnecessary pointer comparisons.
|
|
*/
|
|
#define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
|
|
|
|
/*
|
|
* ..and if you can't take the strict
|
|
* types, you can specify one yourself.
|
|
*
|
|
* Or not use min/max/clamp at all, of course.
|
|
*/
|
|
|
|
/**
|
|
* min_t - return minimum of two values, using the specified type
|
|
* @type: data type to use
|
|
* @x: first value
|
|
* @y: second value
|
|
*/
|
|
#define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
|
|
|
|
/**
|
|
* max_t - return maximum of two values, using the specified type
|
|
* @type: data type to use
|
|
* @x: first value
|
|
* @y: second value
|
|
*/
|
|
#define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
|
|
|
|
/**
|
|
* clamp_t - return a value clamped to a given range using a given type
|
|
* @type: the type of variable to use
|
|
* @val: current value
|
|
* @lo: minimum allowable value
|
|
* @hi: maximum allowable value
|
|
*
|
|
* This macro does no typechecking and uses temporary variables of type
|
|
* @type to make all the comparisons.
|
|
*/
|
|
#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
|
|
|
|
/**
|
|
* clamp_val - return a value clamped to a given range using val's type
|
|
* @val: current value
|
|
* @lo: minimum allowable value
|
|
* @hi: maximum allowable value
|
|
*
|
|
* This macro does no typechecking and uses temporary variables of whatever
|
|
* type the input argument @val is. This is useful when @val is an unsigned
|
|
* type and @lo and @hi are literals that will otherwise be assigned a signed
|
|
* integer type.
|
|
*/
|
|
#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
|
|
|
|
static inline bool in_range64(u64 val, u64 start, u64 len)
|
|
{
|
|
return (val - start) < len;
|
|
}
|
|
|
|
static inline bool in_range32(u32 val, u32 start, u32 len)
|
|
{
|
|
return (val - start) < len;
|
|
}
|
|
|
|
/**
|
|
* in_range - Determine if a value lies within a range.
|
|
* @val: Value to test.
|
|
* @start: First value in range.
|
|
* @len: Number of values in range.
|
|
*
|
|
* This is more efficient than "if (start <= val && val < (start + len))".
|
|
* It also gives a different answer if @start + @len overflows the size of
|
|
* the type by a sufficient amount to encompass @val. Decide for yourself
|
|
* which behaviour you want, or prove that start + len never overflow.
|
|
* Do not blindly replace one form with the other.
|
|
*/
|
|
#define in_range(val, start, len) \
|
|
((sizeof(start) | sizeof(len) | sizeof(val)) <= sizeof(u32) ? \
|
|
in_range32(val, start, len) : in_range64(val, start, len))
|
|
|
|
/**
|
|
* swap - swap values of @a and @b
|
|
* @a: first value
|
|
* @b: second value
|
|
*/
|
|
#define swap(a, b) \
|
|
do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
|
|
|
|
#endif /* _LINUX_MINMAX_H */
|