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: 13509772 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD6C5C47079 for ; Wed, 3 Jan 2024 09:16:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D1EE8D0057; Wed, 3 Jan 2024 04:16:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 683E78D0053; Wed, 3 Jan 2024 04:16:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 523C08D0057; Wed, 3 Jan 2024 04:16:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3E5E18D0053 for ; Wed, 3 Jan 2024 04:16:36 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 18AF180224 for ; Wed, 3 Jan 2024 09:16:36 +0000 (UTC) X-FDA: 81637444392.07.5BE774B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 6B1FC160006 for ; Wed, 3 Jan 2024 09:16:34 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hfcmehe0; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704273394; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dkbsR+hObgkKanb0hWK1+Xo39A1xL8364xJAXCv3uQU=; b=FMOFExvei0sEORyY7OaebH7x3RfQ3SilbqBWXUTo6v3l2j40SV3zwKlRIo81Us2DDyNuyi WbXHY87xU9p29TgXMeRVIiQF96lTYhpW8clBJ3ruQuQlp7hTdu2TI9TJ1LM6NjgufJHpPr eJ1DOrjHahOmcH33uUyiFI3I5/GAHkc= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hfcmehe0; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704273394; a=rsa-sha256; cv=none; b=25EQ9d24ExoKMOZCAAdDEF60D/NTHdMKVfVNqEf61BdpMazSkLeMkxhowHeU26vmODUojY 3I63k4K1q+iK2/fW0caUptCgV8h/SqSwa6raGYlldqI1+w12jiGrVvwYMJaxYllJ6W5TfD LDWY64I/1EU7sGzl8h4LCScNPvyK1Qw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704273393; 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=hfcmehe06fv8w81geqFni+ZztDEZ3+zNQBNuzJ2ShLa8wl0yl1w1qmzu3c745CfqQu9RZY TwWj6SYHZIzpCMisXjL6JKjgxJ70FRXVnNrSyCoD5FijwCYgWgNM1BNy2mw8ENkRZgJwLV N+HFCx2cykIPm1+tdR+SGfFmEx0B7ZQ= 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> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 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oQlUjtRmlbsj19Wfjs+bnXgETLB0bCI/f1 +htIdOnF IbsphceGa1igtrR1H8DeRx8REjPON6IEbswLZXb8aCEI/lYbpLk+zEQ4OT+x9M7l+0uiiT/a0RJi0ie15yxpFv4xFlH30hUJvQcvB9cFyiyJg1wPK9fQXOl55rx7LBvvHad1cWgzuuxF+Stv3LgLhpa5tXhwlmyR3eho6mNiunjsJOyGyCq7fcVsedJDKOFJE5pkR6BXLAZO8pU4NG20NdnWP553Po4dLFZqcsJ/GqcFbOvliqe1w0jDaOA== 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: List-Subscribe: List-Unsubscribe: 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,