return err;
 }
 
+static int move_pages_and_store_status(struct mm_struct *mm, int node,
+               struct list_head *pagelist, int __user *status,
+               int start, int i, unsigned long nr_pages)
+{
+       int err;
+
+       err = do_move_pages_to_node(mm, pagelist, node);
+       if (err) {
+               /*
+                * Positive err means the number of failed
+                * pages to migrate.  Since we are going to
+                * abort and return the number of non-migrated
+                * pages, so need to incude the rest of the
+                * nr_pages that have not been attempted as
+                * well.
+                */
+               if (err > 0)
+                       err += nr_pages - i - 1;
+               return err;
+       }
+       return store_status(status, start, node, i - start);
+}
+
 /*
  * Migrate an array of page address onto an array of nodes and fill
  * the corresponding array of status.
                        current_node = node;
                        start = i;
                } else if (node != current_node) {
-                       err = do_move_pages_to_node(mm, &pagelist, current_node);
-                       if (err) {
-                               /*
-                                * Positive err means the number of failed
-                                * pages to migrate.  Since we are going to
-                                * abort and return the number of non-migrated
-                                * pages, so need to incude the rest of the
-                                * nr_pages that have not been attempted as
-                                * well.
-                                */
-                               if (err > 0)
-                                       err += nr_pages - i - 1;
-                               goto out;
-                       }
-                       err = store_status(status, start, current_node, i - start);
+                       err = move_pages_and_store_status(mm, current_node,
+                                       &pagelist, status, start, i, nr_pages);
                        if (err)
                                goto out;
                        start = i;
                if (err)
                        goto out_flush;
 
-               err = do_move_pages_to_node(mm, &pagelist, current_node);
-               if (err) {
-                       if (err > 0)
-                               err += nr_pages - i - 1;
-                       goto out;
-               }
-               err = store_status(status, start, current_node, i - start);
+               err = move_pages_and_store_status(mm, current_node, &pagelist,
+                               status, start, i, nr_pages);
                if (err)
                        goto out;
                current_node = NUMA_NO_NODE;
                return err;
 
        /* Make sure we do not overwrite the existing error */
-       err1 = do_move_pages_to_node(mm, &pagelist, current_node);
-       /*
-        * Don't have to report non-attempted pages here since:
-        *     - If the above loop is done gracefully all pages have been
-        *       attempted.
-        *     - If the above loop is aborted it means a fatal error
-        *       happened, should return ret.
-        */
-       if (!err1)
-               err1 = store_status(status, start, current_node, i - start);
+       err1 = move_pages_and_store_status(mm, current_node, &pagelist,
+                               status, start, i, nr_pages);
        if (err >= 0)
                err = err1;
 out: