From patchwork Thu Oct 12 17:04:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13419484 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 5BE0FCDB46E for ; Thu, 12 Oct 2023 17:04:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E6EE08D0138; Thu, 12 Oct 2023 13:04:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E1E478D0002; Thu, 12 Oct 2023 13:04:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC0B28D0138; Thu, 12 Oct 2023 13:04:48 -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 B897A8D0002 for ; Thu, 12 Oct 2023 13:04:48 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8E6EBB5254 for ; Thu, 12 Oct 2023 17:04:48 +0000 (UTC) X-FDA: 81337433856.10.928DC8A Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) by imf16.hostedemail.com (Postfix) with ESMTP id 786A71800CD for ; Thu, 12 Oct 2023 17:04:36 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YPIFZHV4; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697130276; 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=s8vXm3Rb5eaL+HF5MglErGcxxNG7EAs28QhToMLXn+Y=; b=5PmnjMQ9MWkNSCZeHutHwdeU9mA/eQZMJboiEZL9BDxZVzjSO6E0w9PxGuQ88KVVnpNTon wczGeWP7vyxVYFKrI9K8mi/p8davvAA+pKSAU2zN1jpfJVF8asuYdwuhtd62MFQVxQaWZU +kl17i8g4vfnVjr5U6wwdi2vV+/7SuU= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YPIFZHV4; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697130276; a=rsa-sha256; cv=none; b=LMVo+mkAtBHVnOkfvlkLtQrB+bFyNWvaTNXd3bBPqEZ1SrnzPojcatbp2+loh5bOebKaWJ Qk0CmBzDacFNVnPLVRVVKf4NHRtDuRGirs1lde2XSglU22rzInKB5oAguFwzH+0Z7aXwkK WLOF7K4UFoHeGfaGhJEpNMsolBB0BPs= Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-40572aeb73cso12923635e9.3 for ; Thu, 12 Oct 2023 10:04:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697130274; x=1697735074; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=s8vXm3Rb5eaL+HF5MglErGcxxNG7EAs28QhToMLXn+Y=; b=YPIFZHV48IRcsCnYQUBVe8h1fW/Up+DOs0C8L8nOP+dVaPg0R1zHJ+wj+XpDOA8A4O iZXZLz+ymr6xUy/PlPe8Rctzcxy0L5lB7YnZfHeMzzg0Vp0jLtBNjGLjutZLc6OOJkt6 biFXoJdGiYBsNxMx2U/pwwO+URsfyk15N84wb6EWUh+uGXfFT1sHjW/CwuhM3HD/jcmB oaKDoR9W8wT5HVP2X8/aHz6EfMxHmENsZ0U6IxLE23D2CLfwRP9jPkK4JpP8nIBUjzOv w+T4uXSoGhLhUmEmTss73uXnsbGCTrjF5Hv3hz1025UmsKnjqjBlercDJaN8C4iFTR2s OMAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697130274; x=1697735074; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s8vXm3Rb5eaL+HF5MglErGcxxNG7EAs28QhToMLXn+Y=; b=lQIuGfW3etkItf7g2VHV/PrAXa4HAsgq8YtArSmDaQf5ik52tFRyij3Rb/9tr7npGp /VKo5hn8p/a3tu7sCt8rp8+z0IKTysOrGCbvghhBTjfYK7IVedlWfbwsYI1GyjJc0WIa INxkbOe6Xeonur0e8AIkErei89FWSXgzW8QwMO5Zi3C/QSBZ19cUtCsfNsbz2Zk/2zm/ YltZPnr+N0PcpueZUI/SFBD06/soEea4lX4vuoh9wevTs3vUazJgAPyBHddeZqEYvwcP COTvc8ImZECRtbLsTm69jpa8e6QXW+uwaPWxRRPDe875Yy0Q6h/Ai+Rq4J2Aq/FamLgd 7/qQ== X-Gm-Message-State: AOJu0Yxw/JfECqCWI2jHaxpxoq3+r47RlR/A4TiG/426YBn8i2Fo1wB0 +OSlunIqGzhrZO233Gj36bDLIzIN25s= X-Google-Smtp-Source: AGHT+IG1WtI4624aFb3uDRoHHKgnKF/CRfbpjPSiwsKx3X2z2FPwp37I71ARseeuIXbEk1RVZb30MA== X-Received: by 2002:a5d:458f:0:b0:321:6ff5:9256 with SMTP id p15-20020a5d458f000000b003216ff59256mr19911164wrq.58.1697130274280; Thu, 12 Oct 2023 10:04:34 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id h16-20020adffd50000000b003197869bcd7sm18875418wrs.13.2023.10.12.10.04.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 10:04:33 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Matthew Wilcox , Hugh Dickins , Andy Lutomirski , Jan Kara , linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v4 1/3] mm: drop the assumption that VM_SHARED always implies writable Date: Thu, 12 Oct 2023 18:04:28 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 4ppbubyinmt95ke75dwxr5pung6uu8ip X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 786A71800CD X-HE-Tag: 1697130276-268442 X-HE-Meta: U2FsdGVkX1+U0mPeInX6Y6s7Pxdk3zsFIodhDojngmNM27cOLK5R0+JiPZ6Tnnvy9VJ5EKkRLAtSWzHnN4HrgxOn4mXuFsGt2RLSSZP1VUqT7dp1pVSz4bVXnk2SSGu3FIldq3IRpOSkhQJXV0UMujZgwsJ5o1BYr5GgOAdDFLz0TFt5cJNWz1sWh4Bc9GbsT7FK2P/ECeHZwvyY6umX1rhzCHs9wyt4UgE/m3fIgiir1CmgB77iUKvjw3ENio8pfe7+4/o3HlGlfCUkIvKuLn067x8xCAQ2JZBBCTVlOrGC2r/6Xw7gl6hLAIc5AZPd1n+38MeZo+BH8hYzlJ7WiWDyPNa5g7mYFHcHZdV/OGB4PjGp+UHER8y7SiRS7SeNmKjWeaWvC+2LwBXlFVF0tKn/bbhyuNVxOzcn3I4iFlIM15PXjfEA5DASowEHGBL0vFiepSvnZw+xAJd76KCsBQpF2x9cFiUjf4IqJeFmgLzJJARogy8uaBFS6WNHioVMcCl/OoRwODxqFBZDCPhw+3c2OWdacC6XRPpoYp/emHMatPqmu9HclwjYVrp9DFyQ2K+SgDxr7J4jHdGAB0/CTEspNuQdSsFJxMweW0tC8hadmvoBMEAENIrent8W3udgmy+Eh71afyggxaR2P+KbY9Hm0So70R+wmmqcpR4/BZNisda59hh7Vz1yev8Yr8LTVKJCffJ+HbpUlO7zjyvdcASvBDAYa41nkWeGRg7vjeuDOPCXf17lNWp0x49zOK784DyiAqKv+uB+yP8AS+Udo1gCwQcCknIjNtdE0H6UkFu3BysIo4YcMHmBOxEfFpKRfH5zTof/1USoyK70z+qgD8e55rfgQa+7ydMCNaOzbL4R6vLJ4RAJqYqxg/wF3tPamFz+uknjrIzhWBXI3ic23zkEnwaS19xJDK4DADpDfPZfiD2u19dZkO6uki61kvzBilaHQLzpNMzKcqd5Fe+ gLTk1yf9 eD27/lnTR1esUxvRm97fNxucbXz/BWDfBkYI4EcCP0Amw9/WEm8DfpKosP+bxUQ5C83gpWPaFzDhqX/FFgXjtfF8zndA4/W933C9wF4KCEDjLNnT8/qgYxQ3gyLPgJ62I4mMaTXo4kZzvwUmooaS5eXyoA+rBPg+ZHIgzGxpDPqmp4ylO9kkAD6YCXPDXO4HujcLlYhuzf+SrlPZ4caAURFeKOz0wI/OaQzvm6u3dhU3pcIDoaoHVq/aU3Awlz4ck3ay3Aszh1imFQAyaf3HBygHYYOSLXKekwWFI/yO6U5sub8g9fX9ZbItUOUmwu2p8FymCF3mnO7cFZ3OdFGHWrxDsl3Ppk2dfsiKdsTEL5iykHlfO3pmhKcrElGmCR0F7D8ENgxByeB5DLupjKiB1oirKoDopfAlMPdjd1gLgZik05/xYan6HPJoFyEyBKcA7XUP2iMzCZ2nqOWQSsGW6ug7VykEU1RZSXqEmF6wb2fHefvbL5eF0u2vIG5THVf1E9USBruRJzR5DMEHgbGya9CTJdZmH76tyDP7D 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: There is a general assumption that VMAs with the VM_SHARED flag set are writable. If the VM_MAYWRITE flag is not set, then this is simply not the case. Update those checks which affect the struct address_space->i_mmap_writable field to explicitly test for this by introducing [vma_]is_shared_maywrite() helper functions. This remains entirely conservative, as the lack of VM_MAYWRITE guarantees that the VMA cannot be written to. Suggested-by: Andy Lutomirski Signed-off-by: Lorenzo Stoakes --- include/linux/fs.h | 4 ++-- include/linux/mm.h | 11 +++++++++++ kernel/fork.c | 2 +- mm/filemap.c | 2 +- mm/madvise.c | 2 +- mm/mmap.c | 12 ++++++------ 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index 92a9c6157de1..e9c03fb00d5c 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -454,7 +454,7 @@ extern const struct address_space_operations empty_aops; * It is also used to block modification of page cache contents through * memory mappings. * @gfp_mask: Memory allocation flags to use for allocating pages. - * @i_mmap_writable: Number of VM_SHARED mappings. + * @i_mmap_writable: Number of VM_SHARED, VM_MAYWRITE mappings. * @nr_thps: Number of THPs in the pagecache (non-shmem only). * @i_mmap: Tree of private and shared mappings. * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable. @@ -557,7 +557,7 @@ static inline int mapping_mapped(struct address_space *mapping) /* * Might pages of this file have been modified in userspace? - * Note that i_mmap_writable counts all VM_SHARED vmas: do_mmap + * Note that i_mmap_writable counts all VM_SHARED, VM_MAYWRITE vmas: do_mmap * marks vma as VM_SHARED if it is shared, and the file was opened for * writing i.e. vma may be mprotected writable even if now readonly. * diff --git a/include/linux/mm.h b/include/linux/mm.h index 74d7547ffb70..bae234d18d81 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -937,6 +937,17 @@ static inline bool vma_is_accessible(struct vm_area_struct *vma) return vma->vm_flags & VM_ACCESS_FLAGS; } +static inline bool is_shared_maywrite(vm_flags_t vm_flags) +{ + return (vm_flags & (VM_SHARED | VM_MAYWRITE)) == + (VM_SHARED | VM_MAYWRITE); +} + +static inline bool vma_is_shared_maywrite(struct vm_area_struct *vma) +{ + return is_shared_maywrite(vma->vm_flags); +} + static inline struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max) { diff --git a/kernel/fork.c b/kernel/fork.c index e45a4457ba83..1e6c656e0857 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -733,7 +733,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, get_file(file); i_mmap_lock_write(mapping); - if (tmp->vm_flags & VM_SHARED) + if (vma_is_shared_maywrite(tmp)) mapping_allow_writable(mapping); flush_dcache_mmap_lock(mapping); /* insert tmp into the share list, just after mpnt */ diff --git a/mm/filemap.c b/mm/filemap.c index 9ef49255f1a5..9710f43a89ac 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3618,7 +3618,7 @@ int generic_file_mmap(struct file *file, struct vm_area_struct *vma) */ int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) { - if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) + if (vma_is_shared_maywrite(vma)) return -EINVAL; return generic_file_mmap(file, vma); } diff --git a/mm/madvise.c b/mm/madvise.c index 70dafc99ff1e..6214a1ab5654 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -981,7 +981,7 @@ static long madvise_remove(struct vm_area_struct *vma, return -EINVAL; } - if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE)) + if (!vma_is_shared_maywrite(vma)) return -EACCES; offset = (loff_t)(start - vma->vm_start) diff --git a/mm/mmap.c b/mm/mmap.c index 3ea52451623b..0041e3631f6c 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -107,7 +107,7 @@ void vma_set_page_prot(struct vm_area_struct *vma) static void __remove_shared_vm_struct(struct vm_area_struct *vma, struct file *file, struct address_space *mapping) { - if (vma->vm_flags & VM_SHARED) + if (vma_is_shared_maywrite(vma)) mapping_unmap_writable(mapping); flush_dcache_mmap_lock(mapping); @@ -384,7 +384,7 @@ static unsigned long count_vma_pages_range(struct mm_struct *mm, static void __vma_link_file(struct vm_area_struct *vma, struct address_space *mapping) { - if (vma->vm_flags & VM_SHARED) + if (vma_is_shared_maywrite(vma)) mapping_allow_writable(mapping); flush_dcache_mmap_lock(mapping); @@ -2846,7 +2846,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_pgoff = pgoff; if (file) { - if (vm_flags & VM_SHARED) { + if (is_shared_maywrite(vm_flags)) { error = mapping_map_writable(file->f_mapping); if (error) goto free_vma; @@ -2920,7 +2920,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, mm->map_count++; if (vma->vm_file) { i_mmap_lock_write(vma->vm_file->f_mapping); - if (vma->vm_flags & VM_SHARED) + if (vma_is_shared_maywrite(vma)) mapping_allow_writable(vma->vm_file->f_mapping); flush_dcache_mmap_lock(vma->vm_file->f_mapping); @@ -2937,7 +2937,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, /* Once vma denies write, undo our temporary denial count */ unmap_writable: - if (file && vm_flags & VM_SHARED) + if (file && is_shared_maywrite(vm_flags)) mapping_unmap_writable(file->f_mapping); file = vma->vm_file; ksm_add_vma(vma); @@ -2985,7 +2985,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, unmap_region(mm, &vmi.mas, vma, prev, next, vma->vm_start, vma->vm_end, vma->vm_end, true); } - if (file && (vm_flags & VM_SHARED)) + if (file && is_shared_maywrite(vm_flags)) mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma); From patchwork Thu Oct 12 17:04:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13419482 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 35CC9CDB483 for ; Thu, 12 Oct 2023 17:04:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 965A78D0136; Thu, 12 Oct 2023 13:04:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C7198D0002; Thu, 12 Oct 2023 13:04:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 71A2E8D0136; Thu, 12 Oct 2023 13:04:40 -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 5C8118D0002 for ; Thu, 12 Oct 2023 13:04:40 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2808880600 for ; Thu, 12 Oct 2023 17:04:40 +0000 (UTC) X-FDA: 81337433520.10.529FFC9 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by imf25.hostedemail.com (Postfix) with ESMTP id 15484A0006 for ; Thu, 12 Oct 2023 17:04:37 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Hh8/sx6v"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697130278; 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=7LHgfYhq7AemM3CMY63djM9UPMuGnWJ3Ca40aCfcDv0=; b=19PV7XjlMDBQOgP3Cgf/jfieIvUgcokLAiP6Ho/KjYP9R0JgLGSGtol0sRULZN8JzX8AI0 8QKegvAL/5KhTu8DhhX1UDzcrOcsHAPFlDibtVgAOmpCxK+6zw/knL7BwAIZT9BaSRe4UK K+rL3ZwQ3Is8bTz/SxgqPO7zbPTyN9g= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Hh8/sx6v"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697130278; a=rsa-sha256; cv=none; b=u08rTW1S4p3ektSVoYfdGSlA/2QAtl6kw+P/vX0DbMW2mR1IBxnCZuV6Ch9jCcQCZRmZbY pWqVgt8VFfb4ViK9lCR/nE1HrlqYL3ZV22dCoQnOmmK47M6WB6AjaxbpVNXNEK/it34O4d DHalMnqI1po9mqNQThvps8FhzCGzM5g= Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-32157c8e4c7so1158192f8f.1 for ; Thu, 12 Oct 2023 10:04:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697130276; x=1697735076; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7LHgfYhq7AemM3CMY63djM9UPMuGnWJ3Ca40aCfcDv0=; b=Hh8/sx6vGXG7LA6gIIkl6dL9TAdpw+HskTu8JjQHAIoNWWLhe3DQa+6ylhOC9USEVx fmaFP12ycH/V7djxSTd5PfwqoOIfsMebBqNETOpDN1jMuCs1f5aFUkDm/QEaNyrYQv5U QlGJgwEnhi3n20/qCJu3Hp/UlrDCYqz3Aodfb3O3LveJfSnYEdoVFSepP1CUcpRT7t1l Ubl5UNXOQrtPlEfxbPkmQNdttQkSOG3EPq6/3CrWS6kBMvKdVoMirwrXd0zj09RxfxIf QOgGWqoOAdSRn9fve7hTxV/ZL6d1wlKD6yTBBMHgcIVuwR2G+s5V+0tjnKPyzFwZmp2P wmfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697130276; x=1697735076; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7LHgfYhq7AemM3CMY63djM9UPMuGnWJ3Ca40aCfcDv0=; b=vT9sJT3M8h86I7WQYrZXCsavIOM6pNWK1K+SDWIt/nOmrtA+egK2o8yjpm1A4+7gfa /Tyhj3Q1RW8YSsI//89P3MhKnoHi0dr6zEd/9Ct5kO/NNfm70AoB0AhWb4mjKKN/+zFD ClEuZsmUYnFaLz4vSxjpmVd9tlMbHQT9A6zu+wA2j8XUs8cK1VDP0Py/D8yxTZlnqiOx 4Fo9uJbX6qYr0+31rnZNLS1mAlq/zIHeR2gT2MBd48/S52xEAvQ7gsfTFsDY4CYbtjLW f/7XvW1ReodSM9SGMstlLE0BxNvgpMpvIOr4wn22VEisUDD4RE1HK2XOfwrq/nJXraH5 n+jw== X-Gm-Message-State: AOJu0YzshT3KAGqytb2wqPQpsEbJbGo6ON8YGCQayBIsw7ySLVjVJX6S Efjcaf6Bech4uEBN+xG2zdYWlyUULyg= X-Google-Smtp-Source: AGHT+IH0xAcE4YpN2PK6WqZDga4ztB5LkRYTzIVE53cOl8uKk2DQm6dZmFs1iPawW4LIUtQS3pkW5Q== X-Received: by 2002:a5d:628a:0:b0:320:bb1:5a73 with SMTP id k10-20020a5d628a000000b003200bb15a73mr21972024wru.22.1697130276078; Thu, 12 Oct 2023 10:04:36 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id h16-20020adffd50000000b003197869bcd7sm18875418wrs.13.2023.10.12.10.04.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 10:04:34 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Matthew Wilcox , Hugh Dickins , Andy Lutomirski , Jan Kara , linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v4 2/3] mm: update memfd seal write check to include F_SEAL_WRITE Date: Thu, 12 Oct 2023 18:04:29 +0100 Message-ID: <913628168ce6cce77df7d13a63970bae06a526e0.1697116581.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 15484A0006 X-Stat-Signature: idc1ume4y4txqq4wwmu6hkweky8zgjej X-HE-Tag: 1697130277-359923 X-HE-Meta: U2FsdGVkX1/KXC8jE3uEQwO2R7S8Wnc4G+HflWxbmaTqv+3yWnbkpNKo8Inok7L/pqCH0S7otdztU0AIukDOGGXEVChDhCN4sQttCu9pStJ/Vhd480eRGFe1BKzDB1hwtX5L5wftVsDJLgrig6HmSjJDQbO5NkztY+JXH32T9eY5/hSR11KbJMaRINwNN/7XuGSEb3wo/J4q6459l+JIqyoiDPvNovoifRO0UW8xe25Jn6cWwjaeva0ICVCzRvgQPMnkanpjwz45Ra+yCXOdZHSV42UE4sCGJ62RW32blb4phabUHEBC7Xj0XYIDe5e8kxV/GvjNMG+7rOvjP3wbWmH02MPMnqk40PE5WK6oe3QNfTt4t710zaUQQby0tptMACOfuNS4nIcy+Qaf24q2lIUQkTvMsDSSH9hMRHvj7puNiIgdxnoNBqj0QcfQP53EhyeLA0tUFJDE+xcsQLBWo6+cS0NwUQ8DihHvWpwbciu+RPmJLClsVmI0ZjBWGrb654kvdmL+twNp64dfZedIH5TYLGTWJ0Urie2KhTO45HRxG92RbSaesBnog8Rlsm2U+L8rI/j3GmoPj1XWmwBsEZ2j5ScrHfZBAIRjPsRkPAw6xkmet3YXXr+TMiBv7Jk6xKVdoDNuTsjSbeXIdwakUlOPDfaOpEdpSEIjhkOAweTwgGDIHAbvCFTgdc8yPCWuKPBiqj6Tf/WJvtKNdiEV7r7sOnIQ2Kg14cXF5lOpnIX4Q/zv1U9bKpKHs3wWwlrIRX+Z6FdwEAPNgJylxul26I9wiso3ceCTbVeVyqpnfY/A4IFgZPWgxSKcEJttpDRHd1w5bIrcelTGKxeLo4YBDTbcWQ3VUlcJ4957Znu0q9snjfSpFpvu0mDD1N0JfHF8iz6HbiGweEO7mCtTZBKaH1l93nM/r65B1EBqU1uGgXdGWnb/ig4QdwAW9nH/fWs8+NAYNIAQDtuw9rsnPdo PHCYFwnA zIC5GfvdinY6OO5gKDD3HssUXq6IoUJ9U5mnSBCun8QFV7p67Eu4DG3nPkfrkMhusnTJ+Q8qidJzCtnipCN9celfewleqN0lEbDqWt166DvEC4jAqmaO8TTowxNUadsPyq0laQK1sm0pWRq44wOFN4JV8wFzR8rrc3oo9myaW4GlJMzDt9zB47Ejl8ULMPJ5ciHj0jYOZp6eWLSLRhkvgVbmUfZgFIwZ6nnto/AngULIj4Zx+/grUUXYtd3pmz55rznOrHJB9DyUUoy+r7/8R/aSJxfsmM0OqhBl/9gkOQsO5taOfSMfMye/7Mp3hVjxKuAT5jYLxkX83gDbwKaRr/EVEIeIUzgEC86NYeNlhlu9RH42GcjwUBUfj3InKqd5VeRR3SsgnooJZMpOZUPOcVaQdnd2HHgfQoZezKAYVOOe0PQcIEpfbXCzNmLN7m1JUqCMO 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: The seal_check_future_write() function is called by shmem_mmap() or hugetlbfs_file_mmap() to disallow any future writable mappings of an memfd sealed this way. The F_SEAL_WRITE flag is not checked here, as that is handled via the mapping->i_mmap_writable mechanism and so any attempt at a mapping would fail before this could be run. However we intend to change this, meaning this check can be performed for F_SEAL_WRITE mappings also. The logic here is equally applicable to both flags, so update this function to accommodate both and rename it accordingly. Signed-off-by: Lorenzo Stoakes Reviewed-by: Jan Kara --- fs/hugetlbfs/inode.c | 2 +- include/linux/mm.h | 15 ++++++++------- mm/shmem.c | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 06693bb1153d..5c333373dcc9 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -112,7 +112,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) vm_flags_set(vma, VM_HUGETLB | VM_DONTEXPAND); vma->vm_ops = &hugetlb_vm_ops; - ret = seal_check_future_write(info->seals, vma); + ret = seal_check_write(info->seals, vma); if (ret) return ret; diff --git a/include/linux/mm.h b/include/linux/mm.h index bae234d18d81..26d7dc3b342b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4078,25 +4078,26 @@ static inline void mem_dump_obj(void *object) {} #endif /** - * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it + * seal_check_write - Check for F_SEAL_WRITE or F_SEAL_FUTURE_WRITE flags and + * handle them. * @seals: the seals to check * @vma: the vma to operate on * - * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on - * the vma flags. Return 0 if check pass, or <0 for errors. + * Check whether F_SEAL_WRITE or F_SEAL_FUTURE_WRITE are set; if so, do proper + * check/handling on the vma flags. Return 0 if check pass, or <0 for errors. */ -static inline int seal_check_future_write(int seals, struct vm_area_struct *vma) +static inline int seal_check_write(int seals, struct vm_area_struct *vma) { - if (seals & F_SEAL_FUTURE_WRITE) { + if (seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { /* * New PROT_WRITE and MAP_SHARED mmaps are not allowed when - * "future write" seal active. + * write seals are active. */ if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE)) return -EPERM; /* - * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as + * Since an F_SEAL_[FUTURE_]WRITE sealed memfd can be mapped as * MAP_SHARED and read-only, take care to not allow mprotect to * revert protections on such mappings. Do this only for shared * mappings. For private mappings, don't need to mask diff --git a/mm/shmem.c b/mm/shmem.c index 6503910b0f54..cab053831fea 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2405,7 +2405,7 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma) struct shmem_inode_info *info = SHMEM_I(inode); int ret; - ret = seal_check_future_write(info->seals, vma); + ret = seal_check_write(info->seals, vma); if (ret) return ret; From patchwork Thu Oct 12 17:04:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13419483 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 CF115CDB485 for ; Thu, 12 Oct 2023 17:04:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 213D88D0137; Thu, 12 Oct 2023 13:04:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C4548D0002; Thu, 12 Oct 2023 13:04:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F09CB8D0137; Thu, 12 Oct 2023 13:04:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E221F8D0002 for ; Thu, 12 Oct 2023 13:04:41 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A7F7F80631 for ; Thu, 12 Oct 2023 17:04:41 +0000 (UTC) X-FDA: 81337433562.13.64DCB9B Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by imf03.hostedemail.com (Postfix) with ESMTP id C4CE32000E for ; Thu, 12 Oct 2023 17:04:39 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=LXh3XlcP; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.45 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697130279; 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=VkUJ0oO3Mp9xCXnylh1JdkHseQH/mUhjkeml5gm5SR8=; b=DC+L+vnNUAkM+7WdGcczTcrkn+w8NUc5VJqhAHroHYcInQvANpGNNTkOZ+LR1jcuBvueYy trrZ7Azf2GZ3rxiQ6NBZ7mo6PAHeRk3OxH4H+GNBMr6Aqtfhlj8xiVsGL5UERG/hVcSvA9 QjeK2lYxERxbw2Qs19pyYilSmDufjxQ= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=LXh3XlcP; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.45 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697130279; a=rsa-sha256; cv=none; b=EFCahX2VgdnOlc0W+9VQkPphsJ6OfciOKBKytEyv9/Aj9485nNWFZvh4m6NIAnsuxns2bd NE5LBhqWtRSo4f7RIpkUv6G4KK+AIhesAt/MXS84kU1GDIb5gtqnW+kEib2AWR1hA6OgC+ D8fcXpGK2STvANw6p2/jgSEap686HTc= Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-32d834ec222so1175074f8f.0 for ; Thu, 12 Oct 2023 10:04:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697130278; x=1697735078; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VkUJ0oO3Mp9xCXnylh1JdkHseQH/mUhjkeml5gm5SR8=; b=LXh3XlcPcU2LkwGCDm1TSIGA1xO71LheJVT5YhrHfCIE5zU3dkCriKfYduXebAr3SX akWKJgGO+06k29uvUwEm5C0fGSX7r8Ci6w5OBM0a7bRFJvQM/G+omQRGzqEif7cqes0f hbZIUUZjxUXxbuE+MBqo8ql2rYW2ZZEl7ApAUAzR3srF8bs7mPkRGCcdYC/Sq4kWeA2o t7Y2m0I5dTHqQ7bjbEJvRtNeOv7HP0sPN4QLiG0i3TLT2SOQNoLkHKw0PG+dofKKKedQ JU6+pe5moM9D44Cesr/ZucD7EZjhb5zVl2me9nFQlkC/9U/ViivG3GVFA/3p6fjj1lTV myQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697130278; x=1697735078; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VkUJ0oO3Mp9xCXnylh1JdkHseQH/mUhjkeml5gm5SR8=; b=aSWh9IOBgKxBmUCH1rIJC//TVdvP4nuJmZSTH0S1SdsOpMlvt8FjhAoEnpPc1hShoA t4pMq+2S3QY/htmVDgiYCNSGqsrj72+S8PTXzA0OeTIIFRlC/NMw/JnLT8Dug8NsyVpq 4vKrsPn6VdZNlIy7bF65IMVjqUHf0UvgIBpl59ceuudsH2bZixTEdtzDv5y2B7ySvfxn pcCP8OR27uziglK4J2ykthBo4OWkEBeMJIewG+1znQ3DdoTsmy1gOMedkC8r68P+Z1kI mS2Fm1eFXPpnvF+c4Uoj380hCKcgXAc3/tCE/IAvo1/h5m8tNOkUPfs8jZK5GyqLG2XC YqLA== X-Gm-Message-State: AOJu0YzSFmctYgzRrDB+ejvIq+8G6ZryFQQ7TaHKN/bJowzaxqQDftxn NnxZeJ25545J3+6NOqJDsOB36SANy4c= X-Google-Smtp-Source: AGHT+IGEfvOX2oiWBKkh4O/yKmWydOZ76wDsxaQCjAqvjCnNn57Y9QF5SeE6lMfg3rl+IKZUkH80og== X-Received: by 2002:a05:6000:b0f:b0:32d:9572:6469 with SMTP id dj15-20020a0560000b0f00b0032d95726469mr1470249wrb.46.1697130277811; Thu, 12 Oct 2023 10:04:37 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id h16-20020adffd50000000b003197869bcd7sm18875418wrs.13.2023.10.12.10.04.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 10:04:36 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Kravetz , Muchun Song , Alexander Viro , Christian Brauner , Matthew Wilcox , Hugh Dickins , Andy Lutomirski , Jan Kara , linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, Lorenzo Stoakes Subject: [PATCH v4 3/3] mm: perform the mapping_map_writable() check after call_mmap() Date: Thu, 12 Oct 2023 18:04:30 +0100 Message-ID: <55e413d20678a1bb4c7cce889062bbb07b0df892.1697116581.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: C4CE32000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: zz3kucjxtabi9a8kj1hc1to58pc73ou1 X-HE-Tag: 1697130279-863664 X-HE-Meta: U2FsdGVkX18JNnVZZE1s4lyLNEii8/sIoz9qq0DDA06fgnFBflYqWWHGTqMU8Gddu38Ka4k8u7FpRy+HHPV8Uz7Ogpj4apRddU2XUa7SY5lUHZVl5tu2h7W7/Hs+fyv21yKP70eDYebrGDeGcd6/RcZRqSaF8AE0FVGvbB3qZEbrasU14JEFbG37nrGqzRRO7kvEvJXLPBUqOKkA0Q66hGId2lyt3b+MlfGspvPPgsv9OjFpoUdFkT7oUc1sq2K+CEdFXYHAp3NJRYlIi1CnweifQMcsTg6JMV5eoiG36R+1hTrkiUt0gGjOm7x2CKcLMQEvbJKebGqcGbrKGIIVkMVODwbUkP4apaVjUtD9bsgkqgvGVQ6g/9vz2iOwL9sfFa4tns04c/zxvbOqcAvwFYZJ7OIDG0OM59AJgvsd6zivh9QBvgN20gE4QOSkddZsYyBUrXFHaDPhzyA3KtZ1SFSl7ODBtRLDSSgEbFF3WSwrHSgI57AWEwPYfdH5o9SA02aSjvTMAQ7FKwu7JB3graRtpjm1NVKX5t8J5bwIE2TH4GTGBr1If+hTpLqgvpLnIeqSY9hMi5FxthIvaznFO3bYmgLx2EWL9hZh16/WRq7t0wGeChGC2Wz/XUjAICAOh4YSwa7irS0OWMolOnDNh80l7N8hUrdm5bQlBGvVVMw3iffmrHuQX+3gCeqyKncOiYVqGjt3nqGXQRPyvz4wsJeeXts8Wm01/kVpfDKr38IujUZOSI8viC4dQUOYFpv3rBuKSIPvYK/241c2h8NjtXXHED0OFmWNvie0y3EOQyBsYxOfTffj+IFGaiiv2fKOg1rsdtlwerpGEI60xR5rJzKhH6+xBwfXrVg3gBS8eeCk0/35yJkpQLOb6YtBIlGydHeRCxT81rnsH+Ss2hUil8rFE+gd3rfNOuQRTdDi8fszzaLPvsbhxG12zh9aH1rzlnwgP38io7++jbkzWd4 gfcaAFIv COcT2uVLE/a0MD+N75bUuP1UGz7vsmFS/tI6J0wRqTazVu7P3xyzpkq3KzwUitABHVcRpB2m5+9zoAjpZL4zoyXdHRAeUGpCk/Y25HB7wPhIzrrRdwPjaRkr7M91dMIFZfP5ujKmX+TZfgDqoxZcRY8xcbEhswnKWJbN5+U7z1F+LRwBT3ppeaTapQCa1bCnNZ2abhTXquxVtGfi3GV5ukQX0RrYAW878BG2FpmDVl1Su/vAZoYyDiHpEeKzlZF46tXwHWseRY5GzCJ2kYVPY1k3rUqLNTeX5wA+iqtbriioRKUw+rBjCLg+QwXXx5on0qP2WLkVMYEzoAGp1rI7G7D4yrVtls78RhAWYckKtGESKHuOs9J9gjdLxre5g4FWgJOUoQnUaWTXYUr4Aj4P6bRCZvaifA2+VGIMdrGrU9lwKplLcEeTYHGr0ogjZn+tiT6wl 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: In order for a F_SEAL_WRITE sealed memfd mapping to have an opportunity to clear VM_MAYWRITE, we must be able to invoke the appropriate vm_ops->mmap() handler to do so. We would otherwise fail the mapping_map_writable() check before we had the opportunity to avoid it. This patch moves this check after the call_mmap() invocation. Only memfd actively denies write access causing a potential failure here (in memfd_add_seals()), so there should be no impact on non-memfd cases. This patch makes the userland-visible change that MAP_SHARED, PROT_READ mappings of an F_SEAL_WRITE sealed memfd mapping will now succeed. There is a delicate situation with cleanup paths assuming that a writable mapping must have occurred in circumstances where it may now not have. In order to ensure we do not accidentally mark a writable file unwritable by mistake, we explicitly track whether we have a writable mapping and unmap only if we do. Link: https://bugzilla.kernel.org/show_bug.cgi?id=217238 Signed-off-by: Lorenzo Stoakes Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 0041e3631f6c..7f45a08e7973 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2752,6 +2752,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, unsigned long charged = 0; unsigned long end = addr + len; unsigned long merge_start = addr, merge_end = end; + bool writable_file_mapping = false; pgoff_t vm_pgoff; int error; VMA_ITERATOR(vmi, mm, addr); @@ -2846,17 +2847,19 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_pgoff = pgoff; if (file) { - if (is_shared_maywrite(vm_flags)) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } - vma->vm_file = get_file(file); error = call_mmap(file, vma); if (error) goto unmap_and_free_vma; + if (vma_is_shared_maywrite(vma)) { + error = mapping_map_writable(file->f_mapping); + if (error) + goto close_and_free_vma; + + writable_file_mapping = true; + } + /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA. @@ -2920,8 +2923,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, mm->map_count++; if (vma->vm_file) { i_mmap_lock_write(vma->vm_file->f_mapping); - if (vma_is_shared_maywrite(vma)) + if (vma_is_shared_maywrite(vma)) { mapping_allow_writable(vma->vm_file->f_mapping); + writable_file_mapping = true; + } flush_dcache_mmap_lock(vma->vm_file->f_mapping); vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); @@ -2937,7 +2942,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, /* Once vma denies write, undo our temporary denial count */ unmap_writable: - if (file && is_shared_maywrite(vm_flags)) + if (writable_file_mapping) mapping_unmap_writable(file->f_mapping); file = vma->vm_file; ksm_add_vma(vma); @@ -2985,7 +2990,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, unmap_region(mm, &vmi.mas, vma, prev, next, vma->vm_start, vma->vm_end, vma->vm_end, true); } - if (file && is_shared_maywrite(vm_flags)) + if (writable_file_mapping) mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma);