Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3539190pxj; Mon, 7 Jun 2021 13:25:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0sa9b3L4xNcYL2UIJkAN9aLPGzzr9tXXVdoMCgRkr6Azf84Yn6tHxi13ls6FHXGYt2YI8 X-Received: by 2002:a17:906:b294:: with SMTP id q20mr19197035ejz.382.1623097532176; Mon, 07 Jun 2021 13:25:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623097532; cv=none; d=google.com; s=arc-20160816; b=zKRoxy/Jp2bYGp82eMRFMMizTlcGfqJ2+pSMju4R//VEihEvVWTqr4mjc5v0ShdeYk zH7Es5I+Squ0Dulys003q7bFurgdOeSvDqm4JP/miEKDYXnFQDDLy31RZoj/CY7jR4aM WTKR4mCF7HqaUk99PlG0ywPLEHnfQ9pzS3NcNKIzAZ9hNiN6t/Qp62KiUG2fP92xqINf OSjLxajhg1NAd1oykle+jxw3gGTcNSj2r0ZXN63kXWf/tAAriCjW3CvV+lNl1M1e+xaO F4ruhazTzxJDAa4wUBrVSO2SnRY2W2ps0HhWmhCFFXmJzg2igha8btfGCirDrl0rfjvF 28Ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Z7ZLcUGLD1CuKefmti9g45BjfBhezDkXgDccglbrfSo=; b=amGpIgAIYILJrAAdf+hJ4cbis69DwSE0bRY2u7UpzmT2pqeXHmMiYd2RCcX2qEDoEy ghMZM5uNJoGtuVin7jQ85f2kmeXBxMKzMnvIVNUpy6TeZCVM9ee+CTXlhIioVC8eOWY4 RHwHbv6UboIDKtMdRwLOJDipWouyb5DBpeSqKSloFc6qMnqVfVoywVgkWsqKny5nngI9 qhyNJSxCimsbXEVzbviUHprGW9FJM+YPrmud04rfbLULcbYjBmLmRlIBvNvKykgPLkv5 lfEgaQwPoj9PWIgS7ILa6sKzdmcG9tSgh7LiQxJCI/+Uc6xr15WasKWIFzNjvT7/6yOO 05TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=cB5nat+g; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a15si14339859edr.562.2021.06.07.13.25.08; Mon, 07 Jun 2021 13:25:32 -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=@infradead.org header.s=casper.20170209 header.b=cB5nat+g; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231322AbhFGUXi (ORCPT + 99 others); Mon, 7 Jun 2021 16:23:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230502AbhFGUXh (ORCPT ); Mon, 7 Jun 2021 16:23:37 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32E3FC061574 for ; Mon, 7 Jun 2021 13:21:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=Z7ZLcUGLD1CuKefmti9g45BjfBhezDkXgDccglbrfSo=; b=cB5nat+gRTIFlVwIsyDFgremu6 RFkiCHi/3xBSfucVb4YN+iM0lGj6BGgHHy8mvidLMVRolJ5BUdzwuUKuJZs0tNCPpt+GZVwH/n02w pkqmRRiuVD316PrOQepdlNah8CGIWYZfKKDf0wQzA25qch3s705qMtTFXYf6YY6l1LWaNvdMDMhEd eKJrjWL2NR0U1dSoBivAWlS7xb1uCJws6Jp8X3tQZ0RWfVJynVHKD9a4bXq0D9GQbZ1VvT3mwIcZW 8MsugKcw5YDzyyqFzQ7caCj8/OBtmichSa44yEbKlvVkerijgwJ/WHb/e948a8ruS+G+0id38Ly6P QHKsPWEw==; Received: from willy by casper.infradead.org with local (Exim 4.94 #2 (Red Hat Linux)) id 1lqLjn-00GCqZ-G7; Mon, 07 Jun 2021 20:20:53 +0000 Date: Mon, 7 Jun 2021 21:20:43 +0100 From: Matthew Wilcox To: Jann Horn Cc: Linux-MM , Zi Yan , Peter Xu , "Kirill A. Shutemov" , Konstantin Khlebnikov , Andrew Morton , chinwen.chang@mediatek.com, kernel list , syzkaller-bugs , Vlastimil Babka , Michel Lespinasse , syzbot Subject: Re: split_huge_page_to_list() races with page_mapcount() on migration entry in smaps code? [was: Re: [syzbot] kernel BUG in __page_mapcount] Message-ID: References: <00000000000017977605c395a751@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 07, 2021 at 09:55:09PM +0200, Jann Horn wrote: > On Mon, Jun 7, 2021 at 8:03 PM Matthew Wilcox wrote: > > On Mon, Jun 07, 2021 at 07:27:23PM +0200, Jann Horn wrote: > > > === Short summary === > > > I believe the issue here is a race between /proc/*/smaps and > > > split_huge_page_to_list(): > > > > > > The codepath for /proc/*/smaps walks the pagetables and (e.g. in > > > smaps_account()) calls page_mapcount() not just on pages from normal > > > PTEs but also on migration entries (since commit b1d4d9e0cbd0a > > > "proc/smaps: carefully handle migration entries", from Linux v3.5). > > > page_mapcount() expects compound pages to be stable. > > > > > > The split_huge_page_to_list() path first protects the compound page by > > > locking it and replacing all its PTEs with migration entries (since > > > the THP rewrite in v4.5, I think?), then does the actual splitting > > > using __split_huge_page(). > > > > > > So there's a mismatch of expectations here: > > > The smaps code expects that migration entries point to stable compound > > > pages, while the THP code expects that it's okay to split a compound > > > page while it has migration entries. > > > > Will it be a colossal performance penalty if we always get the page > > refcount after looking it up? That will cause split_huge_page() to > > fail to split the page if it hits this race. > > Hmm - but with that approach I'm not sure you could even easily take a > refcount on a page whose refcount may be frozen and which may be in > the middle of being shattered? get_page_unless_zero() is wrong because > you can't take references on tail pages, right? (Or can you?) And > try_get_page() is wrong because it bugs out if the refcount is zero - > and even if it didn't do that, you might end up holding a reference on > the head page while the page you're actually interested in is a tail > page? > > I guess if it was really necessary, it'd be possible to do some kind > of retry thing that grabs a reference on the compound head, then > checks that the tail page is still associated with the compound head, > and if not, drops the compound head and tries again? Right; that's how get_user_page_fast() works -- see try_get_compound_head(). If it can't get the reference, it just fails. I suspect for smaps, we can just choose to not count the page. It'll be an inaccuracy in the stats, but I don't think that's a big deal.