Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7014016rdb; Wed, 3 Jan 2024 01:17:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgJG3GaDf28D4zg5mVvSRNZpXnh7QrcKn86jjY83YNJeJvYzR7bXYnNFqb037/54ec3WdA X-Received: by 2002:a05:622a:50e:b0:427:95fa:5a40 with SMTP id l14-20020a05622a050e00b0042795fa5a40mr27985302qtx.124.1704273467317; Wed, 03 Jan 2024 01:17:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704273467; cv=none; d=google.com; s=arc-20160816; b=Exo/m8sXtu/CK1rkisHV5R0oOTZ/eacdxCEfC5vAyo6kOFdkt42TNW84Zs8evg0cQr HCriH6EjOWNP0ql992/tKrcqR9APJbjACdgR9jSODJPtYL8OFSnWanOJZGKdvO/rP53D WJAlC8ZuOzLQ9wN0FIh5n5CJUF2kBp9asvt3F4ecPKwT2CXKIhIggvP8x0JWGoAW0q7g nLSf2mcWJDwx044Q4z2yc2rc3l2UeTHQyXqXTyR+rs3vRAofkYtmlsRAtFiMwDFvLz4t yyzMH36UDSRls76p+7mbTaXWGFd+ZMo+PgrJIqip29dlTSMhHaCue5Zyp4z15LI/5sEq vloQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=dkbsR+hObgkKanb0hWK1+Xo39A1xL8364xJAXCv3uQU=; fh=JvQ3nGflNTIwPBfhSW2OJAIjHOHR+R1SiFkwzYoQoWY=; b=INmykgmzgWS/qI4ZKcRWeA7BlYedHsZOPyhFGzwkgraGkVUTk9WliyR6F0+t1DuiEE nVC6rUBU6TuAea6MM887SPRcAsAiv+B+wUbCqTxwTGi3pAVfPCgjhtnh4dX5ykvupXZc vUek+KPLnSI8ewO6X8WiyYW2Nuk0o1GW7zk7dAUDNMMzJcxBqwkWpiv3WSKWA2kW3Mb+ hTFbvMzyQhU6YcftGIis7NTwXNK3/zJL/pssaXM6B0Eczvz5pnTdLrO52IR2DG7WIghT eqSSEqSbjaKo+A5tZwebeUyER3hrZTlvZN+cJEY4Opdz8tRuE9KrUN5MGVgb2WqNT8HG R0HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=du7VdgO4; spf=pass (google.com: domain of linux-kernel+bounces-15324-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15324-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id i8-20020ac85c08000000b00428204215cdsi5960028qti.74.2024.01.03.01.17.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 01:17:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15324-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=du7VdgO4; spf=pass (google.com: domain of linux-kernel+bounces-15324-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15324-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1709A1C231D8 for ; Wed, 3 Jan 2024 09:17:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E791319BD7; Wed, 3 Jan 2024 09:16:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="du7VdgO4" X-Original-To: linux-kernel@vger.kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D180819BCC for ; Wed, 3 Jan 2024 09:16:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704273392; 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=dkbsR+hObgkKanb0hWK1+Xo39A1xL8364xJAXCv3uQU=; b=du7VdgO4cxnkmxRWxbfQ1qq5y/m7KFtTHooajSvIdqWs4PL53aO4HuDgOJnsj/wXWQYfEc iPTHK9VOOxXN1ugLPw0RtIHGNSRvnbxnfij7aanlWwtyi+gU6britB/c/FD2UWVPmpDJym mXFRAfMTYQuE3lEg/2LVbTrSxRRiYhE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-611-7CEiuF6PMvSSKuTScRXfJw-1; Wed, 03 Jan 2024 04:16:28 -0500 X-MC-Unique: 7CEiuF6PMvSSKuTScRXfJw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8EF2A185A780; Wed, 3 Jan 2024 09:16:27 +0000 (UTC) Received: from x1n.redhat.com (unknown [10.72.116.69]) by smtp.corp.redhat.com (Postfix) with ESMTP id 33861492BE6; Wed, 3 Jan 2024 09:16:15 +0000 (UTC) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: James Houghton , David Hildenbrand , "Kirill A . Shutemov" , Yang Shi , peterx@redhat.com, linux-riscv@lists.infradead.org, Andrew Morton , "Aneesh Kumar K . V" , Rik van Riel , Andrea Arcangeli , Axel Rasmussen , Mike Rapoport , John Hubbard , Vlastimil Babka , Michael Ellerman , Christophe Leroy , Andrew Jones , linuxppc-dev@lists.ozlabs.org, Mike Kravetz , Muchun Song , linux-arm-kernel@lists.infradead.org, Jason Gunthorpe , Christoph Hellwig , Lorenzo Stoakes , Matthew Wilcox Subject: [PATCH v2 09/13] mm/gup: Cache *pudp in follow_pud_mask() Date: Wed, 3 Jan 2024 17:14:19 +0800 Message-ID: <20240103091423.400294-10-peterx@redhat.com> In-Reply-To: <20240103091423.400294-1-peterx@redhat.com> References: <20240103091423.400294-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 From: Peter Xu Introduce "pud_t pud" in the function, so the code won't dereference *pudp multiple time. Not only because that looks less straightforward, but also because if the dereference really happened, it's not clear whether there can be race to see different *pudp values if it's being modified at the same time. Acked-by: James Houghton Signed-off-by: Peter Xu --- mm/gup.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index b8a80e2bfe08..63845b3ec44f 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -753,26 +753,27 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma, unsigned int flags, struct follow_page_context *ctx) { - pud_t *pud; + pud_t *pudp, pud; spinlock_t *ptl; struct page *page; struct mm_struct *mm = vma->vm_mm; - pud = pud_offset(p4dp, address); - if (pud_none(*pud)) + pudp = pud_offset(p4dp, address); + pud = READ_ONCE(*pudp); + if (pud_none(pud)) return no_page_table(vma, flags, address); - if (pud_devmap(*pud)) { - ptl = pud_lock(mm, pud); - page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); + if (pud_devmap(pud)) { + ptl = pud_lock(mm, pudp); + page = follow_devmap_pud(vma, address, pudp, flags, &ctx->pgmap); spin_unlock(ptl); if (page) return page; return no_page_table(vma, flags, address); } - if (unlikely(pud_bad(*pud))) + if (unlikely(pud_bad(pud))) return no_page_table(vma, flags, address); - return follow_pmd_mask(vma, address, pud, flags, ctx); + return follow_pmd_mask(vma, address, pudp, flags, ctx); } static struct page *follow_p4d_mask(struct vm_area_struct *vma, -- 2.41.0