Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2639416rwd; Fri, 9 Jun 2023 14:35:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ48zY0mT+0hvzBVaHLOz7DwriJrUs0ZhMePlBWN8l6XvwVk1rE/cGs8WRejdDE8F6xzYa0J X-Received: by 2002:a17:90a:192:b0:24e:59d2:6f with SMTP id 18-20020a17090a019200b0024e59d2006fmr1864318pjc.11.1686346551908; Fri, 09 Jun 2023 14:35:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686346551; cv=none; d=google.com; s=arc-20160816; b=WpepisYtTvhi/JOYKkL0NrNTanej2aGZKwtIrQAaKf/sf3xDjb94EaYJOsjCKOo2uc MwSKg+pvJBWhfAyeU7ETKsRwGKp1ZnFNz8iF06KctPvWnW5qapzmIC9CtLAp1w4Kq/Ck jHHqA7aASZbfCo6Br0H46K07MYkAA5SxBHuqiMNNpgUS1WspmGNkO55sBFychEn3nGsA Q9qTELeiBe7MTphBT7bzE8pYe/g/lG5QpGwo2h/zXE0gcA/jgyNEJVkqcawwegTKXuHy 4/tCMutPuutx8hzKFgwMVkpkSsLyYNnIYJKhXcVoislsGW1KNCZ0ChLMRD5zRLBzZBr9 LgkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=BtXUpcsSOoMANhYIjxCH4S5rCaK3q7YtG7uZtBil9Bg=; b=fIBA+ylYVEDjjDY7CEkv8ImGlRufVlKj62hbnRm9hnZ6B+OmchDoYiZXueKIVx+mO5 UspQLhv13pKaF1xV3JzTBRPRQnlpNKm8Xq7pZX8jAhJKOe2KwwzV1KJ2J3QLhtl1VtX/ ylkb/B8jghUXiriIV3IDS0viR4a5DsDAdlowur7h8sOxwQe3QcJqwJ+uk5s/gsO4W7f9 3Yy+z1AZbCCiHXudKgTgShNJ7xsLxDF6SMyhKKxk8ucRcUYzgoY7XzTmimtZdCOiNJjl z+9h6t6dr5bgcp1M7E5KTd6RaVfs+7IsJBMkyVSWKk/bXOzTOF22InI2wLzjrebhSy6I SE/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=g9WK6tIE; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oc17-20020a17090b1c1100b002564f833f00si5160591pjb.40.2023.06.09.14.35.24; Fri, 09 Jun 2023 14:35:51 -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=@redhat.com header.s=mimecast20190719 header.b=g9WK6tIE; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232321AbjFIUzI (ORCPT + 99 others); Fri, 9 Jun 2023 16:55:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230000AbjFIUzH (ORCPT ); Fri, 9 Jun 2023 16:55:07 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E54130FC for ; Fri, 9 Jun 2023 13:54:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686344061; 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=BtXUpcsSOoMANhYIjxCH4S5rCaK3q7YtG7uZtBil9Bg=; b=g9WK6tIESTDPOhCkmYf1ecHTZ81YOd8Xpna8LjavHfxDUhPXWCpuj/OsldTmET7bLptm/9 raKyXxM55l1+mnJSRcdmkkFBi+DagC9vkdy/xJyTpEGqqkBFsqZjLU3Xe3gcLM+G+VhcSw izd4IXSdz+ZTEcvjmKxlzxi7MJDqPEk= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-459-cNRTKG45MeGqpy3uCbVorA-1; Fri, 09 Jun 2023 16:54:18 -0400 X-MC-Unique: cNRTKG45MeGqpy3uCbVorA-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-62615f764b4so3601086d6.0 for ; Fri, 09 Jun 2023 13:54:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686344058; x=1688936058; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=BtXUpcsSOoMANhYIjxCH4S5rCaK3q7YtG7uZtBil9Bg=; b=KMMhlnEYpJsKG7cNmJ0K2qz8T62T8bhe1/gcE3Urc1IuMrs1sjmu+A1LG3nc5IFBy4 gAid6DRKkovY3LAh4OsIklGu8O0s8PpLMQjXqAltI839WRwKkiFNFdGhwaXZGCWFh3nj FZOds6XfHpMVVASjpWJcw+UDcpwIe81rlitA0Ymbv/WrFc8kDRPLCYIPvjlkvNP4u/SA TeKUUA7oc/LJc2P/7cFVH1AwWhvlRUNu0PphTBzOvJqY/6FjMrBQWPB7wNe+mwdnbMKe vtcZTyms8+Hhm/nwr9njxjJjFi5HoEsUnu3yi+jBnkMgHFVAsAnPZEalgXT+k7/pRWry vhog== X-Gm-Message-State: AC+VfDxTOKbWiBBhHXZPGjl2O0gAa/fp1xxfjYb9z9q/anj8TkZGLixM Jz3xaRZyFBa42m5fKy6Ei+3JQxNP5n/13btgYe3qa6/zAyqC92fo5grFGJE6sXQKPBiqg58DNFh h/o0WsDK6zg1ia6SSpkdkW54j X-Received: by 2002:a05:6214:411b:b0:622:265e:3473 with SMTP id kc27-20020a056214411b00b00622265e3473mr3267163qvb.1.1686344057858; Fri, 09 Jun 2023 13:54:17 -0700 (PDT) X-Received: by 2002:a05:6214:411b:b0:622:265e:3473 with SMTP id kc27-20020a056214411b00b00622265e3473mr3267135qvb.1.1686344057488; Fri, 09 Jun 2023 13:54:17 -0700 (PDT) Received: from x1n (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id j14-20020a0cf50e000000b0062595cd1972sm1411236qvm.82.2023.06.09.13.54.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jun 2023 13:54:16 -0700 (PDT) Date: Fri, 9 Jun 2023 16:54:14 -0400 From: Peter Xu To: Suren Baghdasaryan 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 Subject: Re: [PATCH v2 5/6] mm: implement folio wait under VMA lock Message-ID: References: <20230609005158.2421285-1-surenb@google.com> <20230609005158.2421285-6-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20230609005158.2421285-6-surenb@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 Thu, Jun 08, 2023 at 05:51:57PM -0700, Suren Baghdasaryan wrote: > Follow the same pattern as mmap_lock when waiting for folio by dropping > VMA lock before the wait and retrying once folio is available. > > Signed-off-by: Suren Baghdasaryan > --- > include/linux/pagemap.h | 14 ++++++++++---- > mm/filemap.c | 43 ++++++++++++++++++++++------------------- > mm/memory.c | 13 ++++++++----- > 3 files changed, 41 insertions(+), 29 deletions(-) > > diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h > index a56308a9d1a4..6c9493314c21 100644 > --- a/include/linux/pagemap.h > +++ b/include/linux/pagemap.h > @@ -896,8 +896,8 @@ static inline bool wake_page_match(struct wait_page_queue *wait_page, > > void __folio_lock(struct folio *folio); > int __folio_lock_killable(struct folio *folio); > -bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, > - unsigned int flags); > +bool __folio_lock_or_retry(struct folio *folio, struct vm_area_struct *vma, > + unsigned int flags, bool *lock_dropped); > void unlock_page(struct page *page); > void folio_unlock(struct folio *folio); > > @@ -1002,10 +1002,16 @@ static inline int folio_lock_killable(struct folio *folio) > * __folio_lock_or_retry(). > */ > static inline bool folio_lock_or_retry(struct folio *folio, > - struct mm_struct *mm, unsigned int flags) > + struct vm_area_struct *vma, unsigned int flags, > + bool *lock_dropped) > { > might_sleep(); > - return folio_trylock(folio) || __folio_lock_or_retry(folio, mm, flags); > + if (folio_trylock(folio)) { > + *lock_dropped = false; > + return true; > + } > + > + return __folio_lock_or_retry(folio, vma, flags, lock_dropped); > } > > /* > diff --git a/mm/filemap.c b/mm/filemap.c > index 7cb0a3776a07..838955635fbc 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -1701,37 +1701,35 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) > > /* > * Return values: > - * true - folio is locked; mmap_lock is still held. > + * true - folio is locked. > * false - folio is not locked. > - * mmap_lock has been released (mmap_read_unlock(), unless flags had both > - * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in > - * which case mmap_lock is still held. > - * If flags had FAULT_FLAG_VMA_LOCK set, meaning the operation is performed > - * with VMA lock only, the VMA lock is still held. > + * > + * lock_dropped indicates whether mmap_lock/VMA lock got dropped. > + * mmap_lock/VMA lock is dropped when function fails to lock the folio, > + * unless flags had both FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT > + * set, in which case mmap_lock/VMA lock is still held. This seems to be a separate change to have "lock_dropped", would it worth a separate patch for it if needed? I do agree it's confusing and it might be the reason of this change, but I think it may or may not help much.. as long as VM_FAULT_RETRY semantics kept unchanged iiuc (it doesn't always imply mmap lock released, only if !NOWAIT, which can be confusing too). Especially that doesn't seem like a must for the vma change. IIUC to support vma lock here we can simply keep everything as before, but only release proper lock based on the fault flag should work. But maybe I just missed something, so that relies on the answer to previous patch... > * > * If neither ALLOW_RETRY nor KILLABLE are set, will always return true > - * with the folio locked and the mmap_lock unperturbed. > + * with the folio locked and the mmap_lock/VMA lock unperturbed. > */ > -bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, > - unsigned int flags) > +bool __folio_lock_or_retry(struct folio *folio, struct vm_area_struct *vma, > + unsigned int flags, bool *lock_dropped) > { > - /* Can't do this if not holding mmap_lock */ > - if (flags & FAULT_FLAG_VMA_LOCK) > - return false; > - > if (fault_flag_allow_retry_first(flags)) { > - /* > - * CAUTION! In this case, mmap_lock is not released > - * even though return 0. > - */ > - if (flags & FAULT_FLAG_RETRY_NOWAIT) > + if (flags & FAULT_FLAG_RETRY_NOWAIT) { > + *lock_dropped = false; > return false; > + } > > - mmap_read_unlock(mm); > + if (flags & FAULT_FLAG_VMA_LOCK) > + vma_end_read(vma); > + else > + mmap_read_unlock(vma->vm_mm); > if (flags & FAULT_FLAG_KILLABLE) > folio_wait_locked_killable(folio); > else > folio_wait_locked(folio); > + *lock_dropped = true; > return false; > } > if (flags & FAULT_FLAG_KILLABLE) { > @@ -1739,13 +1737,18 @@ bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, > > ret = __folio_lock_killable(folio); > if (ret) { > - mmap_read_unlock(mm); > + if (flags & FAULT_FLAG_VMA_LOCK) > + vma_end_read(vma); > + else > + mmap_read_unlock(vma->vm_mm); > + *lock_dropped = true; > return false; > } > } else { > __folio_lock(folio); > } > > + *lock_dropped = false; > return true; > } > > diff --git a/mm/memory.c b/mm/memory.c > index c234f8085f1e..acb09a3aad53 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -3568,6 +3568,7 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) > struct folio *folio = page_folio(vmf->page); > struct vm_area_struct *vma = vmf->vma; > struct mmu_notifier_range range; > + bool lock_dropped; > > /* > * We need a reference to lock the folio because we don't hold > @@ -3580,8 +3581,10 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) > if (!folio_try_get(folio)) > return 0; > > - if (!folio_lock_or_retry(folio, vma->vm_mm, vmf->flags)) { > + if (!folio_lock_or_retry(folio, vma, vmf->flags, &lock_dropped)) { > folio_put(folio); > + if (lock_dropped && vmf->flags & FAULT_FLAG_VMA_LOCK) > + return VM_FAULT_VMA_UNLOCKED | VM_FAULT_RETRY; > return VM_FAULT_RETRY; > } > mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, > @@ -3704,7 +3707,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > bool exclusive = false; > swp_entry_t entry; > pte_t pte; > - int locked; > + bool lock_dropped; > vm_fault_t ret = 0; > void *shadow = NULL; > > @@ -3837,9 +3840,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > goto out_release; > } > > - locked = folio_lock_or_retry(folio, vma->vm_mm, vmf->flags); > - > - if (!locked) { > + if (!folio_lock_or_retry(folio, vma, vmf->flags, &lock_dropped)) { > + if (lock_dropped && vmf->flags & FAULT_FLAG_VMA_LOCK) > + ret |= VM_FAULT_VMA_UNLOCKED; > ret |= VM_FAULT_RETRY; > goto out_release; > } > -- > 2.41.0.162.gfafddb0af9-goog > -- Peter Xu