From patchwork Fri May 31 08:12:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13681304 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 6F42AC27C4F for ; Fri, 31 May 2024 08:13:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D24436B0089; Fri, 31 May 2024 04:13:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD2716B008C; Fri, 31 May 2024 04:13:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B254F6B0092; Fri, 31 May 2024 04:13:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8D6D36B0089 for ; Fri, 31 May 2024 04:13:01 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3075E1A1241 for ; Fri, 31 May 2024 08:13:01 +0000 (UTC) X-FDA: 82177975362.29.39C4253 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by imf18.hostedemail.com (Postfix) with ESMTP id 0C1FB1C001D for ; Fri, 31 May 2024 08:12:58 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=YqzOHD4S; spf=pass (imf18.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=ying.huang@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=1717143179; 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=ccYnb6zhiB/aH/usWIdTsL879a4h4ytGkNRSAUGO+zk=; b=Tjhb+RQlTpT8M74lA/uZKNwofFqQVxQ/yopqOXAibXTWkc7ZFI0AMQvRMuqm+eF2yMiy0S DotTJuSFaJWyRdOIykPVS4jQjufFW+VR+61AAPX9sH1wjwA8c+fNyZadDEW5MtHWU1iihx 0xs0dG3ED7Vk7s0DiRcRs7Ew2cNVg8c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717143179; a=rsa-sha256; cv=none; b=H++zJFyFYY+fq4Rlosq1N8gNLWdi3VKF+O0BNJml5LzULCHiL1qKpMeq4Fa6Y5WWXNngo8 niiQ+ZdobRzf7GOLSXigBj/w/6lCLt3Gdf4eeTOw0Uf0c6ckF8uBySBPDzLJFdkgk1mNPg ldQAGDFdteAvixehyHn8j602xnO9l3o= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=YqzOHD4S; spf=pass (imf18.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=ying.huang@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=1717143179; x=1748679179; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ikiPA1J5OkAmYTlYwym2P+F42MJMV8ZnlTu7TRlEtOg=; b=YqzOHD4S8jOZtNZE5eZuwDL1BdYwFYnekC0/vCh8Vr/kppIWDELLmIWO 2LZxKLjJFbueil/r0XZULahdomkqk0PpOGRHn6BcgnZDJZKr2yKmCXZxu 9vR27ZCKqkQrWEXwpeeg7zE9MbBvC58weXnC4eIHTmenqBn1PFl8AgMVs Ojt3Ubwg2uDq3sR/Qa5E/i+5OBTBYfk8RoUA5ppnp2cvwdaVDIHPSHXpZ W2bdt6kby4FVUCP1VeK/ht32KhrUtOmqCDrgToFhzugUpdglXpqdYNnEo AXFSBptwoDAaz8fjsV46XeP2MwN2OaBgsOWA2Q6C2PwPCNpUUcJenTc8L A==; X-CSE-ConnectionGUID: XNcGHCamR5mZ2UUN4YEvqg== X-CSE-MsgGUID: GMhM57rvQlKErJNCzWGvwA== X-IronPort-AV: E=McAfee;i="6600,9927,11088"; a="25079747" X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="25079747" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 01:12:58 -0700 X-CSE-ConnectionGUID: paWjvUcmRbmdg62OQ/tn+g== X-CSE-MsgGUID: RxkDEidERZauFldGWycaVw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="59270933" Received: from unknown (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.30.35]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 01:12:54 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Hugh Dickins , Alistair Popple , Anshuman Khandual , David Hildenbrand , Mel Gorman , Miaohe Lin , Minchan Kim , Ryan Roberts , Yang Shi , Yu Zhao , Kairui Song , Barry Song , Chris Li , Yosry Ahmed Subject: [PATCH 1/3] mm,swap: fix a theoretical underflow in readahead window calculation Date: Fri, 31 May 2024 16:12:28 +0800 Message-Id: <20240531081230.310128-2-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240531081230.310128-1-ying.huang@intel.com> References: <20240531081230.310128-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: cqhjusg4cjsoa9tmfmnmmbxdckb4s85m X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0C1FB1C001D X-HE-Tag: 1717143178-455785 X-HE-Meta: U2FsdGVkX1/cxCqsYPmeXvAxIKUzmPjOr7JaWCew6VXAjq42J22Cc9iXDpMtccB/6hlb+WHy1mwb5Uj/n3vkiJkI7cQ3s71j7cyek6PTUxfyyTleRPpEH1Mrnz0PbMiap3+vA9vwbPQ2QV8SHOtcLg/qs1JnBhYEYM/p6Zx3KI0zY/9zxUGuN69FRN4UKKMxgDaleNcFS0eAhKj00ZxIlfa0x+B/e+VVEJO1OSjXFq3D5dmuJyPdKu2LTkAplDqjq7BQm8AhA5ieZm7psDCdVuUkkJYnj5jvXIUsAeblWtsHM5nek4p536aBJZa2HubiriehLwBm+h+3hZj5aLk++Uik3uF/LwI2Ed6d+u++jGdLYnjkE1cmWzabQ6/ZZuZnWaFDP6MIB1PYgE37M2UjHvNfLipxexqRksNVHgKgXwq8/S696500zKYDYJTT67/kK/Rpsseyeo9JjeW932+XPuzhBkqDTYbs7j7vLr/SWhHXs4uje3m32G8KipfXVc70CFM4BjVzAaP9R0aQuUYVZC2BQGTceihofUdkZAayKj0y51VdarwE70OLXLRQOPfqF5JmmcvVLHO87TVzj46kP3wtmjVXxY4N9hWC5Udf7mCHaxVsIrl0Czx4Mf1YhW/9+Cs5cMlg9BMOW9WNlgtFG3KmRLCVO/G6IyLmmgxJostpFGwALX1oSn0dlZ7c3qLMmhenCYHgrBq2YOEN2v/uuD3099BPTTZPemjXc2oiYyhybHymll1GjJzmHyYQ9SMtC4LTU3J5COCicXnXJzWzF/y8jgKFiM4QyvlgM8D34PV4mU7Zf0V8Ca6PmvLHDJtVWQbOv/ktT6/SmXPUnVm0acOwhwosfoiwbIrU/C04Mz7wL/6eZ7BBes7v9TPv2jdBiL6stzHqdYLiPVvbCl0Tp/jyxgUplmfjfIk6LEyOZFixrs/tQAcbEjoDPGTKUY9SF/XPjq0vI/laRVXkrip SLcUKW71 zy/Jlk1sczQg30SV+7Gjv/BEtGfWe36dhtJhC/G56JNbillHicf6qL/z2crOVM0HVdEvB32vXf8VB7bxnyKU47ZPq+ybANHfAsGmpFCWg8bzjh0Mq9cjCZJk6n7K4E2LFksm3x1mxObZVTjEHXDe9y2pYzDrENw96uoqVJMTNyN69rakAyQoNCa/xV/7+8BYazpsUOv8EdqOYZ5gWdaKgdn2E0oxs8eTQC3D3MZS/tT5EMA7/uUiLMItibegVC+ze1HLqWb11/MKdnW+5oHgWCfXE9rNK16SXOdYVWaLb9K+rIi5HqFg9iOouhmS4jTECSXh27TfGqiOAHuB18LScr7DBnCzwFdWBKB/RKt17YClxADZxosn0xAcMCk7My1CxRhoraEFraQhuaUpxX9QAArRABGX4wLqC8/NhA4Xj7emFVR7VO7suB7iwFsVSTVJxN3xYTNu+VeZPNhUK0Ma13SF3qdyMseSY+k8hrEJehzJg5PyJ7zsxO9ed/xGsqFk2WHpoYFYu0N78OAX3V70t2viXnZS9eVKVl/iZPzzSUklalK/uDt3fR/edWNTGX9r+RCeY 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: In swap readahead window calculation, if the fault PFN is smaller than the readahead window size, underflow may occurs. This is only possible in theory, because the start of the virtual address space will not be used for anonymous pages in practice. Even if underflow occurs, there will be no functional bugs. In the worst cases, some swap entries may be swapped in incorrectly and some pages may be allocate on the wrong nodes. Anyway, we still needs to fix the issue via some underflow checking. Fixes: ec560175c0b6 ("mm, swap: VMA based swap readahead") Signed-off-by: "Huang, Ying" Cc: Hugh Dickins Cc: Alistair Popple Cc: Anshuman Khandual Cc: David Hildenbrand Cc: Mel Gorman Cc: Miaohe Lin Cc: Minchan Kim Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao Cc: Kairui Song Cc: Barry Song Cc: Chris Li Cc: Yosry Ahmed --- mm/swap_state.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/swap_state.c b/mm/swap_state.c index 642c30d8376c..848c167df530 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -787,6 +787,8 @@ static void swap_ra_info(struct vm_fault *vmf, lpfn = fpfn - left; rpfn = fpfn + win - left; } + if ((long)lpfn < 0) + lpfn = 0; start = max3(lpfn, PFN_DOWN(vma->vm_start), PFN_DOWN(faddr & PMD_MASK)); end = min3(rpfn, PFN_DOWN(vma->vm_end), From patchwork Fri May 31 08:12:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13681305 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 3C3FCC27C44 for ; Fri, 31 May 2024 08:13:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B4E606B0092; Fri, 31 May 2024 04:13:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFD8C6B0093; Fri, 31 May 2024 04:13:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 977B16B0095; Fri, 31 May 2024 04:13:06 -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 76E3D6B0092 for ; Fri, 31 May 2024 04:13:06 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 312241C064D for ; Fri, 31 May 2024 08:13:06 +0000 (UTC) X-FDA: 82177975572.06.89923A8 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by imf18.hostedemail.com (Postfix) with ESMTP id 0B6B11C0002 for ; Fri, 31 May 2024 08:13:03 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=fJMqoplf; spf=pass (imf18.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=ying.huang@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=1717143184; 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=wM3YFGw8/QOMhZCJh1DgDX16jiZffFXk1nhrBF8PuCA=; b=OzlBYgW2rVhlSeATeOeG3RQoiGf7DXwIyDxvapPDpXcka3ZDgPl+8IJiQ/66q+Uk4Gx85D OtYGQv4Pj7YEDI4JzoYiSVCGpx7lP30upXqvyTJyZxmWJyeTx0pzP1XtzNG6ZLV5Q+1IKY VC8fckWaoKFtI1VJAxmJmUvfdOUR39U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717143184; a=rsa-sha256; cv=none; b=Y5x2+hiGIZy2gcbr8GXckZIGGd2TurH8JuSuRWNLVfPVOYj/uY/odBzFhSx9c40rJvIRz6 FwmX31iSrXcfWXyzmhRqQ7aBAA7XjACsXVIAFv3UhffUlA2WWQloDqH7u+VRzjUpJjZRaV dfXYED9GItwAj6xn63xtEsULUFosI58= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=fJMqoplf; spf=pass (imf18.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=ying.huang@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=1717143184; x=1748679184; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DJJ27LGVAKyFkd3DhLbHS54xSb3Z7Ev5IjhsRkqv+7A=; b=fJMqoplfzShj4bhcxBehWuDO8KnCqnXDPK1se6e354obd2sMyeCSy+yK JvZ1Sn1yqQXHAE+Ej9T0YuF9JhQMGfu7YLGpaGU7HtLyTrmL6YugN3I3T O82Bz+eELkmiHejpDSNJJv2j1u7X29sZ04vjrx/usvEhSfksh1wpwQSYC 8IgBdBctcL4+hJ3Qu5E7i4PY1JD4OPyBf1+CPA9mvoisdjC07RAiJ8tQZ mD5q1PBQJSXCKk5w6X9h0e6e7hjTpGbqwZ0XSQ21iGszR0+c+LzL97P7h nK5GmeOxHW2vxOFVzDSa6+Zzy0nrtJkVSEtFd0h68EQmKhqwx4jzkINGi A==; X-CSE-ConnectionGUID: H8goqFCYTY6dAB01RRecDw== X-CSE-MsgGUID: UbHwUsGCSLi/0WFzy55z9A== X-IronPort-AV: E=McAfee;i="6600,9927,11088"; a="25079763" X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="25079763" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 01:13:03 -0700 X-CSE-ConnectionGUID: Aoj6Nli8Q8y4dEyZsXEN8g== X-CSE-MsgGUID: OoElfOprR362hwmdpIgRUw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="59270947" Received: from unknown (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.30.35]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 01:12:59 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Hugh Dickins , Alistair Popple , Anshuman Khandual , David Hildenbrand , Mel Gorman , Miaohe Lin , Minchan Kim , Ryan Roberts , Yang Shi , Yu Zhao , Kairui Song , Barry Song , Chris Li , Yosry Ahmed Subject: [PATCH 2/3] mm,swap: remove struct vma_swap_readahead Date: Fri, 31 May 2024 16:12:29 +0800 Message-Id: <20240531081230.310128-3-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240531081230.310128-1-ying.huang@intel.com> References: <20240531081230.310128-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: j1pojsbdxh3m8j8h9o7hyo1jprk887wo X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0B6B11C0002 X-HE-Tag: 1717143183-433918 X-HE-Meta: U2FsdGVkX18UCKhfK/vwJFx9jjXM88Z6pdWpAlheIkmWGBxl+6VcdV1xcRP0mVridlVd8mswr+6AtvT0j+GBonNaaSxqMv6d3VNWsG4yuoF6hS5cK1qg75ZRk8S6a6OreUz89c7nZpyp3qYaI3Ek2NrJMt3jgrRKQ4J9qBoEu/o6OW2lHNK7k/0Twu7ROWwEXOGGIWjyur0mEf435JYZabyAWFAfYvQ491wwRR2dVuffue5Y+ff4h+pZG9M8zsjv8fM33t0/j7zRaHgHK4qIQT/iavr0SMm+aqDIb8SNXID3k6oE5CtxD5K5fpcCx02iEisr2opudR5JId9lrwgwt0PjkOOMYT0gkl+XR3hPKvWmQbbLVQj4O47JdzMYBWlyZaSeW44oqQfxB4hlYjxOCMpKspvV1CfqO8HgBCQBjhgTbwiZtcHUJYFajXmMvIW5Adxkb0DuZtqB7rLAtdaOySunTvw3nWKXQY2ITJ44uFbdseWjS25yDWpvl1BJLgKz7ORKXmlltM7OjzbcnIxGM/mM4AOk+eQuYMXX2yrmh1IBIytGYj293wyu8A2nABKjNVrr/vvRE4NKQDhDGLdPVS+bo4EPeg5m7ozuZ0MBk1GQuhIEsTjQspdgGcOYZYYcwLFMFpcys3BuiRPF/5EGVBBTzA4MBQRtYY6d9+JyEHmmZL27AihFPkWQGgnJLNlgB7+y+TIOPj/yn1PafZwzbUyzl8s3op6upYW3iDJu5QqbZQmUYP2udLDx2P/oWPSVZKuFPWeppeyj0JMyRDpNhHPkRLgezgppKT9YvT81OkqDnCNgs7kxF2mxs/VvBG6xxbPTgEqfpDv2YEvqUUDWcgjg9HsslZrEzvwcWPeWdoSS8oVcDSrBGws59hUftPEGjjbO7suzOYaOuNDX33Q+YqaVgSrrkBJG9+l8f0IRUR3923bz95/MBZzY6jp7UV6HJE/J2zX7ClnOWy7NZep 5vZc1gC0 n0CPNsRDvQ0+dkCztseQRlMi5LDBr0Gdglf3OhmJMAj0tJeO6gwChm1ClRyTkws7fcqq1mihE8kufhtLa2moGZQ84mO3xlMaysih7HpbcJVxWjLfieAN/8+laorrrXmPw0nR5BanwP5/rT5QPvf45qel5fqWbD0+f8bpouZ/SHgxBAYWDpgym8HtczAwwdzkDkmnBgnpWCt3caZVcVHT9bFBzuOgKDzhDRAIhytkG9maQXp7FO942QdTAkr8FbsOto3CQ8BhRHzQtPwX5WddUZ8VEKPMaQBk+PknsKBmpDztjH7wFauMU7bjAgCf1pEX9kw9zfyU9dvXbjJtnWvEx9EH/w5S2PKTNFhj1voNXydOxQmSjU65cHvnSRxppbL73NrPPt9gG02gToAWX61eYDrui9e43Psz9hcyLFXVEWWNu+CFwfagSf23Gic291lJSUeYb8P84PxZsKUa8hHz+vy9ronCj2r90WvzCBNocShdfi/dEWjXxyKWUIr+81wazzj23KxqDBrCxn+hGxr6FMvGO5B5hvRRzLt3kdqxWo9aZT/qTstc6QN6IIP0KDd7JR/LgtoshFQePL+66nDXmrDr5/qc5e80ABFlKJScYPhNjWoU= 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: When VMA based swap readahead is introduced in commit ec560175c0b6 ("mm, swap: VMA based swap readahead"), "struct vma_swap_readahead" is defined to describe the readahead window. Because we wanted to save the PTE entries in the struct at that time. But after commit 4f8fcf4ced0b ("mm/swap: swap_vma_readahead() do the pte_offset_map()"), we no longer save PTE entries in the struct. The size of the struct becomes so small, that it's better to use the fields of the struct directly. This can simplify the code to improve the code readability. The line number of source code reduces too. No functionality change is expected in this patch. Signed-off-by: "Huang, Ying" Cc: Hugh Dickins Cc: Alistair Popple Cc: Anshuman Khandual Cc: David Hildenbrand Cc: Mel Gorman Cc: Miaohe Lin Cc: Minchan Kim Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao Cc: Kairui Song Cc: Barry Song Cc: Chris Li Cc: Yosry Ahmed --- mm/swap_state.c | 48 ++++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 848c167df530..e1dac70198a6 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -42,6 +42,8 @@ struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly; static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly; static bool enable_vma_readahead __read_mostly = true; +#define SWAP_RA_ORDER_CEILING 5 + #define SWAP_RA_WIN_SHIFT (PAGE_SHIFT / 2) #define SWAP_RA_HITS_MASK ((1UL << SWAP_RA_WIN_SHIFT) - 1) #define SWAP_RA_HITS_MAX SWAP_RA_HITS_MASK @@ -738,16 +740,9 @@ void exit_swap_address_space(unsigned int type) swapper_spaces[type] = NULL; } -#define SWAP_RA_ORDER_CEILING 5 - -struct vma_swap_readahead { - unsigned short win; - unsigned short offset; - unsigned short nr_pte; -}; - -static void swap_ra_info(struct vm_fault *vmf, - struct vma_swap_readahead *ra_info) +static unsigned short swap_vma_ra_win(struct vm_fault *vmf, + unsigned short *offset, + unsigned short *nr_pte) { struct vm_area_struct *vma = vmf->vma; unsigned long ra_val; @@ -757,10 +752,8 @@ static void swap_ra_info(struct vm_fault *vmf, max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster), SWAP_RA_ORDER_CEILING); - if (max_win == 1) { - ra_info->win = 1; - return; - } + if (max_win == 1) + return 1; faddr = vmf->address; fpfn = PFN_DOWN(faddr); @@ -768,12 +761,11 @@ static void swap_ra_info(struct vm_fault *vmf, pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val)); prev_win = SWAP_RA_WIN(ra_val); hits = SWAP_RA_HITS(ra_val); - ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits, - max_win, prev_win); + win = __swapin_nr_pages(pfn, fpfn, hits, max_win, prev_win); atomic_long_set(&vma->swap_readahead_info, SWAP_RA_VAL(faddr, win, 0)); if (win == 1) - return; + return 1; if (fpfn == pfn + 1) { lpfn = fpfn; @@ -794,8 +786,10 @@ static void swap_ra_info(struct vm_fault *vmf, end = min3(rpfn, PFN_DOWN(vma->vm_end), PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE)); - ra_info->nr_pte = end - start; - ra_info->offset = fpfn - start; + *nr_pte = end - start; + *offset = fpfn - start; + + return win; } /** @@ -826,19 +820,17 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, pgoff_t ilx; unsigned int i; bool page_allocated; - struct vma_swap_readahead ra_info = { - .win = 1, - }; + unsigned short win, nr_pte, offset; - swap_ra_info(vmf, &ra_info); - if (ra_info.win == 1) + win = swap_vma_ra_win(vmf, &offset, &nr_pte); + if (win == 1) goto skip; - addr = vmf->address - (ra_info.offset * PAGE_SIZE); - ilx = targ_ilx - ra_info.offset; + addr = vmf->address - offset * PAGE_SIZE; + ilx = targ_ilx - offset; blk_start_plug(&plug); - for (i = 0; i < ra_info.nr_pte; i++, ilx++, addr += PAGE_SIZE) { + for (i = 0; i < nr_pte; i++, ilx++, addr += PAGE_SIZE) { if (!pte++) { pte = pte_offset_map(vmf->pmd, addr); if (!pte) @@ -858,7 +850,7 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, continue; if (page_allocated) { swap_read_folio(folio, false, &splug); - if (i != ra_info.offset) { + if (i != offset) { folio_set_readahead(folio); count_vm_event(SWAP_RA); } From patchwork Fri May 31 08:12:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13681306 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 74378C27C4F for ; Fri, 31 May 2024 08:13:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 003866B0095; Fri, 31 May 2024 04:13:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF52E6B0098; Fri, 31 May 2024 04:13:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D96006B0099; Fri, 31 May 2024 04:13:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B7CA16B0095 for ; Fri, 31 May 2024 04:13:10 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 69588C0EEA for ; Fri, 31 May 2024 08:13:10 +0000 (UTC) X-FDA: 82177975740.12.2B9169D Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by imf18.hostedemail.com (Postfix) with ESMTP id 4F81C1C0016 for ; Fri, 31 May 2024 08:13:08 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="R/9WTWu1"; spf=pass (imf18.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=ying.huang@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=1717143188; 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=fa0mzB/D4ntX5+IbbLHXMCXZ+fi5APaZ9VEgBDgsZrk=; b=XDuZJqPF/0JjCin/kO1lUrx/IgnWDpK2vOHr+YnJufQEX6UM178rqFtAzhjktothvlzF/J SaXteOPosD8yKPH4nbTnpizTyJMMfNUFCZrAE4OLkplJWIv30usP5b4KDHv4nxnDGgJq2v PcXcEr5CUij9MlIvgcEB+Rlhv2NwCac= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717143188; a=rsa-sha256; cv=none; b=ODGjbqXQaV7pueLoDwYAMUDCXzdrP7a0JVp3AxRhJkdNyanCm98kzywdgL+x9bjf+Vqy1w 8I9lYb/Ec9Qi5Kd47T6B5uJwnAA/o5kigOkK396ECeNrkxQ+jOcJgti+YwI1rRfn84Z4BD pf9RXdkDl+dwrTApgmgquyhCeCLh5Ns= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="R/9WTWu1"; spf=pass (imf18.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=ying.huang@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=1717143188; x=1748679188; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XtM1tmkue2R/RWq0rdfSHuRCjgaKpNa2YNSgCs9L/sc=; b=R/9WTWu1EzTlAB+QM7FQOXTvjBF/ATXu2UeYXda6FNFHmRZqgTFHUCPR zW7LoPb/t+NvNXj6u6GLSKvDbIU5H7OARImkLd2pkNETaqwoPOxHMV7aQ FiDHDLcnD2483hjOIIMgiVqI4pMq5PelCMYCpezng6FFgd6I7teTnCN1H SSs0vTagduQT/m2BuElsEumNITmFLcGauHaBqZblqXj16HWCK9axbj7/O rIRmXU9wjuW7dALPP0aJ3OkabLmMXyZoRzRRVkuZUL8vb2y3Nf2vtMvO0 lka+9rGYMyFUoxrfY5wtqTMCDciPU5cDksD9PJphv9/dT3hFosM+AtAZS w==; X-CSE-ConnectionGUID: E52KlOp9TOqN4bjxKVXyWw== X-CSE-MsgGUID: aefUvbj5ScmtiyYT2CombA== X-IronPort-AV: E=McAfee;i="6600,9927,11088"; a="25079778" X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="25079778" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 01:13:08 -0700 X-CSE-ConnectionGUID: ObA5obvcTXa5rgNCCMgFCQ== X-CSE-MsgGUID: rJElwWo5T9qLZMt+cdpi/g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,203,1712646000"; d="scan'208";a="59270962" Received: from unknown (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.30.35]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 01:13:04 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Hugh Dickins , Alistair Popple , Anshuman Khandual , David Hildenbrand , Mel Gorman , Miaohe Lin , Minchan Kim , Ryan Roberts , Yang Shi , Yu Zhao , Kairui Song , Barry Song , Chris Li , Yosry Ahmed Subject: [PATCH 3/3] mm,swap: simplify VMA based swap readahead window calculation Date: Fri, 31 May 2024 16:12:30 +0800 Message-Id: <20240531081230.310128-4-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240531081230.310128-1-ying.huang@intel.com> References: <20240531081230.310128-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: ar455nxa3dwee89nb5arnue5jtg66ofm X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4F81C1C0016 X-HE-Tag: 1717143188-554497 X-HE-Meta: U2FsdGVkX19udan4742ldCvrTYfM32cSOVpGBkt+O5VIOZkxNzEol1lKuKhAN9eU2gv5O/e1IIP5+pOkyQChbbLib0mVTQYo8ImNCYxhqy4aXl3AP4pUGgTras0k4Zuf/AK1jAT2q2HhCXT5tElHXdfIC6oYReeli7a/pWtudwT+35nCHQ4JL3vvmuuDa9U2/81XLpMshFsgCSUyBpPUgXog7Ip8N8XzZdXrgKYZNirguEyLF2K/2FlJIKOYUnzP1cNcvI/6WGfaRxCPqiZVRMbCmNzk6KD+l4EixqRfxm6Gf/qnNkZiBxjcUBmzS6u8CmnW1sVN7xMXfgA9Erf6+vSOjnBaF3MFoG405IzI4pY4ZGxv4wTHY39FBu+qc5mltWJ023gPMxDow6pAfzPzc5X8L9+a1YhqK7Wnzl4zh7FkbaUTSYxMk5fJtiNAeld6jLX0HD6E+IpPfitZaaezC/GT9m5Ks/Hetp2n96orPGb/STbCLozmwnwUuXVQOQYfgV3LmMpHoyQs6Fwd2MPtSG8tDXq5WhurbvYuS0swEJD5mKbdl+qaVLndMFMNoU/QmbwrDIo1YbRXJ/TREPFi3F1WporHFzk8vrfQNAJywNI/RW6GuyvVDo2IIA4ql4dnGCb19ylPE0J8mC+MfNCQm1KlVKdXNq3x5vQgKyQUwFAYpwpEWS5DY7PeX2HmlCPSTewQRYmtbSnlBOHoste0j4SKmDNefLnPIdFt2/3qxgee7NotwYN1nc+DLWnhFaWwzan/kYunTPYjtp5K95AWkBq+KQAk14l2X3Jmn6Xj87QfqYWdGmuU9r/ExpPd8VijvQdo701yoXhI9Qrobm3uUzznYA8QhkFZpEqD72SlBULDH+JxtOVh6Vo+kiCRIhC13Zx9QyND9QzeR/UzeS32ear6UcJhVQCy2kIi7qhDw0X84fs+StZzzHS07SvbHORpfUi3eNiHpk0e94LtOLU Bwrpsjr1 hx+yBkG/+gJ+o2hJu9JX1RsJuQuXV58QQL5IeoS5c48tt+z1xgBhk/JOCQ/SOw4bJVi1ca1/FEUbrXRPe9/R3sFC6Ly5zZyRToZ133P7gIKpalPN0Gt+YmuvG1X3+8O4KoUQKYW3f9S5Gb1kQjgcFuFsnHvLIfbb9IdwrjnpzbGe7tqCplHOKantNC82UFJRMjof2bcXkHNEzKn1raTXyWM6XoS7VxyXAxwmw7lIX+ze9f4VMSA3G6R0Mt2Iqk+nD3XT0g/hKC2wc8wDVvdhTP3pKg/jiP++uCa4sTW+zkSB5TZG0rAExKix6vVRqaYGNF7e0yLBmDkmQz0RXjlq5rxuklB4VPw1+1nxAsFw8GlFXaxAc6DhzwRKoEm+oXNFtRS+QxBrTR33TV2fT8lvxrD6qGk0tgFeel9OBnSMu1iPP4nwhOWOn64zshbpvFnWjT6Hxv2Rn8mPWR8VRHsSjPoD82SkKpIrcpHW1bM/TtOmYYyR//C9wcbd4MLZLhHzahbAbJStn8wiWnVvzKMPpazOsf+hBWhhBwdZi3QTOIc2636DBa0X31CZCTncDMh4nGoVr 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: Replace PFNs with addresses in readahead window calculation. This simplified the logic and reduce the code line number. No functionality change is expected. Signed-off-by: "Huang, Ying" Cc: Hugh Dickins Cc: Alistair Popple Cc: Anshuman Khandual Cc: David Hildenbrand Cc: Mel Gorman Cc: Miaohe Lin Cc: Minchan Kim Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao Cc: Kairui Song Cc: Barry Song Cc: Chris Li Cc: Yosry Ahmed --- mm/swap_state.c | 66 +++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index e1dac70198a6..d2adbd7b571b 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -740,54 +740,40 @@ void exit_swap_address_space(unsigned int type) swapper_spaces[type] = NULL; } -static unsigned short swap_vma_ra_win(struct vm_fault *vmf, - unsigned short *offset, - unsigned short *nr_pte) +static int swap_vma_ra_win(struct vm_fault *vmf, unsigned long *start, + unsigned long *end) { struct vm_area_struct *vma = vmf->vma; unsigned long ra_val; - unsigned long faddr, pfn, fpfn, lpfn, rpfn; - unsigned long start, end; + unsigned long faddr, prev_faddr, left, right; unsigned int max_win, hits, prev_win, win; - max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster), - SWAP_RA_ORDER_CEILING); + max_win = 1 << min(READ_ONCE(page_cluster), SWAP_RA_ORDER_CEILING); if (max_win == 1) return 1; faddr = vmf->address; - fpfn = PFN_DOWN(faddr); ra_val = GET_SWAP_RA_VAL(vma); - pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val)); + prev_faddr = SWAP_RA_ADDR(ra_val); prev_win = SWAP_RA_WIN(ra_val); hits = SWAP_RA_HITS(ra_val); - win = __swapin_nr_pages(pfn, fpfn, hits, max_win, prev_win); - atomic_long_set(&vma->swap_readahead_info, - SWAP_RA_VAL(faddr, win, 0)); + win = __swapin_nr_pages(PFN_DOWN(prev_faddr), PFN_DOWN(faddr), hits, + max_win, prev_win); + atomic_long_set(&vma->swap_readahead_info, SWAP_RA_VAL(faddr, win, 0)); if (win == 1) return 1; - if (fpfn == pfn + 1) { - lpfn = fpfn; - rpfn = fpfn + win; - } else if (pfn == fpfn + 1) { - lpfn = fpfn - win + 1; - rpfn = fpfn + 1; - } else { - unsigned int left = (win - 1) / 2; - - lpfn = fpfn - left; - rpfn = fpfn + win - left; - } - if ((long)lpfn < 0) - lpfn = 0; - start = max3(lpfn, PFN_DOWN(vma->vm_start), - PFN_DOWN(faddr & PMD_MASK)); - end = min3(rpfn, PFN_DOWN(vma->vm_end), - PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE)); - - *nr_pte = end - start; - *offset = fpfn - start; + if (faddr == prev_faddr + PAGE_SIZE) + left = faddr; + else if (prev_faddr == faddr + PAGE_SIZE) + left = faddr - (win << PAGE_SHIFT) + PAGE_SIZE; + else + left = faddr - (((win - 1) / 2) << PAGE_SHIFT); + right = left + (win << PAGE_SHIFT); + if ((long)left < 0) + left = 0; + *start = max3(left, vma->vm_start, faddr & PMD_MASK); + *end = min3(right, vma->vm_end, (faddr & PMD_MASK) + PMD_SIZE); return win; } @@ -815,22 +801,20 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, struct swap_iocb *splug = NULL; struct folio *folio; pte_t *pte = NULL, pentry; - unsigned long addr; + int win; + unsigned long start, end, addr; swp_entry_t entry; pgoff_t ilx; - unsigned int i; bool page_allocated; - unsigned short win, nr_pte, offset; - win = swap_vma_ra_win(vmf, &offset, &nr_pte); + win = swap_vma_ra_win(vmf, &start, &end); if (win == 1) goto skip; - addr = vmf->address - offset * PAGE_SIZE; - ilx = targ_ilx - offset; + ilx = targ_ilx - PFN_DOWN(vmf->address - start); blk_start_plug(&plug); - for (i = 0; i < nr_pte; i++, ilx++, addr += PAGE_SIZE) { + for (addr = start; addr < end; ilx++, addr += PAGE_SIZE) { if (!pte++) { pte = pte_offset_map(vmf->pmd, addr); if (!pte) @@ -850,7 +834,7 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, continue; if (page_allocated) { swap_read_folio(folio, false, &splug); - if (i != offset) { + if (addr != vmf->address) { folio_set_readahead(folio); count_vm_event(SWAP_RA); }