From patchwork Fri Oct 22 01:46:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 12576855 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 0D5E0C433F5 for ; Fri, 22 Oct 2021 01:47:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 80C576008E for ; Fri, 22 Oct 2021 01:47:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 80C576008E 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 EC732900003; Thu, 21 Oct 2021 21:47:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E7686900002; Thu, 21 Oct 2021 21:47:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6684900003; Thu, 21 Oct 2021 21:47:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0220.hostedemail.com [216.40.44.220]) by kanga.kvack.org (Postfix) with ESMTP id C9995900002 for ; Thu, 21 Oct 2021 21:47:03 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 839C48249980 for ; Fri, 22 Oct 2021 01:47:03 +0000 (UTC) X-FDA: 78722385126.21.8FA4037 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf20.hostedemail.com (Postfix) with ESMTP id 3FCBDD0000B0 for ; Fri, 22 Oct 2021 01:46:59 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id s4-20020a252c04000000b005be94057849so2337486ybs.21 for ; Thu, 21 Oct 2021 18:47:02 -0700 (PDT) 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=OSz5Xs/L17ZVVFibJw8deZ4Hpezvx7aG8ZEAixdJaAQ=; b=HtioIS3XvjJWvKb20k1mf4Zj5siAmZj1FBi/lBdkkAdxYzwIqeBQm0wDo8mScUhPtz /6aeFgG9jBeyNeSwA8rRbd4Ty7xf7sy2LA0VfTezC85UujHZQoMDuWgFEM7A7XmrTZZ6 yPwkg+vN9HhSJtHfOUM+HTAwKuarMvUC7lEqnpOaDt9Ns1ohpR3Xfzt6l+lbF5XJX2ri OZn2jlAFqSDDV3FRJ8rs254BxBIblUb1PBSVgVg55cy4GHBlBlFZmXDR29h7JnoOubg/ Y8i9IMtLYPS7ZGxNEgDMj+ldVTgdVmWTMN/AJEKsJtmDbQTAjFFgPyUDN3a9u/gJKTM/ jxNg== 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=OSz5Xs/L17ZVVFibJw8deZ4Hpezvx7aG8ZEAixdJaAQ=; b=G9F9i1IXrwdmG6IuFlholG3KvQnMXIKra9s/V8hjlh2mG4AtvlZTQ4ODQXq5xgeQAa e6cy6GxEadaY81UW9Y9nFEV8rJZSss09PG0Bud+lsxrRke6206NS4W1qYJdbMg/QPi8F dQOHrt9bJhTvgOBJKcUW0dYeBaNKzN8iSDWkDc6fanCErT2PfPGozZxM0aM/6B15rHi+ JRlEOCthz785Kge4krcBwhB7ee+L/IZ6Km1atxp18F+WMtR/bwidNd09HrTjaeT95Bfi QWVygAWgdRDYjmjBl7jzE+AerrwkZ8Tff3JwBTBjo8GKHRgkF92EOysTALofRRDnd8PI YqcQ== X-Gm-Message-State: AOAM532xh9F7HryV0I4ffCQ2rCSyvgkyc5vvpNj3ZywQ5n37+znzPT4d diEm0KyBrIgmd3d5A2WUK9yTAhCw/4E= X-Google-Smtp-Source: ABdhPJyPE9Pi2rOd+GtIRKy74MwB8b6FJ6jUd7F71YgVyepNEY0BZggKczOYEcmw6bLz121N12RDlg9MYXA= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:c063:38ee:4ca3:d373]) (user=surenb job=sendgmr) by 2002:a25:c344:: with SMTP id t65mr10532797ybf.409.1634867222322; Thu, 21 Oct 2021 18:47:02 -0700 (PDT) Date: Thu, 21 Oct 2021 18:46:58 -0700 Message-Id: <20211022014658.263508-1-surenb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH 1/1] mm: prevent a race between process_mrelease and 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, 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, linux-api@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Stat-Signature: t8czeye69usiz9dfa436uwk453umm6zh Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HtioIS3X; spf=pass (imf20.hostedemail.com: domain of 3FhhyYQYKCCEPROBK8DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3FhhyYQYKCCEPROBK8DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3FCBDD0000B0 X-HE-Tag: 1634867219-865304 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: Race between process_mrelease and exit_mmap, where free_pgtables is called while __oom_reap_task_mm is in progress, leads to kernel crash during pte_offset_map_lock call. oom-reaper avoids this race by setting MMF_OOM_VICTIM flag and causing exit_mmap to take and release mmap_write_lock, blocking it until oom-reaper releases mmap_read_lock. Reusing MMF_OOM_VICTIM for process_mrelease would be the simplest way to fix this race, however that would be considered a hack. Fix this race by elevating mm->mm_users and preventing exit_mmap from executing until process_mrelease is finished. Patch slightly refactors the code to adapt for a possible mmget_not_zero failure. This fix has considerable negative impact on process_mrelease performance and will likely need later optimization. Fixes: 884a7e5964e0 ("mm: introduce process_mrelease system call") Signed-off-by: Suren Baghdasaryan Acked-by: Michal Hocko Signed-off-by: Matthew Wilcox (Oracle) --- mm/oom_kill.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 831340e7ad8b..989f35a2bbb1 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -1150,7 +1150,7 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) struct task_struct *task; struct task_struct *p; unsigned int f_flags; - bool reap = true; + bool reap = false; struct pid *pid; long ret = 0; @@ -1177,15 +1177,15 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) goto put_task; } - mm = p->mm; - mmgrab(mm); - - /* If the work has been done already, just exit with success */ - if (test_bit(MMF_OOM_SKIP, &mm->flags)) - reap = false; - else if (!task_will_free_mem(p)) { - reap = false; - ret = -EINVAL; + 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; + } } task_unlock(p); @@ -1201,7 +1201,8 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) mmap_read_unlock(mm); drop_mm: - mmdrop(mm); + if (mm) + mmput(mm); put_task: put_task_struct(task); put_pid: