Skip to contentSkip to conversation

mm, swap: Fix race between swapoff and some swap operations

PATCH In review Huang, Ying 3 participants 7 messages Dec 18, 2017 – Dec 19, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, swap: Fix race between swapoff and some swap operations

From: Huang Ying <[email protected]>

When the swapin is performed, after getting the swap entry information from the page table, system will swap in the swap entry, without any lock held to prevent the swap device from being swapoff. This may cause the race like below,

CPU 1 CPU 2
----- -----
do_swap_page
swapin_readahead
__read_swap_cache_async
swapoff swapcache_prepare
p->swap_map = NULL __swap_duplicate

					  p->swap_map[?] /* !!! NULL pointer access */

Because swapoff is usually done when system shutdown only, the race may not hit many people in practice. But it is still a race need to be fixed.

To fix the race, get_swap_device() is added to check whether the specified swap entry is valid in its swap device. If so, it will keep the swap entry valid via preventing the swap device from being swapoff, until put_swap_device() is called.

Because swapoff() is very race code path, to make the normal path runs as fast as possible, RCU instead of reference count is used to implement get/put_swap_device(). From get_swap_device() to put_swap_device(), the RCU read lock is held, so synchronize_rcu() in swapoff() will wait until put_swap_device() is called.

In addition to swap_map, cluster_info, etc. data structure in the struct swap_info_struct, the swap cache radix tree will be freed after swapoff, so this patch fixes the race between swap cache looking up and swapoff too.

Cc Hugh Dickins <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Tim Chen <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Mel Gorman <[email protected]>
Cc "Jérôme Glisse" <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc David Rientjes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Jan Kara <[email protected]>
Cc Dave Jiang <[email protected]>
Cc Aaron Lu <[email protected]>

Changelog:

v3:

- Re-implemented with RCU to reduce the overhead of normal paths

v2:

- Re-implemented with SRCU to reduce the overhead of normal paths.

- Avoid to check whether the swap device has been swapoff in get_swap_device(). Because we can check the origin of the swap entry to make sure the swap device hasn't bee swapoff.

---
 include/linux/swap.h |  11 +++++-
 mm/memory.c          |   2 +-
 mm/swap_state.c      |  16 ++++++--
 mm/swapfile.c        | 105 +++++++++++++++++++++++++++++++++++++++------------
 4 files changed, 103 insertions(+), 31 deletions(-)
include/linux/swap.h +9 -2
index 2417d288e016..f7e8f26cf07f 100644
@@ -172,8 +172,9 @@ enum {
 	SWP_PAGE_DISCARD = (1 << 9),	/* freed swap page-cluster discards */
 	SWP_STABLE_WRITES = (1 << 10),	/* no overwrite PG_writeback pages */
 	SWP_SYNCHRONOUS_IO = (1 << 11),	/* synchronous IO is efficient */
+	SWP_VALID	= (1 << 12),	/* swap is valid to be operated on? */
 					/* add others here before... */
-	SWP_SCANNING	= (1 << 12),	/* refcount in scan_swap_map */
+	SWP_SCANNING	= (1 << 13),	/* refcount in scan_swap_map */
 };
 
 #define SWAP_CLUSTER_MAX 32UL
@@ -460,7 +461,7 @@ extern unsigned int count_swap_pages(int, int);
 extern sector_t map_swap_page(struct page *, struct block_device **);
 extern sector_t swapdev_block(int, pgoff_t);
 extern int page_swapcount(struct page *);
-extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
+extern int __swap_count(swp_entry_t entry);
 extern int __swp_swapcount(swp_entry_t entry);
 extern int swp_swapcount(swp_entry_t entry);
 extern struct swap_info_struct *page_swap_info(struct page *);
@@ -470,6 +471,12 @@ extern int try_to_free_swap(struct page *);
 struct backing_dev_info;
 extern int init_swap_address_space(unsigned int type, unsigned long nr_pages);
 extern void exit_swap_address_space(unsigned int type);
