From patchwork Mon Sep 10 12:55:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Hocko X-Patchwork-Id: 10594119 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 791E2112B for ; Mon, 10 Sep 2018 12:55:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 68FFA28DA5 for ; Mon, 10 Sep 2018 12:55:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D16E28F76; Mon, 10 Sep 2018 12:55:59 +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 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 E400D28DA5 for ; Mon, 10 Sep 2018 12:55:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E82AE8E0001; Mon, 10 Sep 2018 08:55:56 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E38088E0005; Mon, 10 Sep 2018 08:55:56 -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 C94FE8E0001; Mon, 10 Sep 2018 08:55:56 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id 6A6918E0001 for ; Mon, 10 Sep 2018 08:55:56 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id d47-v6so7204371edb.3 for ; Mon, 10 Sep 2018 05:55:56 -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:in-reply-to:references; bh=5PDAGBEfkj8z/XyEkk42GihkhXKp1ZHv4FXKwWLZYXM=; b=iE3Iaiw7CmaK4Y6+WGckZ9SnSxVCafxpNtN1WJhl3V1ObR7Wmg4ogN1n8k+d6JntQD +Zmj3Aq4FzHs9YvdwhzdnFBQqW5iRSJPvVP9LhRfxuw7OUwVH1RfGsEi7j5exwiiIxNG hCtmqeG7ZSbhTYYXXPpKLp9bth96u44C0Hl1Dp913BofVVL9bAJvlbvgUiLOkvwIcci5 Fqc4q6YvY5ZUUjUQ/6Z1iP52fxQHUO2gE3Df8ewQuQYTMDICLHin+joAYnbyDaV/JY3I hCxOfNWsAFZiAR4sm6TGcssAWR3c93yMWeoqC1Dxkve060yTswviZ8Hxgl6tB4YF+n13 uFlA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of mstsxfx@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=mstsxfx@gmail.com; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Gm-Message-State: APzg51AjCH7q8QoQLzRsaMcF4flqf2Gp2HA7JnSvlSvCmGejihGta5Z8 BkxCAVthj7M9z0tlyDHYYp4jrQfu32ycTfHw3CMfWMr6BvoYO9u1bX11c8O+kLmB9ebLf1SNMcO VfMfbJlY7hTp2HJsZ8+VIE2mF/XL8flmo1nPLg9eMGDI5k122FPG8kH9bcCSxx5XD/9Drsyrokc xEG58EpJ8Q1WG1y49dRnXScm5QPNJTQ+0ufYYizQ1mMp5RpzdWmq7l+HxHh6KQC3OmT4sSbIcND i5U0g9boTdfmSgzn98oLpZpLKCUWdFArtdN2tjzIPKXQ8tND5/GnJx4aEfQDVDKexADjBYmikE/ 2yQeF0xCj7um6kOpmHhQyiKdh3m7rOAH2jQ5RDHHm6di0EOsLunYK6eKF/OPiBiCYnNp+j+2Tg= = X-Received: by 2002:a50:a93c:: with SMTP id l57-v6mr22755617edc.229.1536584155916; Mon, 10 Sep 2018 05:55:55 -0700 (PDT) X-Received: by 2002:a50:a93c:: with SMTP id l57-v6mr22755520edc.229.1536584154581; Mon, 10 Sep 2018 05:55:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536584154; cv=none; d=google.com; s=arc-20160816; b=aS6Hr/E6D2ycOjolmbb+/WqQ01VVoV83AXQ8eK4YZaeNMc67DL93brDa4qE7lilW8+ VM0Xqh7oYEBepvGmU/NnHItgKONfQSdTUURvTpXZNzJ/HinRSAhMaAizmbcvQHDbBw8U vNv0pwSmfFd1pTri4q5S80DK16cbser0JDExLVp1NlZDiEyZqMTbPbBxPLiivbd4GPnl rS3O1vW1xYUgJYf51tPM6f4N3Z6csQyE0uT8s3ehOl+yMP21wmHPdXQ4+K8490BG0qCq 3nIfqqTg8TZ+K24tle62wBYmfsuIDZoZDVkJm9+tUIEH832i21ODM3MWcyexVw1rYosn Hibw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=5PDAGBEfkj8z/XyEkk42GihkhXKp1ZHv4FXKwWLZYXM=; b=HbP4Ylp/1NKwun+3AOksEXPIMvnL1jo5j0ssjxBEBbT9a1jKOevL5unH+4owKfRd3w 6V7OY4YtNt/GOts7ERddCv/YohBBkkfDzysGAeAFqN+j5RIOv4x5ivE9YP7c78Rsrdip RS6D1DxiF1I2DeG23YqilAVTqf74mylydRj/JQFwuMg/2gO8zEwY6YsYEYPBSBraCnq7 AKijWI60Ner7++fFUpCBipXhq1srwxmqSQ5+wFQAVZUrADjXXeExtQVtx9DsXZHHogTA INcXr4aGV5nwQJJskOPtPfQqTfuR/NfmsTB2NFNLGAm3PGVe+QSpM9QuH+YNspL6bMpm fSjQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of mstsxfx@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=mstsxfx@gmail.com; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id g54-v6sor14960078edg.7.2018.09.10.05.55.54 for (Google Transport Security); Mon, 10 Sep 2018 05:55:54 -0700 (PDT) Received-SPF: pass (google.com: domain of mstsxfx@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; spf=pass (google.com: domain of mstsxfx@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=mstsxfx@gmail.com; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Google-Smtp-Source: ANB0VdYESK54MC/TflvNi8J0gThGcqaj5WLOv9Jvc4BlFv1RMQavOdRNeKgyp/WmS0JGHHgsWppvAQ== X-Received: by 2002:a50:9069:: with SMTP id z38-v6mr23076827edz.24.1536584153882; Mon, 10 Sep 2018 05:55:53 -0700 (PDT) Received: from tiehlicka.suse.cz (prg-ext-pat.suse.com. [213.151.95.130]) by smtp.gmail.com with ESMTPSA id h40-v6sm8632245edh.88.2018.09.10.05.55.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Sep 2018 05:55:53 -0700 (PDT) From: Michal Hocko To: Cc: Tetsuo Handa , Roman Gushchin , Andrew Morton , Michal Hocko Subject: [RFC PATCH 1/3] mm, oom: rework mmap_exit vs. oom_reaper synchronization Date: Mon, 10 Sep 2018 14:55:11 +0200 Message-Id: <20180910125513.311-2-mhocko@kernel.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180910125513.311-1-mhocko@kernel.org> References: <1536382452-3443-1-git-send-email-penguin-kernel@I-love.SAKURA.ne.jp> <20180910125513.311-1-mhocko@kernel.org> 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 From: Michal Hocko The oom_reaper cannot handle mlocked vmas right now and therefore we have exit_mmap to reap the memory before it clears the mlock flags on mappings. This is all good but we would like to have a better hand over protocol between the oom_reaper and exit_mmap paths. Therefore use exclusive mmap_sem in exit_mmap whenever exit_mmap has to synchronize with the oom_reaper. There are two notable places. Mlocked vmas (munlock_vma_pages_all) and page tables tear down path. All others should be fine to race with oom_reap_task_mm. This is mostly a preparatory patch which shouldn't introduce functional changes. Signed-off-by: Michal Hocko --- mm/mmap.c | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 5f2b2b184c60..3481424717ac 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3042,39 +3042,29 @@ void exit_mmap(struct mm_struct *mm) struct mmu_gather tlb; struct vm_area_struct *vma; unsigned long nr_accounted = 0; + bool oom = mm_is_oom_victim(mm); /* mm's last user has gone, and its about to be pulled down */ mmu_notifier_release(mm); - if (unlikely(mm_is_oom_victim(mm))) { - /* - * Manually reap the mm to free as much memory as possible. - * Then, as the oom reaper does, set MMF_OOM_SKIP to disregard - * this mm from further consideration. Taking mm->mmap_sem for - * write after setting MMF_OOM_SKIP will guarantee that the oom - * reaper will not run on this mm again after mmap_sem is - * dropped. - * - * Nothing can be holding mm->mmap_sem here and the above call - * to mmu_notifier_release(mm) ensures mmu notifier callbacks in - * __oom_reap_task_mm() will not block. - * - * This needs to be done before calling munlock_vma_pages_all(), - * which clears VM_LOCKED, otherwise the oom reaper cannot - * reliably test it. - */ - (void)__oom_reap_task_mm(mm); - - set_bit(MMF_OOM_SKIP, &mm->flags); - down_write(&mm->mmap_sem); - up_write(&mm->mmap_sem); - } - if (mm->locked_vm) { vma = mm->mmap; while (vma) { - if (vma->vm_flags & VM_LOCKED) + if (vma->vm_flags & VM_LOCKED) { + /* + * oom_reaper cannot handle mlocked vmas but we + * need to serialize it with munlock_vma_pages_all + * which clears VM_LOCKED, otherwise the oom reaper + * cannot reliably test it. + */ + if (oom) + down_write(&mm->mmap_sem); + munlock_vma_pages_all(vma); + + if (oom) + up_write(&mm->mmap_sem); + } vma = vma->vm_next; } } @@ -3091,6 +3081,11 @@ void exit_mmap(struct mm_struct *mm) /* update_hiwater_rss(mm) here? but nobody should be looking */ /* Use -1 here to ensure all VMAs in the mm are unmapped */ unmap_vmas(&tlb, vma, 0, -1); + + /* oom_reaper cannot race with the page tables teardown */ + if (oom) + down_write(&mm->mmap_sem); + free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); tlb_finish_mmu(&tlb, 0, -1); @@ -3104,6 +3099,9 @@ void exit_mmap(struct mm_struct *mm) vma = remove_vma(vma); } vm_unacct_memory(nr_accounted); + + if (oom) + up_write(&mm->mmap_sem); } /* Insert vm structure into process list sorted by address