{
        if (xas_error(xas))
                curr = xas->xa_node;
-       return xa_zero_to_null(curr);
+       return curr;
 }
 
 /**
 void *__xa_erase(struct xarray *xa, unsigned long index)
 {
        XA_STATE(xas, xa, index);
-       return xas_result(&xas, xas_store(&xas, NULL));
+       return xas_result(&xas, xa_zero_to_null(xas_store(&xas, NULL)));
 }
 EXPORT_SYMBOL(__xa_erase);
 
                        xas_clear_mark(&xas, XA_FREE_MARK);
        } while (__xas_nomem(&xas, gfp));
 
-       return xas_result(&xas, curr);
+       return xas_result(&xas, xa_zero_to_null(curr));
 }
 EXPORT_SYMBOL(__xa_store);
 
 }
 EXPORT_SYMBOL(xa_store);
 
+static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index,
+                       void *old, void *entry, gfp_t gfp);
+
 /**
  * __xa_cmpxchg() - Store this entry in the XArray.
  * @xa: XArray.
  */
 void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
                        void *old, void *entry, gfp_t gfp)
+{
+       return xa_zero_to_null(__xa_cmpxchg_raw(xa, index, old, entry, gfp));
+}
+EXPORT_SYMBOL(__xa_cmpxchg);
+
+static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index,
+                       void *old, void *entry, gfp_t gfp)
 {
        XA_STATE(xas, xa, index);
        void *curr;
 
        return xas_result(&xas, curr);
 }
-EXPORT_SYMBOL(__xa_cmpxchg);
 
 /**
  * __xa_insert() - Store this entry in the XArray if no entry is present.
  */
 int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
 {
-       XA_STATE(xas, xa, index);
        void *curr;
+       int errno;
 
-       if (WARN_ON_ONCE(xa_is_advanced(entry)))
-               return -EINVAL;
        if (!entry)
                entry = XA_ZERO_ENTRY;
-
-       do {
-               curr = xas_load(&xas);
-               if (!curr) {
-                       xas_store(&xas, entry);
-                       if (xa_track_free(xa))
-                               xas_clear_mark(&xas, XA_FREE_MARK);
-               } else {
-                       xas_set_err(&xas, -EBUSY);
-               }
-       } while (__xas_nomem(&xas, gfp));
-
-       return xas_error(&xas);
+       curr = __xa_cmpxchg_raw(xa, index, NULL, entry, gfp);
+       errno = xa_err(curr);
+       if (errno)
+               return errno;
+       return (curr != NULL) ? -EBUSY : 0;
 }
 EXPORT_SYMBOL(__xa_insert);