From patchwork Wed Apr 19 17:58:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13217222 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 56FECC6FD18 for ; Wed, 19 Apr 2023 17:58:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 780FA900004; Wed, 19 Apr 2023 13:58:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 709E9900003; Wed, 19 Apr 2023 13:58:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 583AD900004; Wed, 19 Apr 2023 13:58:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 43138900003 for ; Wed, 19 Apr 2023 13:58:43 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 13F8812038D for ; Wed, 19 Apr 2023 17:58:43 +0000 (UTC) X-FDA: 80698900926.11.34A13DE Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf10.hostedemail.com (Postfix) with ESMTP id F098CC001E for ; Wed, 19 Apr 2023 17:58:40 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=b8KAmYXr; spf=pass (imf10.hostedemail.com: domain of 30CtAZAYKCFEBDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=30CtAZAYKCFEBDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681927121; a=rsa-sha256; cv=none; b=im5x6CCUeWm5MMCmOpjEbjhS1AzF58hgS3Ai6ZGdgOgBxUbcj+EjAATEetCmYGylECkkCZ dNofmZ0cHzK2nUIFstEVgriu0DCNVgOkhwzNpTgwv0YTYfvmjXVwxvgF3191h+TlZsL4Ra T6n4974vPkLBT37L8Zm6TcoTuVOMcAY= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=b8KAmYXr; spf=pass (imf10.hostedemail.com: domain of 30CtAZAYKCFEBDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=30CtAZAYKCFEBDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681927121; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=GcWrIDO6GH5yJpHF/uWaF8jYk6l7KP2l0HcvbP0QLVU=; b=17NtF5sbJcZE6SFQ3sKj/LjHk0ENYWU4ZoXkwugvHDk45Ir0LRh8cTW5ZI8V86lgXMkhvS BilbvPI7fjsXezc8qECCU9YR3VSHq0Yav5PauD4sGPsnPUxGLr0CSIC/xah2yodfoHLYtN eICoMlHKdCULUqJdiOMbDWuxnqpoDVk= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-54c17fb245dso4183677b3.21 for ; Wed, 19 Apr 2023 10:58:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681927120; x=1684519120; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=GcWrIDO6GH5yJpHF/uWaF8jYk6l7KP2l0HcvbP0QLVU=; b=b8KAmYXrp9C9kGB31W/3uEQ+Go+Y0JBvLAJeq/hVhD34sd3sj8TtC7ZXZQomTcS79z koWqV9IeWVnH/KWCpZlkKCsuaTOCwJm9EwDVc5CZ3QdsTUQmOfZQpORT0i7eUSBKD2eH OVaH5Ru0DnYgzfXdR5pSiU6XEfHSftrrUL0zGt1TgtHvVXBbUO5c8c+f3zgcf+pSna7P us5hy2jwo/mFvr12D5Aa5lQWPb7AA4sqTbKPHPdE2N/Ee4VwO2ToiQKyRjcvxi9Nn8fL NJV4S5ZWjgNkLxP5R0+W8acutV2PTpe4E4PxUw7DNfFo7G8DukmtLRhxWIiy8l6PWihV 06aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681927120; x=1684519120; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=GcWrIDO6GH5yJpHF/uWaF8jYk6l7KP2l0HcvbP0QLVU=; b=PpKwk5iPJjDM+8bVMRA+Jn1nUqTJgCAgmj2VWp0d8S20YkL/F9LYuxjJDzmBN54tuK sgQEhc6K58SmrH8lK+Xpk2dLzum1pya2fjurO01pQLJfBpN64eZjYgvARwM+osFDRYrw ZWbT2bbWguj9oMzBMe1qqAySyJSj5asKUYhtXOLVVajbqeWEmJZ7DWzygpE6RxGf1c4g Hbp9MEqft1UBtztQTnITw553oB5fEE4+7cvJq4lFcmuC/MAGCV/Dv94/kJF45v8uci2p NN71OdT47zmxiV7hskg0XnlWNNx31tCGYPk3gprYkREhS+Mn4l186/Kfh89F31wRyH/p KOhQ== X-Gm-Message-State: AAQBX9dXlvweJom1NMS/yQLtvFdCyO94ud4CQlMHhbRz31DjRHqyHeVz dt9amSct5T/BOhdNC55d6k2vpsF0uIE= X-Google-Smtp-Source: AKy350ZN/NekAmaDhc7GI/R22C6mGO6MT6c2t8KbzwLAGZNIGAMtX16FRUineRnh4JOkeMeU/vb6XaN+afo= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:4b5f:8e2f:d0bb:d3cf]) (user=surenb job=sendgmr) by 2002:a25:cac1:0:b0:b6c:48c3:3c1c with SMTP id a184-20020a25cac1000000b00b6c48c33c1cmr336892ybg.13.1681927120024; Wed, 19 Apr 2023 10:58:40 -0700 (PDT) Date: Wed, 19 Apr 2023 10:58:36 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230419175836.3857458-1-surenb@google.com> Subject: [PATCH v3 1/1] mm: do not increment pgfault stats when page fault handler retries From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: willy@infradead.org, hannes@cmpxchg.org, mhocko@suse.com, josef@toxicpanda.com, jack@suse.cz, ldufour@linux.ibm.com, laurent.dufour@fr.ibm.com, michel@lespinasse.org, liam.howlett@oracle.com, jglisse@google.com, vbabka@suse.cz, minchan@google.com, dave@stgolabs.net, punit.agrawal@bytedance.com, lstoakes@gmail.com, surenb@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com X-Rspam-User: X-Rspamd-Queue-Id: F098CC001E X-Rspamd-Server: rspam01 X-Stat-Signature: 1iroti7dw7s6miuw7c11ect9xo76ru5r X-HE-Tag: 1681927120-668281 X-HE-Meta: U2FsdGVkX19Lye6vsmt5Cw2spTnQIYwkDfQx40rij28pDYIWpXeex6xipb1Zq0vb5Fmo1Yqwtnh8fE7Xl2cc/7Mu/XQNHj78Y4Js+dEgd1U8dLyjn213OTvcPnT2XOHnfNZ6W7y6U4rYkzbsQkJAxXofn/44NF2DaIiC3UD4KJ0PI+HeJ//ZI5Ba8n/K7XoUIAD4Bg/0d35jAwMk+HGDK9Wx0Zm+sDhlGyrhWg0qH4nc0QKvG5YlL786i9MxNcFEI8HGxixL81fdeHLpwDXnjbc3hR24QKSZRNC7H6OJpLL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r0/gDRzzZWmmmLIGcryi41pmCXtumkVwLFdQ9ZsPzysBvpEyjhJJArBusEnUrpmSNEVS7j5JBlUqNYoVLMnQ4WOH783DMe5upBAU1pkwrFCJRVJ/gj87amJOArvBpCIgdPxOchKGWqm1/HSwkBkkI4G50mg/+Z/UvO7MBtMcqZYvTB8YFvB1U5XmPU90RrbBDRa+mspJ8csQQ0C/fHIhzN2rn//S/oCKiGgulSPPRTLrisfnfZggQWselLkcz53wDD5HX5VR4H7jD2++d88Higkhm44sTurUsja4TaKHsU6Ks2tuKL3UQ== 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: If the page fault handler requests a retry, we will count the fault multiple times. This is a relatively harmless problem as the retry paths are not often requested, and the only user-visible problem is that the fault counter will be slightly higher than it should be. Nevertheless, userspace only took one fault, and should not see the fact that the kernel had to retry the fault multiple times. Move page fault accounting into mm_account_fault() and skip incomplete faults which will be accounted upon completion. Fixes: d065bd810b6d ("mm: retry page fault when blocking on disk transfer") Signed-off-by: Suren Baghdasaryan Reviewed-by: Matthew Wilcox (Oracle) Acked-by: Peter Xu --- Changes from v2 (https://lore.kernel.org/all/20230415000818.1955007-1-surenb@google.com/): - Updated comments per Matthew Wilcox and Peter Xu mm/memory.c | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 01a23ad48a04..7ce9286ee4d4 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5080,24 +5080,31 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, * updates. However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should * still be in per-arch page fault handlers at the entry of page fault. */ -static inline void mm_account_fault(struct pt_regs *regs, +static inline void mm_account_fault(struct mm_struct *mm, struct pt_regs *regs, unsigned long address, unsigned int flags, vm_fault_t ret) { bool major; + /* Incomplete faults will be accounted upon completion. */ + if (ret & VM_FAULT_RETRY) + return; + /* - * We don't do accounting for some specific faults: - * - * - Unsuccessful faults (e.g. when the address wasn't valid). That - * includes arch_vma_access_permitted() failing before reaching here. - * So this is not a "this many hardware page faults" counter. We - * should use the hw profiling for that. - * - * - Incomplete faults (VM_FAULT_RETRY). They will only be counted - * once they're completed. + * To preserve the behavior of older kernels, PGFAULT counters record + * both successful and failed faults, as opposed to perf counters, + * which ignore failed cases. */ - if (ret & (VM_FAULT_ERROR | VM_FAULT_RETRY)) + count_vm_event(PGFAULT); + count_memcg_event_mm(mm, PGFAULT); + + /* + * Do not account for unsuccessful faults (e.g. when the address wasn't + * valid). That includes arch_vma_access_permitted() failing before + * reaching here. So this is not a "this many hardware page faults" + * counter. We should use the hw profiling for that. + */ + if (ret & VM_FAULT_ERROR) return; /* @@ -5180,21 +5187,22 @@ static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, unsigned int flags, struct pt_regs *regs) { + /* If the fault handler drops the mmap_lock, vma may be freed */ + struct mm_struct *mm = vma->vm_mm; vm_fault_t ret; __set_current_state(TASK_RUNNING); - count_vm_event(PGFAULT); - count_memcg_event_mm(vma->vm_mm, PGFAULT); - ret = sanitize_fault_flags(vma, &flags); if (ret) - return ret; + goto out; if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, flags & FAULT_FLAG_INSTRUCTION, - flags & FAULT_FLAG_REMOTE)) - return VM_FAULT_SIGSEGV; + flags & FAULT_FLAG_REMOTE)) { + ret = VM_FAULT_SIGSEGV; + goto out; + } /* * Enable the memcg OOM handling for faults triggered in user @@ -5223,8 +5231,8 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) mem_cgroup_oom_synchronize(false); } - - mm_account_fault(regs, address, flags, ret); +out: + mm_account_fault(mm, regs, address, flags, ret); return ret; }