Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp143382lqh; Wed, 27 Mar 2024 18:27:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWprxT5bldabNhHeMk+TIX5KVgNnVm/w5gJgxoJFxVpotyNEEX5Mpwa8UpxZ9gT7erqZfLwcySBzlx1NFGfEWZMS0/ziZWpG1InYvL+Hw== X-Google-Smtp-Source: AGHT+IEbvhpakQRhIF6n2WhXqxru0/EO+2Kydz3k5k5YFFB0AluqHznFTw6lfgEUHM2u2P0g8lSV X-Received: by 2002:a50:d695:0:b0:56b:d18b:dda6 with SMTP id r21-20020a50d695000000b0056bd18bdda6mr1319521edi.17.1711589273745; Wed, 27 Mar 2024 18:27:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711589273; cv=pass; d=google.com; s=arc-20160816; b=Arx89npS907zIR1FS0Y2UwlmVTL5omaGuQ88i89DIp8mfv2szblZ7iR/eBJsxxx5i2 pNN1JgcGGuiIuVZlHXPnV3XIkGCWpkMNJcRyP9QA0B/h4Dyj5i2qbudk8YNwtYcAO9Xm TSs+CXnyLfiYG5/cteYniGpiomlMSyfoRxbmTKDpb0GFt69th86Wrfi7DXJdWPedYTxc ibNasd3PP5jnvrVcdjr4WTyTgHf80Kg4hhnpr8hvFGNWvhaTRHGNxgD450ZDJtSoGXQD z0+P4rJlzA5ljNwt790pnStzi1IF3v8VXOfih27tHVW/n8BblR2WKTaWuDNxyMrHcVcO NENw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=a4+6bhV4ASkyXzhB6rRjo48e6+mgSPLqfD3+eCsrCCs=; fh=JwVLW29uDiHhsk8qv2q0YGucw6+Qf9o7AzEnA6GyZnc=; b=mLk7PyqSmaKqd9zKYL2H8VGmi4HuDMPrKp9WIZEkTMcvNzZ0KrncGN1tYuDb2Uehjn HJSHdyV3/03mIJUkdU1v8qs2tKfXHJglZgljVKI4Yif/EceEIQYpqgayF3TSZRmYsHbh V1wPhAS0tiZ4bS3OaK/Vo51l8YlvUv9rrNyeI5pSdlyrPILVn88B/P3nnNKTMYzZgTJG vTHwL2cPRwowyRYTeA0R3zxBK/VwUtLe/KS7Wj4dsNr3xebpCZ+nOkqZh3jMHvVGirK2 P5JBWiosVzTN2KO7BPjq2QCWDYeAuEhiR/xR9mE2HtHfgJRbmXCIAHHzrc7mTJXSFc0F Pamg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=iWgsu5tL; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-122275-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122275-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id v23-20020a50a457000000b0056c27851998si198971edb.330.2024.03.27.18.27.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 18:27:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122275-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=iWgsu5tL; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-122275-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122275-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5412D1F280C9 for ; Thu, 28 Mar 2024 01:27:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 630EA19BDC; Thu, 28 Mar 2024 01:27:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iWgsu5tL" Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA4788465 for ; Thu, 28 Mar 2024 01:27:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711589266; cv=none; b=PJ0F2m7VhQPhGuunlGRXM/9EqYEgqDcspQ+eDZlV/LxUnsLf81kF3GEDhq8OmQhfKNS99exe9O5cenfpTWoiapcwRgxUZZ4vl8tdDQN2c3xLoWxCaCr6fPyVAPu0g4tTUUFBfkG37eCZnum09TNqMKgnu8N8AM9+ri46Au6r47Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711589266; c=relaxed/simple; bh=fBO8KAJmy3wMi2rgjJu6UmFyCOj/5ge41qGaheG6254=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=bzcgxPvHkyT8KtbPZegFChIhW0GVzea47CVpR136s7fwHIXsQJBx4FtcYwcPUkIoxisD3sy38PKEUi7a0gD6b2F+e5/yr1EV09XFsLO06msQzuWbTDFBoJGtGIXEO0D7Z4JKKV0x3rIJOnp7ZydpMZhjVN05XfF1yEAAAahwglY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iWgsu5tL; arc=none smtp.client-ip=209.85.208.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2d485886545so5983301fa.2 for ; Wed, 27 Mar 2024 18:27:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711589263; x=1712194063; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=a4+6bhV4ASkyXzhB6rRjo48e6+mgSPLqfD3+eCsrCCs=; b=iWgsu5tLNneGAI8ArfZPjiBnze9Cbxblk/hUzM50dnEUcpBxwcNomO28paj/vD9S74 R2v14eX59VmKihIuZDyqhKYmqNzmMJ67qOmgXtzF4hdLSrRY2EabLXi4YR/hZjjz9mKC Nb5kXgQsQCaQvS+E+p2W267ACEkBwSbA8Z9hNkZNj8Fxw6IcyQ+ymFp++M2D0mTYGjlK ZtQOVLIDrSwmUrYXAr4BVzKu8WNAcBeZ21UaJg+Sy+X/qrkepcj42h8uoCcxJjE/kltZ G+Kohp5GJDCxa+c36n7EdoUq5KgKBUPY/2k7bETVBbvTxoIoUsZ2bz9EGcZCBMwx3nZI vwyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711589263; x=1712194063; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a4+6bhV4ASkyXzhB6rRjo48e6+mgSPLqfD3+eCsrCCs=; b=Ci2l419iPFxvc2Rg+KPG3NA2cTI5KB/muhgnvsnMz6QdJHS3gKoV4/UrYzpfQCdMxQ 85Kt4sVDcSK3MwGGPhTL7oIMkVif+KhbNlA0n6T6wv8d/ySiLhtumMIhc6U4gVmA6+HA 997qMZypeSksTGrhyRaKEZuN/stGvMjkT7E6pGRcvYc/ElN62OysfaW69zfcpI6FhB9I ynY5sKKUb3Pzkm0exkEMnWfZNQqg17kFCNAGNn+bmCbUYn9FO4TP2MWRqUHOn+G9fULP XgTVXSDeiln2n+OTgZmz9Nrz6OYmK6RjpYesiBITFil1ijMVw/NPO8tOdx09S2r5ONAP ljgA== X-Forwarded-Encrypted: i=1; AJvYcCU4BXJazgR2lnWnzSO6IoYFw4DJERdhS1MYcdjtNerqsL6atHeVn5mQ8jkhR7OdvLIixctnheB9Hc3bSFXPGaJb5QGYLLWFeXcNdp7t X-Gm-Message-State: AOJu0YyT2RmG1BDdL17LHpmz/wyUPCPkCuAJl0yqmIb3HqOZbKKp7nL7 PUjOa/3SPKSeXq7uDye+av83XakJ2yE/qb5E6Yv+k+CMq/biaNGILEUmnIxwNHO/EtlYYyS/eMm dVESviF39Vf0D35LZyE77M4rxrXk= X-Received: by 2002:a2e:8551:0:b0:2d5:acde:9065 with SMTP id u17-20020a2e8551000000b002d5acde9065mr1501774ljj.34.1711589262680; Wed, 27 Mar 2024 18:27:42 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: In-Reply-To: From: Zhaoyang Huang Date: Thu, 28 Mar 2024 09:27:31 +0800 Message-ID: Subject: Re: summarize all information again at bottom//reply: reply: [PATCH] mm: fix a race scenario in folio_isolate_lru To: Matthew Wilcox Cc: =?UTF-8?B?6buE5pyd6ZizIChaaGFveWFuZyBIdWFuZyk=?= , Andrew Morton , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , =?UTF-8?B?5bq357qq5ruoIChTdGV2ZSBLYW5nKQ==?= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Mar 27, 2024 at 8:31=E2=80=AFPM Matthew Wilcox wrote: > > On Wed, Mar 27, 2024 at 09:25:59AM +0800, Zhaoyang Huang wrote: > > > Ignoring any other thread, you're basically saying that there's a > > > refcount imbalance here. Which means we'd hit an assert (that folio > > > refcount went below zero) in the normal case where another thread was= n't > > > simultaneously trying to do anything. > > Theoretically Yes but it is rare in practice as aops->readahead will > > launch all pages to IO under most scenarios. > > Rare, but this path has been tested. > > > read_pages > > aops->readahead[1] > > ... > > while (folio =3D readahead_folio)[2] > > filemap_remove_folio > > > > IMO, according to the comments of readahead_page, the refcnt > > represents page cache dropped in [1] makes sense for two reasons, '1. > > The folio is going to do IO and is locked until IO done;2. The refcnt > > will be added back when found again from the page cache and then serve > > for PTE or vfs' while it doesn't make sense in [2] as the refcnt of > > page cache will be dropped in filemap_remove_folio > > > > * Context: The page is locked and has an elevated refcount. The calle= r > > * should decreases the refcount once the page has been submitted for I= /O > > * and unlock the page once all I/O to that page has completed. > > * Return: A pointer to the next page, or %NULL if we are done. > > Follow the refcount through. > > In page_cache_ra_unbounded(): > > folio =3D filemap_alloc_folio(gfp_mask, 0); > (folio has refcount 1) > ret =3D filemap_add_folio(mapping, folio, index + i, gfp_= mask); > (folio has refcount 2) > > Then we call read_pages() > First we call ->readahead() which for some reason stops early. > Then we call readahead_folio() which calls folio_put() > (folio has refcount 1) > Then we call folio_get() > (folio has refcount 2) > Then we call filemap_remove_folio() > (folio has refcount 1) > Then we call folio_unlock() > Then we call folio_put() ok, I missed the refcnt from alloc_pages. However, I still think it is a bug to call readahead_folio in read_pages as the refcnt obtained by alloc_pages should be its final guard which is paired to the one which checked in shrink_folio_list->__remove_mapping->folio_ref_freeze(2)(this 2 represent alloc_pages & page cache). If we removed this one without isolating the folio from LRU, the following race could happen. Furthermore, the refcnt dropped in the readahead_folio represents page cache, it doesn't make sense to drop it twice in read_pages. 0. Thread_readahead: folio_put() folio_put_test_zero() =3D=3D true __folio_put() folio_test_lru() =3D=3D true 1. Thread_isolate folio_isolate_lru folio_test_clear_lru() lruvec_del_folio() 2. Thread_readahead folio_put() folio_put_test_zero() =3D=3D true __folio_put folio_test_lru() =3D=3D true lruvec_del_folio() > (folio has refcount 0 and is freed) > > Yes, other things can happen in there to increment the refcount, so this > folio_put() might not be the last put, but we hold the folio locked the > entire time, so many things which might be attempted will block on the > folio lock. In particular, nobody can remove it from the page cache, > so its refcount cannot reach 0 until the last folio_put() of the > sequence.