Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp311293lqo; Tue, 7 May 2024 23:45:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUHkc5xSpwZ4q9/LPEPC4HWEEKlGLOjNfOFmpje2Whli/AXsIN2m0fb3Rlifm/ZWAfB7Im0Jr19F/Oys+MWg0SP7gzoF1XOCX3WSAwgOQ== X-Google-Smtp-Source: AGHT+IE6ebNsAPgTVeysEB6YfAFjm31ABQ3lnvh/CNXOBCOPG39adTWFeYHFR1uzt3JugYbGbVAU X-Received: by 2002:a05:622a:2288:b0:43a:b55d:64e2 with SMTP id d75a77b69052e-43dbf7367e8mr22178301cf.45.1715150729889; Tue, 07 May 2024 23:45:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715150729; cv=pass; d=google.com; s=arc-20160816; b=w7iGicdndXmQsy3vPrktA2jUVKoQ5pggw3UDGVWvntL3nSgPWn1gaop09LohLkWdBp kZ+D2ftOAPsn2jh2gF+xC0RM1ASIslt3Z5y9DFuXNzpHUiBMAweqe/fB/UxeKZZCr5Ko ZlSE5dEfOsCsF9kWnB9Rev19eYTd5xPX/cVTAVCTpXnYslUAqvUiE8+ancF93yhmGkAx PlyHJ1OEhq7LVeZZ9gmsMGCg6WrN4UAbEO/WACyIFrOz548+qo4GFePK8uNrSKnSkbWv a/XYLvCo3og6BbG5aMi/oDJlgQ4gFwIybdeGAZjipoKJbB57V7v1WPQH3FOnc6VDTwVv hdBA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=6oT4fterRGqjdbsthJIMWwpMG6la6zI7ydewCHQozOE=; fh=mrCelbqgFluA56iy6OKRhq9JjvD4E7tl/r8TfPc0EaQ=; b=Jij5N2AjOY0acj4KCYsQO/fJCzesEl/45Yv8fhDL/oLd0rKUG41UI90St99HAwoi00 YzFTpEtY/Z+4cQOT7gX7y/BObdxYePmB3KilZc3BMmBWdz2Ovxbavh9pJ9LR0JOe2gHT bNrXkfnphpEfFqvJR3iajSoMXC2n7ZQ++2Ct9d1V2mPV0ahLFuWOJ3UJSRZVimpsb32c NSDSMso4SVdkf2vLy6uu78sT6IR60dx9odsioVycXSk10ELo7H/hsEJHlUry7tUNxLdj ULYcWWEH1WlGiRnLzrfG0LJz5aSzJxg9cWpFT5Njdyy4saqmMofUruf8Kn6MBbUIGrIv N8jA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-172766-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172766-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id z16-20020a05622a029000b004369fd7da74si13464790qtw.568.2024.05.07.23.45.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 23:45:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-172766-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-172766-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172766-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 890DF1C21BE5 for ; Wed, 8 May 2024 06:45:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB900225D9; Wed, 8 May 2024 06:45:19 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C82BC1E868 for ; Wed, 8 May 2024 06:45:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715150719; cv=none; b=t1fIhY9Z6i539QRNK+ypd5f1NjevAJA5IlfvDKztJwjXAvxatLToY8LGjGAGG7dEm9rMHi29YAQDEPz4px9RyI1Jn9lgwQ0s7e3FNlkV5aTY2wDqn6GFWZjsQwk/a9U9xTfkuTJuI1C0oX6GCE1nIOxQ0JZcg+Hs6hbFFC7R0OM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715150719; c=relaxed/simple; bh=rzqOWk5A0DG73uHNw+2w07aOZdppqhw/ndXShtnR8ME=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=dkYFRuZMiTIAmloDDsYQA3JE7Eh/XHsbDomVILyCCWPsdGhSSKXw2xggz41U4yrhCszMl9Y9VGLvvigHxVY3b9pfkYWRg61FyNFKskl4pH7N+zr2QTexlGj1uR7vpLXz/+U/0PeL+0R2M7eMD77ft/HyMY4dMFHEyiMOxJhfbx0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D85F91063; Tue, 7 May 2024 23:45:35 -0700 (PDT) Received: from [10.163.34.220] (unknown [10.163.34.220]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 52BAE3F587; Tue, 7 May 2024 23:45:07 -0700 (PDT) Message-ID: Date: Wed, 8 May 2024 12:15:09 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] arm64: mm: force write fault for atomic RMW instructions Content-Language: en-US To: Yang Shi , catalin.marinas@arm.com, will@kernel.org, scott@os.amperecomputing.com, cl@gentwo.org Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org References: <20240507223558.3039562-1-yang@os.amperecomputing.com> From: Anshuman Khandual In-Reply-To: <20240507223558.3039562-1-yang@os.amperecomputing.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hello Yang, On 5/8/24 04:05, Yang Shi wrote: > The atomic RMW instructions, for example, ldadd, actually does load + > add + store in one instruction, it may trigger two page faults, the > first fault is a read fault, the second fault is a write fault. It may or it will definitely create two consecutive page faults. What if the second write fault never came about. In that case an writable page table entry would be created unnecessarily (or even wrongfully), thus breaking the CoW. Just trying to understand, is the double page fault a possibility or a certainty. Does that depend on architecture (please do provide some links) or is it implementation defined. > > Some applications use atomic RMW instructions to populate memory, for > example, openjdk uses atomic-add-0 to do pretouch (populate heap memory But why cannot normal store operation is sufficient for pre-touching the heap memory, why read-modify-write (RMW) is required instead ? > at launch time) between v18 and v22. V18, V22 ? > > But the double page fault has some problems: > > 1. Noticeable TLB overhead. The kernel actually installs zero page with > readonly PTE for the read fault. The write fault will trigger a > write-protection fault (CoW). The CoW will allocate a new page and > make the PTE point to the new page, this needs TLB invalidations. The > tlb invalidation and the mandatory memory barriers may incur > significant overhead, particularly on the machines with many cores. > > 2. Break up huge pages. If THP is on the read fault will install huge > zero pages. The later CoW will break up the huge page and allocate > base pages instead of huge page. The applications have to rely on > khugepaged (kernel thread) to collapse huge pages asynchronously. > This also incurs noticeable performance penalty. > > 3. 512x page faults with huge page. Due to #2, the applications have to > have page faults for every 4K area for the write, this makes the speed > up by using huge page actually gone. The problems mentioned above are reasonable and expected. If the memory address has some valid data, it must have already reached there via a previous write access, which would have caused initial CoW transition ? If the memory address has no valid data to begin with, why even use RMW ? > > So it sounds pointless to have two page faults since we know the memory > will be definitely written very soon. Forcing write fault for atomic RMW > instruction makes some sense and it can solve the aforementioned problems: > > Firstly, it just allocates zero'ed page, no tlb invalidation and memory > barriers anymore. > Secondly, it can populate writable huge pages in the first place and > don't break them up. Just one page fault is needed for 2M area instrad > of 512 faults and also save cpu time by not using khugepaged. > > A simple micro benchmark which populates 1G memory shows the number of > page faults is reduced by half and the time spent by system is reduced > by 60% on a VM running on Ampere Altra platform. > > And the benchmark for anonymous read fault on 1G memory, file read fault > on 1G file (cold page cache and warm page cache) don't show noticeable > regression. > > Some other architectures also have code inspection in page fault path, > for example, SPARC and x86. Okay, I was about to ask, but is not calling get_user() for all data read page faults increase the cost for a hot code path in general for some potential savings for a very specific use case. Not sure if that is worth the trade-off. > > Signed-off-by: Yang Shi > --- > arch/arm64/include/asm/insn.h | 1 + > arch/arm64/mm/fault.c | 19 +++++++++++++++++++ > 2 files changed, 20 insertions(+) > > diff --git a/arch/arm64/include/asm/insn.h b/arch/arm64/include/asm/insn.h > index db1aeacd4cd9..5d5a3fbeecc0 100644 > --- a/arch/arm64/include/asm/insn.h > +++ b/arch/arm64/include/asm/insn.h > @@ -319,6 +319,7 @@ static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \ > * "-" means "don't care" > */ > __AARCH64_INSN_FUNCS(class_branch_sys, 0x1c000000, 0x14000000) > +__AARCH64_INSN_FUNCS(class_atomic, 0x3b200c00, 0x38200000) > > __AARCH64_INSN_FUNCS(adr, 0x9F000000, 0x10000000) > __AARCH64_INSN_FUNCS(adrp, 0x9F000000, 0x90000000) > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c > index 8251e2fea9c7..f7bceedf5ef3 100644 > --- a/arch/arm64/mm/fault.c > +++ b/arch/arm64/mm/fault.c > @@ -529,6 +529,7 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, > unsigned int mm_flags = FAULT_FLAG_DEFAULT; > unsigned long addr = untagged_addr(far); > struct vm_area_struct *vma; > + unsigned int insn; > > if (kprobe_page_fault(regs, esr)) > return 0; > @@ -586,6 +587,24 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, > if (!vma) > goto lock_mmap; > > + if (mm_flags & (FAULT_FLAG_WRITE | FAULT_FLAG_INSTRUCTION)) > + goto continue_fault; > + > + pagefault_disable(); > + > + if (get_user(insn, (unsigned int __user *) instruction_pointer(regs))) { > + pagefault_enable(); > + goto continue_fault; > + } > + > + if (aarch64_insn_is_class_atomic(insn)) { > + vm_flags = VM_WRITE; > + mm_flags |= FAULT_FLAG_WRITE; > + } > + > + pagefault_enable(); > + > +continue_fault: > if (!(vma->vm_flags & vm_flags)) { > vma_end_read(vma); > goto lock_mmap;