From patchwork Tue Oct 13 01:34:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 11834861 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 26BE9175A for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 046AD20678 for ; Tue, 13 Oct 2020 02:45:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="J2rOTWZ8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728221AbgJMBeY (ORCPT ); Mon, 12 Oct 2020 21:34:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728200AbgJMBeW (ORCPT ); Mon, 12 Oct 2020 21:34:22 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2795C0613D0 for ; Mon, 12 Oct 2020 18:34:21 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id k18so19909115wmj.5 for ; Mon, 12 Oct 2020 18:34:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DaxIr5597WF0kxpEElTZVLTN61vAhnpO/2r1W4YxrO0=; b=J2rOTWZ8Lr5Wf2QMpwxeIwTk31F0CcPsxAE+ecneTuceI94efNcmEIgFo7sgQ5lmkn zLqcJTHbLpzPMw8OtVmyeRDaSk7WV69pJ35WU8S/Ni5BQSLXCGOMFKIr7yCZ95uiklXu AhGmVvlhf//oWpBLnP9Pbi9S9Pwm1hZ5YPKuv0zyUoCV3Fn+Aha/78ZfCxKT06boM6hV CEQgE4qDGlvBcUzSZvJQRXYHTypYqNFf9/hCzW4zFumXGHjPHoQ4nsxJGEqUsifC9zB+ ggvSzmzm+sUfR8KHUE7y6WLZ12vUAGmWR+1sz/qeHvP3y5XGFPLwNBoyR9cfLfWhG3fx qTYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DaxIr5597WF0kxpEElTZVLTN61vAhnpO/2r1W4YxrO0=; b=KHtkoywqTUKxB/Sm4uyPcwBAlxgE3SnglzHixc03eTfyb10TOYb9ndC2u72TdT2hBY rg1Ig2nHJRmbS+kBKkm3kGGQWEpr2wKm1UejQ/6DkVaVQAPtCVPEFpsl6VS7BwQ1PNR5 a6oQhUlfX0ARL5Gqocw7IcZ80h6+ntcyjyDqeaJORsylLgBNl7+80TmxyKVgy4u3hz4L lmSUH0aX+afdnNdaiDXVZ+Us+mcKmsA6Gf4b+oQVAB5mIe56J7PeZ5dKaD6zOY1ioWaN J/IdXhFJtbQUv6FwgH8691GuODcoADrvEaGXtGjIMhjnhWMNeIJx9ygnmjWOx098c480 3kMg== X-Gm-Message-State: AOAM531zrwy8mm1zyBrsFAoA9q240V0v6Dh/muqrvZMX4tacc0Eg6AM4 9xgubjXpqPykBQ/2vMHRLbFKgA== X-Google-Smtp-Source: ABdhPJwGpJy4DdZ/eC/fCwJjqmeUbVJXNMLzWnkwR0hTzvknEAdfYsGaIbJtZWKk1I+xy8WZjoA44w== X-Received: by 2002:a1c:48d4:: with SMTP id v203mr10067253wma.122.1602552860468; Mon, 12 Oct 2020 18:34:20 -0700 (PDT) Received: from localhost.localdomain ([2a02:8084:e84:2480:228:f8ff:fe6f:83a8]) by smtp.gmail.com with ESMTPSA id d23sm24825325wmb.6.2020.10.12.18.34.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Oct 2020 18:34:19 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Alexander Viro , Andrew Morton , Andy Lutomirski , Brian Geffon , Catalin Marinas , Dan Williams , Dave Jiang , Hugh Dickins , Ingo Molnar , "Kirill A. Shutemov" , Mike Kravetz , Minchan Kim , Russell King , Thomas Bogendoerfer , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 1/6] mm/mremap: Account memory on do_munmap() failure Date: Tue, 13 Oct 2020 02:34:11 +0100 Message-Id: <20201013013416.390574-2-dima@arista.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201013013416.390574-1-dima@arista.com> References: <20201013013416.390574-1-dima@arista.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org move_vma() copies VMA without adding it to account, then unmaps old part of VMA. On failure it unmaps the new VMA. With hacks accounting in munmap is disabled as it's a copy of existing VMA. Account the memory on munmap() failure which was previously copied into a new VMA. Fixes: commit e2ea83742133 ("[PATCH] mremap: move_vma fixes and cleanup") Signed-off-by: Dmitry Safonov --- mm/mremap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/mremap.c b/mm/mremap.c index 138abbae4f75..03d31a0d4c67 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -450,7 +450,8 @@ static unsigned long move_vma(struct vm_area_struct *vma, if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { /* OOM: unable to split vma, just get accounts right */ - vm_unacct_memory(excess >> PAGE_SHIFT); + if (vm_flags & VM_ACCOUNT) + vm_acct_memory(new_len >> PAGE_SHIFT); excess = 0; } From patchwork Tue Oct 13 01:34:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 11834863 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4D5B314D5 for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 321F620678 for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="FGKDY4+X" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728238AbgJMBeY (ORCPT ); Mon, 12 Oct 2020 21:34:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728220AbgJMBeX (ORCPT ); Mon, 12 Oct 2020 21:34:23 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 510C3C0613D2 for ; Mon, 12 Oct 2020 18:34:23 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id p15so19257530wmi.4 for ; Mon, 12 Oct 2020 18:34:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N0dBjMGQZwr5fzpQZu0PyDC9RTW1+FomX32MrbIIN2c=; b=FGKDY4+X3JgwoUoN1C/3HtoPUDzAwaeKRnm9weDnUEBjEuPWSOeILJ5WitdE7ezOUP Z8h6z8QjmHrQkKSfvP/CqOKrMe2VeWaoWuM1DTGhWnLuZk1g88dKU5Im4GTOJwORpW2f UuV18UZCz944g3qypRZ2XQhlg6JzVZmLhMr5Bs/tnrm1RAUdEgB/WdGaLZGTdqqhIMgV pfG53+RblNOsJA22ZNjqtEkKPrKzJBc7Og2rE+hr0YIrlaORjXWV2alA7tDY1jhP1fno 43O1Nu5NaFD+pRDCg3zxhKfu0Owyg1VkXKC8ZVHZFjGsiuAaTBtUv+EuEgHOsI8dOIdW j1Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N0dBjMGQZwr5fzpQZu0PyDC9RTW1+FomX32MrbIIN2c=; b=GPRLPSJ9ZvuM7NQyvWfsJ3isovzZB910x3wGi+40Hb6RMQhTmZbjrrpFJTVs+hxRZz TRph8hBQAGFd3tPTFLqu5kQOOfzTGJcq3NvmFb8rCxf/NTFCsHguwrYRgrkeNMxXm4H2 LpmMElsc26Pv7VlEu0BLr7CnOR4I5PjodHKWu3HGtyGDRsK48g/Sz9iwVWo5igCEv7E4 ZKRoEG2V8bAgvu4HEyXaSnz3RK2qUCg/b3ogIDzItCo7K2Elwv2ILHSOVE20roVQfu68 cpTMCRQyOLYVdE8WjKbXvr5bEtcC+ZpHnl46dRV1xg8xSJJYJWyvtIKRwSD2R6JXkAjy TnvQ== X-Gm-Message-State: AOAM5309QRrTP1UzKeJXetE5Gm423USW1jEaQWHIkza+2hJfTd1tCMVW ulQnrWbvIypkTzufK/uWAUbRYQ== X-Google-Smtp-Source: ABdhPJzYLde4t7k258XJEoIwmIIg3SOE4QcEX3b/iLBAwsA51BD5ldG7hvxtYYzYqpyGbgSwcygcoQ== X-Received: by 2002:a7b:c0c8:: with SMTP id s8mr12752537wmh.78.1602552862013; Mon, 12 Oct 2020 18:34:22 -0700 (PDT) Received: from localhost.localdomain ([2a02:8084:e84:2480:228:f8ff:fe6f:83a8]) by smtp.gmail.com with ESMTPSA id d23sm24825325wmb.6.2020.10.12.18.34.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Oct 2020 18:34:21 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Alexander Viro , Andrew Morton , Andy Lutomirski , Brian Geffon , Catalin Marinas , Dan Williams , Dave Jiang , Hugh Dickins , Ingo Molnar , "Kirill A. Shutemov" , Mike Kravetz , Minchan Kim , Russell King , Thomas Bogendoerfer , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 2/6] mm/mremap: For MREMAP_DONTUNMAP check security_vm_enough_memory_mm() Date: Tue, 13 Oct 2020 02:34:12 +0100 Message-Id: <20201013013416.390574-3-dima@arista.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201013013416.390574-1-dima@arista.com> References: <20201013013416.390574-1-dima@arista.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Currently memory is accounted post-mremap() with MREMAP_DONTUNMAP, which may break overcommit policy. So, check if there's enough memory before doing actual VMA copy. Don't unset VM_ACCOUNT on MREMAP_DONTUNMAP. By semantics, such mremap() is actually a memory allocation. That also simplifies the error-path a little. Also, as it's memory allocation on success don't reset hiwater_vm value. Fixes: commit e346b3813067 ("mm/mremap: add MREMAP_DONTUNMAP to mremap()") Signed-off-by: Dmitry Safonov --- mm/mremap.c | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/mm/mremap.c b/mm/mremap.c index 03d31a0d4c67..c248f9a52125 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -365,11 +365,19 @@ static unsigned long move_vma(struct vm_area_struct *vma, if (err) return err; + if (unlikely(flags & MREMAP_DONTUNMAP && vm_flags & VM_ACCOUNT)) { + if (security_vm_enough_memory_mm(mm, new_len >> PAGE_SHIFT)) + return -ENOMEM; + } + new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, &need_rmap_locks); - if (!new_vma) + if (!new_vma) { + if (unlikely(flags & MREMAP_DONTUNMAP && vm_flags & VM_ACCOUNT)) + vm_unacct_memory(new_len >> PAGE_SHIFT); return -ENOMEM; + } moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len, need_rmap_locks); @@ -398,7 +406,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, } /* Conceal VM_ACCOUNT so old reservation is not undone */ - if (vm_flags & VM_ACCOUNT) { + if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) { vma->vm_flags &= ~VM_ACCOUNT; excess = vma->vm_end - vma->vm_start - old_len; if (old_addr > vma->vm_start && @@ -423,34 +431,16 @@ static unsigned long move_vma(struct vm_area_struct *vma, untrack_pfn_moved(vma); if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) { - if (vm_flags & VM_ACCOUNT) { - /* Always put back VM_ACCOUNT since we won't unmap */ - vma->vm_flags |= VM_ACCOUNT; - - vm_acct_memory(new_len >> PAGE_SHIFT); - } - - /* - * VMAs can actually be merged back together in copy_vma - * calling merge_vma. This can happen with anonymous vmas - * which have not yet been faulted, so if we were to consider - * this VMA split we'll end up adding VM_ACCOUNT on the - * next VMA, which is completely unrelated if this VMA - * was re-merged. - */ - if (split && new_vma == vma) - split = 0; - /* We always clear VM_LOCKED[ONFAULT] on the old vma */ vma->vm_flags &= VM_LOCKED_CLEAR_MASK; /* Because we won't unmap we don't need to touch locked_vm */ - goto out; + return new_addr; } if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { /* OOM: unable to split vma, just get accounts right */ - if (vm_flags & VM_ACCOUNT) + if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) vm_acct_memory(new_len >> PAGE_SHIFT); excess = 0; } @@ -459,7 +449,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, mm->locked_vm += new_len >> PAGE_SHIFT; *locked = true; } -out: + mm->hiwater_vm = hiwater_vm; /* Restore VM_ACCOUNT if one or two pieces of vma left */ From patchwork Tue Oct 13 01:34:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 11834865 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7000717D1 for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 56DEC20678 for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="jh6Shp34" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728279AbgJMBea (ORCPT ); Mon, 12 Oct 2020 21:34:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728246AbgJMBeZ (ORCPT ); Mon, 12 Oct 2020 21:34:25 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02AA8C0613D0 for ; Mon, 12 Oct 2020 18:34:25 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id s9so9736582wro.8 for ; Mon, 12 Oct 2020 18:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VQFsEpWuF4crrAFVUaO/CXC4VHrsA3+ZAZo85Yi/pU0=; b=jh6Shp34HrXFqwhbRrxN+YgzPDA8OoKF04HoFiyNjYZbVjAQaB03yWzpCRAqet1tBl qznQQVvt39gfc+rca19EffWNQulmC0gW4kOZ8oTyFIs115lHAaCkdHebfOmk4vwVJUR1 d2bQ9UoW57Nh9V9EzhGNzvekxXH9NTWdA1QVVsRmgMdO4ONwKofcPq8xEzaBF3S4R5jg jdusZLNIDiReYRlgKGWx6Z/UqIV8WzrGb6kyLnbpUec/Bapgh5SfttlYv6fPecZcoanq byDqDGINOzYslI8U3EIjdSphlS2zzm6PJPCEHPm660qZ4fQUqAT8u67Dx5YXCG+X9fLu uI9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VQFsEpWuF4crrAFVUaO/CXC4VHrsA3+ZAZo85Yi/pU0=; b=X0pUAnaRTLcfU5Hh3eWBdDVEVFLytO1u2WIpcmSZ2J+cp9hUXCDR2a8/z68KVO/Eph ZvQUEc31PA8JsT7WCtoYnm9pPSkEpgpENS9VdzOLJVG5Q+4mq/1HzL3Od6UpLK20eWMP Bomy9kjyW//UIzecdy6Vz+Wyim19fULMKVureiKbys9NWoYtw2HUuOg86/s5MUZ4pk57 5c4m0H2Dlucx0PV63jbtH734EeYbHxLBi1VcTYjP0xTi3fn3GdOkkHqN9n3Ta/2AqStC w5P2DPmEami8ZZxaZTU0l3Ch5HQnOw0ASHPi4qM/838Ht9arL2lMcNQAqe0La8itVTky F4zw== X-Gm-Message-State: AOAM531UcI25ZW8XRxYgTFrrzypYgvcToyHLenoJmOws+BkK3vN2jVo5 XjfaWVj1BuJVeBUlTlVZ/+IKbg== X-Google-Smtp-Source: ABdhPJw5vKLkrkjcJCNIMOOzrKmqWdjiE3iZi/mIzmQ8aAHgnoINflz5eE8APWdVPpZyrKnFlUqGEw== X-Received: by 2002:adf:e881:: with SMTP id d1mr21447682wrm.395.1602552863641; Mon, 12 Oct 2020 18:34:23 -0700 (PDT) Received: from localhost.localdomain ([2a02:8084:e84:2480:228:f8ff:fe6f:83a8]) by smtp.gmail.com with ESMTPSA id d23sm24825325wmb.6.2020.10.12.18.34.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Oct 2020 18:34:23 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Alexander Viro , Andrew Morton , Andy Lutomirski , Brian Geffon , Catalin Marinas , Dan Williams , Dave Jiang , Hugh Dickins , Ingo Molnar , "Kirill A. Shutemov" , Mike Kravetz , Minchan Kim , Russell King , Thomas Bogendoerfer , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 3/6] mremap: Don't allow MREMAP_DONTUNMAP on special_mappings and aio Date: Tue, 13 Oct 2020 02:34:13 +0100 Message-Id: <20201013013416.390574-4-dima@arista.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201013013416.390574-1-dima@arista.com> References: <20201013013416.390574-1-dima@arista.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org As kernel expect to see only one of such mappings, any further operations on the VMA-copy may be unexpected by the kernel. Maybe it's being on the safe side, but there doesn't seem to be any expected use-case for this, so restrict it now. Fixes: commit e346b3813067 ("mm/mremap: add MREMAP_DONTUNMAP to mremap()") Signed-off-by: Dmitry Safonov --- arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 2 +- fs/aio.c | 5 ++++- include/linux/mm.h | 2 +- mm/mmap.c | 6 +++++- mm/mremap.c | 2 +- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/pseudo_lock.c b/arch/x86/kernel/cpu/resctrl/pseudo_lock.c index 0daf2f1cf7a8..e916646adc69 100644 --- a/arch/x86/kernel/cpu/resctrl/pseudo_lock.c +++ b/arch/x86/kernel/cpu/resctrl/pseudo_lock.c @@ -1458,7 +1458,7 @@ static int pseudo_lock_dev_release(struct inode *inode, struct file *filp) return 0; } -static int pseudo_lock_dev_mremap(struct vm_area_struct *area) +static int pseudo_lock_dev_mremap(struct vm_area_struct *area, unsigned long flags) { /* Not supported */ return -EINVAL; diff --git a/fs/aio.c b/fs/aio.c index d5ec30385566..3be3c0f77548 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -324,13 +324,16 @@ static void aio_free_ring(struct kioctx *ctx) } } -static int aio_ring_mremap(struct vm_area_struct *vma) +static int aio_ring_mremap(struct vm_area_struct *vma, unsigned long flags) { struct file *file = vma->vm_file; struct mm_struct *mm = vma->vm_mm; struct kioctx_table *table; int i, res = -EINVAL; + if (flags & MREMAP_DONTUNMAP) + return -EINVAL; + spin_lock(&mm->ioctx_lock); rcu_read_lock(); table = rcu_dereference(mm->ioctx_table); diff --git a/include/linux/mm.h b/include/linux/mm.h index 16b799a0522c..fd51a4a1f722 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -550,7 +550,7 @@ struct vm_operations_struct { void (*open)(struct vm_area_struct * area); void (*close)(struct vm_area_struct * area); int (*split)(struct vm_area_struct * area, unsigned long addr); - int (*mremap)(struct vm_area_struct * area); + int (*mremap)(struct vm_area_struct *area, unsigned long flags); vm_fault_t (*fault)(struct vm_fault *vmf); vm_fault_t (*huge_fault)(struct vm_fault *vmf, enum page_entry_size pe_size); diff --git a/mm/mmap.c b/mm/mmap.c index bdd19f5b994e..50f853b0ec39 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3372,10 +3372,14 @@ static const char *special_mapping_name(struct vm_area_struct *vma) return ((struct vm_special_mapping *)vma->vm_private_data)->name; } -static int special_mapping_mremap(struct vm_area_struct *new_vma) +static int special_mapping_mremap(struct vm_area_struct *new_vma, + unsigned long flags) { struct vm_special_mapping *sm = new_vma->vm_private_data; + if (flags & MREMAP_DONTUNMAP) + return -EINVAL; + if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) return -EFAULT; diff --git a/mm/mremap.c b/mm/mremap.c index c248f9a52125..898e9818ba6d 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -384,7 +384,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, if (moved_len < old_len) { err = -ENOMEM; } else if (vma->vm_ops && vma->vm_ops->mremap) { - err = vma->vm_ops->mremap(new_vma); + err = vma->vm_ops->mremap(new_vma, flags); } if (unlikely(err)) { From patchwork Tue Oct 13 01:34:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 11834867 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9591E109B for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7987020678 for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="YferU0tb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728278AbgJMBea (ORCPT ); Mon, 12 Oct 2020 21:34:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728248AbgJMBe0 (ORCPT ); Mon, 12 Oct 2020 21:34:26 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C633C0613D2 for ; Mon, 12 Oct 2020 18:34:26 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id g12so21825154wrp.10 for ; Mon, 12 Oct 2020 18:34:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pPwHwZEP2PqHjnQGol/aPrlKhQ+v52jFp6WQVgPqijE=; b=YferU0tbNRf+gxmODbnA9Mqj4CpE0dtIAEistHAFS9i3faI1yKc65RMZjMueeuAd6K v90BcKD8EX8QrUHOBuc4RWU+YiTI127qs5q/pJVwzTOsSBeMcxB+oU2aVU55wR6pUlRv SK4XoL4NVCK1yi9eXXrxvr+Hj+52qancDHwYD07EaxXrNrtycjOMzvm0DxsjDe0Yr0Ym fy/5nhiDgsBYbPKPmos1hfATHBYfbuDpAX3q/tHFinsU09UpL8L6CMHFoHKZdGQST4Fx VqAQoWLpkvmc+4PZzRrMb/5p1F7aQWI0x1fji1HYeph0/+jjPTkmcPOZJhw2LejVQqoH 0K7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pPwHwZEP2PqHjnQGol/aPrlKhQ+v52jFp6WQVgPqijE=; b=hkPzZDSCR5hsnUpW98Giu5OMldmKLBe2W8I2HHY8R/HNbQ5NaEsrhnmAe+LFiWhrJ/ 09s7iI4w8Mp93tBu+rEJTYOm6YMbTBl9AlYO8kahfFnX/U0B/45J5WeeCssijE2WD6dg 9QfJw+NJAhSM8Gakl8BuFmpW7hjIW9/fjZRaktIn37F+PuV67MM7DWaQaxFZRfBUOLfx Qad4v041ooB769mbqNjGLBYROCYX3138o5WtSKOLK7eOGsg4Ey1tQ7pLAXOzJaVl6RU3 zSQZWmCbUjwYPHzKX1So4tn+DxqJ7D65FjdrfVBkq8NzOr6MMnXiL9kYIMDkpN67D4pu drLQ== X-Gm-Message-State: AOAM533xHnLJ4alvO57QymWzxzhHC1ytqtX+wqwY2X4Vn25yxJVnDJhi j1fW2EqB91Xvabw9d9UWUOsriQ== X-Google-Smtp-Source: ABdhPJx2Iz5ia07SkXg8JuVb45XrLxBzUgObuPt4vpXjnHItp8RoscX39wxroX44gq9L0v4LP5bsSA== X-Received: by 2002:adf:fc8b:: with SMTP id g11mr480698wrr.300.1602552865214; Mon, 12 Oct 2020 18:34:25 -0700 (PDT) Received: from localhost.localdomain ([2a02:8084:e84:2480:228:f8ff:fe6f:83a8]) by smtp.gmail.com with ESMTPSA id d23sm24825325wmb.6.2020.10.12.18.34.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Oct 2020 18:34:24 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Alexander Viro , Andrew Morton , Andy Lutomirski , Brian Geffon , Catalin Marinas , Dan Williams , Dave Jiang , Hugh Dickins , Ingo Molnar , "Kirill A. Shutemov" , Mike Kravetz , Minchan Kim , Russell King , Thomas Bogendoerfer , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 4/6] vm_ops: Rename .split() callback to .may_split() Date: Tue, 13 Oct 2020 02:34:14 +0100 Message-Id: <20201013013416.390574-5-dima@arista.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201013013416.390574-1-dima@arista.com> References: <20201013013416.390574-1-dima@arista.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Rename the callback to reflect that it's not called *on* or *after* split, but rather some time before the splitting to check if it's possible. Signed-off-by: Dmitry Safonov --- drivers/dax/device.c | 4 ++-- include/linux/mm.h | 3 ++- ipc/shm.c | 8 ++++---- mm/hugetlb.c | 2 +- mm/mmap.c | 4 ++-- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/drivers/dax/device.c b/drivers/dax/device.c index 1e89513f3c59..223dd1d13d5c 100644 --- a/drivers/dax/device.c +++ b/drivers/dax/device.c @@ -276,7 +276,7 @@ static vm_fault_t dev_dax_fault(struct vm_fault *vmf) return dev_dax_huge_fault(vmf, PE_SIZE_PTE); } -static int dev_dax_split(struct vm_area_struct *vma, unsigned long addr) +static int dev_dax_may_split(struct vm_area_struct *vma, unsigned long addr) { struct file *filp = vma->vm_file; struct dev_dax *dev_dax = filp->private_data; @@ -299,7 +299,7 @@ static unsigned long dev_dax_pagesize(struct vm_area_struct *vma) static const struct vm_operations_struct dax_vm_ops = { .fault = dev_dax_fault, .huge_fault = dev_dax_huge_fault, - .split = dev_dax_split, + .may_split = dev_dax_may_split, .pagesize = dev_dax_pagesize, }; diff --git a/include/linux/mm.h b/include/linux/mm.h index fd51a4a1f722..90887661ef44 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -549,7 +549,8 @@ enum page_entry_size { struct vm_operations_struct { void (*open)(struct vm_area_struct * area); void (*close)(struct vm_area_struct * area); - int (*split)(struct vm_area_struct * area, unsigned long addr); + /* Called any time before splitting to check if it's allowed */ + int (*may_split)(struct vm_area_struct *area, unsigned long addr); int (*mremap)(struct vm_area_struct *area, unsigned long flags); vm_fault_t (*fault)(struct vm_fault *vmf); vm_fault_t (*huge_fault)(struct vm_fault *vmf, diff --git a/ipc/shm.c b/ipc/shm.c index e25c7c6106bc..febd88daba8c 100644 --- a/ipc/shm.c +++ b/ipc/shm.c @@ -434,13 +434,13 @@ static vm_fault_t shm_fault(struct vm_fault *vmf) return sfd->vm_ops->fault(vmf); } -static int shm_split(struct vm_area_struct *vma, unsigned long addr) +static int shm_may_split(struct vm_area_struct *vma, unsigned long addr) { struct file *file = vma->vm_file; struct shm_file_data *sfd = shm_file_data(file); - if (sfd->vm_ops->split) - return sfd->vm_ops->split(vma, addr); + if (sfd->vm_ops->may_split) + return sfd->vm_ops->may_split(vma, addr); return 0; } @@ -582,7 +582,7 @@ static const struct vm_operations_struct shm_vm_ops = { .open = shm_open, /* callback for a new vm-area open */ .close = shm_close, /* callback for when the vm-area is released */ .fault = shm_fault, - .split = shm_split, + .may_split = shm_may_split, .pagesize = shm_pagesize, #if defined(CONFIG_NUMA) .set_policy = shm_set_policy, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 67fc6383995b..c4dc0e453be1 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3759,7 +3759,7 @@ const struct vm_operations_struct hugetlb_vm_ops = { .fault = hugetlb_vm_op_fault, .open = hugetlb_vm_op_open, .close = hugetlb_vm_op_close, - .split = hugetlb_vm_op_split, + .may_split = hugetlb_vm_op_split, .pagesize = hugetlb_vm_op_pagesize, }; diff --git a/mm/mmap.c b/mm/mmap.c index 50f853b0ec39..a62cb3ccafce 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2693,8 +2693,8 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma, struct vm_area_struct *new; int err; - if (vma->vm_ops && vma->vm_ops->split) { - err = vma->vm_ops->split(vma, addr); + if (vma->vm_ops && vma->vm_ops->may_split) { + err = vma->vm_ops->may_split(vma, addr); if (err) return err; } From patchwork Tue Oct 13 01:34:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 11834869 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B88A392C for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9D0C220678 for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="CYzWXkjH" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728264AbgJMBea (ORCPT ); Mon, 12 Oct 2020 21:34:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728253AbgJMBe2 (ORCPT ); Mon, 12 Oct 2020 21:34:28 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E797C0613D7 for ; Mon, 12 Oct 2020 18:34:28 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id h7so21863856wre.4 for ; Mon, 12 Oct 2020 18:34:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o+hT7zzmskqvyrywNbGv1SqYy6yeRy0QTDltdgYSbdc=; b=CYzWXkjHBNX+YU71eO5qpuuPH6qwUIdqJ/91p3uYMEMCdZAKCSTrNpq5TVbhmE0CD3 I/VxDj5wkbfpz7t0kQmbILNl/5EseAGLKDK8dCemcemqrzP3b56toQhhE3rNbnkWmkmm AICs1qLVVt2lax+VdQCmztePp5r0dg4+rFYAPJGdEALV5rSwyMg9LJiqUmL79gPixms9 ntjlR6MHQ1kYOjnTSbioqJvPiK4/DfA2peP3dpqJYRhN6pOfIolRGMfCAz3AdiWY+SBD Hjd8PJdjC3spCD+xAvw0TX9aA0hdWnJyRznCUfgv2rcesWONvNxkVjwNeIhJxKvga2lq sbdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=o+hT7zzmskqvyrywNbGv1SqYy6yeRy0QTDltdgYSbdc=; b=J77cXi8Nj2SPPJ8IbDEuDFCncsaKR4+91byrf2GHVix5f1Xw/WkTGREOsrSeKkIAab yq3pi6TESEymjUCOTxYqVZFbQ/61/AxV4famaCIOpUbk/Kr7AjHy+dzi/m1881604kKL 6QkV1yxbUvkwBPULUyziISSCcBkoI7XJDFCG2koydieSW1220sxVtWPUusltJU1I2F2/ f9iNlGnxvw+rpdYrqgJ1SUFCbI6jOkibLt8R92mdPjtwg1BYOGBotYL3/iCqu9tt/hJ0 nguYfGh910w3pge+dt9cO6B6GZAa/MF7DHXPTZOjV/tGJ/aND/yiGvgrjjGgtb3tIkIs SG3w== X-Gm-Message-State: AOAM533UpmfZgu8rmRqE+OriEjFzvgFeWNh2SAnUIxZjOnalVSv+5ffA J4oIyNJeKL0jJ/sLFmxAtZ+6IA== X-Google-Smtp-Source: ABdhPJx/eA0xaURdD39kuIvNIumfP2FJFkk5wEpy22MoNkD2iAsN3onMEMvZoLmBi3z0b1VkhtsO1w== X-Received: by 2002:a5d:4c8d:: with SMTP id z13mr16731759wrs.412.1602552866741; Mon, 12 Oct 2020 18:34:26 -0700 (PDT) Received: from localhost.localdomain ([2a02:8084:e84:2480:228:f8ff:fe6f:83a8]) by smtp.gmail.com with ESMTPSA id d23sm24825325wmb.6.2020.10.12.18.34.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Oct 2020 18:34:26 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Alexander Viro , Andrew Morton , Andy Lutomirski , Brian Geffon , Catalin Marinas , Dan Williams , Dave Jiang , Hugh Dickins , Ingo Molnar , "Kirill A. Shutemov" , Mike Kravetz , Minchan Kim , Russell King , Thomas Bogendoerfer , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 5/6] mremap: Check if it's possible to split original vma Date: Tue, 13 Oct 2020 02:34:15 +0100 Message-Id: <20201013013416.390574-6-dima@arista.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201013013416.390574-1-dima@arista.com> References: <20201013013416.390574-1-dima@arista.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org If original VMA can't be split at the desired address, do_munmap() will fail and leave both new-copied VMA and old VMA. De-facto it's MREMAP_DONTUNMAP behaviour, which is unexpected. Currently, it may fail such way for hugetlbfs and dax device mappings. Minimize such unpleasant situations to OOM by checking .may_split() before attempting to create a VMA copy. Signed-off-by: Dmitry Safonov --- mm/mremap.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/mm/mremap.c b/mm/mremap.c index 898e9818ba6d..3c4047c23673 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -343,7 +343,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, unsigned long excess = 0; unsigned long hiwater_vm; int split = 0; - int err; + int err = 0; bool need_rmap_locks; /* @@ -353,6 +353,15 @@ static unsigned long move_vma(struct vm_area_struct *vma, if (mm->map_count >= sysctl_max_map_count - 3) return -ENOMEM; + if (vma->vm_ops && vma->vm_ops->may_split) { + if (vma->vm_start != old_addr) + err = vma->vm_ops->may_split(vma, old_addr); + if (!err && vma->vm_end != old_addr + old_len) + err = vma->vm_ops->may_split(vma, old_addr + old_len); + if (err) + return err; + } + /* * Advise KSM to break any KSM pages in the area to be moved: * it would be confusing if they were to turn up at the new From patchwork Tue Oct 13 01:34:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 11834871 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DE5D21862 for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C1CF720678 for ; Tue, 13 Oct 2020 02:45:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="YoPP5Gks" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728342AbgJMBeo (ORCPT ); Mon, 12 Oct 2020 21:34:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728263AbgJMBe3 (ORCPT ); Mon, 12 Oct 2020 21:34:29 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BA0CC0613D0 for ; Mon, 12 Oct 2020 18:34:29 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id x7so13231099wrl.3 for ; Mon, 12 Oct 2020 18:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QI03JfUdAeqx8H20V8QB+drTDnXgJr9KGaylcrY6E98=; b=YoPP5GksBayPf3YA7iKAhJRnUEw9hLY1epr3iRj1JZcb8AczM+hA8gE9m8KN3op2hH oJsdXJN/juKuz/IX0LfynhImiuc5BXLZODpopg9/dKr6AO5DRbo9eA+/Tnk/Gabp3M32 tkI9HX8Xfzgr1+RZ8d30CYRYuDbqOlpwz4S6/cLFPpzLykgpt4SgsKyVRhuDvdZU7s4v NAO+0SFtu8dVFDXQeh+Auwyk497u1Pq+RVy+QnuzyxP0dcf8CWnY74eu++KCgV7f6NBm tPhM5ye4nI4QHXHZl0BXKrOGmccM9xxAGF+qOIqP9KaDmawf/AP/lR7OtcjlGuH0ZUil +mlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QI03JfUdAeqx8H20V8QB+drTDnXgJr9KGaylcrY6E98=; b=pktax9aiJnDs2JwgkQCLVRXHU/NT01UnK8WOnQCXSyQvGfbZPWPG1cKJJ+zyr1wXV6 XtLw5ul5IyGe6/1BP+oZLUABmJRivhnRipilZ0L8sk3xOAVfCsweaT6ZdGvCnQvz0Dvc p0bbyHEWV65m48YoBBfWaXmkfWEL1bE8upAdNugdle6yLNoKZrARNUkFGOFm47sb0EM9 1tcv/RFgEtMiu/dm8SQqfpMs8wENmSYusAdDZkBjKj4HFArTY/JPqKlzWu3GtvEfnWEA 7X/RRC5HpErCNu9seDoWnBdK9kyzYHBj1Ny/xDrgrvzhZbGXaJAd72Q2e2ahArIQLe/K 0k2Q== X-Gm-Message-State: AOAM533c7yAzcVKniz5Oy4eZxuRgj32djgExYrUFqzboIYv7VlG4GRau dibGlJiWcRXqoNBT/uICrWK8cA== X-Google-Smtp-Source: ABdhPJzRUrGoqc2gRFrRPbwBQLI+3EviW230fK5qUbXR+5lWN/w2oMhECQzgHSMuyap8rQ2Z9wPpZQ== X-Received: by 2002:adf:e64e:: with SMTP id b14mr7124845wrn.68.1602552868235; Mon, 12 Oct 2020 18:34:28 -0700 (PDT) Received: from localhost.localdomain ([2a02:8084:e84:2480:228:f8ff:fe6f:83a8]) by smtp.gmail.com with ESMTPSA id d23sm24825325wmb.6.2020.10.12.18.34.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Oct 2020 18:34:27 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Alexander Viro , Andrew Morton , Andy Lutomirski , Brian Geffon , Catalin Marinas , Dan Williams , Dave Jiang , Hugh Dickins , Ingo Molnar , "Kirill A. Shutemov" , Mike Kravetz , Minchan Kim , Russell King , Thomas Bogendoerfer , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 6/6] mm: Forbid splitting special mappings Date: Tue, 13 Oct 2020 02:34:16 +0100 Message-Id: <20201013013416.390574-7-dima@arista.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201013013416.390574-1-dima@arista.com> References: <20201013013416.390574-1-dima@arista.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Don't allow splitting of vm_special_mapping's. It affects vdso/vvar areas. Uprobes have only one page in xol_area so they aren't affected. Those restrictions were enforced by checks in .mremap() callbacks. Restrict resizing with generic .split() callback. Signed-off-by: Dmitry Safonov Acked-by: Will Deacon --- arch/arm/kernel/vdso.c | 9 --------- arch/arm64/kernel/vdso.c | 41 +++------------------------------------ arch/mips/vdso/genvdso.c | 4 ---- arch/s390/kernel/vdso.c | 11 +---------- arch/x86/entry/vdso/vma.c | 17 ---------------- mm/mmap.c | 12 ++++++++++++ 6 files changed, 16 insertions(+), 78 deletions(-) diff --git a/arch/arm/kernel/vdso.c b/arch/arm/kernel/vdso.c index fddd08a6e063..3408269d19c7 100644 --- a/arch/arm/kernel/vdso.c +++ b/arch/arm/kernel/vdso.c @@ -50,15 +50,6 @@ static const struct vm_special_mapping vdso_data_mapping = { static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma) { - unsigned long new_size = new_vma->vm_end - new_vma->vm_start; - unsigned long vdso_size; - - /* without VVAR page */ - vdso_size = (vdso_total_pages - 1) << PAGE_SHIFT; - - if (vdso_size != new_size) - return -EINVAL; - current->mm->context.vdso = new_vma->vm_start; return 0; diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c index d4202a32abc9..a1a4220a405b 100644 --- a/arch/arm64/kernel/vdso.c +++ b/arch/arm64/kernel/vdso.c @@ -82,17 +82,9 @@ static union { } vdso_data_store __page_aligned_data; struct vdso_data *vdso_data = vdso_data_store.data; -static int __vdso_remap(enum vdso_abi abi, - const struct vm_special_mapping *sm, - struct vm_area_struct *new_vma) +static int vdso_mremap(const struct vm_special_mapping *sm, + struct vm_area_struct *new_vma) { - unsigned long new_size = new_vma->vm_end - new_vma->vm_start; - unsigned long vdso_size = vdso_info[abi].vdso_code_end - - vdso_info[abi].vdso_code_start; - - if (vdso_size != new_size) - return -EINVAL; - current->mm->context.vdso = (void *)new_vma->vm_start; return 0; @@ -223,17 +215,6 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm, return vmf_insert_pfn(vma, vmf->address, pfn); } -static int vvar_mremap(const struct vm_special_mapping *sm, - struct vm_area_struct *new_vma) -{ - unsigned long new_size = new_vma->vm_end - new_vma->vm_start; - - if (new_size != VVAR_NR_PAGES * PAGE_SIZE) - return -EINVAL; - - return 0; -} - static int __setup_additional_pages(enum vdso_abi abi, struct mm_struct *mm, struct linux_binprm *bprm, @@ -284,14 +265,6 @@ static int __setup_additional_pages(enum vdso_abi abi, /* * Create and map the vectors page for AArch32 tasks. */ -#ifdef CONFIG_COMPAT_VDSO -static int aarch32_vdso_mremap(const struct vm_special_mapping *sm, - struct vm_area_struct *new_vma) -{ - return __vdso_remap(VDSO_ABI_AA32, sm, new_vma); -} -#endif /* CONFIG_COMPAT_VDSO */ - enum aarch32_map { AA32_MAP_VECTORS, /* kuser helpers */ #ifdef CONFIG_COMPAT_VDSO @@ -313,11 +286,10 @@ static struct vm_special_mapping aarch32_vdso_maps[] = { [AA32_MAP_VVAR] = { .name = "[vvar]", .fault = vvar_fault, - .mremap = vvar_mremap, }, [AA32_MAP_VDSO] = { .name = "[vdso]", - .mremap = aarch32_vdso_mremap, + .mremap = vdso_mremap, }, #endif /* CONFIG_COMPAT_VDSO */ [AA32_MAP_SIGPAGE] = { @@ -465,12 +437,6 @@ int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) } #endif /* CONFIG_COMPAT */ -static int vdso_mremap(const struct vm_special_mapping *sm, - struct vm_area_struct *new_vma) -{ - return __vdso_remap(VDSO_ABI_AA64, sm, new_vma); -} - enum aarch64_map { AA64_MAP_VVAR, AA64_MAP_VDSO, @@ -480,7 +446,6 @@ static struct vm_special_mapping aarch64_vdso_maps[] __ro_after_init = { [AA64_MAP_VVAR] = { .name = "[vvar]", .fault = vvar_fault, - .mremap = vvar_mremap, }, [AA64_MAP_VDSO] = { .name = "[vdso]", diff --git a/arch/mips/vdso/genvdso.c b/arch/mips/vdso/genvdso.c index abb06ae04b40..09e30eb4be86 100644 --- a/arch/mips/vdso/genvdso.c +++ b/arch/mips/vdso/genvdso.c @@ -263,10 +263,6 @@ int main(int argc, char **argv) fprintf(out_file, " const struct vm_special_mapping *sm,\n"); fprintf(out_file, " struct vm_area_struct *new_vma)\n"); fprintf(out_file, "{\n"); - fprintf(out_file, " unsigned long new_size =\n"); - fprintf(out_file, " new_vma->vm_end - new_vma->vm_start;\n"); - fprintf(out_file, " if (vdso_image.size != new_size)\n"); - fprintf(out_file, " return -EINVAL;\n"); fprintf(out_file, " current->mm->context.vdso =\n"); fprintf(out_file, " (void *)(new_vma->vm_start);\n"); fprintf(out_file, " return 0;\n"); diff --git a/arch/s390/kernel/vdso.c b/arch/s390/kernel/vdso.c index c4baefaa6e34..291ead792d64 100644 --- a/arch/s390/kernel/vdso.c +++ b/arch/s390/kernel/vdso.c @@ -59,17 +59,8 @@ static vm_fault_t vdso_fault(const struct vm_special_mapping *sm, static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *vma) { - unsigned long vdso_pages; - - vdso_pages = vdso64_pages; - - if ((vdso_pages << PAGE_SHIFT) != vma->vm_end - vma->vm_start) - return -EINVAL; - - if (WARN_ON_ONCE(current->mm != vma->vm_mm)) - return -EFAULT; - current->mm->context.vdso_base = vma->vm_start; + return 0; } diff --git a/arch/x86/entry/vdso/vma.c b/arch/x86/entry/vdso/vma.c index 9185cb1d13b9..0c942b31825d 100644 --- a/arch/x86/entry/vdso/vma.c +++ b/arch/x86/entry/vdso/vma.c @@ -89,30 +89,14 @@ static void vdso_fix_landing(const struct vdso_image *image, static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma) { - unsigned long new_size = new_vma->vm_end - new_vma->vm_start; const struct vdso_image *image = current->mm->context.vdso_image; - if (image->size != new_size) - return -EINVAL; - vdso_fix_landing(image, new_vma); current->mm->context.vdso = (void __user *)new_vma->vm_start; return 0; } -static int vvar_mremap(const struct vm_special_mapping *sm, - struct vm_area_struct *new_vma) -{ - const struct vdso_image *image = new_vma->vm_mm->context.vdso_image; - unsigned long new_size = new_vma->vm_end - new_vma->vm_start; - - if (new_size != -image->sym_vvar_start) - return -EINVAL; - - return 0; -} - #ifdef CONFIG_TIME_NS static struct page *find_timens_vvar_page(struct vm_area_struct *vma) { @@ -252,7 +236,6 @@ static const struct vm_special_mapping vdso_mapping = { static const struct vm_special_mapping vvar_mapping = { .name = "[vvar]", .fault = vvar_fault, - .mremap = vvar_mremap, }; /* diff --git a/mm/mmap.c b/mm/mmap.c index a62cb3ccafce..41100f6505ff 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3389,6 +3389,17 @@ static int special_mapping_mremap(struct vm_area_struct *new_vma, return 0; } +static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) +{ + /* + * Forbid splitting special mappings - kernel has expectations over + * the number of pages in mapping. Together with VM_DONTEXPAND + * the size of vma should stay the same over the special mapping's + * lifetime. + */ + return -EINVAL; +} + static const struct vm_operations_struct special_mapping_vmops = { .close = special_mapping_close, .fault = special_mapping_fault, @@ -3396,6 +3407,7 @@ static const struct vm_operations_struct special_mapping_vmops = { .name = special_mapping_name, /* vDSO code relies that VVAR can't be accessed remotely */ .access = NULL, + .may_split = special_mapping_split, }; static const struct vm_operations_struct legacy_special_mapping_vmops = {