From patchwork Wed Sep 1 20:56:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12470317 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,URIBL_BLOCKED,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 D77D9C432BE for ; Wed, 1 Sep 2021 20:56:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6C8566102A for ; Wed, 1 Sep 2021 20:56:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6C8566102A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id D9166900003; Wed, 1 Sep 2021 16:56:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D41E6900002; Wed, 1 Sep 2021 16:56:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B44CC900003; Wed, 1 Sep 2021 16:56:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0171.hostedemail.com [216.40.44.171]) by kanga.kvack.org (Postfix) with ESMTP id A464F900002 for ; Wed, 1 Sep 2021 16:56:32 -0400 (EDT) Received: from smtpin35.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 54CFC252C9 for ; Wed, 1 Sep 2021 20:56:32 +0000 (UTC) X-FDA: 78540213024.35.5F7418D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf16.hostedemail.com (Postfix) with ESMTP id F336CF00008C for ; Wed, 1 Sep 2021 20:56:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630529791; 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=H80HJYI0qyzkG5zbmWICcaBUu/kpdbMbG4peNGDuQ4A=; b=LBPbANKYJ0PJAKhGN4WDQG6XV2mWwaOgcZCDenl4WRKfdTE3pKGkv1RTF7kYBIBZX9NxlG NzzR8s/07XbAJQ1fOJeLwfLi4mOK+EHjEkWOKDXSQwXMGDJg0W/oMCCeihYg5hRuEPYHF/ JJkCLYc7aCb7XK4XmsZmH5+QNWEG8kU= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-460-Db0PLC4SPBKL7vyL3_8RVA-1; Wed, 01 Sep 2021 16:56:30 -0400 X-MC-Unique: Db0PLC4SPBKL7vyL3_8RVA-1 Received: by mail-qv1-f70.google.com with SMTP id dv7-20020ad44ee7000000b0036fa79fd337so984933qvb.6 for ; Wed, 01 Sep 2021 13:56:30 -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=H80HJYI0qyzkG5zbmWICcaBUu/kpdbMbG4peNGDuQ4A=; b=pRCY3tqqEhpPHQaAYqRZ8rk5Z5vHUzR7roVVLJ+CaXrlJsfipUc2JdJg4/T9u5c8od xviCA0hEyShUgYIUnSdiVv5h0aSSnOrBRnCN9dElRhRNDdmtukGt+YXgqBpUypg0ex+b eFKUDdRv/KK/jqt1/OJ7gPYQJThSvwXBTs7X38W96k1JaXE/4fk+gBTZdB/BvbG9lGss FLMpnfd9XIDqkzhV0VhiGQbJ/fr9K/WwlSBrpTc0OhjgHrwGYYMRSplEqCfRQd/MmIR7 CKwxEaWXRzEtMaojls7YEeVorZD1AQVsIzmOLZQ0bt+9a/ptzKZvN+I3Mjd4KYgKgmDU 3DYQ== X-Gm-Message-State: AOAM530g4FrFGeMUvhz/Z6FPBYCVVsm4POpC+U0Y1VDs4UUDxq3n+hGq msWcNBqNScbSBOgw949ofMr9jpNRjhdfAR7Y10yRUhzEOPvFAm3k3NNoYwMVlHrWATxX7f3CDA9 L5kreuGGj+dA= X-Received: by 2002:ac8:4241:: with SMTP id r1mr433337qtm.296.1630529790194; Wed, 01 Sep 2021 13:56:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxfX94m0Q5rz7prZMT3zggqIDbw5ktE9Et7MQf17IWYgA/u195BGGhxvkI+Dg/upwPF/Y2ASQ== X-Received: by 2002:ac8:4241:: with SMTP id r1mr433306qtm.296.1630529789960; Wed, 01 Sep 2021 13:56:29 -0700 (PDT) Received: from t490s.redhat.com ([2607:fea8:56a3:500::ad7f]) by smtp.gmail.com with ESMTPSA id f3sm624865qti.65.2021.09.01.13.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Sep 2021 13:56:29 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrea Arcangeli , Mike Rapoport , peterx@redhat.com, Jerome Glisse , Alistair Popple , Yang Shi , Andrew Morton , David Hildenbrand , Miaohe Lin , "Kirill A . Shutemov" , Matthew Wilcox , Hugh Dickins Subject: [PATCH 2/5] mm: Clear vmf->pte after pte_unmap_same() returns Date: Wed, 1 Sep 2021 16:56:19 -0400 Message-Id: <20210901205622.6935-3-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210901205622.6935-1-peterx@redhat.com> References: <20210901205622.6935-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=LBPbANKY; spf=none (imf16.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: F336CF00008C X-Stat-Signature: 8tq3wsqd3ijs49xkkzsq4hkbyk3ucfd5 X-HE-Tag: 1630529791-772150 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. Directly pass in vmf into pte_unmap_same() and then we can also avoid the long parameter list too, which should be a nice cleanup. Reviewed-by: Miaohe Lin Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand --- mm/memory.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 25fc46e87214..204141e8a53d 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);