From patchwork Tue Jun 25 11:44:15 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: 13710936 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FB8515A86D; Tue, 25 Jun 2024 11:44:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.161 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719315896; cv=none; b=oT12BVsWfliGQz1SKQ3Xm3M87KKUufNDYq+go0OGx4L/W0bG0HKvP2ukb3fYR5e5MFnAdxCBWcug17vFx61t0Xr5JzbERSWlISdBdCf1Ct6LzAkGSH2e7ce0ANbMPPTRJY2HWpm/abwuiW6rqRgPA5MZj/6DhWC8OZGbZYLP//s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719315896; c=relaxed/simple; bh=IqOkTBTNGwELslnaHk/2Z8/9HIdgaCcbk+8pyiAPzS0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U9AFXDVpZw0q/WbkDThy3JUAK1PynvN65B/VWS2JdbncDUjtCErziDEpYzlwbU3ncP9AJs2lVpbmtHLGSrRY6EuFDxmaFYo/c0/snN5/0M6m5MTR3qF2Sru7M9SFUfGDoMcHb8eSAwYxtqr+ZomwK4X7A2wfoZrJv9y36KOsy/M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pankajraghav.com; spf=pass smtp.mailfrom=pankajraghav.com; dkim=pass (2048-bit key) header.d=pankajraghav.com header.i=@pankajraghav.com header.b=Gl9MDuRF; arc=none smtp.client-ip=80.241.56.161 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pankajraghav.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pankajraghav.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pankajraghav.com header.i=@pankajraghav.com header.b="Gl9MDuRF" Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.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-103.mailbox.org (Postfix) with ESMTPS id 4W7jg11qg5z9ssB; Tue, 25 Jun 2024 13:44:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1719315885; 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=B3OUZbSfJC/PeNvlGf0eSPPicsSbDeYTsWtl9oDbqvs=; b=Gl9MDuRFaJeRCemR22+awf7zPbIO1zOHLEr3PDpxFJOrDCUo2YJ/Ov7jr3R1FYm7kHzA/i ylPymM9EuDr0WA7A13RxyuRFuX3oFA4VGXswbFQG1UAHgTYyAO6nWwm1Nw+lVoSDPu3Nf2 kURCO/Stb/5fitNOlIbzweXb53CUKT3A13qBhEnV+309WWZ/Oh/mwDEQbvecaDaWecIVb1 4gYZO4fOiTbN5An7f98ORIy5vTSGX5gak03FVnLbMV0eQo4L7J8jJh2XXYuGlO0B+NgLC3 JAS9lG21cK+5PW7LK1XEUwF2I/No07K1phD3V2vT7vmXG7TmqQGwbx0qPfhyzA== 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: linux-kernel@vger.kernel.org, yang@os.amperecomputing.com, 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, hch@lst.de, Zi Yan Subject: [PATCH v8 05/10] filemap: cap PTE range to be created to allowed zero fill in folio_map_range() Date: Tue, 25 Jun 2024 11:44:15 +0000 Message-ID: <20240625114420.719014-6-kernel@pankajraghav.com> In-Reply-To: <20240625114420.719014-1-kernel@pankajraghav.com> References: <20240625114420.719014-1-kernel@pankajraghav.com> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 8eafbd4a4d0c..56ff1d936aa8 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3612,7 +3612,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; @@ -3638,6 +3638,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;