From patchwork Fri Jul 26 11:59:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pankaj Raghav (Samsung)" X-Patchwork-Id: 13742704 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 8DDCCC3DA49 for ; Fri, 26 Jul 2024 12:00:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 877656B00AE; Fri, 26 Jul 2024 08:00:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 800476B00AF; Fri, 26 Jul 2024 08:00:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 62B416B00B0; Fri, 26 Jul 2024 08:00:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 3D2CE6B00AE for ; Fri, 26 Jul 2024 08:00:33 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C18B1141812 for ; Fri, 26 Jul 2024 12:00:32 +0000 (UTC) X-FDA: 82381761504.19.7FDD18B Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by imf14.hostedemail.com (Postfix) with ESMTP id E7E34100025 for ; Fri, 26 Jul 2024 12:00:30 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=S8zUd4b9; spf=pass (imf14.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.152 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com; dmarc=pass (policy=quarantine) header.from=pankajraghav.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721995180; 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=xbld/pizLMEJfrPYBqwQ81gjcSKIsZj5n7TDu8kDUY0=; b=Hj5ecUL4YMcAqWliT5RifxpD5abT425NjgrVMOme/U19gXepzj1shWPRWZHnOVBhP8ZRxa P/NLl4hvemso4RiE9as9CpPiW+q8++qTq3ciN30KFwY0n0oxYWOy/3MXS2NktxV703zOZb vJjR/cDoRfLBEFyIUV3TK9pXD1SxEGY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721995180; a=rsa-sha256; cv=none; b=iHbqB4lc8mEOTOpLIgAd2l8+PVyjfMXlvo+jbQJypV71FZnzwjdLtgrEqKO4ecib66mrD9 vGFWxqB76d2YlvzoT9flG/iUOq6+il9dJ9a3i4HcTESqD95XL5Tas99gd9gv2sehGq1TVu COkB9xiWOBlkG3+WON+kzual+1USmM0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=S8zUd4b9; spf=pass (imf14.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.152 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com; dmarc=pass (policy=quarantine) header.from=pankajraghav.com Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4WVmXq3GNbz9t61; Fri, 26 Jul 2024 14:00:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1721995227; 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=xbld/pizLMEJfrPYBqwQ81gjcSKIsZj5n7TDu8kDUY0=; b=S8zUd4b9nleV0856IBngGbMzeTPCVO64N152k7I1ao1Q2BDdzmiasjo0YlYJL+9SwYI8wy M3i5xAWMFzqlDmscQeIVxcpJoPByQaD3IXZmK52GN545FLNaNDQk4H0L3lDlQmM34Top1e N+n3N6SGuiX+swJCDr46SdKD5KUZjO/Te2HdnlGFz7755gYEr7X81lnN89zn/4krjtDCOl 7RtCx0LhczAUhqqUN6NtYT41BVWmzZU3BVrJiIeos7oOhlIMWJ2QbS4fWd/R8bobyqRKkp 2JDiKib9LAcrI3iZJ20YkPeq9vc4Yj4C8T68oXLWo8XigGJ23I13gZ2XhjpceA== From: "Pankaj Raghav (Samsung)" To: david@fromorbit.com, willy@infradead.org, chandan.babu@oracle.com, djwong@kernel.org, brauner@kernel.org, akpm@linux-foundation.org Cc: yang@os.amperecomputing.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, john.g.garry@oracle.com, linux-fsdevel@vger.kernel.org, hare@suse.de, p.raghav@samsung.com, mcgrof@kernel.org, gost.dev@samsung.com, cl@os.amperecomputing.com, linux-xfs@vger.kernel.org, kernel@pankajraghav.com, ryan.roberts@arm.com, hch@lst.de, Zi Yan Subject: [PATCH v11 05/10] filemap: cap PTE range to be created to allowed zero fill in folio_map_range() Date: Fri, 26 Jul 2024 13:59:51 +0200 Message-ID: <20240726115956.643538-6-kernel@pankajraghav.com> In-Reply-To: <20240726115956.643538-1-kernel@pankajraghav.com> References: <20240726115956.643538-1-kernel@pankajraghav.com> MIME-Version: 1.0 X-Stat-Signature: mxwjjmo1wju1j5uno1ujh3i1iqr6mnnd X-Rspamd-Queue-Id: E7E34100025 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1721995230-828509 X-HE-Meta: U2FsdGVkX1+GcitfpS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ynJHEhJNQHYE+R77jgE5c4zfBszBai4v5v+qCxfnpJKrF+YwgDtWFCmG5o+Gfb/vBdk0I2NmnnDBeBSaWVHOqCSMMv1mIzwUYENe57Xy8N+pbge4Zi8bIZYrHey1uAJowNC/uXgIAi4UQHXMgMBGZaBHXCw8YvYUnq1DuFMbU+l/ilBXfTu3iSd4A9587OfFj/Nyxw2+t1j4/BDzvddHw1+kwa+IEqdG9/vYv1Cbq/ID1r/ROCycOJRvOBfjO/MPWSbEhtObgyyLukWMH0CLR4HD/IXlFAZn9PHJnHuXE8Ov1ww2hjzmZH5e8i8GdAdNxJJVk+FM= 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: Pankaj Raghav Usually the page cache does not extend beyond the size of the inode, therefore, no PTEs are created for folios that extend beyond the size. But with LBS support, we might extend page cache beyond the size of the inode as we need to guarantee folios of minimum order. While doing a read, do_fault_around() can create PTEs for pages that lie beyond the EOF leading to incorrect error return when accessing a page beyond the mapped file. Cap the PTE range to be created for the page cache up to the end of file(EOF) in filemap_map_pages() so that return error codes are consistent with POSIX[1] for LBS configurations. generic/749(currently in xfstest-dev patches-in-queue branch [0]) has been created to trigger this edge case. This also fixes generic/749 for tmpfs with huge=always on systems with 4k base page size. [0] https://lore.kernel.org/all/20240615002935.1033031-3-mcgrof@kernel.org/ [1](from mmap(2)) SIGBUS Attempted access to a page of the buffer that lies beyond the end of the mapped file. For an explanation of the treatment of the bytes in the page that corresponds to the end of a mapped file that is not a multiple of the page size, see NOTES. Signed-off-by: Luis Chamberlain Signed-off-by: Pankaj Raghav Reviewed-by: Hannes Reinecke Reviewed-by: Matthew Wilcox (Oracle) Reviewed-by: Darrick J. Wong --- mm/filemap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/filemap.c b/mm/filemap.c index d27e9ac54309d..d322109274532 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3608,7 +3608,7 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, struct vm_area_struct *vma = vmf->vma; struct file *file = vma->vm_file; struct address_space *mapping = file->f_mapping; - pgoff_t last_pgoff = start_pgoff; + pgoff_t file_end, last_pgoff = start_pgoff; unsigned long addr; XA_STATE(xas, &mapping->i_pages, start_pgoff); struct folio *folio; @@ -3634,6 +3634,10 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, goto out; } + file_end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE) - 1; + if (end_pgoff > file_end) + end_pgoff = file_end; + folio_type = mm_counter_file(folio); do { unsigned long end;