]> git.openfabrics.org - ~shefty/libibverbs.git/commitdiff
Fix incorrect splits/merges in the memory tree when madvise() fails.
authorAlex Vainman <alexonlists@gmail.com>
Mon, 1 Feb 2010 05:57:45 +0000 (07:57 +0200)
committerRoland Dreier <rolandd@cisco.com>
Fri, 19 Mar 2010 18:04:43 +0000 (11:04 -0700)
ibv_madvise_range() first manages (splits or merges) memory ranges in
the tree and only then calls madvise().  If madvise() fails, the
tree's memory range may contain incorrectly split or merged ranges.
The patch undoes the split and merge operations performed on the node
which caused the madvise() failure as well as on that node's
neighbors.

Signed-off-by: Alex Vainman <alexv@voltaire.com>
Signed-off-by: Roland Dreier <rolandd@cisco.com>
src/memory.c

index 6a3305f3fd64c172e332a8807c53e2c3d531d337..03f49c89b0fa24a1e4f1e243529dd15fd936ce8e 100644 (file)
@@ -489,6 +489,39 @@ static struct ibv_mem_node *get_start_node(uintptr_t start, uintptr_t end,
        return node;
 }
 
+/*
+ * This function is called if madvise() fails to undo merging/splitting
+ * operations performed on the node.
+ */
+static struct ibv_mem_node *undo_node(struct ibv_mem_node *node,
+                                     uintptr_t start, int inc)
+{
+       struct ibv_mem_node *tmp = NULL;
+
+       /*
+        * This condition can be true only if we merged this
+        * node with the previous one, so we need to split them.
+       */
+       if (start > node->start) {
+               tmp = split_range(node, start);
+               if (tmp) {
+                       node->refcnt += inc;
+                       node = tmp;
+               } else
+                       return NULL;
+       }
+
+       tmp  =  __mm_prev(node);
+       if (tmp && tmp->refcnt == node->refcnt)
+               node = merge_ranges(node, tmp);
+
+       tmp  =  __mm_next(node);
+       if (tmp && tmp->refcnt == node->refcnt)
+               node = merge_ranges(tmp, node);
+
+       return node;
+}
+
 static int ibv_madvise_range(void *base, size_t size, int advice)
 {
        uintptr_t start, end;
@@ -541,8 +574,10 @@ static int ibv_madvise_range(void *base, size_t size, int advice)
                                ret = madvise((void *) node->start,
                                              node->end - node->start + 1,
                                              advice);
-                       if (ret)
+                       if (ret) {
+                               node = undo_node(node, start, inc);
                                goto out;
+                       }
                }
 
                node->refcnt += inc;