From: Nicholas Piggin <npiggin@gmail.com> Date: 2018-08-28 11:20:48
Here are some patches that didn't get much comment last time. It
looks like x86 might benefit too though, so that might get people
interested.
I improved changelogs and added some comments, but no real logic
changes.
I hope I didn't get the x86 numbers wrong, they're more significant
than I expected so it could quite well be a problem with my test
(corrections welcome). Any data from other archs would be interesting
too.
Andrew perhaps if there aren't objections these could go in mm for
a while.
Thanks,
Nick
Nicholas Piggin (3):
mm/cow: don't bother write protectig already write-protected huge
pages
mm/cow: optimise pte dirty/accessed bits handling in fork
mm: optimise pte dirty/accessed bit setting by demand based pte
insertion
mm/huge_memory.c | 24 +++++++++++++++---------
mm/memory.c | 18 ++++++++++--------
mm/vmscan.c | 8 ++++++++
3 files changed, 33 insertions(+), 17 deletions(-)
--
2.18.0
From: Nicholas Piggin <npiggin@gmail.com> Date: 2018-08-28 11:20:52
This is the THP equivalent for 1b2de5d039c8 ("mm/cow: don't bother write
protecting already write-protected pages").
Explicit hugetlb pages don't get the same treatment because they don't
appear to have the right accessor functions.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
mm/huge_memory.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
From: Nicholas Piggin <npiggin@gmail.com> Date: 2018-08-28 11:20:56
fork clears dirty/accessed bits from new ptes in the child. This logic
has existed since mapped page reclaim was done by scanning ptes when
it may have been quite important. Today with physical based pte
scanning, there is less reason to clear these bits. Dirty bits are all
tested and cleared together and any dirty bit is the same as many
dirty bits. Any young bit is treated similarly to many young bits, but
not quite the same. A comment has been added where there is some
difference.
This eliminates a major source of faults powerpc/radix requires to set
dirty/accessed bits in ptes, speeding up a fork/exit microbenchmark by
about 5% on POWER9 (16600 -> 17500 fork/execs per second).
Skylake appears to have a micro-fault overhead too -- a test which
allocates 4GB anonymous memory, reads each page, then forks, and times
the child reading a byte from each page. The first pass over the pages
takes about 1000 cycles per page, the second pass takes about 27
cycles (TLB miss). With no additional minor faults measured due to
either child pass, and the page array well exceeding TLB capacity, the
large cost must be caused by micro faults caused by setting accessed
bit.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
mm/huge_memory.c | 2 --
mm/memory.c | 10 +++++-----
mm/vmscan.c | 8 ++++++++
3 files changed, 13 insertions(+), 7 deletions(-)
@@ -1830,10 +1830,9 @@ static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,entry=pte_mkspecial(pfn_t_pte(pfn,prot));out_mkwrite:-if(mkwrite){-entry=pte_mkyoung(entry);+entry=pte_mkyoung(entry);+if(mkwrite)entry=maybe_mkwrite(pte_mkdirty(entry),vma);-}set_pte_at(mm,addr,pte,entry);update_mmu_cache(vma,addr,pte);/* XXX: why not for insert_page? */
On Tue, Aug 28, 2018 at 4:20 AM Nicholas Piggin [off-list ref] wrote:
fork clears dirty/accessed bits from new ptes in the child. This logic
has existed since mapped page reclaim was done by scanning ptes when
it may have been quite important. Today with physical based pte
scanning, there is less reason to clear these bits.
Can you humor me, and make the dirty/accessed bit patches separate?
There is actually a difference wrt the dirty bit: if we unmap an area
with dirty pages, we have to do the special synchronous flush.
So a clean page in the virtual mapping is _literally_ cheaper to have.
This eliminates a major source of faults powerpc/radix requires to set
dirty/accessed bits in ptes, speeding up a fork/exit microbenchmark by
about 5% on POWER9 (16600 -> 17500 fork/execs per second).
I don't think the dirty bit matters.
The accessed bit I think may be worth keeping, so by all means remove the mkold.
Linus
From: Nicholas Piggin <npiggin@gmail.com> Date: 2018-08-29 23:12:26
On Wed, 29 Aug 2018 08:42:09 -0700
Linus Torvalds [off-list ref] wrote:
On Tue, Aug 28, 2018 at 4:20 AM Nicholas Piggin [off-list ref] wrote:
quoted
fork clears dirty/accessed bits from new ptes in the child. This logic
has existed since mapped page reclaim was done by scanning ptes when
it may have been quite important. Today with physical based pte
scanning, there is less reason to clear these bits.
Can you humor me, and make the dirty/accessed bit patches separate?
Yeah sure.
There is actually a difference wrt the dirty bit: if we unmap an area
with dirty pages, we have to do the special synchronous flush.
So a clean page in the virtual mapping is _literally_ cheaper to have.
Oh yeah true, that blasted thing. Good point.
Dirty micro fault seems to be the big one for my Skylake, takes 300
nanoseconds per access. Accessed takes about 100. (I think, have to
go over my benchmark a bit more carefully and re-test).
Dirty will happen less often though, particularly as most places we
do write to (stack, heap, etc) will be write protected for COW anyway,
I think. Worst case might be a big shared shm segment like a database
buffer cache, but those kind of forks should happen very very
infrequently I would hope.
Yes maybe we can do that. I'll split them up and try to get some
numbers for them individually.
Thanks,
Nick
On Wed, Aug 29, 2018 at 4:12 PM Nicholas Piggin [off-list ref] wrote:
Dirty micro fault seems to be the big one for my Skylake, takes 300
nanoseconds per access. Accessed takes about 100. (I think, have to
go over my benchmark a bit more carefully and re-test).
Yeah, but they only happen for shared areas after fork, which sounds
like it shouldn't be a big deal in most cases.
And I'm not entirely objecting to your patch per se, I just would want
to keep the accessed bit changes separate from the dirty bit ones.
*If* somebody has bisectable issues with it (performance or not), it
will then be clearer what the exact issue is.
Linus
From: Nicholas Piggin <npiggin@gmail.com> Date: 2018-08-29 23:57:56
On Wed, 29 Aug 2018 16:15:37 -0700
Linus Torvalds [off-list ref] wrote:
On Wed, Aug 29, 2018 at 4:12 PM Nicholas Piggin [off-list ref] wrote:
quoted
Dirty micro fault seems to be the big one for my Skylake, takes 300
nanoseconds per access. Accessed takes about 100. (I think, have to
go over my benchmark a bit more carefully and re-test).
Yeah, but they only happen for shared areas after fork, which sounds
like it shouldn't be a big deal in most cases.
You might be right there.
And I'm not entirely objecting to your patch per se, I just would want
to keep the accessed bit changes separate from the dirty bit ones.
*If* somebody has bisectable issues with it (performance or not), it
will then be clearer what the exact issue is.
Yeah that makes a lot of sense. I'll do a bit more testing and send
Andrew a respin at least with those split (and a good comment for
the dirty bit vs unmap handling that you pointed out).
Thanks,
Nick
Hi,
=20
On Tue, Aug 28, 2018 at 09:20:34PM +1000, Nicholas Piggin wrote:
quoted
Similarly to the previous patch, this tries to optimise dirty/accessed
bits in ptes to avoid access costs of hardware setting them.
=20
=20
This patch results in silent nios2 boot failures, silent meaning that
the boot stalls.
=20
...
Unpacking initramfs...
Freeing initrd memory: 2168K
workingset: timestamp_bits=3D30 max_order=3D15 bucket_order=3D0
jffs2: version 2.2. (NAND) =C2=A9 2001-2006 Red Hat, Inc.
random: fast init done
random: crng init done
=20
[no further activity until the qemu session is aborted]
=20
Reverting the patch fixes the problem. Bisect log is attached.
Thanks for bisecting it, I'll try to reproduce. Just qemu with no
obscure options? Interesting that it's hit nios2 but apparently not
other archs (yet).
Thanks,
Nick
Thanks for bisecting it, I'll try to reproduce. Just qemu with no
obscure options? Interesting that it's hit nios2 but apparently not
other archs (yet).
Hi,
On Tue, Aug 28, 2018 at 09:20:34PM +1000, Nicholas Piggin wrote:
quoted
Similarly to the previous patch, this tries to optimise dirty/accessed
bits in ptes to avoid access costs of hardware setting them.
This patch results in silent nios2 boot failures, silent meaning that
the boot stalls.
Okay I just got back to looking at this. The reason for the hang is
I think a bug in the nios2 TLB code, but maybe other archs have similar
issues.
In case of a missing / !present Linux pte, nios2 installs a TLB entry
with no permissions via its fast TLB exception handler (software TLB
fill). Then it relies on that causing a TLB permission exception in a
slower handler that calls handle_mm_fault to set the Linux pte and
flushes the old TLB. Then the fast exception handler will find the new
Linux pte.
With this patch, nios2 has a case where handle_mm_fault does not flush
the old TLB, which results in the TLB permission exception continually
being retried.
What happens now is that fault paths like do_read_fault will install a
Linux pte with the young bit clear and return. That will cause nios2 to
fault again but this time go down the bottom of handle_pte_fault and to
the access flags update with the young bit set. The young bit is seen to
be different, so that causes ptep_set_access_flags to do a TLB flush and
that finally allows the fast TLB handler to fire and pick up the new
Linux pte.
With this patch, the young bit is set in the first handle_mm_fault, so
the second handle_mm_fault no longer sees the ptes are different and
does not flush the TLB. The spurious fault handler also does not flush
them unless FAULT_FLAG_WRITE is set.
What nios2 should do is invalidate the TLB in update_mmu_cache. What it
*really* should do is install the new TLB entry, I have some patches to
make that work in qemu I can submit. But I would like to try getting
these dirty/accessed bit optimisation in 4.20, so I will send a simple
path to just do the TLB invalidate that could go in Andrew's git tree.
Is that agreeable with the nios2 maintainers?
Thanks,
Nick
=20
Hi,
=20
On Tue, Aug 28, 2018 at 09:20:34PM +1000, Nicholas Piggin wrote:
quoted
=20
Similarly to the previous patch, this tries to optimise
dirty/accessed
bits in ptes to avoid access costs of hardware setting them.
=20
This patch results in silent nios2 boot failures, silent meaning
that
the boot stalls.
Okay I just got back to looking at this. The reason for the hang is
I think a bug in the nios2 TLB code, but maybe other archs have
similar
issues.
=20
In case of a missing / !present Linux pte, nios2 installs a TLB entry
with no permissions via its fast TLB exception handler (software TLB
fill). Then it relies on that causing a TLB permission exception in a
slower handler that calls handle_mm_fault to set the Linux pte and
flushes the old TLB. Then the fast exception handler will find the
new
Linux pte.
=20
With this patch, nios2 has a case where handle_mm_fault does not
flush
the old TLB, which results in the TLB permission exception
continually
being retried.
=20
What happens now is that fault paths like do_read_fault will install
a
Linux pte with the young bit clear and return. That will cause nios2
to
fault again but this time go down the bottom of handle_pte_fault and
to
the access flags update with the young bit set. The young bit is seen
to
be different, so that causes ptep_set_access_flags to do a TLB flush
and
that finally allows the fast TLB handler to fire and pick up the new
Linux pte.
=20
With this patch, the young bit is set in the first handle_mm_fault,
so
the second handle_mm_fault no longer sees the ptes are different and
does not flush the TLB. The spurious fault handler also does not
flush
them unless FAULT_FLAG_WRITE is set.
=20
What nios2 should do is invalidate the TLB in update_mmu_cache. What
it
*really* should do is install the new TLB entry, I have some patches
to
make that work in qemu I can submit. But I would like to try getting
these dirty/accessed bit optimisation in 4.20, so I will send a
simple
path to just do the TLB invalidate that could go in Andrew's git
tree.
=20
Is that agreeable with the nios2 maintainers?
=20
Thanks,
Nick
=20
Hi,
On Tue, Aug 28, 2018 at 09:20:34PM +1000, Nicholas Piggin wrote:
quoted
Similarly to the previous patch, this tries to optimise
dirty/accessed
bits in ptes to avoid access costs of hardware setting them.
This patch results in silent nios2 boot failures, silent meaning
that
the boot stalls.
Okay I just got back to looking at this. The reason for the hang is
I think a bug in the nios2 TLB code, but maybe other archs have
similar
issues.
In case of a missing / !present Linux pte, nios2 installs a TLB entry
with no permissions via its fast TLB exception handler (software TLB
fill). Then it relies on that causing a TLB permission exception in a
slower handler that calls handle_mm_fault to set the Linux pte and
flushes the old TLB. Then the fast exception handler will find the
new
Linux pte.
With this patch, nios2 has a case where handle_mm_fault does not
flush
the old TLB, which results in the TLB permission exception
continually
being retried.
What happens now is that fault paths like do_read_fault will install
a
Linux pte with the young bit clear and return. That will cause nios2
to
fault again but this time go down the bottom of handle_pte_fault and
to
the access flags update with the young bit set. The young bit is seen
to
be different, so that causes ptep_set_access_flags to do a TLB flush
and
that finally allows the fast TLB handler to fire and pick up the new
Linux pte.
With this patch, the young bit is set in the first handle_mm_fault,
so
the second handle_mm_fault no longer sees the ptes are different and
does not flush the TLB. The spurious fault handler also does not
flush
them unless FAULT_FLAG_WRITE is set.
What nios2 should do is invalidate the TLB in update_mmu_cache. What
it
*really* should do is install the new TLB entry, I have some patches
to
make that work in qemu I can submit. But I would like to try getting
these dirty/accessed bit optimisation in 4.20, so I will send a
simple
path to just do the TLB invalidate that could go in Andrew's git
tree.
Is that agreeable with the nios2 maintainers?
Thanks,
Nick
Hi
Do you have patches to test?
I've been working on some, it has taken longer than I expected, I'll
hopefully have something to send out by tomorrow.
Thanks,
Nick