From patchwork Thu Dec 19 12:47:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Donet Tom X-Patchwork-Id: 13915047 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 E42D3E77184 for ; Thu, 19 Dec 2024 12:47:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 57E756B0082; Thu, 19 Dec 2024 07:47:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 52DA56B0083; Thu, 19 Dec 2024 07:47:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3CFE66B0085; Thu, 19 Dec 2024 07:47:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 190D76B0082 for ; Thu, 19 Dec 2024 07:47:31 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9052E1C7C12 for ; Thu, 19 Dec 2024 12:47:30 +0000 (UTC) X-FDA: 82911683988.11.ED249DC Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by imf02.hostedemail.com (Postfix) with ESMTP id 56E108000E for ; Thu, 19 Dec 2024 12:46:25 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=Keg1u6Ik; spf=pass (imf02.hostedemail.com: domain of donettom@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=donettom@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734612417; a=rsa-sha256; cv=none; b=dx59BLGGOvr6GNSe9BT1qtqJSPc9IxQWJXj6c5s7a8IV8bCmrUJoA1TzAduedn47N0LotD uOyCdNMCGCYaB7hcIZMWWJHjT6061YoUOPiGs0EW/bdk3WJdREDHo9+PjsR/MMYGYKBlKx BXbxY2+gO+6+KOrdkdhpuIgiF/8/Dsg= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=Keg1u6Ik; spf=pass (imf02.hostedemail.com: domain of donettom@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=donettom@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734612417; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=elaGijxkkLnrovQ0wbhS4K0PFXYRA3vhn0PBXG6wyw0=; b=lyZxEJOmTYZXbsZQI0/T9MzVIpcgISoEq/vEKr9savMt/aVWzIUob/miTtIfdfIZdo5T8u bL2xcsAfM5O3qa/H/g7tQH01mtZHTmk507vD0P2aIIvp5EOU7Rwm6z2Qe4Orm1bFW1eotL Xhsz9huFBiGuiDl8FXgial3XpqJ0NfE= Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4BJB0SEg014352; Thu, 19 Dec 2024 12:47:23 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:message-id:mime-version :subject:to; s=pp1; bh=elaGijxkkLnrovQ0wbhS4K0PFXYRA3vhn0PBXG6wy w0=; b=Keg1u6Ik0ndS/sTH4+ZlrFuHAVAA1aT2K7B98QKgDXsVasf5rbSV4iL91 njd4Ghzn3QlJqX1MIy3PukxLqwtcVa9IvFW/2fQD5Hx2UFUzxoCIf+NAbLdcOkKN sDC0atFHbPa1kulxPSNCPd09mJuuF3ircxXedMNSEPmH+AWioUx/DqxPkpdjrpTR WBJC6HG5/krk+7UmBeE12ts6wnYKJWeG25mDye+bpaQaf0lIKlrA7OSdt8CCJa7G wl8JjpB1AxNp2vDVjRwkrcXrw6aqsz+lgENqrjWvKudVnlAUODeadTB79IgJHrgh By+dwvZtIFeThxArJaa+uksRwWbGw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43mj808ebm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 19 Dec 2024 12:47:23 +0000 (GMT) Received: from m0356516.ppops.net (m0356516.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 4BJClMgx016110; Thu, 19 Dec 2024 12:47:22 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43mj808ebh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 19 Dec 2024 12:47:22 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 4BJBRpWS014320; Thu, 19 Dec 2024 12:47:22 GMT Received: from smtprelay04.fra02v.mail.ibm.com ([9.218.2.228]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 43hmqyd98n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 19 Dec 2024 12:47:22 +0000 Received: from smtpav01.fra02v.mail.ibm.com (smtpav01.fra02v.mail.ibm.com [10.20.54.100]) by smtprelay04.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4BJClKPC35127956 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Dec 2024 12:47:20 GMT Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 094E220043; Thu, 19 Dec 2024 12:47:20 +0000 (GMT) Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7293520040; Thu, 19 Dec 2024 12:47:18 +0000 (GMT) Received: from ltczz402-lp1.aus.stglabs.ibm.com (unknown [9.40.194.31]) by smtpav01.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 19 Dec 2024 12:47:18 +0000 (GMT) From: Donet Tom To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Ritesh Harjani , Baolin Wang , "Aneesh Kumar K . V" , Zi Yan , David Hildenbrand , shuah Khan , Dev Jain Subject: [PATCH] mm: migration :shared anonymous migration test is failing Date: Thu, 19 Dec 2024 06:47:17 -0600 Message-ID: <20241219124717.4907-1-donettom@linux.ibm.com> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: yfdMwDDzJGX9PH5RVx8P9ejigjup5UHP X-Proofpoint-ORIG-GUID: xiV9b35L8M_EvD3qix6qkJKdK61Ddlax X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 lowpriorityscore=0 priorityscore=1501 malwarescore=0 phishscore=0 mlxlogscore=999 bulkscore=0 adultscore=0 suspectscore=0 clxscore=1011 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412190100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dp+ZSK4gviDceCiVOFJLAws48eaYUpndQqYUU e/unM5M+ 0Bdc2LRanJ5mKf2GljT1GZcWtAa78FYK+8klVY2WnZnSwJ2x/hwz1dNwdEeLWktWG+gSl+b8M5P9GmiLpxltkJfR9VgzK3l59EoRb6lt+iP4dFPCM0sn6peznMDjtt2XAQC+dGXG7Fm4WoGxpfYPeOIpzp9eYB3NVokoAeKYSUFBTzQz+Dm9eTPpd2dQ0L6pEJNxfhSoKGyWAgU6RNWH1rahV+8aI7uyY4TtAq0D5FzlzswXVzveBjWhPiKBkmkpg8Sg7QeCYE/wgolA59ol98NerVg== 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: The migration selftest is currently failing for shared anonymous mappings due to a race condition. During migration, the source folio's PTE is unmapped by nuking the PTE, flushing the TLB,and then marking the page for migration (by creating the swap entries). The issue arises when, immediately after the PTE is nuked and the TLB is flushed, but before the page is marked for migration, another thread accesses the page. This triggers a page fault, and the page fault handler invokes do_pte_missing() instead of do_swap_page(), as the page is not yet marked for migration. In the fault handling path, do_pte_missing() calls __do_fault() ->shmem_fault() -> shmem_get_folio_gfp() -> filemap_get_entry(). This eventually calls folio_try_get(), incrementing the reference count of the folio undergoing migration. The thread then blocks on folio_lock(), as the migration path holds the lock. This results in the migration failing in __migrate_folio(), which expects the folio's reference count to be 2. However, the reference count is incremented by the fault handler, leading to the failure. The issue arises because, after nuking the PTE and before marking the page for migration, the page is accessed. To address this, we have updated the logic to first nuke the PTE, then mark the page for migration, and only then flush the TLB. With this patch, If the page is accessed immediately after nuking the PTE, the TLB entry is still valid, so no fault occurs. After marking the page for migration, flushing the TLB ensures that the next page fault correctly triggers do_swap_page() and waits for the migration to complete. Test Result without this patch ============================== # ./tools/testing/selftests/mm/migration TAP version 13 1..3 # Starting 3 tests from 1 test cases. # RUN migration.private_anon ... # OK migration.private_anon ok 1 migration.private_anon # RUN migration.shared_anon ... Didn't migrate 1 pages # migration.c:175:shared_anon:Expected migrate(ptr, self->n1, self->n2) (-2) == 0 (0) # shared_anon: Test terminated by assertion # FAIL migration.shared_anon not ok 2 migration.shared_anon # RUN migration.private_anon_thp ... # OK migration.private_anon_thp ok 3 migration.private_anon_thp # FAILED: 2 / 3 tests passed. # Totals: pass:2 fail:1 xfail:0 xpass:0 skip:0 error:0 Test result with this patch =========================== # ./tools/testing/selftests/mm/migration TAP version 13 1..3 # Starting 3 tests from 1 test cases. # RUN migration.private_anon ... # OK migration.private_anon ok 1 migration.private_anon # RUN migration.shared_anon ... # OK migration.shared_anon ok 2 migration.shared_anon # RUN migration.private_anon_thp ... # OK migration.private_anon_thp ok 3 migration.private_anon_thp # PASSED: 3 / 3 tests passed. # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0 Signed-off-by: Donet Tom --- mm/rmap.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index c6c4d4ea29a7..920ae46e977f 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2154,7 +2154,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, hugetlb_vma_unlock_write(vma); } /* Nuke the hugetlb page table entry */ - pteval = huge_ptep_clear_flush(vma, address, pvmw.pte); + pteval = huge_ptep_get_and_clear(mm, address, pvmw.pte); } else { flush_cache_page(vma, address, pfn); /* Nuke the page table entry. */ @@ -2171,7 +2171,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, set_tlb_ubc_flush_pending(mm, pteval, address); } else { - pteval = ptep_clear_flush(vma, address, pvmw.pte); + pteval = ptep_get_and_clear(mm, address, pvmw.pte); } } @@ -2320,6 +2320,14 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, folio_remove_rmap_pte(folio, subpage, vma); if (vma->vm_flags & VM_LOCKED) mlock_drain_local(); + + if (!should_defer_flush(mm, flags)) { + if (folio_test_hugetlb(folio)) + flush_hugetlb_page(vma, address); + else + flush_tlb_page(vma, address); + } + folio_put(folio); }