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 From patchwork Mon Sep 10 12:55:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Hocko X-Patchwork-Id: 10594121 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 AD797920 for ; Mon, 10 Sep 2018 12:56:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E6F228DA5 for ; Mon, 10 Sep 2018 12:56:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 930A128F76; Mon, 10 Sep 2018 12:56:02 +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 DD3EB28DA5 for ; Mon, 10 Sep 2018 12:56:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CF938E0005; Mon, 10 Sep 2018 08:55:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 758258E0006; Mon, 10 Sep 2018 08:55:57 -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 644558E0005; Mon, 10 Sep 2018 08:55:57 -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 08CDC8E0006 for ; Mon, 10 Sep 2018 08:55:57 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id 57-v6so7210559edt.15 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=I0hR/0E0KYQL2e73jyWIU2Glnbsw6oNe9h3qdbaIIfA=; b=dQQv0URcsw6vNRY1H6XMHTu9gaHSFeV1XKpSJRzXrmqm+dGZQHQU8fLle1IKdPlZVB UnXljeM9v3351yMCDSWcQMrguJMZWOylQhBksyFR0rdMt+yowszVh0Gmy6CKWD9WNuyd 1N2cNEDFHe/iilQ2nlDYir3ybLbl0VfESP/3QotyOxyGxfzehO7jx8Np4Jy/QJKvH1Zj tL5TNzZl7OCQQO4YYO0Fm5M6lm858cizMEP1Pf1v8cBAP3QrcLodNessgyzH/1ExWolE djyEDCRICLIQazDTBRvLZe3nmHH3Li4lAyq7p2yjlBPDQqafXQ3lCALGY2C1i3nMO2Rh FCqA== 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: APzg51Df99hU2jlcvz7GQbL/P5F4HgcLvJrLM39kMWPTxDtZOnsovXqy GfW9sja0JGpPHb/AErAk0a23nToaqBur0t7NPV+8so5KT1T1PnEog+uZnIVPx+mfIr2gSfuGLaV jOXyd+rjgatoeCGBYhXJWhCsbTt67ADeiJZjBMME4OtaVkdW8vtSByGORoUXJn+QXU8//wZp3f6 YdbF4IvuS2F6R2UqUNvGdTl1Ofh9NrHvVN0/GDI/npQMYX+XJLNf6SADrk6XvmbyoHoizPSHhFV 0dfKsGzEItZmoixV5M0djTnzTaIX9RTeX/ulGhAx9tb5QSli6mzzoDl1DWzIVqwC2FxTim9HJFl 5OQ0rduod8GkoZcy+f1/mGWUknYqaFFKTSl1XeELSMLCFfzvwF9qIG9QTcMeCp93TIGZOKWmLQ= = X-Received: by 2002:a50:a7e2:: with SMTP id i89-v6mr22262204edc.176.1536584156524; Mon, 10 Sep 2018 05:55:56 -0700 (PDT) X-Received: by 2002:a50:a7e2:: with SMTP id i89-v6mr22262134edc.176.1536584155506; Mon, 10 Sep 2018 05:55:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536584155; cv=none; d=google.com; s=arc-20160816; b=e6VjVvGOxm4N6mu17BXrVGdgf4BUKjoKmdTFFehyMVCazqnym9de7UrAd/WYzp4GBX olXs2d3IFaTUJnYZToBj1rFUg4CiEdCGh79c7VPC4WEevaDg88XFcqd2YjEV6HbqFF1L 8d3+5/aqu9yECOnq9O199bvicXi50wqHHlIKFCQ7AbqJRh0tpaNAJ6gbikmTOjD1JR4z 27W0NlprkmiaZvhRLSYirvIY3GoSGZ0XFMwJJWz+NBf+J5QiY6WBx8HgXvZ/OvvPne6v tZtEMARrBWphqG/xtP/G7tdBNC4pjt/ro/PEOcubHSGUiwwWKbC5cPRkwqcyATu8Ba8l CD1g== 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=I0hR/0E0KYQL2e73jyWIU2Glnbsw6oNe9h3qdbaIIfA=; b=YXwQ8m4QryH6gzsLYL0Ges4BfIFw4WxI6smEK+izPlX2b/HAYL49NjdsLiYGZNtUuL 6cZikqQUWcpjfDsxEA0CdmiVW/+BgE+y3RGjOOxe/FfjEzdCQm78Kw7i6Sj4Dkh5VBor 1hRxv94XMgdpOPPY+w2xOZj2LRAvIFxHtVVhRPd6+RU4GXskuI40Z3tROZoeUNvboHw4 +2y4UAhClGQYdCIGMaWa/jXR+MS5KKRDoCvpNy/0ORhFl6v3JyMUk9zvIKfQnUSHcvH6 8Tn6oNt/yC8XtFdRtlpQRTbyahu+5pQYbx2BtfuYa4XV32yfjXKaj3QXqH0H+CzlgZkc PsSw== 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 y50-v6sor14275219edd.15.2018.09.10.05.55.55 for (Google Transport Security); Mon, 10 Sep 2018 05:55:55 -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: ANB0VdYLO6V8MsOferz/ypt0MZy1lkHcjKSqBTcU7CNWkFCmLGOL9XERUNi7LrBWcnf7QbTUVEkloQ== X-Received: by 2002:a05:6402:1652:: with SMTP id s18mr23342837edx.35.1536584154940; Mon, 10 Sep 2018 05:55:54 -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:54 -0700 (PDT) From: Michal Hocko To: Cc: Tetsuo Handa , Roman Gushchin , Andrew Morton , Michal Hocko Subject: [RFC PATCH 2/3] mm, oom: keep retrying the oom_reap operation as long as there is substantial memory left Date: Mon, 10 Sep 2018 14:55:12 +0200 Message-Id: <20180910125513.311-3-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 oom_reaper is not able to reap all types of memory. E.g. mlocked mappings or page tables. In some cases this might be a lot of memory and we do rely on exit_mmap to release that memory. Yet we cannot rely on exit_mmap to set MMF_OOM_SKIP right now because there are several places when sleeping locks are taken. This patch adds a simple heuristic to check for the amount of memory the mm is sitting on after oom_reaper is done with it. If this is still few megabytes (this is a subject for further tunning based on real world usecases) then simply keep retrying oom_reap_task_mm. Signed-off-by: Michal Hocko --- mm/oom_kill.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index f10aa5360616..049e67dc039b 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -189,6 +189,16 @@ static bool is_dump_unreclaim_slabs(void) return (global_node_page_state(NR_SLAB_UNRECLAIMABLE) > nr_lru); } +/* + * Rough memory consumption of the given mm which should be theoretically freed + * when the mm is removed. + */ +static unsigned long oom_badness_pages(struct mm_struct *mm) +{ + return get_mm_rss(mm) + get_mm_counter(mm, MM_SWAPENTS) + + mm_pgtables_bytes(mm) / PAGE_SIZE; +} + /** * oom_badness - heuristic function to determine which candidate task to kill * @p: task struct of which task we should calculate @@ -230,8 +240,7 @@ unsigned long oom_badness(struct task_struct *p, struct mem_cgroup *memcg, * The baseline for the badness score is the proportion of RAM that each * task's rss, pagetable and swap space use. */ - points = get_mm_rss(p->mm) + get_mm_counter(p->mm, MM_SWAPENTS) + - mm_pgtables_bytes(p->mm) / PAGE_SIZE; + points = oom_badness_pages(p->mm); task_unlock(p); /* Normalize to oom_score_adj units */ @@ -532,6 +541,16 @@ bool __oom_reap_task_mm(struct mm_struct *mm) } } + /* + * If we still sit on a noticeable amount of memory even after successfully + * reaping the address space then keep retrying until exit_mmap makes some + * further progress. + * TODO: add a flag for a stage when the exit path doesn't block anymore + * and hand over MMF_OOM_SKIP handling there in that case + */ + if (ret && oom_badness_pages(mm) > 1024) + ret = false; + return ret; } From patchwork Mon Sep 10 12:55:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Hocko X-Patchwork-Id: 10594123 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 AD213112B for ; Mon, 10 Sep 2018 12:56:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9D78C28D8D for ; Mon, 10 Sep 2018 12:56:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 919E028EF9; Mon, 10 Sep 2018 12:56:05 +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 1712328D8D for ; Mon, 10 Sep 2018 12:56:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 475218E0007; Mon, 10 Sep 2018 08:55:58 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3F9F88E0006; Mon, 10 Sep 2018 08:55:58 -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 33C348E0007; Mon, 10 Sep 2018 08:55:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id BD7CD8E0006 for ; Mon, 10 Sep 2018 08:55:57 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id d47-v6so7204403edb.3 for ; Mon, 10 Sep 2018 05:55:57 -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=/JOCw+NbfDz6lgIyF+Yv3IQvhEgkMeNnG6mNNRVK3Sc=; b=FqAujMfTurjANrcczNR+Qq5o644hibNQEUq4E25dKnrrRhqseOUjsb8jB0wkDi4b3w wPkIt2WXazNYpv9TS9G36C4QGiEySaZlirKaCbaCnpmqQb34kO9JJSv2hy7Q/d4tYqfB uvERMm2l2hh4N1yOS+clafkBjVdGGPeOKSHTb0X6LXHBJplUsx2R9PULuC+0vpGL5q9m KUwywoztSa98NXzcK8/c9tIG4sFgq1rE5JyA4m23FbWwulbgTKPuRs8lzZ0u4nEjFM5g 8EeGGrujEdGlvCTIgFOJ0VsNcrCuYPXjFqIS1Qfz6onshQoXQ/qqJ6ALIcxfZVV2MhBE ucug== 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: APzg51B9Z+T0rhpRiygjYF0Tfh9Tmyl0Tw0EXYchT4Fybz7QxKfraggE 3l9PMcLgGp9QkYiIgP1ultMwbfbXoidyTdye4gnXSwWY3UhVFrbhGsk1Q5Sibmio0e69S+VM9ea TGJBq0p/AtTHajAqfgeREJE2DeOLFKFl1mKq5AdJyvRr8hPzM9Tw9gnjPls6h/mWKDSr4Vt3fpU ZdytzmhTNN90VRKzJNnS3N9B/Nt/AzVFWMJ0nKblGlhDj8DV91AxA4TaRphqnQ89sB6Ez/3NYxS 4Ic/whgr5Hzt5z9eBWRkycNUp4yguO2QJ3W4EJNUJKDGFVpxhRmmJModxyoOp4GUj5/PTxnLRh8 TN3nSjD2rLT4oQCWUmR+643U3GqXLK46Ma1RhsrIIuzH7GF6OpibRz8WCyTgxqtMiYcERTvwhw= = X-Received: by 2002:a50:a4a1:: with SMTP id w30-v6mr22934503edb.67.1536584157259; Mon, 10 Sep 2018 05:55:57 -0700 (PDT) X-Received: by 2002:a50:a4a1:: with SMTP id w30-v6mr22934428edb.67.1536584156202; Mon, 10 Sep 2018 05:55:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536584156; cv=none; d=google.com; s=arc-20160816; b=YDNQlp/3e8jxcXfh/wexIQlKbTvHSNRuAA+/2C8TbvVtul0MDm9ydbKLJSOIXoU7IR mysNDcOYCMoSqKVecfu70EgwCYgMviCwsyxoNKpn8BfTDzrkG9vf+3F8SujKSk8Tm8N5 w0nQrRotDbW2fIDTVx5fWtzfnxdwDfZHZYtwaMxcl7c6LESAue+d3JilLUzZquyi5MhV m/Qv8q1WNizfOMIV3NZYzHAiULMYFNZn0oceCf3GOFz3qkg6pv7uym8GGWmBBjPTqNBR K7kPFiXxBuCIfv+moVeihyIucev+tDQZH9LBdIDda3O43xGFw9yYZdnryCvLof6pbg+n AXzw== 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=/JOCw+NbfDz6lgIyF+Yv3IQvhEgkMeNnG6mNNRVK3Sc=; b=hV8F5uoVEu+sOR/1+JtTXQlidJDkUGdsS4JtvR0i3WjrM/8JTA2pz4e99w5h+2ILli zesprmeZQOtRYb5HWDNOEHRHovpz/p1rWqjxa6njAJ9BemcFZT485Oq+gYwWpktSwSSW fN9QXEVtGelyJ9NowUPimyrtBBhzu0wZBrk+h9y+voCaPkWJj1XKnhtTAEiqHl4SGH+Y ZoSGhq4HoLcqGwsuTnB0ZsV/dtWG7cgdvC2fElqmYEsWdEPDQTgBOVhdId8H8BbOaG2g OnxpBW4xc6VujnvIvlUYm3Hgs8eag50h0FoiLngZN8eUty/i4RsbgRa6PTDczFinbbXz p3Nw== 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 v1-v6sor14861574edf.9.2018.09.10.05.55.56 for (Google Transport Security); Mon, 10 Sep 2018 05:55:56 -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: ANB0VdaOoZhYwqbi7Nu/N2uezNhxj3dGT8BGbSUneV6AHhUfM329kZplq9bKtEyfwAOauXkq35SMyA== X-Received: by 2002:a50:8978:: with SMTP id f53-v6mr22635587edf.166.1536584155687; Mon, 10 Sep 2018 05:55:55 -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.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Sep 2018 05:55:55 -0700 (PDT) From: Michal Hocko To: Cc: Tetsuo Handa , Roman Gushchin , Andrew Morton , Michal Hocko Subject: [RFC PATCH 3/3] mm, oom: hand over MMF_OOM_SKIP to exit path if it is guranteed to finish Date: Mon, 10 Sep 2018 14:55:13 +0200 Message-Id: <20180910125513.311-4-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 David Rientjes has noted that certain user space memory allocators leave a lot of page tables behind and the current implementation of oom_reaper doesn't deal with those workloads very well. In order to improve these workloads define a point when exit_mmap is guaranteed to finish the tear down without any further blocking etc. This is right after we unlink vmas (those still depend on locks which are held while performing memory allocations from other contexts) and before we start releasing page tables. Opencode free_pgtables and explicitly unlink all vmas first. Then set mm->mmap to NULL (there shouldn't be anybody looking at it at this stage) and check for mm->mmap in the oom_reaper path. If the mm->mmap is NULL we rely on the exit path and won't set MMF_OOM_SKIP from the reaper. Signed-off-by: Michal Hocko --- mm/mmap.c | 24 ++++++++++++++++++++---- mm/oom_kill.c | 13 +++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 3481424717ac..99bb9ce29bc5 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3085,8 +3085,27 @@ void exit_mmap(struct mm_struct *mm) /* oom_reaper cannot race with the page tables teardown */ if (oom) down_write(&mm->mmap_sem); + /* + * Hide vma from rmap and truncate_pagecache before freeing + * pgtables + */ + while (vma) { + unlink_anon_vmas(vma); + unlink_file_vma(vma); + vma = vma->vm_next; + } + vma = mm->mmap; + if (oom) { + /* + * the exit path is guaranteed to finish without any unbound + * blocking at this stage so make it clear to the caller. + */ + mm->mmap = NULL; + up_write(&mm->mmap_sem); + } - free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); + free_pgd_range(&tlb, vma->vm_start, vma->vm_prev->vm_end, + FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); tlb_finish_mmu(&tlb, 0, -1); /* @@ -3099,9 +3118,6 @@ 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 diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 049e67dc039b..0ebf93c76c81 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -570,12 +570,10 @@ static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm) } /* - * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't - * work on the mm anymore. The check for MMF_OOM_SKIP must run - * under mmap_sem for reading because it serializes against the - * down_write();up_write() cycle in exit_mmap(). + * If exit path clear mm->mmap then we know it will finish the tear down + * and we can go and bail out here. */ - if (test_bit(MMF_OOM_SKIP, &mm->flags)) { + if (!mm->mmap) { trace_skip_task_reaping(tsk->pid); goto out_unlock; } @@ -624,8 +622,11 @@ static void oom_reap_task(struct task_struct *tsk) /* * Hide this mm from OOM killer because it has been either reaped or * somebody can't call up_write(mmap_sem). + * Leave the MMF_OOM_SKIP to the exit path if it managed to reach the + * point it is guaranteed to finish without any blocking */ - set_bit(MMF_OOM_SKIP, &mm->flags); + if (mm->mmap) + set_bit(MMF_OOM_SKIP, &mm->flags); /* Drop a reference taken by wake_oom_reaper */ put_task_struct(tsk);