From patchwork Mon Mar 29 05:24:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Lipnitskiy X-Patchwork-Id: 12169503 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16B82C433DB for ; Mon, 29 Mar 2021 05:25:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6FE016195D for ; Mon, 29 Mar 2021 05:25:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6FE016195D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B2BE86B006C; Mon, 29 Mar 2021 01:24:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADB906B006E; Mon, 29 Mar 2021 01:24:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 954736B0070; Mon, 29 Mar 2021 01:24:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0086.hostedemail.com [216.40.44.86]) by kanga.kvack.org (Postfix) with ESMTP id 722A06B006C for ; Mon, 29 Mar 2021 01:24:59 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 37BC42C78 for ; Mon, 29 Mar 2021 05:24:59 +0000 (UTC) X-FDA: 77971772718.19.8BC8BB7 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf10.hostedemail.com (Postfix) with ESMTP id 231AB40001FE for ; Mon, 29 Mar 2021 05:24:56 +0000 (UTC) Received: by mail-pf1-f181.google.com with SMTP id c204so9147383pfc.4 for ; Sun, 28 Mar 2021 22:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=bAlA5isJ9rRIE85RSXmWhvh36Efvgn4+AnafoJbdfLk=; b=lpwoeJRYlzK5ORY0JsfaLhgvNY19UFdv0JdosiJwbvwN+J/BUOP9ZhUJUmsOJRuLN1 ikaSaQYX4wirVStbPpxr12VEAblYeo7/+vn3mXl+Pk0Lh0R77ReUW4DpUtXcPrRMbPIY puiQrPdCOgG3N5q2ZayWJU8JRY+ZBUhQ9sUX20W31Q5hiyXkF0P5kSdfu1SOWmmdeKDM AdAEM16EijR3fpTWVV0fe0UlbhIO1E5962W8IVHnElJ0g4FFroS0M6ELhH/9M24nKaGM oUTPMN42I5KzwtmMpLaRlmqpz0WYxhIyWX1PHm5BmEW8PsVA+bMoeOoBe5ry3Gfzv5q+ lTlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=bAlA5isJ9rRIE85RSXmWhvh36Efvgn4+AnafoJbdfLk=; b=LUhqOLjHIcwYG8ptWPVL/3HbvHbi2vP8Wu1bFGlUgS7k1UBWZ+BbRWLpy47CvpmP52 eyBRMgi1dn1ON176PSGOv8xWC/hLYTsvaDL1paceU1p+BGN08i6LXAfafkTaSt1w/FVg 2be9qlJJK7uKpDFgjBg8ucx5dMWHc3AgGOvZ3ldy48VXhJOOObBzIqc+TRt9WBwcfa9o qVgtjAITsWF16df/+1TXp0vg4RIhRs+6tcIpXfok3pUbc9Bb4rBR2SDJplD1QcRPqJl6 CjLfUW7dtIAX3+A721pmbPSOM9I/TBY0N9Q6QLBXxdL8X1NlWr03KnEVfmqP3PYBp5C/ 1SPA== X-Gm-Message-State: AOAM532S4t01celnc8UZpBIY6lKk1rwUgwAuYqRjithEqZCZIfp1F4ka nSUP2rdf/GxjoZWOQnnrRZg= X-Google-Smtp-Source: ABdhPJxQKNa6jNKc6WbeXXDaz7ckpek2dWgPBBt9V9AjSkvc1+UirWZP+wkK6Ep0J8jH8dwBLHGnzQ== X-Received: by 2002:a63:3245:: with SMTP id y66mr2654278pgy.435.1616995497799; Sun, 28 Mar 2021 22:24:57 -0700 (PDT) Received: from z640-arch.lan ([2602:61:7344:f100::678]) by smtp.gmail.com with ESMTPSA id q205sm16107886pfc.126.2021.03.28.22.24.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Mar 2021 22:24:57 -0700 (PDT) From: Ilya Lipnitskiy To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Ilya Lipnitskiy , "Eric W. Biederman" Subject: [PATCH] mm: fix race by making init_zero_pfn() early_initcall Date: Sun, 28 Mar 2021 22:24:26 -0700 Message-Id: <20210329052426.1130040-1-ilya.lipnitskiy@gmail.com> X-Mailer: git-send-email 2.31.0 MIME-Version: 1.0 X-Stat-Signature: 5kmo1ojxc86podzkjhauinypeee4xnon X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 231AB40001FE Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf10; identity=mailfrom; envelope-from=""; helo=mail-pf1-f181.google.com; client-ip=209.85.210.181 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616995496-328279 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: There are code paths that rely on zero_pfn to be fully initialized before core_initcall. For example, wq_sysfs_init() is a core_initcall function that eventually results in a call to kernel_execve, which causes a page fault with a subsequent mmput. If zero_pfn is not initialized by then it may not get cleaned up properly and result in an error: BUG: Bad rss-counter state mm:(ptrval) type:MM_ANONPAGES val:1 Here is an analysis of the race as seen on a MIPS device. On this particular MT7621 device (Ubiquiti ER-X), zero_pfn is PFN 0 until initialized, at which point it becomes PFN 5120: 1. wq_sysfs_init calls into kobject_uevent_env at core_initcall: [<80340dc8>] kobject_uevent_env+0x7e4/0x7ec [<8033f8b8>] kset_register+0x68/0x88 [<803cf824>] bus_register+0xdc/0x34c [<803cfac8>] subsys_virtual_register+0x34/0x78 [<8086afb0>] wq_sysfs_init+0x1c/0x4c [<80001648>] do_one_initcall+0x50/0x1a8 [<8086503c>] kernel_init_freeable+0x230/0x2c8 [<8066bca0>] kernel_init+0x10/0x100 [<80003038>] ret_from_kernel_thread+0x14/0x1c 2. kobject_uevent_env() calls call_usermodehelper_exec() which executes kernel_execve asynchronously. 3. Memory allocations in kernel_execve cause a page fault, bumping the MM reference counter: [<8015adb4>] add_mm_counter_fast+0xb4/0xc0 [<80160d58>] handle_mm_fault+0x6e4/0xea0 [<80158aa4>] __get_user_pages.part.78+0x190/0x37c [<8015992c>] __get_user_pages_remote+0x128/0x360 [<801a6d9c>] get_arg_page+0x34/0xa0 [<801a7394>] copy_string_kernel+0x194/0x2a4 [<801a880c>] kernel_execve+0x11c/0x298 [<800420f4>] call_usermodehelper_exec_async+0x114/0x194 4. In case zero_pfn has not been initialized yet, zap_pte_range does not decrement the MM_ANONPAGES RSS counter and the BUG message is triggered shortly afterwards when __mmdrop checks the ref counters: [<800285e8>] __mmdrop+0x98/0x1d0 [<801a6de8>] free_bprm+0x44/0x118 [<801a86a8>] kernel_execve+0x160/0x1d8 [<800420f4>] call_usermodehelper_exec_async+0x114/0x194 [<80003198>] ret_from_kernel_thread+0x14/0x1c To avoid races such as described above, initialize init_zero_pfn at early_initcall level. Depending on the architecture, ZERO_PAGE is either constant or gets initialized even earlier, at paging_init, so there is no issue with initializing zero_pfn earlier. ML discussion: https://lore.kernel.org/lkml/CALCv0x2YqOXEAy2Q=hafjhHCtTHVodChv1qpM=niAXOpqEbt7w@mail.gmail.com/ Signed-off-by: Ilya Lipnitskiy Cc: "Eric W. Biederman" --- mm/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/memory.c b/mm/memory.c index 46ef306375bd..a8bbc4fc121f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -166,7 +166,7 @@ static int __init init_zero_pfn(void) zero_pfn = page_to_pfn(ZERO_PAGE(0)); return 0; } -core_initcall(init_zero_pfn); +early_initcall(init_zero_pfn); void mm_trace_rss_stat(struct mm_struct *mm, int member, long count) {