+extern struct swap_info_struct *get_swap_device(swp_entry_t entry);
+
+static inline void put_swap_device(struct swap_info_struct *si)
+{
+	rcu_read_unlock();
+}
 
 #else /* CONFIG_SWAP */
 
mm/memory.c +1 -1
index 1a969992f76b..77a7d6191218 100644
@@ -2909,7 +2909,7 @@ int do_swap_page(struct vm_fault *vmf)
 		struct swap_info_struct *si = swp_swap_info(entry);
 
 		if (si->flags & SWP_SYNCHRONOUS_IO &&
-				__swap_count(si, entry) == 1) {
+		    __swap_count(entry) == 1) {
 			/* skip swapcache */
 			page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
 							vmf->address);
mm/swap_state.c +13 -3
index 0b8ae361981f..8dde719e973c 100644
@@ -337,8 +337,13 @@ struct page *lookup_swap_cache(swp_entry_t entry, struct vm_area_struct *vma,
 			       unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si;
 
+	si = get_swap_device(entry);
+	if (!si)
+		return NULL;
 	page = find_get_page(swap_address_space(entry), swp_offset(entry));
+	put_swap_device(si);
 
 	INC_CACHE_INFO(find_total);
 	if (page) {
@@ -376,8 +381,8 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr,
 			bool *new_page_allocated)
 {
-	struct page *found_page, *new_page = NULL;
-	struct address_space *swapper_space = swap_address_space(entry);
+	struct page *found_page = NULL, *new_page = NULL;
+	struct swap_info_struct *si;
 	int err;
 	*new_page_allocated = false;
 
@@ -387,7 +392,12 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
 		 * called after lookup_swap_cache() failed, re-calling
 		 * that would confuse statistics.
 		 */
-		found_page = find_get_page(swapper_space, swp_offset(entry));
+		si = get_swap_device(entry);
+		if (!si)
+			break;
+		found_page = find_get_page(swap_address_space(entry),
+					   swp_offset(entry));
+		put_swap_device(si);
 		if (found_page)
 			break;
 
mm/swapfile.c +80 -25

192 lines not on this page · raw message

Signature · 1 line
2.15.0
#1 lore raw
Quoted 59 lines · Ying

From: Huang Ying <[email protected]>

When the swapin is performed, after getting the swap entry information from the page table, system will swap in the swap entry, without any lock held to prevent the swap device from being swapoff. This may cause the race like below,

CPU 1 CPU 2
----- -----
do_swap_page
swapin_readahead
__read_swap_cache_async
swapoff swapcache_prepare
p->swap_map = NULL __swap_duplicate

					  p->swap_map[?] /* !!! NULL pointer access */

Because swapoff is usually done when system shutdown only, the race may not hit many people in practice. But it is still a race need to be fixed.

To fix the race, get_swap_device() is added to check whether the specified swap entry is valid in its swap device. If so, it will keep the swap entry valid via preventing the swap device from being swapoff, until put_swap_device() is called.

Because swapoff() is very race code path, to make the normal path runs as fast as possible, RCU instead of reference count is used to implement get/put_swap_device(). From get_swap_device() to put_swap_device(), the RCU read lock is held, so synchronize_rcu() in swapoff() will wait until put_swap_device() is called.

In addition to swap_map, cluster_info, etc. data structure in the struct swap_info_struct, the swap cache radix tree will be freed after swapoff, so this patch fixes the race between swap cache looking up and swapoff too.

Cc Hugh Dickins <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Tim Chen <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Mel Gorman <[email protected]>
Cc "JA(C)rA'me Glisse" <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc David Rientjes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Jan Kara <[email protected]>
Cc Dave Jiang <[email protected]>
Cc Aaron Lu <[email protected]>

Changelog:

v3:

- Re-implemented with RCU to reduce the overhead of normal paths

Quoted 7 lines

v2:

- Re-implemented with SRCU to reduce the overhead of normal paths.

- Avoid to check whether the swap device has been swapoff in get_swap_device(). Because we can check the origin of the swap entry to make sure the swap device hasn't bee swapoff.

A version implemented via stop_machine() could be gotten via a small patch as below. If you still prefer stop_machine(), I can resend a version implemented with stop_machine().

And, it appears that if we replace smp_wmb() in _enable_swap_info() with stop_machine() in some way, we can avoid smp_rmb() in get_swap_device(). This can reduce overhead in normal path further. Can we get same effect with RCU? For example, use synchronize_rcu() instead of stop_machine()?

Hi, Paul, can you help me on this?

Best Regards,
Huang, Ying

----------------8<------------------------------

---
 include/linux/swap.h |  2 +-
 mm/swapfile.c        | 12 +++++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)
include/linux/swap.h +1 -1
index f7e8f26cf07f..1027169d5a04 100644
@@ -475,7 +475,7 @@ extern struct swap_info_struct *get_swap_device(swp_entry_t entry);
 
 static inline void put_swap_device(struct swap_info_struct *si)
 {
-	rcu_read_unlock();
+	preempt_enable();
 }
 
 #else /* CONFIG_SWAP */
mm/swapfile.c +9 -3
index ca7b4c5ebe34..feb13ce01045 100644
@@ -38,6 +38,7 @@
 #include <linux/export.h>
 #include <linux/swap_slots.h>
 #include <linux/sort.h>
+#include <linux/stop_machine.h>
 
 #include <asm/pgtable.h>
 #include <asm/tlbflush.h>
@@ -1125,7 +1126,7 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
 		goto bad_nofile;
 	si = swap_info[type];
 
-	rcu_read_lock();
+	preempt_disable();
 	if (!(si->flags & SWP_VALID))
 		goto unlock_out;
 	/*
@@ -1143,7 +1144,7 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
 out:
 	return NULL;
 unlock_out:
-	rcu_read_unlock();
+	preempt_enable();
 	return NULL;
 }
 
@@ -2581,6 +2582,11 @@ bool has_usable_swap(void)
 	return ret;
 }
 
+static int swapoff_stop(void *arg)
+{
+	return 0;
+}
+
 SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 {
 	struct swap_info_struct *p = NULL;
@@ -2677,7 +2683,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 	 * wait for swap operations protected by get/put_swap_device()
 	 * to complete
 	 */
-	synchronize_rcu();
+	stop_machine(swapoff_stop, NULL, cpu_online_mask);
 
 	flush_work(&p->discard_work);
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 5 lines · Ying

A version implemented via stop_machine() could be gotten via a small patch as below. If you still prefer stop_machine(), I can resend a version implemented with stop_machine().

For the stop_machine() version, would it work to just put preempt_disable/enable at the start and end of lock_cluster() rather than introducing get/put_swap_device? Something like that might be simpler and would also disable preemption for less duration.

Thanks,
Junaid

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
In reply to Huang, Ying
Quoted 73 lines · Ying
Quoted 59 lines · Ying

From: Huang Ying <[email protected]>

When the swapin is performed, after getting the swap entry information from the page table, system will swap in the swap entry, without any lock held to prevent the swap device from being swapoff. This may cause the race like below,

CPU 1 CPU 2
----- -----
do_swap_page
swapin_readahead
__read_swap_cache_async
swapoff swapcache_prepare
p->swap_map = NULL __swap_duplicate

					  p->swap_map[?] /* !!! NULL pointer access */

Because swapoff is usually done when system shutdown only, the race may not hit many people in practice. But it is still a race need to be fixed.

To fix the race, get_swap_device() is added to check whether the specified swap entry is valid in its swap device. If so, it will keep the swap entry valid via preventing the swap device from being swapoff, until put_swap_device() is called.

Because swapoff() is very race code path, to make the normal path runs as fast as possible, RCU instead of reference count is used to implement get/put_swap_device(). From get_swap_device() to put_swap_device(), the RCU read lock is held, so synchronize_rcu() in swapoff() will wait until put_swap_device() is called.

In addition to swap_map, cluster_info, etc. data structure in the struct swap_info_struct, the swap cache radix tree will be freed after swapoff, so this patch fixes the race between swap cache looking up and swapoff too.

Cc Hugh Dickins <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Tim Chen <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Mel Gorman <[email protected]>
Cc "Jerome Glisse" <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc David Rientjes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Jan Kara <[email protected]>
Cc Dave Jiang <[email protected]>
Cc Aaron Lu <[email protected]>

Changelog:

v3:

- Re-implemented with RCU to reduce the overhead of normal paths

Quoted 7 lines

v2:

- Re-implemented with SRCU to reduce the overhead of normal paths.

- Avoid to check whether the swap device has been swapoff in get_swap_device(). Because we can check the origin of the swap entry to make sure the swap device hasn't bee swapoff.

A version implemented via stop_machine() could be gotten via a small patch as below. If you still prefer stop_machine(), I can resend a version implemented with stop_machine().

Quoted 6 lines

And, it appears that if we replace smp_wmb() in _enable_swap_info() with stop_machine() in some way, we can avoid smp_rmb() in get_swap_device(). This can reduce overhead in normal path further. Can we get same effect with RCU? For example, use synchronize_rcu() instead of stop_machine()?

Hi, Paul, can you help me on this?

If the key loads before and after the smp_rmb() are within the same RCU read-side critical section, -and- if one of the critical writes is before the synchronize_rcu() and the other critical write is after the synchronize_rcu(), then you normally don't need the smp_rmb().

Otherwise, you likely do still need the smp_rmb().

Thanx, Paul

Quoted 66 lines

Best Regards,
Huang, Ying

----------------8<------------------------------

---
 include/linux/swap.h |  2 +-
 mm/swapfile.c        | 12 +++++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)
