{
        struct address_space *mapping = file->f_mapping;
        struct page *hpage;
+       struct page *page;
+       struct page *tmp;
+       struct folio *folio;
        pgoff_t index = 0, end = start + HPAGE_PMD_NR;
        LIST_HEAD(pagelist);
        XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
 
        xas_set(&xas, start);
        for (index = start; index < end; index++) {
-               struct page *page = xas_next(&xas);
-               struct folio *folio;
+               page = xas_next(&xas);
 
                VM_BUG_ON(index != xas.xa_index);
                if (is_shmem) {
                put_page(page);
                goto xa_unlocked;
        }
-       nr = thp_nr_pages(hpage);
 
-       if (is_shmem)
-               __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr);
-       else {
-               __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr);
+       if (!is_shmem) {
                filemap_nr_thps_inc(mapping);
                /*
                 * Paired with smp_mb() in do_dentry_open() to ensure
                smp_mb();
                if (inode_is_open_for_write(mapping->host)) {
                        result = SCAN_FAIL;
-                       __mod_lruvec_page_state(hpage, NR_FILE_THPS, -nr);
                        filemap_nr_thps_dec(mapping);
-                       goto xa_locked;
                }
        }
 
-       if (nr_none) {
-               __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none);
-               /* nr_none is always 0 for non-shmem. */
-               __mod_lruvec_page_state(hpage, NR_SHMEM, nr_none);
-       }
-
-       /* Join all the small entries into a single multi-index entry */
-       xas_set_order(&xas, start, HPAGE_PMD_ORDER);
-       xas_store(&xas, hpage);
        /* Here we can't get an ENOMEM (because entries were
         * previously allocated) But let's check for errors
         * (XArray implementation can be changed in the future)
        try_to_unmap_flush();
 
        if (result == SCAN_SUCCEED) {
-               struct page *page, *tmp;
-               struct folio *folio;
-
                /*
                 * Replacing old pages with new one has succeeded, now we
-                * need to copy the content and free the old pages.
+                * attempt to copy the contents.
                 */
                index = start;
-               list_for_each_entry_safe(page, tmp, &pagelist, lru) {
+               list_for_each_entry(page, &pagelist, lru) {
                        while (index < page->index) {
                                clear_highpage(hpage + (index % HPAGE_PMD_NR));
                                index++;
                        }
-                       copy_highpage(hpage + (page->index % HPAGE_PMD_NR),
-                                     page);
+                       if (copy_mc_highpage(hpage + (page->index % HPAGE_PMD_NR),
+                                            page) > 0) {
+                               result = SCAN_COPY_MC;
+                               break;
+                       }
+                       index++;
+               }
+               while (result == SCAN_SUCCEED && index < end) {
+                       clear_highpage(hpage + (index % HPAGE_PMD_NR));
+                       index++;
+               }
+       }
+
+       nr = thp_nr_pages(hpage);
+       if (result == SCAN_SUCCEED) {
+               /*
+                * Copying old pages to huge one has succeeded, now we
+                * need to free the old pages.
+                */
+               list_for_each_entry_safe(page, tmp, &pagelist, lru) {
                        list_del(&page->lru);
                        page->mapping = NULL;
                        page_ref_unfreeze(page, 1);
                        ClearPageUnevictable(page);
                        unlock_page(page);
                        put_page(page);
-                       index++;
                }
-               while (index < end) {
-                       clear_highpage(hpage + (index % HPAGE_PMD_NR));
-                       index++;
+
+               xas_lock_irq(&xas);
+               if (is_shmem)
+                       __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr);
+               else
+                       __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr);
+
+               if (nr_none) {
+                       __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none);
+                       /* nr_none is always 0 for non-shmem. */
+                       __mod_lruvec_page_state(hpage, NR_SHMEM, nr_none);
                }
+               /* Join all the small entries into a single multi-index entry. */
+               xas_set_order(&xas, start, HPAGE_PMD_ORDER);
+               xas_store(&xas, hpage);
+               xas_unlock_irq(&xas);
 
                folio = page_folio(hpage);
                folio_mark_uptodate(folio);
                unlock_page(hpage);
                hpage = NULL;
        } else {
-               struct page *page;
-
                /* Something went wrong: roll back page cache changes */
                xas_lock_irq(&xas);
                if (nr_none) {
                        xas_lock_irq(&xas);
                }
                VM_BUG_ON(nr_none);
+               /*
+                * Undo the updates of filemap_nr_thps_inc for non-SHMEM
+                * file only. This undo is not needed unless failure is
+                * due to SCAN_COPY_MC.
+                */
+               if (!is_shmem && result == SCAN_COPY_MC) {
+                       filemap_nr_thps_dec(mapping);
+                       /*
+                        * Paired with smp_mb() in do_dentry_open() to
+                        * ensure the update to nr_thps is visible.
+                        */
+                       smp_mb();
+               }
+
                xas_unlock_irq(&xas);
 
                hpage->mapping = NULL;