Skip to contentSkip to conversation

mm: thp: grab the lock before manipulation defer list

RFC PATCH Superseded Wei Yang 4 participants 20 messages Jan 3, 2020 – Jan 9, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

RFC PATCH 1/1 mm: thp: grab the lock before manipulation defer list

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Fixes 87eaceb3faa5 ("mm: thp: make deferred split shrinker memcg aware")

---
I notice the difference during code reading and just confused about the difference. No specific test is done since limited knowledge about cgroup.

Maybe I miss something important?

---
 mm/memcontrol.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/memcontrol.c +4 -4
index bc01423277c5..62b7ec34ef1a 100644
@@ -5368,12 +5368,12 @@ static int mem_cgroup_move_account(struct page *page,
 	}
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock(&from->deferred_split_queue.split_queue_lock);
 	if (compound && !list_empty(page_deferred_list(page))) {
-		spin_lock(&from->deferred_split_queue.split_queue_lock);
 		list_del_init(page_deferred_list(page));
 		from->deferred_split_queue.split_queue_len--;
-		spin_unlock(&from->deferred_split_queue.split_queue_lock);
 	}
+	spin_unlock(&from->deferred_split_queue.split_queue_lock);
 #endif
 	/*
 	 * It is safe to change page->mem_cgroup here because the page
@@ -5385,13 +5385,13 @@ static int mem_cgroup_move_account(struct page *page,
 	page->mem_cgroup = to;
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock(&to->deferred_split_queue.split_queue_lock);
 	if (compound && list_empty(page_deferred_list(page))) {
-		spin_lock(&to->deferred_split_queue.split_queue_lock);
 		list_add_tail(page_deferred_list(page),
 			      &to->deferred_split_queue.split_queue);
 		to->deferred_split_queue.split_queue_len++;
-		spin_unlock(&to->deferred_split_queue.split_queue_lock);
 	}
+	spin_unlock(&to->deferred_split_queue.split_queue_lock);
 #endif
 
 	spin_unlock_irqrestore(&from->move_lock, flags);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 5 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Fixes 87eaceb3faa5 ("mm: thp: make deferred split shrinker memcg aware")
Quoted 7 lines

---
I notice the difference during code reading and just confused about the difference. No specific test is done since limited knowledge about cgroup.

Maybe I miss something important?

The check for !list_empty(page_deferred_list(page)) must certainly be serialized with doing list_del_init(page_deferred_list(page)).

Quoted 44 lines
---
 mm/memcontrol.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/memcontrol.c +4 -4
index bc01423277c5..62b7ec34ef1a 100644
@@ -5368,12 +5368,12 @@ static int mem_cgroup_move_account(struct page *page,
 	}
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock(&from->deferred_split_queue.split_queue_lock);
 	if (compound && !list_empty(page_deferred_list(page))) {
-		spin_lock(&from->deferred_split_queue.split_queue_lock);
 		list_del_init(page_deferred_list(page));
 		from->deferred_split_queue.split_queue_len--;
-		spin_unlock(&from->deferred_split_queue.split_queue_lock);
 	}
+	spin_unlock(&from->deferred_split_queue.split_queue_lock);
 #endif
 	/*
 	 * It is safe to change page->mem_cgroup here because the page
@@ -5385,13 +5385,13 @@ static int mem_cgroup_move_account(struct page *page,
 	page->mem_cgroup = to;
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock(&to->deferred_split_queue.split_queue_lock);
 	if (compound && list_empty(page_deferred_list(page))) {
-		spin_lock(&to->deferred_split_queue.split_queue_lock);
 		list_add_tail(page_deferred_list(page),
 			      &to->deferred_split_queue.split_queue);
 		to->deferred_split_queue.split_queue_len++;
-		spin_unlock(&to->deferred_split_queue.split_queue_lock);
 	}
+	spin_unlock(&to->deferred_split_queue.split_queue_lock);
 #endif
 
 	spin_unlock_irqrestore(&from->move_lock, flags);
Signature · 1 line
2.17.1
author
#2 lore raw
Quoted 9 lines · David Rientjes
Quoted 7 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Fixes 87eaceb3faa5 ("mm: thp: make deferred split shrinker memcg aware")
Quoted 9 lines
Quoted 5 lines

---
I notice the difference during code reading and just confused about the difference. No specific test is done since limited knowledge about cgroup.

Maybe I miss something important?

The check for !list_empty(page_deferred_list(page)) must certainly be serialized with doing list_del_init(page_deferred_list(page)).

Hi David

Would you mind giving more information? You mean list_empty and list_del_init is atomic?

Signature · 2 lines
Wei Yang
Help you, Help me
#3 lore raw
Quoted 16 lines · Wei Yang
Quoted 15 lines · David Rientjes
Quoted 12 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Fixes 87eaceb3faa5 ("mm: thp: make deferred split shrinker memcg aware")

---
I notice the difference during code reading and just confused about the difference. No specific test is done since limited knowledge about cgroup.

Maybe I miss something important?

Quoted 9 lines
Quoted 3 lines

The check for !list_empty(page_deferred_list(page)) must certainly be serialized with doing list_del_init(page_deferred_list(page)).

Hi David

Would you mind giving more information? You mean list_empty and list_del_init is atomic?

I mean your patch is obviously correct :) It should likely also have a [email protected] # 5.4+

author
#4 lore raw
Quoted 22 lines · David Rientjes
Quoted 20 lines · Wei Yang
Quoted 18 lines · David Rientjes
Quoted 5 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Fixes 87eaceb3faa5 ("mm: thp: make deferred split shrinker memcg aware")
Quoted 7 lines

---
I notice the difference during code reading and just confused about the difference. No specific test is done since limited knowledge about cgroup.

Maybe I miss something important?

The check for !list_empty(page_deferred_list(page)) must certainly be serialized with doing list_del_init(page_deferred_list(page)).

Quoted 8 lines
Quoted 5 lines

Hi David

Would you mind giving more information? You mean list_empty and list_del_init is atomic?

I mean your patch is obviously correct :) It should likely also have a [email protected] # 5.4+

Ah, my poor English ;-)

Quoted 2 lines
Signature · 2 lines
Wei Yang
Help you, Help me
maintainer
#5 lore raw
In reply to Wei Yang
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Quoted 50 lines
Fixes 87eaceb3faa5 ("mm: thp: make deferred split shrinker memcg aware")

---
I notice the difference during code reading and just confused about the difference. No specific test is done since limited knowledge about cgroup.

Maybe I miss something important?

---
 mm/memcontrol.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/memcontrol.c +4 -4
index bc01423277c5..62b7ec34ef1a 100644
@@ -5368,12 +5368,12 @@ static int mem_cgroup_move_account(struct page *page,
 	}
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock(&from->deferred_split_queue.split_queue_lock);
 	if (compound && !list_empty(page_deferred_list(page))) {
-		spin_lock(&from->deferred_split_queue.split_queue_lock);
 		list_del_init(page_deferred_list(page));
 		from->deferred_split_queue.split_queue_len--;
-		spin_unlock(&from->deferred_split_queue.split_queue_lock);
 	}
+	spin_unlock(&from->deferred_split_queue.split_queue_lock);
 #endif
 	/*
 	 * It is safe to change page->mem_cgroup here because the page
@@ -5385,13 +5385,13 @@ static int mem_cgroup_move_account(struct page *page,
 	page->mem_cgroup = to;
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock(&to->deferred_split_queue.split_queue_lock);
 	if (compound && list_empty(page_deferred_list(page))) {
-		spin_lock(&to->deferred_split_queue.split_queue_lock);
 		list_add_tail(page_deferred_list(page),
 			      &to->deferred_split_queue.split_queue);
 		to->deferred_split_queue.split_queue_len++;
-		spin_unlock(&to->deferred_split_queue.split_queue_lock);
 	}
+	spin_unlock(&to->deferred_split_queue.split_queue_lock);
 #endif
 
 	spin_unlock_irqrestore(&from->move_lock, flags);
Signature · 1 line
2.17.1
Signature · 2 lines
Michal Hocko
SUSE Labs
#16 lore raw
In reply to Wei Yang
Quoted 26 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Fixes 87eaceb3faa5 ("mm: thp: make deferred split shrinker memcg aware")

---
I notice the difference during code reading and just confused about the difference. No specific test is done since limited knowledge about cgroup.

Maybe I miss something important?

---
 mm/memcontrol.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/memcontrol.c +1 -0
index bc01423277c5..62b7ec34ef1a 100644
@@ -5368,12 +5368,12 @@ static int mem_cgroup_move_account(struct page *page,
        }
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+       spin_lock(&from->deferred_split_queue.split_queue_lock);

mm/memcontrol.c static int mem_cgroup_move_account(struct page *page, · line 5379

Quoted 10 lines
mm/memcontrol.c +1 -2
        if (compound && !list_empty(page_deferred_list(page))) {
-               spin_lock(&from->deferred_split_queue.split_queue_lock);
                list_del_init(page_deferred_list(page));
                from->deferred_split_queue.split_queue_len--;
-               spin_unlock(&from->deferred_split_queue.split_queue_lock);
        }
+       spin_unlock(&from->deferred_split_queue.split_queue_lock);
 #endif
        /*
         * It is safe to change page->mem_cgroup here because the page

So I suspect the lock placement has to do with the compound boolean value passed to the function.

One thing you might want to do is pull the "if (compound)" check out and place it outside of the spinlock check. It would then simplify this signficantly so it is something like
if (compound) {
spin_lock();
list = page_deferred_list(page);

  if (!list_empty(list)) {
    list_del_init(list);
    from->..split_queue_len--;

}
spin_unlock();
}

Same for the block below. I would pull the check for compound outside of the spinlock call since it is a value that shouldn't change and would eliminate an unnecessary lock in the non-compound case.

Quoted 17 lines
mm/memcontrol.c +2 -3
@@ -5385,13 +5385,13 @@ static int mem_cgroup_move_account(struct page *page,
        page->mem_cgroup = to;
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+       spin_lock(&to->deferred_split_queue.split_queue_lock);
        if (compound && list_empty(page_deferred_list(page))) {
-               spin_lock(&to->deferred_split_queue.split_queue_lock);
                list_add_tail(page_deferred_list(page),
                              &to->deferred_split_queue.split_queue);
                to->deferred_split_queue.split_queue_len++;
-               spin_unlock(&to->deferred_split_queue.split_queue_lock);
        }
+       spin_unlock(&to->deferred_split_queue.split_queue_lock);
 #endif
 
        spin_unlock_irqrestore(&from->move_lock, flags);
--
author
#6 lore raw
In reply to Michal Hocko
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

Signature · 2 lines
Wei Yang
Help you, Help me
author
#17 lore raw
In reply to Alexander Duyck
Quoted 25 lines · Alexander Duyck
Quoted 24 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Fixes 87eaceb3faa5 ("mm: thp: make deferred split shrinker memcg aware")

---
I notice the difference during code reading and just confused about the difference. No specific test is done since limited knowledge about cgroup.

Maybe I miss something important?

---
 mm/memcontrol.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/memcontrol.c +0 -0
index bc01423277c5..62b7ec34ef1a 100644
@@ -5368,12 +5368,12 @@ static int mem_cgroup_move_account(struct page *page,
        }
 
Quoted 16 lines

mm/memcontrol.c static int mem_cgroup_move_account(struct page *page, · line 5379

Quoted 12 lines
mm/memcontrol.c +2 -2
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+       spin_lock(&from->deferred_split_queue.split_queue_lock);
        if (compound && !list_empty(page_deferred_list(page))) {
-               spin_lock(&from->deferred_split_queue.split_queue_lock);
                list_del_init(page_deferred_list(page));
                from->deferred_split_queue.split_queue_len--;
-               spin_unlock(&from->deferred_split_queue.split_queue_lock);
        }
+       spin_unlock(&from->deferred_split_queue.split_queue_lock);
 #endif
        /*
         * It is safe to change page->mem_cgroup here because the page

So I suspect the lock placement has to do with the compound boolean value passed to the function.

Hey, Alexander

Thanks for your comment.

Quoted 9 lines

One thing you might want to do is pull the "if (compound)" check out and place it outside of the spinlock check. It would then simplify this signficantly so it is something like
if (compound) {
spin_lock();
list = page_deferred_list(page);

 if (!list_empty(list)) {
   list_del_init(list);
   from->..split_queue_len--;
Quoted 7 lines

}
spin_unlock();
}

Same for the block below. I would pull the check for compound outside of the spinlock call since it is a value that shouldn't change and would eliminate an unnecessary lock in the non-compound case.

This is reasonable, if no objection from others, I would change this in v2.

Signature · 2 lines
Wei Yang
Help you, Help me
#18 lore raw
Quoted 13 lines · Wei Yang
Quoted 13 lines

One thing you might want to do is pull the "if (compound)" check out and place it outside of the spinlock check. It would then simplify this signficantly so it is something like
if (compound) {
spin_lock();
list = page_deferred_list(page);

 if (!list_empty(list)) {
   list_del_init(list);
   from->..split_queue_len--;

}
spin_unlock();
}

Quoted 5 lines
Quoted 3 lines

Same for the block below. I would pull the check for compound outside of the spinlock call since it is a value that shouldn't change and would eliminate an unnecessary lock in the non-compound case.

This is reasonable, if no objection from others, I would change this in v2.

Looks fine to me; I don't see it as a necessary improvement but there's also no reason to object to it. It's definitely a patch that is needed, however, for the simple reason that with the existing code we can manipulate the deferred split queue incorrectly so either way works for me. Feel free to keep my acked-by.

author
#19 lore raw
Quoted 19 lines · David Rientjes
Quoted 17 lines · Wei Yang
Quoted 16 lines

One thing you might want to do is pull the "if (compound)" check out and place it outside of the spinlock check. It would then simplify this signficantly so it is something like
if (compound) {
spin_lock();
list = page_deferred_list(page);

 if (!list_empty(list)) {
   list_del_init(list);
   from->..split_queue_len--;

}
spin_unlock();
}

Same for the block below. I would pull the check for compound outside of the spinlock call since it is a value that shouldn't change and would eliminate an unnecessary lock in the non-compound case.

Quoted 7 lines
Quoted 1 line

This is reasonable, if no objection from others, I would change this in v2.

Looks fine to me; I don't see it as a necessary improvement but there's also no reason to object to it. It's definitely a patch that is needed, however, for the simple reason that with the existing code we can manipulate the deferred split queue incorrectly so either way works for me. Feel free to keep my acked-by.

Ah, thanks David. You are so supportive.

Signature · 2 lines
Wei Yang
Help you, Help me
maintainer
#7 lore raw
In reply to Wei Yang
Quoted 10 lines · Wei Yang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

My point is that the changelog should describe that bad behavior.

Quoted 3 lines

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

And how does list_empty check under the lock guarantee that the page is on the deferred list?

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#8 lore raw
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Wei Yang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

Quoted 8 lines

My point is that the changelog should describe that bad behavior.

Quoted 3 lines

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

And how does list_empty check under the lock guarantee that the page is on the deferred list?

Just one confusion, is this kind of description basic concept of concurrent programming? How detail level we need to describe the effect?

To me, grab the lock before accessing the critical section is obvious. list_empty and list_del should be the critical section. And the lock should protect the whole critical section instead of part of it.

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
maintainer
#9 lore raw
Quoted 19 lines · Wei Yang
Quoted 18 lines · Michal Hocko
Quoted 10 lines · Wei Yang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

My point is that the changelog should describe that bad behavior.

Quoted 3 lines

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

Quoted 5 lines
Quoted 2 lines

And how does list_empty check under the lock guarantee that the page is on the deferred list?

Just one confusion, is this kind of description basic concept of concurrent programming? How detail level we need to describe the effect?

When I write changelogs for patches like this I usually describe, what is the potential race - e.g.

	CPU1			CPU2
	path1			path2
	  check			  lock
	  			    operation2
				  unlock
	    lock
	    # check might not hold anymore
	    operation1
	    unlock

and what is the effect of the race - e.g. a crash, data corruption, pointless attempt for operation1 which fails with user visible effect etc.
This helps reviewers and everybody reading the code in the future to understand the locking scheme.

Quoted 1 line

To me, grab the lock before accessing the critical section is obvious.

It might be obvious but in many cases it is useful to minimize the locking and do a potentially race check before the lock is taken if the resulting operation can handle that.

Quoted 2 lines

list_empty and list_del should be the critical section. And the lock should protect the whole critical section instead of part of it.

I am not disputing that. What I am trying to say is that the changelog should described the problem in the first place.

Moreover, look at the code you are trying to fix. Sure extending the locking seem straightforward but does it result in a correct code though? See my question in the previous email. How do we know that the page is actually enqued in a non-empty list?

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#10 lore raw
Quoted 28 lines · Michal Hocko
Quoted 19 lines · Wei Yang
Quoted 18 lines · Michal Hocko
Quoted 10 lines · Wei Yang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

My point is that the changelog should describe that bad behavior.

Quoted 3 lines

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

Quoted 5 lines
Quoted 2 lines

And how does list_empty check under the lock guarantee that the page is on the deferred list?

Just one confusion, is this kind of description basic concept of concurrent programming? How detail level we need to describe the effect?

When I write changelogs for patches like this I usually describe, what is the potential race - e.g.

Quoted 10 lines
	CPU1			CPU2
	path1			path2
	  check			  lock
	  			    operation2
				  unlock
	    lock
	    # check might not hold anymore
	    operation1
	    unlock

Nice, I would prepare a changelog like this.

Quoted 15 lines

and what is the effect of the race - e.g. a crash, data corruption, pointless attempt for operation1 which fails with user visible effect etc.
This helps reviewers and everybody reading the code in the future to understand the locking scheme.

Quoted 1 line

To me, grab the lock before accessing the critical section is obvious.

It might be obvious but in many cases it is useful to minimize the locking and do a potentially race check before the lock is taken if the resulting operation can handle that.

Quoted 2 lines

list_empty and list_del should be the critical section. And the lock should protect the whole critical section instead of part of it.

Quoted 7 lines

I am not disputing that. What I am trying to say is that the changelog should described the problem in the first place.

Moreover, look at the code you are trying to fix. Sure extending the locking seem straightforward but does it result in a correct code though? See my question in the previous email. How do we know that the page is actually enqued in a non-empty list?

I may not get your point for the last sentence.

The list_empty() doesn't check the queue is empty but check the list, here is the page, is not enqueued into any list. Is this your concern?

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
author
#11 lore raw
In reply to Michal Hocko
Quoted 28 lines · Michal Hocko
Quoted 19 lines · Wei Yang
Quoted 18 lines · Michal Hocko
Quoted 10 lines · Wei Yang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

My point is that the changelog should describe that bad behavior.

Quoted 3 lines

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

Quoted 5 lines
Quoted 2 lines

And how does list_empty check under the lock guarantee that the page is on the deferred list?

Just one confusion, is this kind of description basic concept of concurrent programming? How detail level we need to describe the effect?

When I write changelogs for patches like this I usually describe, what is the potential race - e.g.

Quoted 10 lines
	CPU1			CPU2
	path1			path2
	  check			  lock
	  			    operation2
				  unlock
	    lock
	    # check might not hold anymore
	    operation1
	    unlock

Nice, I would prepare a changelog like this.

Quoted 15 lines

and what is the effect of the race - e.g. a crash, data corruption, pointless attempt for operation1 which fails with user visible effect etc.
This helps reviewers and everybody reading the code in the future to understand the locking scheme.

Quoted 1 line

To me, grab the lock before accessing the critical section is obvious.

It might be obvious but in many cases it is useful to minimize the locking and do a potentially race check before the lock is taken if the resulting operation can handle that.

Quoted 2 lines

list_empty and list_del should be the critical section. And the lock should protect the whole critical section instead of part of it.

Quoted 7 lines

I am not disputing that. What I am trying to say is that the changelog should described the problem in the first place.

Moreover, look at the code you are trying to fix. Sure extending the locking seem straightforward but does it result in a correct code though? See my question in the previous email. How do we know that the page is actually enqued in a non-empty list?

I may not get your point for the last sentence.

The list_empty() doesn't check the queue is empty but check the list, here is the page, is not enqueued into any list. Is this your concern?

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
author
#13 lore raw
In reply to Michal Hocko
Quoted 38 lines · Michal Hocko
Quoted 19 lines · Wei Yang
Quoted 18 lines · Michal Hocko
Quoted 10 lines · Wei Yang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

My point is that the changelog should describe that bad behavior.

Quoted 3 lines

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

Quoted 5 lines
Quoted 2 lines

And how does list_empty check under the lock guarantee that the page is on the deferred list?

Just one confusion, is this kind of description basic concept of concurrent programming? How detail level we need to describe the effect?

When I write changelogs for patches like this I usually describe, what is the potential race - e.g.

	CPU1			CPU2
	path1			path2
	  check			  lock
	  			    operation2
				  unlock
	    lock
	    # check might not hold anymore
	    operation1
	    unlock
Quoted 3 lines

and what is the effect of the race - e.g. a crash, data corruption, pointless attempt for operation1 which fails with user visible effect etc.

Hi, Michal, here is my attempt for an example. Hope this one looks good to you.

For example, the potential race would be:

CPU1 CPU2
mem_cgroup_move_account split_huge_page_to_list
!list_empty
lock
!list_empty
list_del
unlock
lock
# !list_empty might not hold anymore
list_del_init
unlock

When this sequence happens, the list_del_init() in mem_cgroup_move_account() would crash since the page is already been removed by list_del in split_huge_page_to_list().

Signature · 2 lines
Wei Yang
Help you, Help me
maintainer
#12 lore raw
In reply to Wei Yang
Quoted 1 line · Wei Yang

On Wed, Jan 08, 2020 at 10:40:41AM +0100, Michal Hocko wrote:

[...]

Quoted 9 lines
Quoted 4 lines

Moreover, look at the code you are trying to fix. Sure extending the locking seem straightforward but does it result in a correct code though? See my question in the previous email. How do we know that the page is actually enqued in a non-empty list?

I may not get your point for the last sentence.

The list_empty() doesn't check the queue is empty but check the list, here is the page, is not enqueued into any list. Is this your concern?

My bad. For some reason I have misread the code and thought this was get_deferred_split_queue rather than page_deferred_list. Sorry about the confusion.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#14 lore raw
In reply to Wei Yang
Quoted 61 lines · Wei Yang
Quoted 38 lines · Michal Hocko
Quoted 19 lines · Wei Yang
Quoted 18 lines · Michal Hocko
Quoted 10 lines · Wei Yang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

My point is that the changelog should describe that bad behavior.

Quoted 3 lines

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

Quoted 5 lines
Quoted 2 lines

And how does list_empty check under the lock guarantee that the page is on the deferred list?

Just one confusion, is this kind of description basic concept of concurrent programming? How detail level we need to describe the effect?

When I write changelogs for patches like this I usually describe, what is the potential race - e.g.

	CPU1			CPU2
	path1			path2
	  check			  lock
	  			    operation2
				  unlock
	    lock
	    # check might not hold anymore
	    operation1
	    unlock
Quoted 3 lines

and what is the effect of the race - e.g. a crash, data corruption, pointless attempt for operation1 which fails with user visible effect etc.

Hi, Michal, here is my attempt for an example. Hope this one looks good to you.

For example, the potential race would be:

CPU1 CPU2
mem_cgroup_move_account split_huge_page_to_list
!list_empty
lock
!list_empty
list_del
unlock
lock
# !list_empty might not hold anymore
list_del_init
unlock

Quoted 3 lines

When this sequence happens, the list_del_init() in mem_cgroup_move_account() would crash since the page is already been removed by list_del in split_huge_page_to_list().

Yes this looks much more informative. I would just add that this will crash if CONFIG_DEBUG_LIST.

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#15 lore raw
Quoted 62 lines · Michal Hocko
Quoted 61 lines · Wei Yang
Quoted 38 lines · Michal Hocko
Quoted 19 lines · Wei Yang
Quoted 18 lines · Michal Hocko
Quoted 10 lines · Wei Yang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Wei Yang

As all the other places, we grab the lock before manipulate the defer list. Current implementation may face a race condition.

Please always make sure to describe the effect of the change. Why a racy list_empty check matters?

Hmm... access the list without proper lock leads to many bad behaviors.

My point is that the changelog should describe that bad behavior.

Quoted 3 lines

For example, if we grab the lock after checking list_empty, the page may already be removed from list in split_huge_page_list. And then list_del_init would trigger bug.

Quoted 5 lines
Quoted 2 lines

And how does list_empty check under the lock guarantee that the page is on the deferred list?

Just one confusion, is this kind of description basic concept of concurrent programming? How detail level we need to describe the effect?

When I write changelogs for patches like this I usually describe, what is the potential race - e.g.

	CPU1			CPU2
	path1			path2
	  check			  lock
	  			    operation2
				  unlock
	    lock
	    # check might not hold anymore
	    operation1
	    unlock
Quoted 3 lines

and what is the effect of the race - e.g. a crash, data corruption, pointless attempt for operation1 which fails with user visible effect etc.

Hi, Michal, here is my attempt for an example. Hope this one looks good to you.

For example, the potential race would be:

CPU1 CPU2
mem_cgroup_move_account split_huge_page_to_list
!list_empty
lock
!list_empty
list_del
unlock
lock
# !list_empty might not hold anymore
list_del_init
unlock

Quoted 8 lines
Quoted 3 lines

When this sequence happens, the list_del_init() in mem_cgroup_move_account() would crash since the page is already been removed by list_del in split_huge_page_to_list().

Yes this looks much more informative. I would just add that this will crash if CONFIG_DEBUG_LIST.

Thanks!

Glad you like it~

Will prepare v2 with your suggestion :-)

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me