include/linux/swap.h +1 -1
index f7e8f26cf07f..1027169d5a04 100644
@@ -475,7 +475,7 @@ extern struct swap_info_struct *get_swap_device(swp_entry_t entry);
 
 static inline void put_swap_device(struct swap_info_struct *si)
 {
-	rcu_read_unlock();
+	preempt_enable();
 }
 
 #else /* CONFIG_SWAP */
mm/swapfile.c +8 -2
index ca7b4c5ebe34..feb13ce01045 100644
@@ -38,6 +38,7 @@
 #include <linux/export.h>
 #include <linux/swap_slots.h>
 #include <linux/sort.h>
+#include <linux/stop_machine.h>
 
 #include <asm/pgtable.h>
 #include <asm/tlbflush.h>
@@ -1125,7 +1126,7 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
 		goto bad_nofile;
 	si = swap_info[type];
 
-	rcu_read_lock();
+	preempt_disable();
 	if (!(si->flags & SWP_VALID))
 		goto unlock_out;
 	/*
@@ -1143,7 +1144,7 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
 out:
 	return NULL;
 unlock_out:
-	rcu_read_unlock();
+	preempt_enable();
 	return NULL;
 }
 
@@ -2581,6 +2582,11 @@ bool has_usable_swap(void)
 	return ret;
 }
 
+static int swapoff_stop(void *arg)
+{
+	return 0;
+}
+
 SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 {
 	struct swap_info_struct *p = NULL;

mm/swapfile.c SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) · line 2689

Quoted 8 lines
mm/swapfile.c +1 -1
@@ -2677,7 +2683,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 	 * wait for swap operations protected by get/put_swap_device()
 	 * to complete
 	 */
