09 Sep, 2017

3 commits

  • While overall the code is very nicely commented, it might not be
    immediately obvious from the diagrams what is going on. Add a very
    brief summary of each case. Opposite cases where the node is the left
    child are left untouched.

    Link: http://lkml.kernel.org/r/20170719014603.19029-4-dave@stgolabs.net
    Signed-off-by: Davidlohr Bueso
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Davidlohr Bueso
     
  • The only times the nil-parent (root node) condition is true is when the
    node is the first in the tree, or after fixing rbtree rule #4 and the
    case 1 rebalancing made the node the root. Such conditions do not apply
    most of the time:

    (i) The common case in an rbtree is to have more than a single node,
    so this is only true for the first rb_insert().

    (ii) While there is a chance only one first rotation is needed, cases
    where the node's uncle is black (cases 2,3) are more common as we can
    have the following scenarios during the rotation looping:

    case1 only, case1+1, case2+3, case1+2+3, case3 only, etc.

    This patch, therefore, adds an unlikely() optimization to this
    conditional. When profiling with CONFIG_PROFILE_ANNOTATED_BRANCHES, a
    kernel build shows that the incorrect rate is less than 15%, and for
    workloads that involve insert mostly trees overtime tend to have less
    than 2% incorrect rate.

    Link: http://lkml.kernel.org/r/20170719014603.19029-3-dave@stgolabs.net
    Signed-off-by: Davidlohr Bueso
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Davidlohr Bueso
     
  • Patch series "rbtree: Cache leftmost node internally", v4.

    A series to extending rbtrees to internally cache the leftmost node such
    that we can have fast overlap check optimization for all interval tree
    users[1]. The benefits of this series are that:

    (i) Unify users that do internal leftmost node caching.
    (ii) Optimize all interval tree users.
    (iii) Convert at least two new users (epoll and procfs) to the new interface.

    This patch (of 16):

    Red-black tree semantics imply that nodes with smaller or greater (or
    equal for duplicates) keys always be to the left and right,
    respectively. For the kernel this is extremely evident when considering
    our rb_first() semantics. Enabling lookups for the smallest node in the
    tree in O(1) can save a good chunk of cycles in not having to walk down
    the tree each time. To this end there are a few core users that
    explicitly do this, such as the scheduler and rtmutexes. There is also
    the desire for interval trees to have this optimization allowing faster
    overlap checking.

    This patch introduces a new 'struct rb_root_cached' which is just the
    root with a cached pointer to the leftmost node. The reason why the
    regular rb_root was not extended instead of adding a new structure was
    that this allows the user to have the choice between memory footprint
    and actual tree performance. The new wrappers on top of the regular
    rb_root calls are:

    - rb_first_cached(cached_root) -- which is a fast replacement
    for rb_first.

    - rb_insert_color_cached(node, cached_root, new)

    - rb_erase_cached(node, cached_root)

    In addition, augmented cached interfaces are also added for basic
    insertion and deletion operations; which becomes important for the
    interval tree changes.

    With the exception of the inserts, which adds a bool for updating the
    new leftmost, the interfaces are kept the same. To this end, porting rb
    users to the cached version becomes really trivial, and keeping current
    rbtree semantics for users that don't care about the optimization
    requires zero overhead.

    Link: http://lkml.kernel.org/r/20170719014603.19029-2-dave@stgolabs.net
    Signed-off-by: Davidlohr Bueso
    Reviewed-by: Jan Kara
    Acked-by: Peter Zijlstra (Intel)
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Davidlohr Bueso
     

25 Feb, 2017

1 commit

  • Prepare to mark sensitive kernel structures for randomization by making
    sure they're using designated initializers. These were identified
    during allyesconfig builds of x86, arm, and arm64, with most initializer
    fixes extracted from grsecurity.

    Link: http://lkml.kernel.org/r/20161217010253.GA140470@beast
    Signed-off-by: Kees Cook
    Acked-by: Peter Zijlstra (Intel)
    Cc: David Howells
    Cc: Jie Chen
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Kees Cook
     

