Peng Zhang c45ea315a6 maple_tree: fix a potential concurrency bug in RCU mode
There is a concurrency bug that may cause the wrong value to be loaded
when a CPU is modifying the maple tree.

CPU1:
mtree_insert_range()
  mas_insert()
    mas_store_root()
      ...
      mas_root_expand()
        ...
        rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
        ma_set_meta(node, maple_leaf_64, 0, slot);    <---IP

CPU2:
mtree_load()
  mtree_lookup_walk()
    ma_data_end();

When CPU1 is about to execute the instruction pointed to by IP, the
ma_data_end() executed by CPU2 may return the wrong end position, which
will cause the value loaded by mtree_load() to be wrong.

An example of triggering the bug:

Add mdelay(100) between rcu_assign_pointer() and ma_set_meta() in
mas_root_expand().

static DEFINE_MTREE(tree);
int work(void *p) {
	unsigned long val;
	for (int i = 0 ; i< 30; ++i) {
		val = (unsigned long)mtree_load(&tree, 8);
		mdelay(5);
		pr_info("%lu",val);
	}
	return 0;
}

mt_init_flags(&tree, MT_FLAGS_USE_RCU);
mtree_insert(&tree, 0, (void*)12345, GFP_KERNEL);
run_thread(work)
mtree_insert(&tree, 1, (void*)56789, GFP_KERNEL);

In RCU mode, mtree_load() should always return the value before or after
the data structure is modified, and in this example mtree_load(&tree, 8)
may return 56789 which is not expected, it should always return NULL.  Fix
it by put ma_set_meta() before rcu_assign_pointer().

Link: https://lkml.kernel.org/r/20230314124203.91572-4-zhangpeng.00@bytedance.com
Fixes: 54a611b60590 ("Maple Tree: add new data structure")
Signed-off-by: Peng Zhang <zhangpeng.00@bytedance.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-04-05 18:06:25 -07:00
..
2022-05-09 17:20:37 -07:00
2021-07-08 11:48:20 -07:00
2022-12-13 10:43:59 -08:00
2023-03-06 15:54:54 -08:00
2021-05-06 19:24:12 -07:00
2022-09-26 12:19:12 -07:00
2018-08-16 12:14:42 -07:00
2021-01-21 14:06:00 -07:00
2022-03-07 12:48:35 -07:00
2021-08-19 09:02:55 +09:00
2022-12-12 17:28:58 -08:00
2022-09-01 18:04:43 +02:00
2023-02-02 22:50:01 -08:00
2023-02-02 22:50:01 -08:00
2023-02-02 22:50:01 -08:00
2022-04-22 21:30:57 +02:00
2023-02-15 15:44:43 +01:00
2021-01-03 20:05:18 -05:00
2023-03-19 10:02:04 -07:00
2022-03-07 12:48:35 -07:00
2022-04-29 14:38:01 -07:00
2022-07-10 13:55:49 -07:00
2022-10-03 14:03:21 -07:00
2021-08-19 09:02:55 +09:00
2022-12-14 09:15:43 -08:00
2021-07-08 11:48:20 -07:00
2021-07-08 11:48:20 -07:00
2018-10-16 13:45:44 +02:00
2022-10-03 17:34:32 -07:00
2021-07-08 11:48:20 -07:00
2021-07-08 11:48:20 -07:00
2022-06-03 10:34:34 -07:00
2023-01-27 11:42:57 -08:00
2021-06-18 11:43:09 +02:00
2021-07-08 11:48:20 -07:00
2022-11-11 18:18:05 -08:00
2022-01-20 08:52:54 +02:00
2018-10-15 16:31:29 -04:00
2023-02-02 22:33:13 -08:00
2022-12-13 15:47:48 -08:00
2023-02-21 11:07:23 -08:00