From patchwork Tue Mar 23 00:48:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12156479 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 66175C433DB for ; Tue, 23 Mar 2021 00:49:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E1B6F6199F for ; Tue, 23 Mar 2021 00:49:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E1B6F6199F 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 8B3C66B0100; Mon, 22 Mar 2021 20:49:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8637F6B0103; Mon, 22 Mar 2021 20:49:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B8E26B0107; Mon, 22 Mar 2021 20:49:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0073.hostedemail.com [216.40.44.73]) by kanga.kvack.org (Postfix) with ESMTP id 396246B0100 for ; Mon, 22 Mar 2021 20:49:22 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id E4E3A8419 for ; Tue, 23 Mar 2021 00:49:21 +0000 (UTC) X-FDA: 77949305322.36.8B6C5A9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 5331FA0009E5 for ; Tue, 23 Mar 2021 00:49:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616460560; 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=qzE2FQvBgwjcQuvAyEoA4yo8mQxpoaRUw1a4rmG7XTE=; b=PqLAGVOGYWKFuD2AA22/2Rcwd8aGdRL4DwNQyZw3QexC0SPmRLfP2u50JFqy3fpPMYUDNp Ddf8psj82+nBINCksF4SSrHGPZFfftUKVfcWWZZHfIpYON1g9pL5j9qNW+scav0+naMLr/ P5hOmebphGw9D3/3TBhrR+uk+9u9olw= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-157-fW8Iq_VvNDi8OciPqKKSXg-1; Mon, 22 Mar 2021 20:49:19 -0400 X-MC-Unique: fW8Iq_VvNDi8OciPqKKSXg-1 Received: by mail-qt1-f199.google.com with SMTP id m8so392477qtp.14 for ; Mon, 22 Mar 2021 17:49:19 -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=qzE2FQvBgwjcQuvAyEoA4yo8mQxpoaRUw1a4rmG7XTE=; b=hyx0mOPfhIJ4qGYWhE4mDvoSJ6TR17KYjFV0EiR7oqF49AyVlR8uzlC9JaLFnhG2sY rX5XDsdfYpdyfyDo+eSLHwo4siwfONMoKNiSWVF8eRLd0QEq72ainSmZHetwRgxjcPL0 DBpBXrSEIIWD95wI/xrYo0VJq/9axGSy9Qef3W1pIWaMnW1gL+DDla/kmxH1pbClnyZA 9s4XGY0ykueA7HeMJ6oUG4qQfJ3iS+NY2Ir9euf30RZdgI/M8gc6gDpwXrHvnn6O77Sq zrmCw/s6KKP+HP57zxLXdAxJIrPHl9A9P/s9uAEUspugGxetvCBz5nlXaeQcWeYtT6FH L7UQ== X-Gm-Message-State: AOAM530yS1+ntfqvFVh69p8cVWRCis6jXR41reiXqJhX//4peV33bfdI oWkCcMPvZixCza6xKPjOMcAOplEwNu6JqzpYMPT0/LwXLA0a1dR8H+Pz5bWKRlPQ+WdGw+2wQCD 9KvwqMARB3vI= X-Received: by 2002:ac8:4314:: with SMTP id z20mr2399455qtm.127.1616460558822; Mon, 22 Mar 2021 17:49:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGnxyaubPdosmeB8+KjtwtJv00yj3i3xw1XrG7tvd1zJbFrOCn0HMVDuIqra3FYF+x1R7Apg== X-Received: by 2002:ac8:4314:: with SMTP id z20mr2399429qtm.127.1616460558565; Mon, 22 Mar 2021 17:49:18 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id n6sm5031793qtx.22.2021.03.22.17.49.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 17:49:18 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: "Kirill A . Shutemov" , Jerome Glisse , Mike Kravetz , Matthew Wilcox , Andrew Morton , Axel Rasmussen , Hugh Dickins , peterx@redhat.com, Nadav Amit , Andrea Arcangeli , Mike Rapoport Subject: [PATCH 02/23] mm: Clear vmf->pte after pte_unmap_same() returns Date: Mon, 22 Mar 2021 20:48:51 -0400 Message-Id: <20210323004912.35132-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210323004912.35132-1-peterx@redhat.com> References: <20210323004912.35132-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: prx4hq48zu7cidhep5en3sfwnr8df4o7 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 5331FA0009E5 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf24; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616460560-768737 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. Signed-off-by: Peter Xu Reviewed-by: Miaohe Lin --- mm/memory.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index a458a595331f..d534eba85756 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2607,19 +2607,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; } @@ -3308,7 +3309,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);