From patchwork Tue Aug 8 02:09:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13345580 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 449D8EB64DD for ; Tue, 8 Aug 2023 02:10:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C73706B0074; Mon, 7 Aug 2023 22:10:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BFBFE6B0075; Mon, 7 Aug 2023 22:10:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A9D148D0001; Mon, 7 Aug 2023 22:10:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 962FD6B0074 for ; Mon, 7 Aug 2023 22:10:56 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6AF3C140691 for ; Tue, 8 Aug 2023 02:10:56 +0000 (UTC) X-FDA: 81099309312.26.C9FEE98 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) by imf02.hostedemail.com (Postfix) with ESMTP id 63D858000D for ; Tue, 8 Aug 2023 02:10:54 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=j7D3fIfL; spf=pass (imf02.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691460654; a=rsa-sha256; cv=none; b=AOcZdRzV9Iwp97UPCA7tNuN9+xGuwR6HoX1axUvCMHGh2Zc+1THioJO33Av1DdkVxiFWZH GaSKo5sX3VSCha2PEjb668m9Q7ariT36SukG8G0BgKaaHGIi7lHNBHTY7YhWOTtJt+temk DNjpFcXmEokrrSCGtKaRWrApfF2cW4A= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=j7D3fIfL; spf=pass (imf02.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691460654; 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:in-reply-to:references:references:dkim-signature; bh=Ljmm8K/QkDciIB4DKIAS5aVl8fxci5EsuS9jbdr0jIg=; b=m8W6+W6uqryxd1pDsTdQgWnPnoxX5qjiEMJR+llyL6P9hgDBk5Mc97nrUyRfbaHMExuSsb QI/ClchmWNruw31FuqUch5jEY2x2r6DBcIpKprENSNCPGRjC5noQdeFah9fh4bmCp4jUgK 4rgTwg5nABVXvozcMaaSFuDta8Yr9XM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1691460654; x=1722996654; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=I0sX9zQ55/pnapaBgwBXw+8hFPy7SpHzYq+j/u0jVVw=; b=j7D3fIfLJOitaaySFBMRWnEzOC2jFt197MFAMWhT8ofPEvQmwN71wzr+ HyWL92ZR0jpWx0ZbtrS9KHA0dWSXGAZva+BrStpDU7BRlWmQPgYUIfrHc 2NNtDACFoUsm1gUsZAZpmd7OIFobk9RNG01L/eHfvuoe0u6O6pfRs5mOl Ved95wsoXkB0Woum02O9OD1R1Kzb+iPcf4rufpt/ZJF0LQ1+PTgBmr8dL sIFbxUi9goIbW2/i+QYQLWW3SeCoX2DHlkGa4wceEFimn9sEHw9jp0q00 R7GE6SK1UjHAgtARqomhoKLeqinT80uZdaRWjZLctp4urqly7egymujr1 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="373453503" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="373453503" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2023 19:10:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="977689341" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="977689341" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga006.fm.intel.com with ESMTP; 07 Aug 2023 19:10:49 -0700 From: Yin Fengwei To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, akpm@linux-foundation.org, willy@infradead.org, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, minchan@kernel.org, yuzhao@google.com, david@redhat.com, ryan.roberts@arm.com, shy828301@gmail.com Cc: fengwei.yin@intel.com Subject: [PATCH v2 1/3] madvise:madvise_cold_or_pageout_pte_range(): don't use mapcount() against large folio for sharing check Date: Tue, 8 Aug 2023 10:09:15 +0800 Message-Id: <20230808020917.2230692-2-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230808020917.2230692-1-fengwei.yin@intel.com> References: <20230808020917.2230692-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 63D858000D X-Stat-Signature: hnog7aaxaz8np3kuxf8wxkckzco8ahha X-Rspam-User: X-HE-Tag: 1691460654-430024 X-HE-Meta: U2FsdGVkX18RTvDrd1pVhf4Z/cA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zDsAtwJ/rKGBLZJ2BFMd3el7YjpqjtWfggwhS/hOz/ugTVvlbQA5fzpJ1VNL1cHmW6SalCfHLAVlsqMeRyRuO1Xfp5u1zGSiYjjc/hjt0wDyjz4JD0H72Dw9PF+BXw4k+dG0MnxTdznKAKHbB+1Mx70sp1B68fMSHrUe3E/C7GHg0mOIgJ5+1Ff781/VKSfSJhYpDRylOZhCf4evj2gMIYjQ9OaUjVJ9FnRKoh2BbUzQDrPfcdidrBReRGmTCUINOJUeQlnkn+l1MfdyBp2m2UZfZRI2nwxUnYaObRW2bKgBfGTIYVT7v 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: Commit 07e8c82b5eff ("madvise: convert madvise_cold_or_pageout_pte_range() to use folios") replaced the page_mapcount() with folio_mapcount() to check whether the folio is shared by other mapping. It's not correct for large folio. folio_mapcount() returns the total mapcount of large folio which is not suitable to detect whether the folio is shared. Use folio_estimated_sharers() which returns a estimated number of shares. That means it's not 100% correct. It should be OK for madvise case here. User-visible effects is that the THP is skipped when user call madvise. But the correct behavior is THP should be split and processed then. NOTE: this change is a temporary fix to reduce the user-visible effects before the long term fix from David is ready. Fixes: 07e8c82b5eff ("madvise: convert madvise_cold_or_pageout_pte_range() to use folios") Cc: stable@vger.kernel.org Signed-off-by: Yin Fengwei Reviewed-by: Yu Zhao Reviewed-by: Ryan Roberts --- mm/madvise.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 1d7933933e31..49af35e2d99a 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -383,7 +383,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, folio = pfn_folio(pmd_pfn(orig_pmd)); /* Do not interfere with other mappings of this folio */ - if (folio_mapcount(folio) != 1) + if (folio_estimated_sharers(folio) != 1) goto huge_unlock; if (pageout_anon_only_filter && !folio_test_anon(folio)) @@ -459,7 +459,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, if (folio_test_large(folio)) { int err; - if (folio_mapcount(folio) != 1) + if (folio_estimated_sharers(folio) != 1) break; if (pageout_anon_only_filter && !folio_test_anon(folio)) break; From patchwork Tue Aug 8 02:09:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13345581 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 260FAC001B0 for ; Tue, 8 Aug 2023 02:11:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DB416B0075; Mon, 7 Aug 2023 22:11:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 964256B0078; Mon, 7 Aug 2023 22:11:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 804C68D0001; Mon, 7 Aug 2023 22:11:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6DFCA6B0075 for ; Mon, 7 Aug 2023 22:11:11 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4AD93B20E4 for ; Tue, 8 Aug 2023 02:11:11 +0000 (UTC) X-FDA: 81099309942.06.77F64BF Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by imf17.hostedemail.com (Postfix) with ESMTP id 245B34000A for ; Tue, 8 Aug 2023 02:11:08 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gbjbalHj; spf=pass (imf17.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691460669; 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:in-reply-to:references:references:dkim-signature; bh=o6jkD1q2jWi2X8kGNkhNdNjBTl2DhE6CD0Wi6fdQEcw=; b=dAy+JZVGQiUDqKphJZbKTUix8eTlCOEmzmsI88Q3vdqHc62B94tBGNjtB1VXWze1EnWNm3 9mGYykMVg9GRQPptdjZLj3LdSPqfOVIOdh85awdbOIZk6bIY68qCPYR39S4JOvhkJLQmWI iqQmOi35VUxL3ubCdmPz7IWkj2moRJs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691460669; a=rsa-sha256; cv=none; b=gFj/hG91jkA8qKujga1exJu1GXl3hbGipwVYUe7e/SexeAaBurvUjspoCTp43YZzLpEsrV +j/V6B8tfRPC3wop2WzV9O6OySI1g5Qc6wbK81EZ7si3fAtwJSb28qjzEDkIzFLg4F32i7 alhe2jF+h9u+kmY/4yCFVfme6QbK9gc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gbjbalHj; spf=pass (imf17.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1691460669; x=1722996669; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=F3oORw0alZIlqdHjL/85Fxei/Bpe8d1CgQMNfWf9J+M=; b=gbjbalHj/loBHFKeNOf3jKfkVm1836tdIpJa1oEUqOAZ57va2RR225WJ JMINJ4sSyUpp/fRAweqbc04102uTwUM1HAPYeSM8/5VrUzfAq5288081J 3RW50E1ToQsyplup929EQeojDhfu2L3bL+Ix9/81uyf+ctHY3s4O9ws9e V7b7Z/MiKaOjb31FUAenGVk0asPcqOgGkVUuMTEXM1pX+M9VXTreCoFS2 Q8MLcmUrvViiqUCzjj/dIudw2CDXGVutfVYFU/AtufIQQab4ONc3C9ECJ gE6PxHjun7E2nIcDT3vvcxLPdAKBN2hj1hz8jlTLJU+GAcFhx+EAjIY+j g==; X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="374371060" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="374371060" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2023 19:11:07 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="681051006" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="681051006" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by orsmga003.jf.intel.com with ESMTP; 07 Aug 2023 19:11:03 -0700 From: Yin Fengwei To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, akpm@linux-foundation.org, willy@infradead.org, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, minchan@kernel.org, yuzhao@google.com, david@redhat.com, ryan.roberts@arm.com, shy828301@gmail.com Cc: fengwei.yin@intel.com Subject: [PATCH v2 2/3] madvise:madvise_free_huge_pmd(): don't use mapcount() against large folio for sharing check Date: Tue, 8 Aug 2023 10:09:16 +0800 Message-Id: <20230808020917.2230692-3-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230808020917.2230692-1-fengwei.yin@intel.com> References: <20230808020917.2230692-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Stat-Signature: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O9wi PRHuoTFH 6fZckQrgGKNDN5MKIy6ug9Lq2zeYJHeMZNkwi9+yikDsgYXeACu0lfBDtEfiJ5zx8WHpSm7sfX4jybFOYMdENwg9lkfu7DESL13/C5Owrs5Nk/nPXNi8jSekDW4fr7ei7yAEae2efJJq8ZibGF4JqCkFSVqwokwwqxOHIhkUTkQI0wPhtbNyGgvt4MgM1zKq/I9ve8d5MKfmjIG8NGTRczxDtXUU4n0x382eXf5SaIwFRSfggs5Khuito5vMPcJXh868Z9T8szdQNZg2VdEJp5MVKfKM0GkVrG/qSS912PQBSgKCSZz5XazqxwPVSN4dk0NUle8tLDR9lSZdtUM/CeTXSueJFOOBrOzfs77KMNvSp75y9aZpjsFgWoOY0FAR14erjTc71z/I9rMc5gwfyGmVz+7Ge5/VzJY8Y 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: Commit fc986a38b670 ("mm: huge_memory: convert madvise_free_huge_pmd to use a folio") replaced the page_mapcount() with folio_mapcount() to check whether the folio is shared by other mapping. It's not correct for large folios. folio_mapcount() returns the total mapcount of large folio which is not suitable to detect whether the folio is shared. Use folio_estimated_sharers() which returns a estimated number of shares. That means it's not 100% correct. It should be OK for madvise case here. User-visible effects is that the THP is skipped when user call madvise. But the correct behavior is THP should be split and processed then. NOTE: this change is a temporary fix to reduce the user-visible effects before the long term fix from David is ready. Fixes: fc986a38b670 ("mm: huge_memory: convert madvise_free_huge_pmd to use a folio") Cc: stable@vger.kernel.org Signed-off-by: Yin Fengwei Reviewed-by: Yu Zhao Reviewed-by: Ryan Roberts --- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 154c210892a1..0b709d2c46c6 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1612,7 +1612,7 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, * If other processes are mapping this folio, we couldn't discard * the folio unless they all do MADV_FREE so let's skip the folio. */ - if (folio_mapcount(folio) != 1) + if (folio_estimated_sharers(folio) != 1) goto out; if (!folio_trylock(folio)) From patchwork Tue Aug 8 02:09:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13345582 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 84847EB64DD for ; Tue, 8 Aug 2023 02:11:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EB506B0078; Mon, 7 Aug 2023 22:11:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 175286B007B; Mon, 7 Aug 2023 22:11:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 015F98D0001; Mon, 7 Aug 2023 22:11:24 -0400 (EDT) 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 DA63A6B0078 for ; Mon, 7 Aug 2023 22:11:24 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A6DBEA0609 for ; Tue, 8 Aug 2023 02:11:24 +0000 (UTC) X-FDA: 81099310488.20.1ACE43F Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) by imf13.hostedemail.com (Postfix) with ESMTP id 8B93B2000B for ; Tue, 8 Aug 2023 02:11:22 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EuJd4qa0; spf=pass (imf13.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691460682; 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:in-reply-to:references:references:dkim-signature; bh=SnBkVWTLCU6LpGHkVu+arSBfRN4pTVOEAmzdbz8UspQ=; b=E8BtDC3D/MDBkSsoHENPcx1orVnSnsyYVnk+pTKl6a6D5nOqFZgcIFewYoOzSYg4BG0aRD MFE8QiTJ+nZ0Jiwb80c3EXtXLzoMYfXXHdP/lyHIrTIlJ/gBy172nGWQAbUsBcfrnb2LqN kmV6Nthr0wqutDxnIa3BOPyrStB1M2M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691460682; a=rsa-sha256; cv=none; b=H7K5jYpqhp75kP/zDClCqg9TvuYHdrUd/Uxn/3aSPJnGocTCSaoXxq9m6rWC6pNvm9E0sf 9t7qjrS0586LwiFkard7sEUsPkO6FBzRoyZsmFSXAY9v5bBL0iTnRTbKswjA8JugUza6kQ 353VMkXtK6pSzjms0D9dxfYfKJW2ATE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EuJd4qa0; spf=pass (imf13.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1691460682; x=1722996682; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=d3VIX2xY+fu1wWsahOfoN14pobtO1cM2MgEWqpBvgLY=; b=EuJd4qa0AAArhVMsFAlVuljUHzrNuN+NCuFOvd/FratfgL2JETvU6eCh qg6KhSCb58AqZmPXhmlnGTLUklrJP7iYSWSXQYEQRj7TcTSSk+e0wJc+m JURZuL/L5ni+vhYi93jQ0OaAWfp8hWPV15Xe0MPextHEwx2/gBdsKQHPx 12/mcxBQm0bm7Olx+egeufVxqoqMDaxF/ysXGR2cqJc/UB5n5PAPL8Hvp iQzc2W1QdyXnuBwG/qtj/XSzk3PlTQcOZVp0R3aZ0IrVRgu1tzPhbFvzG dsZRTrN9URLfshzRnuKIzObcL+fm3PdBJpkYAg1HUJIQWN7+NgtF1ONx1 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="373453599" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="373453599" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2023 19:11:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="977689411" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="977689411" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga006.fm.intel.com with ESMTP; 07 Aug 2023 19:11:18 -0700 From: Yin Fengwei To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, akpm@linux-foundation.org, willy@infradead.org, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, minchan@kernel.org, yuzhao@google.com, david@redhat.com, ryan.roberts@arm.com, shy828301@gmail.com Cc: fengwei.yin@intel.com Subject: [PATCH v2 3/3] madvise:madvise_free_pte_range(): don't use mapcount() against large folio for sharing check Date: Tue, 8 Aug 2023 10:09:17 +0800 Message-Id: <20230808020917.2230692-4-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230808020917.2230692-1-fengwei.yin@intel.com> References: <20230808020917.2230692-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-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dPwt29 St1Aht96 4YHyT4gbS73JqMBexrhCIARplWX5pXEwW+zzUt7gMpyHHxAGK41/ju+/beVfnOehsjLrBLvv4h96l1cuHd7u6CRKIK9Grq5mQT6iherLPgJ4Miqf7JqL6jBVaqxtmCgIznw9jSAW+k3QwnLYNxnEVh7YI1E5qpBPaYHFaUC5SwAORC45lNgisZmVHPS0Tm8+FsqwBGuUuOzYmdFB4G8DrbGlhratpL0JzBgsV0lv0zYoQxGaXYxQuqRYGjGSE2y4DLaZ/Gr23H1DHaGR5J0iWG+0auj0VqiOmYvicc+M50LIDEjtOrpB6flG+5vmKCC3LxRwzFV3GrDfCojgpyHjkmGLMrYRXPx1eLDpkJpZDs+dfNDYivOYfscZPMnnBNTt0j/252FfhtyT25Bp7Oz1s7cFAi0w2g9pQvO/u 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: Commit 98b211d6415f ("madvise: convert madvise_free_pte_range() to use a folio") replaced the page_mapcount() with folio_mapcount() to check whether the folio is shared by other mapping. It's not correct for large folios. folio_mapcount() returns the total mapcount of large folio which is not suitable to detect whether the folio is shared. Use folio_estimated_sharers() which returns a estimated number of shares. That means it's not 100% correct. It should be OK for madvise case here. User-visible effects is that the THP is skipped when user call madvise. But the correct behavior is THP should be split and processed then. NOTE: this change is a temporary fix to reduce the user-visible effects before the long term fix from David is ready. Fixes: 98b211d6415f ("madvise: convert madvise_free_pte_range() to use a folio") Cc: stable@vger.kernel.org Signed-off-by: Yin Fengwei Reviewed-by: Yu Zhao Reviewed-by: Ryan Roberts --- mm/madvise.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/madvise.c b/mm/madvise.c index 49af35e2d99a..4dded5d27e7e 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -683,7 +683,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, if (folio_test_large(folio)) { int err; - if (folio_mapcount(folio) != 1) + if (folio_estimated_sharers(folio) != 1) break; if (!folio_trylock(folio)) break;