*/
 #define mas_for_each(__mas, __entry, __max) \
        while (((__entry) = mas_find((__mas), (__max))) != NULL)
+
+#ifdef CONFIG_DEBUG_MAPLE_TREE
+enum mt_dump_format {
+       mt_dump_dec,
+       mt_dump_hex,
+};
+
+extern atomic_t maple_tree_tests_run;
+extern atomic_t maple_tree_tests_passed;
+
+void mt_dump(const struct maple_tree *mt, enum mt_dump_format format);
+void mas_dump(const struct ma_state *mas);
+void mas_wr_dump(const struct ma_wr_state *wr_mas);
+void mt_validate(struct maple_tree *mt);
+void mt_cache_shrink(void);
+#define MT_BUG_ON(__tree, __x) do {                                    \
+       atomic_inc(&maple_tree_tests_run);                              \
+       if (__x) {                                                      \
+               pr_info("BUG at %s:%d (%u)\n",                          \
+               __func__, __LINE__, __x);                               \
+               mt_dump(__tree, mt_dump_hex);                           \
+               pr_info("Pass: %u Run:%u\n",                            \
+                       atomic_read(&maple_tree_tests_passed),          \
+                       atomic_read(&maple_tree_tests_run));            \
+               dump_stack();                                           \
+       } else {                                                        \
+               atomic_inc(&maple_tree_tests_passed);                   \
+       }                                                               \
+} while (0)
+
+#define MAS_BUG_ON(__mas, __x) do {                                    \
+       atomic_inc(&maple_tree_tests_run);                              \
+       if (__x) {                                                      \
+               pr_info("BUG at %s:%d (%u)\n",                          \
+               __func__, __LINE__, __x);                               \
+               mas_dump(__mas);                                        \
+               mt_dump((__mas)->tree, mt_dump_hex);                    \
+               pr_info("Pass: %u Run:%u\n",                            \
+                       atomic_read(&maple_tree_tests_passed),          \
+                       atomic_read(&maple_tree_tests_run));            \
+               dump_stack();                                           \
+       } else {                                                        \
+               atomic_inc(&maple_tree_tests_passed);                   \
+       }                                                               \
+} while (0)
+
+#define MAS_WR_BUG_ON(__wrmas, __x) do {                               \
+       atomic_inc(&maple_tree_tests_run);                              \
+       if (__x) {                                                      \
+               pr_info("BUG at %s:%d (%u)\n",                          \
+               __func__, __LINE__, __x);                               \
+               mas_wr_dump(__wrmas);                                   \
+               mas_dump((__wrmas)->mas);                               \
+               mt_dump((__wrmas)->mas->tree, mt_dump_hex);             \
+               pr_info("Pass: %u Run:%u\n",                            \
+                       atomic_read(&maple_tree_tests_passed),          \
+                       atomic_read(&maple_tree_tests_run));            \
+               dump_stack();                                           \
+       } else {                                                        \
+               atomic_inc(&maple_tree_tests_passed);                   \
+       }                                                               \
+} while (0)
+
+#define MT_WARN_ON(__tree, __x)  ({                                    \
+       int ret = !!(__x);                                              \
+       atomic_inc(&maple_tree_tests_run);                              \
+       if (ret) {                                                      \
+               pr_info("WARN at %s:%d (%u)\n",                         \
+               __func__, __LINE__, __x);                               \
+               mt_dump(__tree, mt_dump_hex);                           \
+               pr_info("Pass: %u Run:%u\n",                            \
+                       atomic_read(&maple_tree_tests_passed),          \
+                       atomic_read(&maple_tree_tests_run));            \
+               dump_stack();                                           \
+       } else {                                                        \
+               atomic_inc(&maple_tree_tests_passed);                   \
+       }                                                               \
+       unlikely(ret);                                                  \
+})
+
+#define MAS_WARN_ON(__mas, __x) ({                                     \
+       int ret = !!(__x);                                              \
+       atomic_inc(&maple_tree_tests_run);                              \
+       if (ret) {                                                      \
+               pr_info("WARN at %s:%d (%u)\n",                         \
+               __func__, __LINE__, __x);                               \
+               mas_dump(__mas);                                        \
+               mt_dump((__mas)->tree, mt_dump_hex);                    \
+               pr_info("Pass: %u Run:%u\n",                            \
+                       atomic_read(&maple_tree_tests_passed),          \
+                       atomic_read(&maple_tree_tests_run));            \
+               dump_stack();                                           \
+       } else {                                                        \
+               atomic_inc(&maple_tree_tests_passed);                   \
+       }                                                               \
+       unlikely(ret);                                                  \
+})
+
+#define MAS_WR_WARN_ON(__wrmas, __x) ({                                        \
+       int ret = !!(__x);                                              \
+       atomic_inc(&maple_tree_tests_run);                              \
+       if (ret) {                                                      \
+               pr_info("WARN at %s:%d (%u)\n",                         \
+               __func__, __LINE__, __x);                               \
+               mas_wr_dump(__wrmas);                                   \
+               mas_dump((__wrmas)->mas);                               \
+               mt_dump((__wrmas)->mas->tree, mt_dump_hex);             \
+               pr_info("Pass: %u Run:%u\n",                            \
+                       atomic_read(&maple_tree_tests_passed),          \
+                       atomic_read(&maple_tree_tests_run));            \
+               dump_stack();                                           \
+       } else {                                                        \
+               atomic_inc(&maple_tree_tests_passed);                   \
+       }                                                               \
+       unlikely(ret);                                                  \
+})
+#else
+#define MT_BUG_ON(__tree, __x)         BUG_ON(__x)
+#define MAS_BUG_ON(__mas, __x)         BUG_ON(__x)
+#define MAS_WR_BUG_ON(__mas, __x)      BUG_ON(__x)
+#define MT_WARN_ON(__tree, __x)                WARN_ON(__x)
+#define MAS_WARN_ON(__mas, __x)                WARN_ON(__x)
+#define MAS_WR_WARN_ON(__mas, __x)     WARN_ON(__x)
+#endif /* CONFIG_DEBUG_MAPLE_TREE */
+
 /**
  * __mas_set_range() - Set up Maple Tree operation state to a sub-range of the
  * current location.
 static inline void __mas_set_range(struct ma_state *mas, unsigned long start,
                unsigned long last)
 {
+       /* Ensure the range starts within the current slot */
+       MAS_WARN_ON(mas, mas_is_active(mas) &&
+                  (mas->index > start || mas->last < start));
        mas->index = start;
        mas->last = last;
 }
 static inline
 void mas_set_range(struct ma_state *mas, unsigned long start, unsigned long last)
 {
-       __mas_set_range(mas, start, last);
        mas->node = MAS_START;
+       __mas_set_range(mas, start, last);
 }
 
 /**
        for (__entry = mt_find(__tree, &(__index), __max); \
                __entry; __entry = mt_find_after(__tree, &(__index), __max))
 
-
-#ifdef CONFIG_DEBUG_MAPLE_TREE
-enum mt_dump_format {
-       mt_dump_dec,
-       mt_dump_hex,
-};
-
-extern atomic_t maple_tree_tests_run;
-extern atomic_t maple_tree_tests_passed;
-
-void mt_dump(const struct maple_tree *mt, enum mt_dump_format format);
-void mas_dump(const struct ma_state *mas);
-void mas_wr_dump(const struct ma_wr_state *wr_mas);
-void mt_validate(struct maple_tree *mt);
-void mt_cache_shrink(void);
-#define MT_BUG_ON(__tree, __x) do {                                    \
-       atomic_inc(&maple_tree_tests_run);                              \
-       if (__x) {                                                      \
-               pr_info("BUG at %s:%d (%u)\n",                          \
-               __func__, __LINE__, __x);                               \
-               mt_dump(__tree, mt_dump_hex);                           \
-               pr_info("Pass: %u Run:%u\n",                            \
-                       atomic_read(&maple_tree_tests_passed),          \
-                       atomic_read(&maple_tree_tests_run));            \
-               dump_stack();                                           \
-       } else {                                                        \
-               atomic_inc(&maple_tree_tests_passed);                   \
-       }                                                               \
-} while (0)
-
-#define MAS_BUG_ON(__mas, __x) do {                                    \
-       atomic_inc(&maple_tree_tests_run);                              \
-       if (__x) {                                                      \
-               pr_info("BUG at %s:%d (%u)\n",                          \
-               __func__, __LINE__, __x);                               \
-               mas_dump(__mas);                                        \
-               mt_dump((__mas)->tree, mt_dump_hex);                    \
-               pr_info("Pass: %u Run:%u\n",                            \
-                       atomic_read(&maple_tree_tests_passed),          \
-                       atomic_read(&maple_tree_tests_run));            \
-               dump_stack();                                           \
-       } else {                                                        \
-               atomic_inc(&maple_tree_tests_passed);                   \
-       }                                                               \
-} while (0)
-
-#define MAS_WR_BUG_ON(__wrmas, __x) do {                               \
-       atomic_inc(&maple_tree_tests_run);                              \
-       if (__x) {                                                      \
-               pr_info("BUG at %s:%d (%u)\n",                          \
-               __func__, __LINE__, __x);                               \
-               mas_wr_dump(__wrmas);                                   \
-               mas_dump((__wrmas)->mas);                               \
-               mt_dump((__wrmas)->mas->tree, mt_dump_hex);             \
-               pr_info("Pass: %u Run:%u\n",                            \
-                       atomic_read(&maple_tree_tests_passed),          \
-                       atomic_read(&maple_tree_tests_run));            \
-               dump_stack();                                           \
-       } else {                                                        \
-               atomic_inc(&maple_tree_tests_passed);                   \
-       }                                                               \
-} while (0)
-
-#define MT_WARN_ON(__tree, __x)  ({                                    \
-       int ret = !!(__x);                                              \
-       atomic_inc(&maple_tree_tests_run);                              \
-       if (ret) {                                                      \
-               pr_info("WARN at %s:%d (%u)\n",                         \
-               __func__, __LINE__, __x);                               \
-               mt_dump(__tree, mt_dump_hex);                           \
-               pr_info("Pass: %u Run:%u\n",                            \
-                       atomic_read(&maple_tree_tests_passed),          \
-                       atomic_read(&maple_tree_tests_run));            \
-               dump_stack();                                           \
-       } else {                                                        \
-               atomic_inc(&maple_tree_tests_passed);                   \
-       }                                                               \
-       unlikely(ret);                                                  \
-})
-
-#define MAS_WARN_ON(__mas, __x) ({                                     \
-       int ret = !!(__x);                                              \
-       atomic_inc(&maple_tree_tests_run);                              \
-       if (ret) {                                                      \
-               pr_info("WARN at %s:%d (%u)\n",                         \
-               __func__, __LINE__, __x);                               \
-               mas_dump(__mas);                                        \
-               mt_dump((__mas)->tree, mt_dump_hex);                    \
-               pr_info("Pass: %u Run:%u\n",                            \
-                       atomic_read(&maple_tree_tests_passed),          \
-                       atomic_read(&maple_tree_tests_run));            \
-               dump_stack();                                           \
-       } else {                                                        \
-               atomic_inc(&maple_tree_tests_passed);                   \
-       }                                                               \
-       unlikely(ret);                                                  \
-})
-
-#define MAS_WR_WARN_ON(__wrmas, __x) ({                                        \
-       int ret = !!(__x);                                              \
-       atomic_inc(&maple_tree_tests_run);                              \
-       if (ret) {                                                      \
-               pr_info("WARN at %s:%d (%u)\n",                         \
-               __func__, __LINE__, __x);                               \
-               mas_wr_dump(__wrmas);                                   \
-               mas_dump((__wrmas)->mas);                               \
-               mt_dump((__wrmas)->mas->tree, mt_dump_hex);             \
-               pr_info("Pass: %u Run:%u\n",                            \
-                       atomic_read(&maple_tree_tests_passed),          \
-                       atomic_read(&maple_tree_tests_run));            \
-               dump_stack();                                           \
-       } else {                                                        \
-               atomic_inc(&maple_tree_tests_passed);                   \
-       }                                                               \
-       unlikely(ret);                                                  \
-})
-#else
-#define MT_BUG_ON(__tree, __x)         BUG_ON(__x)
-#define MAS_BUG_ON(__mas, __x)         BUG_ON(__x)
-#define MAS_WR_BUG_ON(__mas, __x)      BUG_ON(__x)
-#define MT_WARN_ON(__tree, __x)                WARN_ON(__x)
-#define MAS_WARN_ON(__mas, __x)                WARN_ON(__x)
-#define MAS_WR_WARN_ON(__mas, __x)     WARN_ON(__x)
-#endif /* CONFIG_DEBUG_MAPLE_TREE */
-
 #endif /*_LINUX_MAPLE_TREE_H */