From mboxrd@z Thu Jan 1 00:00:00 1970 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=-19.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 35E2BC432BE for ; Thu, 2 Sep 2021 07:20:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CE31760FDC for ; Thu, 2 Sep 2021 07:20:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org CE31760FDC 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 37F598D0002; Thu, 2 Sep 2021 03:20:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32F138D0001; Thu, 2 Sep 2021 03:20:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CFE08D0002; Thu, 2 Sep 2021 03:20:13 -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 0EEF48D0001 for ; Thu, 2 Sep 2021 03:20:13 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id AD59020318 for ; Thu, 2 Sep 2021 07:20:12 +0000 (UTC) X-FDA: 78541784664.05.997B328 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 3D0B04002085 for ; Thu, 2 Sep 2021 07:20:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630567211; 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=yHe29GsWYeThVgB4v24Lig4XybIq4Mhjph9NrrexDUI=; b=DG6Zd+aqRg2VfAcWw4Bfl84nfEY0Ty19Iq8LJKyNQn1F2qwz2Km172epplcgyQm+9FLObb iPKLQ21MfgeIkgpI+hnN/Gp+Rgz9h13rt5472FW8aOvT8gNCKdBEulSG18c7LEbXVOh25o 0wR++OU2tqja8ouiZ/Jr7qnYpzhxx/A= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-414--bpLmlVWOWuM4aFLb3HgIA-1; Thu, 02 Sep 2021 03:20:08 -0400 X-MC-Unique: -bpLmlVWOWuM4aFLb3HgIA-1 Received: by mail-wm1-f70.google.com with SMTP id z18-20020a1c7e120000b02902e69f6fa2e0so337394wmc.9 for ; Thu, 02 Sep 2021 00:20:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=yHe29GsWYeThVgB4v24Lig4XybIq4Mhjph9NrrexDUI=; b=s0kQZPAJnQmHGaz/xHij6bmBAJ5glIRY0XBXmYsvfcHjse4XpINgCG26G4GrYui+C8 345cJA/ly0sNjq7kT+Eo2Jw2PcwSxpaGI5uixGmXTgC03zVBSUpy/D7mRRQEMKuf8yT7 op2joYjAkStFhnaXyaoDejtK2uS1ZPFjR6Vc5NpVAKJi8/agoc/CYzxhAow9vsaJxCD2 whxB23tLGXkqk40e8CVy2U62wCxClUaHnxO+wVMHyQeme0AuF/CyzmzjvCo7jAx3T7Mv blBLxTK28NCOrvkqlvZQLFSvMxxYiRvhvitZuCH85nmIRRYT58t5qEr5EIKbP2cbau/g +wOg== X-Gm-Message-State: AOAM533MNLhhj3vQwPmCmIKTIMyZRaLC2aZt72Y/YaGjWs/tzWqmrUuF YSXFptUdd8oONxLTTtzOGXLA2nlNRKrlLuTElvNHySwHgUN7kcQymie2YGKtEj1vud+Ds5r899h N486/Qdo/VBU= X-Received: by 2002:a1c:7dd0:: with SMTP id y199mr1647354wmc.23.1630567207468; Thu, 02 Sep 2021 00:20:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsEKNjsbvVWbrQJsZ/T5V1paWGh4CBK2Z6E2eOCXZvlWYWIGVAIoAWJaVnEl44c1dqguLf3Q== X-Received: by 2002:a1c:7dd0:: with SMTP id y199mr1647311wmc.23.1630567207205; Thu, 02 Sep 2021 00:20:07 -0700 (PDT) Received: from [192.168.3.132] (p5b0c60bd.dip0.t-ipconnect.de. [91.12.96.189]) by smtp.gmail.com with ESMTPSA id q13sm956103wrv.79.2021.09.02.00.20.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Sep 2021 00:20:06 -0700 (PDT) Subject: Re: [PATCH 2/5] mm: Clear vmf->pte after pte_unmap_same() returns To: Peter Xu , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrea Arcangeli , Mike Rapoport , Jerome Glisse , Alistair Popple , Yang Shi , Andrew Morton , Miaohe Lin , "Kirill A . Shutemov" , Matthew Wilcox , Hugh Dickins References: <20210901205622.6935-1-peterx@redhat.com> <20210901205622.6935-3-peterx@redhat.com> From: David Hildenbrand Organization: Red Hat Message-ID: <51db1487-053f-7009-8321-9c28f9c5798b@redhat.com> Date: Thu, 2 Sep 2021 09:20:05 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210901205622.6935-3-peterx@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DG6Zd+aq; spf=none (imf18.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3D0B04002085 X-Stat-Signature: 31wt19m5mdadh1zgonou9tgrfnn5qwrq X-HE-Tag: 1630567212-935453 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: On 01.09.21 22:56, Peter Xu wrote: > 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. */ I'd just drop the comment, it's what we do in similar code. > + 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; Funny, I prototyped something similar yesterday. I did it via same = pte_lock_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte); pte_unmap(vmf->pte); vmf->pte = NULL; if (!same) goto out; To just move handling to the caller. But this also looks fine, whatever you prefer. Reviewed-by: David Hildenbrand -- Thanks, David / dhildenb