Skip to contentSkip to conversation

mm: mlock: remove lru_add_drain_all()

PATCH Superseded Shakeel Butt 5 participants 14 messages Oct 18, 2017 – Oct 19, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: mlock: remove lru_add_drain_all()

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

Is lru_add_drain_all() required by mlock()? The answer is no and the reason it is still in mlock() is to rapidly move mlocked pages to unevictable LRU. Without lru_add_drain_all() the mlocked pages which were on pagevec at mlock() time will be moved to evictable LRUs but will eventually be moved back to unevictable LRU by reclaim. So, we can safely remove lru_add_drain_all() from mlock(). Also there is no need for local lru_add_drain() as it will be called deep inside __mm_populate() (in follow_page_pte()).

---
 mm/mlock.c | 5 -----
 1 file changed, 5 deletions(-)
mm/mlock.c +0 -5
index dfc6f1912176..3ceb2935d1e0 100644
@@ -669,8 +669,6 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
 	if (!can_do_mlock())
 		return -EPERM;
 
-	lru_add_drain_all();	/* flush pagevec */
-
 	len = PAGE_ALIGN(len + (offset_in_page(start)));
 	start &= PAGE_MASK;
 
@@ -797,9 +795,6 @@ SYSCALL_DEFINE1(mlockall, int, flags)
 	if (!can_do_mlock())
 		return -EPERM;
 
-	if (flags & MCL_CURRENT)
-		lru_add_drain_all();	/* flush pagevec */
-
 	lock_limit = rlimit(RLIMIT_MEMLOCK);
 	lock_limit >>= PAGE_SHIFT;
 
Signature · 7 lines
2.15.0.rc1.287.g2b38de12cc-goog

--
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>
#1 lore raw
Quoted 14 lines · On Wed, 18 Oct 2017 16:17:30 -0700 Shakeel Butt <[email protected]> wrote:

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

Is lru_add_drain_all() required by mlock()? The answer is no and the reason it is still in mlock() is to rapidly move mlocked pages to unevictable LRU. Without lru_add_drain_all() the mlocked pages which were on pagevec at mlock() time will be moved to evictable LRUs but will eventually be moved back to unevictable LRU by reclaim. So, we can safely remove lru_add_drain_all() from mlock(). Also there is no need for local lru_add_drain() as it will be called deep inside __mm_populate() (in follow_page_pte()).

Quoted 2 lines

---

Does this perturb statistics around LRU pages in cgroups and meminfo about where the pages actually belong?

Balbir Singh.

--
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 17 lines · Balbir Singh
Quoted 14 lines · On Wed, 18 Oct 2017 16:17:30 -0700 Shakeel Butt <[email protected]> wrote:

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

Is lru_add_drain_all() required by mlock()? The answer is no and the reason it is still in mlock() is to rapidly move mlocked pages to unevictable LRU. Without lru_add_drain_all() the mlocked pages which were on pagevec at mlock() time will be moved to evictable LRUs but will eventually be moved back to unevictable LRU by reclaim. So, we can safely remove lru_add_drain_all() from mlock(). Also there is no need for local lru_add_drain() as it will be called deep inside __mm_populate() (in follow_page_pte()).

Quoted 6 lines
Quoted 2 lines

---

Does this perturb statistics around LRU pages in cgroups and meminfo about where the pages actually belong?

Yes, it would because the page can be in the evictable LRU until the reclaim moves it back to the unevictable LRU. However even with the draining there is a chance that the same thing can happen. For example, after mlock drains all caches and before getting mmap_sem, another cpu swaps in the page which the mlock syscall wants to mlock. Though the without draining the chance of this scenario will increase and in worst case mlock() can fail to move at most PAGEVEC_SIZE * (number of cpus - 1) pages to the unevictable LRU.

--
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 Shakeel Butt
Quoted 4 lines · Shakeel Butt

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

With and without this I patch I dont see much difference in number of instructions executed in the kernel for mlock() system call on POWER8 platform just after reboot (all the pagevecs might not been filled by then though). There is an improvement but its very less.

Could you share your latency numbers and how this patch is making them better.

Quoted 6 lines

Is lru_add_drain_all() required by mlock()? The answer is no and the reason it is still in mlock() is to rapidly move mlocked pages to unevictable LRU. Without lru_add_drain_all() the mlocked pages which were on pagevec at mlock() time will be moved to evictable LRUs but will eventually be moved back to unevictable LRU by reclaim. So, we

Wont this affect the performance during reclaim ?

Quoted 3 lines

