From patchwork Fri Aug 16 16:10:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bert Karwatzki X-Patchwork-Id: 13766578 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 0C27AC531DC for ; Fri, 16 Aug 2024 16:10:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92E2E8D008C; Fri, 16 Aug 2024 12:10:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DE1A8D007E; Fri, 16 Aug 2024 12:10:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CC478D008C; Fri, 16 Aug 2024 12:10:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 58E468D007E for ; Fri, 16 Aug 2024 12:10:44 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0A2E1A0125 for ; Fri, 16 Aug 2024 16:10:44 +0000 (UTC) X-FDA: 82458596808.19.B4AFA1B Received: from mout.web.de (mout.web.de [217.72.192.78]) by imf14.hostedemail.com (Postfix) with ESMTP id E4CCB10000D for ; Fri, 16 Aug 2024 16:10:41 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b="Hm/VZ4nV"; spf=pass (imf14.hostedemail.com: domain of spasswolf@web.de designates 217.72.192.78 as permitted sender) smtp.mailfrom=spasswolf@web.de; dmarc=pass (policy=quarantine) header.from=web.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723824628; 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:references:dkim-signature; bh=tY1u/l18ICxFndL+rMYHYdlbuL5/Ji75gvQvXubIHqs=; b=XcVX5cGgnA4+IurPgYzkbY4K/jSlADuakgkkhEldi6GeW5+feLuHAax4EsnXQ/lespuruD T0fr+bv7oqosyup7c1zzZ9rFTH+bsn7jxlOfo+5LzQ9ryx6Y8MkLg0cQbUgY9Nt3CHaTm6 cu+PS5dr+LcFZCf8Xa4jfnsghXlhmuM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b="Hm/VZ4nV"; spf=pass (imf14.hostedemail.com: domain of spasswolf@web.de designates 217.72.192.78 as permitted sender) smtp.mailfrom=spasswolf@web.de; dmarc=pass (policy=quarantine) header.from=web.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723824628; a=rsa-sha256; cv=none; b=h0a9Vzj9G8qnzj7ewLKnqwahJTjEzFCp9Se+/zmIuQNuxFekDgIVhUQaZjoXbYLjOah4Um SRPtuzh5RmyHeP8sPUXDIildN7Tcmo8jhvDlsEAfbHccIqEDA0qF5lnUpWLtKTR0T/tV95 B3QFLumZDXkK2xNPbBs3G4BaGWgwoUQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1723824634; x=1724429434; i=spasswolf@web.de; bh=tY1u/l18ICxFndL+rMYHYdlbuL5/Ji75gvQvXubIHqs=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=Hm/VZ4nVyBdr5dDUQNibfqMELTiimJ+x3CK5jWY5wydb/FCVtPBhUdDFKjewTK0L utd1GQ4xBY9YI1hBOuxPV74EEEsS0qfuZg8sZsO8ZpYYtWBPBlSsL4rMxZDzPZSYr R6NZSV55IFL9bNNRUETyp+csYo/R+DUgmuPLp+4XAakp0CRvpA0WvWSZ15soIkBow 7IWy2epJ6itNjOOAI8l9d7dpPsX0fh32xjHfUwitePRCqDJOXLe47Pvym9OJN2r+Y AUBC4yyBw6kx3VLT1vVvoEGqCCNOndOf76c3N4hTWGy46fFrDfvYrGdwzANuNhoRu xeTHQuWvh9Y7fSx0jw== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from localhost.localdomain ([84.119.92.193]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MjgX3-1ru43N0hrf-00dbwl; Fri, 16 Aug 2024 18:10:34 +0200 From: Bert Karwatzki To: "Liam R . Howlett" Cc: Bert Karwatzki , Suren Baghdasaryan , Vlastimil Babka , Lorenzo Stoakes , Matthew Wilcox , sidhartha.kumar@oracle.com, "Paul E . McKenney" , Jiri Olsa , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Kees Cook , Jeff Xu , "Liam R . Howlett" , Lorenzo Stoakes Subject: [PATCH v5.1 19/19] mm/mmap: Move may_expand_vm() check in mmap_region() Date: Fri, 16 Aug 2024 18:10:27 +0200 Message-ID: <20240816161029.2865-1-spasswolf@web.de> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Provags-ID: V03:K1:CpBP6kSbrB8sL8flKl+ECNfHj/Uc35TmetZAHX0QnWtel46XMNb ZiZBIbboeJJ6tVLYcRzceYO1MDLzuDd23AZvXklcX1N5/dSjrmMN5hc8CUFPzWDqomsO6Zj efZvSyAq2qKR3ajjesfQhlBS1V7I1Zo2tl3gcC0mMHjTj4rdN+sSq+wKGpyMPyFIomNU2X5 SkFEdwpRClee9hPkap6sQ== UI-OutboundReport: notjunk:1;M01:P0:TD/RjrwwdTk=;IQOlooLy+sbkfuRILnJuuPi8E05 eTOFKmGrhsyKxUGmTKfbVqPOTXKxrVCFgKtxI9Ge35vs1fZaRTnG2vkZqgesytNioXiXSeaE0 QYgatLk2+Y1P18xgsrv6jvAWBcxTIogQn/00Tu4bXu3m+E39ZSE7jnp/UEmARv/W26cfn/5FP BKWdOQr8bNvp85EcYFMpEAtYyfbPsQmkFqrnV4UNWg5tK1TTxyVMx2LeKleR9oNk011/34Hc2 jW3UOFZiXz97xpsHUoA09NrJPiK+cvSHbRPFOjLq2wnVM8rysqI4AFlkEA8JlaE4C94quRVlZ ERLXkAGDDBwZx2CY0Tnczw57wBX9eOb2qneQGjhUcQkzsUO2abmTwcqiUBvJluIShOCN98rEb qD1q0EGDRWCciyL7HNr/Vaptu7hTsopiXiSHMqxW4pf+LxqDYgFfri+mVltiQsCkdDw4QB4jz C8jSXFeFsYhBR8HKHaaFBfNQlZApxM+x01QXwvPQWafgZc67ePXg4JsiD3TR0spEkmIHZ2dAe IgLhtupZJXjT3EHG9UsbJ6f73QYd9/46PMukJXTgzPkYKZc2JnEMChXZJnrhAYsxq26p+Fwsi txnXkeneShJYs+QayAhRAhdgfOCfJXqLqw18w368jRPdrtOvIpKtDNyz0m46RRC6m5nJlEwR8 794pRsOvVEYGW+PAcQxu0sEHjdN0hNbQmZ+OrMSp4eUaUFEtCWh/KzEK4iIcJVTwfXZHPUzyj TZm9s7eQbXgP1E8bpkmddo0KD4NN8BUkq+/HB5Vqev6YuwJb36knkUDhAuNxveFacQo5YT/Zd eWjaBNunMLJMmts+0dSNhg3Q== X-Rspam-User: X-Stat-Signature: cej3xcwrpzupq8mx5o11mo316tem839r X-Rspamd-Queue-Id: E4CCB10000D X-Rspamd-Server: rspam11 X-HE-Tag: 1723824641-954905 X-HE-Meta: U2FsdGVkX19/YaJjm9qnuR2v+gfpiNOzk8jsoe1JU2ohv1nhv+TqC5F4SWlRESM641T4nwzZMJn5LP2ib+JfDvEDhq3HeOgtvvQV7iRuOEmHkKd11ooLXN18WPDFoW8+YSAf0Nz7oTWD9ypg7RKvkWmuBuTC2XjABJfmSleX7/8AZx7G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ZqF1t3OWpPRmV72/3JM5nnVJ1LMbbL+srEldidsMWROFq9gNw6wyUJFDGm6q4AwyaH 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: The may_expand_vm() check requires the count of the pages within the munmap range. Since this is needed for accounting and obtained later, the reodering of may_expand_vm() to later in the call stack, after the vma munmap struct (vms) is initialised and the gather stage is potentially run, will allow for a single loop over the vmas. The gather sage does not commit any work and so everything can be undone in the case of a failure. The MAP_FIXED page count is available after the vms_gather_munmap_vmas() call, so use it instead of looping over the vmas twice. Signed-off-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/mmap.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) -- 2.45.2 diff --git a/mm/mmap.c b/mm/mmap.c index d2f47cd66650..e0c321b7c642 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1376,17 +1376,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr, pgoff_t vm_pgoff; int error = -ENOMEM; VMA_ITERATOR(vmi, mm, addr); - unsigned long nr_pages, nr_accounted; - - nr_pages = count_vma_pages_range(mm, addr, end, &nr_accounted); - - /* - * Check against address space limit. - * MAP_FIXED may remove pages of mappings that intersects with requested - * mapping. Account for the pages it would unmap. - */ - if (!may_expand_vm(mm, vm_flags, pglen - nr_pages)) - return -ENOMEM; /* Find the first overlapping VMA */ @@ -1414,6 +1403,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma_iter_next_range(&vmi); } + /* Check against address space limit. */ + if (!may_expand_vm(mm, vm_flags, pglen - vms.nr_pages)) + goto abort_munmap; + /* * Private writable mapping: check memory availability */