-	synchronize_rcu();
+	stop_machine(swapoff_stop, NULL, cpu_online_mask);
 
 	flush_work(&p->discard_work);
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 7 lines · Paul E. McKenney
Quoted 6 lines · Ying

"Huang, Ying" <[email protected]> writes:
And, it appears that if we replace smp_wmb() in _enable_swap_info() with stop_machine() in some way, we can avoid smp_rmb() in get_swap_device(). This can reduce overhead in normal path further. Can we get same effect with RCU? For example, use synchronize_rcu() instead of stop_machine()?

Quoted 8 lines
Quoted 1 line

Hi, Paul, can you help me on this?

If the key loads before and after the smp_rmb() are within the same RCU read-side critical section, -and- if one of the critical writes is before the synchronize_rcu() and the other critical write is after the synchronize_rcu(), then you normally don't need the smp_rmb().

Otherwise, you likely do still need the smp_rmb().

My question may be too general, let make it more specific. For the following program,

"
int a;
int b;

void intialize(void)

{
        a = 1;
        synchronize_rcu();
        b = 2;
}

void test(void)

{
        int c;

        rcu_read_lock();
        c = b;
        /* ignored smp_rmb() */
        if (c)
                pr_info("a=%d\n", a);
        rcu_read_unlock();
}

