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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8EF6CDB47E for ; Fri, 20 Oct 2023 03:57:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 297868D01B9; Thu, 19 Oct 2023 23:57:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 248488D0003; Thu, 19 Oct 2023 23:57:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E9098D01B9; Thu, 19 Oct 2023 23:57:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id ECA7D8D0003 for ; Thu, 19 Oct 2023 23:57:30 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B0F5BC0ED8 for ; Fri, 20 Oct 2023 03:57:30 +0000 (UTC) X-FDA: 81364480260.28.FC54AD5 Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) by imf22.hostedemail.com (Postfix) with ESMTP id E8A65C000E for ; Fri, 20 Oct 2023 03:57:28 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=OgNFPkJJ; spf=pass (imf22.hostedemail.com: domain of hughd@google.com designates 209.85.219.170 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697774249; a=rsa-sha256; cv=none; b=tZu2kLABlVr5QEJ5IU3KxKFRtVRmMZnbHXFGhty8NviABXPctMxFapq7MzxjrP0Ve1wmAj QjpwkXPVl/hKJS1vGsM2bPRbV+t+cv1qycfDbg/+Kr4fxIup3nOUteBpctrqhucguxw8/a 7ZODUnFhU5fiQ+ruj4qPh8krMZnCUcc= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=OgNFPkJJ; spf=pass (imf22.hostedemail.com: domain of hughd@google.com designates 209.85.219.170 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697774249; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=FCG9D23X9LO7A11eS3HPLGicKoRO9n+ElQMyqWlHih0=; b=LbDekLlm+//yFrD3eJY4KuaGdGmGJN42eJOoijr8XTxbxGfVy1WP3AKSjB4A9UNhgKpV1i gel/jwWMueMfi7b1Y1PgorUEmKtiNrbTgPo3mTs482Yam+ib54/m7FN1NviS6jXrVpwwSM 40ffA/W06vktAW0Qj3/Sp7ejVd4xo5w= Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-d9a58aa4983so417119276.0 for ; Thu, 19 Oct 2023 20:57:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697774248; x=1698379048; darn=kvack.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=FCG9D23X9LO7A11eS3HPLGicKoRO9n+ElQMyqWlHih0=; b=OgNFPkJJFsDRhUO8SHUpPRU5Ff9dadRxw3eCabkwKomv+QhRkW5e9b5PJmKSvzwvAO iSE5/8ih20P7Tbo8h6DI1zgpdwFa7qrWfQRPgig48p8kyDyug9iJzsgxAqTt/o/bWpF5 X5GmS4QBv7llN78rRmRf35moAUogt3POdXDu1cC0ok3KIZmkmZyvOlInTSJYxp9U7gmV Rc4htG/0ekOkcrnSwHgUyewAsXcZZrLch6fqymlWk2k98l9fv84Tmk/oQy81K0SeiodN L7rq1dquxYUOA8PqlQ22ccsx0rFgKSWEHwCndMVJVpXJIAjytWS4Cf5iExgyF8C4Do1s mptg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697774248; x=1698379048; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FCG9D23X9LO7A11eS3HPLGicKoRO9n+ElQMyqWlHih0=; b=Bj4bod8tnw9ARP6sz0zoxasbAjUkuAlBF2t5f5FPRZ+5zjpqaN1Ofq+pcuxQHTv4AF vE8gbYLEbD4PUZ9UlFBNzce89w/LBP65tCKPMspJd1kKHmeUECTPYX+fo798mNUFMlbO hs1cHdgrSpPlZq0ttAaHecRyDx4PD/HwPri6nDN4BjmTZqUTOJtSq0WuaM5jVYtX88+G 0WPY4F3mdb9peAm7nf1s4htsmsnOwT2lHH128GY+mbB/3I2LPUK4pIQ36BWW6GDe54jQ KhVoSNGBdQJ/NAhSq7B2weI8t/6Wnugoh11JuxGocAg7kAo1w8KWqm9y7JjLPOff1vBj XGFg== X-Gm-Message-State: AOJu0YweGLA0gbqKwjDBtpZTaarjaHJFcRNSymtc0NzjHMWaZuFpEyEy YQ78Vn7XtmEvSUPlbukcE0pWyg== X-Google-Smtp-Source: AGHT+IEKaGDlhs5IjQyYFZANNeaAFcmTlSMdDnG4PXokOgibZs4ONYIlz6dQkWblO7RsNGqn9WdAXw== X-Received: by 2002:a25:ade4:0:b0:d9a:be04:7fae with SMTP id d36-20020a25ade4000000b00d9abe047faemr809376ybe.17.1697774247786; Thu, 19 Oct 2023 20:57:27 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id 205-20020a2505d6000000b00d814d8dfd69sm289751ybf.27.2023.10.19.20.57.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 20:57:26 -0700 (PDT) Date: Thu, 19 Oct 2023 20:57:04 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: mawupeng cc: hughd@google.com, akpm@linux-foundation.org, vbabka@suse.cz, linux-mm@kvack.org Subject: Re: [5.10/5.15 LTS] Question on mlock race between ksm and cow In-Reply-To: <200c9c35-bc59-4b0e-98e7-be1d937f3bbf@huawei.com> Message-ID: <10dcb0f4-3b0d-7388-e19c-67762914b4dc@google.com> References: <4263470f-77f8-47e2-be03-e1f8d790999e@huawei.com> <26614f10-5003-44f8-82c1-7ea204eec612@huawei.com> <200c9c35-bc59-4b0e-98e7-be1d937f3bbf@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E8A65C000E X-Stat-Signature: hdb5kmstkofdfn4ue3gz4qe8dogisnxa X-Rspam-User: X-HE-Tag: 1697774248-458946 X-HE-Meta: U2FsdGVkX1+njfy+gOihFpNCPL1wCM8WRQlJy6nARyIC8nykRXEzf3g1Uov1qp3ikJwCI2EzKBI2CYmy1JUrOt/0qhr3sgB7WfIHWd2SjvHikVxd8yH6xJjHTxIlCN/lJtqN5Dwpd7LadTHJtaJ52DBRhRX2Q5v8CTAwRjjZ+l+f+8Yw6Vncf0EsWnMuk7+SE7HLmlTopnDriNnpxC+PY8ZaaGniidF2ggNabqanpnLwCFRZLd3vMqI7hTt+5jslaLfeACZ67E3Gom+idWuQ8Y+Y+hXipIBMj1o9qy/GPF93zlB4+wdqAI1NxC/Q1mittuoQKB13iOhRWt6U588sna4JIVw8KMuW46G9V5svR2pmPKJF5X3KpAEoiFVJsTzUZms5wLJrSkuN0zEkqMFx/eV6BMN/BY3OKj+jEaZTW3lazXjVQOmrL3fWq83vwNcIT43JX7tMNRbXBdBbdjr4yLE00VH3fQQWS73b5ymRvrgxPdQGrYIfaqRgRacSFaWnPU8xAuKoNw1gsxmUOYvGuuE8F6Ftq82rnvv2PyuKtw0TBzmqhvKaPQ7jpEVg0qIRqkxY1VEjFFgGhTpyvsrECdqgs+iXZay3tdxaBbiblNMwzTlpSOex2L5I3j7d4fDIkefYD4UjdqYSX63ROYlavAnUpKvmpvhLhydr8eSiU7pdNRy2UvQ1FCMc0TuzDZLSzBnOnpykoujmA7IWZ4Ha+NQ0bJcZ9KU/WFldKQdAhWajcoFnYyIjaukTYNV6xp7V1sruftYPgPt40HzZxAL0lwCWPM6lo7yx2QzSJaAEKoLv9StlOPEB0Qjjrgtr3jzjxNAFGtMLBFSUShBWtZNXA7iu7AUvvMOAzYcQcuyB2jPq4qc9qXBsQUAPJMR64WvY4lOLykEMYOAxoo0Cev1tmq/Di6rIuEXgNB2o1Z993iDm5GPgrF+U07rqze5wHCGdIk/R90/PhdYwnQBY7ta gVSotf1g JKj2xJoGW563I23VEQ4CwTB5bk+Xr5GGUKXEk7ZAZQyVDdGZbkYk3oCHRuyABZTYZPkjbBhNO7disZhUb9kt5pufuSa1sKtMLj/3WEbM2RAeFQur+opKDmz5iqugUaUE6uei2mbngfOV21nExpu1Y07oXZYklz4t549sp7zmZNONDlxQkcZ+KDYdBn39cE8dT8UTeDTXbE5lqPQDU2UQCCFyge9Ct+xjGVn3mDKK+328/PqtCfouQmfw920NdSYi7Mbsoy+tw9WEB0235KhXdnFzUBL/4wacm+M3Ayd9pETvKLTqrwuhRl67AFaiYI4wCFPgMRUsJsSaDAWI= 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: On Wed, 18 Oct 2023, mawupeng wrote: > This problem can be easily reproduced with syz in LTS 5.10/5.15. > > Kindly ping. Sorry, but it takes me a long time to find a long enough slot to sink back deep enough into understanding these things. > > On 2023/9/18 9:07, mawupeng wrote: > > Kindly ping. > > > > On 2023/8/16 11:52, mawupeng wrote: > >> Since page_remove_rmap in wp_page_copy only clear this mlocked page iff > >> page's mapcount is -1. which can be seen as follow. > >> > >> wp_page_copy > >> page_remove_rmap > >> if (!atomic_add_negative(-1, &page->_mapcount)) > >> goto out; > >> clear_page_mlock(page); // clear mlocked flag > >> > >> During out test, we can test this mapcount before mlock the kpage, this > >> can close this race. > >> > >> diff --git a/mm/ksm.c b/mm/ksm.c > >> index 62feb478a367..347f4c0339c2 100644 > >> --- a/mm/ksm.c > >> +++ b/mm/ksm.c > >> @@ -1295,7 +1295,8 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, > >> if (!PageMlocked(kpage)) { > >> unlock_page(page); > >> lock_page(kpage); > >> - mlock_vma_page(kpage); > >> + if (page_mapcount(kpage) > 0) > >> + mlock_vma_page(kpage); Yes, that's safe enough; though if we thought for longer, we might devise other such races which this would not be enough for. But I'm not going to spend longer on it, this is good enough, if you find that it enables you to get much further with your syzbottery. If you prepare a proper Signed-off patch to send GregKH & stable, Cc me and I shall probably be able to support it with a grudging Ack. > >> page = kpage; /* for final unlock */ > >> } > >> } > >> > >> > >> On 2023/8/15 15:07, mawupeng wrote: > >>> Our syzbot reports a warning on bad page state. The mlocked flag is not > >>> cleared during page free. > >>> > >>> During try_to_merge_one_page in ksm, kpage will be remlocked if vma > >>> contains flag VM_LOCKED, however this flag is just cleared in wp_page_copy. > >>> Since the mapcount of this kpage is -1, no one can remove its mlocked flag > >>> before free, this lead to the bad page report. > >>> > >>> Since mlock changes a lot in v5.18-rc1[1], the latest linux do not have > >>> this problem. The 5.10/5.15 LTS do have this issue. I was glad to hear that it happens to have been fixed by 5.18 changes. I was going to complain that you're asking me for a fix to old kernels, just because I fixed it in new kernels. But that would be unfair: you'll have noticed that I was guilty of this KSM PageMlocked code too (and one of the pleasures of those mlock changes was being able to delete this). > >>> > >>> Here is the simplified calltrace: > >>> try_to_merge_one_page wp_page_copy > >>> > >>> try_to_merge_one_page > >>> // clear page mlocked during rmap removal > >>> replace_page > >>> page_remove_rmap > >>> if (unlikely(PageMlocked(page))) > >>> clear_page_mlock(compound_head(page)); > >>> > >>> if ((vma->vm_flags & VM_LOCKED) > >>> lock_page(old_page); > >>> if (vma->vm_flags & VM_LOCKED) > >>> if (PageMlocked(old_page)) > >>> munlock_vma_page(old_page); > >>> if (!PageMlocked(kpage)) > >>> lock_page(kpage); > >>> mlock_vma_page(kpage); > >>> unlock_page(kpage); > >>> ------------------------------------------------- > >>> > >>> This problem can be easily reproduced with the following modifies: > >>> 1. enable the following CONFIG > >>> a) CONFIG_DEBUG_VM > >>> b) CONFIG_KSM > >>> c) CONFIG_MEMORY_FAIALURE CONFIG_MEMORY_FAILURE > >>> > >>> 2. add delay in try_to_merge_one_page > >>> diff --git a/mm/ksm.c b/mm/ksm.c > >>> index a5716fdec1aa..f9ee2ec615ac 100644 > >>> --- a/mm/ksm.c > >>> +++ b/mm/ksm.c > >>> @@ -1248,8 +1248,10 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, > >>> > >>> if ((vma->vm_flags & VM_LOCKED) && kpage && !err) { > >>> munlock_vma_page(page); > >>> + mdelay(10); > >>> if (!PageMlocked(kpage)) { > >>> unlock_page(page); > >>> + mdelay(100); > >>> lock_page(kpage); > >>> mlock_vma_page(kpage); > >>> page = kpage; /* for final unlock */ > >>> > >>> 3. run syzbot with the following content: > >>> > >>> madvise(&(0x7f0000ff3000/0xc000)=nil, 0xc000, 0xc) > >>> mlockall(0x1) > >>> mlockall(0x5) > >>> madvise(&(0x7f0000ff3000/0xc000)=nil, 0xc04c, 0x65) 0x65: MADV_SOFT_OFFLINE /* soft offline page for testing */ Whereby CAP_SYS_ADMIN allows syzbot to take advantage of memory-failure testing backdoors to inject unexpected memory failures. Which gives it the right to TTU_IGNORE_MLOCK unmap pages which would ordinarily be kept mapped by VM_LOCKED. I said "grudging Ack" above, because I don't enjoy the time spent on working around such issues. There's lots of other damage that can be done with CAP_SYS_ADMIN, and with memory failures on kernel memory. But you've devised a patch which takes you further along... so okay. Please mention the CAP_SYS_ADMIN MADV_SOFT_OFFLINE error injection dependence in your commitlog - thanks. Hugh > >>> > >>> madvise(&(0x7f0000ff5000/0x4000)=nil, 0x4000, 0xc) > >>> mlockall(0x1) > >>> mlockall(0xa5) > >>> mlockall(0x0) > >>> munlock(&(0x7f0000ff7000/0x4000)=nil, 0x4000) > >>> > >>> ------------------------------------------------- > >>> The detail bug report can be seen as follow: > >>> > >>> BUG: Bad page state in process rs:main Q:Reg pfn:11406a > >>> page:fffff7b004501a80 refcount:0 mapcount:0 mapping:0000000000000000 index:0x20ff4 pfn:0x11406a > >>> flags: 0x30000000028000e(referenced|uptodate|dirty|swapbacked|mlocked|node=0|zone=3) > >>> raw: 030000000028000e fffff7b00456aec8 fffff7b011439908 0000000000000000 > >>> Soft offlining pfn 0x455e8f at process virtual address 0x20ff6000 > >>> raw: 0000000000020ff4 0000000000000000 00000000ffffffff 0000000000000000 > >>> page dumped because: PAGE_FLAGS_CHECK_AT_FREE flag(s) set > >>> Modules linked in: > >>> CPU: 1 PID: 239 Comm: rs:main Q:Reg Not tainted 5.15.126+ #580 > >>> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 0.0.0 02/06/2015 > >>> Call Trace: > >>> > >>> dump_stack_lvl+0x33/0x46 > >>> bad_page+0x9e/0xe0 > >>> free_pcp_prepare+0x14b/0x1f0 > >>> free_unref_page_list+0x7c/0x210 > >>> release_pages+0x2fe/0x3c0 > >>> __pagevec_lru_add+0x21a/0x360 > >>> lru_cache_add+0x80/0xe0 > >>> add_to_page_cache_lru+0x71/0xd0 > >>> pagecache_get_page+0x245/0x460 > >>> grab_cache_page_write_begin+0x1a/0x40 > >>> ext4_da_write_begin+0xb7/0x280 > >>> generic_perform_write+0xb4/0x1e0 > >>> ext4_buffered_write_iter+0x9c/0x140 > >>> ext4_file_write_iter+0x5b/0x840 > >>> ? do_futex+0x1af/0xb60 > >>> ? check_preempt_curr+0x21/0x60 > >>> ? ttwu_do_wakeup.isra.140+0xd/0xf0 > >>> new_sync_write+0x117/0x1b0 > >>> vfs_write+0x1ff/0x260 > >>> ksys_write+0xa0/0xe0 > >>> do_syscall_64+0x37/0x90 > >>> entry_SYSCALL_64_after_hwframe+0x67/0xd1 > >>> RIP: 0033:0x7fb815cef32f > >>> Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 29 fd ff ff 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 b8 05 <48> 3d 00 f0 ff ff 77 2d 44 89 c7 48 89 44 24 08 e8 5c fd ff ff 48 > >>> RSP: 002b:00007fb814b2b860 EFLAGS: 00000293 ORIG_RAX: 0000000000000001 > >>> RAX: ffffffffffffffda RBX: 00007fb808004f20 RCX: 00007fb815cef32f > >>> RDX: 000000000000006e RSI: 00007fb808004f20 RDI: 0000000000000007 > >>> RBP: 00007fb808004c40 R08: 0000000000000000 R09: 0000000000000000 > >>> R10: 0000000000000000 R11: 0000000000000293 R12: 00007fb808009550 > >>> R13: 000000000000006e R14: 0000000000000000 R15: 0000000000000000 > >>> > >>> > >>> [1]: https://lore.kernel.org/linux-mm/e7fbbdca-6590-7e45-3efd-279fba7f8376@suse.cz/T/#m0cb6e42b2a5ad634e1ec16e59f0f98f2e9382460