Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp17980651rwd; Tue, 27 Jun 2023 09:56:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7LS97+79/YYcMguibxbuXYjDFzJYU45PW5z9AwTfLxULxbKjOS0CECan7ApzXn99endDrn X-Received: by 2002:a17:902:c951:b0:1b5:219a:cbbd with SMTP id i17-20020a170902c95100b001b5219acbbdmr12248985pla.3.1687884978134; Tue, 27 Jun 2023 09:56:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687884978; cv=none; d=google.com; s=arc-20160816; b=qBTlLKCnTcHhi6CI8WHNhLpeYyU6SAQso01lQrw02TFXVdgnP1MJfjpE1G+nP4iNTa wGvvKSTH4TXIuho5OlWeF+e9CCRS5ze9rSIqUJDCfhD9UYm0GPMxPlNeAzIvOGmhpZR6 788f/vvDv1ZVEkM/sIV9VuHuBUTlSq5jMNFfS3CL48Zf/kEo+5Z3rlie1VStqI90O1hV dBIESGqDKszim9YLd4xVhuQGa43/B30DrXly9+vS0MsuBc1gtzKRg44Q3OjHqiPGwe4J ZMtme49XPvER894GT3WoUg52sRz2JyQ4JuWmYezqFTEf/bUwkd1yza0i6kdZXcg1zv5M X9CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=qymS9y/iBQJ/Nbtgv3hEJrG0PMa0rrPgMrPQ2j32eRU=; fh=AqulJRHISBOAIcIpbI+DW0fnVYrI/ky+Zsza1VlaGkM=; b=ez9DzPZmKxbFdVvuKySmvyj3c7NWJ3vJ/tuLjcdyej0qwLIzYk9Y7go6rgw+IKTDD8 obwZtDE71hcfoyQcI5Q54DECuGokZNEQTBIRnB89XyuYoD+W9i6xWrJcxjC52kg0Byac nA/qIwY5aEaoSdnu14krPrm8mTDdg/biC7vmJjBUFUvjonIwYhFqnd5Et6twar39+Fkz ixxUVwIqDLbUtfr9OpBZFnSgNHxmSP/PxXPQsXg7t/BAJ7rRhyLGE0KAUxCDS+1ME0qL v7F9KsSdeglQmgVnZt4ISAaxJt39ihCZL2dCWT8+mcb+TPhHmZmfOzwou7SiUhV4QTuR D24g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=cX6aE86F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d23-20020a170902b71700b001b596a0fa8esi6692808pls.383.2023.06.27.09.56.04; Tue, 27 Jun 2023 09:56:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=cX6aE86F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230486AbjF0QXl (ORCPT + 99 others); Tue, 27 Jun 2023 12:23:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229845AbjF0QXk (ORCPT ); Tue, 27 Jun 2023 12:23:40 -0400 Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C005E1FF0 for ; Tue, 27 Jun 2023 09:23:38 -0700 (PDT) Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-c11e2b31b95so3772641276.3 for ; Tue, 27 Jun 2023 09:23:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687883018; x=1690475018; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=qymS9y/iBQJ/Nbtgv3hEJrG0PMa0rrPgMrPQ2j32eRU=; b=cX6aE86FpHdfRFsdIIgbTpEgKD/Vu2YaitK2vLRrQg49v5sfKauLKa+/hmPC64QbWN lZYVvZRR80L6HLdIU1vhGPGJgnwsxf1Bb1wEeoKAGrBVzk9Snfun7mghg9W+u+hgR67X LKWGZLIeNqusorujboi2uhuN3vpDdOMs35gUGte5KRAInUeSoXLZgk1a1evNWDsoxNxs w9Yfer2GlW92ulsQm+8CRdz7cjowDs3WNqV7wk0jO6lpXK1T/0JYTv0w9+eN9kPgEFGd fPoLKr1qMrEYyPrEv4eyZ/0ZuMTU/sWbL0AjFqwNzLkgZd/fbZjfirILKN37PGDUIk+G NM9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687883018; x=1690475018; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qymS9y/iBQJ/Nbtgv3hEJrG0PMa0rrPgMrPQ2j32eRU=; b=geYK3AI5u6Yb9ulPw4WNAkZGCJWEg7yHxcP2+iLlWJWQsklN265rZsymhNybrnytOk dIm6NlnTvLx7HoCeqlhRidGEWMwBFLrrqnAGdqBz+giPYcxJuBtgyyGtRwqcBtiImyMx 6zjc45zhug6JTxvY85QD0urpHL3nBadT2wSnYwyb5I11uDsh1OX8VJyp6d+jsWpKLFC6 S9C/TO93PpvzeWeead7PWLUQFBdB+3NJvuFdtsS2B0jBGE2JlOMHlUO/3zPFPXYz19bI /JMMZhm2GEDY8xRT+AUky8n8Cd5W7MJ3QIJplW9n+iY5vZ/adNmPkAiGrmqoOHNRSyDd 4bJw== X-Gm-Message-State: AC+VfDyNrg+j3+z8EPcBb0UpoS1i/yni67lBM5faU4jW0SuH9xjSVUHR 8ZClb8k4DvC8UzsXfEpZIpMkMrUDgNGTzGgVtDac+A== X-Received: by 2002:a05:6902:1342:b0:c1c:f99e:ef55 with SMTP id g2-20020a056902134200b00c1cf99eef55mr6600031ybu.57.1687883017742; Tue, 27 Jun 2023 09:23:37 -0700 (PDT) MIME-Version: 1.0 References: <20230627042321.1763765-1-surenb@google.com> <20230627042321.1763765-8-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Tue, 27 Jun 2023 09:23:26 -0700 Message-ID: Subject: Re: [PATCH v3 7/8] mm: drop VMA lock before waiting for migration To: Peter Xu Cc: akpm@linux-foundation.org, 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, 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, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 27, 2023 at 8:49=E2=80=AFAM Peter Xu wrote: > > On Mon, Jun 26, 2023 at 09:23:20PM -0700, Suren Baghdasaryan wrote: > > migration_entry_wait does not need VMA lock, therefore it can be > > dropped before waiting. > > Hmm, I'm not sure.. > > Note that we're still dereferencing *vmf->pmd when waiting, while *pmd is > on the page table and IIUC only be guaranteed if the vma is still there. > If without both mmap / vma lock I don't see what makes sure the pgtable i= s > always there. E.g. IIUC a race can happen where unmap() runs right after > vma_end_read() below but before pmdp_get_lockless() (inside > migration_entry_wait()), then pmdp_get_lockless() can read some random > things if the pgtable is freed. That sounds correct. I thought ptl would keep pmd stable but there is time between vma_end_read() and spin_lock(ptl) when it can be freed from under us. I think it would work if we do vma_end_read() after spin_lock(ptl) but that requires code refactoring. I'll probably drop this optimization from the patchset for now to keep things simple and will get back to it later. > > > > > Signed-off-by: Suren Baghdasaryan > > --- > > mm/memory.c | 14 ++++++++++++-- > > 1 file changed, 12 insertions(+), 2 deletions(-) > > > > diff --git a/mm/memory.c b/mm/memory.c > > index 5caaa4c66ea2..bdf46fdc58d6 100644 > > --- a/mm/memory.c > > +++ b/mm/memory.c > > @@ -3715,8 +3715,18 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > > entry =3D 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 =3D vma->vm_mm; > > + > > + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { > > + /* > > + * No need to hold VMA lock for migration= . > > + * WARNING: vma can't be used after this! > > + */ > > + vma_end_read(vma); > > + ret |=3D VM_FAULT_COMPLETED; > > + } > > + migration_entry_wait(mm, vmf->pmd, vmf->address); > > } else if (is_device_exclusive_entry(entry)) { > > vmf->page =3D pfn_swap_entry_to_page(entry); > > ret =3D remove_device_exclusive_entry(vmf); > > -- > > 2.41.0.178.g377b9f9a00-goog > > > > -- > Peter Xu >