EM( SCAN_ALLOC_HUGE_PAGE_FAIL,  "alloc_huge_page_failed")       \
        EM( SCAN_CGROUP_CHARGE_FAIL,    "ccgroup_charge_failed")        \
        EM( SCAN_TRUNCATED,             "truncated")                    \
-       EMe(SCAN_PAGE_HAS_PRIVATE,      "page_has_private")             \
+       EM( SCAN_PAGE_HAS_PRIVATE,      "page_has_private")             \
+       EMe(SCAN_STORE_FAILED,          "store_failed")
 
 #undef EM
 #undef EMe
 
        SCAN_CGROUP_CHARGE_FAIL,
        SCAN_TRUNCATED,
        SCAN_PAGE_HAS_PRIVATE,
+       SCAN_STORE_FAILED,
 };
 
 #define CREATE_TRACE_POINTS
                                        goto xa_locked;
                                }
                                xas_store(&xas, hpage);
+                               if (xas_error(&xas)) {
+                                       /* revert shmem_charge performed
+                                        * in the previous condition
+                                        */
+                                       mapping->nrpages--;
+                                       shmem_uncharge(mapping->host, 1);
+                                       result = SCAN_STORE_FAILED;
+                                       goto xa_locked;
+                               }
                                nr_none++;
                                continue;
                        }
 
                /* Finally, replace with the new page. */
                xas_store(&xas, hpage);
+               /* We can't get an ENOMEM here (because the allocation happened before)
+                * but let's check for errors (XArray implementation can be
+                * changed in the future)
+                */
+               WARN_ON_ONCE(xas_error(&xas));
                continue;
 out_unlock:
                unlock_page(page);
        /* 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)
+        */
+       WARN_ON_ONCE(xas_error(&xas));
 xa_locked:
        xas_unlock_irq(&xas);
 xa_unlocked: