From patchwork Thu Jul 6 22:50:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 700731 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA91DEB64DC for ; Thu, 6 Jul 2023 22:51:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231925AbjGFWvP (ORCPT ); Thu, 6 Jul 2023 18:51:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231674AbjGFWu5 (ORCPT ); Thu, 6 Jul 2023 18:50:57 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C5A31BE1 for ; Thu, 6 Jul 2023 15:50:56 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bacfa4ef059so1337807276.2 for ; Thu, 06 Jul 2023 15:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683855; x=1691275855; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zDsVvAgbqAnX6PMRHG2ZwgnBlt3+9ofsZz4Jdu5BLSk=; b=HaacAumEvn1eewgFiWzFDvsikzZVzPppCLT9rnA4TuMVOYLLy/vYJp6o8eS4v2uERC eDEIsx+BKjHd/+jFBv97VKbIdryCcYHF7rTvthDzDOT6vjTdMik5bmYGYq9YywI8gk/E IGbbrW20IF+yQTBxXBW0WV2O4wlEDTsHUqaG4YRBMSJGdJJ7YunZJhzePLuWnx9Wd57A 27l/ThhbyJNl1BV6XUx7oUBXPVXukMu7rVGxP8OS6VqC0GqDxzyj1IOddqpGqcyY2e9m uEIi8hNXSJHFLEllQyYR0l9vuzQePw7paEHgOJxnlvQ+OYByrCTbg4bjwmtqYY5COLUP d0Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683855; x=1691275855; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zDsVvAgbqAnX6PMRHG2ZwgnBlt3+9ofsZz4Jdu5BLSk=; b=ZADQ+gMN/IElSUY2+QqN9wupuNmtHK9UP7DWB96Lkj37SZJN8elR2LACn5rwBGzVud ywiNSBFooChy9xLWEDpAm7mRaY2JHYR31LOERgkcejEgYD7akY2wSZZIR28ZFg0djILp 7zz2Y7xRvOLRXkouUdRIKA6vANgrbMbE08rzIyd8d9iyw9ftAukVJg7y/NDxVtWTq+DD 2ZMZibILVRP51UKgnWXHgdaV+iltp5uNAP0PDOII8IuobFd53FpTlvVo7pJGiI5Ce3JZ h1l70mAfR/xFHwvj5C9P/ieJ8+pBhbkYCAj8wvbQlwRXERRA6GkwBoSc9E65dCcNlr1L 3UdQ== X-Gm-Message-State: ABy/qLZzTn23f51/tY2cTqHoaL1HLJFZqhDuKGo0345NykYS3+46Eptl nipHYCqTX+veFPcJ04zEbbimMxUqQ8XTNZwYW0kb X-Google-Smtp-Source: APBJJlEyx3c2b3n+HetuTYESkQKsyNdyuLP/Hi0we79ObPjhaTnQQ2CLDQEif0ahTjNKD3svWuq1bxNKKEwVrZS6kxor X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a25:53c2:0:b0:be4:7214:7aef with SMTP id h185-20020a2553c2000000b00be472147aefmr17503ybb.10.1688683855407; Thu, 06 Jul 2023 15:50:55 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:31 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-4-axelrasmussen@google.com> Subject: [PATCH v3 3/8] mm: userfaultfd: extract file size check out into a helper From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This code is already duplicated twice, and UFFDIO_POISON will do the same check a third time. So, it's worth extracting into a helper to save repetitive lines of code. Signed-off-by: Axel Rasmussen --- mm/userfaultfd.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index a2bf37ee276d..4244ca7ee903 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -45,6 +45,22 @@ struct vm_area_struct *find_dst_vma(struct mm_struct *dst_mm, return dst_vma; } +/* Check if dst_addr is outside of file's size. Must be called with ptl held. */ +static bool mfill_file_over_size(struct vm_area_struct *dst_vma, + unsigned long dst_addr) +{ + struct inode *inode; + pgoff_t offset, max_off; + + if (!dst_vma->vm_file) + return false; + + inode = dst_vma->vm_file->f_inode; + offset = linear_page_index(dst_vma, dst_addr); + max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); + return offset >= max_off; +} + /* * Install PTEs, to map dst_addr (within dst_vma) to page. * @@ -64,8 +80,6 @@ int mfill_atomic_install_pte(pmd_t *dst_pmd, bool page_in_cache = page_mapping(page); spinlock_t *ptl; struct folio *folio; - struct inode *inode; - pgoff_t offset, max_off; _dst_pte = mk_pte(page, dst_vma->vm_page_prot); _dst_pte = pte_mkdirty(_dst_pte); @@ -81,14 +95,9 @@ int mfill_atomic_install_pte(pmd_t *dst_pmd, if (!dst_pte) goto out; - if (vma_is_shmem(dst_vma)) { - /* serialize against truncate with the page table lock */ - inode = dst_vma->vm_file->f_inode; - offset = linear_page_index(dst_vma, dst_addr); - max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); + if (mfill_file_over_size(dst_vma, dst_addr)) { ret = -EFAULT; - if (unlikely(offset >= max_off)) - goto out_unlock; + goto out_unlock; } ret = -EEXIST; @@ -211,8 +220,6 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, pte_t _dst_pte, *dst_pte; spinlock_t *ptl; int ret; - pgoff_t offset, max_off; - struct inode *inode; _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), dst_vma->vm_page_prot)); @@ -220,14 +227,9 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, dst_pte = pte_offset_map_lock(dst_vma->vm_mm, dst_pmd, dst_addr, &ptl); if (!dst_pte) goto out; - if (dst_vma->vm_file) { - /* the shmem MAP_PRIVATE case requires checking the i_size */ - inode = dst_vma->vm_file->f_inode; - offset = linear_page_index(dst_vma, dst_addr); - max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); + if (mfill_file_over_size(dst_vma, dst_addr)) { ret = -EFAULT; - if (unlikely(offset >= max_off)) - goto out_unlock; + goto out_unlock; } ret = -EEXIST; if (!pte_none(ptep_get(dst_pte)))