Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3280635pxf; Sun, 28 Mar 2021 19:52:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6+drfP3gomOC+Dvhj275qYpKpxmYyCQ89NVwv71mBQ/gXBmA+9OO4qRg6L6cTTHtKNf99 X-Received: by 2002:a17:906:d153:: with SMTP id br19mr25809364ejb.360.1616986353923; Sun, 28 Mar 2021 19:52:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616986353; cv=none; d=google.com; s=arc-20160816; b=Yupbfm8Mc4msowtsNyv89Sq/EaaFaH+geLui3jU13JkFa4XwVOB9oFUzvyK3AMMBwe /Jv5DU2UknDBmxQxQfymUc/VGiNnx1Sew9H7e5jdE0UFvA1KubxKU/4UZeQgrTNvaR+/ Y3arRozhbffRWuo9IaCrqPdQjthqeGVfSr+VYdsLiUokJ+5QEaSCzsZ2QfI+s+5E3mCV kbRDUMw7m9gKN9N01y8ou9nB50/lwBt910PJJmjD5hm/yqDMwre7JB/a+twvrVqTyl6W HI13aF7K+dgNoll3i58oHcLhZ3qpCue3d9+meQlhqo655v0XOkmTEgd6ab+3y8+UPNqc JyPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=L59rftjViXvOsrY5NrGI0yIgsP0PoylFj7glT8z5VLA=; b=j5bEy6mJsaox2BvshAHu4Frci/QG1kv8hRbNmC5PvH2aE2yHlTx1xzi8SZ4mUZ7E9R QCJa+4KrbR+e3UCAYj/CQHhnWYF42Yh9HE2LZ7s4nHWEdVkYee7gz3H/5kRCBzXAnZy8 YTbrorknbxGFVvm2D9/LuvW2YuhTMrA4quTJIDpuizFqTPqopmJY87ztTVtN4gtG79Us agLk4L9D1/5QlYQDDmn/A/wuEUW4nn+yKF8JuTs8cQNBm1/WT+2co+Hffozb3+xAPKiL xYj52uYoiZPjIH2KVQCzc1Ak5gSq/1jSpX0faJQk9xYE9Pln16S74o7GQw+b1kjoZwwr zTBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=KDQfUILI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a26si11654532edx.539.2021.03.28.19.52.11; Sun, 28 Mar 2021 19:52:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=KDQfUILI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230242AbhC2CtF (ORCPT + 99 others); Sun, 28 Mar 2021 22:49:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229645AbhC2Cs3 (ORCPT ); Sun, 28 Mar 2021 22:48:29 -0400 Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 308BAC061574; Sun, 28 Mar 2021 19:48:29 -0700 (PDT) Received: by mail-io1-xd2d.google.com with SMTP id e8so11311842iok.5; Sun, 28 Mar 2021 19:48:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=L59rftjViXvOsrY5NrGI0yIgsP0PoylFj7glT8z5VLA=; b=KDQfUILIwMRXKsgeGaJRP6v6nywZVjOo5VuqQQ2yEjfRK5Ce08mKavK4hnXHgxP31P +Pg2yL1aEnoOcnVprbrict2Wjg91/HNzHOy3LMJaKrfyNNhs7wN5Xr6deLyNmWf4tveX Hq9cI3IqZw6x0OKYjvA9BgllorHJ4D/oNALurteYu9r3emOTxn7I6O5Qt00wYpKfZDTS tEqNKEFkj6xrcZnYEgCEwUKqAK6taFBlmLcyXDh1XnFNmyIgWAh8gkxA3xlKdqGbDWr9 f43/lB2oo9Iqk2+d/96saECZ3QOXrmuupiWWJCWJoadLw/Ik90BEf/hxZb2j4KUJ6xlY CW1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=L59rftjViXvOsrY5NrGI0yIgsP0PoylFj7glT8z5VLA=; b=e23rCwfC23WjCbKVwWr8sjJjRwfg0f+4dzHxQZWZ6t+80Cd6z9zDGHDZHulUPXmLmT nGHRW/s0XKlJmpw+yuNVzSodtWeyAk+Jqis+VIwdjqgAIzU3vbaB/VYx5xRU6PJUOhPv abk+YlZMI9jra/qKmd7XWAnnZ6o6kFbZkAI8nJ0egXFm0y4xNrCrqmWFNfsZ00l/OaHB XjboUXw7bdKrlkO9gQ+8Uj6zs71r+NYbIDaGOBlYrltTvSCbjGH8AoPEqXnQ7g9KfPFE 0fe4hUKxE6zAlRCYtA+LjKgiWeWwgn3nDIA8Fp8inSR9+ZreWX4f+8j4CU5/NJ8wgI3V SF1A== X-Gm-Message-State: AOAM531f/WcVtuwc5adC3P1G35z8FIs0yFWkx2fLU1GJUfgiUS/YtZqo iaJ5/TAzCxoDsv0fqaLbiDvBfCG1VjgxNYNR9WQ= X-Received: by 2002:a6b:fc05:: with SMTP id r5mr18661308ioh.103.1616986108614; Sun, 28 Mar 2021 19:48:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ilya Lipnitskiy Date: Sun, 28 Mar 2021 19:48:17 -0700 Message-ID: Subject: Re: exec error: BUG: Bad rss-counter To: Zhou Yanjie Cc: "Eric W. Biederman" , Linux-MM , Linux Kernel Mailing List , linux-fsdevel , Kees Cook , Christoph Hellwig , Linus Torvalds Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Mar 20, 2021 at 8:59 AM Zhou Yanjie wro= te: > > Hi Ilya, > > On 2021/3/3 =E4=B8=8B=E5=8D=8811:55, Ilya Lipnitskiy wrote: > > On Wed, Mar 3, 2021 at 7:50 AM Eric W. Biederman wrote: > >> Ilya Lipnitskiy writes: > >> > >>> On Tue, Mar 2, 2021 at 11:37 AM Eric W. Biederman wrote: > >>>> Ilya Lipnitskiy writes: > >>>> > >>>>> On Mon, Mar 1, 2021 at 12:43 PM Eric W. Biederman wrote: > >>>>>> Ilya Lipnitskiy writes: > >>>>>> > >>>>>>> Eric, All, > >>>>>>> > >>>>>>> The following error appears when running Linux 5.10.18 on an embe= dded > >>>>>>> MIPS mt7621 target: > >>>>>>> [ 0.301219] BUG: Bad rss-counter state mm:(ptrval) type:MM_ANO= NPAGES val:1 > >>>>>>> > >>>>>>> Being a very generic error, I started digging and added a stack d= ump > >>>>>>> before the BUG: > >>>>>>> Call Trace: > >>>>>>> [<80008094>] show_stack+0x30/0x100 > >>>>>>> [<8033b238>] dump_stack+0xac/0xe8 > >>>>>>> [<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 > >>>>>>> > >>>>>>> So that's how I got to looking at fs/exec.c and noticed quite a f= ew > >>>>>>> changes last year. Turns out this message only occurs once very e= arly > >>>>>>> at boot during the very first call to kernel_execve. current->mm = is > >>>>>>> NULL at this stage, so acct_arg_size() is effectively a no-op. > >>>>>> If you believe this is a new error you could bisect the kernel > >>>>>> to see which change introduced the behavior you are seeing. > >>>>>> > >>>>>>> More digging, and I traced the RSS counter increment to: > >>>>>>> [<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 > >>>>>>> [<80003198>] ret_from_kernel_thread+0x14/0x1c > >>>>>>> > >>>>>>> In fact, I also checked vma_pages(bprm->vma) and lo and behold it= is set to 1. > >>>>>>> > >>>>>>> How is fs/exec.c supposed to handle implied RSS increments that h= appen > >>>>>>> due to page faults when discarding the bprm structure? In this ca= se, > >>>>>>> the bug-generating kernel_execve call never succeeded, it returne= d -2, > >>>>>>> but I didn't trace exactly what failed. > >>>>>> Unless I am mistaken any left over pages should be purged by exit_= mmap > >>>>>> which is called by mmput before mmput calls mmdrop. > >>>>> Good to know. Some more digging and I can say that we hit this erro= r > >>>>> when trying to unmap PFN 0 (is_zero_pfn(pfn) returns TRUE, > >>>>> vm_normal_page returns NULL, zap_pte_range does not decrement > >>>>> MM_ANONPAGES RSS counter). Is my understanding correct that PFN 0 i= s > >>>>> usable, but special? Or am I totally off the mark here? > >>>> It would be good to know if that is the page that get_user_pages_rem= ote > >>>> returned to copy_string_kernel. The zero page that is always zero, > >>>> should never be returned when a writable mapping is desired. > >>> Indeed, pfn 0 is returned from get_arg_page: (page is 0x809cf000, > >>> page_to_pfn(page) is 0) and it is the same page that is being freed a= nd not > >>> refcounted in mmput/zap_pte_range. Confirmed with good old printk. Al= so, > >>> ZERO_PAGE(0)=3D=3D0x809fc000 -> PFN 5120. > >>> > >>> I think I have found the problem though, after much digging and thank= s to all > >>> the information provided. init_zero_pfn() gets called too late (after > >>> the call to > >>> is_zero_pfn(0) from mmput returns true), until then zero_pfn =3D=3D 0= , and after, > >>> zero_pfn =3D=3D 5120. Boom. > >>> > >>> So PFN 0 is special, but only for a little bit, enough for something > >>> on my system > >>> to call kernel_execve :) > >>> > >>> Question: is my system not supposed to be calling kernel_execve this > >>> early or does > >>> init_zero_pfn() need to happen earlier? init_zero_pfn is currently a > >>> core_initcall. > >> Looking quickly it seems that init_zero_pfn() is in mm/memory.c and is > >> common for both mips and x86. Further it appears init_zero_pfn() has > >> been that was since 2009 a13ea5b75964 ("mm: reinstate ZERO_PAGE"). > >> > >> Given the testing that x86 gets and that nothing like this has been > >> reported it looks like whatever driver is triggering the kernel_execve > >> is doing something wrong. > >> Because honestly. If the zero page isn't working there is not a chanc= e > >> that anything in userspace is working so it is clearly much too early. > >> > >> I suspect there is some driver that is initialized very early that is > >> doing something that looks innocuous (like triggering a hotplug event) > >> and that happens to cause a call_usermode_helper which then calls > >> kernel_execve. > > I will investigate the offenders more closely. However, I do not > > notice this behavior on the same system based on the 5.4 kernel. Is it > > > I also encountered this problem on Ingenic X1000 and X1830. This is the > printed information: > > [ 0.120715] BUG: Bad rss-counter state mm:(ptrval) > type:MM_ANONPAGES val:1 > > I tested kernel 5.9, kernel 5.10, kernel 5.11, and kernel 5.12, only > kernel 5.9 did not have this problem, so we can know that this problem > was introduced in kernel 5.10, have you found any effective solution? Try: diff --git a/mm/memory.c b/mm/memory.c index c8e357627318..1fd753245369 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -166,7 +166,7 @@ static int __init init_zero_pfn(void) zero_pfn =3D 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) {