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: 13304273 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 85A84C0015E for ; Thu, 6 Jul 2023 22:50:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B6658D0005; Thu, 6 Jul 2023 18:50:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 117D78D0002; Thu, 6 Jul 2023 18:50:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EAD938D0005; Thu, 6 Jul 2023 18:50:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D6A658D0002 for ; Thu, 6 Jul 2023 18:50:58 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 584DAA0CB9 for ; Thu, 6 Jul 2023 22:50:58 +0000 (UTC) X-FDA: 80982683796.03.3846E3C Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 7D48E20018 for ; Thu, 6 Jul 2023 22:50:56 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=HaacAumE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3T0WnZA0KCKICZGNTCUOWUUGPIQQING.EQONKPWZ-OOMXCEM.QTI@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3T0WnZA0KCKICZGNTCUOWUUGPIQQING.EQONKPWZ-OOMXCEM.QTI@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688683856; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zDsVvAgbqAnX6PMRHG2ZwgnBlt3+9ofsZz4Jdu5BLSk=; b=S99CrMxnHJygDRS9H2G+aftHCcAoixfEdt4FM6YYKyHLuYJfmSCJbeUOD1WLRX0GuO3xXk OVEAGzPDHJInJUcVqL0B9gFqMzpAI4hHv+rWOISMOn446NEkTwVlDY3hngPJRFHsjEuhzu RdIBj2o869S6A039AWIhYFlBowXxRP8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=HaacAumE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3T0WnZA0KCKICZGNTCUOWUUGPIQQING.EQONKPWZ-OOMXCEM.QTI@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3T0WnZA0KCKICZGNTCUOWUUGPIQQING.EQONKPWZ-OOMXCEM.QTI@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688683856; a=rsa-sha256; cv=none; b=LGsmtspgeX1QPvDQ+tilT5yjeUSwGpR1pWfKnxyhjsrwGDGcoc64QzgY77xajFGqn0WNZs 2yV+HqFvHJ8O0n3sg38EGRJpOZRQcx0nW0XHiKgAqBCNMfjBtSKr4iFnynqDh9LfuxU7XX aSIygAc1hIRwuXy+hsbUK7HmYpVYjCM= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-c0f35579901so1352098276.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=CIaMHd6VdQQ3n4H0xq3nnnt7p5WmbnIF2Wd/VKXOENeLCMi/x8hyCZpqIcH4Tg1Kkm nida6D2bFMreMCziwGQwqHJQqfHC532OJDdFIVLiFMaPx3LVzgIMSB2Zvt0u2A73THA6 ZUdtoLE/7FXgbQVCCBfDcPTHL6YJNRUPpGrVIePUw8au280jABu7VMm5pgo/ZH7qq5NH nlYVZKwgmXKrIbg6jlEgEeGBsKSzGpKguWHJSnpAQxd7GsvDxsg767VPdjQXPTFpLNXD OSjWG3N3j8FS5LalrN2eQ1jQYthC8eo0pKCSVQiLiHdK6TSpMo7MXhFuwqR0SZBLaiUw f4PQ== X-Gm-Message-State: ABy/qLacNyzWfWEi63gZ9CE/IxqmIky2gquSjN5PeD0GklBdMW/itL46 /G9zHnzq93aoJovY9KJGf7v83crNiN2yhtakMLCo 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 X-Rspamd-Queue-Id: 7D48E20018 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: u9jypqfbhbzp88xx3g3i7osyjoc4inqm X-HE-Tag: 1688683856-823135 X-HE-Meta: U2FsdGVk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Yck/22D3a3lTgcsoAkw8uwHdnXq9je/X87uYxvVmTncuQJv6EndnrMBIJZAjtl5xcuAwiHRJeSm02Ry79HcPgyP9vFCCSb7WT/Be/AiY41Z8yhWQ7MHUK8xpOfBl8UR6/mWEXDZWjFumRY7lqLUwCkGftiCKzhTCPzBe2axGwTbwkAKhVjjvEblQRUFMv6aaV3eZjmAVewAr2SN234r4iqx8E6p4Rdjrx5Zpfg0OHvwW6El9uZnt72Ei1zTZiuvmtQiZtc6VB4LeIqJaJ5lqBm/rycEX+4/30elJ1oTne501okjybo4aOhOHYoN9O2yLa+uY50XrqcYYto/+dlyNJxcb9B/mqI8ScJSFpDErNhul8EyJd54reOJ+4I3kL3shc1+ZQ2/EdWAlT1V9lZlHd7IO7D0uq6Ep/FKTn0TwoF8HfZmWbagePR0xjbAXnVymV3qjVbJYd66e6JctWiegiQsmW7BqOQKK1fjNpL5ojP6ddSeiuRiUqGfEm0buCf/wt4mIVt17pUwvdcNWXSCu+05RQLhpkSvsP9rJ5RmTwjQJLDjSkOvz/oikZ15 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: 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)))