From patchwork Fri Aug 16 11:13:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bert Karwatzki X-Patchwork-Id: 13765999 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 D932AC3DA4A for ; Fri, 16 Aug 2024 11:14:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C3766B0141; Fri, 16 Aug 2024 07:14:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64B736B0142; Fri, 16 Aug 2024 07:14:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4ED4B6B0148; Fri, 16 Aug 2024 07:14:31 -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 30B986B0141 for ; Fri, 16 Aug 2024 07:14:31 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DFEAA1A1AF9 for ; Fri, 16 Aug 2024 11:14:30 +0000 (UTC) X-FDA: 82457850300.12.48AA5D6 Received: from mout.web.de (mout.web.de [217.72.192.78]) by imf28.hostedemail.com (Postfix) with ESMTP id E6D93C000A for ; Fri, 16 Aug 2024 11:14:28 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b=HIKle6HL; spf=pass (imf28.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=1723806832; 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=0obejYoa2r7hw/w5zRqv2QmgIUrn0jX3NuGgwo7FlG8=; b=W7zcaWJb4vadFqAMAOuJVLFRwFOpaXfvq+gy422YWslEvmab+N66H6t+mUswoqExeP3p16 YWouG+bLcd2KOgLAu04F9b1x2fWJD7HGvIWXzH63eF1QSMtxfeZSkkFLR0lJJC1U72M79l FQxxWUJ8WoA9M/GSt/drV6VTAIMEF80= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=web.de header.s=s29768273 header.b=HIKle6HL; spf=pass (imf28.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=1723806832; a=rsa-sha256; cv=none; b=RDoYm0SjFN+yGB6KRIU6NA1B5rXc1x3KXRDNaUkYyLWteQpsoqL+ywHIG5F77xgCdiYRFu 2OH0W1zm1L9fn+dScz0DVAABDQbvARBcR58B7p3gZc4YpW7NHmaxOEXH9JJuSgT1HSTeat 0Ov5cZ6HYXJkqTZEXwPyeZVgulWAZvU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1723806861; x=1724411661; i=spasswolf@web.de; bh=0obejYoa2r7hw/w5zRqv2QmgIUrn0jX3NuGgwo7FlG8=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=HIKle6HL3gik2rTP8fMx51a1KXQCUfWcUkbj5im6liQLmMAMbETBT2P/PLQLctmI wB+KQZZnNgoMVsc1pUf96GR1kjXkPYZ9JvI18CLEWXeM+77LTAhc0BV0GMtlkl/xJ Zm8uq3dJ3tma0hkBl7mf/G06FaKbgB347mJTgVkUpGMr6cSG6u9AFKZy6QTVGLzWq WDiS0ifH6K4yAwyZGy+Y81+uO3DfQ1HJ4+SCRLxzQeFCV0vcsIQlozGMFNHEHZ+u6 PffBHx58cNAZtZavb5XQ6IO6ft8FSWLz8pYeZcUsGAnV+oI2DsYScTd0vNO0EkTww y75iYhSdrrG+Nl466w== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from localhost.localdomain ([84.119.92.193]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1My6lR-1sEzR41xWx-00zkD7; Fri, 16 Aug 2024 13:14:21 +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 , Lorenzo Stoakes Subject: [PATCH v5.1 03/19] mm/mmap: Introduce vmi_complete_munmap_vmas() Date: Fri, 16 Aug 2024 13:13:44 +0200 Message-ID: <20240816111405.11793-4-spasswolf@web.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240816111405.11793-1-spasswolf@web.de> References: <20240816111405.11793-1-spasswolf@web.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:eqWc1iB4g+G2VRb/rfgfTQ4sRq2PmtECXZa+ZieLZIbxA1UeXNo 0Ym4FiJ7Auovew6A0uW84ljNoqkHTaxNAX4YQto5wUKBt5IU2jTjlCP7i2csrJfTUb8dW6l WiqqSYLmF7P4CIJdkfkyCm/Fe1bLnggOsqruJeBk2lff+eAhZL2is4UG4EynqYVqTE4hNLz 3zIfEnBggLKafepPJI8xg== UI-OutboundReport: notjunk:1;M01:P0:0ZOF32seaA8=;vYnMaxAP0Qy1WSqp1i/hpOix4hI +RotyKtfiWNyobKZ9g1oSvEzRj/DTuKmFp/3GtpTLajVktOeGnEeUhqL9RetMT554UOrzAtu1 8M9+dpLjy8uPqlatJFFm7ljzWJ5rPPxMf3WfMzPk4MjWMPg4c+2DoMSyLsx6BG2uypX7Bh9Sb +K+it78vLrj8y2RzaQudTQbG4bFb5jG0CAOpRFUqpVtY7bCoB/m6+RLFhziMN0OYRQUOHkm0n O8OhBsrVo4goCDNXcl2UwNrfRf+COq6hQzp3qHermgG3JPBFObWCE74wqqyW1AgPQiYlMtxE1 U5AOHYQkOa78mj4CEuXzlxJH6fWDeqA8Dgstc5jfRIk/xYhbis+T+hLXH63lY5ICFzdVIlmGu ldLL3bjpEvfhj/qYYzXDDnYeiYhFmXFSr7itdp41w/JD26XA4MGAD6CbXBMyQ/EIupiKoerkO EWy+fIlyUucumAm4fzX1KKJzbu9BaIncMCDFDaoaD4hwT6kmNvAhAI7HnQIbSJeOTzecxFbvX 6FiG0MXdSwNAVBZHKWdEkCOBzTxqJgH11Oy0rwJKGSRQvkYF870pVhrkEYoVigGnfNhdVNvDE 1jSULfEgKpVfaWpfKZF0QsjnG0Hvpxi/tBxXMkQM1N9iRbPz3VJeYMwnipGDvKsX50t3t+6v5 mFxmysHS6s6I2Qt5Bej8admktNyFjT0AzYXKEzvWb20aX3rbV9WzaqZgQ4vL11FW6BitkzqB9 FlGBC8/cKI83JyRd7RU2Zvcv+k4RSfadHhDabsNGu5vdCQgm/MRhf9FOWha53dGJWSMPKtZbg PKWVm4bn4VtBl2RxjijWKlFg== X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: E6D93C000A X-Stat-Signature: 56bgjx8emttd75k536cz7bkk3j9zsiu7 X-HE-Tag: 1723806868-676715 X-HE-Meta: U2FsdGVkX19ursAXmEjbRisFjG5/v9UOHvP18LVStXbXsNpg4KhmIgmAuSuWoawOwtmbDnGsOwdXIpflMQXyfm+GqdNSKnssdv6JvmS4hYQd7lkZj49Is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yw6nla6oliGq661bXhqjYAF/c72Cbv/uYQcUeGgrauMbtHskKXZtCQvRi86HTo7uMfqgGkzMjaBBli2mBqXpScZjtfpdFYHz96t2Y6rAxnLiRVlfjkYh73BcvCcvonnFI9PXvwQ== 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: Extract all necessary operations that need to be completed after the vma maple tree is updated from a munmap() operation. Extracting this makes the later patch in the series easier to understand. Signed-off-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes Reviewed-by: Suren Baghdasaryan --- mm/vma.c | 81 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 26 deletions(-) -- 2.45.2 diff --git a/mm/vma.c b/mm/vma.c index 83260a3142b3..d331ba575019 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -684,6 +684,58 @@ static inline void abort_munmap_vmas(struct ma_state *mas_detach) __mt_destroy(mas_detach->tree); } +/* + * vmi_complete_munmap_vmas() - Finish the munmap() operation + * @vmi: The vma iterator + * @vma: The first vma to be munmapped + * @mm: The mm struct + * @start: The start address + * @end: The end address + * @unlock: Unlock the mm or not + * @mas_detach: them maple state of the detached vma maple tree + * @locked_vm: The locked_vm count in the detached vmas + * + * This function updates the mm_struct, unmaps the region, frees the resources + * used for the munmap() and may downgrade the lock - if requested. Everything + * needed to be done once the vma maple tree is updated. + */ +static void +vmi_complete_munmap_vmas(struct vma_iterator *vmi, struct vm_area_struct *vma, + struct mm_struct *mm, unsigned long start, + unsigned long end, bool unlock, struct ma_state *mas_detach, + unsigned long locked_vm) +{ + struct vm_area_struct *prev, *next; + int count; + + count = mas_detach->index + 1; + mm->map_count -= count; + mm->locked_vm -= locked_vm; + if (unlock) + mmap_write_downgrade(mm); + + prev = vma_iter_prev_range(vmi); + next = vma_next(vmi); + if (next) + vma_iter_prev_range(vmi); + + /* + * We can free page tables without write-locking mmap_lock because VMAs + * were isolated before we downgraded mmap_lock. + */ + mas_set(mas_detach, 1); + unmap_region(mm, mas_detach, vma, prev, next, start, end, count, + !unlock); + /* Statistics and freeing VMAs */ + mas_set(mas_detach, 0); + remove_mt(mm, mas_detach); + validate_mm(mm); + if (unlock) + mmap_read_unlock(mm); + + __mt_destroy(mas_detach->tree); +} + /* * do_vmi_align_munmap() - munmap the aligned region from @start to @end. * @vmi: The vma iterator @@ -703,7 +755,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, struct mm_struct *mm, unsigned long start, unsigned long end, struct list_head *uf, bool unlock) { - struct vm_area_struct *prev, *next = NULL; + struct vm_area_struct *next = NULL; struct maple_tree mt_detach; int count = 0; int error = -ENOMEM; @@ -807,31 +859,8 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, goto clear_tree_failed; /* Point of no return */ - mm->locked_vm -= locked_vm; - mm->map_count -= count; - if (unlock) - mmap_write_downgrade(mm); - - prev = vma_iter_prev_range(vmi); - next = vma_next(vmi); - if (next) - vma_iter_prev_range(vmi); - - /* - * We can free page tables without write-locking mmap_lock because VMAs - * were isolated before we downgraded mmap_lock. - */ - mas_set(&mas_detach, 1); - unmap_region(mm, &mas_detach, vma, prev, next, start, end, count, - !unlock); - /* Statistics and freeing VMAs */ - mas_set(&mas_detach, 0); - remove_mt(mm, &mas_detach); - validate_mm(mm); - if (unlock) - mmap_read_unlock(mm); - - __mt_destroy(&mt_detach); + vmi_complete_munmap_vmas(vmi, vma, mm, start, end, unlock, &mas_detach, + locked_vm); return 0; clear_tree_failed: