Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp104261pxj; Tue, 1 Jun 2021 16:24:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzfrODwWd44UA/blSypfoIbkHVdMUbavFBnS0aEU9fEJWqtFOmirUIO2lYSmD8trE1SYsbY X-Received: by 2002:a92:c689:: with SMTP id o9mr15563671ilg.297.1622589856563; Tue, 01 Jun 2021 16:24:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622589856; cv=none; d=google.com; s=arc-20160816; b=zqt+9U2zs7O8dN8MyHHIejo+OvK0UIfXleFAJGLDxLbbKy2EBMLMWEidsS19cMyngp MdrToMr5Pf9MIlqLrvJHLe6H69+Hut2CuITM2ZSv0J8t80dsVQouxcogy3R4Etrco5pc 3QNFW6fyjxF87yPo0pjsWag9maixlnr2yY4rWU9W/KaWvTiuWbrGhhEb2Qyib1OA58LY y6VcGrddkkGt9nFEWJIsySjaa2cQlQ+0aTXF70+6Wit4Il0Yjd5F4M9LHD5a+uV6svHT TfRb6PtChoW+zlO8fAu1j7FmHIB1us/u6ZsGHyw7Q/xUeG6v1whFQRqYZs1Ugab5pvB/ 9QwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=nKrLm39hD8Qz/W2AYpL8BsAAsYKXpXbJe1piCcLw98o=; b=Ux69x6iuanfX0xzM/OpOnt78wEitYyf6/d1LEvyxBLlYtLAvEgVndqP6Q8GKyd65RH WJqd2PJ1WZcDw4y6vzUa1wqNeCfD/Q9ahD9OZkfur86Wv8PYzoC6FxY9ri/K8SwebDO5 xR0ZGclZMV/50/+y9RknF7mvI8oY/tIN1UhyZ6ce1aeozRjGbo/y5Oadqr8BrCbdy7Z5 QUUNT4ME4OzMJv2iorjwzITq0RNTOb4W77jZd1ha74YFtHaDTJohlUnWbsFavSpRd0DU gNFc25axpp8kFU7+CEbHoBE/OrwDCQg3GSbG9S5x02A5F4zVDQnC/+GdpjKrlGkJIfCT X7rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ftu0zJj3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q3si21736037ilu.88.2021.06.01.16.24.03; Tue, 01 Jun 2021 16:24:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ftu0zJj3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235179AbhFAXYV (ORCPT + 99 others); Tue, 1 Jun 2021 19:24:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:46308 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235137AbhFAXYR (ORCPT ); Tue, 1 Jun 2021 19:24:17 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 69192613BC; Tue, 1 Jun 2021 23:22:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1622589755; bh=d6j6EDGkhnc+GxiEmiU29Lm/NCKNEqKbXTPVfNJiuuM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ftu0zJj3/weUgRsC8CSXzsmqeuhN2FdlfFbM5ew8+E1xgWmbSx50aC2TeDff/fo3r rXK/4YMRtWDpxZ50xfhXMJM3yljE5igxQ6kcRa58MomH/F5L3TNhL/t884dtSmTIo+ 09ytxS7PJVtcn5dj3yw4PyjcYw2tGvkIVW8OQ6ZWZD48lgYqOpLsFdXQDLWjTH9P85 7oHZbbIgqE5qoxr3gzF+FM4CckTHXQRscojVBqXI2wgPvH8I2utWPqJdlz5LQ+N7pT TM+e02mUxQEZMPM0EBBBYG3z5XlVgjf55p6Q/I8nuq6tynxWktFEIPZ5FcbLpVVNog E77bBQpTQwZTw== From: Ming Lin To: Linus Torvalds , Hugh Dickins , Simon Ser Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Ming Lin Subject: [PATCH 2/2] mm: adds NOSIGBUS extension for out-of-band shmem read Date: Tue, 1 Jun 2021 16:22:33 -0700 Message-Id: <1622589753-9206-3-git-send-email-mlin@kernel.org> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1622589753-9206-1-git-send-email-mlin@kernel.org> References: <1622589753-9206-1-git-send-email-mlin@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Adds new flag MAP_NOSIGBUS of mmap() to specify the behavior of "don't SIGBUS on read beyond i_size". This flag is only allowed for read only shmem mapping. If you use MAP_NOSIGBUS, and you access pages that don't have a backing store, you will get zero pages, and they will NOT BE SYNCHRONIZED with the backing store possibly later being updated. Any user that uses MAP_NOSIGBUS had better just accept that it's not compatible with expanding the shmem backing store later. Signed-off-by: Ming Lin --- include/linux/mm.h | 2 ++ include/linux/mman.h | 1 + include/uapi/asm-generic/mman-common.h | 1 + mm/mmap.c | 3 +++ mm/shmem.c | 17 ++++++++++++++++- 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index e9d67bc..5d0e0dc 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -373,6 +373,8 @@ int __add_to_page_cache_locked(struct page *page, struct address_space *mapping, # define VM_UFFD_MINOR VM_NONE #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ +#define VM_NOSIGBUS VM_FLAGS_BIT(38) /* Do not SIGBUS on out-of-band shmem read */ + /* Bits set in the VMA until the stack is in its final location */ #define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ) diff --git a/include/linux/mman.h b/include/linux/mman.h index b2cbae9..c966b08 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -154,6 +154,7 @@ static inline bool arch_validate_flags(unsigned long flags) _calc_vm_trans(flags, MAP_DENYWRITE, VM_DENYWRITE ) | _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) | _calc_vm_trans(flags, MAP_SYNC, VM_SYNC ) | + _calc_vm_trans(flags, MAP_NOSIGBUS, VM_NOSIGBUS ) | arch_calc_vm_flag_bits(flags); } diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h index f94f65d..55f4be0 100644 --- a/include/uapi/asm-generic/mman-common.h +++ b/include/uapi/asm-generic/mman-common.h @@ -29,6 +29,7 @@ #define MAP_HUGETLB 0x040000 /* create a huge page mapping */ #define MAP_SYNC 0x080000 /* perform synchronous page faults for the mapping */ #define MAP_FIXED_NOREPLACE 0x100000 /* MAP_FIXED which doesn't unmap underlying mapping */ +#define MAP_NOSIGBUS 0x200000 /* do not SIGBUS on out-of-band shmem read */ #define MAP_UNINITIALIZED 0x4000000 /* For anonymous mmap, memory could be * uninitialized */ diff --git a/mm/mmap.c b/mm/mmap.c index 096bba4..69cd856 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1419,6 +1419,9 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (!len) return -EINVAL; + if ((flags & MAP_NOSIGBUS) && ((prot & PROT_WRITE) || !shmem_file(file))) + return -EINVAL; + /* * Does the application expect PROT_READ to imply PROT_EXEC? * diff --git a/mm/shmem.c b/mm/shmem.c index 5d46611..5d15b08 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1812,7 +1812,22 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, repeat: if (sgp <= SGP_CACHE && ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { - return -EINVAL; + if (!vma || !(vma->vm_flags & VM_NOSIGBUS)) + return -EINVAL; + + vma->vm_flags |= VM_MIXEDMAP; + /* + * Get zero page for MAP_NOSIGBUS mapping, which isn't + * coherent wrt shmem contents that are expanded and + * filled in later. + */ + error = vm_insert_page(vma, (unsigned long)vmf->address, + ZERO_PAGE(0)); + if (error) + return error; + + *fault_type = VM_FAULT_NOPAGE; + return 0; } sbinfo = SHMEM_SB(inode->i_sb); -- 1.8.3.1