From nobody Tue Dec 16 07:19:44 2025 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 4AB03156F3B for ; Wed, 15 Jan 2025 03:38:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736912315; cv=none; b=Uxvem5jXeohl7WcakpHPzRtQMh8gCPZkW4UGsnWqR2lkWRCCTXMo0+xju7iXtoBNGAioG3xaoQ1QBvu7aUTxsNChben+P3sHQxHNkG53Go3reHl+s+dqPXrPErQ9oF/C+i7WFKw4+S4jYOiuh/gD1v6izkngnUh4S8VJcZ/+Fu0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736912315; c=relaxed/simple; bh=DFTj4se5xxWk8XKqtFqUSTomY2Qcp3pLsECTVxgvQ/g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tV8o6iCXF29WCcpYxtaDIAAaBXJvenB7u2Xp/Uvap+eEMJi6gRIvNt7KgHAWfHRJpk++Ws5nZ/DlNABoQw1IcmMtoUMEqdc3peheV0r67BAakeHZupj2FQJ+bLVkXbfibaBzj+lrv9kdtwX5QIPmVJ6UABp8fwMZ4Wa3MYXy93E= 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=Se/T2hB8; arc=none smtp.client-ip=209.85.214.177 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Se/T2hB8" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-21649a7bcdcso106811755ad.1 for ; Tue, 14 Jan 2025 19:38:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736912313; x=1737517113; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KJIQvsAs93F2Rgx1xFn4Hzm1URQ2cAp3NCo21K6rE7w=; b=Se/T2hB8crAwnZ07QT8WryyVurRogGYmvqC/c5nNrpxp4ZsFaZECwINjRm0+3abQVW Z4r4khObFQ1IAIej3Y80JUMob8q4x7QIPRe4fvuJ9fknTXZxGjlzSpsprbuyFMdrjjXZ uPFyqn+nxftp9toz1LlEjBYUsbyE17NBjYa1WV4APNF9WOa8sMAsRlkQCOzdaUFaletd ttS8ojHrrPYRKic130jnnC7VR1i4p8cyLnJUhoBuytDcRfbLNge3+raxs5OS56ZWfkkr 89otMHBAzMcrwpIOnTkTF5dNUm3++6TK0eaPp2AUKnelk+32YvXtEuevNgd87zdZQZeK 00wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736912313; x=1737517113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KJIQvsAs93F2Rgx1xFn4Hzm1URQ2cAp3NCo21K6rE7w=; b=O/2o9l4QIhIysfH7XP0CijfL5kTTALNo7HeKv1XWT6/3DWy2MkCa1IHGdMgyVuTdRE qHqgX/+dukxQ6FMGrUPhOoKqILYSND/zbArKyGBO+klRZ/j93j85imxCZ89e9OkdevFX eFE9Ha4OWlzzG4ZB66FHU+Pmm4GNmDsqsOFXT6lS5x1Jx1yL5xLk1S0kknc9Rmh/kEX2 hiqqgQK4WiKhCxbT/ctZ6+oYtF2V9I4rcuZxpvqIc8UaRlmV59AYZr7iibgRzyaMLEwo ydEMUyGPxMmiB1vYAAANgyWvakgLHQ8QhpfjZ0qA4noNRb7LwjZZnYeNd87Ib8YqgMaV 9HJw== X-Forwarded-Encrypted: i=1; AJvYcCUz3C3C0UboeNBZcnJxydNKUxjtgI6L25p90tO/C4qq/9woYECHrCwyY91R+RoCt8TqLbIvw0m/SWU2X6I=@vger.kernel.org X-Gm-Message-State: AOJu0YxDFHHygiFYYWWMsU3EKtCUrbAyRlunrENBl9fJ5ztRT6s9vK62 NRemePisx7fTJf3MVjKQrd8+ygFetX8ihI2Q6az5on55hLmvu3bs X-Gm-Gg: ASbGncvFK1QH0jrKSSPZh+maIoc/G0VDUDAQMAr2SkwjcNy26/2fVGpSKUHagF5LE2w GJ0U2XxDfj0qvzumSThHe2EixYa8Zn+Uo8RrICNIacG6rF1w7ebbUMYmbqXc9EnerRh/akYPRCa WJ58V19QEXQJgmLoyY3CicaZwMU8rmh3cgRBzBZNsGT/1Pdlr2dxlKjwQY1m/uES1yDAkhwR1hn hPChrlHVViqgpoEsIP2gFCep+xfWjfd1FY3UXbs7crFwm8sJn4S7yul3b4MexJaXM2XPpw6Ll1R BSvYkYoM X-Google-Smtp-Source: AGHT+IHnFl4K6iDBiLirFwWAnLa88ULHZZM7psfa+Le3bhG1J3ARcy1prtquvYLzB7/JWDl2R1Bcjw== X-Received: by 2002:a17:903:1cf:b0:216:39fa:5cb4 with SMTP id d9443c01a7336-21a83f67c50mr435295355ad.25.1736912313634; Tue, 14 Jan 2025 19:38:33 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:af65:8200:e5d5:b870:ca9b:78f8]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f10dffbsm73368195ad.49.2025.01.14.19.38.26 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 14 Jan 2025 19:38:32 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 21cnbao@gmail.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, ioworker0@gmail.com, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, lorenzo.stoakes@oracle.com, ryan.roberts@arm.com, v-songbaohua@oppo.com, x86@kernel.org, ying.huang@intel.com, zhengtangquan@oppo.com, Mauricio Faria de Oliveira Subject: [PATCH v3 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one Date: Wed, 15 Jan 2025 16:38:05 +1300 Message-Id: <20250115033808.40641-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20250115033808.40641-1-21cnbao@gmail.com> References: <20250115033808.40641-1-21cnbao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Barry Song The refcount may be temporarily or long-term increased, but this does not change the fundamental nature of the folio already being lazy- freed. Therefore, we only reset 'swapbacked' when we are certain the folio is dirty and not droppable. Fixes: 6c8e2a256915 ("mm: fix race between MADV_FREE reclaim and blkdev dir= ect IO read") Suggested-by: David Hildenbrand Signed-off-by: Barry Song Cc: Mauricio Faria de Oliveira Acked-by: David Hildenbrand Reviewed-by: Baolin Wang Reviewed-by: Lance Yang --- mm/rmap.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index c6c4d4ea29a7..de6b8c34e98c 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1868,34 +1868,29 @@ static bool try_to_unmap_one(struct folio *folio, s= truct vm_area_struct *vma, */ smp_rmb(); =20 - /* - * The only page refs must be one from isolation - * plus the rmap(s) (dropped by discard:). - */ - if (ref_count =3D=3D 1 + map_count && - (!folio_test_dirty(folio) || - /* - * Unlike MADV_FREE mappings, VM_DROPPABLE - * ones can be dropped even if they've - * been dirtied. - */ - (vma->vm_flags & VM_DROPPABLE))) { - dec_mm_counter(mm, MM_ANONPAGES); - goto discard; - } - - /* - * If the folio was redirtied, it cannot be - * discarded. Remap the page to page table. - */ - set_pte_at(mm, address, pvmw.pte, pteval); - /* - * Unlike MADV_FREE mappings, VM_DROPPABLE ones - * never get swap backed on failure to drop. - */ - if (!(vma->vm_flags & VM_DROPPABLE)) + if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) { + /* + * redirtied either using the page table or a previously + * obtained GUP reference. + */ + set_pte_at(mm, address, pvmw.pte, pteval); folio_set_swapbacked(folio); - goto walk_abort; + goto walk_abort; + } else if (ref_count !=3D 1 + map_count) { + /* + * Additional reference. Could be a GUP reference or any + * speculative reference. GUP users must mark the folio + * dirty if there was a modification. This folio cannot be + * reclaimed right now either way, so act just like nothing + * happened. + * We'll come back here later and detect if the folio was + * dirtied when the additional reference is gone. + */ + set_pte_at(mm, address, pvmw.pte, pteval); + goto walk_abort; + } + dec_mm_counter(mm, MM_ANONPAGES); + goto discard; } =20 if (swap_duplicate(entry) < 0) { --=20 2.39.3 (Apple Git-146)