From patchwork Tue Nov 22 08:24:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muhammad Usama Anjum X-Patchwork-Id: 24189 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2076331wrr; Tue, 22 Nov 2022 00:29:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf4lKwm6RxbfU9cogJiSJnqmEqubn32v12YSLCUDMlpJAQ9MsdlWBCExH6IU/q6KXygxX/1k X-Received: by 2002:a05:6402:4286:b0:458:7489:34ea with SMTP id g6-20020a056402428600b00458748934eamr6481688edc.264.1669105766235; Tue, 22 Nov 2022 00:29:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669105766; cv=none; d=google.com; s=arc-20160816; b=LQcRNTZW1Kg6Ay6TvDKnEd10WocY5mesdqAMCJHLCCwIk7q0NTrb0jtI7P87e38u2T ZIRgxva6NKLGwfcmewxISXwflCrFi3cVjyArfMSSYaLbGSDjUHxvBUKnFUfX9oy0i97K xvPYXyQriG2tPw/worYkLXRPPvk6VPFJIlERN1toEkcWHheR3Kin7Pw8KqM8RnYr3DzB RV14VWRMhI1g7uROkMTjeT9X+OimlyI6cn8cCLYdhFsNaxTZ55k/bqwfJDtEwfphtJbS u9ThzvFH8yR0ANGrU11HyRd+eURkE70I36fUcCp8SXzyfNfHfCm7z9delSmlD9WKxOvB BQ6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=TUvI9uwKzaeue6F+QOznCUqXiNDsy5wDW0jt8zjQeks=; b=NIdyknLk5bsMjgAwzafo20ZHrNDnnH7/bJpf3mmMbyt3C/Xht3HS4b1IOsM9p+rlcK R3QJr40f1eE0clJ2CgXWY5V618IyGMPuT9/RjZsDh5CIRL4ZiKnr4rSYJ0qY1ejD68UU bohyxX6Tvfev67Zbsd/lK/ynHI8bP+TlzdX6b4+Fv99K+CIwSwyBRF3lYwgDu2/kY9B8 ma61JnGKWmuCj8VtW13JICJuaHUkLhgjHOxrPJrqtDGYklfH7w5JeP7lfdJdSFj+UyZo v77Fc+i37HquSat2xoXSzLi81ykZJHg50TRiF9I0WKtLPYQ2D8LWE5EOo7v7hFAyLoTi Aizg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=AsNLV8YX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xb1-20020a170907070100b0078d8cc2006csi13149205ejb.697.2022.11.22.00.29.02; Tue, 22 Nov 2022 00:29:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=AsNLV8YX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232795AbiKVIZW (ORCPT + 99 others); Tue, 22 Nov 2022 03:25:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232744AbiKVIZQ (ORCPT ); Tue, 22 Nov 2022 03:25:16 -0500 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1CF740441; Tue, 22 Nov 2022 00:25:07 -0800 (PST) Received: from localhost.localdomain (unknown [39.45.241.105]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: usama.anjum) by madras.collabora.co.uk (Postfix) with ESMTPSA id D200E6602A89; Tue, 22 Nov 2022 08:25:02 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1669105505; bh=8UqUmiJ6AjGUWkeqighSLul+GOLZRjtMltLqZjflXsc=; h=From:To:Cc:Subject:Date:From; b=AsNLV8YX3qAXSS0anyE4EqPtYVtZzdg8B+5EvcSjIJyBeJxXyDfx6bIDhxT3SBBlW khbaUPmxJ1+bESaslS4c54pcP3Dh61CcUxZCciyv56J0QzV+KEDg1Q+EvDSmujyIQj f8Nwg5Wlo27fKaeSBoGbT0x6aTwTFVtcbB8m81S3VW60ydTsix5W7QckVWiAuBnCx1 jPqzn8Luqok8D3HgH0fRkKBX+ZoIGX7dtPJk/zqrCSu49ikeDVB4xITbjULePLBc6r HiyViM5abEa6zA9V6AJ/ZaV2B7JSqUCd9MO6V0r8hmg4jz8+nkx7u7PeBx/QAOqeCk C9y1I4Ah28YRg== From: Muhammad Usama Anjum To: Andrew Morton , Cyrill Gorcunov Cc: Mel Gorman , Peter Xu , David Hildenbrand , Andrei Vagin , Muhammad Usama Anjum , kernel@collabora.com, stable@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: set the vma flags dirty before testing if it is mergeable Date: Tue, 22 Nov 2022 13:24:41 +0500 Message-Id: <20221122082442.1938606-1-usama.anjum@collabora.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750184247840875744?= X-GMAIL-MSGID: =?utf-8?q?1750184247840875744?= The VM_SOFTDIRTY should be set in the vma flags to be tested if new allocation should be merged in previous vma or not. With this patch, the new allocations are merged in the previous VMAs. I've tested it by reverting the commit 34228d473efe ("mm: ignore VM_SOFTDIRTY on VMA merging") and after adding this following patch, I'm seeing that all the new allocations done through mmap() are merged in the previous VMAs. The number of VMAs doesn't increase drastically which had contributed to the crash of gimp. If I run the same test after reverting and not including this patch, the number of VMAs keep on increasing with every mmap() syscall which proves this patch. The commit 34228d473efe ("mm: ignore VM_SOFTDIRTY on VMA merging") seems like a workaround. But it lets the soft-dirty and non-soft-dirty VMA to get merged. It helps in avoiding the creation of too many VMAs. But it creates the problem while adding the feature of clearing the soft-dirty status of only a part of the memory region. Cc: Fixes: d9104d1ca966 ("mm: track vma changes with VM_SOFTDIRTY bit") Signed-off-by: Muhammad Usama Anjum --- We need more testing of this patch. While implementing clear soft-dirty bit for a range of address space, I'm facing an issue. The non-soft dirty VMA gets merged sometimes with the soft dirty VMA. Thus the non-soft dirty VMA become dirty which is undesirable. When discussed with the some other developers they consider it the regression. Why the non-soft dirty page should appear as soft dirty when it isn't soft dirty in reality? I agree with them. Should we revert 34228d473efe or find a workaround in the IOCTL? * Revert may cause the VMAs to expand in uncontrollable situation where the soft dirty bit of a lot of memory regions or the whole address space is being cleared again and again. AFAIK normal process must either be only clearing a few memory regions. So the applications should be okay. There is still chance of regressions if some applications are already using the soft-dirty bit. I'm not sure how to test it. * Add a flag in the IOCTL to ignore the dirtiness of VMA. The user will surely lose the functionality to detect reused memory regions. But the extraneous soft-dirty pages would not appear. I'm trying to do this in the patch series [1]. Some discussion is going on that this fails with some mprotect use case [2]. I still need to have a look at the mprotect selftest to see how and why this fails. I think this can be implemented after some more work probably in mprotect side. [1] https://lore.kernel.org/all/20221109102303.851281-1-usama.anjum@collabora.com/ [2] https://lore.kernel.org/all/bfcae708-db21-04b4-0bbe-712badd03071@redhat.com/ --- mm/mmap.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index f9b96b387a6f..6934b8f61fdc 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1708,6 +1708,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vm_flags |= VM_ACCOUNT; } + /* + * New (or expanded) vma always get soft dirty status. + * Otherwise user-space soft-dirty page tracker won't + * be able to distinguish situation when vma area unmapped, + * then new mapped in-place (which must be aimed as + * a completely new data area). + */ + vm_flags |= VM_SOFTDIRTY; + /* * Can we just expand an old mapping? */ @@ -1823,15 +1832,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr, if (file) uprobe_mmap(vma); - /* - * New (or expanded) vma always get soft dirty status. - * Otherwise user-space soft-dirty page tracker won't - * be able to distinguish situation when vma area unmapped, - * then new mapped in-place (which must be aimed as - * a completely new data area). - */ - vma->vm_flags |= VM_SOFTDIRTY; - vma_set_page_prot(vma); return addr; @@ -2998,6 +2998,8 @@ static int do_brk_flags(unsigned long addr, unsigned long len, unsigned long fla if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) return -ENOMEM; + flags |= VM_SOFTDIRTY; + /* Can we just expand an old private anonymous mapping? */ vma = vma_merge(mm, prev, addr, addr + len, flags, NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX, NULL); @@ -3026,7 +3028,6 @@ static int do_brk_flags(unsigned long addr, unsigned long len, unsigned long fla mm->data_vm += len >> PAGE_SHIFT; if (flags & VM_LOCKED) mm->locked_vm += (len >> PAGE_SHIFT); - vma->vm_flags |= VM_SOFTDIRTY; return 0; }