From patchwork Tue Nov 16 21:57:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 12623223 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 932ACC433F5 for ; Tue, 16 Nov 2021 21:57:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3CB4963222 for ; Tue, 16 Nov 2021 21:57:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3CB4963222 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id CB1D16B0072; Tue, 16 Nov 2021 16:57:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C61B26B0073; Tue, 16 Nov 2021 16:57:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B02686B0074; Tue, 16 Nov 2021 16:57:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0098.hostedemail.com [216.40.44.98]) by kanga.kvack.org (Postfix) with ESMTP id A0BAE6B0072 for ; Tue, 16 Nov 2021 16:57:31 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 684A3869B7 for ; Tue, 16 Nov 2021 21:57:21 +0000 (UTC) X-FDA: 78816155082.16.2D4364B Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf07.hostedemail.com (Postfix) with ESMTP id 0FB5010000AC for ; Tue, 16 Nov 2021 21:57:20 +0000 (UTC) Received: by mail-pg1-f202.google.com with SMTP id z19-20020a630a53000000b002dc2f4542faso200536pgk.13 for ; Tue, 16 Nov 2021 13:57:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=Ds7CkJIXOL2mYL105KfyaplaQRNJT0sgaXWilVjc75w=; b=a4UUleXv4Vh0+4iNT2NyvFFpMROT9+yq0O/b+cpkv7VQa6AYLh4gfJfXpvxTfZbEH5 OPQn3NsKK79KyNEWT2Wyz0IRL/kOkJRIKvprEB5pu8jdfHXz5d4Ba01tNMMqIyuSUTFZ xMQ1rqK2GQX0Kfqv7ATKT7TGPs3AkOcHPbNOPAthwrZFdqKRyCEohjAgfvyYXOPgnnGg YWwhwcvY2C4XBGMJvrDfZlq1ILMglQO+sB6sDRhWjDM3gOQNbZNPteXwHqo4/CaHHPnY xjsMVcjn79zbjBbMC32Qakfoof0Dwlv7rchpJzMujCNyk8bq3ZXweo/rWiKDwWg44E1R BzVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=Ds7CkJIXOL2mYL105KfyaplaQRNJT0sgaXWilVjc75w=; b=SOHrtmIn1b4lGLkEO5ljZtws8K/JxaoPfGmpntjwRf3XGCzDZW+Ifn0QRQsOLyOp1m thJiQU6xAuPZ17gnMb6ihnMIcEiSm2Iy5RfmxWUxYJP2y1FgFlODZLZxk82zftlTs427 bXCXCU9+7q+on4M4CYnxBHn2fTgBzwhFp9wQNvykL8HSci26IJLQfiw7G02sPli8XJX4 q09r2x6qKW7OkN2JAcUsroEiHDdV8r8V6yQPtWFd7sNz/zEn5tt+Xhhh7pIxZsRh6NY9 Q34GGJs79Hu9JQMIAaP7zg/QEnelGzByAVTcxZzNrVMfFAxbfxVhwBGykuDudpB6dijR govg== X-Gm-Message-State: AOAM532bpZIoU0qlznDbtofWBDDcXNw/DIlsO38U3C4g0Af2a/B2roZQ 88OxW9O4MaeXLyV6sraP9HQh68XCFLg= X-Google-Smtp-Source: ABdhPJye+XfttuNAxgGm4XDnZjHBwsdwvyOlKQAeoGZWLGAuc/JE6AaUFQlJvGYlhrKfh4nhYdVuXmyX5pE= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:91d1:d198:1088:5801]) (user=surenb job=sendgmr) by 2002:a17:902:b718:b0:143:72b7:409e with SMTP id d24-20020a170902b71800b0014372b7409emr48831424pls.28.1637099839672; Tue, 16 Nov 2021 13:57:19 -0800 (PST) Date: Tue, 16 Nov 2021 13:57:14 -0800 Message-Id: <20211116215715.645231-1-surenb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.0.rc2.393.gf8c9666880-goog Subject: [PATCH 1/2] mm: protect free_pgtables with mmap_lock write lock in exit_mmap From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: mhocko@kernel.org, mhocko@suse.com, rientjes@google.com, willy@infradead.org, hannes@cmpxchg.org, guro@fb.com, riel@surriel.com, minchan@kernel.org, kirill@shutemov.name, aarcange@redhat.com, christian@brauner.io, hch@infradead.org, oleg@redhat.com, david@redhat.com, jannh@google.com, shakeelb@google.com, luto@kernel.org, christian.brauner@ubuntu.com, fweimer@redhat.com, jengelh@inai.de, timmurray@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0FB5010000AC X-Stat-Signature: 34t6ktzh9gnoy1kzxuht9xkhcnya6pae Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=a4UUleXv; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3PymUYQYKCPQoqnajXckkcha.Ykihejqt-iigrWYg.knc@flex--surenb.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3PymUYQYKCPQoqnajXckkcha.Ykihejqt-iigrWYg.knc@flex--surenb.bounces.google.com X-HE-Tag: 1637099840-433443 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: oom-reaper and process_mrelease system call should protect against races with exit_mmap which can destroy page tables while they walk the VMA tree. oom-reaper protects from that race by setting MMF_OOM_VICTIM and by relying on exit_mmap to set MMF_OOM_SKIP before taking and releasing mmap_write_lock. process_mrelease has to elevate mm->mm_users to prevent such race. Both oom-reaper and process_mrelease hold mmap_read_lock when walking the VMA tree. The locking rules and mechanisms could be simpler if exit_mmap takes mmap_write_lock while executing destructive operations such as free_pgtables. Change exit_mmap to hold the mmap_write_lock when calling free_pgtables. Operations like unmap_vmas() and unlock_range() are not destructive and could run under mmap_read_lock but for simplicity we take one mmap_write_lock during almost the entire operation. Note also that because oom-reaper checks VM_LOCKED flag, unlock_range() should not be allowed to race with it. In most cases this lock should be uncontended. Previously, Kirill reported ~4% regression caused by a similar change [1]. We reran the same test and although the individual results are quite noisy, the percentiles show lower regression with 1.6% being the worst case [2]. The change allows oom-reaper and process_mrelease to execute safely under mmap_read_lock without worries that exit_mmap might destroy page tables from under them. [1] https://lore.kernel.org/all/20170725141723.ivukwhddk2voyhuc@node.shutemov.name/ [2] https://lore.kernel.org/all/CAJuCfpGC9-c9P40x7oy=jy5SphMcd0o0G_6U1-+JAziGKG6dGA@mail.gmail.com/ Signed-off-by: Suren Baghdasaryan --- mm/mmap.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index bfb0ea164a90..69b3036c6dee 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3142,25 +3142,27 @@ void exit_mmap(struct mm_struct *mm) * 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(), + * This needs to be done before calling unlock_range(), * 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); - mmap_write_lock(mm); - mmap_write_unlock(mm); } + mmap_write_lock(mm); if (mm->locked_vm) unlock_range(mm->mmap, ULONG_MAX); arch_exit_mmap(mm); vma = mm->mmap; - if (!vma) /* Can happen if dup_mmap() received an OOM */ + if (!vma) { + /* Can happen if dup_mmap() received an OOM */ + mmap_write_unlock(mm); return; + } lru_add_drain(); flush_cache_mm(mm); @@ -3170,6 +3172,7 @@ void exit_mmap(struct mm_struct *mm) unmap_vmas(&tlb, vma, 0, -1); free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); tlb_finish_mmu(&tlb); + mmap_write_unlock(mm); /* * Walk the list again, actually closing and freeing it, From patchwork Tue Nov 16 21:57:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 12623225 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2738DC433EF for ; Tue, 16 Nov 2021 21:58:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C82DA615E2 for ; Tue, 16 Nov 2021 21:58:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C82DA615E2 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B2AE16B0073; Tue, 16 Nov 2021 16:57:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ABEFE6B0074; Tue, 16 Nov 2021 16:57:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 954CC6B007B; Tue, 16 Nov 2021 16:57:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0218.hostedemail.com [216.40.44.218]) by kanga.kvack.org (Postfix) with ESMTP id 87C726B0073 for ; Tue, 16 Nov 2021 16:57:33 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 4E8168248D52 for ; Tue, 16 Nov 2021 21:57:23 +0000 (UTC) X-FDA: 78816155166.21.F11873A Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf11.hostedemail.com (Postfix) with ESMTP id E6EF6F000209 for ; Tue, 16 Nov 2021 21:57:22 +0000 (UTC) Received: by mail-pg1-f201.google.com with SMTP id e4-20020a630f04000000b002cc40fe16afso184884pgl.23 for ; Tue, 16 Nov 2021 13:57:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=RcxO89LsZtHgdWSYVweaJQPcYAbZUnKnBWG821s8zas=; b=sadMJNIDL9S3tfFJIrrJR2oyw/Fpm76XFjUCAQxx5qAVWIfC1+8OULcZFMJktCxOlk tjU8MxNlVgGMBPq1Oc0bUjfB2bB7hK3Rg33hxLGdi0+HdDCir3/JiqMwjY16IK9oLj3L 5toksxo5EbqhHJHXksLvDXWhH8nj5han4FAR8DMJLIJqyUxKokkZfqavlMO97qvmhcb6 i5D0W/5LrZnSdelv0TJVhDNsBNPLITY6FhKwUpgthNfmATlnUZfLLqWESkX2LX+ypW/i z2ANTtjj5hLbZS9PJALUGqhUQgDxMzgEUYqmqUqDtAWjjDOn2ZB5sfPHVZFx0fRac1v7 m9Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=RcxO89LsZtHgdWSYVweaJQPcYAbZUnKnBWG821s8zas=; b=fsTrznzKhOey4uTUrX7GKkkCrRX8i06eEnfouMYfpqPyHF/w41EA17FGVfvzQrXhP9 KgLXYzJBrfAP7U6Kr07O6+QmRNGRgfazpPJrRU+pCIw+VmAhpkRYlh/XSzHR/JaXv3tE qsxebpAsqvgehPqdpOmBYfsUmSi2EPOCFtGlgsbIjcS+5G1JQguCxYeeOH7VAXRrimkI zK4KxGelV8P8BrN9LQOAH09jGC08XIXmcd2yXehlFBHN9UUN2WRujipJrSnI/hoXD2LG AxxdY83BLtXVKltXeZnEk4/Gls6pKzYtCP4UrMTOWcgjbdoHd3o+YbxzbU6xY1UIcsMn cSNw== X-Gm-Message-State: AOAM531Y3VSF4dMuDtKWVlYobwduk7Y0o8CTaSzoc4xIAnAmkj5BAZTQ Z4MMeZTQ2dXuJ5Lmms0iLIkn583CWho= X-Google-Smtp-Source: ABdhPJw97ZImSXy/35Rbj61WyaJeUIeljhV1L18+fR0M/gOLQuFIwGLtXq43ozIk7RQDTw8knKOeaXWo9zI= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:91d1:d198:1088:5801]) (user=surenb job=sendgmr) by 2002:a63:86c1:: with SMTP id x184mr1591868pgd.295.1637099841832; Tue, 16 Nov 2021 13:57:21 -0800 (PST) Date: Tue, 16 Nov 2021 13:57:15 -0800 In-Reply-To: <20211116215715.645231-1-surenb@google.com> Message-Id: <20211116215715.645231-2-surenb@google.com> Mime-Version: 1.0 References: <20211116215715.645231-1-surenb@google.com> X-Mailer: git-send-email 2.34.0.rc2.393.gf8c9666880-goog Subject: [PATCH 2/2] mm/oom_kill: allow process_mrelease to run under mmap_lock protection From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: mhocko@kernel.org, mhocko@suse.com, rientjes@google.com, willy@infradead.org, hannes@cmpxchg.org, guro@fb.com, riel@surriel.com, minchan@kernel.org, kirill@shutemov.name, aarcange@redhat.com, christian@brauner.io, hch@infradead.org, oleg@redhat.com, david@redhat.com, jannh@google.com, shakeelb@google.com, luto@kernel.org, christian.brauner@ubuntu.com, fweimer@redhat.com, jengelh@inai.de, timmurray@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sadMJNID; spf=pass (imf11.hostedemail.com: domain of 3QSmUYQYKCPYqspclZemmejc.amkjglsv-kkitYai.mpe@flex--surenb.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3QSmUYQYKCPYqspclZemmejc.amkjglsv-kkitYai.mpe@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: 51pkmyh1xqwipfst7ripqcncampaw3bx X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E6EF6F000209 X-HE-Tag: 1637099842-885205 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: With exit_mmap holding mmap_write_lock during free_pgtables call, process_mrelease does not need to elevate mm->mm_users in order to prevent exit_mmap from destrying pagetables while __oom_reap_task_mm is walking the VMA tree. The change prevents process_mrelease from calling the last mmput, which can lead to waiting for IO completion in exit_aio. Fixes: 337546e83fc7 ("mm/oom_kill.c: prevent a race between process_mrelease and exit_mmap") Signed-off-by: Suren Baghdasaryan --- mm/oom_kill.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 1ddabefcfb5a..67780386f478 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -1169,15 +1169,15 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) goto put_task; } - if (mmget_not_zero(p->mm)) { - mm = p->mm; - if (task_will_free_mem(p)) - reap = true; - else { - /* Error only if the work has not been done already */ - if (!test_bit(MMF_OOM_SKIP, &mm->flags)) - ret = -EINVAL; - } + mm = p->mm; + mmgrab(mm); + + if (task_will_free_mem(p)) + reap = true; + else { + /* Error only if the work has not been done already */ + if (!test_bit(MMF_OOM_SKIP, &mm->flags)) + ret = -EINVAL; } task_unlock(p); @@ -1188,13 +1188,16 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) ret = -EINTR; goto drop_mm; } - if (!__oom_reap_task_mm(mm)) + /* + * Check MMF_OOM_SKIP again under mmap_read_lock protection to ensure + * possible change in exit_mmap is seen + */ + if (!test_bit(MMF_OOM_SKIP, &mm->flags) && !__oom_reap_task_mm(mm)) ret = -EAGAIN; mmap_read_unlock(mm); drop_mm: - if (mm) - mmput(mm); + mmdrop(mm); put_task: put_task_struct(task); return ret;