can safely remove lru_add_drain_all() from mlock(). Also there is no need for local lru_add_drain() as it will be called deep inside __mm_populate() (in follow_page_pte()).

The following commit which originally added lru_add_drain_all() during mlock() and mlockall() has similar explanation.

8891d6da ("mm: remove lru_add_drain_all() from the munlock path")

"In addition, this patch add lru_add_drain_all() to sys_mlock() and sys_mlockall(). it isn't must. but it reduce the failure of moving to unevictable list. its failure can rescue in vmscan later. but reducing is better."

Which sounds like either we have to handle the active to inactive LRU movement during reclaim or it can be done here to speed up reclaim later on.

--
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 6 lines · Anshuman Khandual
Quoted 4 lines · Shakeel Butt

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

Quoted 8 lines

With and without this I patch I dont see much difference in number of instructions executed in the kernel for mlock() system call on POWER8 platform just after reboot (all the pagevecs might not been filled by then though). There is an improvement but its very less.

Could you share your latency numbers and how this patch is making them better.

The latency is very dependent on the workload and the number of cores on the machine. On production workload, the customers were complaining single mlock() was taking around 10 seconds on tmpfs files which were already in memory.

Quoted 9 lines
Quoted 6 lines

Is lru_add_drain_all() required by mlock()? The answer is no and the reason it is still in mlock() is to rapidly move mlocked pages to unevictable LRU. Without lru_add_drain_all() the mlocked pages which were on pagevec at mlock() time will be moved to evictable LRUs but will eventually be moved back to unevictable LRU by reclaim. So, we

Wont this affect the performance during reclaim ?

Yes, but reclaim is already a slow path and to seriously impact reclaim we will need a very very antagonistic workload which is very hard to trigger (i.e. for each mlock on a cpu, the pages being mlocked happen to be on the cache of other cpus).

Quoted 9 lines
Quoted 3 lines

can safely remove lru_add_drain_all() from mlock(). Also there is no need for local lru_add_drain() as it will be called deep inside __mm_populate() (in follow_page_pte()).

The following commit which originally added lru_add_drain_all() during mlock() and mlockall() has similar explanation.

8891d6da ("mm: remove lru_add_drain_all() from the munlock path")

Quoted 9 lines

"In addition, this patch add lru_add_drain_all() to sys_mlock() and sys_mlockall(). it isn't must. but it reduce the failure of moving to unevictable list. its failure can rescue in vmscan later. but reducing is better."

Which sounds like either we have to handle the active to inactive LRU movement during reclaim or it can be done here to speed up reclaim later on.

--
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
In reply to Shakeel Butt
Quoted 4 lines · Shakeel Butt

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

Hm. Isn't the point of mlock() to pay price upfront and make execution smoother after this?

With this you shift latency onto reclaim (and future memory allocation).

I'm not sure if it's a win.

Signature · 7 lines
 Kirill A. Shutemov

--
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 6 lines · Kirill A. Shutemov
Quoted 4 lines · Shakeel Butt

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

Quoted 7 lines

Hm. Isn't the point of mlock() to pay price upfront and make execution smoother after this?

With this you shift latency onto reclaim (and future memory allocation).

I'm not sure if it's a win.

It will not shift latency to fast path memory allocation. Reclaim happens on slow path and only reclaim may see more mlocked pages. Please note that the very antagonistics workload i.e. for each mlock on a cpu, the pages being mlocked happen to be on the cache of other cpus, is very hard to trigger.

--
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>

#7 lore raw
In reply to Shakeel Butt
Quoted 4 lines · Shakeel Butt

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

some numbers would be really nice

Quoted 3 lines

Is lru_add_drain_all() required by mlock()? The answer is no and the reason it is still in mlock() is to rapidly move mlocked pages to unevictable LRU.

Is this really true? lru_add_drain_all will flush the previously cached LRU pages. We are not flushing after the pages have been faulted in so this might not do anything wrt. mlocked pages, right?

Quoted 6 lines

Without lru_add_drain_all() the mlocked pages which
were on pagevec at mlock() time will be moved to evictable LRUs but will eventually be moved back to unevictable LRU by reclaim. So, we can safely remove lru_add_drain_all() from mlock(). Also there is no need for local lru_add_drain() as it will be called deep inside __mm_populate() (in follow_page_pte()).

Anyway, I do agree that lru_add_drain_all here is pointless. Either we should drain after the memory has been faulted in and mlocked or not at all. So the patch looks good to me I am just not sure about the changelog.

