From patchwork Mon May 1 17:50:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13227758 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 F40D8C77B73 for ; Mon, 1 May 2023 17:50:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B6A9900004; Mon, 1 May 2023 13:50:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 565AF900002; Mon, 1 May 2023 13:50:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42DED900004; Mon, 1 May 2023 13:50:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 36671900002 for ; Mon, 1 May 2023 13:50:33 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E6083C07A4 for ; Mon, 1 May 2023 17:50:32 +0000 (UTC) X-FDA: 80742425904.04.7EBDEFD Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf01.hostedemail.com (Postfix) with ESMTP id 1581240024 for ; Mon, 1 May 2023 17:50:30 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=dZCcwJnc; spf=pass (imf01.hostedemail.com: domain of 35vtPZAYKCEc130nwkpxxpun.lxvurw36-vvt4jlt.x0p@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=35vtPZAYKCEc130nwkpxxpun.lxvurw36-vvt4jlt.x0p@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=1682963431; 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=Vjmy9npoFS1ltTIQSU6SYKXLbdap9UrmwfWM+Y2ixPk=; b=zP/aJjT6ZmoGs7FHJFtt6MsWmBLuC6dXqnr/lTkFXHnyMMEjfPdA2KNwS6Xt2m4CwRXsJN 3jxsvolJEWfXQI8UOgZoJlmwOYmHwfM9GLHggbb5Zi41HWhlojVqZlwVzEK/duROdXr9qX E9Mhz/mDLfStSIAHZR9716RP7F30t8M= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=dZCcwJnc; spf=pass (imf01.hostedemail.com: domain of 35vtPZAYKCEc130nwkpxxpun.lxvurw36-vvt4jlt.x0p@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=35vtPZAYKCEc130nwkpxxpun.lxvurw36-vvt4jlt.x0p@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682963431; a=rsa-sha256; cv=none; b=fuOwIcuL1FEUGMdxSB/LIyDmZIwQMeKe1CUZ3ieopjerM6Ngvfayd9h68BBVSl2LiZ7yh4 Umkl5cTeko4CmQ6no91fMAJVc7lahp5a+mRDQYy8IQuTmZugewlaH3OhuCuChYorQU9MWB RpmrdpLjANSj31WsFAuh5CdC6T8qxMs= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-54c2999fdc7so54682827b3.2 for ; Mon, 01 May 2023 10:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682963430; x=1685555430; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Vjmy9npoFS1ltTIQSU6SYKXLbdap9UrmwfWM+Y2ixPk=; b=dZCcwJncaHug3B8HkBJA8I4rogyge1EVTo4b3P8k7VcTSJVpEkICoBGsnhmO7W+EaQ BIxtz97RUXIfYWYlU1CJsKlwqGVGZ7x+vS+3P/rfb8ANuZUjR7SbifV/F6aSBN5z7ZRt /AMQ4KNfWlhXtFlPUDhe7iDHC11AWgJzvh0HYI/QSVDj28sAvGrMcNoB1jrqM2PiCRxV MKWY+ho9Y/9MhG5dJ5YP+l84eWwLxXehtDV3C+sB45yhtiKFovPsBc0ojtZnWpwnmUO1 OgbB8QClj+k71UJXHVn/U9WcLkhMORsNKUzA9GKVhmcWDmrnB5+OYkqWUJRZdkLT90UK DpmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682963430; x=1685555430; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Vjmy9npoFS1ltTIQSU6SYKXLbdap9UrmwfWM+Y2ixPk=; b=bzYnilJ/hRwdemf+rS2zppkgcmriEEo664oFMCrLqcalSZXog1Bx0xK8Sb+c3p9Y/Z coK/zQXb7f3Vci/mo17XPwgv2+7hQoACDVg0whDQ5NLxFpCytYYehCqv2OXuQ3XzSNQ+ dmbBx7Lf1pFgtt1xteny7xdgzrgNgKGzN7aLdhuvjeGPXqVQ1UrcS74g89gDbYYCnsKy G1od/RQTghR9+E5ooMdOl0yGslDGN2sEohJjCKHhsYL0EhKb4EgfOlLLt6SPNn6aYo/I klEphPDAp1GkdOb1lDxI8dyaWV2MACUjen8v5Lev5waPM+H0m/pdTBvgDlCrixl7zU8H yWug== X-Gm-Message-State: AC+VfDyc8KUzJEuktfOW3tvVYRfSUTjsjmNSVCsNRz7/nrFkmOcGThOO q83LXO6iErEjL81uwN82B/8euhu5WwQ= X-Google-Smtp-Source: ACHHUZ64NZ3/9tsMewuAy2AdTfarKe08wL+z4DEH/85FRjs3ZEIBqL9XqLFWxrB85BCLpG0FPVBpKygFEUY= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6d24:3efd:facc:7ac4]) (user=surenb job=sendgmr) by 2002:a81:9f09:0:b0:559:e830:60f1 with SMTP id s9-20020a819f09000000b00559e83060f1mr4687408ywn.8.1682963430249; Mon, 01 May 2023 10:50:30 -0700 (PDT) Date: Mon, 1 May 2023 10:50:23 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230501175025.36233-1-surenb@google.com> Subject: [PATCH 1/3] mm: handle swap page faults under VMA lock if page is uncontended 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, surenb@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com X-Stat-Signature: aboti4d8h7fnpuyyru6qjmb1s9a7a6km X-Rspam-User: X-Rspamd-Queue-Id: 1581240024 X-Rspamd-Server: rspam06 X-HE-Tag: 1682963430-990805 X-HE-Meta: U2FsdGVkX19mUUkv1A+DaSg2hCrXzkiA9TAqCXDKjj1v40umuRQL2kGDx1HdaFhBAzbb/dn+COXWRnNx5H0Ds9KPP5sMo0lPuWP21OrwJkiRzPxGb3+uaPCOVQDEhc76Ywpjf7Z7B94h5bbJPzamuBIX+2s0Sggh3w/WTgjAe97qQ3ZhSdinCnEkYfAe8p6XCs0tWP36baqX/W8aSX2jg/QyXSUVW11ariGrSaUcsPe8ud+cEOEiCp6TgHGFc1ZEraYCMzMrx4hkp2RrWp+WuJsOcep4xbfsGACgdXYaP0KX65/B9zw9Vr690VCwdl0VuN65uzZuRtYJCI1Nn/TxBFJMLYe3ii+55mwx+zH5n8ZMt4YbmE/f/ZU/IWphN4D1WLU10yEUeKO8O8RofB/JcjzcsOEvyo5rXZXDAH97sgSjsuyvYa8vFto2y4uBrLSaMTmyuka0jzwRCDsYnrosd6hulLGvxe4QoEpPjM8xP3M6kQZYsMw4F29Udh6Oa/euIbVFClcpVvOdWSA1KuzLMjlMp+DZsN0np61EXA/YGSUdQ7Pq5G1ZZMs619JvVcvWBicfl7ilCsMQY7mh4VMWxJS1HlX5Nh007C4EzKi/Lte/CnrBZdkPHGAqyfV18WYkG5lZIOchldxgRA0dXcxunxah2ROEwh+4II1lPjaJwSlHxtbxdSn3NSbwwfR5GAlY+/wTtaN7AwJBqFLosh63Ex/ca6/RA6dwp940rDB9+8DhJGurdeVpPVrXFI3dp8OIqvibrdxE6v0hl6sA3RKeyW9NfVRL0ASep6BQ6NMB+nwXErluFKY4Q7XShbMK/2Slx+jikdw8k62iTLQ3i4Ap/hizjS+Ily4DCFdmv6RGkUyF/Ybymv4+oRUpggusWxEg1QeBuFWOrZX8QGrknDkv/eTrBij/d97cEnugu52T6QmmwJBh1344Iiw5QkpNGpEnp+TEew9dfFMUDpy+xzL 9juze8ON ITOw2fL4s3KFhFbKUHgzU6bXMlfUiMdv3gd57Hcow/i5xmUmpxZQCkZcKqmTUhWkQ2P3NfdvyJZ/QhtulCJMvu5aogvmfHhLzXlSA+RJkHrXRwgG3TWzdU+YqXqQzZN6jxh9Qh0U2Htv/Q4IEueKNR+rizJeZ0J+V4PALS42IlBhzR9f1CDImIkRcFDsJeAmWOQw8edPpm1ubVglK6T8kchf4ByMVEnFkX2l4l3u9L9MJXGMYKogjQVJ4tyrMf9kGs32g31PJh6inWqvBF5MWV+EJWxurCBlzYhodrfPcFIWBpbGvByE/CSu9gZZlxT7YVEfYA/1IOGEZr/fjGuXYzg3pvZKVmyk58/dhNFqb1+IybvWQol43UtZf8NAtDWbJ+R80KbHVaKKH+k0+MK9Kyh8q7rxbbtTutwlsVFOqTL4hMnE0NyBR9/rumcA7J//+yVHHXXg8u02NREz0dWoTCDSu0pehooptIoKW1hZZ+xB/oZCVPCRiRVkAomkWb1YeMKUqhXmahBD7LI296mw7W+mqx+Zqrobzntb7swuZizqlSDZWsQ+kAEajGlTkpzNVxX5lEH3jN4kJ5afhXwbAyUCq6m/tAOvY325hesDso5ZqHqU= 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: When page fault is handled under VMA lock protection, all swap page faults are retried with mmap_lock because folio_lock_or_retry implementation has to drop and reacquire mmap_lock if folio could not be immediately locked. Instead of retrying all swapped page faults, retry only when folio locking fails. Drivers implementing ops->migrate_to_ram might still rely on mmap_lock, therefore fall back to mmap_lock in this case. Signed-off-by: Suren Baghdasaryan --- mm/filemap.c | 6 ++++++ mm/memory.c | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index a34abfe8c654..84f39114d4de 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1706,6 +1706,8 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) * 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. * * If neither ALLOW_RETRY nor KILLABLE are set, will always return true * with the folio locked and the mmap_lock unperturbed. @@ -1713,6 +1715,10 @@ static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait) bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm, unsigned int flags) { + /* 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 diff --git a/mm/memory.c b/mm/memory.c index f69fbc251198..41f45819a923 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3711,11 +3711,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (!pte_unmap_same(vmf)) goto out; - if (vmf->flags & FAULT_FLAG_VMA_LOCK) { - ret = VM_FAULT_RETRY; - goto out; - } - entry = pte_to_swp_entry(vmf->orig_pte); if (unlikely(non_swap_entry(entry))) { if (is_migration_entry(entry)) { @@ -3725,6 +3720,15 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) vmf->page = pfn_swap_entry_to_page(entry); ret = remove_device_exclusive_entry(vmf); } else if (is_device_private_entry(entry)) { + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { + /* + * migrate_to_ram is not yet ready to operate + * under VMA lock. + */ + ret |= VM_FAULT_RETRY; + goto out; + } + vmf->page = pfn_swap_entry_to_page(entry); vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl); From patchwork Mon May 1 17:50:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13227759 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 26ACBC7EE26 for ; Mon, 1 May 2023 17:50:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5AB6900002; Mon, 1 May 2023 13:50:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE400280001; Mon, 1 May 2023 13:50:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98471900005; Mon, 1 May 2023 13:50:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 83E84900002 for ; Mon, 1 May 2023 13:50:35 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 52320A0818 for ; Mon, 1 May 2023 17:50:35 +0000 (UTC) X-FDA: 80742426030.15.036321C Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf21.hostedemail.com (Postfix) with ESMTP id 76F411C001C for ; Mon, 1 May 2023 17:50:33 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=5HsHeOLj; spf=pass (imf21.hostedemail.com: domain of 36PtPZAYKCEk352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=36PtPZAYKCEk352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682963433; a=rsa-sha256; cv=none; b=OGNfY5HLx4W2DnNqegLmRdMCvin72S9ocoKCGZPJoOT121N7V4MXbBLLhlt6LXTnb675ti +Llc64LcNrfNu5kPcTlI1u1pKyRfF83KX4ed7VjUhnofo56nyDeozK7VG8uFubZEa8pHH6 XAr6UnOp6qLwWxOwfSx1TfXGt3mxcQU= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=5HsHeOLj; spf=pass (imf21.hostedemail.com: domain of 36PtPZAYKCEk352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=36PtPZAYKCEk352pymrzzrwp.nzxwty58-xxv6lnv.z2r@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=1682963433; 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=Vw7vchvPa5zb0i4uc6VlbyjDM+/1CtZDGIGJ1EuR9xw=; b=kJ48M2bEQdMEkpIP/ZEx4mHZWk2gPFMB49GPa097rpPBCkS/HH5G4tlQuiVRmPF6icGkYo hH4Zy3DiCFUw0sLef03eNlJHqxXFnUZfRQYf60CFmAO6EAUIP5hNh7RSEcxR54yrYUZIhL GSTIFzbtMXITXT3SuA03/oVzfb/OU7I= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-b9a7df507c5so5481539276.1 for ; Mon, 01 May 2023 10:50:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682963432; x=1685555432; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vw7vchvPa5zb0i4uc6VlbyjDM+/1CtZDGIGJ1EuR9xw=; b=5HsHeOLjpvUCguo8Lk5686FqyMyO/K1KiWJihXWf5ybeHIs84XPoHkts2mjy/kUs1l OVyccOydInIzF83t7slw6eryjNnVfxRNEDu94e6rnWhRVLxxc+mL+BA49regOAf5kslZ LLCq5lh1PW8I8Z10gv2qYm/fP8iSSz6PfSup+xBmEYK32jLAyJ72rp0lAb9Ja6S+ZNFp vpHyDid2/Jb8uaq1IaiD5weLPcePb/KM0hw6ZU+KPOncjNStmsgmO9gjEswNlynFjsvG Mb2tpXrasG6CZ2vTifbGdq0/Y2mX7Cc6JG0UUlhWyX6kwnaEJR4lOKi95Lq/0G+NMlK8 jz4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682963432; x=1685555432; 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=Vw7vchvPa5zb0i4uc6VlbyjDM+/1CtZDGIGJ1EuR9xw=; b=F36d7lFBdYy6GBVrjlhZY+2FfuQEqfEM8paSwd7e4yUpJTWq0mHxKMjSAEQoOekk5H j1JxoumNzHJxWxk/cqT/n5TjIdmFvONk0C/FvB5dikSgzstPfXn5mKK0xBNPAlyvCPWQ +UiZJMM/K4QjYPpAApcbaHg6SAalMsyihKht49j5bmiguqf1gILT3duqkKvEOYyVPnYt 7X0/5Cg3++eBy2GiLBFGae++3Rdpzpa8Y3UHMgkmVM1uPNKdtdx9cdBeDQdS297IxClz IFbrZlc6Y8+Gy84GExUbGF+h3Ly3DbRoZKc2XKbcKtWQyBLsjcU2bGqGN32VBaBjsHU2 fezg== X-Gm-Message-State: AC+VfDyI2YboSBNLzRm2uWrm7vc8hrmkOmpwB+Qx+VWhx9ucBIxaGrnZ SZyE5BjubvZCf9r8oNub3Z9D63+2Ceg= X-Google-Smtp-Source: ACHHUZ52U0jmkTB2neK+iG10+QPSsqzCMma8yYtYnx1BEqBYvpEc0F6TFjBKuNu+xchIpilXB3tZ+Ex1C4w= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6d24:3efd:facc:7ac4]) (user=surenb job=sendgmr) by 2002:a25:4884:0:b0:b9a:6a68:7c25 with SMTP id v126-20020a254884000000b00b9a6a687c25mr8582160yba.5.1682963432608; Mon, 01 May 2023 10:50:32 -0700 (PDT) Date: Mon, 1 May 2023 10:50:24 -0700 In-Reply-To: <20230501175025.36233-1-surenb@google.com> Mime-Version: 1.0 References: <20230501175025.36233-1-surenb@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230501175025.36233-2-surenb@google.com> Subject: [PATCH 2/3] 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, surenb@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com X-Rspam-User: X-Rspamd-Queue-Id: 76F411C001C X-Rspamd-Server: rspam01 X-Stat-Signature: 56bht9ypw5hndjc8szjmk81ekn86w1fg X-HE-Tag: 1682963433-403283 X-HE-Meta: U2FsdGVkX19lf5HJAG+S1r9LSspRksNMf+tz90LySUtLRBoEUhsliKkDisC8nk/r/i/rDaviOU4t2r9zxzQK3maipe6DtolAWg+aXVkcTL+he0qh4/IMj2hNGdt8U9PflT1yrGQayxnUtG9q79gE7RPuVH67pAuUVVk5UdrepJ2MJmHrVkHOP7CJ2yFlGb6oxCi2+PI/BnCGWHT5yFLIaSEK45XasNS8H1inSAdbP9Usoh+XobJ4WaxIovL7M9GoqKPrRVcXguGiQLdRpAC0k/iUxD83N2df7RniBA8m7CwAeop7yUE0qc7IE4olJlnSRqxiUJ8kgr+My8zHX0dnaDsmVK1etFkxQT4374Sb/rMYGDkcCYdXsaKmnMdxWZwCGZEvYPSRMeUlouOpG5at3cBUa4pwyMsxdv0CLmCMXo0cqw1UkG3BPkPxRwdtsKDnoJWR58gBJfquvX6pBODnDbxoVoBxGM9IzPoKDtZOEQzjgQLwPqZ7mwa68Hu8vJocVCiO+gO7MTiuzDVa2AtmIsAV1m5k5ofNabelzX0NygXlZZOVo+b824ZAVJ/LOLtD1csGhMz5u4lmxV1ehw284hGeE20a9aZYzlnW/5ZcpeVz8k7PhGPPgoj4hqkwnu0wnZNgNKbfRQnhIogVE5K8DhBat5o+mvojyYIF1lNLsvhGugT49VDccpW3auHjg0exx9rRI7PNFFh9UTYZkB49oCsXbdxVN5mAaUfd5LUoeViQ9jzRgrGjPaoAEd6l/hKu1uag39otd9Lx/O7Rd7OTotQbifc7nzhazg6kE4eNFTTmSRlgQ8C/b5z0FLrurRvGAiaTMp4ZI63sR5hcOSz1fwxxgE8s5BSzT/XlWIBvbxrA/DY+Ce9aq6Rsl6GyailoaShN7VpXy5vWujQXA9KHn46vP64lPuIAUV2Y7M875CpmiowqUfVVAVQWbVhLCkp3w3vSQVY2d861GPI5AA1 qVKEtzpK PpTIUKWnXL9O1kOyXaY+ksd2s97tzrTWJOcGOgmIuWpUn7mtm47aQ4V67wikuJVVzAOk03sdFOfrTd+abB1eRPl0AqFbfKfOKAA6N+fZc2VXLROysTMa3W5SFygOwWbsurkdF4awHVMk0OmCzUxiMXQ0dclp4xcqGuzLGWRxS2PERlEyOFmNeZAsExJ81lYrJL+HQHlLymNTxnUBYqDCeFPNAPzvSXkLQ/wJdXTTi3FyBFoo40RfQ1QKinG95u2fUp0PH3pjC046fh5J4snF0vyt8ps1kKIGgnXyiRXOwdk/nGQmTJL61UGXU5GihHK1O8BXx3OemCobgTYI4rMP74BjRJxxPlPJoU3tRqG+sU3ypb6xQk4jnTDEyRYi3zyxH6JY9f7FuYQ/T3ugDyDRgUEXErXdeDClVuLHxPzaT4GibkLu0SrEGExFjTIBVXbSgs8T5aXjW+IujJq3Q/nB7A9scyN3dA+dtDQDMLJ50SS5/cg/PnJHafVOgRVyfOJTF0ToGLzR0R3j+exPiafld5uyEe+4GjT+2hDS1R2lKLlbpDR1a4OB9Yvv4EKNcEMZ/ywqvBKcX/BvkpILNnbwDIU8el2BdAkJ3KXe0sJcS39n4nPI= 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 9e0db5c387e3..8fa281f49d61 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -602,7 +602,8 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, } fault = handle_mm_fault(vma, addr & PAGE_MASK, 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 306a3d1a0fa6..b3b57c6da0e1 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1030,6 +1030,7 @@ 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 * @VM_FAULT_HINDEX_MASK: mask HINDEX value * */ @@ -1047,6 +1048,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, }; @@ -1070,7 +1072,9 @@ enum vm_fault_reason { { VM_FAULT_RETRY, "RETRY" }, \ { VM_FAULT_FALLBACK, "FALLBACK" }, \ { VM_FAULT_DONE_COW, "DONE_COW" }, \ - { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" } + { VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ + { 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..8222acf74fd3 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); + /* CAUTION! 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); From patchwork Mon May 1 17:50:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13227760 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 A3423C77B73 for ; Mon, 1 May 2023 17:50:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45718280002; Mon, 1 May 2023 13:50:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4061C280001; Mon, 1 May 2023 13:50:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A897280002; Mon, 1 May 2023 13:50:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1E26F280001 for ; Mon, 1 May 2023 13:50:38 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D81091C72AE for ; Mon, 1 May 2023 17:50:37 +0000 (UTC) X-FDA: 80742426114.24.32F90B9 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf21.hostedemail.com (Postfix) with ESMTP id 0B3501C002B for ; Mon, 1 May 2023 17:50:35 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Kbw2vWgf; spf=pass (imf21.hostedemail.com: domain of 36vtPZAYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=36vtPZAYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@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=1682963436; 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=oNrjh4NDZfCOCA+XspYc7I9kB51AWPXlbM0N3pX4I38=; b=f52NWu02F6skVaRyOjJY6XeW0vlG4bfapoTS0f59S24LbHmc07qK+P1csDWhmmwbFX5qOs /m9BNDoOxabOCLMzmVDGhXbJJgsFv3yovFt2pJniXz2gazWGmlob2Pf15OjRES4BuPS35Z EjRvT67V1WCC69y9n/HXyNbMeAoniBY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Kbw2vWgf; spf=pass (imf21.hostedemail.com: domain of 36vtPZAYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=36vtPZAYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682963436; a=rsa-sha256; cv=none; b=bJL9ssVSgLAFpmAvfP/dcgGY9VJlLXELwUUEKMXRlnsSmnk90gb/TeirBadaT0z9GxG5gI n14HdH8TejrUk/hallF2btBN55YxiymD+60vrzxZT0UZjWKNRa1zFoJ1EP3Zx8qmdu1wSA 4tuTpc8IbVjICeS0CS+NwTLAvJKaCzY= Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-64115ef7234so23606162b3a.1 for ; Mon, 01 May 2023 10:50:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682963435; x=1685555435; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=oNrjh4NDZfCOCA+XspYc7I9kB51AWPXlbM0N3pX4I38=; b=Kbw2vWgfaNr9wwaOaALtstqdWrM8vexXN61YFf/VngXG0KW+KOPVkcePNZuJ8BunHG 4fpSDYYnv+oGNFNonvZma1sxd7lY+pamluLU1rV2cF//+7bWNniJgJBWEyyKJ8Vh7+6F 9iUtSaPHWAZ9eK6TXIhhIoxbL87AdOepwayQg5RNHhsGMUhaXjd/xW95LMnIJV5qHKsT ZO0KE701s1k305YVP18qbfaonF6MXOH89tuj3WV3vRzavu+9Gwau1Sh80hFUZAu7ob7d posHsah7zh+ARWr8FuaWg+eUdTnxNa8x9N/3HBaa0SrwutIbbkwlqTQY4bXjTqb8TVCJ gauQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682963435; x=1685555435; 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=oNrjh4NDZfCOCA+XspYc7I9kB51AWPXlbM0N3pX4I38=; b=h+R4IxtHIV+b/e051hE6dfTXJnu7U8yCszBBCu7S3Le07QPM9wWJgZa6DlcqKqdMBn NeQqvLfTgj8ICRAY4UvVAIKLJO4tBWWu7LWgOVuXqI+I/Ic9ErRgYtsVnr10zD/lZruU g/2TFJqYWbqLqUFrMubAYx1FfubF6DwltgM2DtlZSCKWkHf8ZDCO14Xizf5G/L5vipT+ OGLcKHj2il+XfMIvheHUG6024vEB6PJzABLswYKriUvt4bEpzv9ZUhOKbWNmD9ZOUW7M WzNmU6TJrehUVHE6145HuBpKU4sXNFrs9GE6VDHQFp3XuyrqycThgBKNSB+GmCw1Qv+N AzSQ== X-Gm-Message-State: AC+VfDziUswvnE6z28r5E3W79c3ZhWumOthIAPrXgZ3XYrMg55fjHaEk OOrWYSg6TzuNLoXfuVgOT1TySK1E4v0= X-Google-Smtp-Source: ACHHUZ6EimADaT391MpwBUkL6hz/B/GfVS5bvY+TKbOxuncQS8LbI8CgIxJ7ch0/4tBeArriD6dOerX5ZI8= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6d24:3efd:facc:7ac4]) (user=surenb job=sendgmr) by 2002:a17:90a:72cc:b0:24d:e14a:6361 with SMTP id l12-20020a17090a72cc00b0024de14a6361mr1794469pjk.3.1682963434788; Mon, 01 May 2023 10:50:34 -0700 (PDT) Date: Mon, 1 May 2023 10:50:25 -0700 In-Reply-To: <20230501175025.36233-1-surenb@google.com> Mime-Version: 1.0 References: <20230501175025.36233-1-surenb@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230501175025.36233-3-surenb@google.com> Subject: [PATCH 3/3] mm: implement folio wait under VMA lock 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, surenb@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0B3501C002B X-Rspam-User: X-Stat-Signature: zre78niwd8comctsntn94mrjnkpbo358 X-HE-Tag: 1682963435-941228 X-HE-Meta: U2FsdGVkX1/5AnWrh3uJEV+3bJiZ2uy2AOZNPZj6ww/Hg/wJAeYHc/CnVd9jL8w48RzJglB+TFsQD8Y+mUPRR0qKqZj8IQIFilpekzvV2YrrWAkY9qPMklQTJsATDMgGdPqjz6u5Lr0kaaLjL7c9P3iYxbrNjXWV7eFTdcrpdT7pNuzriArADpXLRoG/uTSSfgATf66ogZ182yCal4eb3km9KhG0apg1jQiNuXGFrkS94pib6xkGXkCgypLTEvltqVTJ2sCWzOOIP7FcXZ0s2o9YTeD/cAOWPy5k2IZet74GS0r9VnF3KbD4S1aWwQOzL0dGOf/6vEdhmtNFtF+tUszx4QsZOzCOiFUA2ZytE0oqxL95kMXx6apYaXkmvJm/Z5mgcfH6B/6U9yjM/5tuhli6QwoaAn4dInGqaC3KniRTrcThFkOBef5dOy+P8CxBlzL9cnY4Kx2kmycfQEbOGCa9r7aPb1ON9NwnXTzHT/rUeXLWApiTljNZh5/kqthyFgYt9O5nWeCbD3N6wZQsW5+F0nEjmgqFP9jVVR2TpN6Dp0KbCISuKl4ahoEtxXka15bFurSuLdLFBeAxF4Jxpj1NXraOdZgInQX53Lwz6tjeOx6sQdx9MbvV9qf7cua4Z2owk91maIDWK5UoAg3POrpEnvb9W51cJVCleg9JHREexwueV/zt8f5QH0LCngG0XnrKuRCzZC7QPDFa1VVCjwUXljXNNLwPSNeVbmsCJOcj49Spb6R3IALfRpMj36TAJoRIUgcZmGZiOkuqARQRm34KZ4or8Hl24CrwhsCbnFamR7YFWYbXYQfdM4ss/WaPCtJpvFCOT+bZT9d7wSedqVo4c2Pa0MyfNyKkmhIhJCyosXJtFRbq9m+XzGb9TjUswjlgLAJy8kHp3fyW/m59Dhr8u0pL2sZsD56If45B5CNyaL6UK4IX4r+oBQQXm3pqEG+ivmRcUM/elvPzX4Y rvVPTzVJ Hwap+s4mV9h6eEU0n/lT8RWpwn8XvdLdJuW8MsBUswSx4lLt1MYH9bVVTDbhVAem+xMPAF88CyL500w5OUBLaHa4S3tTxbOteUH+MlEFiKOlCL+hHtGtXgU29BQF1z9WH9WPbY9Lusjfk4+d6mnjSQScbWV9MgO1eVLsQbnkrjy54xRdZD6TV8iVIZdXO13Qlg1x9X63iqmgaKc/oewqEWH2nePv66ZperSFkdRmllr90T9Qal0LcP5zXXm982UTlm5Ng2+vvQsCI3k9yx+jhr9cwqXaAXD2+ICrxmiJvjrT63pN940554r9l++jWYl5hphhrC7Aj0QeMFsE7yMLQ05OzJbucV4Zq78GxLxgIw4+cTAPB7fdxKbEKzDJtPJ1XpfwUpn22WLDD3jmGkIJIAAwvPXj0KIXojooYBeiM9M+pGxLhJFEk5Xf59/eba3pfaYu6hRhXPycYNpC3TjsWwHesiVmitA/tt1pqom4Mn+byW2TvnBSrfkRUKhs7Js1RRtZC2D1vfo1/ktO8Kr/M7Fs1PBqRa7EbzxL1uXbLFu5suACW0hymmTRkwyaoRB/DKJHhFBkNQpVdu60g30dix2KMp1BR/6F/TiZkwwnn1sw6ahQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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, 42 insertions(+), 28 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 84f39114d4de..9c0fa8578b2f 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. * * 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 8222acf74fd3..e1cd39f00756 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,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) bool exclusive = false; swp_entry_t entry; pte_t pte; - int locked; + bool locked; + bool lock_dropped; vm_fault_t ret = 0; void *shadow = NULL; @@ -3837,9 +3841,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out_release; } - locked = folio_lock_or_retry(folio, vma->vm_mm, vmf->flags); - + locked = folio_lock_or_retry(folio, vma, vmf->flags, &lock_dropped); if (!locked) { + if (lock_dropped && vmf->flags & FAULT_FLAG_VMA_LOCK) + ret |= VM_FAULT_VMA_UNLOCKED; ret |= VM_FAULT_RETRY; goto out_release; }