From patchwork Wed Jul 14 22:20:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12377941 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FEF8C12002 for ; Wed, 14 Jul 2021 22:21:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E3BB9613CA for ; Wed, 14 Jul 2021 22:21:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E3BB9613CA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3FF896B00A9; Wed, 14 Jul 2021 18:21:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 33CE96B00AB; Wed, 14 Jul 2021 18:21:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C93D6B00AC; Wed, 14 Jul 2021 18:21:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0091.hostedemail.com [216.40.44.91]) by kanga.kvack.org (Postfix) with ESMTP id D75FE6B00A9 for ; Wed, 14 Jul 2021 18:21:29 -0400 (EDT) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id CF34D160AF for ; Wed, 14 Jul 2021 22:21:28 +0000 (UTC) X-FDA: 78362615856.31.8471523 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf16.hostedemail.com (Postfix) with ESMTP id 70B9FF00009D for ; Wed, 14 Jul 2021 22:21:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626301288; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aMR1rwc2VptPVcJogLZEh7U1JPTpYleS0uDSBMQzgKI=; b=fdYbPUin0TQJTOiyUFqdMvaTxohc8NEf6kidv5SA1AALsr5WHgScj7Nom45zCwJhKmoHBt vVDiGHDuJ1v6pFZj3tK4/at6s/5KjbejbMCXbVOF1BaESjtfc99dnU+f8FoMF9I7FRoSwj dcBICwGDARhPwY9dNsXGZ41BJS9n318= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-377-EPFsQOBdPEKmzg_K25gYNA-1; Wed, 14 Jul 2021 18:21:26 -0400 X-MC-Unique: EPFsQOBdPEKmzg_K25gYNA-1 Received: by mail-qt1-f200.google.com with SMTP id w3-20020ac80ec30000b029024e8c2383c1so2834013qti.5 for ; Wed, 14 Jul 2021 15:21:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aMR1rwc2VptPVcJogLZEh7U1JPTpYleS0uDSBMQzgKI=; b=ERSaF5jOpj8bbu5OGunrstnc4Gke4SZZgd3Lssywx9LopeDNMf5laobF2lHhzWFgVT phk/YJnydrIoTwhthH/DjJXzZS2Vv8+I5nQDssjPvSVamrafgxDpt6cWuD5kSNC+e9RM Rw1+4jdcBZHTUVVDqc1YJk/6kIIfm2sLntOzoheWnqTZo4SSn1a1vIJK915vkW02Z0Be Kt41QO07HlaoHpzmvtZELEGTL9QhNr3L3ipv6k9sYFVHgGrQETf8js0D27FuR/uQZLXj RqizD4KLpcfN9pQ+CnAiGO5026Xdn2DA04W0i82HP3NN7WN3QOzKF6dx9DH/rFpuxuux RYqA== X-Gm-Message-State: AOAM531KhCb//z6pj7Rgju7WGBfHW99sqX9+aQJihTEZ1M+C8hP4IyGM 5sgRUHhPxWuQLMMYtWkpPdqZu96/k6U3f2yUW0e/1Wps/Iz47AIwLCxkyNU548oZ42aQAz579gZ q4bwKdYqV+RA= X-Received: by 2002:a05:620a:684:: with SMTP id f4mr223902qkh.258.1626301286424; Wed, 14 Jul 2021 15:21:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJymnAJAG1s3IIuL8U1KCptLk3RkFUZv2CP2yXs2+mvIhz26/ER+UYtoKJw1cDncE9s3s+WTuw== X-Received: by 2002:a05:620a:684:: with SMTP id f4mr223873qkh.258.1626301286213; Wed, 14 Jul 2021 15:21:26 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-65-184-144-111-238.dsl.bell.ca. [184.144.111.238]) by smtp.gmail.com with ESMTPSA id b25sm1625854qka.123.2021.07.14.15.21.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 15:21:25 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Jason Gunthorpe , peterx@redhat.com, Matthew Wilcox , Andrew Morton , Axel Rasmussen , Nadav Amit , Jerome Glisse , Mike Rapoport , Miaohe Lin , Hugh Dickins , Alistair Popple , Andrea Arcangeli , Mike Kravetz , "Kirill A . Shutemov" , David Hildenbrand Subject: [PATCH v4 03/26] mm: Clear vmf->pte after pte_unmap_same() returns Date: Wed, 14 Jul 2021 18:20:54 -0400 Message-Id: <20210714222117.47648-4-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714222117.47648-1-peterx@redhat.com> References: <20210714222117.47648-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fdYbPUin; spf=none (imf16.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: p3y861werssu4nhqkg9y8nr94qomf3jk X-Rspamd-Queue-Id: 70B9FF00009D X-Rspamd-Server: rspam01 X-HE-Tag: 1626301288-987286 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: pte_unmap_same() will always unmap the pte pointer. After the unmap, vmf->pte will not be valid any more. We should clear it. It was safe only because no one is accessing vmf->pte after pte_unmap_same() returns, since the only caller of pte_unmap_same() (so far) is do_swap_page(), where vmf->pte will in most cases be overwritten very soon. pte_unmap_same() will be used in other places in follow up patches, so that vmf->pte will not always be re-written. This patch enables us to call functions like finish_fault() because that'll conditionally unmap the pte by checking vmf->pte first. Or, alloc_set_pte() will make sure to allocate a new pte even after calling pte_unmap_same(). Since we'll need to modify vmf->pte, directly pass in vmf into pte_unmap_same() and then we can also avoid the long parameter list. Reviewed-by: Miaohe Lin Signed-off-by: Peter Xu --- mm/memory.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 747a01d495f2..0e0de08a2cd5 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2724,19 +2724,20 @@ EXPORT_SYMBOL_GPL(apply_to_existing_page_range); * proceeding (but do_wp_page is only called after already making such a check; * and do_anonymous_page can safely check later on). */ -static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, - pte_t *page_table, pte_t orig_pte) +static inline int pte_unmap_same(struct vm_fault *vmf) { int same = 1; #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION) if (sizeof(pte_t) > sizeof(unsigned long)) { - spinlock_t *ptl = pte_lockptr(mm, pmd); + spinlock_t *ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); spin_lock(ptl); - same = pte_same(*page_table, orig_pte); + same = pte_same(*vmf->pte, vmf->orig_pte); spin_unlock(ptl); } #endif - pte_unmap(page_table); + pte_unmap(vmf->pte); + /* After unmap of pte, the pointer is invalid now - clear it. */ + vmf->pte = NULL; return same; } @@ -3487,7 +3488,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) vm_fault_t ret = 0; void *shadow = NULL; - if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) + if (!pte_unmap_same(vmf)) goto out; entry = pte_to_swp_entry(vmf->orig_pte);