Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp966060pxb; Wed, 1 Sep 2021 13:58:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwixF1/vscylKfTtghwyR3XAnD0KYuqGsqqQ3eKiIDbGIR8Aw7eTEf8id5vtUoVaJUnxNY5 X-Received: by 2002:a17:906:840f:: with SMTP id n15mr1565404ejx.460.1630529932519; Wed, 01 Sep 2021 13:58:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630529932; cv=none; d=google.com; s=arc-20160816; b=iZ9DKjCNWVWwYe/PmEG9hYbsOi9nDuWoRn+X9MsjxymWERUuy6TV+6soSWfA6c7rE5 UvHHHPTGM0K6eAAzw95JT25Kthw/ICMG68tYYoRr4t0UQax5W8wfePhtoxXXqqNdr7NX W1wvic5BHyYyF1bdacz08VCeKf6Ys6k36LMBzUTRMH0wS/VMvq+gw2l4x3qD5AvCkOb5 FnHs8DfTTWNiRSd6H0fZ6UAQjjwKCM6je3qAOvtkJkoseWx+scKCHonArzGiFnLblfaD WqPBn4BvCR5TC6kZ4eVjz4qQ9YVVKJtEAEqpvSgcN6O3uoc5Cff8NxiEfQm7i4hmeAyg YBEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H80HJYI0qyzkG5zbmWICcaBUu/kpdbMbG4peNGDuQ4A=; b=hwvAoenuZpcrAVl/U70vDK+AQ6q2X/gwHV8rA6awLGjbvrkM6VP20NTiJLty/RYDeS Df/syoSxLgjRbCmuzclSgUxim8JSWC9ScmCwcjcUxNh4ye1SAIsr4iY8FxzpSHZfZ0wV y5hbCRFqdeTpXnedQfdqjnO8w19YRhPFAxeICvl3FhdfQ2PoZC+SuloWvK317pH3IB7B h4gFes4TmuJ6CkrpEhleOR5sNz8sc9UkgPrzmzvX9H/j0GIiKqqEAGv0xiMQaxn9CbBV ScR3LhrYUN0G+UoJQrzWZOOCwwUEd/+KRxQky9QMOvCG10m+qNT9FhNCmDd3wfYXCW7T 4ziw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="AQwDPwF/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n9si723005ejj.544.2021.09.01.13.58.29; Wed, 01 Sep 2021 13:58:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="AQwDPwF/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S242640AbhIAU5k (ORCPT + 99 others); Wed, 1 Sep 2021 16:57:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:24013 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238528AbhIAU53 (ORCPT ); Wed, 1 Sep 2021 16:57:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630529792; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H80HJYI0qyzkG5zbmWICcaBUu/kpdbMbG4peNGDuQ4A=; b=AQwDPwF/i0xiMQ5H5Y4IHAurf78a6FP9bfIDaGJ8Ejo/InSpzh2H8qLAV1JRHYfI/stkxs 9xaoaq3kjIedOsox2lESN2/ON8+ARbTOiYd3f68LUMQ8p4wYFyzG2G6ZEuYSZHxpPhDo2t By0bGK8FRUsAz3QrxTkrT/msDW7++tE= 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-462-I8XtxldKMlaTrocXcnSB3g-1; Wed, 01 Sep 2021 16:56:31 -0400 X-MC-Unique: I8XtxldKMlaTrocXcnSB3g-1 Received: by mail-qt1-f200.google.com with SMTP id q19-20020ac87353000000b0029a09eca2afso843349qtp.21 for ; Wed, 01 Sep 2021 13:56:31 -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=i/3m96dKHkMXEPojh0Rj2EQU5U3+FNb5TEf7Xqn0/kZ28hVtxJjhBjerD6nV4UOHq2 4qm0aKOAOefD7l9UFjEqM/0qPGfVOJys3/NT0RNVjlOhE7qwDoz/IcecLq5w/NiL2N9/ 3S1lXLK6sx5n1+T0P4O8KqdQtiqAFSkXrHnYKXEOAwUI6t6pNg/G4AopAxmj46uc7z+1 rwjYOq1EpxObdOKrr9h/Lk0mUteKjGsTM/GhmU1c2XUytOKbXKDCAC67xA9HAF6yQA4M adqx+2Utt2v9oC6OokvMYP8dg7JEukA5qvqvIpS+TJgSZ6hfMS//Z+/QaPWT3gGzYhib FwMg== X-Gm-Message-State: AOAM530JA4uyYgNFU/URs3i/Jf9y+VN9dLA4dfj0gIjjObJj+tQyDUMY GgcAdGADxBKODAdn3uOrAEnkWjiS4TrBUNLJMA7OeoZEyRpPBBcV6GA6FX1sNko+rD+bKOcm6Ec pJcFf1YRLsAqj6UCJalEWjvcRDTSgw/0PV48RGfRFnLeuJgcltIletBfBuk4QoZC/dn9LYw7VnA == X-Received: by 2002:ac8:4241:: with SMTP id r1mr433343qtm.296.1630529790220; Wed, 01 Sep 2021 13:56:30 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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); -- 2.31.1