From patchwork Wed Jan 3 09:14:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 184682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4912266dyb; Wed, 3 Jan 2024 01:19:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IGbmGwyeBprhyfJXgdLw/1APDTXX+CACNUOvFB8gkdVQO00nVrWM0IVAbo6OLiz/ZXnKvPh X-Received: by 2002:a05:6870:ec8f:b0:203:b8c3:f60e with SMTP id eo15-20020a056870ec8f00b00203b8c3f60emr7877797oab.103.1704273574260; Wed, 03 Jan 2024 01:19:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704273574; cv=none; d=google.com; s=arc-20160816; b=Xe0Zox9lR/3WADYJi7i5mgdGWItegif/5xQ2Kkx2hDYREytmZeklia9MkGWxh6TAKs jaDJRTWh3xvplJpSaldOHaQzhvt4MBwDGC7hThVyy8+bzYazIfD9VpuZjgCTNlnH1xDh QiGJr48iO/FR9SW8JVfnKsZfBmxHNyop9kcnXjSC9PV1PInII+qKDmIp0wn8NUaSQHnG usnJyv7oCpBZEdfu92+b89DjM3GxOgj/oEwjg3AlSljxza+96fpF6a/51jn9Ovvuueav AItOeFFwV3oh5aLrPXwXT+s2VEU0lT4rRcBgLTmnDZcbC9bd2MrLyYKBQFKLT79A9D6z FOLQ== 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15324-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d65-20020a633644000000b005ce006d9875si18659157pga.587.2024.01.03.01.19.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 01:19:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15324-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15324-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id D11E1B23713 for ; Wed, 3 Jan 2024 09:17:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ABD131A282; Wed, 3 Jan 2024 09:16:37 +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 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787060367378907479 X-GMAIL-MSGID: 1787060367378907479 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 Reviewed-by: Jason Gunthorpe --- 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,