"

Is it possible for it to show

"
a=0
"

in kernel log?

If it couldn't, this could be a useful usage model of RCU to accelerate hot path.

Best Regards,
Huang, Ying

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
Quoted 51 lines · Ying
Quoted 7 lines · Paul E. McKenney
Quoted 6 lines · Ying

"Huang, Ying" <[email protected]> writes:
And, it appears that if we replace smp_wmb() in _enable_swap_info() with stop_machine() in some way, we can avoid smp_rmb() in get_swap_device(). This can reduce overhead in normal path further. Can we get same effect with RCU? For example, use synchronize_rcu() instead of stop_machine()?

Quoted 8 lines
Quoted 1 line

Hi, Paul, can you help me on this?

If the key loads before and after the smp_rmb() are within the same RCU read-side critical section, -and- if one of the critical writes is before the synchronize_rcu() and the other critical write is after the synchronize_rcu(), then you normally don't need the smp_rmb().

Otherwise, you likely do still need the smp_rmb().

My question may be too general, let make it more specific. For the following program,

"
int a;
int b;

void intialize(void)

{
        a = 1;
        synchronize_rcu();
        b = 2;
}

void test(void)

{
        int c;

        rcu_read_lock();
        c = b;
        /* ignored smp_rmb() */
        if (c)
                pr_info("a=%d\n", a);
        rcu_read_unlock();
}

"

Is it possible for it to show

"
a=0
"

Quoted 5 lines

in kernel log?

If it couldn't, this could be a useful usage model of RCU to accelerate hot path.

This is not possible, and it can be verified using the Linux kernel memory model. An introduction to an older version of this model may be found here (including an introduction to litmus tests and their output):

	https://lwn.net/Articles/718628/
	https://lwn.net/Articles/720550/

The litmus test and its output are shown below.

The reason it is not possible is that the entirety of test()'s RCU read-side critical section must do one of two things:

1. Come before the return from initialize()'s synchronize_rcu().
2. Come after the call to initialize()'s synchronize_rcu().

Suppose test()'s load from "b" sees initialize()'s assignment. Then some part of test()'s RCU read-side critical section came after initialize()'s call to synchronize_rcu(), which means that the entirety of test()'s RCU read-side critical section must come after initialize()'s call to synchronize_rcu(). Therefore, whenever "c" is non-zero, the pr_info() must see "a" non-zero.

Thanx, Paul

------------------------------------------------------------------------

C MP-o-sync-o+rl-o-ctl-o-rul

{}

P0(int *a, int *b)

{
	WRITE_ONCE(*a, 1);
	synchronize_rcu();
	WRITE_ONCE(*b, 2);
}

P1(int *a, int *b)

{
	int r0;
	int r1;

	rcu_read_lock();
	r0 = READ_ONCE(*b);
	if (r0)
		r1 = READ_ONCE(*a);
	rcu_read_unlock();
}

exists (1:r0=1 /\ 1:r1=0)

------------------------------------------------------------------------

