From patchwork Tue Apr 27 16:12:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12226869 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 2383DC433B4 for ; Tue, 27 Apr 2021 16:13:31 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AA398613E7 for ; Tue, 27 Apr 2021 16:13:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AA398613E7 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 1F6326B0070; Tue, 27 Apr 2021 12:13:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CD656B0071; Tue, 27 Apr 2021 12:13:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 020706B0072; Tue, 27 Apr 2021 12:13:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0120.hostedemail.com [216.40.44.120]) by kanga.kvack.org (Postfix) with ESMTP id CF8E26B0070 for ; Tue, 27 Apr 2021 12:13:29 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 81E61180AD81D for ; Tue, 27 Apr 2021 16:13:29 +0000 (UTC) X-FDA: 78078642138.23.4DC1B79 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 81E0C2000261 for ; Tue, 27 Apr 2021 16:13:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619540008; 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=tertuAFVCCpsfAn7kyP9pkuWqwtVeARZYQAUe1yux2Y=; b=B5wgvjKhJRZ6jjMhnToQAK3VGVf+OEA+Yuq40ZWtyLN0n8WMkp9HcysYKJ8egfcdm3xo8C 64l/523ktaSjBG90WpjUzkl5xPDDRcX2/Z5jZTOolgMhoVjoDfLZMt+XsMC5BJfv//P4CM eJgBEqa3roTS07fB1Ak/1v3f4YFZVpQ= 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-564--Enzg-k-M9qVbRjxDpx-Xw-1; Tue, 27 Apr 2021 12:13:25 -0400 X-MC-Unique: -Enzg-k-M9qVbRjxDpx-Xw-1 Received: by mail-qt1-f199.google.com with SMTP id p15-20020a05622a00cfb02901ae13813340so23581783qtw.15 for ; Tue, 27 Apr 2021 09:13:24 -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=tertuAFVCCpsfAn7kyP9pkuWqwtVeARZYQAUe1yux2Y=; b=Tu1raAYuDzq+wCWERyNBo7+eckYiN7URW5UN2s0d9MWvOqpslKCf97TGyJavydsW2P XFa5oTeAy35Qge8gl8IA8GWScSv5JOIp6IM7FRbvsSpPLCxpK9L4Hlm+T3h9Val4ogVs 4tSUiv/BizqEJqmc7ku4L13YWHBbVr62sLkuhDsH4t6CIVc8FYl4i+Z+w0EtGDSP5Jie FF4qmv3VdD+i33RUWC7EwgbVp30YFIz/XTDeZeCHmgAxFpESJR6B41MF5UMg/C5Fhsar nB1fsk8A7j/sSLO+OdhvhWiAnSNs+U3nIWjuvtW8Nji8zUS8bkS+7ewCK2DSAxhMMaQi zlww== X-Gm-Message-State: AOAM533bMprrjytbU6G98C/Wco56+B10fo/4jRNyjtYfTLiqNWq064ao qtp71KUsa+rRKuSxFt+yKS56DyYhmNlR6CzAP7jT6LVkye9jcAOzTF0EaQAnyQ21HWqVpkxiBf7 LMla0BBTfXjcjctvbcVPCYHfzlg/gcweHLUtCIX8eRKU0tuqYPoBHQWqeOGu8 X-Received: by 2002:a05:620a:1439:: with SMTP id k25mr15521492qkj.439.1619540003961; Tue, 27 Apr 2021 09:13:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxt4RxZQcMhYZdYCMXlw1LswxlEQbOKnDM/0s3R+vwolBpcTn/z++YiH5clcERdhpGhQS5p/Q== X-Received: by 2002:a05:620a:1439:: with SMTP id k25mr15521457qkj.439.1619540003656; Tue, 27 Apr 2021 09:13:23 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-77-184-145-104-227.dsl.bell.ca. [184.145.104.227]) by smtp.gmail.com with ESMTPSA id v66sm3103621qkd.113.2021.04.27.09.13.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 09:13:23 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , Miaohe Lin , Mike Rapoport , Andrea Arcangeli , Hugh Dickins , peterx@redhat.com, Jerome Glisse , Mike Kravetz , Jason Gunthorpe , Matthew Wilcox , Andrew Morton , Axel Rasmussen , "Kirill A . Shutemov" Subject: [PATCH v2 02/24] mm: Clear vmf->pte after pte_unmap_same() returns Date: Tue, 27 Apr 2021 12:12:55 -0400 Message-Id: <20210427161317.50682-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210427161317.50682-1-peterx@redhat.com> References: <20210427161317.50682-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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 81E0C2000261 X-Stat-Signature: koonbbh8g4syxsyw9zrh87pznjf9ksb6 Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf28; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=170.10.133.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1619540010-881449 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 ffda19542bc6d..955a0bb6b855c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2618,19 +2618,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; } @@ -3319,7 +3320,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);