Quoted 31 lines
---
 mm/mlock.c | 5 -----
 1 file changed, 5 deletions(-)
mm/mlock.c +0 -5
index dfc6f1912176..3ceb2935d1e0 100644
@@ -669,8 +669,6 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
 	if (!can_do_mlock())
 		return -EPERM;
 
-	lru_add_drain_all();	/* flush pagevec */
-
 	len = PAGE_ALIGN(len + (offset_in_page(start)));
 	start &= PAGE_MASK;
 
@@ -797,9 +795,6 @@ SYSCALL_DEFINE1(mlockall, int, flags)
 	if (!can_do_mlock())
 		return -EPERM;
 
-	if (flags & MCL_CURRENT)
-		lru_add_drain_all();	/* flush pagevec */
-
 	lock_limit = rlimit(RLIMIT_MEMLOCK);
 	lock_limit >>= PAGE_SHIFT;
 
Signature · 1 line
2.15.0.rc1.287.g2b38de12cc-goog
Signature · 8 lines
Michal Hocko
SUSE Labs

--
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>
#8 lore raw
Quoted 8 lines · Michal Hocko
Quoted 4 lines · Shakeel Butt

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

some numbers would be really nice

On a production workload, customers complained that single mlock() call took around 10 seconds on mapped tmpfs files and the perf profile showed lru_add_drain_all as culprit.

I wasn't able to replicate the workload on my test machine but a simple workload of calling mlock() many type on a free machine shows significant difference. Other than workload, the machine size (number of cores) also matters.

Quoted 8 lines
Quoted 3 lines

Is lru_add_drain_all() required by mlock()? The answer is no and the reason it is still in mlock() is to rapidly move mlocked pages to unevictable LRU.

Is this really true? lru_add_drain_all will flush the previously cached LRU pages. We are not flushing after the pages have been faulted in so this might not do anything wrt. mlocked pages, right?

Sorry for the confusion. I wanted to say that if the pages which are being mlocked are on caches of remote cpus then lru_add_drain_all will move them to their corresponding LRUs and then remaining functionality of mlock will move them again from their evictable LRUs to unevictable LRU.

Quoted 40 lines
Quoted 6 lines

Without lru_add_drain_all() the mlocked pages which
were on pagevec at mlock() time will be moved to evictable LRUs but will eventually be moved back to unevictable LRU by reclaim. So, we can safely remove lru_add_drain_all() from mlock(). Also there is no need for local lru_add_drain() as it will be called deep inside __mm_populate() (in follow_page_pte()).

Anyway, I do agree that lru_add_drain_all here is pointless. Either we should drain after the memory has been faulted in and mlocked or not at all. So the patch looks good to me I am just not sure about the changelog.

Quoted 28 lines
---
 mm/mlock.c | 5 -----
 1 file changed, 5 deletions(-)
mm/mlock.c +0 -5
index dfc6f1912176..3ceb2935d1e0 100644
@@ -669,8 +669,6 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
      if (!can_do_mlock())
              return -EPERM;
 
-     lru_add_drain_all();    /* flush pagevec */
-
      len = PAGE_ALIGN(len + (offset_in_page(start)));
      start &= PAGE_MASK;
 
@@ -797,9 +795,6 @@ SYSCALL_DEFINE1(mlockall, int, flags)
      if (!can_do_mlock())
              return -EPERM;
 
-     if (flags & MCL_CURRENT)
-             lru_add_drain_all();    /* flush pagevec */
-
      lock_limit = rlimit(RLIMIT_MEMLOCK);
      lock_limit >>= PAGE_SHIFT;
 
Quoted 7 lines
Quoted 3 lines

--
2.15.0.rc1.287.g2b38de12cc-goog

--
Michal Hocko
SUSE Labs

--
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>

#9 lore raw
Quoted 7 lines · Shakeel Butt
Quoted 6 lines · Michal Hocko
Quoted 4 lines · Shakeel Butt

Recently we have observed high latency in mlock() in our generic library and noticed that users have started using tmpfs files even without swap and the latency was due to expensive remote LRU cache draining.

Quoted 6 lines
Quoted 2 lines

some numbers would be really nice

On a production workload, customers complained that single mlock() call took around 10 seconds on mapped tmpfs files and the perf profile showed lru_add_drain_all as culprit.

draining can take some time. I wouldn't expect orders of seconds so perf data would be definitely helpful in the changelog.

[...]

Quoted 10 lines
Quoted 4 lines

Is this really true? lru_add_drain_all will flush the previously cached LRU pages. We are not flushing after the pages have been faulted in so this might not do anything wrt. mlocked pages, right?

