From patchwork Tue Apr 20 13:16:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 12214231 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.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable 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 304A4C433B4 for ; Tue, 20 Apr 2021 13:16:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ECA07613D1 for ; Tue, 20 Apr 2021 13:16:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232362AbhDTNRL (ORCPT ); Tue, 20 Apr 2021 09:17:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:33150 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232359AbhDTNRL (ORCPT ); Tue, 20 Apr 2021 09:17:11 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8D3E9613CD; Tue, 20 Apr 2021 13:16:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618924599; bh=WQBjtjBUVixKPUq09lDhJ2RWgVokupnypM/eycKpBe4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KLusc3hvcK/USIRTI1/AA31CBHshBIRyZKJvd5mzmsf8uSSJ7TpzKDUsknaC7MPq6 eN0zAy+9/IAGzPjtf0706PNPT2er4LIkgsjACRHnJ0RD6mEJSD9zoue15HlmXFbsNB WGr5S/g/5n5tBlMAVJCDdPgvHQnj5JM/Bjkejl8nPPCZ8wGk0lluRfswUy871QdR0G NvuSLdYZOYvhOTbadz3yEZRjXbepapK8sotHUAsbDxshP5vjW7LQpXxOenROX7IQQr FdwihTAykTp4q2B0yXS34iR+4uoUJCi072sqeX6ixjq5AcceB0CrHyE/nOjCJknzzP mVOEnaY9ajjYg== From: Mike Rapoport To: Andrew Morton Cc: Alexander Viro , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christopher Lameter , Dan Williams , Dave Hansen , David Hildenbrand , Elena Reshetova , "H. Peter Anvin" , Ingo Molnar , James Bottomley , "Kirill A. Shutemov" , Matthew Wilcox , Matthew Garrett , Mark Rutland , Michal Hocko , Mike Rapoport , Mike Rapoport , Michael Kerrisk , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , "Rafael J. Wysocki" , Rick Edgecombe , Roman Gushchin , Shakeel Butt , Shuah Khan , Thomas Gleixner , Tycho Andersen , Will Deacon , Yury Norov , linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-riscv@lists.infradead.org, x86@kernel.org Subject: [PATCH v2 1/2] secretmem/gup: don't check if page is secretmem without reference Date: Tue, 20 Apr 2021 16:16:08 +0300 Message-Id: <20210420131611.8259-2-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210420131611.8259-1-rppt@kernel.org> References: <20210420131611.8259-1-rppt@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Mike Rapoport The check in gup_pte_range() whether a page belongs to a secretmem mapping is performed before grabbing the page reference. To avoid potential race move the check after try_grab_compound_head(). Signed-off-by: Mike Rapoport --- mm/gup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index c3a17b189064..4b58c016e949 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2080,13 +2080,13 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, VM_BUG_ON(!pfn_valid(pte_pfn(pte))); page = pte_page(pte); - if (page_is_secretmem(page)) - goto pte_unmap; - head = try_grab_compound_head(page, 1, flags); if (!head) goto pte_unmap; + if (page_is_secretmem(page)) + goto pte_unmap; + if (unlikely(pte_val(pte) != pte_val(*ptep))) { put_compound_head(head, 1, flags); goto pte_unmap; From patchwork Tue Apr 20 13:16:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 12214235 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.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,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 87990C433B4 for ; Tue, 20 Apr 2021 13:17:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4D9AB613D3 for ; Tue, 20 Apr 2021 13:17:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232419AbhDTNRe (ORCPT ); Tue, 20 Apr 2021 09:17:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:33546 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232412AbhDTNRd (ORCPT ); Tue, 20 Apr 2021 09:17:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A58626101C; Tue, 20 Apr 2021 13:16:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618924622; bh=WQBjtjBUVixKPUq09lDhJ2RWgVokupnypM/eycKpBe4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o20bjAEWBJIEF1h8/qnesID8pJlsOtYuQBEFthZNIuah5vjhGL7mYjpmIzPheyOx4 zLmqEbpr2OKL7I9OmN22sKr2N7ibr2TrEl5aMvAOxoDBtPZqdRTGpZoyJ2PJqkfxJV Qbxe7kAtQvCe0Cszrfy03TeNMo4aOoMIbPjQ/IhtksgBNK+KbWwn5JjkQyeTlh+9GW 7ayegBRJ7F9EeOZgbRVUmdGD+Gtl0pbyUlvLoUAxk3zVPW/ZgQB24WZZ3cq+M4yt5b cS+Z7QvAnRl9GQRZmMTOmd34XlEqDq8drJq7x8VbwE6eT/oQpXisCqiOOov/gnN9P+ LmH3i4FuAUXpw== From: Mike Rapoport To: Andrew Morton Cc: Alexander Viro , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christopher Lameter , Dan Williams , Dave Hansen , David Hildenbrand , Elena Reshetova , "H. Peter Anvin" , Ingo Molnar , James Bottomley , "Kirill A. Shutemov" , Matthew Wilcox , Matthew Garrett , Mark Rutland , Michal Hocko , Mike Rapoport , Mike Rapoport , Michael Kerrisk , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , "Rafael J. Wysocki" , Rick Edgecombe , Roman Gushchin , Shakeel Butt , Shuah Khan , Thomas Gleixner , Tycho Andersen , Will Deacon , Yury Norov , linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-riscv@lists.infradead.org, x86@kernel.org Subject: [PATCH v2 2/2] secretmem/gup: don't check if page is secretmem without reference Date: Tue, 20 Apr 2021 16:16:10 +0300 Message-Id: <20210420131611.8259-4-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210420131611.8259-1-rppt@kernel.org> References: <20210420131611.8259-1-rppt@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Mike Rapoport The check in gup_pte_range() whether a page belongs to a secretmem mapping is performed before grabbing the page reference. To avoid potential race move the check after try_grab_compound_head(). Signed-off-by: Mike Rapoport --- mm/gup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index c3a17b189064..4b58c016e949 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2080,13 +2080,13 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, VM_BUG_ON(!pfn_valid(pte_pfn(pte))); page = pte_page(pte); - if (page_is_secretmem(page)) - goto pte_unmap; - head = try_grab_compound_head(page, 1, flags); if (!head) goto pte_unmap; + if (page_is_secretmem(page)) + goto pte_unmap; + if (unlikely(pte_val(pte) != pte_val(*ptep))) { put_compound_head(head, 1, flags); goto pte_unmap;