13 Dec, 2016

1 commit

  • In Case 3 of `sibling == parent->rb_right':

    Right rotation will not change color of sl and S in the diagram
    (i.e. should not change "sl" to "Sl", "S" to "s")

    In Case 3 of `sibling == parent->rb_left':

    (p) (p)
    / \ / \
    S N --> sr N
    / \ /
    Sl sr S
    /
    Sl

    This is actually left rotation at "S", not right rotation.

    In Case 4 of `sibling == parent->rb_left':

    (p) (s)
    / \ / \
    S N --> Sl P
    / \ / \
    sl (sr) (sr) N

    This is actually right rotation at "(p)" + color flips, not left
    rotation + color flips.

    Link: http://lkml.kernel.org/r/1472391115-3702-1-git-send-email-fykcee1@gmail.com
    Signed-off-by: Jie Chen
    Cc: Wei Yang
    Cc: Xiao Guangrong
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Jie Chen
     

06 Jul, 2016

1 commit


28 May, 2015

1 commit

  • Change the insert and erase code such that lockless searches are
    non-fatal.

    In and of itself an rbtree cannot be correctly searched while
    in-modification, we can however provide weaker guarantees that will
    allow the rbtree to be used in conjunction with other techniques, such
    as latches; see 9b0fd802e8c0 ("seqcount: Add raw_write_seqcount_latch()").

    For this to work we need the following guarantees from the rbtree
    code:

    1) a lockless reader must not see partial stores, this would allow it
    to observe nodes that are invalid memory.

    2) there must not be (temporary) loops in the tree structure in the
    modifier's program order, this would cause a lookup which
    interrupts the modifier to get stuck indefinitely.

    For 1) we must use WRITE_ONCE() for all updates to the tree structure;
    in particular this patch only does rb_{left,right} as those are the
    only element required for simple searches.

    It generates slightly worse code, probably because volatile. But in
    pointer chasing heavy code a few instructions more should not matter.

    For 2) I have carefully audited the code and drawn every intermediate
    link state and not found a loop.

    Cc: Mathieu Desnoyers
    Cc: "Paul E. McKenney"
    Cc: Oleg Nesterov
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Cc: Rik van Riel
    Reviewed-by: Michel Lespinasse
    Signed-off-by: Peter Zijlstra (Intel)
    Signed-off-by: Rusty Russell

    Peter Zijlstra
     

09 Aug, 2014

1 commit

  • In case 1, it passes down the BLACK color from G to p and u, and maintains
    the color of n. By doing so, it maintains the black height of the
    sub-tree.

    While in the comment, it marks the color of n to BLACK. This is a typo
    and not consistents with the code.

    This patch fixs this typo in comment.

    Signed-off-by: Wei Yang
    Acked-by: Michel Lespinasse
    Cc: Xiao Guangrong
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Wei Yang
     

12 Sep, 2013

1 commit

  • Postorder iteration yields all of a node's children prior to yielding the
    node itself, and this particular implementation also avoids examining the
    leaf links in a node after that node has been yielded.

    In what I expect will be its most common usage, postorder iteration allows
    the deletion of every node in an rbtree without modifying the rbtree nodes
    (no _requirement_ that they be nulled) while avoiding referencing child
    nodes after they have been "deleted" (most commonly, freed).

    I have only updated zswap to use this functionality at this point, but
    numerous bits of code (most notably in the filesystem drivers) use a hand
    rolled postorder iteration that NULLs child links as it traverses the
    tree. Each of those instances could be replaced with this common
    implementation.

    1 & 2 add rbtree postorder iteration functions.
    3 adds testing of the iteration to the rbtree runtime tests
    4 allows building the rbtree runtime tests as builtins
    5 updates zswap.

    This patch:

    Add postorder iteration functions for rbtree. These are useful for safely
    freeing an entire rbtree without modifying the tree at all.

    Signed-off-by: Cody P Schafer
    Reviewed-by: Seth Jennings
    Cc: David Woodhouse
    Cc: Rik van Riel
    Cc: Michel Lespinasse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Cody P Schafer
     

12 Jan, 2013

1 commit

  • lib/rbtree.c declared __rb_erase_color() as __always_inline void, and
    then exported it with EXPORT_SYMBOL.

    This was because __rb_erase_color() must be exported for augmented
    rbtree users, but it must also be inlined into rb_erase() so that the
    dummy callback can get optimized out of that call site.

    (Actually with a modern compiler, none of the dummy callback functions
    should even be generated as separate text functions).

    The above usage is legal C, but it was unusual enough for some compilers
    to warn about it. This change makes things more explicit, with a static
    __always_inline ____rb_erase_color function for use in rb_erase(), and a
    separate non-inline __rb_erase_color function for use in
    rb_erase_augmented call sites.

    Signed-off-by: Michel Lespinasse
    Reported-by: Wu Fengguang
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     

09 Oct, 2012

17 commits

  • Provide rb_insert_augmented() and rb_erase_augmented() through a new
    rbtree_augmented.h include file. rb_erase_augmented() is defined there as
    an __always_inline function, in order to allow inlining of augmented
    rbtree callbacks into it. Since this generates a relatively large
    function, each augmented rbtree user should make sure to have a single
    call site.

    Signed-off-by: Michel Lespinasse
    Cc: Rik van Riel
    Cc: Hillf Danton
    Cc: Peter Zijlstra
    Cc: Catalin Marinas
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • convert arch/x86/mm/pat_rbtree.c to the proposed augmented rbtree api
    and remove the old augmented rbtree implementation.

    Signed-off-by: Michel Lespinasse
    Acked-by: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • Introduce new augmented rbtree APIs that allow minimal recalculation of
    augmented node information.

    A new callback is added to the rbtree insertion and erase rebalancing
    functions, to be called on each tree rotations. Such rotations preserve
    the subtree's root augmented value, but require recalculation of the one
    child that was previously located at the subtree root.

    In the insertion case, the handcoded search phase must be updated to
    maintain the augmented information on insertion, and then the rbtree
    coloring/rebalancing algorithms keep it up to date.

    In the erase case, things are more complicated since it is library
    code that manipulates the rbtree in order to remove internal nodes.
    This requires a couple additional callbacks to copy a subtree's
    augmented value when a new root is stitched in, and to recompute
    augmented values down the ancestry path when a node is removed from
    the tree.

    In order to preserve maximum speed for the non-augmented case,
    we provide two versions of each tree manipulation function.
    rb_insert_augmented() is the augmented equivalent of rb_insert_color(),
    and rb_erase_augmented() is the augmented equivalent of rb_erase().

    Signed-off-by: Michel Lespinasse
    Acked-by: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • Various minor optimizations in rb_erase():
    - Avoid multiple loading of node->__rb_parent_color when computing parent
    and color information (possibly not in close sequence, as there might
    be further branches in the algorithm)
    - In the 1-child subcase of case 1, copy the __rb_parent_color field from
    the erased node to the child instead of recomputing it from the desired
    parent and color
    - When searching for the erased node's successor, differentiate between
    cases 2 and 3 based on whether any left links were followed. This avoids
    a condition later down.
    - In case 3, keep a pointer to the erased node's right child so we don't
    have to refetch it later to adjust its parent.
    - In the no-childs subcase of cases 2 and 3, place the rebalance assigment
    last so that the compiler can remove the following if(rebalance) test.

    Also, added some comments to illustrate cases 2 and 3.

    Signed-off-by: Michel Lespinasse
    Acked-by: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • An interesting observation for rb_erase() is that when a node has
    exactly one child, the node must be black and the child must be red.
    An interesting consequence is that removing such a node can be done by
    simply replacing it with its child and making the child black,
    which we can do efficiently in rb_erase(). __rb_erase_color() then
    only needs to handle the no-childs case and can be modified accordingly.

    Signed-off-by: Michel Lespinasse
    Acked-by: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • In rb_erase, move the easy case (node to erase has no more than
    1 child) first. I feel the code reads easier that way.

    Signed-off-by: Michel Lespinasse
    Reviewed-by: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • Add __rb_change_child() as an inline helper function to replace code that
    would otherwise be duplicated 4 times in the source.

    No changes to binary size or speed.

    Signed-off-by: Michel Lespinasse
    Reviewed-by: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • When looking to fetch a node's sibling, we went through a sequence of:
    - check if node is the parent's left child
    - if it is, then fetch the parent's right child

    This can be replaced with:
    - fetch the parent's right child as an assumed sibling
    - check that node is NOT the fetched child

    This avoids fetching the parent's left child when node is actually
    that child. Saves a bit on code size, though it doesn't seem to make
    a large difference in speed.

    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Cc: David Woodhouse
    Acked-by: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • Set comment and indentation style to be consistent with linux coding style
    and the rest of the file, as suggested by Peter Zijlstra

    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • In __rb_erase_color(), we often already have pointers to the nodes being
    rotated and/or know what their colors must be, so we can generate more
    efficient code than the generic __rb_rotate_left() and __rb_rotate_right()
    functions.

    Also when the current node is red or when flipping the sibling's color,
    the parent is already known so we can use the more efficient
    rb_set_parent_color() function to set the desired color.

    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • In __rb_erase_color(), we have to select one of 3 cases depending on the
    color on the 'other' node children. If both children are black, we flip a
    few node colors and iterate. Otherwise, we do either one or two tree
    rotations, depending on the color of the 'other' child opposite to 'node',
    and then we are done.

    The corresponding logic had duplicate checks for the color of the 'other'
    child opposite to 'node'. It was checking it first to determine if both
    children are black, and then to determine how many tree rotations are
    required. Rearrange the logic to avoid that extra check.

    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • In __rb_erase_color(), we were always setting a node to black after
    exiting the main loop. And in one case, after fixing up the tree to
    satisfy all rbtree invariants, we were setting the current node to root
    just to guarantee a loop exit, at which point the root would be set to
    black. However this is not necessary, as the root of an rbtree is already
    known to be black. The only case where the color flip is required is when
    we exit the loop due to the current node being red, and it's easiest to
    just do the flip at that point instead of doing it after the loop.

    [adrian.hunter@intel.com: perf tools: fix build for another rbtree.c change]
    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: Adrian Hunter
    Cc: Alexander Shishkin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • - Use the newly introduced rb_set_parent_color() function to flip the color
    of nodes whose parent is already known.
    - Optimize rb_parent() when the node is known to be red - there is no need
    to mask out the color in that case.
    - Flipping gparent's color to red requires us to fetch its rb_parent_color
    field, so we can reuse it as the parent value for the next loop iteration.
    - Do not use __rb_rotate_left() and __rb_rotate_right() to handle tree
    rotations: we already have pointers to all relevant nodes, and know their
    colors (either because we want to adjust it, or because we've tested it,
    or we can deduce it as black due to the node proximity to a known red node).
    So we can generate more efficient code by making use of the node pointers
    we already have, and setting both the parent and color attributes for
    nodes all at once. Also in Case 2, some node attributes don't have to
    be set because we know another tree rotation (Case 3) will always follow
    and override them.

    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • The root node of an rbtree must always be black. However,
    rb_insert_color() only needs to maintain this invariant when it has been
    broken - that is, when it exits the loop due to the current (red) node
    being the root. In all other cases (exiting after tree rotations, or
    exiting due to an existing black parent) the invariant is already
    satisfied, so there is no need to adjust the root node color.

    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • It is a well known property of rbtrees that insertion never requires more
    than two tree rotations. In our implementation, after one loop iteration
    identified one or two necessary tree rotations, we would iterate and look
    for more. However at that point the node's parent would always be black,
    which would cause us to exit the loop.

    We can make the code flow more obvious by just adding a break statement
    after the tree rotations, where we know we are done. Additionally, in the
    cases where two tree rotations are necessary, we don't have to update the
    'node' pointer as it wouldn't be used until the next loop iteration, which
    we now avoid due to this break statement.

    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • rbtree users must use the documented APIs to manipulate the tree
    structure. Low-level helpers to manipulate node colors and parenthood are
    not part of that API, so move them to lib/rbtree.c

    [dwmw2@infradead.org: fix jffs2 build issue due to renamed __rb_parent_color field]
    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Signed-off-by: David Woodhouse
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     
  • Empty nodes have no color. We can make use of this property to simplify
    the code emitted by the RB_EMPTY_NODE and RB_CLEAR_NODE macros. Also,
    we can get rid of the rb_init_node function which had been introduced by
    commit 88d19cf37952 ("timers: Add rb_init_node() to allow for stack
    allocated rb nodes") to avoid some issue with the empty node's color not
    being initialized.

    I'm not sure what the RB_EMPTY_NODE checks in rb_prev() / rb_next() are
    doing there, though. axboe introduced them in commit 10fd48f2376d
    ("rbtree: fixed reversed RB_EMPTY_NODE and rb_next/prev"). The way I
    see it, the 'empty node' abstraction is only used by rbtree users to
    flag nodes that they haven't inserted in any rbtree, so asking the
    predecessor or successor of such nodes doesn't make any sense.

    One final rb_init_node() caller was recently added in sysctl code to
    implement faster sysctl name lookups. This code doesn't make use of
    RB_EMPTY_NODE at all, and from what I could see it only called
    rb_init_node() under the mistaken assumption that such initialization was
    required before node insertion.

    [sfr@canb.auug.org.au: fix net/ceph/osd_client.c build]
    Signed-off-by: Michel Lespinasse
    Cc: Andrea Arcangeli
    Acked-by: David Woodhouse
    Cc: Rik van Riel
    Cc: Peter Zijlstra
    Cc: Daniel Santos
    Cc: Jens Axboe
    Cc: "Eric W. Biederman"
    Cc: John Stultz
    Signed-off-by: Stephen Rothwell
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Michel Lespinasse
     

08 Mar, 2012

1 commit


28 Jan, 2011

1 commit


05 Jul, 2010

1 commit

  • Reimplement augmented RB-trees without sprinkling extra branches
    all over the RB-tree code (which lives in the scheduler hot path).

    This approach is 'borrowed' from Fabio's BFQ implementation and
    relies on traversing the rebalance path after the RB-tree-op to
    correct the heap property for insertion/removal and make up for
    the damage done by the tree rotations.

    For insertion the rebalance path is trivially that from the new
    node upwards to the root, for removal it is that from the deepest
    node in the path from the to be removed node that will still
    be around after the removal.

    [ This patch also fixes a video driver regression reported by
    Ali Gholami Rudi - the memtype->subtree_max_end was updated
    incorrectly. ]

    Acked-by: Suresh Siddha
    Acked-by: Venkatesh Pallipadi
    Signed-off-by: Peter Zijlstra
    Tested-by: Ali Gholami Rudi
    Cc: Fabio Checconi
    Cc: "H. Peter Anvin"
    Cc: Andrew Morton
    Cc: Linus Torvalds
    LKML-Reference:
    Signed-off-by: Ingo Molnar

    Peter Zijlstra
     

19 Feb, 2010

1 commit

  • Add support for augmented rbtrees in core rbtree code.

    This will be used in subsequent patches, in x86 PAT code, which needs
    interval trees to efficiently keep track of PAT ranges.

    Signed-off-by: Venkatesh Pallipadi
    LKML-Reference:
    Signed-off-by: Suresh Siddha
    Signed-off-by: H. Peter Anvin

    Pallipadi, Venkatesh
     

17 Jun, 2009

3 commits

  • Furthermore, notice that the initial checks:

    if (!node->rb_left)
    child = node->rb_right;
    else if (!node->rb_right)
    child = node->rb_left;
    else
    {
    ...
    }
    guarantee that old->rb_right is set in the final else branch, therefore
    we can omit checking that again.

    Signed-off-by: Wolfram Strepp
    Signed-off-by: Peter Zijlstra
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Wolfram Strepp
     
  • There are two cases when a node, having 2 childs, is erased:
    'normal case': the successor is not the right-hand-child of the node to be erased
    'special case': the successor is the right-hand child of the node to be erased

    Here some ascii-art, with following symbols (referring to the code):
    O: node to be deleted
    N: the successor of O
    P: parent of N
    C: child of N
    L: some other node

    normal case:

    O N
    / \ / \
    / \ / \
    L \ L \
    / \ P ----> / \ P
    / \ / \
    / /
    N C
    \ / \
    \
    C
    / \

    special case:
    O|P N
    / \ / \
    / \ / \
    L \ L \
    / \ N ----> / C
    \ / \
    \
    C
    / \

    Notice that for the special case we don't have to reconnect C to N.

    Signed-off-by: Wolfram Strepp
    Signed-off-by: Peter Zijlstra
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Wolfram Strepp
     
  • First, move some code around in order to make the next change more obvious.

    [akpm@linux-foundation.org: coding-style fixes]
    Signed-off-by: Peter Zijlstra
    Signed-off-by: Wolfram Strepp
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Wolfram Strepp
     

01 Apr, 2009

1 commit

  • Tfour 4 redundant if-conditions in function __rb_erase_color() in
    lib/rbtree.c are removed.

    In pseudo-source-code, the structure of the code is as follows:

    if ((!A || B) && (!C || D)) {
    .
    .
    .
    } else {
    if (!C || D) {//if this is true, it implies: (A == true) && (B == false)
    if (A) {//hence this always evaluates to 'true'...
    .
    }
    .
    //at this point, C always becomes true, because of:
    __rb_rotate_right/left();
    //and:
    other = parent->rb_right/left;
    }
    .
    .
    if (C) {//...and this too !
    .
    }
    }

    Signed-off-by: Wolfram Strepp
    Acked-by: Peter Zijlstra
    Cc: Andrea Arcangeli
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Wolfram Strepp
     

10 Jan, 2009

1 commit

  • The 'rb_first()', 'rb_last()', 'rb_next()' and 'rb_prev()' calls
    take a pointer to an RB node or RB root. They do not change the
    pointed objects, so add a 'const' qualifier in order to make life
    of the users of these functions easier.

    Indeed, if I have my own constant pointer &const struct my_type *p,
    and I call 'rb_next(&p->rb)', I get a GCC warning:

    warning: passing argument 1 of ‘rb_next’ discards qualifiers from pointer target type

    Signed-off-by: Artem Bityutskiy
    Signed-off-by: David Woodhouse
    Signed-off-by: Linus Torvalds

    Artem Bityutskiy
     

01 Oct, 2006

1 commit


06 Jun, 2006

1 commit


21 Apr, 2006

2 commits

  • We only used a single bit for colour information, so having a whole
    machine word of space allocated for it was a bit wasteful. Instead,
    store it in the lowest bit of the 'parent' pointer, since that was
    always going to be aligned anyway.

    Signed-off-by: David Woodhouse

    David Woodhouse
     
  • Observe rb_erase(), when the victim node 'old' has two children so
    neither of the simple cases at the beginning are taken.

    Observe that it effectively does an 'rb_next()' operation to find the
    next (by value) node in the tree. That is; we go to the victim's
    right-hand child and then follow left-hand pointers all the way
    down the tree as far as we can until we find the next node 'node'. We
    end up with 'node' being either the same immediate right-hand child of
    'old', or one of its descendants on the far left-hand side.

    For a start, we _know_ that 'node' has a parent. We can drop that check.

    We also know that if 'node's parent is 'old', then 'node' is the
    right-hand child of its parent. And that if 'node's parent is _not_
    'old', then 'node' is the left-hand child of its parent.

    So instead of checking for 'node->rb_parent == old' in one place and
    also checking 'node's heritage separately when we're trying to change
    its link from its parent, we can shuffle things around a bit and do
    it like this...

    Signed-off-by: David Woodhouse

    David Woodhouse