From mboxrd@z Thu Jan 1 00:00:00 1970 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 F05A0C41513 for ; Thu, 4 Jul 2024 03:28:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40D7F6B0088; Wed, 3 Jul 2024 23:28:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BD216B008C; Wed, 3 Jul 2024 23:28:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 25DEA6B0093; Wed, 3 Jul 2024 23:28:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 06B906B0088 for ; Wed, 3 Jul 2024 23:28:47 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 746E7C0FA1 for ; Thu, 4 Jul 2024 03:28:46 +0000 (UTC) X-FDA: 82300638252.13.DCC5D4E Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf29.hostedemail.com (Postfix) with ESMTP id 94712120008 for ; Thu, 4 Jul 2024 03:28:44 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=N+EjkFUc; spf=pass (imf29.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720063701; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fYQ6q/N4S1rIbdR2X80ehJuOhiHy9Om7Jy4vYPXu9Hs=; b=PDbjqXqHXAxBxGDjrKIr7K0bqikKj1uvUrqPF6plEVZuE2f7W/1C8jMOmiP36By7MagIxC eRFybjxfm+Tj/cyf2RfKfTDVVORm7lriJUA0skwx5Ht3yDT8BNoAD81nqMpVSjKg2EHjX+ 7yubHhF3y9pscS5GSDkghmxvT9H9x6Q= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720063701; a=rsa-sha256; cv=none; b=2lMZ1A+cyFu3DlDNa/Rm+5iTTVd7Kgk1Ez+YKcpHv9WDuanQQ9AEPZducGiGA+9dbT6M9A dVcknhVyQK6Fb4Bg9C8/FvtZsCafqaCrXjZ6TUBTeZ2YSQfnle52vDUG/o/DRFK/K17fMa 1wtwHZ1Hzi/CcijALxo4HWA8KlIRJYY= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=N+EjkFUc; spf=pass (imf29.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 64DA062431; Thu, 4 Jul 2024 03:28:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7BCEC3277B; Thu, 4 Jul 2024 03:28:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1720063723; bh=5fXgIYSCBeWnApcXJyPKIVtz9r/QMZkA/T0lGQu2Wdc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=N+EjkFUc93iGPNLFFxaHJuGjS0cjYqXzi7rjARm20vooFV3VijeSswcXpG3J8QPKq DJFXv85U6dAlm2eUgRQmmzm4BDt/lfg6t9N/TCrgwAyRkKlF9JF8Ho/gPORQmpZezs 6Rmj2SvbwNPQXS5ROyRZqs9bHWFon8imyjemTGO4= Date: Wed, 3 Jul 2024 20:28:42 -0700 From: Andrew Morton To: Hugh Dickins Cc: Baolin Wang , Nhat Pham , Yang Shi , Zi Yan , Barry Song , Kefeng Wang , David Hildenbrand , Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH hotfix] mm: fix crashes from deferred split racing folio migration Message-Id: <20240703202842.e9e50fbeba1ea0cd3a4605f1@linux-foundation.org> In-Reply-To: <825653a7-a4d4-89f2-278f-4b18f8f8da5d@google.com> References: <29c83d1a-11ca-b6c9-f92e-6ccb322af510@google.com> <20240703193536.78bce768a9330da3a361ca8a@linux-foundation.org> <825653a7-a4d4-89f2-278f-4b18f8f8da5d@google.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 94712120008 X-Stat-Signature: rtmit1wq5sbihdf3rmxz88ki95w9r5wq X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1720063724-738524 X-HE-Meta: U2FsdGVkX1+pkQfTLjT9pruaqFMPy52BWRQYHjxxwQLa8ZTTiJO9BeXW1bQEoPipw6kTAUTgybiZh22WldmaXgeYy3pPfbOGPmMC3Xz/OSnyHCqP9UI62J+ItPnKPvYFYHJvY5Rxxi/qercdXA+KnlHvg5+GXAPsvYB77GIFSlbxO6Hw8WvW8gdPtarLSiHFC4dPWIWDE61ts6Rn8XA+aDTk/pp2paNF4DECuvNoy1wAS8N//SgXMhpBWAHwAcBAqASFNgPrd0GMNPCe6p/FhMw1IfTqjqM0n8Sgi6VVMXbqc7rQGgHuxR4GohSDj4p2n+EIgrenXZRcqrSSMNB5rxFOKJ2Qg0Z+t9CvWfGfc6Ug7OlXwh3F6kyGeONITe7VhnpkVMiVNmyMHK+VM5THJ1XoLhwxwI1q8DB0EzI5E5u9aliqEow9XsknxYHIAuEp4JXEAryGBlFWoqovYVSZJICFiz51Q4Is5sytimy1yVaXx0QP4ytz4L8B8YWKt+Z2raisB+b+mvni9GqCtjSzp9NE0dDSGdViKBu3PFYdgiTxuhxKGIKs7Hg+5qakRpSzytOneNiYWOiJ6Kpy4tc1r8S7FJDjLALjdJlcMLVL+8TkEgsc1d0+S9WgVSF2PQ90MQ6ZJeL6meBjhDKv2LSXFBBNjQLqBNAh+I2U1v7zjsbZW/NQotGiuhuhlgTQj6z1ftfkUohJ4tAvO6cODMAP7plreKV/g9CGI8UGcUckoiNpiyCXolWLoSuxkIk/MR/t7Jex11J6+TJpx0UUK/87lS14LuhpHPipAup3JnJi2yWpWk2MDADk5/uQ/DUdqRL9ncE3laD6CXBCyDkC2AvFdSXglq+IdG8wd0DghJHz3KIyZMCi8kdfz+AKR5yAqSMVq7fr0Wciy1GTstpPP354guWT8g3jGka02hGm2lIEb76HXzy7YC1K6ij1m/uUoN9sMuxH24dCjYlvdrBKUcE sSsKlFdF LAlhL3sZaWZB90k15FvJWf+uz0blU7+NoxAUef+l/rfJix8XFN0brnx/iUVzm+WXcCzq69Q+O1qJKbpwPvKjy675kP3dToSchtRdn8tze3zkO9egKnKUuI/OB+B7sMOQhuL6p8LUSv6SvV6dkER6GJzUpdZoPyZTjkGD4JfOEZNFtz3GjsfWw1IIYNkBwQ7yHmTIQnlpA3Kx7HkdXBHHo3i8RR4OZ/Opg+N0qQkhYVNKyPdiRxZXRFrCM4csIhWWKmh79kbA6qJVz2L/HgG/vlYXQeiyEUZwl7xm34n669yJxNtdXwfG4yyXfND7NJ/P9Esv0LV3+NRh/Zgo= 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: List-Subscribe: List-Unsubscribe: On Wed, 3 Jul 2024 20:21:22 -0700 (PDT) Hugh Dickins wrote: > On Wed, 3 Jul 2024, Andrew Morton wrote: > > On Tue, 2 Jul 2024 00:40:55 -0700 (PDT) Hugh Dickins wrote: > > > > > Even on 6.10-rc6, I've been seeing elusive "Bad page state"s (often on > > > flags when freeing, yet the flags shown are not bad: PG_locked had been > > > set and cleared??), and VM_BUG_ON_PAGE(page_ref_count(page) == 0)s from > > > deferred_split_scan()'s folio_put(), and a variety of other BUG and WARN > > > symptoms implying double free by deferred split and large folio migration. > > > > > > 6.7 commit 9bcef5973e31 ("mm: memcg: fix split queue list crash when large > > > folio migration") was right to fix the memcg-dependent locking broken in > > > 85ce2c517ade ("memcontrol: only transfer the memcg data for migration"), > > > but missed a subtlety of deferred_split_scan(): it moves folios to its own > > > local list to work on them without split_queue_lock, during which time > > > folio->_deferred_list is not empty, but even the "right" lock does nothing > > > to secure the folio and the list it is on. > > > > > > Fortunately, deferred_split_scan() is careful to use folio_try_get(): so > > > folio_migrate_mapping() can avoid the race by folio_undo_large_rmappable() > > > while the old folio's reference count is temporarily frozen to 0 - adding > > > such a freeze in the !mapping case too (originally, folio lock and > > > unmapping and no swap cache left an anon folio unreachable, so no freezing > > > was needed there: but the deferred split queue offers a way to reach it). > > > > There's a conflict when applying Kefeng's "mm: refactor > > folio_undo_large_rmappable()" > > (https://lkml.kernel.org/r/20240521130315.46072-1-wangkefeng.wang@huawei.com) > > on top of this hotfix. > > Yes, anticipated in my "below the --- line" comments: > sorry for giving you this nuisance. np > And perhaps a conflict with another one of Kefeng's, which deletes a hunk > in mm/migrate.c just above where I add a hunk: and that's indeed how it > should end up, hunk deleted by Kefeng, hunk added by me. Sorted, I hope. > > > > --- mm/memcontrol.c~mm-refactor-folio_undo_large_rmappable > > +++ mm/memcontrol.c > > @@ -7832,8 +7832,7 @@ void mem_cgroup_migrate(struct folio *ol > > * In addition, the old folio is about to be freed after migration, so > > * removing from the split queue a bit earlier seems reasonable. > > */ > > - if (folio_test_large(old) && folio_test_large_rmappable(old)) > > - folio_undo_large_rmappable(old); > > + folio_undo_large_rmappable(old); > > old->memcg_data = 0; > > } > > > > I'm resolving this by simply dropping the above hunk. So Kefeng's > > patch is now as below. Please check. > > Checked, and that is correct, thank you Andrew. great. > Correct, but not quite > complete: because I'm sure that if Kefeng had written his patch after > mine, he would have made the equivalent change in mm/migrate.c: > > --- a/mm/migrate.c > +++ b/mm/migrate.c > @@ -443,8 +443,7 @@ int folio_migrate_mapping(struct address_space *mapping, > } > > /* Take off deferred split queue while frozen and memcg set */ > - if (folio_test_large(folio) && folio_test_large_rmappable(folio)) > - folio_undo_large_rmappable(folio); > + folio_undo_large_rmappable(folio); > > /* > * Now we know that no one else is looking at the folio: > > But there's no harm done if you push out a tree without that additional > mod: we can add it as a fixup afterwards, it's no more than a cleanup. OK, someone please send that along? I'll queue it as a -fix so a single line of changelog is all that I shall retain (but more is welcome! People can follow the Link:) > (I'm on the lookout for an mm.git update, hope to give it a try when it > appears.) 12 seconds ago.