From patchwork Fri Jun 9 00:51:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13273055 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 1FD78C87FE2 for ; Fri, 9 Jun 2023 00:52:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFE6E8E0007; Thu, 8 Jun 2023 20:52:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AD5AD8E0001; Thu, 8 Jun 2023 20:52:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9768C8E0007; Thu, 8 Jun 2023 20:52:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 89B1D8E0001 for ; Thu, 8 Jun 2023 20:52:15 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4BB011402F6 for ; Fri, 9 Jun 2023 00:52:15 +0000 (UTC) X-FDA: 80881383030.07.7F5101A Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf14.hostedemail.com (Postfix) with ESMTP id 81FE6100011 for ; Fri, 9 Jun 2023 00:52:13 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=aoHc7JPC; spf=pass (imf14.hostedemail.com: domain of 3vHeCZAYKCN8TVSFOCHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--surenb.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3vHeCZAYKCN8TVSFOCHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686271933; a=rsa-sha256; cv=none; b=blGjjgLtRUq+YOxfxWL4mSdTfN2o89cYJp2b7fCH8ozqIkucYzlonqO6wo9nIXKSKIM2K1 28tXS7GSrLuwV5CWKj7DqgqBGxT9RcPHdr5SVdiH0JXq1ZX8FwMRos3wVTOOEA+s+Vxo07 1OVmcnPo/iotM+omuf8ieqoI+qIW+4U= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=aoHc7JPC; spf=pass (imf14.hostedemail.com: domain of 3vHeCZAYKCN8TVSFOCHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--surenb.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3vHeCZAYKCN8TVSFOCHPPHMF.DPNMJOVY-NNLWBDL.PSH@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=1686271933; 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:in-reply-to:references:references:dkim-signature; bh=uuo3Il/2NbgOWAM0rjqTihVZSZhUnoZMQ/ESG1PbGOc=; b=fTrTpdOIAUz5eQOI26khEJ964Cevzie5ge847SwH1I/Ka5F/yDjnC9OPONrkaxWlBio+ib yUNcKFnAHw1D/LaESg8X2ceyV27s9VDGSP/V/C5ekSp1MEiptLSEwJe2mcfTd2TkXFwdsC H5pCxAWNqG99xOeVT3qhFiuTZ4ss64k= Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-53f44c25595so208624a12.2 for ; Thu, 08 Jun 2023 17:52:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686271932; x=1688863932; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=uuo3Il/2NbgOWAM0rjqTihVZSZhUnoZMQ/ESG1PbGOc=; b=aoHc7JPCZ32CAYt9q46WDYGEOxxS+8Zg8n+MkQ8tBFVy2AxaNM8+3Gds918auhPTHa q1i93JJIOBjYqWdat77Bw43ovYfILELgsQ3A15M4ncn74YtVwsuP81alu9yqHz/6QEf4 0ZCIwnE+WqrId9F8Np2B9UzySwNVPcW9qaPUxrGBEsK8nkCGUUE7v/JS22HdzPoOb4MB Chi4Dc24/vK44G+H3O7mb/dNtV3gVAS4dKZye3QS8d/JCKeQ8FR8f6Rt4chkxNrZVvil NBHO4/LslE98QkZt8lZcAJ+QXDZaDtGSI1vY2Loous6hPUzUbnAktXCDF6R5gPFfIwoD hcMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686271932; x=1688863932; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uuo3Il/2NbgOWAM0rjqTihVZSZhUnoZMQ/ESG1PbGOc=; b=lKckqhEHfGuUY4MbWFmsQ9EQzMcjl1omknWL2nMgQeTXWIde0bzaa9rkfsHJHL+pZu ARhF5JAEmhU2dwm76L7aeQJTME/0zuFayq9PB27XH7gYsGydPyTcgOwvuX/Ir4a/epd8 utCMUCR0Yb5m/APm7LrYERZTnHdkL7B1yhOluCiRzI8+28KEgY6eDNHxg+6+IrGvxIpi W24pY9wNU7wzJ5krTTFImDAP5Ngwm9KjPWiIfwfxT4MkedNOfIAGDUTF7R3wi0oRw2bC qWi+7uUyEitCxkbhQNUvNvMzWcdTSODtJKr1qW6iCIXxGdPl4FghRlAb0zfFm9TZToGu 5RYg== X-Gm-Message-State: AC+VfDylJubj8XEPayzAzGPXxmURbfXuZdnrgpImsDR9hWdZLO6aA1jy R+00ploASQppkzAqEQ5c1YiZ6HOafcU= X-Google-Smtp-Source: ACHHUZ6b1B6MYsfuF9F0w1PhABF3eJ3XJNF2fY5wRRyw5gd4GldbBtWiJ9mZhA7X4yciara2bOj4oc1yZEs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:c03e:d3b7:767a:9467]) (user=surenb job=sendgmr) by 2002:a63:d44:0:b0:503:77c9:45aa with SMTP id 4-20020a630d44000000b0050377c945aamr225410pgn.9.1686271932107; Thu, 08 Jun 2023 17:52:12 -0700 (PDT) Date: Thu, 8 Jun 2023 17:51:56 -0700 In-Reply-To: <20230609005158.2421285-1-surenb@google.com> Mime-Version: 1.0 References: <20230609005158.2421285-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230609005158.2421285-5-surenb@google.com> Subject: [PATCH v2 4/6] mm: drop VMA lock before waiting for migration 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, hdanton@sina.com, apopple@nvidia.com, peterx@redhat.com, ying.huang@intel.com, david@redhat.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, pasha.tatashin@soleen.com, surenb@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 81FE6100011 X-Stat-Signature: edkwpjobrz56g66f9k9q93t3snzijewm X-Rspam-User: X-HE-Tag: 1686271933-903926 X-HE-Meta: U2FsdGVkX18zK3gH07r4M7AlqWjFj2Xxudea7BEnjv3FFWnbqhBojtugx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UQrXz082c0oqn7JTzQ4cvHEE+gCp+DS4yibRAPg5xucdeqvigtE/Qz8Pb9bg0lWeHq/fokB5PCB2mrxx8k6cy268OVLnbvUrceKfFBF6m/moLyQwlD4kKDgSRM37ysT+cwriWuR98ogwaoLAPSKAA1LXDKy3M8UG2tyXZr82oIl94lwMAq9WHCihoJf5cRH4PcGF+Ire5pwiaFlxGjzHqqPFmeaNjDMlHgck2maGNFwPLIGmXh+4jeXvNXDlgyc5wb79nL62F/RgngS3aRqSsszNLldYKY67SXKBY9MJggjKpN6vE8dMNQyZgbEEG4iYprdSI8uDqpGB7okWFS8j307UJSzfGq6WaEvj74RBRc0XVaxIXUtbPZ/vX7ud3It9tVmjKCQ1VZaG2Af8khVngWtHM1c68teP3TtaMv+snxeQIvvbDkj1wW4lHZLzlrO2wO+XoOro2iu13h67WOAsk= 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: migration_entry_wait does not need VMA lock, therefore it can be dropped before waiting. Introduce VM_FAULT_VMA_UNLOCKED to indicate that VMA lock was dropped while in handle_mm_fault(). Note that once VMA lock is dropped, the VMA reference can't be used as there are no guarantees it was not freed. Signed-off-by: Suren Baghdasaryan --- arch/arm64/mm/fault.c | 3 ++- arch/powerpc/mm/fault.c | 3 ++- arch/s390/mm/fault.c | 3 ++- arch/x86/mm/fault.c | 3 ++- include/linux/mm_types.h | 6 +++++- mm/memory.c | 12 ++++++++++-- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 6045a5117ac1..8f59badbffb5 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -601,7 +601,8 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, goto lock_mmap; } fault = handle_mm_fault(vma, addr, mm_flags | FAULT_FLAG_VMA_LOCK, regs); - vma_end_read(vma); + if (!(fault & VM_FAULT_VMA_UNLOCKED)) + vma_end_read(vma); if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c index 531177a4ee08..b27730f07141 100644 --- a/arch/powerpc/mm/fault.c +++ b/arch/powerpc/mm/fault.c @@ -494,7 +494,8 @@ static int ___do_page_fault(struct pt_regs *regs, unsigned long address, } fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs); - vma_end_read(vma); + if (!(fault & VM_FAULT_VMA_UNLOCKED)) + vma_end_read(vma); if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c index b65144c392b0..cc923dbb0821 100644 --- a/arch/s390/mm/fault.c +++ b/arch/s390/mm/fault.c @@ -418,7 +418,8 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) goto lock_mmap; } fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs); - vma_end_read(vma); + if (!(fault & VM_FAULT_VMA_UNLOCKED)) + vma_end_read(vma); if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); goto out; diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index e4399983c50c..ef62ab2fd211 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1347,7 +1347,8 @@ void do_user_addr_fault(struct pt_regs *regs, goto lock_mmap; } fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs); - vma_end_read(vma); + if (!(fault & VM_FAULT_VMA_UNLOCKED)) + vma_end_read(vma); if (!(fault & VM_FAULT_RETRY)) { count_vm_vma_lock_event(VMA_LOCK_SUCCESS); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 79765e3dd8f3..bd6b95c82f7a 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1030,6 +1030,8 @@ typedef __bitwise unsigned int vm_fault_t; * fsync() to complete (for synchronous page faults * in DAX) * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released + * @VM_FAULT_VMA_UNLOCKED: VMA lock was released, vmf->vma should no longer + * be accessed * @VM_FAULT_HINDEX_MASK: mask HINDEX value * */ @@ -1047,6 +1049,7 @@ enum vm_fault_reason { VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000, VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000, VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000, + VM_FAULT_VMA_UNLOCKED = (__force vm_fault_t)0x008000, VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, }; @@ -1071,7 +1074,8 @@ enum vm_fault_reason { { VM_FAULT_FALLBACK, "FALLBACK" }, \ { VM_FAULT_DONE_COW, "DONE_COW" }, \ { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ - { VM_FAULT_COMPLETED, "COMPLETED" } + { VM_FAULT_COMPLETED, "COMPLETED" }, \ + { VM_FAULT_VMA_UNLOCKED, "VMA_UNLOCKED" } struct vm_special_mapping { const char *name; /* The name, e.g. "[vdso]". */ diff --git a/mm/memory.c b/mm/memory.c index 41f45819a923..c234f8085f1e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3714,8 +3714,16 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) entry = pte_to_swp_entry(vmf->orig_pte); if (unlikely(non_swap_entry(entry))) { if (is_migration_entry(entry)) { - migration_entry_wait(vma->vm_mm, vmf->pmd, - vmf->address); + /* Save mm in case VMA lock is dropped */ + struct mm_struct *mm = vma->vm_mm; + + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { + /* No need to hold VMA lock for migration */ + vma_end_read(vma); + /* WARNING: VMA can't be used after this */ + ret |= VM_FAULT_VMA_UNLOCKED; + } + migration_entry_wait(mm, vmf->pmd, vmf->address); } else if (is_device_exclusive_entry(entry)) { vmf->page = pfn_swap_entry_to_page(entry); ret = remove_device_exclusive_entry(vmf);