From patchwork Wed Aug 15 18:49:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 10566747 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6BB95921 for ; Wed, 15 Aug 2018 18:50:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 408342AF4D for ; Wed, 15 Aug 2018 18:50:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3E2392AF18; Wed, 15 Aug 2018 18:50:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B66F2AF3C for ; Wed, 15 Aug 2018 18:50:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07E6E6B0008; Wed, 15 Aug 2018 14:50:23 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 006506B000A; Wed, 15 Aug 2018 14:50:22 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC1E46B000C; Wed, 15 Aug 2018 14:50:22 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f72.google.com (mail-pl0-f72.google.com [209.85.160.72]) by kanga.kvack.org (Postfix) with ESMTP id 94FE16B000A for ; Wed, 15 Aug 2018 14:50:22 -0400 (EDT) Received: by mail-pl0-f72.google.com with SMTP id d10-v6so1113565pll.22 for ; Wed, 15 Aug 2018 11:50:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id; bh=VnpgmATZO/va4AjtQZdX6jujTd/YrKiMn7hcNJB2cKE=; b=sEs7TjlvD2ZFNvKzgaFnhIV1TWOau2+VyCkLKKN2R5I0nimFSHWuFW9F8yVx6jpI7f +tLmjwwvOhDSvkrYTBing4JKHB3WVyAbiznO5z7jqwEXNsKYoSWEWdNDm36Z6w9axCAV 4VTHFIosNLonQsDLSGMiwYMNMcamJR4ACbV/1JPBnp8IIM7eoz8mR4kaY0bEM6jZxjWG UEhD7ZUjaoC09BWk5jHlIkP/64meYEJNSP2vqssPQTF5Unma9YJfAZxYsi3ILG03Vc2n xDEKxF+YQfBsiudgTqb/DCrnL4ZuzcdUY3+Ka00NZkXWLDzTydJCgW4cVYKptAv2i3mT f01Q== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com X-Gm-Message-State: AOUpUlH/TWpJ4/KPgyVjKRpAbfjXktoKGzG5qU4Mp0DGgSX4XgWkIBBF 8w28cZs+ne4/DkG06oBphocig2UDcOCDQvDR4u61DeHEt7iDGeJRV/87P5CF1EzjHG8wQw//fIp IxMYTKeAj4U1LN9nXvJvfP2gvlyMbGv+l3+K3skM80CiOuMPIzky0+Xxs2EMicfcueQ== X-Received: by 2002:a63:e14a:: with SMTP id h10-v6mr26825020pgk.358.1534359022224; Wed, 15 Aug 2018 11:50:22 -0700 (PDT) X-Google-Smtp-Source: AA+uWPyevsPYdldWIi2Xf8ru3QCKtMCDi5aVWcmH/WIohK5fXs+LaqcZHY3LaTVxPbMoRzZ76d9e X-Received: by 2002:a63:e14a:: with SMTP id h10-v6mr26824980pgk.358.1534359021055; Wed, 15 Aug 2018 11:50:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534359021; cv=none; d=google.com; s=arc-20160816; b=HnTLvmy7rmu5fQ7Z0YdUcHjHFJGbphk3hIzRUuXG8vSajWLktbaXaNFSVz794W8/e5 tsKZJXRIWQrQH1wlH5Ilx3gGya4F4XM3OGJj1XGsUV3MGwxtzfmWgVHAi6AtmjxD/+DX u5uglmXTDh16Tfl/eNCywkpci+6tV5iW9cQtYajCGN36WxLYU05VhUeM4YsR1zwuuJUI 0VGO+X4GPvpvDkfrDo3ADLZFc/GsQfKfEObWoVthO6nGKZtTtgtWyQmDt7zSkqrLtw3k erBA93bzjl/VuOvcuXzM/8I7sAhizzMnaplFItLmi2NBmhtfLSkfMZU6rBo5nbtLMQ4c rVmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=VnpgmATZO/va4AjtQZdX6jujTd/YrKiMn7hcNJB2cKE=; b=zrqe1o52SvQ3hsPI4nbeMcNPWr48D4inh0A7lovX0W2V5ugtWlvO+tWe8fryCew9a4 nqgS9N/nT8rI3Jt5pMw/d6pXvz3Ltu2eNBmGE9nM2mqj3cB9yqt2RuOwyuqF3LcJwGJE SbE+EuZw3jtKFejK/CYofQyn7riY294RrcpenqQBG4/DRXAOtTg/Z552f+H2cc5RSw+7 qKp6aQ/iVgG0UwxhN3VpigdTgQy4g+OoboDZRJ7TcQtBJtP8cgjLdo+hzqXjyIa5pIQ4 oYeOyr3jyk4ssV7LoobY61H3Gfh2d9SB2oMJCTCIgRGBMpeAHWRvBpkAKKEQc5R7vTSH HePQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com. [115.124.30.133]) by mx.google.com with ESMTPS id n70-v6si24120596pfa.320.2018.08.15.11.50.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Aug 2018 11:50:21 -0700 (PDT) Received-SPF: pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.133 as permitted sender) client-ip=115.124.30.133; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R121e4;CH=green;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07487;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=15;SR=0;TI=SMTPD_---0T6gJtNa_1534358999; Received: from e19h19392.et15sqa.tbsite.net(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0T6gJtNa_1534358999) by smtp.aliyun-inc.com(127.0.0.1); Thu, 16 Aug 2018 02:50:07 +0800 From: Yang Shi To: mhocko@kernel.org, willy@infradead.org, ldufour@linux.vnet.ibm.com, kirill@shutemov.name, vbabka@suse.cz, akpm@linux-foundation.org, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org Cc: yang.shi@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC v8 PATCH 0/5] mm: zap pages with read mmap_sem in munmap for large mapping Date: Thu, 16 Aug 2018 02:49:45 +0800 Message-Id: <1534358990-85530-1-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 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: X-Virus-Scanned: ClamAV using ClamSMTP Background: Recently, when we ran some vm scalability tests on machines with large memory, we ran into a couple of mmap_sem scalability issues when unmapping large memory space, please refer to https://lkml.org/lkml/2017/12/14/733 and https://lkml.org/lkml/2018/2/20/576. History: Then akpm suggested to unmap large mapping section by section and drop mmap_sem at a time to mitigate it (see https://lkml.org/lkml/2018/3/6/784). V1 patch series was submitted to the mailing list per Andrew's suggestion (see https://lkml.org/lkml/2018/3/20/786). Then I received a lot great feedback and suggestions. Then this topic was discussed on LSFMM summit 2018. In the summit, Michal Hocko suggested (also in the v1 patches review) to try "two phases" approach. Zapping pages with read mmap_sem, then doing via cleanup with write mmap_sem (for discussion detail, see https://lwn.net/Articles/753269/) Approach: Zapping pages is the most time consuming part, according to the suggestion from Michal Hocko [1], zapping pages can be done with holding read mmap_sem, like what MADV_DONTNEED does. Then re-acquire write mmap_sem to cleanup vmas. But, we can't call MADV_DONTNEED directly, since there are two major drawbacks: * The unexpected state from PF if it wins the race in the middle of munmap. It may return zero page, instead of the content or SIGSEGV. * Can't handle VM_LOCKED | VM_HUGETLB | VM_PFNMAP and uprobe mappings, which is a showstopper from akpm But, some part may need write mmap_sem, for example, vma splitting. So, the design is as follows: acquire write mmap_sem lookup vmas (find and split vmas) deal with special mappings detach vmas downgrade_write zap pages free page tables release mmap_sem The vm events with read mmap_sem may come in during page zapping, but since vmas have been detached before, they, i.e. page fault, gup, etc, will not be able to find valid vma, then just return SIGSEGV or -EFAULT as expected. If the vma has VM_HUGETLB | VM_PFNMAP or uprobe, they are considered as special mappings. They will be handled by falling back to regular do_munmap() with exclusive mmap_sem held in this patch since they may update vm flags. But, with the "detach vmas first" approach, the vmas have been detached when vm flags are updated, so it sounds safe to update vm flags with read mmap_sem for this specific case. So, VM_HUGETLB and VM_PFNMAP will be handled by using the optimized path in the following separate patches for bisectable sake. However, uprobes mappings will keep using regular do_munmap() since unmapping uprobe areas may need update mm flags. It might be not safe with just holding read mmap_sem even though affected vmas have been detached. With the "detach vmas first" approach we don't have to re-acquire mmap_sem again to clean up vmas to avoid race window which might get the address space changed since downgrade_write() doesn't release the lock to lead regression, which simply downgrades to read lock. And, since the lock acquire/release cost is managed to the minimum and almost as same as before, the optimization could be extended to any size of mapping without incurring significant penalty to small mappings. For the time being, just do this in munmap syscall path. Other vm_munmap() or do_munmap() call sites (i.e mmap, mremap, etc) remain intact due to some implementation difficulties since they acquire write mmap_sem from very beginning and hold it until the end, do_munmap() might be called in the middle. But, the optimized do_munmap would like to be called without mmap_sem held so that we can do the optimization. So, if we want to do the similar optimization for mmap/mremap path, I'm afraid we would have to redesign them. mremap might be called on very large area depending on the usecases, the optimization to it will be considered in the future. Changelog v7 -> v8: * Added Acked-by from Vlastimil for patch 1/5. Thanks. * Fixed the wrong "evolution" direction. Converted VM_HUGETLB and VM_PFNMAP mapping use the optimized path in separate patches respectively for safe and bisectable sake per Michal's suggestion. * Extracted has_uprobes() helper from uprobes_munmap() to check if mm or vmas have uprobes, which could save some cycles instead of calling vma_has_uprobes() directly for some cases. Per Vlastimil's suggestion. * Keep unmapping uprobes area using regular do_munmap() since it might update mm flags, that might be not safe with read mmap_sem even though vmas have been detached. * Fixed some comments from Willy. v6 -> v7: * Rename some helper functions per Michal and Vlastimil's comments. * Refactor munmap_lookup_vma() to return the pointer of start vma per Michal's suggestion. * Rephrase some commit log for patch 2/4 per Michal's comments. * Deal with special mappings (VM_HUGETLB | VM_PFNMAP | uprobes) with regular do_munmap() in a separate patch per Michal's suggestion. * Bring the patch which makes vma_has_uprobes() non-static back since it is needed to check if a vma has uprobes or not. v5 -> v6: * Fixed the comments from Kirill and Laurent * Added Laurent's reviewed-by to patch 1/2. Thanks. v4 -> v5: * Detach vmas before zapping pages so that we don't have to use VM_DEAD to mark a being unmapping vma since they have been detached from rbtree when zapping pages. Per Kirill * Eliminate VM_DEAD stuff * With this change we don't have to re-acquire write mmap_sem to do cleanup. So, we could eliminate a potential race window * Eliminate PUD_SIZE check, and extend this optimization to all size v3 -> v4: * Extend check_stable_address_space to check VM_DEAD as Michal suggested * Deal with vm_flags update of VM_LOCKED | VM_HUGETLB | VM_PFNMAP and uprobe mappings with exclusive lock held. The actual unmapping is still done with read mmap_sem to solve akpm's concern * Clean up vmas with calling do_munmap to prevent from race condition by not carrying vmas as Kirill suggested * Extracted more common code * Solved some code cleanup comments from akpm * Dropped uprobe and arch specific code, now all the changes are mm only * Still keep PUD_SIZE threshold, if everyone thinks it is better to extend to all sizes or smaller size, will remove it * Make this optimization 64 bit only explicitly per akpm's suggestion v2 -> v3: * Refactor do_munmap code to extract the common part per Peter's sugestion * Introduced VM_DEAD flag per Michal's suggestion. Just handled VM_DEAD in x86's page fault handler for the time being. Other architectures will be covered once the patch series is reviewed * Now lookup vma (find and split) and set VM_DEAD flag with write mmap_sem, then zap mapping with read mmap_sem, then clean up pgtables and vmas with write mmap_sem per Peter's suggestion v1 -> v2: * Re-implemented the code per the discussion on LSFMM summit Regression and performance data: Did the below regression test with setting thresh to 4K manually in the code: * Full LTP * Trinity (munmap/all vm syscalls) * Stress-ng: mmap/mmapfork/mmapfixed/mmapaddr/mmapmany/vm * mm-tests: kernbench, phpbench, sysbench-mariadb, will-it-scale * vm-scalability With the patches, exclusive mmap_sem hold time when munmap a 80GB address space on a machine with 32 cores of E5-2680 @ 2.70GHz dropped to us level from second. munmap_test-15002 [008] 594.380138: funcgraph_entry: | vm_munmap_zap_rlock() { munmap_test-15002 [008] 594.380146: funcgraph_entry: !2485684 us | unmap_region(); munmap_test-15002 [008] 596.865836: funcgraph_exit: !2485692 us | } Here the excution time of unmap_region() is used to evaluate the time of holding read mmap_sem, then the remaining time is used with holding exclusive lock. Yang Shi (5): mm: refactor do_munmap() to extract the common part uprobes: introduce has_uprobes helper mm: mmap: zap pages with read mmap_sem in munmap mm: unmap VM_HUGETLB mappings with optimized path mm: unmap VM_PFNMAP mappings with optimized path include/linux/uprobes.h | 7 ++++ kernel/events/uprobes.c | 23 ++++++++---- mm/mmap.c | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------ 3 files changed, 188 insertions(+), 41 deletions(-)