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: 116872 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp2880026vqx; Thu, 6 Jul 2023 15:52:42 -0700 (PDT) X-Google-Smtp-Source: APBJJlHVmzOF1x5IbyOlaNrmAY6qTCE3Dc7JVM3oWwK6YJ8iBKNLX9XkShR913tq7sA7mF7qQzYQ X-Received: by 2002:a05:6358:906:b0:134:c984:ab7e with SMTP id r6-20020a056358090600b00134c984ab7emr3014978rwi.0.1688683962588; Thu, 06 Jul 2023 15:52:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688683962; cv=none; d=google.com; s=arc-20160816; b=fGIe3fxaOgbgXJUylwMW63Faik5q8EGG71QPatIL6kOm9sbEy8o5rGhBvyTWCTIOkE drZrqvpKv1tf8NdENLo21PEPzvz+8dPS0KKaZgaPyF9tItzo2ecYnS8+wKb0nvv9uCJm FYz9xnlJYVQK9G+dQLyRhHiZ7IFV736WUOCWG5zoCCFZitZgGWdP82grFh9ObZSzOMhw YgC6vpeA2A09/WFLWu5/Jw+DoJ7u2WXeovaWZtaWiRqdS0i6wuDsv01wk5yH5oA9UtkM NwdWTt/LzuNUvulLd7vH2Q8DiYEO5j8X8sNSFq6Z3zDzzVXwG4xJk1Cfe+3Jon1fI29D zdeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=zDsVvAgbqAnX6PMRHG2ZwgnBlt3+9ofsZz4Jdu5BLSk=; fh=Opjbx899L+35XA6SNQJRoMm3wFpvelEj3IooMxkZLjI=; b=Q/y+ZODtrLZcm6gn2YdzSZeVM7wC5hiwjfoEeFpeVtNZ+HNml//cPiXzWsNWZDr2FR VXfKUj6AWLppgBAbBHP3Y8a/X5ujPOHaSvQeaXwQFwsQArTttl8psqFDYfCPdCszWdUs 3t6CAP1fn2LxQSsWb4++Hk8kPUNasTTJYOHlCM1gbLNLDz4jMd1rwhkDb8jzDvuhPmDg ZhauTGtuSwtlqtYqjt60N6/OrkduIZmCSDm9xdZiJQG9n4Z2dEizz+DVSNWS6e4RI5Mi k+7a+cW6WoBoKTHIAP6KuDMfIepqx18moOf63aIl9Isl5dBE2irdP9UjlRBUM/ZIXqa6 UDyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=HaacAumE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l185-20020a6388c2000000b005533c53f5cesi2334130pgd.27.2023.07.06.15.52.27; Thu, 06 Jul 2023 15:52:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=HaacAumE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231976AbjGFWvP (ORCPT + 99 others); Thu, 6 Jul 2023 18:51:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231693AbjGFWu5 (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 47AE61992 for ; Thu, 6 Jul 2023 15:50:56 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c0f35579901so1352097276.0 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=ANSRWbu16PY2Om8N5aoEhM5j/zRJHHRdtPdaeqcwwEWDWteXLrCCDlS2UvyttGlHBF yINWbG+J//LRM9ktVgWVkKiwJ23CXjq7vR85qDvKcRlGV+q3akb/GQlEEno6d1LDvcLE xHo+knbhfmYsyRc5VWasRJpLagAu3s/A8Ccgeg5FvQZ0R2a4DIHQPGi/OK7AdNLngU3z eeC4M503SOgiCqeJKtEIIU684J4agf9samiVtuitD5nvtIVpkzcjntaSRUWbZ+7y+QTO gXXUeKcjPS2ndTOCr9VYsB0wOd5frN8bgBJz3fa0S5JgFGpYrhqSi3rSIJ7tTsUG/d5M rTtg== X-Gm-Message-State: ABy/qLaxsd1ApmFjLIYhNi60Sm7M7IYt9xrRy12Ks3oDCMPJ9teOe5xQ y4wPTrh2I65uP/0WzEKJLCtCgazCmxJsV/jTPKy+ 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770713474628218100?= X-GMAIL-MSGID: =?utf-8?q?1770713474628218100?= 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 Reviewed-by: Peter Xu --- 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)))