LCOV - code coverage report
Current view: top level - lib - radix-tree.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 281 412 68.2 %
Date: 2014-02-18 Functions: 18 30 60.0 %
Branches: 201 307 65.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2001 Momchil Velikov
       3                 :            :  * Portions Copyright (C) 2001 Christoph Hellwig
       4                 :            :  * Copyright (C) 2005 SGI, Christoph Lameter
       5                 :            :  * Copyright (C) 2006 Nick Piggin
       6                 :            :  * Copyright (C) 2012 Konstantin Khlebnikov
       7                 :            :  *
       8                 :            :  * This program is free software; you can redistribute it and/or
       9                 :            :  * modify it under the terms of the GNU General Public License as
      10                 :            :  * published by the Free Software Foundation; either version 2, or (at
      11                 :            :  * your option) any later version.
      12                 :            :  *
      13                 :            :  * This program is distributed in the hope that it will be useful, but
      14                 :            :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16                 :            :  * General Public License for more details.
      17                 :            :  *
      18                 :            :  * You should have received a copy of the GNU General Public License
      19                 :            :  * along with this program; if not, write to the Free Software
      20                 :            :  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <linux/errno.h>
      24                 :            : #include <linux/init.h>
      25                 :            : #include <linux/kernel.h>
      26                 :            : #include <linux/export.h>
      27                 :            : #include <linux/radix-tree.h>
      28                 :            : #include <linux/percpu.h>
      29                 :            : #include <linux/slab.h>
      30                 :            : #include <linux/notifier.h>
      31                 :            : #include <linux/cpu.h>
      32                 :            : #include <linux/string.h>
      33                 :            : #include <linux/bitops.h>
      34                 :            : #include <linux/rcupdate.h>
      35                 :            : #include <linux/hardirq.h>                /* in_interrupt() */
      36                 :            : 
      37                 :            : 
      38                 :            : #ifdef __KERNEL__
      39                 :            : #define RADIX_TREE_MAP_SHIFT    (CONFIG_BASE_SMALL ? 4 : 6)
      40                 :            : #else
      41                 :            : #define RADIX_TREE_MAP_SHIFT    3       /* For more stressful testing */
      42                 :            : #endif
      43                 :            : 
      44                 :            : #define RADIX_TREE_MAP_SIZE     (1UL << RADIX_TREE_MAP_SHIFT)
      45                 :            : #define RADIX_TREE_MAP_MASK     (RADIX_TREE_MAP_SIZE-1)
      46                 :            : 
      47                 :            : #define RADIX_TREE_TAG_LONGS    \
      48                 :            :         ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
      49                 :            : 
      50                 :            : struct radix_tree_node {
      51                 :            :         unsigned int    height;         /* Height from the bottom */
      52                 :            :         unsigned int    count;
      53                 :            :         union {
      54                 :            :                 struct radix_tree_node *parent; /* Used when ascending tree */
      55                 :            :                 struct rcu_head rcu_head;       /* Used when freeing node */
      56                 :            :         };
      57                 :            :         void __rcu      *slots[RADIX_TREE_MAP_SIZE];
      58                 :            :         unsigned long   tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
      59                 :            : };
      60                 :            : 
      61                 :            : #define RADIX_TREE_INDEX_BITS  (8 /* CHAR_BIT */ * sizeof(unsigned long))
      62                 :            : #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
      63                 :            :                                           RADIX_TREE_MAP_SHIFT))
      64                 :            : 
      65                 :            : /*
      66                 :            :  * The height_to_maxindex array needs to be one deeper than the maximum
      67                 :            :  * path as height 0 holds only 1 entry.
      68                 :            :  */
      69                 :            : static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
      70                 :            : 
      71                 :            : /*
      72                 :            :  * Radix tree node cache.
      73                 :            :  */
      74                 :            : static struct kmem_cache *radix_tree_node_cachep;
      75                 :            : 
      76                 :            : /*
      77                 :            :  * The radix tree is variable-height, so an insert operation not only has
      78                 :            :  * to build the branch to its corresponding item, it also has to build the
      79                 :            :  * branch to existing items if the size has to be increased (by
      80                 :            :  * radix_tree_extend).
      81                 :            :  *
      82                 :            :  * The worst case is a zero height tree with just a single item at index 0,
      83                 :            :  * and then inserting an item at index ULONG_MAX. This requires 2 new branches
      84                 :            :  * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
      85                 :            :  * Hence:
      86                 :            :  */
      87                 :            : #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
      88                 :            : 
      89                 :            : /*
      90                 :            :  * Per-cpu pool of preloaded nodes
      91                 :            :  */
      92                 :            : struct radix_tree_preload {
      93                 :            :         int nr;
      94                 :            :         struct radix_tree_node *nodes[RADIX_TREE_PRELOAD_SIZE];
      95                 :            : };
      96                 :            : static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
      97                 :            : 
      98                 :            : static inline void *ptr_to_indirect(void *ptr)
      99                 :            : {
     100                 :       5120 :         return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
     101                 :            : }
     102                 :            : 
     103                 :            : static inline void *indirect_to_ptr(void *ptr)
     104                 :            : {
     105                 :   17244465 :         return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
     106                 :            : }
     107                 :            : 
     108                 :            : static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
     109                 :            : {
     110                 :     104821 :         return root->gfp_mask & __GFP_BITS_MASK;
     111                 :            : }
     112                 :            : 
     113                 :            : static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
     114                 :            :                 int offset)
     115                 :            : {
     116                 :     572815 :         __set_bit(offset, node->tags[tag]);
     117                 :            : }
     118                 :            : 
     119                 :            : static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
     120                 :            :                 int offset)
     121                 :            : {
     122                 :            :         __clear_bit(offset, node->tags[tag]);
     123                 :            : }
     124                 :            : 
     125                 :            : static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
     126                 :            :                 int offset)
     127                 :            : {
     128                 :   23817475 :         return test_bit(offset, node->tags[tag]);
     129                 :            : }
     130                 :            : 
     131                 :            : static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
     132                 :            : {
     133                 :     213799 :         root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
     134                 :            : }
     135                 :            : 
     136                 :            : static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
     137                 :            : {
     138                 :     201095 :         root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
     139                 :            : }
     140                 :            : 
     141                 :            : static inline void root_tag_clear_all(struct radix_tree_root *root)
     142                 :            : {
     143                 :      87825 :         root->gfp_mask &= __GFP_BITS_MASK;
     144                 :            : }
     145                 :            : 
     146                 :            : static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
     147                 :            : {
     148                 :    6654531 :         return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
     149                 :            : }
     150                 :            : 
     151                 :            : /*
     152                 :            :  * Returns 1 if any slot in the node has this tag set.
     153                 :            :  * Otherwise returns 0.
     154                 :            :  */
     155                 :            : static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
     156                 :            : {
     157                 :            :         int idx;
     158         [ +  + ]:    6080364 :         for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
     159         [ +  + ]:    5628379 :                 if (node->tags[tag][idx])
     160                 :            :                         return 1;
     161                 :            :         }
     162                 :            :         return 0;
     163                 :            : }
     164                 :            : 
     165                 :            : /**
     166                 :            :  * radix_tree_find_next_bit - find the next set bit in a memory region
     167                 :            :  *
     168                 :            :  * @addr: The address to base the search on
     169                 :            :  * @size: The bitmap size in bits
     170                 :            :  * @offset: The bitnumber to start searching at
     171                 :            :  *
     172                 :            :  * Unrollable variant of find_next_bit() for constant size arrays.
     173                 :            :  * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
     174                 :            :  * Returns next bit offset, or size if nothing found.
     175                 :            :  */
     176                 :            : static __always_inline unsigned long
     177                 :            : radix_tree_find_next_bit(const unsigned long *addr,
     178                 :            :                          unsigned long size, unsigned long offset)
     179                 :            : {
     180                 :            :         if (!__builtin_constant_p(size))
     181                 :            :                 return find_next_bit(addr, size, offset);
     182                 :            : 
     183         [ +  + ]:    5831099 :         if (offset < size) {
     184                 :            :                 unsigned long tmp;
     185                 :            : 
     186                 :     650652 :                 addr += offset / BITS_PER_LONG;
     187                 :     650652 :                 tmp = *addr >> (offset % BITS_PER_LONG);
     188         [ +  + ]:     650652 :                 if (tmp)
     189                 :     358094 :                         return __ffs(tmp) + offset;
     190                 :     292558 :                 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
     191         [ +  + ]:     854957 :                 while (offset < size) {
     192                 :     201608 :                         tmp = *++addr;
     193         [ +  + ]:     201608 :                         if (tmp)
     194                 :      72582 :                                 return __ffs(tmp) + offset;
     195                 :     129026 :                         offset += BITS_PER_LONG;
     196                 :            :                 }
     197                 :            :         }
     198                 :            :         return size;
     199                 :            : }
     200                 :            : 
     201                 :            : /*
     202                 :            :  * This assumes that the caller has performed appropriate preallocation, and
     203                 :            :  * that the caller has pinned this thread of control to the current CPU.
     204                 :            :  */
     205                 :            : static struct radix_tree_node *
     206                 :          0 : radix_tree_node_alloc(struct radix_tree_root *root)
     207                 :            : {
     208                 :            :         struct radix_tree_node *ret = NULL;
     209                 :            :         gfp_t gfp_mask = root_gfp_mask(root);
     210                 :            : 
     211                 :            :         /*
     212                 :            :          * Preload code isn't irq safe and it doesn't make sence to use
     213                 :            :          * preloading in the interrupt anyway as all the allocations have to
     214                 :            :          * be atomic. So just do normal allocation when in interrupt.
     215                 :            :          */
     216    [ +  + ][ + ]:     104821 :         if (!(gfp_mask & __GFP_WAIT) && !in_interrupt()) {
     217                 :            :                 struct radix_tree_preload *rtp;
     218                 :            : 
     219                 :            :                 /*
     220                 :            :                  * Provided the caller has preloaded here, we will always
     221                 :            :                  * succeed in getting a node here (and never reach
     222                 :            :                  * kmem_cache_alloc)
     223                 :            :                  */
     224                 :     209642 :                 rtp = &__get_cpu_var(radix_tree_preloads);
     225         [ +  + ]:     104821 :                 if (rtp->nr) {
     226                 :     104820 :                         ret = rtp->nodes[rtp->nr - 1];
     227                 :     104820 :                         rtp->nodes[rtp->nr - 1] = NULL;
     228                 :     104820 :                         rtp->nr--;
     229                 :            :                 }
     230                 :            :         }
     231         [ #  # ]:     104821 :         if (ret == NULL)
     232                 :          0 :                 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
     233                 :            : 
     234         [ -  + ]:     104819 :         BUG_ON(radix_tree_is_indirect_ptr(ret));
     235                 :     104819 :         return ret;
     236                 :            : }
     237                 :            : 
     238                 :          0 : static void radix_tree_node_rcu_free(struct rcu_head *head)
     239                 :            : {
     240                 :     104025 :         struct radix_tree_node *node =
     241                 :            :                         container_of(head, struct radix_tree_node, rcu_head);
     242                 :            :         int i;
     243                 :            : 
     244                 :            :         /*
     245                 :            :          * must only free zeroed nodes into the slab. radix_tree_shrink
     246                 :            :          * can leave us with a non-NULL entry in the first slot, so clear
     247                 :            :          * that here to make sure.
     248                 :            :          */
     249         [ +  + ]:     416138 :         for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
     250                 :     312113 :                 tag_clear(node, i, 0);
     251                 :            : 
     252                 :     104025 :         node->slots[0] = NULL;
     253                 :     104025 :         node->count = 0;
     254                 :            : 
     255                 :     104025 :         kmem_cache_free(radix_tree_node_cachep, node);
     256                 :     104062 : }
     257                 :            : 
     258                 :            : static inline void
     259                 :            : radix_tree_node_free(struct radix_tree_node *node)
     260                 :            : {
     261                 :     104065 :         call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
     262                 :            : }
     263                 :            : 
     264                 :            : /*
     265                 :            :  * Load up this CPU's radix_tree_node buffer with sufficient objects to
     266                 :            :  * ensure that the addition of a single element in the tree cannot fail.  On
     267                 :            :  * success, return zero, with preemption disabled.  On error, return -ENOMEM
     268                 :            :  * with preemption not disabled.
     269                 :            :  *
     270                 :            :  * To make use of this facility, the radix tree must be initialised without
     271                 :            :  * __GFP_WAIT being passed to INIT_RADIX_TREE().
     272                 :            :  */
     273                 :          0 : static int __radix_tree_preload(gfp_t gfp_mask)
     274                 :            : {
     275                 :            :         struct radix_tree_preload *rtp;
     276                 :            :         struct radix_tree_node *node;
     277                 :            :         int ret = -ENOMEM;
     278                 :            : 
     279                 :    2196872 :         preempt_disable();
     280                 :    4393496 :         rtp = &__get_cpu_var(radix_tree_preloads);
     281         [ +  + ]:    2301570 :         while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
     282                 :     104822 :                 preempt_enable();
     283                 :     104821 :                 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
     284         [ +  - ]:     104822 :                 if (node == NULL)
     285                 :            :                         goto out;
     286                 :     104822 :                 preempt_disable();
     287                 :     209644 :                 rtp = &__get_cpu_var(radix_tree_preloads);
     288         [ +  - ]:     104822 :                 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
     289                 :     104822 :                         rtp->nodes[rtp->nr++] = node;
     290                 :            :                 else
     291                 :     104822 :                         kmem_cache_free(radix_tree_node_cachep, node);
     292                 :            :         }
     293                 :            :         ret = 0;
     294                 :            : out:
     295                 :    2196748 :         return ret;
     296                 :            : }
     297                 :            : 
     298                 :            : /*
     299                 :            :  * Load up this CPU's radix_tree_node buffer with sufficient objects to
     300                 :            :  * ensure that the addition of a single element in the tree cannot fail.  On
     301                 :            :  * success, return zero, with preemption disabled.  On error, return -ENOMEM
     302                 :            :  * with preemption not disabled.
     303                 :            :  *
     304                 :            :  * To make use of this facility, the radix tree must be initialised without
     305                 :            :  * __GFP_WAIT being passed to INIT_RADIX_TREE().
     306                 :            :  */
     307                 :          0 : int radix_tree_preload(gfp_t gfp_mask)
     308                 :            : {
     309                 :            :         /* Warn on non-sensical use... */
     310 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(!(gfp_mask & __GFP_WAIT));
                 [ #  # ]
     311                 :          0 :         return __radix_tree_preload(gfp_mask);
     312                 :            : }
     313                 :            : EXPORT_SYMBOL(radix_tree_preload);
     314                 :            : 
     315                 :            : /*
     316                 :            :  * The same as above function, except we don't guarantee preloading happens.
     317                 :            :  * We do it, if we decide it helps. On success, return zero with preemption
     318                 :            :  * disabled. On error, return -ENOMEM with preemption not disabled.
     319                 :            :  */
     320                 :          0 : int radix_tree_maybe_preload(gfp_t gfp_mask)
     321                 :            : {
     322         [ +  - ]:    2196898 :         if (gfp_mask & __GFP_WAIT)
     323                 :    2196898 :                 return __radix_tree_preload(gfp_mask);
     324                 :            :         /* Preloading doesn't help anything with this gfp mask, skip it */
     325                 :          0 :         preempt_disable();
     326                 :          0 :         return 0;
     327                 :            : }
     328                 :            : EXPORT_SYMBOL(radix_tree_maybe_preload);
     329                 :            : 
     330                 :            : /*
     331                 :            :  *      Return the maximum key which can be store into a
     332                 :            :  *      radix tree with height HEIGHT.
     333                 :            :  */
     334                 :            : static inline unsigned long radix_tree_maxindex(unsigned int height)
     335                 :            : {
     336                 :   94227340 :         return height_to_maxindex[height];
     337                 :            : }
     338                 :            : 
     339                 :            : /*
     340                 :            :  *      Extend a radix tree so it can store key @index.
     341                 :            :  */
     342                 :          0 : static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
     343                 :            : {
     344                 :            :         struct radix_tree_node *node;
     345                 :            :         struct radix_tree_node *slot;
     346                 :            :         unsigned int height;
     347                 :            :         int tag;
     348                 :            : 
     349                 :            :         /* Figure out what the height should be.  */
     350                 :      54438 :         height = root->height + 1;
     351         [ +  + ]:      70178 :         while (index > radix_tree_maxindex(height))
     352                 :      15740 :                 height++;
     353                 :            : 
     354         [ +  + ]:      54438 :         if (root->rnode == NULL) {
     355                 :      20248 :                 root->height = height;
     356                 :      54438 :                 goto out;
     357                 :            :         }
     358                 :            : 
     359                 :            :         do {
     360                 :            :                 unsigned int newheight;
     361         [ +  - ]:      34213 :                 if (!(node = radix_tree_node_alloc(root)))
     362                 :            :                         return -ENOMEM;
     363                 :            : 
     364                 :            :                 /* Propagate the aggregated tag info into the new root */
     365         [ +  + ]:     136852 :                 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
     366         [ +  + ]:     102639 :                         if (root_tag_get(root, tag))
     367                 :            :                                 tag_set(node, tag, 0);
     368                 :            :                 }
     369                 :            : 
     370                 :            :                 /* Increase the height.  */
     371                 :      34213 :                 newheight = root->height+1;
     372                 :      34213 :                 node->height = newheight;
     373                 :      34213 :                 node->count = 1;
     374                 :      34213 :                 node->parent = NULL;
     375                 :      34213 :                 slot = root->rnode;
     376         [ +  + ]:      34213 :                 if (newheight > 1) {
     377                 :            :                         slot = indirect_to_ptr(slot);
     378                 :       5638 :                         slot->parent = node;
     379                 :            :                 }
     380                 :      34213 :                 node->slots[0] = slot;
     381                 :            :                 node = ptr_to_indirect(node);
     382                 :      34213 :                 rcu_assign_pointer(root->rnode, node);
     383                 :      34213 :                 root->height = newheight;
     384         [ +  + ]:      34213 :         } while (height > root->height);
     385                 :            : out:
     386                 :            :         return 0;
     387                 :            : }
     388                 :            : 
     389                 :            : /**
     390                 :            :  *      radix_tree_insert    -    insert into a radix tree
     391                 :            :  *      @root:          radix tree root
     392                 :            :  *      @index:         index key
     393                 :            :  *      @item:          item to insert
     394                 :            :  *
     395                 :            :  *      Insert an item into the radix tree at position @index.
     396                 :            :  */
     397                 :          0 : int radix_tree_insert(struct radix_tree_root *root,
     398                 :            :                         unsigned long index, void *item)
     399                 :            : {
     400                 :            :         struct radix_tree_node *node = NULL, *slot;
     401                 :            :         unsigned int height, shift;
     402                 :            :         int offset;
     403                 :            :         int error;
     404                 :            : 
     405         [ -  + ]:    2196936 :         BUG_ON(radix_tree_is_indirect_ptr(item));
     406                 :            : 
     407                 :            :         /* Make sure the tree is high enough.  */
     408         [ +  + ]:    2196936 :         if (index > radix_tree_maxindex(root->height)) {
     409                 :      54439 :                 error = radix_tree_extend(root, index);
     410            [ + ]:      54439 :                 if (error)
     411                 :            :                         return error;
     412                 :            :         }
     413                 :            : 
     414                 :    2197072 :         slot = indirect_to_ptr(root->rnode);
     415                 :            : 
     416                 :    2197072 :         height = root->height;
     417                 :    2197072 :         shift = (height-1) * RADIX_TREE_MAP_SHIFT;
     418                 :            : 
     419                 :            :         offset = 0;                     /* uninitialised var warning */
     420         [ +  + ]:    6781146 :         while (height > 0) {
     421         [ +  + ]:    4584298 :                 if (slot == NULL) {
     422                 :            :                         /* Have to add a child node.  */
     423            [ + ]:      70831 :                         if (!(slot = radix_tree_node_alloc(root)))
     424                 :            :                                 return -ENOMEM;
     425                 :      70608 :                         slot->height = height;
     426                 :      70608 :                         slot->parent = node;
     427         [ +  + ]:      70608 :                         if (node) {
     428                 :      50360 :                                 rcu_assign_pointer(node->slots[offset], slot);
     429                 :      50360 :                                 node->count++;
     430                 :            :                         } else
     431                 :      20248 :                                 rcu_assign_pointer(root->rnode, ptr_to_indirect(slot));
     432                 :            :                 }
     433                 :            : 
     434                 :            :                 /* Go a level down */
     435                 :    4584074 :                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
     436                 :            :                 node = slot;
     437                 :    4584074 :                 slot = node->slots[offset];
     438                 :    4584074 :                 shift -= RADIX_TREE_MAP_SHIFT;
     439                 :    4584074 :                 height--;
     440                 :            :         }
     441                 :            : 
     442            [ + ]:    2196848 :         if (slot != NULL)
     443                 :            :                 return -EEXIST;
     444                 :            : 
     445         [ +  + ]:    2196883 :         if (node) {
     446                 :    2132377 :                 node->count++;
     447                 :    2132377 :                 rcu_assign_pointer(node->slots[offset], item);
     448         [ -  + ]:    2132341 :                 BUG_ON(tag_get(node, 0, offset));
     449         [ -  + ]:    2132341 :                 BUG_ON(tag_get(node, 1, offset));
     450                 :            :         } else {
     451                 :      64506 :                 rcu_assign_pointer(root->rnode, item);
     452         [ -  + ]:      64506 :                 BUG_ON(root_tag_get(root, 0));
     453         [ -  + ]:      64506 :                 BUG_ON(root_tag_get(root, 1));
     454                 :            :         }
     455                 :            : 
     456                 :            :         return 0;
     457                 :            : }
     458                 :            : EXPORT_SYMBOL(radix_tree_insert);
     459                 :            : 
     460                 :            : /*
     461                 :            :  * is_slot == 1 : search for the slot.
     462                 :            :  * is_slot == 0 : search for the node.
     463                 :            :  */
     464                 :          0 : static void *radix_tree_lookup_element(struct radix_tree_root *root,
     465                 :            :                                 unsigned long index, int is_slot)
     466                 :            : {
     467                 :            :         unsigned int height, shift;
     468                 :            :         struct radix_tree_node *node, **slot;
     469                 :            : 
     470                 :   83841294 :         node = rcu_dereference_raw(root->rnode);
     471         [ +  + ]:   83841294 :         if (node == NULL)
     472                 :            :                 return NULL;
     473                 :            : 
     474         [ +  + ]:   83697284 :         if (!radix_tree_is_indirect_ptr(node)) {
     475         [ +  + ]:    1244751 :                 if (index > 0)
     476                 :            :                         return NULL;
     477         [ +  - ]:    1217623 :                 return is_slot ? (void *)&root->rnode : node;
     478                 :            :         }
     479                 :            :         node = indirect_to_ptr(node);
     480                 :            : 
     481                 :   82452533 :         height = node->height;
     482         [ +  + ]:   82452533 :         if (index > radix_tree_maxindex(height))
     483                 :            :                 return NULL;
     484                 :            : 
     485                 :   80594383 :         shift = (height-1) * RADIX_TREE_MAP_SHIFT;
     486                 :            : 
     487                 :            :         do {
     488                 :  143915090 :                 slot = (struct radix_tree_node **)
     489                 :  143915090 :                         (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
     490                 :  143915090 :                 node = rcu_dereference_raw(*slot);
     491            [ + ]:  143915090 :                 if (node == NULL)
     492                 :            :                         return NULL;
     493                 :            : 
     494                 :  140722789 :                 shift -= RADIX_TREE_MAP_SHIFT;
     495                 :  140722789 :                 height--;
     496         [ +  + ]:  224564083 :         } while (height > 0);
     497                 :            : 
     498         [ +  + ]:   88635370 :         return is_slot ? (void *)slot : indirect_to_ptr(node);
     499                 :            : }
     500                 :            : 
     501                 :            : /**
     502                 :            :  *      radix_tree_lookup_slot    -    lookup a slot in a radix tree
     503                 :            :  *      @root:          radix tree root
     504                 :            :  *      @index:         index key
     505                 :            :  *
     506                 :            :  *      Returns:  the slot corresponding to the position @index in the
     507                 :            :  *      radix tree @root. This is useful for update-if-exists operations.
     508                 :            :  *
     509                 :            :  *      This function can be called under rcu_read_lock iff the slot is not
     510                 :            :  *      modified by radix_tree_replace_slot, otherwise it must be called
     511                 :            :  *      exclusive from other writers. Any dereference of the slot must be done
     512                 :            :  *      using radix_tree_deref_slot.
     513                 :            :  */
     514                 :          0 : void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
     515                 :            : {
     516                 :   72067940 :         return (void **)radix_tree_lookup_element(root, index, 1);
     517                 :            : }
     518                 :            : EXPORT_SYMBOL(radix_tree_lookup_slot);
     519                 :            : 
     520                 :            : /**
     521                 :            :  *      radix_tree_lookup    -    perform lookup operation on a radix tree
     522                 :            :  *      @root:          radix tree root
     523                 :            :  *      @index:         index key
     524                 :            :  *
     525                 :            :  *      Lookup the item at the position @index in the radix tree @root.
     526                 :            :  *
     527                 :            :  *      This function can be called under rcu_read_lock, however the caller
     528                 :            :  *      must manage lifetimes of leaf nodes (eg. RCU may also be used to free
     529                 :            :  *      them safely). No RCU barriers are required to access or modify the
     530                 :            :  *      returned item, however.
     531                 :            :  */
     532                 :          0 : void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
     533                 :            : {
     534                 :   11771472 :         return radix_tree_lookup_element(root, index, 0);
     535                 :            : }
     536                 :            : EXPORT_SYMBOL(radix_tree_lookup);
     537                 :            : 
     538                 :            : /**
     539                 :            :  *      radix_tree_tag_set - set a tag on a radix tree node
     540                 :            :  *      @root:          radix tree root
     541                 :            :  *      @index:         index key
     542                 :            :  *      @tag:           tag index
     543                 :            :  *
     544                 :            :  *      Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
     545                 :            :  *      corresponding to @index in the radix tree.  From
     546                 :            :  *      the root all the way down to the leaf node.
     547                 :            :  *
     548                 :            :  *      Returns the address of the tagged item.   Setting a tag on a not-present
     549                 :            :  *      item is a bug.
     550                 :            :  */
     551                 :          0 : void *radix_tree_tag_set(struct radix_tree_root *root,
     552                 :            :                         unsigned long index, unsigned int tag)
     553                 :            : {
     554                 :            :         unsigned int height, shift;
     555                 :            :         struct radix_tree_node *slot;
     556                 :            : 
     557                 :    3171114 :         height = root->height;
     558         [ -  + ]:    3171114 :         BUG_ON(index > radix_tree_maxindex(height));
     559                 :            : 
     560                 :    3171114 :         slot = indirect_to_ptr(root->rnode);
     561                 :    3171114 :         shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
     562                 :            : 
     563         [ +  + ]:    9881516 :         while (height > 0) {
     564                 :            :                 int offset;
     565                 :            : 
     566                 :    6710296 :                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
     567         [ +  + ]:    6710296 :                 if (!tag_get(slot, tag, offset))
     568                 :            :                         tag_set(slot, tag, offset);
     569                 :    6710296 :                 slot = slot->slots[offset];
     570            [ + ]:    6710296 :                 BUG_ON(slot == NULL);
     571                 :    6710402 :                 shift -= RADIX_TREE_MAP_SHIFT;
     572                 :    6710402 :                 height--;
     573                 :            :         }
     574                 :            : 
     575                 :            :         /* set the root's tag bit */
     576    [ + ][ +  + ]:    3171220 :         if (slot && !root_tag_get(root, tag))
     577                 :            :                 root_tag_set(root, tag);
     578                 :            : 
     579                 :    3171220 :         return slot;
     580                 :            : }
     581                 :            : EXPORT_SYMBOL(radix_tree_tag_set);
     582                 :            : 
     583                 :            : /**
     584                 :            :  *      radix_tree_tag_clear - clear a tag on a radix tree node
     585                 :            :  *      @root:          radix tree root
     586                 :            :  *      @index:         index key
     587                 :            :  *      @tag:           tag index
     588                 :            :  *
     589                 :            :  *      Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
     590                 :            :  *      corresponding to @index in the radix tree.  If
     591                 :            :  *      this causes the leaf node to have no tags set then clear the tag in the
     592                 :            :  *      next-to-leaf node, etc.
     593                 :            :  *
     594                 :            :  *      Returns the address of the tagged item on success, else NULL.  ie:
     595                 :            :  *      has the same return value and semantics as radix_tree_lookup().
     596                 :            :  */
     597                 :          0 : void *radix_tree_tag_clear(struct radix_tree_root *root,
     598                 :            :                         unsigned long index, unsigned int tag)
     599                 :            : {
     600                 :            :         struct radix_tree_node *node = NULL;
     601                 :            :         struct radix_tree_node *slot = NULL;
     602                 :            :         unsigned int height, shift;
     603                 :            :         int uninitialized_var(offset);
     604                 :            : 
     605                 :    4103573 :         height = root->height;
     606            [ + ]:    4103573 :         if (index > radix_tree_maxindex(height))
     607                 :            :                 goto out;
     608                 :            : 
     609                 :    4104136 :         shift = height * RADIX_TREE_MAP_SHIFT;
     610                 :    4104136 :         slot = indirect_to_ptr(root->rnode);
     611                 :            : 
     612         [ +  + ]:   13279572 :         while (shift) {
     613         [ +  + ]:    9175943 :                 if (slot == NULL)
     614                 :            :                         goto out;
     615                 :            : 
     616                 :    9175436 :                 shift -= RADIX_TREE_MAP_SHIFT;
     617                 :    9175436 :                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
     618                 :            :                 node = slot;
     619                 :    9175436 :                 slot = slot->slots[offset];
     620                 :            :         }
     621                 :            : 
     622            [ + ]:    4103629 :         if (slot == NULL)
     623                 :            :                 goto out;
     624                 :            : 
     625         [ +  + ]:    4555552 :         while (node) {
     626         [ +  + ]:    4339643 :                 if (!tag_get(node, tag, offset))
     627                 :            :                         goto out;
     628                 :            :                 tag_clear(node, tag, offset);
     629         [ +  + ]:    3915709 :                 if (any_tag_set(node, tag))
     630                 :            :                         goto out;
     631                 :            : 
     632                 :     451954 :                 index >>= RADIX_TREE_MAP_SHIFT;
     633                 :     451954 :                 offset = index & RADIX_TREE_MAP_MASK;
     634                 :     451954 :                 node = node->parent;
     635                 :            :         }
     636                 :            : 
     637                 :            :         /* clear the root's tag bit */
     638         [ +  + ]:     215909 :         if (root_tag_get(root, tag))
     639                 :            :                 root_tag_clear(root, tag);
     640                 :            : 
     641                 :            : out:
     642                 :          0 :         return slot;
     643                 :            : }
     644                 :            : EXPORT_SYMBOL(radix_tree_tag_clear);
     645                 :            : 
     646                 :            : /**
     647                 :            :  * radix_tree_tag_get - get a tag on a radix tree node
     648                 :            :  * @root:               radix tree root
     649                 :            :  * @index:              index key
     650                 :            :  * @tag:                tag index (< RADIX_TREE_MAX_TAGS)
     651                 :            :  *
     652                 :            :  * Return values:
     653                 :            :  *
     654                 :            :  *  0: tag not present or not set
     655                 :            :  *  1: tag set
     656                 :            :  *
     657                 :            :  * Note that the return value of this function may not be relied on, even if
     658                 :            :  * the RCU lock is held, unless tag modification and node deletion are excluded
     659                 :            :  * from concurrency.
     660                 :            :  */
     661                 :          0 : int radix_tree_tag_get(struct radix_tree_root *root,
     662                 :            :                         unsigned long index, unsigned int tag)
     663                 :            : {
     664                 :            :         unsigned int height, shift;
     665                 :            :         struct radix_tree_node *node;
     666                 :            : 
     667                 :            :         /* check the root's tag bit */
     668         [ #  # ]:          0 :         if (!root_tag_get(root, tag))
     669                 :            :                 return 0;
     670                 :            : 
     671                 :          0 :         node = rcu_dereference_raw(root->rnode);
     672         [ #  # ]:          0 :         if (node == NULL)
     673                 :            :                 return 0;
     674                 :            : 
     675         [ #  # ]:          0 :         if (!radix_tree_is_indirect_ptr(node))
     676                 :          0 :                 return (index == 0);
     677                 :            :         node = indirect_to_ptr(node);
     678                 :            : 
     679                 :          0 :         height = node->height;
     680         [ #  # ]:          0 :         if (index > radix_tree_maxindex(height))
     681                 :            :                 return 0;
     682                 :            : 
     683                 :          0 :         shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
     684                 :            : 
     685                 :            :         for ( ; ; ) {
     686                 :            :                 int offset;
     687                 :            : 
     688         [ #  # ]:          0 :                 if (node == NULL)
     689                 :            :                         return 0;
     690                 :            : 
     691                 :          0 :                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
     692         [ #  # ]:          0 :                 if (!tag_get(node, tag, offset))
     693                 :            :                         return 0;
     694         [ #  # ]:          0 :                 if (height == 1)
     695                 :            :                         return 1;
     696                 :          0 :                 node = rcu_dereference_raw(node->slots[offset]);
     697                 :          0 :                 shift -= RADIX_TREE_MAP_SHIFT;
     698                 :          0 :                 height--;
     699                 :          0 :         }
     700                 :            : }
     701                 :            : EXPORT_SYMBOL(radix_tree_tag_get);
     702                 :            : 
     703                 :            : /**
     704                 :            :  * radix_tree_next_chunk - find next chunk of slots for iteration
     705                 :            :  *
     706                 :            :  * @root:       radix tree root
     707                 :            :  * @iter:       iterator state
     708                 :            :  * @flags:      RADIX_TREE_ITER_* flags and tag index
     709                 :            :  * Returns:     pointer to chunk first slot, or NULL if iteration is over
     710                 :            :  */
     711                 :          0 : void **radix_tree_next_chunk(struct radix_tree_root *root,
     712                 :            :                              struct radix_tree_iter *iter, unsigned flags)
     713                 :            : {
     714                 :    5177750 :         unsigned shift, tag = flags & RADIX_TREE_ITER_TAG_MASK;
     715                 :            :         struct radix_tree_node *rnode, *node;
     716                 :            :         unsigned long index, offset;
     717                 :            : 
     718 [ +  + ][ +  + ]:    5177750 :         if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
     719                 :            :                 return NULL;
     720                 :            : 
     721                 :            :         /*
     722                 :            :          * Catch next_index overflow after ~0UL. iter->index never overflows
     723                 :            :          * during iterating; it can be zero only at the beginning.
     724                 :            :          * And we cannot overflow iter->next_index in a single step,
     725                 :            :          * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
     726                 :            :          *
     727                 :            :          * This condition also used by radix_tree_next_slot() to stop
     728                 :            :          * contiguous iterating, and forbid swithing to the next chunk.
     729                 :            :          */
     730                 :    3199719 :         index = iter->next_index;
     731 [ +  + ][ +  + ]:    3199719 :         if (!index && iter->index)
     732                 :            :                 return NULL;
     733                 :            : 
     734                 :    3199716 :         rnode = rcu_dereference_raw(root->rnode);
     735         [ +  + ]:    3199716 :         if (radix_tree_is_indirect_ptr(rnode)) {
     736                 :            :                 rnode = indirect_to_ptr(rnode);
     737         [ +  + ]:    2011851 :         } else if (rnode && !index) {
     738                 :            :                 /* Single-slot tree */
     739                 :      71048 :                 iter->index = 0;
     740                 :      71048 :                 iter->next_index = 1;
     741                 :      71048 :                 iter->tags = 1;
     742                 :     462314 :                 return (void **)&root->rnode;
     743                 :            :         } else
     744                 :            :                 return NULL;
     745                 :            : 
     746                 :            : restart:
     747                 :    1579131 :         shift = (rnode->height - 1) * RADIX_TREE_MAP_SHIFT;
     748                 :    1579131 :         offset = index >> shift;
     749                 :            : 
     750                 :            :         /* Index outside of the tree */
     751         [ +  + ]:    1579131 :         if (offset >= RADIX_TREE_MAP_SIZE)
     752                 :            :                 return NULL;
     753                 :            : 
     754                 :            :         node = rnode;
     755                 :            :         while (1) {
     756 [ +  + ][ +  + ]:    2586187 :                 if ((flags & RADIX_TREE_ITER_TAGGED) ?
     757                 :    1437379 :                                 !test_bit(offset, node->tags[tag]) :
     758                 :    1148808 :                                 !node->slots[offset]) {
     759                 :            :                         /* Hole detected */
     760            [ + ]:     893211 :                         if (flags & RADIX_TREE_ITER_CONTIG)
     761                 :            :                                 return NULL;
     762                 :            : 
     763         [ +  + ]:    6071012 :                         if (flags & RADIX_TREE_ITER_TAGGED)
     764                 :    5831099 :                                 offset = radix_tree_find_next_bit(
     765                 :    5831099 :                                                 node->tags[tag],
     766                 :            :                                                 RADIX_TREE_MAP_SIZE,
     767                 :            :                                                 offset + 1);
     768                 :            :                         else
     769         [ +  + ]:    9029369 :                                 while (++offset < RADIX_TREE_MAP_SIZE) {
     770         [ +  + ]:    9099582 :                                         if (node->slots[offset])
     771                 :            :                                                 break;
     772                 :            :                                 }
     773                 :     893262 :                         index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1);
     774                 :     893262 :                         index += offset << shift;
     775                 :            :                         /* Overflow after ~0UL */
     776         [ +  + ]:     893262 :                         if (!index)
     777                 :            :                                 return NULL;
     778         [ +  + ]:     893202 :                         if (offset == RADIX_TREE_MAP_SIZE)
     779                 :            :                                 goto restart;
     780                 :            :                 }
     781                 :            : 
     782                 :            :                 /* This is leaf-node */
     783         [ +  + ]:    2194935 :                 if (!shift)
     784                 :            :                         break;
     785                 :            : 
     786                 :    1285923 :                 node = rcu_dereference_raw(node->slots[offset]);
     787         [ +  + ]:    1285923 :                 if (node == NULL)
     788                 :            :                         goto restart;
     789                 :    1285900 :                 shift -= RADIX_TREE_MAP_SHIFT;
     790                 :    1285900 :                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
     791                 :    1285900 :         }
     792                 :            : 
     793                 :            :         /* Update the iterator state */
     794                 :     909012 :         iter->index = index;
     795                 :     909012 :         iter->next_index = (index | RADIX_TREE_MAP_MASK) + 1;
     796                 :            : 
     797                 :            :         /* Construct iter->tags bit-mask from node->tags[tag] array */
     798         [ +  + ]:     909012 :         if (flags & RADIX_TREE_ITER_TAGGED) {
     799                 :            :                 unsigned tag_long, tag_bit;
     800                 :            : 
     801                 :     487226 :                 tag_long = offset / BITS_PER_LONG;
     802                 :     487226 :                 tag_bit  = offset % BITS_PER_LONG;
     803                 :     487226 :                 iter->tags = node->tags[tag][tag_long] >> tag_bit;
     804                 :            :                 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
     805         [ +  + ]:     487226 :                 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
     806                 :            :                         /* Pick tags from next element */
     807         [ +  + ]:     252923 :                         if (tag_bit)
     808                 :     204100 :                                 iter->tags |= node->tags[tag][tag_long + 1] <<
     809                 :     204100 :                                                 (BITS_PER_LONG - tag_bit);
     810                 :            :                         /* Clip chunk size, here only BITS_PER_LONG tags */
     811                 :     252923 :                         iter->next_index = index + BITS_PER_LONG;
     812                 :            :                 }
     813                 :            :         }
     814                 :            : 
     815                 :     909012 :         return node->slots + offset;
     816                 :            : }
     817                 :            : EXPORT_SYMBOL(radix_tree_next_chunk);
     818                 :            : 
     819                 :            : /**
     820                 :            :  * radix_tree_range_tag_if_tagged - for each item in given range set given
     821                 :            :  *                                 tag if item has another tag set
     822                 :            :  * @root:               radix tree root
     823                 :            :  * @first_indexp:       pointer to a starting index of a range to scan
     824                 :            :  * @last_index:         last index of a range to scan
     825                 :            :  * @nr_to_tag:          maximum number items to tag
     826                 :            :  * @iftag:              tag index to test
     827                 :            :  * @settag:             tag index to set if tested tag is set
     828                 :            :  *
     829                 :            :  * This function scans range of radix tree from first_index to last_index
     830                 :            :  * (inclusive).  For each item in the range if iftag is set, the function sets
     831                 :            :  * also settag. The function stops either after tagging nr_to_tag items or
     832                 :            :  * after reaching last_index.
     833                 :            :  *
     834                 :            :  * The tags must be set from the leaf level only and propagated back up the
     835                 :            :  * path to the root. We must do this so that we resolve the full path before
     836                 :            :  * setting any tags on intermediate nodes. If we set tags as we descend, then
     837                 :            :  * we can get to the leaf node and find that the index that has the iftag
     838                 :            :  * set is outside the range we are scanning. This reults in dangling tags and
     839                 :            :  * can lead to problems with later tag operations (e.g. livelocks on lookups).
     840                 :            :  *
     841                 :            :  * The function returns number of leaves where the tag was set and sets
     842                 :            :  * *first_indexp to the first unscanned index.
     843                 :            :  * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
     844                 :            :  * be prepared to handle that.
     845                 :            :  */
     846                 :          0 : unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
     847                 :            :                 unsigned long *first_indexp, unsigned long last_index,
     848                 :            :                 unsigned long nr_to_tag,
     849                 :            :                 unsigned int iftag, unsigned int settag)
     850                 :            : {
     851                 :      39127 :         unsigned int height = root->height;
     852                 :            :         struct radix_tree_node *node = NULL;
     853                 :            :         struct radix_tree_node *slot;
     854                 :            :         unsigned int shift;
     855                 :            :         unsigned long tagged = 0;
     856                 :      39127 :         unsigned long index = *first_indexp;
     857                 :            : 
     858                 :      39127 :         last_index = min(last_index, radix_tree_maxindex(height));
     859         [ +  + ]:      39127 :         if (index > last_index)
     860                 :            :                 return 0;
     861            [ + ]:      39125 :         if (!nr_to_tag)
     862                 :            :                 return 0;
     863         [ +  + ]:      78257 :         if (!root_tag_get(root, iftag)) {
     864                 :       1900 :                 *first_indexp = last_index + 1;
     865                 :       1900 :                 return 0;
     866                 :            :         }
     867         [ +  + ]:      76357 :         if (height == 0) {
     868                 :       2918 :                 *first_indexp = last_index + 1;
     869                 :            :                 root_tag_set(root, settag);
     870                 :       2918 :                 return 1;
     871                 :            :         }
     872                 :            : 
     873                 :      34312 :         shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
     874                 :      34312 :         slot = indirect_to_ptr(root->rnode);
     875                 :            : 
     876                 :            :         for (;;) {
     877                 :            :                 unsigned long upindex;
     878                 :            :                 int offset;
     879                 :            : 
     880                 :    4784185 :                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
     881         [ +  + ]:    4784185 :                 if (!slot->slots[offset])
     882                 :            :                         goto next;
     883         [ +  + ]:    1974740 :                 if (!tag_get(slot, iftag, offset))
     884                 :            :                         goto next;
     885         [ +  + ]:     641624 :                 if (shift) {
     886                 :            :                         /* Go down one level */
     887                 :      68809 :                         shift -= RADIX_TREE_MAP_SHIFT;
     888                 :            :                         node = slot;
     889                 :            :                         slot = slot->slots[offset];
     890                 :      68809 :                         continue;
     891                 :            :                 }
     892                 :            : 
     893                 :            :                 /* tag the leaf */
     894                 :     572815 :                 tagged++;
     895                 :            :                 tag_set(slot, settag, offset);
     896                 :            : 
     897                 :            :                 /* walk back up the path tagging interior nodes */
     898                 :            :                 upindex = index;
     899         [ +  + ]:     631707 :                 while (node) {
     900                 :      68416 :                         upindex >>= RADIX_TREE_MAP_SHIFT;
     901                 :      68416 :                         offset = upindex & RADIX_TREE_MAP_MASK;
     902                 :            : 
     903                 :            :                         /* stop if we find a node with the tag already set */
     904         [ +  + ]:      68416 :                         if (tag_get(node, settag, offset))
     905                 :            :                                 break;
     906                 :            :                         tag_set(node, settag, offset);
     907                 :      58892 :                         node = node->parent;
     908                 :            :                 }
     909                 :            : 
     910                 :            :                 /*
     911                 :            :                  * Small optimization: now clear that node pointer.
     912                 :            :                  * Since all of this slot's ancestors now have the tag set
     913                 :            :                  * from setting it above, we have no further need to walk
     914                 :            :                  * back up the tree setting tags, until we update slot to
     915                 :            :                  * point to another radix_tree_node.
     916                 :            :                  */
     917                 :            :                 node = NULL;
     918                 :            : 
     919                 :            : next:
     920                 :            :                 /* Go to next item at level determined by 'shift' */
     921                 :    4715376 :                 index = ((index >> shift) + 1) << shift;
     922                 :            :                 /* Overflow can happen when last_index is ~0UL... */
     923         [ +  + ]:    4715376 :                 if (index > last_index || !index)
     924                 :            :                         break;
     925         [ +  + ]:    4681613 :                 if (tagged >= nr_to_tag)
     926                 :            :                         break;
     927         [ +  + ]:    4736687 :                 while (((index >> shift) & RADIX_TREE_MAP_MASK) == 0) {
     928                 :            :                         /*
     929                 :            :                          * We've fully scanned this node. Go up. Because
     930                 :            :                          * last_index is guaranteed to be in the tree, what
     931                 :            :                          * we do below cannot wander astray.
     932                 :            :                          */
     933                 :      55623 :                         slot = slot->parent;
     934                 :      55623 :                         shift += RADIX_TREE_MAP_SHIFT;
     935                 :            :                 }
     936                 :            :         }
     937                 :            :         /*
     938                 :            :          * We need not to tag the root tag if there is no tag which is set with
     939                 :            :          * settag within the range from *first_indexp to last_index.
     940                 :            :          */
     941         [ +  + ]:      34312 :         if (tagged > 0)
     942                 :            :                 root_tag_set(root, settag);
     943                 :      34312 :         *first_indexp = index;
     944                 :            : 
     945                 :      34312 :         return tagged;
     946                 :            : }
     947                 :            : EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
     948                 :            : 
     949                 :            : 
     950                 :            : /**
     951                 :            :  *      radix_tree_next_hole    -    find the next hole (not-present entry)
     952                 :            :  *      @root:          tree root
     953                 :            :  *      @index:         index key
     954                 :            :  *      @max_scan:      maximum range to search
     955                 :            :  *
     956                 :            :  *      Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
     957                 :            :  *      indexed hole.
     958                 :            :  *
     959                 :            :  *      Returns: the index of the hole if found, otherwise returns an index
     960                 :            :  *      outside of the set specified (in which case 'return - index >= max_scan'
     961                 :            :  *      will be true). In rare cases of index wrap-around, 0 will be returned.
     962                 :            :  *
     963                 :            :  *      radix_tree_next_hole may be called under rcu_read_lock. However, like
     964                 :            :  *      radix_tree_gang_lookup, this will not atomically search a snapshot of
     965                 :            :  *      the tree at a single point in time. For example, if a hole is created
     966                 :            :  *      at index 5, then subsequently a hole is created at index 10,
     967                 :            :  *      radix_tree_next_hole covering both indexes may return 10 if called
     968                 :            :  *      under rcu_read_lock.
     969                 :            :  */
     970                 :          0 : unsigned long radix_tree_next_hole(struct radix_tree_root *root,
     971                 :            :                                 unsigned long index, unsigned long max_scan)
     972                 :            : {
     973                 :            :         unsigned long i;
     974                 :            : 
     975         [ +  + ]:      38142 :         for (i = 0; i < max_scan; i++) {
     976         [ +  + ]:      37673 :                 if (!radix_tree_lookup(root, index))
     977                 :            :                         break;
     978                 :      35857 :                 index++;
     979         [ +  - ]:      35857 :                 if (index == 0)
     980                 :            :                         break;
     981                 :            :         }
     982                 :            : 
     983                 :          0 :         return index;
     984                 :            : }
     985                 :            : EXPORT_SYMBOL(radix_tree_next_hole);
     986                 :            : 
     987                 :            : /**
     988                 :            :  *      radix_tree_prev_hole    -    find the prev hole (not-present entry)
     989                 :            :  *      @root:          tree root
     990                 :            :  *      @index:         index key
     991                 :            :  *      @max_scan:      maximum range to search
     992                 :            :  *
     993                 :            :  *      Search backwards in the range [max(index-max_scan+1, 0), index]
     994                 :            :  *      for the first hole.
     995                 :            :  *
     996                 :            :  *      Returns: the index of the hole if found, otherwise returns an index
     997                 :            :  *      outside of the set specified (in which case 'index - return >= max_scan'
     998                 :            :  *      will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
     999                 :            :  *
    1000                 :            :  *      radix_tree_next_hole may be called under rcu_read_lock. However, like
    1001                 :            :  *      radix_tree_gang_lookup, this will not atomically search a snapshot of
    1002                 :            :  *      the tree at a single point in time. For example, if a hole is created
    1003                 :            :  *      at index 10, then subsequently a hole is created at index 5,
    1004                 :            :  *      radix_tree_prev_hole covering both indexes may return 5 if called under
    1005                 :            :  *      rcu_read_lock.
    1006                 :            :  */
    1007                 :          0 : unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
    1008                 :            :                                    unsigned long index, unsigned long max_scan)
    1009                 :            : {
    1010                 :            :         unsigned long i;
    1011                 :            : 
    1012         [ +  + ]:     301752 :         for (i = 0; i < max_scan; i++) {
    1013         [ +  + ]:     296556 :                 if (!radix_tree_lookup(root, index))
    1014                 :            :                         break;
    1015                 :     199682 :                 index--;
    1016         [ +  + ]:     199682 :                 if (index == ULONG_MAX)
    1017                 :            :                         break;
    1018                 :            :         }
    1019                 :            : 
    1020                 :        208 :         return index;
    1021                 :            : }
    1022                 :            : EXPORT_SYMBOL(radix_tree_prev_hole);
    1023                 :            : 
    1024                 :            : /**
    1025                 :            :  *      radix_tree_gang_lookup - perform multiple lookup on a radix tree
    1026                 :            :  *      @root:          radix tree root
    1027                 :            :  *      @results:       where the results of the lookup are placed
    1028                 :            :  *      @first_index:   start the lookup from this key
    1029                 :            :  *      @max_items:     place up to this many items at *results
    1030                 :            :  *
    1031                 :            :  *      Performs an index-ascending scan of the tree for present items.  Places
    1032                 :            :  *      them at *@results and returns the number of items which were placed at
    1033                 :            :  *      *@results.
    1034                 :            :  *
    1035                 :            :  *      The implementation is naive.
    1036                 :            :  *
    1037                 :            :  *      Like radix_tree_lookup, radix_tree_gang_lookup may be called under
    1038                 :            :  *      rcu_read_lock. In this case, rather than the returned results being
    1039                 :            :  *      an atomic snapshot of the tree at a single point in time, the semantics
    1040                 :            :  *      of an RCU protected gang lookup are as though multiple radix_tree_lookups
    1041                 :            :  *      have been issued in individual locks, and results stored in 'results'.
    1042                 :            :  */
    1043                 :            : unsigned int
    1044                 :          0 : radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
    1045                 :            :                         unsigned long first_index, unsigned int max_items)
    1046                 :            : {
    1047                 :            :         struct radix_tree_iter iter;
    1048                 :            :         void **slot;
    1049                 :            :         unsigned int ret = 0;
    1050                 :            : 
    1051         [ #  # ]:          0 :         if (unlikely(!max_items))
    1052                 :            :                 return 0;
    1053                 :            : 
    1054 [ #  # ][ #  # ]:          0 :         radix_tree_for_each_slot(slot, root, &iter, first_index) {
    1055                 :          0 :                 results[ret] = indirect_to_ptr(rcu_dereference_raw(*slot));
    1056         [ #  # ]:          0 :                 if (!results[ret])
    1057                 :          0 :                         continue;
    1058         [ #  # ]:          0 :                 if (++ret == max_items)
    1059                 :            :                         break;
    1060                 :            :         }
    1061                 :            : 
    1062                 :          0 :         return ret;
    1063                 :            : }
    1064                 :            : EXPORT_SYMBOL(radix_tree_gang_lookup);
    1065                 :            : 
    1066                 :            : /**
    1067                 :            :  *      radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
    1068                 :            :  *      @root:          radix tree root
    1069                 :            :  *      @results:       where the results of the lookup are placed
    1070                 :            :  *      @indices:       where their indices should be placed (but usually NULL)
    1071                 :            :  *      @first_index:   start the lookup from this key
    1072                 :            :  *      @max_items:     place up to this many items at *results
    1073                 :            :  *
    1074                 :            :  *      Performs an index-ascending scan of the tree for present items.  Places
    1075                 :            :  *      their slots at *@results and returns the number of items which were
    1076                 :            :  *      placed at *@results.
    1077                 :            :  *
    1078                 :            :  *      The implementation is naive.
    1079                 :            :  *
    1080                 :            :  *      Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
    1081                 :            :  *      be dereferenced with radix_tree_deref_slot, and if using only RCU
    1082                 :            :  *      protection, radix_tree_deref_slot may fail requiring a retry.
    1083                 :            :  */
    1084                 :            : unsigned int
    1085                 :          0 : radix_tree_gang_lookup_slot(struct radix_tree_root *root,
    1086                 :            :                         void ***results, unsigned long *indices,
    1087                 :            :                         unsigned long first_index, unsigned int max_items)
    1088                 :            : {
    1089                 :            :         struct radix_tree_iter iter;
    1090                 :            :         void **slot;
    1091                 :            :         unsigned int ret = 0;
    1092                 :            : 
    1093         [ #  # ]:          0 :         if (unlikely(!max_items))
    1094                 :            :                 return 0;
    1095                 :            : 
    1096 [ #  # ][ #  # ]:          0 :         radix_tree_for_each_slot(slot, root, &iter, first_index) {
    1097                 :          0 :                 results[ret] = slot;
    1098         [ #  # ]:          0 :                 if (indices)
    1099                 :          0 :                         indices[ret] = iter.index;
    1100         [ #  # ]:          0 :                 if (++ret == max_items)
    1101                 :            :                         break;
    1102                 :            :         }
    1103                 :            : 
    1104                 :          0 :         return ret;
    1105                 :            : }
    1106                 :            : EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
    1107                 :            : 
    1108                 :            : /**
    1109                 :            :  *      radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
    1110                 :            :  *                                   based on a tag
    1111                 :            :  *      @root:          radix tree root
    1112                 :            :  *      @results:       where the results of the lookup are placed
    1113                 :            :  *      @first_index:   start the lookup from this key
    1114                 :            :  *      @max_items:     place up to this many items at *results
    1115                 :            :  *      @tag:           the tag index (< RADIX_TREE_MAX_TAGS)
    1116                 :            :  *
    1117                 :            :  *      Performs an index-ascending scan of the tree for present items which
    1118                 :            :  *      have the tag indexed by @tag set.  Places the items at *@results and
    1119                 :            :  *      returns the number of items which were placed at *@results.
    1120                 :            :  */
    1121                 :            : unsigned int
    1122                 :          0 : radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
    1123                 :            :                 unsigned long first_index, unsigned int max_items,
    1124                 :            :                 unsigned int tag)
    1125                 :            : {
    1126                 :            :         struct radix_tree_iter iter;
    1127                 :            :         void **slot;
    1128                 :            :         unsigned int ret = 0;
    1129                 :            : 
    1130         [ #  # ]:          0 :         if (unlikely(!max_items))
    1131                 :            :                 return 0;
    1132                 :            : 
    1133 [ #  # ][ #  # ]:          0 :         radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
    1134                 :          0 :                 results[ret] = indirect_to_ptr(rcu_dereference_raw(*slot));
    1135         [ #  # ]:          0 :                 if (!results[ret])
    1136                 :          0 :                         continue;
    1137         [ #  # ]:          0 :                 if (++ret == max_items)
    1138                 :            :                         break;
    1139                 :            :         }
    1140                 :            : 
    1141                 :          0 :         return ret;
    1142                 :            : }
    1143                 :            : EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
    1144                 :            : 
    1145                 :            : /**
    1146                 :            :  *      radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
    1147                 :            :  *                                        radix tree based on a tag
    1148                 :            :  *      @root:          radix tree root
    1149                 :            :  *      @results:       where the results of the lookup are placed
    1150                 :            :  *      @first_index:   start the lookup from this key
    1151                 :            :  *      @max_items:     place up to this many items at *results
    1152                 :            :  *      @tag:           the tag index (< RADIX_TREE_MAX_TAGS)
    1153                 :            :  *
    1154                 :            :  *      Performs an index-ascending scan of the tree for present items which
    1155                 :            :  *      have the tag indexed by @tag set.  Places the slots at *@results and
    1156                 :            :  *      returns the number of slots which were placed at *@results.
    1157                 :            :  */
    1158                 :            : unsigned int
    1159                 :          0 : radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
    1160                 :            :                 unsigned long first_index, unsigned int max_items,
    1161                 :            :                 unsigned int tag)
    1162                 :            : {
    1163                 :            :         struct radix_tree_iter iter;
    1164                 :            :         void **slot;
    1165                 :            :         unsigned int ret = 0;
    1166                 :            : 
    1167         [ #  # ]:          0 :         if (unlikely(!max_items))
    1168                 :            :                 return 0;
    1169                 :            : 
    1170 [ #  # ][ #  # ]:          0 :         radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
    1171                 :          0 :                 results[ret] = slot;
    1172         [ #  # ]:          0 :                 if (++ret == max_items)
    1173                 :            :                         break;
    1174                 :            :         }
    1175                 :            : 
    1176                 :          0 :         return ret;
    1177                 :            : }
    1178                 :            : EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
    1179                 :            : 
    1180                 :            : #if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
    1181                 :            : #include <linux/sched.h> /* for cond_resched() */
    1182                 :            : 
    1183                 :            : /*
    1184                 :            :  * This linear search is at present only useful to shmem_unuse_inode().
    1185                 :            :  */
    1186                 :          0 : static unsigned long __locate(struct radix_tree_node *slot, void *item,
    1187                 :            :                               unsigned long index, unsigned long *found_index)
    1188                 :            : {
    1189                 :            :         unsigned int shift, height;
    1190                 :            :         unsigned long i;
    1191                 :            : 
    1192                 :          0 :         height = slot->height;
    1193                 :          0 :         shift = (height-1) * RADIX_TREE_MAP_SHIFT;
    1194                 :            : 
    1195         [ #  # ]:          0 :         for ( ; height > 1; height--) {
    1196                 :          0 :                 i = (index >> shift) & RADIX_TREE_MAP_MASK;
    1197                 :            :                 for (;;) {
    1198         [ #  # ]:          0 :                         if (slot->slots[i] != NULL)
    1199                 :            :                                 break;
    1200                 :          0 :                         index &= ~((1UL << shift) - 1);
    1201                 :          0 :                         index += 1UL << shift;
    1202         [ #  # ]:          0 :                         if (index == 0)
    1203                 :            :                                 goto out;       /* 32-bit wraparound */
    1204                 :          0 :                         i++;
    1205         [ #  # ]:          0 :                         if (i == RADIX_TREE_MAP_SIZE)
    1206                 :            :                                 goto out;
    1207                 :            :                 }
    1208                 :            : 
    1209                 :          0 :                 shift -= RADIX_TREE_MAP_SHIFT;
    1210                 :          0 :                 slot = rcu_dereference_raw(slot->slots[i]);
    1211         [ #  # ]:          0 :                 if (slot == NULL)
    1212                 :            :                         goto out;
    1213                 :            :         }
    1214                 :            : 
    1215                 :            :         /* Bottom level: check items */
    1216         [ #  # ]:          0 :         for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
    1217         [ #  # ]:          0 :                 if (slot->slots[i] == item) {
    1218                 :          0 :                         *found_index = index + i;
    1219                 :            :                         index = 0;
    1220                 :          0 :                         goto out;
    1221                 :            :                 }
    1222                 :            :         }
    1223                 :          0 :         index += RADIX_TREE_MAP_SIZE;
    1224                 :            : out:
    1225                 :          0 :         return index;
    1226                 :            : }
    1227                 :            : 
    1228                 :            : /**
    1229                 :            :  *      radix_tree_locate_item - search through radix tree for item
    1230                 :            :  *      @root:          radix tree root
    1231                 :            :  *      @item:          item to be found
    1232                 :            :  *
    1233                 :            :  *      Returns index where item was found, or -1 if not found.
    1234                 :            :  *      Caller must hold no lock (since this time-consuming function needs
    1235                 :            :  *      to be preemptible), and must check afterwards if item is still there.
    1236                 :            :  */
    1237                 :          0 : unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
    1238                 :            : {
    1239                 :            :         struct radix_tree_node *node;
    1240                 :            :         unsigned long max_index;
    1241                 :            :         unsigned long cur_index = 0;
    1242                 :          0 :         unsigned long found_index = -1;
    1243                 :            : 
    1244                 :            :         do {
    1245                 :            :                 rcu_read_lock();
    1246                 :          0 :                 node = rcu_dereference_raw(root->rnode);
    1247         [ #  # ]:          0 :                 if (!radix_tree_is_indirect_ptr(node)) {
    1248                 :            :                         rcu_read_unlock();
    1249         [ #  # ]:          0 :                         if (node == item)
    1250                 :          0 :                                 found_index = 0;
    1251                 :            :                         break;
    1252                 :            :                 }
    1253                 :            : 
    1254                 :            :                 node = indirect_to_ptr(node);
    1255                 :          0 :                 max_index = radix_tree_maxindex(node->height);
    1256         [ #  # ]:          0 :                 if (cur_index > max_index)
    1257                 :            :                         break;
    1258                 :            : 
    1259                 :          0 :                 cur_index = __locate(node, item, cur_index, &found_index);
    1260                 :            :                 rcu_read_unlock();
    1261                 :          0 :                 cond_resched();
    1262         [ #  # ]:          0 :         } while (cur_index != 0 && cur_index <= max_index);
    1263                 :            : 
    1264                 :          0 :         return found_index;
    1265                 :            : }
    1266                 :            : #else
    1267                 :            : unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
    1268                 :            : {
    1269                 :            :         return -1;
    1270                 :            : }
    1271                 :            : #endif /* CONFIG_SHMEM && CONFIG_SWAP */
    1272                 :            : 
    1273                 :            : /**
    1274                 :            :  *      radix_tree_shrink    -    shrink height of a radix tree to minimal
    1275                 :            :  *      @root           radix tree root
    1276                 :            :  */
    1277                 :            : static inline void radix_tree_shrink(struct radix_tree_root *root)
    1278                 :            : {
    1279                 :            :         /* try to shrink tree height */
    1280         [ +  + ]:     356646 :         while (root->height > 0) {
    1281                 :     355790 :                 struct radix_tree_node *to_free = root->rnode;
    1282                 :            :                 struct radix_tree_node *slot;
    1283                 :            : 
    1284         [ +  + ]:     355790 :                 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
    1285                 :            :                 to_free = indirect_to_ptr(to_free);
    1286                 :            : 
    1287                 :            :                 /*
    1288                 :            :                  * The candidate node has more than one child, or its child
    1289                 :            :                  * is not at the leftmost slot, we cannot shrink.
    1290                 :            :                  */
    1291         [ +  + ]:     355784 :                 if (to_free->count != 1)
    1292                 :            :                         break;
    1293         [ +  + ]:      35879 :                 if (!to_free->slots[0])
    1294                 :            :                         break;
    1295                 :            : 
    1296                 :            :                 /*
    1297                 :            :                  * We don't need rcu_assign_pointer(), since we are simply
    1298                 :            :                  * moving the node from one part of the tree to another: if it
    1299                 :            :                  * was safe to dereference the old pointer to it
    1300                 :            :                  * (to_free->slots[0]), it will be safe to dereference the new
    1301                 :            :                  * one (root->rnode) as far as dependent read barriers go.
    1302                 :            :                  */
    1303                 :            :                 slot = to_free->slots[0];
    1304         [ +  + ]:       5976 :                 if (root->height > 1) {
    1305                 :       5120 :                         slot->parent = NULL;
    1306                 :            :                         slot = ptr_to_indirect(slot);
    1307                 :            :                 }
    1308                 :       5976 :                 root->rnode = slot;
    1309                 :       5976 :                 root->height--;
    1310                 :            : 
    1311                 :            :                 /*
    1312                 :            :                  * We have a dilemma here. The node's slot[0] must not be
    1313                 :            :                  * NULLed in case there are concurrent lookups expecting to
    1314                 :            :                  * find the item. However if this was a bottom-level node,
    1315                 :            :                  * then it may be subject to the slot pointer being visible
    1316                 :            :                  * to callers dereferencing it. If item corresponding to
    1317                 :            :                  * slot[0] is subsequently deleted, these callers would expect
    1318                 :            :                  * their slot to become empty sooner or later.
    1319                 :            :                  *
    1320                 :            :                  * For example, lockless pagecache will look up a slot, deref
    1321                 :            :                  * the page pointer, and if the page is 0 refcount it means it
    1322                 :            :                  * was concurrently deleted from pagecache so try the deref
    1323                 :            :                  * again. Fortunately there is already a requirement for logic
    1324                 :            :                  * to retry the entire slot lookup -- the indirect pointer
    1325                 :            :                  * problem (replacing direct root node with an indirect pointer
    1326                 :            :                  * also results in a stale slot). So tag the slot as indirect
    1327                 :            :                  * to force callers to retry.
    1328                 :            :                  */
    1329         [ +  + ]:       5976 :                 if (root->height == 0)
    1330                 :        856 :                         *((unsigned long *)&to_free->slots[0]) |=
    1331                 :            :                                                 RADIX_TREE_INDIRECT_PTR;
    1332                 :            : 
    1333                 :            :                 radix_tree_node_free(to_free);
    1334                 :            :         }
    1335                 :            : }
    1336                 :            : 
    1337                 :            : /**
    1338                 :            :  *      radix_tree_delete    -    delete an item from a radix tree
    1339                 :            :  *      @root:          radix tree root
    1340                 :            :  *      @index:         index key
    1341                 :            :  *
    1342                 :            :  *      Remove the item at @index from the radix tree rooted at @root.
    1343                 :            :  *
    1344                 :            :  *      Returns the address of the deleted item, or NULL if it was not present.
    1345                 :            :  */
    1346                 :          0 : void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
    1347                 :            : {
    1348                 :            :         struct radix_tree_node *node = NULL;
    1349                 :            :         struct radix_tree_node *slot = NULL;
    1350                 :            :         struct radix_tree_node *to_free;
    1351                 :            :         unsigned int height, shift;
    1352                 :            :         int tag;
    1353                 :            :         int uninitialized_var(offset);
    1354                 :            : 
    1355                 :    2193879 :         height = root->height;
    1356         [ +  + ]:    2193879 :         if (index > radix_tree_maxindex(height))
    1357                 :            :                 goto out;
    1358                 :            : 
    1359                 :    2193844 :         slot = root->rnode;
    1360         [ +  + ]:    2193844 :         if (height == 0) {
    1361                 :            :                 root_tag_clear_all(root);
    1362                 :      40589 :                 root->rnode = NULL;
    1363                 :      40589 :                 goto out;
    1364                 :            :         }
    1365                 :            :         slot = indirect_to_ptr(slot);
    1366                 :    2153255 :         shift = height * RADIX_TREE_MAP_SHIFT;
    1367                 :            : 
    1368                 :            :         do {
    1369            [ + ]:    4949487 :                 if (slot == NULL)
    1370                 :            :                         goto out;
    1371                 :            : 
    1372                 :    4949497 :                 shift -= RADIX_TREE_MAP_SHIFT;
    1373                 :    4949497 :                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
    1374                 :            :                 node = slot;
    1375                 :    4949497 :                 slot = slot->slots[offset];
    1376         [ +  + ]:    4949497 :         } while (shift);
    1377                 :            : 
    1378            [ + ]:    2153265 :         if (slot == NULL)
    1379                 :            :                 goto out;
    1380                 :            : 
    1381                 :            :         /*
    1382                 :            :          * Clear all tags associated with the item to be deleted.
    1383                 :            :          * This way of doing it would be inefficient, but seldom is any set.
    1384                 :            :          */
    1385         [ +  + ]:    8612984 :         for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
    1386         [ +  + ]:    6459698 :                 if (tag_get(node, tag, offset))
    1387                 :    1180081 :                         radix_tree_tag_clear(root, index, tag);
    1388                 :            :         }
    1389                 :            : 
    1390                 :            :         to_free = NULL;
    1391                 :            :         /* Now free the nodes we do not need anymore */
    1392         [ +  + ]:    2251376 :         while (node) {
    1393                 :    2204140 :                 node->slots[offset] = NULL;
    1394                 :    2204140 :                 node->count--;
    1395                 :            :                 /*
    1396                 :            :                  * Queue the node for deferred freeing after the
    1397                 :            :                  * last reference to it disappears (set NULL, above).
    1398                 :            :                  */
    1399         [ +  + ]:    2204140 :                 if (to_free)
    1400                 :            :                         radix_tree_node_free(to_free);
    1401                 :            : 
    1402         [ +  + ]:    2204139 :                 if (node->count) {
    1403         [ +  + ]:    2106049 :                         if (node == indirect_to_ptr(root->rnode))
    1404                 :            :                                 radix_tree_shrink(root);
    1405                 :            :                         goto out;
    1406                 :            :                 }
    1407                 :            : 
    1408                 :            :                 /* Node with zero slots in use so free it */
    1409                 :            :                 to_free = node;
    1410                 :            : 
    1411                 :      98090 :                 index >>= RADIX_TREE_MAP_SHIFT;
    1412                 :      98090 :                 offset = index & RADIX_TREE_MAP_MASK;
    1413                 :      98090 :                 node = node->parent;
    1414                 :            :         }
    1415                 :            : 
    1416                 :            :         root_tag_clear_all(root);
    1417                 :      47236 :         root->height = 0;
    1418                 :      47236 :         root->rnode = NULL;
    1419         [ +  - ]:      47236 :         if (to_free)
    1420                 :            :                 radix_tree_node_free(to_free);
    1421                 :            : 
    1422                 :            : out:
    1423                 :          0 :         return slot;
    1424                 :            : }
    1425                 :            : EXPORT_SYMBOL(radix_tree_delete);
    1426                 :            : 
    1427                 :            : /**
    1428                 :            :  *      radix_tree_tagged - test whether any items in the tree are tagged
    1429                 :            :  *      @root:          radix tree root
    1430                 :            :  *      @tag:           tag to test
    1431                 :            :  */
    1432                 :          0 : int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
    1433                 :            : {
    1434                 :     342390 :         return root_tag_get(root, tag);
    1435                 :            : }
    1436                 :            : EXPORT_SYMBOL(radix_tree_tagged);
    1437                 :            : 
    1438                 :            : static void
    1439                 :          0 : radix_tree_node_ctor(void *node)
    1440                 :            : {
    1441                 :      14924 :         memset(node, 0, sizeof(struct radix_tree_node));
    1442                 :      14924 : }
    1443                 :            : 
    1444                 :          0 : static __init unsigned long __maxindex(unsigned int height)
    1445                 :            : {
    1446                 :          0 :         unsigned int width = height * RADIX_TREE_MAP_SHIFT;
    1447                 :          0 :         int shift = RADIX_TREE_INDEX_BITS - width;
    1448                 :            : 
    1449         [ #  # ]:          0 :         if (shift < 0)
    1450                 :            :                 return ~0UL;
    1451         [ #  # ]:          0 :         if (shift >= BITS_PER_LONG)
    1452                 :            :                 return 0UL;
    1453                 :          0 :         return ~0UL >> shift;
    1454                 :            : }
    1455                 :            : 
    1456                 :          0 : static __init void radix_tree_init_maxindex(void)
    1457                 :            : {
    1458                 :            :         unsigned int i;
    1459                 :            : 
    1460         [ #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
    1461                 :          0 :                 height_to_maxindex[i] = __maxindex(i);
    1462                 :          0 : }
    1463                 :            : 
    1464                 :          0 : static int radix_tree_callback(struct notifier_block *nfb,
    1465                 :            :                             unsigned long action,
    1466                 :            :                             void *hcpu)
    1467                 :            : {
    1468                 :          0 :        int cpu = (long)hcpu;
    1469                 :            :        struct radix_tree_preload *rtp;
    1470                 :            : 
    1471                 :            :        /* Free per-cpu pool of perloaded nodes */
    1472         [ #  # ]:          0 :        if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
    1473                 :          0 :                rtp = &per_cpu(radix_tree_preloads, cpu);
    1474         [ #  # ]:          0 :                while (rtp->nr) {
    1475                 :          0 :                        kmem_cache_free(radix_tree_node_cachep,
    1476                 :          0 :                                        rtp->nodes[rtp->nr-1]);
    1477                 :          0 :                        rtp->nodes[rtp->nr-1] = NULL;
    1478                 :          0 :                        rtp->nr--;
    1479                 :            :                }
    1480                 :            :        }
    1481                 :          0 :        return NOTIFY_OK;
    1482                 :            : }
    1483                 :            : 
    1484                 :          0 : void __init radix_tree_init(void)
    1485                 :            : {
    1486                 :          0 :         radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
    1487                 :            :                         sizeof(struct radix_tree_node), 0,
    1488                 :            :                         SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
    1489                 :            :                         radix_tree_node_ctor);
    1490                 :          0 :         radix_tree_init_maxindex();
    1491                 :          0 :         hotcpu_notifier(radix_tree_callback, 0);
    1492                 :          0 : }

Generated by: LCOV version 1.9