From patchwork Thu Aug 9 23:35:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 10562133 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 EB9A01057 for ; Thu, 9 Aug 2018 23:36:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D7EB9289DD for ; Thu, 9 Aug 2018 23:36:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CA64F2BB3A; Thu, 9 Aug 2018 23:36:45 +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 1823F289DD for ; Thu, 9 Aug 2018 23:36:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 19C846B0008; Thu, 9 Aug 2018 19:36:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 182A56B000C; Thu, 9 Aug 2018 19:36:44 -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 062426B000A; Thu, 9 Aug 2018 19:36:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id BC0226B0007 for ; Thu, 9 Aug 2018 19:36:43 -0400 (EDT) Received: by mail-pf1-f200.google.com with SMTP id j15-v6so4193392pff.12 for ; Thu, 09 Aug 2018 16:36:43 -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:mime-version:content-transfer-encoding; bh=eOGdq2TbY2Zqi5UhZ1IjVBrN/GDqHto44Md434YOiBM=; b=BMsymeP1gpNGy2HVWPve/ePxmG8Uz48g7B40MCQzWZH2tpRFUrA4Vfb4Hw9wkAnxJA 7BEsKEEvnRrW62S9znAdBxJThNUb7N4jGGy5IR2x1eRQnxnzpj6tZa1p+CYPy9CjT6DT 83IQyJd9L/Os0zoUVgPlvJ8F3/JTtXflZCgplA9uGFlgOlhVXEjTZ2098zPIxDtJ5P/1 WOmFSKiKRvq8YMLfXEqVze9fxjDFZQ3uM+fMpQ9MdluXbFUmTfhP6HUgaRe5rTTVsOsl 5k4nolUtAC+aKW06EKe7T2GY9oMiSuRYE938qxyj2qgzcupGL3/hzIK9mJpLlwjCF3oO STUQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 47.88.44.37 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: AOUpUlH9txk1BPR2CaUCQPCyrFtR0Ef29M1jcl6O1/xX4+StrFeXLoDn JQ4P30gU3UZM9PZ0+U4EyftGauxWZ1yopcK375HZRh/xGoq39OurJapd+I3UugRmmTwjKHFUlt4 aKl1vgZerpeyfZRDIhOdiJKDzkuDQxOovgsUGHIH2ijxohjBvQIg4dppbcHjU7JoEog== X-Received: by 2002:a62:b917:: with SMTP id z23-v6mr4386412pfe.131.1533857803423; Thu, 09 Aug 2018 16:36:43 -0700 (PDT) X-Google-Smtp-Source: AA+uWPwWzO/SAYFFK8cTWbJ0mLbvHJ3QNCHUmUsYnd9kmhwVGtauufpIkt+EZw/Os5h+yyTkpYw7 X-Received: by 2002:a62:b917:: with SMTP id z23-v6mr4386372pfe.131.1533857802454; Thu, 09 Aug 2018 16:36:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533857802; cv=none; d=google.com; s=arc-20160816; b=Qu10+kn4wiACAHBJdvqoTarvzWJQ29iZefceCE4qZ3EwLJm36zP/hrAE8L8P4th3mo qqDhjAKPrDM2AAs/YKMQMdgMeEMtPR5zO5lLDoC1cRWEOfk/0hBxAFSwPtnSCRTlyb5d rQY4gJaFqRjXnn3AYxl5+LzWivu4bty4gyuIkQgI3DPtczwAnIL2DfOXM4wR6ER+L5fb WI/dHt1OSxDMbRBcd3himcOYhQMqmW18HvApUjqlwz6x1xCQBnRp+cmfyjG5L1aZTVB4 gSWBSzqT9TJcnsNgEjf1xfHToRG1gN1GwXlfmR0sAnInxWVtvZaSpSAk5VIgSslom5GP et5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:arc-authentication-results; bh=eOGdq2TbY2Zqi5UhZ1IjVBrN/GDqHto44Md434YOiBM=; b=iHdlQsFvxQgmCzHK8xwSunokupui6PVFwER4Lb0pUr/NWx7LIfhByB1RZ7TnPgpg8u XUe4Tvoqlp7tamHanPSUcQqbDwu/isJSpVTAKebK2nYzJ4tqdAmvQWFe/EjKuWCM6j+7 +dVqN7lzj+TJHlYzS3an8steoo8P/Iy4KXCQhnFQlIKSUGwOn0CDMPC4ZnDyYYvM2Uze ZjT8eMhhsj2cLRJ9AVf1OfRPpKMpgM5ahJOpVZhGOn4RWFz1bdVqF8qQXMQHjfRtyM9b OjhE6HMJW8gFoA6T5Cv5ElCkGSbjFdwiR+YVCF5bkzbWcFsG3A5jydfk6PMVxA6iFPaz jccA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 47.88.44.37 as permitted sender) smtp.mailfrom=yang.shi@linux.alibaba.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out4437.biz.mail.alibaba.com (out4437.biz.mail.alibaba.com. [47.88.44.37]) by mx.google.com with ESMTPS id 3-v6si6739266plu.65.2018.08.09.16.36.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Aug 2018 16:36:42 -0700 (PDT) Received-SPF: pass (google.com: domain of yang.shi@linux.alibaba.com designates 47.88.44.37 as permitted sender) client-ip=47.88.44.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yang.shi@linux.alibaba.com designates 47.88.44.37 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=01201311R201e4;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_---0T6MWc.W_1533857777; Received: from e19h19392.et15sqa.tbsite.net(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0T6MWc.W_1533857777) by smtp.aliyun-inc.com(127.0.0.1); Fri, 10 Aug 2018 07:36:26 +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 v7 PATCH 0/4] mm: zap pages with read mmap_sem in munmap for large mapping Date: Fri, 10 Aug 2018 07:35:59 +0800 Message-Id: <1533857763-43527-1-git-send-email-yang.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 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. For the safer and bisectable sake, they will be handled by falling back to regular do_munmap() with exclusive mmap_sem held in a separate patch. Since it may be not safe to update vm_flags with read mmap_sem, although it sounds safe for this specific case hence 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 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 (4): mm: refactor do_munmap() to extract the common part mm: mmap: zap pages with read mmap_sem in munmap uprobes: make vma_has_uprobes non-static mm: unmap special vmas with regular do_munmap() include/linux/uprobes.h | 7 +++ kernel/events/uprobes.c | 2 +- mm/mmap.c | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 182 insertions(+), 32 deletions(-)