Sorry for the confusion. I wanted to say that if the pages which are being mlocked are on caches of remote cpus then lru_add_drain_all will move them to their corresponding LRUs and then remaining functionality of mlock will move them again from their evictable LRUs to unevictable LRU.

yes, but the point is that we are draining pages which might be not directly related to pages which _will_ be mlocked by the syscall. In fact those will stay on the cache. This is the primary reason why this draining doesn't make much sense.

Or am I still misunderstanding what you are saying here?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
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>
#10 lore raw
Quoted 8 lines

[...]

Quoted 6 lines

Sorry for the confusion. I wanted to say that if the pages which are being mlocked are on caches of remote cpus then lru_add_drain_all will move them to their corresponding LRUs and then remaining functionality of mlock will move them again from their evictable LRUs to unevictable LRU.

Quoted 7 lines

yes, but the point is that we are draining pages which might be not directly related to pages which _will_ be mlocked by the syscall. In fact those will stay on the cache. This is the primary reason why this draining doesn't make much sense.

Or am I still misunderstanding what you are saying here?

lru_add_drain_all() will drain everything irrespective if those pages are being mlocked or not.

--
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>

#11 lore raw
Quoted 13 lines · Shakeel Butt
Quoted 13 lines

[...]

Quoted 6 lines

Sorry for the confusion. I wanted to say that if the pages which are being mlocked are on caches of remote cpus then lru_add_drain_all will move them to their corresponding LRUs and then remaining functionality of mlock will move them again from their evictable LRUs to unevictable LRU.

yes, but the point is that we are draining pages which might be not directly related to pages which _will_ be mlocked by the syscall. In fact those will stay on the cache. This is the primary reason why this draining doesn't make much sense.

Quoted 5 lines
Quoted 2 lines

Or am I still misunderstanding what you are saying here?

lru_add_drain_all() will drain everything irrespective if those pages are being mlocked or not.

yes, let me be more specific. lru_add_drain_all will drain everything that has been cached at the time mlock is called. And that is not really related to the memory which will be faulted in (and cached) and mlocked by the syscall itself. Does it make more sense now?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
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>
#12 lore raw
Quoted 17 lines · Michal Hocko
Quoted 16 lines · Shakeel Butt
Quoted 15 lines

[...]

Quoted 6 lines

Sorry for the confusion. I wanted to say that if the pages which are being mlocked are on caches of remote cpus then lru_add_drain_all will move them to their corresponding LRUs and then remaining functionality of mlock will move them again from their evictable LRUs to unevictable LRU.

yes, but the point is that we are draining pages which might be not directly related to pages which _will_ be mlocked by the syscall. In fact those will stay on the cache. This is the primary reason why this draining doesn't make much sense.

Or am I still misunderstanding what you are saying here?

Quoted 8 lines
Quoted 2 lines

lru_add_drain_all() will drain everything irrespective if those pages are being mlocked or not.

yes, let me be more specific. lru_add_drain_all will drain everything that has been cached at the time mlock is called. And that is not really related to the memory which will be faulted in (and cached) and mlocked by the syscall itself. Does it make more sense now?

Yes, you are absolutely right. Sorry for the confusion.

--
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>

#13 lore raw
Quoted 21 lines · Shakeel Butt
Quoted 20 lines · Michal Hocko
Quoted 18 lines · Shakeel Butt
Quoted 8 lines

[...]

Quoted 6 lines

Sorry for the confusion. I wanted to say that if the pages which are being mlocked are on caches of remote cpus then lru_add_drain_all will move them to their corresponding LRUs and then remaining functionality of mlock will move them again from their evictable LRUs to unevictable LRU.

Quoted 7 lines

yes, but the point is that we are draining pages which might be not directly related to pages which _will_ be mlocked by the syscall. In fact those will stay on the cache. This is the primary reason why this draining doesn't make much sense.

Or am I still misunderstanding what you are saying here?

lru_add_drain_all() will drain everything irrespective if those pages are being mlocked or not.

Quoted 7 lines
Quoted 5 lines

yes, let me be more specific. lru_add_drain_all will drain everything that has been cached at the time mlock is called. And that is not really related to the memory which will be faulted in (and cached) and mlocked by the syscall itself. Does it make more sense now?

Yes, you are absolutely right. Sorry for the confusion.

So I think it would be much better to justify this change by arguing that paying a random overhead for something that doesn't relate to the work to be done is simply wrong.

Signature · 2 lines
Michal Hocko
SUSE Labs