From mboxrd@z Thu Jan 1 00:00:00 1970 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 7D3F5C43334 for ; Thu, 16 Jun 2022 08:23:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E8E346B0072; Thu, 16 Jun 2022 04:23:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3D5D6B0073; Thu, 16 Jun 2022 04:23:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CDDE16B0074; Thu, 16 Jun 2022 04:23:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BC1A26B0072 for ; Thu, 16 Jun 2022 04:23:16 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 89EDB35ABB for ; Thu, 16 Jun 2022 08:23:16 +0000 (UTC) X-FDA: 79583409192.07.74004B0 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf16.hostedemail.com (Postfix) with ESMTP id F253B180010 for ; Thu, 16 Jun 2022 08:23:15 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id A7C6E1FAD8; Thu, 16 Jun 2022 08:23:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1655367794; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AUtA210qzdZ6fUsIsgH+SNKu39X41m219nLUSwZSKSI=; b=x48p8S9rEBW7ROB6oU5fxO/F+ubrR2bW19nxIQ4OFVb05QFFt6UeB941rbdkiNot/Ly3MW a84vmC35p1IBHOPIigJm0wC1XWyBOdpw7iojpe5mD3IZTB7Pi5yH4Io2p4Bv63HdgaHOy1 8ItwmWGf5bSJDMnPy56o+P9CSXtY1vw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1655367794; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AUtA210qzdZ6fUsIsgH+SNKu39X41m219nLUSwZSKSI=; b=zuqSic0tew8V31tKSaYi8+u2Od5jxQ7D5n53XNn2tx9ObuOxGMIGrFGzTBVQuT1ffvaJYc gVqZbQm48eM0HbBg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 713F21344E; Thu, 16 Jun 2022 08:23:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id CMLvGnLoqmJYLgAAMHmgww (envelope-from ); Thu, 16 Jun 2022 08:23:14 +0000 Message-ID: Date: Thu, 16 Jun 2022 10:23:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [PATCH v4 2/2] [PATCH 2/2] mm: add merging after mremap resize Content-Language: en-US To: =?UTF-8?Q?Jakub_Mat=c4=9bna?= , akpm@linux-foundation.org Cc: linux-mm@kvack.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org, mhocko@kernel.org, mgorman@techsingularity.net, willy@infradead.org, liam.howlett@oracle.com, hughd@google.com, kirill@shutemov.name, riel@surriel.com, rostedt@goodmis.org, peterz@infradead.org References: <20220603145719.1012094-1-matenajakub@gmail.com> <20220603145719.1012094-3-matenajakub@gmail.com> From: Vlastimil Babka In-Reply-To: <20220603145719.1012094-3-matenajakub@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655367796; a=rsa-sha256; cv=none; b=h9eWN/1h6u1bV2DKuibK8rMhqo87J49f4ZzwfcfRj9QxMNm/wv9F1gK44YFjQLmdYFEXM1 KB8e9bcuvtc+cRGglFHMe1f/8ejSRvMYzBuTLJVVXnMx6/s4EbrPiTek9Zu/o+Iszue0mM c6KLZ6PwhkYGLvI1AGi/BdGLZIcXHDQ= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=x48p8S9r; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=zuqSic0t; dmarc=none; spf=pass (imf16.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655367796; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AUtA210qzdZ6fUsIsgH+SNKu39X41m219nLUSwZSKSI=; b=4Tdn0CV4WpP0RrA59fHb1qX1x5DfivkYHXQVd84mktqqz6CljqKg4oPWq37wF94FM7/X4t tlS2+Z8yEXuvvKmFS1j0NruuZpSC4KNGm7ZIkXp4fI6EthERJeDAgR/B7cTu7k8JmYf5lp JHz39nTFaP673pLMH0Zo87wMwBeBcfo= Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=x48p8S9r; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=zuqSic0t; dmarc=none; spf=pass (imf16.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz X-Rspam-User: X-Stat-Signature: y6r5z3pdq4dg8mjhfqa9595k5mrgmi4h X-Rspamd-Queue-Id: F253B180010 X-Rspamd-Server: rspam08 X-HE-Tag: 1655367795-883093 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: On 6/3/22 16:57, Jakub Matěna wrote: > When mremap call results in expansion, it might be possible to merge the > VMA with the next VMA which might become adjacent. This patch adds > vma_merge call after the expansion is done to try and merge. > > Signed-off-by: Jakub Matěna Reviewed-by: Vlastimil Babka > --- > mm/mremap.c | 19 +++++++++- > tools/testing/selftests/vm/mremap_test.c | 47 +++++++++++++++++++++++- > 2 files changed, 63 insertions(+), 3 deletions(-) > > diff --git a/mm/mremap.c b/mm/mremap.c > index 0b93fac76851..66970dcd636a 100644 > --- a/mm/mremap.c > +++ b/mm/mremap.c > @@ -9,6 +9,7 @@ > */ > > #include > +#include > #include > #include > #include > @@ -23,6 +24,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -1014,6 +1016,9 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, > /* can we just expand the current mapping? */ > if (vma_expandable(vma, new_len - old_len)) { > long pages = (new_len - old_len) >> PAGE_SHIFT; > + unsigned long extension_start = addr + old_len; > + unsigned long extension_end = addr + new_len; > + pgoff_t extension_pgoff = vma->vm_pgoff + (old_len >> PAGE_SHIFT); > > if (vma->vm_flags & VM_ACCOUNT) { > if (security_vm_enough_memory_mm(mm, pages)) { > @@ -1022,8 +1027,18 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, > } > } > > - if (vma_adjust(vma, vma->vm_start, addr + new_len, > - vma->vm_pgoff, NULL)) { > + /* > + * Function vma_merge() is called on the extension we are adding to > + * the already existing vma, vma_merge() will merge this extension with > + * the already existing vma (expand operation itself) and possibly also > + * with the next vma if it becomes adjacent to the expanded vma and > + * otherwise compatible. > + */ > + vma = vma_merge(mm, vma, extension_start, extension_end, > + vma->vm_flags, vma->anon_vma, vma->vm_file, > + extension_pgoff, vma_policy(vma), > + vma->vm_userfaultfd_ctx, anon_vma_name(vma)); > + if (!vma) { > vm_unacct_memory(pages); > ret = -ENOMEM; > goto out; > diff --git a/tools/testing/selftests/vm/mremap_test.c b/tools/testing/selftests/vm/mremap_test.c > index db0270127aeb..0865a6cb5bdb 100644 > --- a/tools/testing/selftests/vm/mremap_test.c > +++ b/tools/testing/selftests/vm/mremap_test.c > @@ -118,6 +118,48 @@ static unsigned long long get_mmap_min_addr(void) > return addr; > } > > +/* > + * This test validates that merge is called when expanding a mapping. > + * Mapping containing three pages is created, middle page is unmapped > + * and then the mapping containing the first page is expanded so that > + * it fills the created hole. The two parts should merge creating > + * single mapping with three pages. > + */ > +static void mremap_expand_merge(unsigned long page_size) > +{ > + char *test_name = "mremap expand merge"; > + FILE *fp; > + char *line = NULL; > + size_t len = 0; > + bool success = false; > + > + char *start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > + munmap(start + page_size, page_size); > + mremap(start, page_size, 2 * page_size, 0); > + > + fp = fopen("/proc/self/maps", "r"); > + if (fp == NULL) { > + ksft_test_result_fail("%s\n", test_name); > + return; > + } > + > + while(getline(&line, &len, fp) != -1) { > + char *first = strtok(line,"- "); > + void *first_val = (void *) strtol(first, NULL, 16); > + char *second = strtok(NULL,"- "); > + void *second_val = (void *) strtol(second, NULL, 16); > + if (first_val == start && second_val == start + 3 * page_size) { > + success = true; > + break; > + } > + } > + if (success) > + ksft_test_result_pass("%s\n", test_name); > + else > + ksft_test_result_fail("%s\n", test_name); > + fclose(fp); > +} > + > /* > * Returns the start address of the mapping on success, else returns > * NULL on failure. > @@ -336,6 +378,7 @@ int main(int argc, char **argv) > int i, run_perf_tests; > unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD; > unsigned int pattern_seed; > + int num_expand_tests = 1; > struct test test_cases[MAX_TEST]; > struct test perf_test_cases[MAX_PERF_TEST]; > int page_size; > @@ -407,12 +450,14 @@ int main(int argc, char **argv) > (threshold_mb * _1MB >= _1GB); > > ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ? > - ARRAY_SIZE(perf_test_cases) : 0)); > + ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests); > > for (i = 0; i < ARRAY_SIZE(test_cases); i++) > run_mremap_test_case(test_cases[i], &failures, threshold_mb, > pattern_seed); > > + mremap_expand_merge(page_size); > + > if (run_perf_tests) { > ksft_print_msg("\n%s\n", > "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:");