minmax: add in_range() macro
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Wed, 2 Aug 2023 15:13:29 +0000 (16:13 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 24 Aug 2023 23:20:18 +0000 (16:20 -0700)
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>
16 files changed:
arch/arm/mm/pageattr.c
drivers/gpu/drm/arm/display/include/malidp_utils.h
drivers/gpu/drm/arm/display/komeda/komeda_pipeline_state.c
drivers/gpu/drm/msm/adreno/a6xx_gmu.c
drivers/net/ethernet/chelsio/cxgb3/cxgb3_main.c
drivers/virt/acrn/ioreq.c
fs/btrfs/misc.h
fs/ext2/balloc.c
fs/ext4/ext4.h
fs/ufs/util.h
include/linux/minmax.h
lib/logic_pio.c
net/netfilter/nf_nat_core.c
net/tipc/core.h
net/tipc/link.c
tools/testing/selftests/bpf/progs/get_branch_snapshot.c

index c3c34fe714b06f94d3dd0c7c60ef3cab1db4ac18..064ad508c149d4ec7be2f8a955fac4f19a8caac7 100644 (file)
@@ -25,7 +25,7 @@ static int change_page_range(pte_t *ptep, unsigned long addr, void *data)
        return 0;
 }
 
-static bool in_range(unsigned long start, unsigned long size,
+static bool range_in_range(unsigned long start, unsigned long size,
        unsigned long range_start, unsigned long range_end)
 {
        return start >= range_start && start < range_end &&
@@ -63,8 +63,8 @@ static int change_memory_common(unsigned long addr, int numpages,
        if (!size)
                return 0;
 
-       if (!in_range(start, size, MODULES_VADDR, MODULES_END) &&
-           !in_range(start, size, VMALLOC_START, VMALLOC_END))
+       if (!range_in_range(start, size, MODULES_VADDR, MODULES_END) &&
+           !range_in_range(start, size, VMALLOC_START, VMALLOC_END))
                return -EINVAL;
 
        return __change_memory_common(start, size, set_mask, clear_mask);
index 49a1d7f3539c28489f3372994a619cac618ba477..9f83baac6ed8715e7ac1809d1aa5539b57556f41 100644 (file)
@@ -35,7 +35,7 @@ static inline void set_range(struct malidp_range *rg, u32 start, u32 end)
        rg->end   = end;
 }
 
-static inline bool in_range(struct malidp_range *rg, u32 v)
+static inline bool malidp_in_range(struct malidp_range *rg, u32 v)
 {
        return (v >= rg->start) && (v <= rg->end);
 }
index 3276a3e82c628ecc47e512bbd5244157451dadd3..4618687a8f4d64e68d93b7324901fcea40942d93 100644 (file)
@@ -305,12 +305,12 @@ komeda_layer_check_cfg(struct komeda_layer *layer,
        if (komeda_fb_check_src_coords(kfb, src_x, src_y, src_w, src_h))
                return -EINVAL;
 
-       if (!in_range(&layer->hsize_in, src_w)) {
+       if (!malidp_in_range(&layer->hsize_in, src_w)) {
                DRM_DEBUG_ATOMIC("invalidate src_w %d.\n", src_w);
                return -EINVAL;
        }
 
-       if (!in_range(&layer->vsize_in, src_h)) {
+       if (!malidp_in_range(&layer->vsize_in, src_h)) {
                DRM_DEBUG_ATOMIC("invalidate src_h %d.\n", src_h);
                return -EINVAL;
        }
@@ -452,14 +452,14 @@ komeda_scaler_check_cfg(struct komeda_scaler *scaler,
        hsize_out = dflow->out_w;
        vsize_out = dflow->out_h;
 
-       if (!in_range(&scaler->hsize, hsize_in) ||
-           !in_range(&scaler->hsize, hsize_out)) {
+       if (!malidp_in_range(&scaler->hsize, hsize_in) ||
+           !malidp_in_range(&scaler->hsize, hsize_out)) {
                DRM_DEBUG_ATOMIC("Invalid horizontal sizes");
                return -EINVAL;
        }
 
-       if (!in_range(&scaler->vsize, vsize_in) ||
-           !in_range(&scaler->vsize, vsize_out)) {
+       if (!malidp_in_range(&scaler->vsize, vsize_in) ||
+           !malidp_in_range(&scaler->vsize, vsize_out)) {
                DRM_DEBUG_ATOMIC("Invalid vertical sizes");
                return -EINVAL;
        }
@@ -574,13 +574,13 @@ komeda_splitter_validate(struct komeda_splitter *splitter,
                return -EINVAL;
        }
 
-       if (!in_range(&splitter->hsize, dflow->in_w)) {
+       if (!malidp_in_range(&splitter->hsize, dflow->in_w)) {
                DRM_DEBUG_ATOMIC("split in_w:%d is out of the acceptable range.\n",
                                 dflow->in_w);
                return -EINVAL;
        }
 
-       if (!in_range(&splitter->vsize, dflow->in_h)) {
+       if (!malidp_in_range(&splitter->vsize, dflow->in_h)) {
                DRM_DEBUG_ATOMIC("split in_h: %d exceeds the acceptable range.\n",
                                 dflow->in_h);
                return -EINVAL;
@@ -624,13 +624,13 @@ komeda_merger_validate(struct komeda_merger *merger,
                return -EINVAL;
        }
 
-       if (!in_range(&merger->hsize_merged, output->out_w)) {
+       if (!malidp_in_range(&merger->hsize_merged, output->out_w)) {
                DRM_DEBUG_ATOMIC("merged_w: %d is out of the accepted range.\n",
                                 output->out_w);
                return -EINVAL;
        }
 
-       if (!in_range(&merger->vsize_merged, output->out_h)) {
+       if (!malidp_in_range(&merger->vsize_merged, output->out_h)) {
                DRM_DEBUG_ATOMIC("merged_h: %d is out of the accepted range.\n",
                                 output->out_h);
                return -EINVAL;
@@ -866,8 +866,8 @@ void komeda_complete_data_flow_cfg(struct komeda_layer *layer,
         * input/output range.
         */
        if (dflow->en_scaling && scaler)
-               dflow->en_split = !in_range(&scaler->hsize, dflow->in_w) ||
-                                 !in_range(&scaler->hsize, dflow->out_w);
+               dflow->en_split = !malidp_in_range(&scaler->hsize, dflow->in_w) ||
+                                 !malidp_in_range(&scaler->hsize, dflow->out_w);
 }
 
 static bool merger_is_available(struct komeda_pipeline *pipe,
index 5deb79924897afded45a9399e1e3e29e5e534921..d90011c813b9ba39ca12c3a1607901ef9b95eff3 100644 (file)
@@ -676,12 +676,6 @@ struct block_header {
        u32 data[];
 };
 
-/* this should be a general kernel helper */
-static int in_range(u32 addr, u32 start, u32 size)
-{
-       return addr >= start && addr < start + size;
-}
-
 static bool fw_block_mem(struct a6xx_gmu_bo *bo, const struct block_header *blk)
 {
        if (!in_range(blk->addr, bo->iova, bo->size))
index 9b84c8d8d30973598972b06929c829fe7fb1e64d..d117022d15d7f73cca002d9fadd697de21de26ba 100644 (file)
@@ -2126,7 +2126,7 @@ static const struct ethtool_ops cxgb_ethtool_ops = {
        .set_link_ksettings = set_link_ksettings,
 };
 
-static int in_range(int val, int lo, int hi)
+static int cxgb_in_range(int val, int lo, int hi)
 {
        return val < 0 || (val <= hi && val >= lo);
 }
@@ -2162,19 +2162,19 @@ static int cxgb_siocdevprivate(struct net_device *dev,
                        return -EINVAL;
                if (t.qset_idx >= SGE_QSETS)
                        return -EINVAL;
-               if (!in_range(t.intr_lat, 0, M_NEWTIMER) ||
-                   !in_range(t.cong_thres, 0, 255) ||
-                   !in_range(t.txq_size[0], MIN_TXQ_ENTRIES,
+               if (!cxgb_in_range(t.intr_lat, 0, M_NEWTIMER) ||
+                   !cxgb_in_range(t.cong_thres, 0, 255) ||
+                   !cxgb_in_range(t.txq_size[0], MIN_TXQ_ENTRIES,
                              MAX_TXQ_ENTRIES) ||
-                   !in_range(t.txq_size[1], MIN_TXQ_ENTRIES,
+                   !cxgb_in_range(t.txq_size[1], MIN_TXQ_ENTRIES,
                              MAX_TXQ_ENTRIES) ||
-                   !in_range(t.txq_size[2], MIN_CTRL_TXQ_ENTRIES,
+                   !cxgb_in_range(t.txq_size[2], MIN_CTRL_TXQ_ENTRIES,
                              MAX_CTRL_TXQ_ENTRIES) ||
-                   !in_range(t.fl_size[0], MIN_FL_ENTRIES,
+                   !cxgb_in_range(t.fl_size[0], MIN_FL_ENTRIES,
                              MAX_RX_BUFFERS) ||
-                   !in_range(t.fl_size[1], MIN_FL_ENTRIES,
+                   !cxgb_in_range(t.fl_size[1], MIN_FL_ENTRIES,
                              MAX_RX_JUMBO_BUFFERS) ||
-                   !in_range(t.rspq_size, MIN_RSPQ_ENTRIES,
+                   !cxgb_in_range(t.rspq_size, MIN_RSPQ_ENTRIES,
                              MAX_RSPQ_ENTRIES))
                        return -EINVAL;
 
index cecdc1c13af7b648a07f0e892d1ecb9f6d98b33d..29e1ef1915fd2ed51285a2c9066bc7591d52281b 100644 (file)
@@ -351,7 +351,7 @@ static bool handle_cf8cfc(struct acrn_vm *vm,
        return is_handled;
 }
 
-static bool in_range(struct acrn_ioreq_range *range,
+static bool acrn_in_range(struct acrn_ioreq_range *range,
                     struct acrn_io_request *req)
 {
        bool ret = false;
@@ -389,7 +389,7 @@ static struct acrn_ioreq_client *find_ioreq_client(struct acrn_vm *vm,
        list_for_each_entry(client, &vm->ioreq_clients, list) {
                read_lock_bh(&client->range_lock);
                list_for_each_entry(range, &client->range_list, list) {
-                       if (in_range(range, req)) {
+                       if (acrn_in_range(range, req)) {
                                found = client;
                                break;
                        }
index 005751a1291101a1ea7f19d3d9419da6516dd418..40f2d9f1a17a9c0af31dfe10243323ead05523da 100644 (file)
@@ -8,8 +8,6 @@
 #include <linux/math64.h>
 #include <linux/rbtree.h>
 
-#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
-
 /*
  * Enumerate bits using enum autoincrement. Define the @name as the n-th bit.
  */
index eca60b747c6b54025f2e3b6044d28a912e6e50f8..c8049c90323d6c17752fffee4b0828ffb463d743 100644 (file)
@@ -36,8 +36,6 @@
  */
 
 
-#define in_range(b, first, len)        ((b) >= (first) && (b) <= (first) + (len) - 1)
-
 struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
                                             unsigned int block_group,
                                             struct buffer_head ** bh)
index 0a2d55faa095ef262c1bd5462b756663aa289706..465db8f8c11dc5b257fa5504dfcce69176f52899 100644 (file)
@@ -3774,8 +3774,6 @@ static inline void set_bitmap_uptodate(struct buffer_head *bh)
        set_bit(BH_BITMAP_UPTODATE, &(bh)->b_state);
 }
 
-#define in_range(b, first, len)        ((b) >= (first) && (b) <= (first) + (len) - 1)
-
 /* For ioend & aio unwritten conversion wait queues */
 #define EXT4_WQ_HASH_SZ                37
 #define ext4_ioend_wq(v)   (&ext4__ioend_wq[((unsigned long)(v)) %\
index 4931bec1a01cad7f76433ca2ef1b01da3bd0ffa0..89247193d96d860a7c8bf1138042802f53e47bc5 100644 (file)
 #include <linux/fs.h>
 #include "swab.h"
 
-
-/*
- * some useful macros
- */
-#define in_range(b,first,len)  ((b)>=(first)&&(b)<(first)+(len))
-
 /*
  * functions used for retyping
  */
index 396df1121bffba9e3247018ec49256f25774b76d..4f011eb6533d168f5146cd7c05ef10da6cb555d5 100644 (file)
@@ -3,6 +3,7 @@
 #define _LINUX_MINMAX_H
 
 #include <linux/const.h>
+#include <linux/types.h>
 
 /*
  * min()/max()/clamp() macros must accomplish three things:
  */
 #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
index 07b4b9a1f54b6bf5ead2ee0fc2f4aa40c460ea2c..2ea564a400644274e8bfbe98ab0ddef616f1836d 100644 (file)
@@ -20,9 +20,6 @@
 static LIST_HEAD(io_range_list);
 static DEFINE_MUTEX(io_range_mutex);
 
-/* Consider a kernel general helper for this */
-#define in_range(b, first, len)        ((b) >= (first) && (b) < (first) + (len))
-
 /**
  * logic_pio_register_range - register logical PIO range for a host
  * @new_range: pointer to the IO range to be registered.
index fadbd4ed3dc047fa1fc3be82d049c61eaeb30f50..c4e0516a8dfab43e7eb1a9bf6b7b7ae2bd00d18b 100644 (file)
@@ -327,7 +327,7 @@ static bool l4proto_in_range(const struct nf_conntrack_tuple *tuple,
 /* If we source map this tuple so reply looks like reply_tuple, will
  * that meet the constraints of range.
  */
-static int in_range(const struct nf_conntrack_tuple *tuple,
+static int nf_in_range(const struct nf_conntrack_tuple *tuple,
                    const struct nf_nat_range2 *range)
 {
        /* If we are supposed to map IPs, then we must be in the
@@ -376,7 +376,7 @@ find_appropriate_src(struct net *net,
                                       &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
                        result->dst = tuple->dst;
 
-                       if (in_range(result, range))
+                       if (nf_in_range(result, range))
                                return 1;
                }
        }
@@ -607,7 +607,7 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple,
        if (maniptype == NF_NAT_MANIP_SRC &&
            !(range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL)) {
                /* try the original tuple first */
-               if (in_range(orig_tuple, range)) {
+               if (nf_in_range(orig_tuple, range)) {
                        if (!nf_nat_used_tuple(orig_tuple, ct)) {
                                *tuple = *orig_tuple;
                                return;
index 0a3f7a70a50a17b2785da8010e7a7d7814ac305b..7eccd97e0609798b17f8b0707d9eecf80794512d 100644 (file)
@@ -197,7 +197,7 @@ static inline int less(u16 left, u16 right)
        return less_eq(left, right) && (mod(right) != mod(left));
 }
 
-static inline int in_range(u16 val, u16 min, u16 max)
+static inline int tipc_in_range(u16 val, u16 min, u16 max)
 {
        return !less(val, min) && !more(val, max);
 }
index 2eff1c7949cbcc5f97855bb58373ec3595496aa0..e33b4f29f77cf2a9f34be9e669374771196633df 100644 (file)
@@ -1623,7 +1623,7 @@ next_gap_ack:
                                          last_ga->bgack_cnt);
                        }
                        /* Check against the last Gap ACK block */
-                       if (in_range(seqno, start, end))
+                       if (tipc_in_range(seqno, start, end))
                                continue;
                        /* Update/release the packet peer is acking */
                        bc_has_acked = true;
@@ -2251,12 +2251,12 @@ static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
                strncpy(if_name, data, TIPC_MAX_IF_NAME);
 
                /* Update own tolerance if peer indicates a non-zero value */
-               if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
+               if (tipc_in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
                        l->tolerance = peers_tol;
                        l->bc_rcvlink->tolerance = peers_tol;
                }
                /* Update own priority if peer's priority is higher */
-               if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
+               if (tipc_in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
                        l->priority = peers_prio;
 
                /* If peer is going down we want full re-establish cycle */
@@ -2299,13 +2299,13 @@ static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
                l->rcv_nxt_state = msg_seqno(hdr) + 1;
 
                /* Update own tolerance if peer indicates a non-zero value */
-               if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
+               if (tipc_in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
                        l->tolerance = peers_tol;
                        l->bc_rcvlink->tolerance = peers_tol;
                }
                /* Update own prio if peer indicates a different value */
                if ((peers_prio != l->priority) &&
-                   in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
+                   tipc_in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
                        l->priority = peers_prio;
                        rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
                }
index a1b139888048c934837239cd158612aac22ce7d2..511ac634eef0edfadf6788bfbae2d8eec269b6b8 100644 (file)
@@ -15,7 +15,7 @@ long total_entries = 0;
 #define ENTRY_CNT 32
 struct perf_branch_entry entries[ENTRY_CNT] = {};
 
-static inline bool in_range(__u64 val)
+static inline bool gbs_in_range(__u64 val)
 {
        return (val >= address_low) && (val < address_high);
 }
@@ -31,7 +31,7 @@ int BPF_PROG(test1, int n, int ret)
        for (i = 0; i < ENTRY_CNT; i++) {
                if (i >= total_entries)
                        break;
-               if (in_range(entries[i].from) && in_range(entries[i].to))
+               if (gbs_in_range(entries[i].from) && gbs_in_range(entries[i].to))
                        test1_hits++;
                else if (!test1_hits)
                        wasted_entries++;