States 2
1:r0=0; 1:r1=0;
1:r0=2; 1:r1=1;
No
Witnesses
Positive: 0 Negative: 2
Condition exists (1:r0=1 /\ 1:r1=0)
Observation MP-o-sync-o+rl-o-ctl-o-rul Never 0 2
Time MP-o-sync-o+rl-o-ctl-o-rul 0.01
Hash=b20eca2da50fa84b15e489502420ff56

------------------------------------------------------------------------

The "Never 0 2" means that the condition cannot happen.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw
Quoted 123 lines · Paul E. McKenney
Quoted 51 lines · Ying
Quoted 7 lines · Paul E. McKenney
Quoted 6 lines · Ying

"Huang, Ying" <[email protected]> writes:
And, it appears that if we replace smp_wmb() in _enable_swap_info() with stop_machine() in some way, we can avoid smp_rmb() in get_swap_device(). This can reduce overhead in normal path further. Can we get same effect with RCU? For example, use synchronize_rcu() instead of stop_machine()?

Quoted 8 lines
Quoted 1 line

Hi, Paul, can you help me on this?

If the key loads before and after the smp_rmb() are within the same RCU read-side critical section, -and- if one of the critical writes is before the synchronize_rcu() and the other critical write is after the synchronize_rcu(), then you normally don't need the smp_rmb().

Otherwise, you likely do still need the smp_rmb().

My question may be too general, let make it more specific. For the following program,

"
int a;
int b;

void intialize(void)

{
        a = 1;
        synchronize_rcu();
        b = 2;
}

void test(void)

{
        int c;

        rcu_read_lock();
        c = b;
        /* ignored smp_rmb() */
        if (c)
                pr_info("a=%d\n", a);
        rcu_read_unlock();
}

"

Is it possible for it to show

"
a=0
"

Quoted 5 lines

in kernel log?

If it couldn't, this could be a useful usage model of RCU to accelerate hot path.

This is not possible, and it can be verified using the Linux kernel memory model. An introduction to an older version of this model may be found here (including an introduction to litmus tests and their output):

	https://lwn.net/Articles/718628/
	https://lwn.net/Articles/720550/

The litmus test and its output are shown below.

The reason it is not possible is that the entirety of test()'s RCU read-side critical section must do one of two things:

1. Come before the return from initialize()'s synchronize_rcu().
2. Come after the call to initialize()'s synchronize_rcu().

Suppose test()'s load from "b" sees initialize()'s assignment. Then some part of test()'s RCU read-side critical section came after initialize()'s call to synchronize_rcu(), which means that the entirety of test()'s RCU read-side critical section must come after initialize()'s call to synchronize_rcu(). Therefore, whenever "c" is non-zero, the pr_info() must see "a" non-zero.

Thanx, Paul

------------------------------------------------------------------------

C MP-o-sync-o+rl-o-ctl-o-rul

{}

P0(int *a, int *b)

{
	WRITE_ONCE(*a, 1);
	synchronize_rcu();
	WRITE_ONCE(*b, 2);
}

P1(int *a, int *b)

{
	int r0;
	int r1;

	rcu_read_lock();
	r0 = READ_ONCE(*b);
	if (r0)
		r1 = READ_ONCE(*a);
	rcu_read_unlock();
}

exists (1:r0=1 /\ 1:r1=0)

------------------------------------------------------------------------

States 2
1:r0=0; 1:r1=0;
1:r0=2; 1:r1=1;
No
Witnesses
Positive: 0 Negative: 2
Condition exists (1:r0=1 /\ 1:r1=0)
Observation MP-o-sync-o+rl-o-ctl-o-rul Never 0 2
Time MP-o-sync-o+rl-o-ctl-o-rul 0.01
Hash=b20eca2da50fa84b15e489502420ff56

Quoted 3 lines

------------------------------------------------------------------------

The "Never 0 2" means that the condition cannot happen.

Thanks a lot for your detailed explanation! That helps me much!

Best Regards,
Huang, Ying

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>