From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 971BAC433EF for ; Fri, 27 May 2022 13:49:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0244B8D0012; Fri, 27 May 2022 09:49:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F12048D0002; Fri, 27 May 2022 09:49:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3D418D0012; Fri, 27 May 2022 09:49:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C1B418D0002 for ; Fri, 27 May 2022 09:49:12 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 95B4B347F3 for ; Fri, 27 May 2022 13:49:12 +0000 (UTC) X-FDA: 79511654544.17.4B59EE1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf31.hostedemail.com (Postfix) with ESMTP id 2E60B20047 for ; Fri, 27 May 2022 13:48:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653659351; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=u3wM6LMtd0WRKF0TJX2qWAIusndSWh4uKD1DEnhNev8=; b=CTZvYTIfZ3Y27fh0YE9H49d/CjM7XmxFtq9fPAj+8/Un+oJR7katf0EA9Lln7xIE0iFhId fV4t8wJpBwuTca0HlSfFqewPywVCUnp/5Wh9NqLT2UjLjajmq2YZqIhr/aFw6kNoYT/7Bb B9T3twSrXbrZ9vZDjddsqu/u8q1YHk4= Received: from mail-il1-f198.google.com (mail-il1-f198.google.com [209.85.166.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-655-7TC3khK-MLGZsJyoDGXo7A-1; Fri, 27 May 2022 09:49:08 -0400 X-MC-Unique: 7TC3khK-MLGZsJyoDGXo7A-1 Received: by mail-il1-f198.google.com with SMTP id q15-20020a056e0220ef00b002d15dcd2750so3122313ilv.1 for ; Fri, 27 May 2022 06:49:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=u3wM6LMtd0WRKF0TJX2qWAIusndSWh4uKD1DEnhNev8=; b=WM33szw4mS0AIUsBlATD9tfTwHXYDBV+19J8xlcZoJxz3PdltisdKtcOk4pazfaMdh ODvadiqLWIvQvlmlLbeTY7nsw9mo96+6ZoR1ENZDqsSbKa3gB6W68MTrZ/jm/F64/pDQ FYncI5AHgh6p8iMKfumk+vQKSqj5Iv6T+XodztA7hg5RvKmT5Z29QODu63VZIewImxkD UP5YVCdXr78Us8VzQUHnW92l24KgGCu8L9cOHzuKhKHFLahmpVW1mNcGxuJ0x+rZ98Y2 V2vKXeLJf3z0f6oPK/JZ4BQE2dxOmG9R2sRc2BEiw+B9fjASKN24kO38ATTwjt532Zns cfTg== X-Gm-Message-State: AOAM5321nCHTnhWIokvptIxlCkeV5xvHosj2dwqntbRLGGK10IN+IwJq lmvCo0JBUBa+ESACP4qq8vBssvX/MJVfZ0mHX1TpM7+18V2OcMHFBdTUmaSUXpPDs0tUGICuyil Lk/Zp7MpvJQc= X-Received: by 2002:a05:6602:1584:b0:664:ab0f:5339 with SMTP id e4-20020a056602158400b00664ab0f5339mr10502548iow.146.1653659347390; Fri, 27 May 2022 06:49:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCqN3qwLLuYZMqrMI7aGsb6Zn5OHs31WIJQ3aUgDc8PhP19ReX/GZlaKbEBJ1PdD9YunS6AA== X-Received: by 2002:a05:6602:1584:b0:664:ab0f:5339 with SMTP id e4-20020a056602158400b00664ab0f5339mr10502527iow.146.1653659347005; Fri, 27 May 2022 06:49:07 -0700 (PDT) Received: from xz-m1.local (cpec09435e3e0ee-cmc09435e3e0ec.cpe.net.cable.rogers.com. [99.241.198.116]) by smtp.gmail.com with ESMTPSA id x25-20020a029719000000b0032b3a7817d6sm563254jai.154.2022.05.27.06.49.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 May 2022 06:49:06 -0700 (PDT) Date: Fri, 27 May 2022 09:49:01 -0400 From: Peter Xu To: Heiko Carstens Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Richard Henderson , David Hildenbrand , Matt Turner , Albert Ou , Michal Simek , Russell King , Ivan Kokshaysky , linux-riscv@lists.infradead.org, Alexander Gordeev , Dave Hansen , Jonas Bonn , Will Deacon , "James E . J . Bottomley" , "H . Peter Anvin" , Andrea Arcangeli , openrisc@lists.librecores.org, linux-s390@vger.kernel.org, Ingo Molnar , linux-m68k@lists.linux-m68k.org, Palmer Dabbelt , Chris Zankel , Peter Zijlstra , Alistair Popple , linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, Vlastimil Babka , Thomas Gleixner , sparclinux@vger.kernel.org, Christian Borntraeger , Stafford Horne , Michael Ellerman , x86@kernel.org, Thomas Bogendoerfer , Paul Mackerras , linux-arm-kernel@lists.infradead.org, Sven Schnelle , Benjamin Herrenschmidt , linux-xtensa@linux-xtensa.org, Nicholas Piggin , linux-sh@vger.kernel.org, Vasily Gorbik , Borislav Petkov , linux-mips@vger.kernel.org, Max Filippov , Helge Deller , Vineet Gupta , Al Viro , Paul Walmsley , Johannes Weiner , Anton Ivanov , Catalin Marinas , linux-um@lists.infradead.org, linux-alpha@vger.kernel.org, Johannes Berg , linux-ia64@vger.kernel.org, Geert Uytterhoeven , Dinh Nguyen , Guo Ren , linux-snps-arc@lists.infradead.org, Hugh Dickins , Rich Felker , Andy Lutomirski , Richard Weinberger , linuxppc-dev@lists.ozlabs.org, Brian Cain , Yoshinori Sato , Andrew Morton , Stefan Kristiansson , linux-parisc@vger.kernel.org, "David S . Miller" , Janosch Frank Subject: Re: [PATCH v3] mm: Avoid unnecessary page fault retires on shared memory types Message-ID: References: <20220524234531.1949-1-peterx@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2E60B20047 X-Stat-Signature: 43rmpy3xdaf61r4ecfmyauji1c4aeiik Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CTZvYTIf; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf31.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com X-Rspam-User: X-HE-Tag: 1653659314-369384 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hi, Heiko, On Fri, May 27, 2022 at 02:23:42PM +0200, Heiko Carstens wrote: > On Tue, May 24, 2022 at 07:45:31PM -0400, Peter Xu wrote: > > I observed that for each of the shared file-backed page faults, we're very > > likely to retry one more time for the 1st write fault upon no page. It's > > because we'll need to release the mmap lock for dirty rate limit purpose > > with balance_dirty_pages_ratelimited() (in fault_dirty_shared_page()). > > > > Then after that throttling we return VM_FAULT_RETRY. > > > > We did that probably because VM_FAULT_RETRY is the only way we can return > > to the fault handler at that time telling it we've released the mmap lock. > > > > However that's not ideal because it's very likely the fault does not need > > to be retried at all since the pgtable was well installed before the > > throttling, so the next continuous fault (including taking mmap read lock, > > walk the pgtable, etc.) could be in most cases unnecessary. > > > > It's not only slowing down page faults for shared file-backed, but also add > > more mmap lock contention which is in most cases not needed at all. > > > > To observe this, one could try to write to some shmem page and look at > > "pgfault" value in /proc/vmstat, then we should expect 2 counts for each > > shmem write simply because we retried, and vm event "pgfault" will capture > > that. > > > > To make it more efficient, add a new VM_FAULT_COMPLETED return code just to > > show that we've completed the whole fault and released the lock. It's also > > a hint that we should very possibly not need another fault immediately on > > this page because we've just completed it. > > > > This patch provides a ~12% perf boost on my aarch64 test VM with a simple > > program sequentially dirtying 400MB shmem file being mmap()ed and these are > > the time it needs: > > > > Before: 650.980 ms (+-1.94%) > > After: 569.396 ms (+-1.38%) > > > > I believe it could help more than that. > > > > We need some special care on GUP and the s390 pgfault handler (for gmap > > code before returning from pgfault), the rest changes in the page fault > > handlers should be relatively straightforward. > > > > Another thing to mention is that mm_account_fault() does take this new > > fault as a generic fault to be accounted, unlike VM_FAULT_RETRY. > > > > I explicitly didn't touch hmm_vma_fault() and break_ksm() because they do > > not handle VM_FAULT_RETRY even with existing code, so I'm literally keeping > > them as-is. > > > > Signed-off-by: Peter Xu > ... > > diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c > > index e173b6187ad5..9503a7cfaf03 100644 > > --- a/arch/s390/mm/fault.c > > +++ b/arch/s390/mm/fault.c > > @@ -339,6 +339,7 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) > > unsigned long address; > > unsigned int flags; > > vm_fault_t fault; > > + bool need_unlock = true; > > bool is_write; > > > > tsk = current; > > @@ -433,6 +434,13 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) > > goto out_up; > > goto out; > > } > > + > > + /* The fault is fully completed (including releasing mmap lock) */ > > + if (fault & VM_FAULT_COMPLETED) { > > + need_unlock = false; > > + goto out_gmap; > > + } > > + > > if (unlikely(fault & VM_FAULT_ERROR)) > > goto out_up; > > > > @@ -452,6 +460,7 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) > > mmap_read_lock(mm); > > goto retry; > > } > > +out_gmap: > > if (IS_ENABLED(CONFIG_PGSTE) && gmap) { > > address = __gmap_link(gmap, current->thread.gmap_addr, > > address); > > @@ -466,7 +475,8 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) > > } > > fault = 0; > > out_up: > > - mmap_read_unlock(mm); > > + if (need_unlock) > > + mmap_read_unlock(mm); > > out: > > This seems to be incorrect. __gmap_link() requires the mmap_lock to be > held. Christian, Janosch, or David, could you please check? Thanks for pointing that out. Indeed I see the clue right above the comment of __gmap_link(): /* * ... * The mmap_lock of the mm that belongs to the address space must be held * when this function gets called. */ int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr) A further fact is it'll walk the pgtable right afterwards, assuming gmap->mm will definitely be the current mm or it'll definitely go wrong. I'll change s390 to retake the lock with the new COMPLETE retcode, so at least it'll avoid one pgtable work procedure even if the lock overhead was kept. With that, one more possible further optimization for s390 only will be conditionally not taking that lock when !CONFIG_PGSTE, but I have totally no idea whether that'll be a common use case, so I plan to leave that for later in all cases. Actually after doing that the whole changeset of s390 is even more straightforward: ---8<--- diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c index e173b6187ad5..4608cc962ecf 100644 --- a/arch/s390/mm/fault.c +++ b/arch/s390/mm/fault.c @@ -433,6 +433,17 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) goto out_up; goto out; } + + /* The fault is fully completed (including releasing mmap lock) */ + if (fault & VM_FAULT_COMPLETED) { + /* + * Gmap will need the mmap lock again, so retake it. TODO: + * only conditionally take the lock when CONFIG_PGSTE set. + */ + mmap_read_lock(mm); + goto out_gmap; + } + if (unlikely(fault & VM_FAULT_ERROR)) goto out_up; @@ -452,6 +463,7 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) mmap_read_lock(mm); goto retry; } +out_gmap: if (IS_ENABLED(CONFIG_PGSTE) && gmap) { address = __gmap_link(gmap, current->thread.gmap_addr, address); ---8<--- Thanks, -- Peter Xu