From patchwork Mon Dec 19 18:58:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13077054 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 A25C0C4332F for ; Mon, 19 Dec 2022 18:59:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 434898E0003; Mon, 19 Dec 2022 13:59:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E4A88E0001; Mon, 19 Dec 2022 13:59:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2AC6F8E0003; Mon, 19 Dec 2022 13:59:20 -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 1C1348E0001 for ; Mon, 19 Dec 2022 13:59:20 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E7EB4AB0EF for ; Mon, 19 Dec 2022 18:59:19 +0000 (UTC) X-FDA: 80259968838.29.E1466A3 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 3685E1C0010 for ; Mon, 19 Dec 2022 18:59:17 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=gTdt6BQe; spf=pass (imf20.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671476358; h=from:from:sender:reply-to: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=XocXQJ8/KIfX9/nTT1Ghmvl4XAcC2jwtLySqVPgnO78=; b=fg08vxl8XdlPA7SENrHVptBHlbBz4VFGK/JbvdWuu68hHgPhcAY/mO0HNli0PCXIDtSGkN yvv7zMBBXZFND0PlcoJjeo9anyziZLPbqu8V5nQHgFnbhkd41H+PLH+Je+dOfQrj/7tzWb /EwuTcKNd7HAGz5z1lZa8+VtqgZ1GZ4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=gTdt6BQe; spf=pass (imf20.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671476358; a=rsa-sha256; cv=none; b=1F5r/GT6gpjkZ30jOAmh0wnT0K9mfZLBGuzJcwrXLPZDWkgFx5KnBP2KHNwL4VjCGFoDGe yBDQNk9PGNq4ICYLk+/dxywndM/++8niI9bTaK4/+kdJDtO3ZQip7NLSb3+yRpxQEA7L+n Udfa67tDZ4XmxxJrxAEje0lhoSC3OMg= Received: by mail-pl1-f172.google.com with SMTP id d15so9939677pls.6 for ; Mon, 19 Dec 2022 10:59:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=XocXQJ8/KIfX9/nTT1Ghmvl4XAcC2jwtLySqVPgnO78=; b=gTdt6BQeQx60iQ2xEpVkG9N2cVTn02TqELcNPrH2DPQHOHfHgBH7nbVVm4lJ0If6bi u8KJrL4+epaHOp5Ca5289ihVcOtRR8nvfWgIYq4R4IUNs45HAdcXI2X0EmzFMGuLVQRm rYddyOZNUm6HMseaqPywQxKdvhG2PrAOir8HWF8OlQ3muwxqLRO1dpkL6Eof7Lc5DHsW nExx/9uTYg/ylbOa4u5j4XAy35yUDRjgsu1L3drtgnxRxc5xbLN9wgJjbTR5fPIq5k7h kjMeYKpySfSdX4ZrmK7ECOULbiV6BH1SAgg3unw12ilu4j/ouOJRVoSSO7S4krtdRzZV sWaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to: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=XocXQJ8/KIfX9/nTT1Ghmvl4XAcC2jwtLySqVPgnO78=; b=LZohOwvqLpzrYmvLacKKKd4vT48xUE6nbQylbg/lKijK1mrhOV41/sdV4445polOEu C1l83EU5dcz3KIWHDxY0tfo7iOLKOLlVNWNqaRjdAAFeAslBjR41c3rp04uMli+Ti0cV s4QDoX6EPCM7Nq8oYydf36t8T+d6IzGQ2EbBGIIFGL0rIR7COg2jjJVIPWSzpv5J6GLe QI96mUOShL8GJ4+mWUWxdPzdKWYkvPju7gtmF3uTq8MRQezwhHpIW49sI4r0CwB63piM PWFcTxW73MqpXJiaXnBYmKjAyNSO+7XW/g3oSF1GhETtJmXQJCkS1I+W4xv18M8hEqzA LO6Q== X-Gm-Message-State: ANoB5pnSy6v5rFQK7LTNGgNRj9lZ35duo5OpRfQQ8MLV80BVR2N//+O+ tsRBPaSE/cGxHb84DAykd0YoIJMlPocSRu+f X-Google-Smtp-Source: AA0mqf5+mjYb7s7cWrqX58p+GXu392SobpICcwlH+tQQtjEkCkRadzwoFuctBrNg/jxllqNTLEPk4Q== X-Received: by 2002:a17:902:8209:b0:189:851d:644c with SMTP id x9-20020a170902820900b00189851d644cmr40913337pln.40.1671476356452; Mon, 19 Dec 2022 10:59:16 -0800 (PST) Received: from localhost.localdomain ([198.13.51.166]) by smtp.gmail.com with ESMTPSA id i6-20020a170902c94600b00189847cd4acsm7480790pla.237.2022.12.19.10.59.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 19 Dec 2022 10:59:15 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Miaohe Lin , David Hildenbrand , "Huang, Ying" , Hugh Dickins , Matthew Wilcox , Kairui Song Subject: [PATCH v2 1/4] swapfile: get rid of volatile and avoid redundant read Date: Tue, 20 Dec 2022 02:58:37 +0800 Message-Id: <20221219185840.25441-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20221219185840.25441-1-ryncsn@gmail.com> References: <20221219185840.25441-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3685E1C0010 X-Stat-Signature: shwiyixss6f4mf3zco1b147jx93mcquw X-HE-Tag: 1671476357-272018 X-HE-Meta: U2FsdGVkX19OJeigg7zYXf9P2LKZLAZI8XcbVBfh1z+ocXIijFJmTAmS8Saynx2cB74VUWQLJ+6X8Vn+K/q9GPTumq0SXmTTaonKudWcA+eRyFzpnibekzedczleWk5eVBGRmEjEfNE2hV/kSOJgSBWWzXl96GdDe1fd0icMZ8F3TgS09ZMYXETzuOGWGuWMgkyWDByAs+fWRx8NG4UkqIbvqtRPwinw0KPaYTN4L7VS/IjSed/OnfsVIsKDIm4BjoHfzpTXcfor7+7MSNIM9DnEDirK7yqneScEd6/j+Dy2wQGfUA/yXYENVA1XT5feGTRI3uyIGgTU2OPpQUdfpA9LRD420KupARUV5iV1aBrGeMxPABIZhL55wbrq/sMDWkVyGSM92Wtt7IY1RlJXgk+P1jxWd8KPY4s3BgpMEatPuc0YUuvNqwSaZJfhhsDu+9RvPJ03HohJd54aXYjkPZnO7z7e9tl0lTqqZm7EjVOw/XJsQnRJFYPI/t6VIEYYrbKbjGmbbhCFax6ckZSCqCir55SVCuySWrbic1kqCpuXTV1432rbou7NFebBeIGOFno1JqQrjy0X0QtRY/kaikWdJlQ529kzDI365ZjQ+65PUCweuvKE52mwC0brB/0liBRRPtVG1YDZbAInxJ5dFfahERogoJtvWhHEvuQe6mcEglSSvrXe05Hm7HaSHTIMeflianrl/PGEJEPUurFqn43AVKM40txu/bqV+Qrm4oKqFRkPYyaxK/cjKYturIoiN8iIHJUxMOUVt5Db9L3rJugTE0DmSrKw1PgeHRIemavOcV9jqKJywKPaN3kyyMwacBqEe1WD1Egfy6DDpwg7IV+M43hKYW53kcdls0qSmv3jJaR3eFTn+jgWnaCWla5nvbi+9VeOBH76RaWjz56jm7OJ5c1NR6DPwG2EtqJX1q3bO+rpIMpjtRmXBAzd9zpd1pgVL3eQY3uj/JvzzYO Ux+DurOe ORsyIoS9iYFGIDrHPD4Mj6xOCtP5FMCNZ+TpT9semKPn6Nslzzk/aHs55+CN6ma/ulX/FrziYdGmIj8qYl/EbX6zaQRL37c5bADGLJFKpF1Y2lrUNvPit0eknZJg6/l9VL2N+X1sSCbn43invGz2tWF0GbHEFF7wGHuJzvW344DCJjkufpabp6XvV6N4JgzNYDGxjd7CK1LHBnm3fkgCQCZTguR54K1nDvhLxoh+uvfUO7FVfL+EdqIa0cPzaS/0zeIWRRXNaIwGivMDSkYbYHeSvyA== 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: From: Kairui Song Convert a volatile variable to more readable READ_ONCE. And this actually avoids the code from reading the variable twice redundantly when it races. Signed-off-by: Kairui Song Reviewed-by: "Huang, Ying" --- mm/swapfile.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 908a529bca12..6d3f60bd383b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1835,13 +1835,13 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, pte_t *pte; struct swap_info_struct *si; int ret = 0; - volatile unsigned char *swap_map; si = swap_info[type]; pte = pte_offset_map(pmd, addr); do { struct folio *folio; unsigned long offset; + unsigned char swp_count; if (!is_swap_pte(*pte)) continue; @@ -1852,7 +1852,6 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, offset = swp_offset(entry); pte_unmap(pte); - swap_map = &si->swap_map[offset]; folio = swap_cache_get_folio(entry, vma, addr); if (!folio) { struct page *page; @@ -1869,8 +1868,10 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, folio = page_folio(page); } if (!folio) { - if (*swap_map == 0 || *swap_map == SWAP_MAP_BAD) + swp_count = READ_ONCE(si->swap_map[offset]); + if (swp_count == 0 || swp_count == SWAP_MAP_BAD) goto try_next; + return -ENOMEM; } From patchwork Mon Dec 19 18:58:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13077055 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 BFC16C4332F for ; Mon, 19 Dec 2022 18:59:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40D168E0005; Mon, 19 Dec 2022 13:59:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BD538E0001; Mon, 19 Dec 2022 13:59:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 285598E0005; Mon, 19 Dec 2022 13:59:26 -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 1AE1A8E0001 for ; Mon, 19 Dec 2022 13:59:26 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E512CC09CD for ; Mon, 19 Dec 2022 18:59:25 +0000 (UTC) X-FDA: 80259969090.13.ED1CFA1 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by imf16.hostedemail.com (Postfix) with ESMTP id 3C3DB180015 for ; Mon, 19 Dec 2022 18:59:24 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="SjsPir/G"; spf=pass (imf16.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.50 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671476364; a=rsa-sha256; cv=none; b=pd1cNOoDHSUeEMEi0RXq7jQV4ciz/ZRfqFQ5krkyL0fOaCsOi+8yLvTTY/28XP9qbZwArz xfDgcPwHeXVm2yp11KdqYAVPTr6vA0PpAAjoOXTs14faXJpWv0yMOqyT9LGA2BXapqefsG 91OVB3WV24wdnxiRt17yqKOnfg/bfgs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="SjsPir/G"; spf=pass (imf16.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.50 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671476364; h=from:from:sender:reply-to: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=rNW8A16mCA3HvLKh5wBxi0WsnpxyoQsg0W035rUDdlU=; b=WmQf2Wz5tqol2gH9CrUE4qKpheJIiCqezbSKJws0qyipz+3eGwmz7aPVMFsNOzRu126zpq WBoWbWthhxVShXBcDSNjDKdwskbhglwFJko7nxjbaJMW/Siw+RJYNGxIXorR/UV7Yx35OA yo0hmJ7vcoviGRR7fHGt+9cL7kMcAcE= Received: by mail-pj1-f50.google.com with SMTP id u5so10023278pjy.5 for ; Mon, 19 Dec 2022 10:59:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=rNW8A16mCA3HvLKh5wBxi0WsnpxyoQsg0W035rUDdlU=; b=SjsPir/GRG0O5nhdNnhsaE3MhQHqJZ4mVl/THUD4eCydfYFbtbpemySFb+Ft5Wdx5U 2MrDiiZASOzg8/oHQZoGZFNeTeGjXDbgTDDOlSs6tLS6zQk1tzOsRZos8c0IR8miW92d XShpB9RiypeH8hMu1FE4S7AUJDYfQ3+9P9pQf6ZPJMJBhHCIwPruDMC6aOy6HctXFevj g9tBhlQDX3N1yS+QcdS+/VZhPcmlP+SuchHqDbQjIulJD6bCkDFcBWPKFCTTNnibzw40 FUXZN88VJWNIfWZ1wzctW56tuC23pAApGc0qxk11gf4vEEwEKzDO3uURBz3G9z0MXAFU yHsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to: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=rNW8A16mCA3HvLKh5wBxi0WsnpxyoQsg0W035rUDdlU=; b=cGZucHHa+JO7ko14f+kzjXT6J0U5L0kmiBKwkb1VRYPE3eI+/SQIlnShpBGTlP5P4w VoRNq93Sz6YIgF/gO22oi3FoGtruPTfTnpw02M+J1ON2iY5en8XeNCWIDpryHSD/kkI7 na+I9AEPu9sF4ZrfYT96FyUR/s9haxOU5HLzwOZ27gGCVDR2VM/GQ2L0TTSYTATRZCbE x2quC/gSqvGIHwimCPpkxTd2t+4lyc2ca65YvgJYms6nomjJLmKCymipu+8rOgP77bzc fQuzUXHKa60ASl5yNzBGSWsIy+FJHMWC2Yj8oOyfHerlUlpFr9oeopRC5gWe796nJ0Zi tVBA== X-Gm-Message-State: AFqh2kq56Tc1CbNsWGWBkAReHnKHM2Q+DBw6bOxcZr3s0UjF+OGT9pMg AAZ7rYep0fD+sVLkG3jTDXgpRvJL3UGVi0b/ X-Google-Smtp-Source: AMrXdXue4571aidkiNSZgU3GJnAm6ssIl5Vm/3VAGWBWSHwRJxQCuxfwEu2/ilYQ+rpJ1+1QRO2McQ== X-Received: by 2002:a17:902:8343:b0:191:2b76:612c with SMTP id z3-20020a170902834300b001912b76612cmr2600746pln.62.1671476362679; Mon, 19 Dec 2022 10:59:22 -0800 (PST) Received: from localhost.localdomain ([198.13.51.166]) by smtp.gmail.com with ESMTPSA id i6-20020a170902c94600b00189847cd4acsm7480790pla.237.2022.12.19.10.59.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 19 Dec 2022 10:59:21 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Miaohe Lin , David Hildenbrand , "Huang, Ying" , Hugh Dickins , Matthew Wilcox , Kairui Song Subject: [PATCH v2 2/4] swap: avoid a redundant pte map if ra window is 1 Date: Tue, 20 Dec 2022 02:58:38 +0800 Message-Id: <20221219185840.25441-3-ryncsn@gmail.com> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20221219185840.25441-1-ryncsn@gmail.com> References: <20221219185840.25441-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 3C3DB180015 X-Rspamd-Server: rspam01 X-Stat-Signature: htycaabd9qww3hzkq7dmugoeqm7xmam7 X-HE-Tag: 1671476364-399590 X-HE-Meta: U2FsdGVkX18iha7DC8yxN+tZ3w3PCZtoqER/LENTR5emYuyJGiqlXUq/xgXcKjH3c2YPGcyr9KIDlM7lYnwctQXgqC2nF2965xSqzw8mPXBxSb6y6HIOHhlratH+F0BYu5Yuu1myrOSCqKmeBXe5IBE5GK3gBTQvut275lDO/EbMXiJ/DV8eKmaG2pA1zBY0E3FstMJXymuFzWn9Ln3BFdngUg83qxVZzPTfTZa9xEfJhCweO6v2js2VSVYgmyIHBMXoG1pRa1Jfx+PFl8dV7/RMhPNWy16oWUxMh3OcHlRXKT4BnOPRpSHecWUquErYJYEF2Dc3VGVCz9ok9CuBTnkeiwPubcSPGSGM5OP8aPBBA/8scsb7cZksPADDb3k3mHgatrBUyeU5wXhTCHxtC9GxEW1zDbZJAm/KA5cFGiajG+zevTPHDcfC2Bvdptk6v0RtDLqEZGd1J/mMlDcglRYfAoqO0BJ0bsrtrTypTdCofxpHR8yUXI9bfOC3pgkS9GUQJ50nAhu2zCKH8aCm1FcW1BJoCeALq7DjlJG8VyhE8rmuUbcVqs8x1LSMv1G6T506PsKk6mqCPQg6AmXlaBj7TtVyL3F18uO/3st+tVtFEzKT3OllA+uq6NzlD43LFPzDkOs+CJ6Ml+HrwYppbE5KOiFHzHs8ftoIxgy58di8fVhgP+UY6+UL/jQnrKfO2M0DrzUaqy9lj92LYjhddT8TU7yzk1OUSgtNZ+qH69RaMu1HZ6Athmv2ZFD+0GVNFxyJB/3SLxY/ckpWS2F+1O6PZpOuK0dz0cxvFrU5NBIqz5QfEyp4ykQ1lkCASZqNXO17SNICstDAF51WQPLsNsPhDJtRubVFefFDfkwzQrTslphfVZ5MgnznbToy+im/Q36S+xrCOJt2u13DAAdf6YxgcRDiusrH4rPoIV6xUKOVMA1b4M3mPCuFnIDBk7DJynVBcjdlLjb3I/ulIBE Efuxe8Pz Cw9wbFkcembH7SzD4FElQl9mAwges0yQPk4zd87PuTN+fbljJTipJYXRm9w/IEkNjvU8tYL0ZX5oJHaBSpU4DPmROGKmL/7Gp+/p3JW1wAz58vNmLnmMYZqDmjHKoaRdeQmVv5yeRuKXUnCzczHktWKykaImH0z0WJ+Bn5gpr/aLp9SuXazD6rZSkuPGI8yYP50O6bAZ/xDYrx31UuO3SwLqtoPIrUAksdFOUtBmy3KN+Shk9DAw0FvqZEasT92Ccqa1C5eEc89zJMSl98KCQP1s7jQ== 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: From: Kairui Song Avoid a redundant pte map/unmap when swap readahead window is 1. Signed-off-by: Kairui Song Reviewed-by: "Huang, Ying" --- mm/swap_state.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 2927507b43d8..af8bc123b7c4 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -727,8 +727,6 @@ static void swap_ra_info(struct vm_fault *vmf, } faddr = vmf->address; - orig_pte = pte = pte_offset_map(vmf->pmd, faddr); - fpfn = PFN_DOWN(faddr); ra_val = GET_SWAP_RA_VAL(vma); pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val)); @@ -739,12 +737,11 @@ static void swap_ra_info(struct vm_fault *vmf, atomic_long_set(&vma->swap_readahead_info, SWAP_RA_VAL(faddr, win, 0)); - if (win == 1) { - pte_unmap(orig_pte); + if (win == 1) return; - } /* Copy the PTEs because the page table may be unmapped */ + orig_pte = pte = pte_offset_map(vmf->pmd, faddr); if (fpfn == pfn + 1) swap_ra_clamp_pfn(vma, faddr, fpfn, fpfn + win, &start, &end); else if (pfn == fpfn + 1) From patchwork Mon Dec 19 18:58:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13077056 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 D5806C4332F for ; Mon, 19 Dec 2022 18:59:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6CCD88E0006; Mon, 19 Dec 2022 13:59:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 67C6F8E0001; Mon, 19 Dec 2022 13:59:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56B898E0006; Mon, 19 Dec 2022 13:59:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 48D088E0001 for ; Mon, 19 Dec 2022 13:59:32 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1336F1205BF for ; Mon, 19 Dec 2022 18:59:32 +0000 (UTC) X-FDA: 80259969384.29.263EDAC Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf24.hostedemail.com (Postfix) with ESMTP id 66B5218000F for ; Mon, 19 Dec 2022 18:59:30 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=eDZyM7UO; spf=pass (imf24.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671476370; h=from:from:sender:reply-to: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=e8B2I+4NRvgsSq6ExVIS5zUbrcMf1l4UJ5x1/qJc8cA=; b=D8jZySqJXbSTOvpBG4oxt9fxlf+crLi3Ihdu1fbsNqlZs5oSdrByLmA/KiZktSRMGUbVqo ptyMgHL2InNAj3cJ31LJYalBPPUloicni1ZCHO8Zf/+0vccs/nCId3wDavnHgxcX1N4hJU meRH2awm6WjNICb7D5PKr8QvgI4HYqc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=eDZyM7UO; spf=pass (imf24.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671476370; a=rsa-sha256; cv=none; b=XCZt7jSIoe/mKR3l+fzfYFCUCGja572/8BOl108UqGivUtwFi7JRBzY6S/h/RwKKxxXzqs CmfL9OQ2jR/Yjjgntl5Cay+3H4Qu8E7A9avEqO11GbeSHrroC+FkNEPg39pPiz1Vt7lrRl sNlSPdSAsOlY5a4zaZi6099DpoAaWKw= Received: by mail-pl1-f169.google.com with SMTP id u7so1747890plq.11 for ; Mon, 19 Dec 2022 10:59:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=e8B2I+4NRvgsSq6ExVIS5zUbrcMf1l4UJ5x1/qJc8cA=; b=eDZyM7UOpN208tVAX+hBqxHyra/S+kQPzV2RhHjUpgkC9oo2h5Ktaz/fro6c2oe/PI AiE0zgUos7a8I423N7mG0KgJrZbDbZjAuWEzhLGcsc4EetSbBy1aMp8FeQIqHi1Hqxqn /mMWiYwsa4h1ySc2e1WYwy1U8XSH06HeDaI3chHznqgvFcCkUMVwJdUKuQPt2Pb1uiRc ZcO5tNFbF1z+vFDCInDRnW/UpVs4kNmOWUagDrXtr1sYk7b4I6IGW+5X49eK1ICfP6Yc 2Nt0p3JG5RSnqmvBmQNcIgP82uZ3RVr5b1D+Vl9i1cIFY/GPycw8aNT5PXlUOLbbE1in hFbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to: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=e8B2I+4NRvgsSq6ExVIS5zUbrcMf1l4UJ5x1/qJc8cA=; b=eQooeBdLbEGcC5rhIY1Ov0dRFW3lD4CZHNe7oCKqlepU9rMT8rMC95ZZMoIDSfi6I+ d3fdCBUkIRdrO/eS6rbiEbxNCaIqjQLMYT8kWmkHK4Kh0BIXA1sd6VOXKT3DFEZ/nYC+ qZvIvpxzHGQdyXxUxZTBRUxsN/7UyHVJkUVNhOsYTjNsIz1x3gFutD+K5ze2d+hWWfvf R8UBZ7TIvcBz8oVB/PmUgWvbvl3m+LdY4uIi8yoHRmPeQu2/nB5tvTCB5lkti9XKWOFw gdKCkqrL5b0XIzqy2LG0rGO8uAvEBdo/J/X0X6iY+WpJWETkcK7yeEM9bwpg6elyG3Ai 5dQg== X-Gm-Message-State: ANoB5pkJYCofHByb2za70hvXPfttQZ0GOdXxRQHrf68OA98s/IOIcULW JbV/hmbFyK2wp+93mQN0olJfWD7szh/oow== X-Google-Smtp-Source: AA0mqf5haLYTtDDLTVNr5GE8KrQCnl2ZPXRQYj++5TqGjcUqyeaN1eo2aDvP3ExoJcIc66C/EvYGTg== X-Received: by 2002:a17:903:2448:b0:189:f277:3830 with SMTP id l8-20020a170903244800b00189f2773830mr65253828pls.68.1671476368808; Mon, 19 Dec 2022 10:59:28 -0800 (PST) Received: from localhost.localdomain ([198.13.51.166]) by smtp.gmail.com with ESMTPSA id i6-20020a170902c94600b00189847cd4acsm7480790pla.237.2022.12.19.10.59.23 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 19 Dec 2022 10:59:28 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Miaohe Lin , David Hildenbrand , "Huang, Ying" , Hugh Dickins , Matthew Wilcox , Kairui Song Subject: [PATCH v2 3/4] swap: fold swap_ra_clamp_pfn into swap_ra_info Date: Tue, 20 Dec 2022 02:58:39 +0800 Message-Id: <20221219185840.25441-4-ryncsn@gmail.com> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20221219185840.25441-1-ryncsn@gmail.com> References: <20221219185840.25441-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 66B5218000F X-Stat-Signature: 3ioxf66aydr8ori6qpzjjnkof3c3m4ao X-Rspam-User: X-HE-Tag: 1671476370-329627 X-HE-Meta: U2FsdGVkX1+HwcJ8PtznEw7nY4knDo2XtCFBQcroU2LPJ5kNNT9ezTSSSMh/Sht/bbunBmLU5NjXX62+3BnaVYT30VWIyW8RBrOwNpEi1AaK6EzJAwxftiTXDLaNk9Ld0sAOd8lzIBtZiQZ783pwSlRzjMk35J7uDb1qMocgnpjLeaTPzcFqJ41ZsnE6sSE+hrBWkdDOsXmJ/yqM5WiDMkybtUcoyhtk0B1AWSfJCyOCfkZloYXyRadPsvBTtQQDJC4M38HCMWWF90fgnGX1/tO/g0MPgKLiRMrOEjbGvRSeL8iyPqZXYxAHOlszSGVjzsg0MgTjyYZljk2VENM8xGZ7y5DF3zt9Rac5bXJX+kKR46vSeBLH3lbIpLqzuIVxMVTiGtLVELYF/aVSKkOb65ov9QmGAPkMS8EEoDOga8VyLnsgA1fSI/gTRHFG6t+S8JD4C3LRansAzIDaBQ8zm4KBEzG/mINpV/9vq/hUlBfU06LY+VPGu5HyM40F374y0txZxxtgN2QyKU8gb/JK/8CO8106IsrWUh0B76JBbZZ9dI8Vzv3KuR7vN4hTCldsNQhJ2HhHKZ8OgVTQSVBaB6uMT2AN7AmifFzS0GNmWTYPtbvJEh42m7Dh4XrFix7+q0fsYu5B/vDVCxq6UNhEQAqqLaWUTn0wBeL9P3VcbmiOzR6o1QXCQ27akmDWupyKNJUu7hbFA8hIUX2rUb28srEYMi7x5oCschUuEZM8Rv+Qi0f6r1sktBEmFCVp3kM7Io29yPeuQ0Fn9wtduF78iKd0XuWogBwPTxHPYYo2WiKfVAdDJJ0II3VvrXcDMLqZFzQIK2yjf9upWEMBplByR/Nvq70DCDcTp3G5NmnpeXBbbCw9pkPsG7I0ZnH4MxoRWnljHqdfxMCqME+xK3uSaocOjV2qXD5ZIGtOU+1t79FI0l8MI7u627v0nZ4b8jg42kSC48u/w/qgVSg6+o2 g2Pq3yTO 6a7g5AATpDnK7gaHvXIdQdJphMf3oeJt/t7RkPwYGciqAbXbw2JaDULGQiHAzu1Wk1VKFR3c6foDycLEmhw47CMGIFr9uPIPrm138z/YTZMSpMH+mkmq/jCzAUEBxQ8VXNAs3ybcw8POhSm1iuWaBCmVzNMVZgTebBg53MnGQQR5IQe57yO1QKP8Pc7L/BsPsLRzthCui/k2KebDqde8gTuQO/QZec/w4oSZ1NTdR8sBTll23U3sk6a6/MlO7deIA/D0dGRvHvPkHyyFXQ1vuU8xPE9gdTohmabR/4ikr0H5Egsi6BdAMRmA7Z68+xyyUVIWoFo+Wism0TJ0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Kairui Song This make the code cleaner. This helper is made of only two line of self explanational code and not reused anywhere else. And this actually make the compiled object smaller by a bit. bloat-o-meter results on x86_64 of mm/swap_state.o: add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-35 (-35) Function old new delta swap_ra_info.constprop 512 477 -35 Total: Before=8388, After=8353, chg -0.42% Signed-off-by: Kairui Song Reviewed-by: "Huang, Ying" --- mm/swap_state.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index af8bc123b7c4..d8d171195a3a 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -693,28 +693,15 @@ void exit_swap_address_space(unsigned int type) swapper_spaces[type] = NULL; } -static inline void swap_ra_clamp_pfn(struct vm_area_struct *vma, - unsigned long faddr, - unsigned long lpfn, - unsigned long rpfn, - unsigned long *start, - unsigned long *end) -{ - *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)); -} - static void swap_ra_info(struct vm_fault *vmf, - struct vma_swap_readahead *ra_info) + struct vma_swap_readahead *ra_info) { struct vm_area_struct *vma = vmf->vma; unsigned long ra_val; - unsigned long faddr, pfn, fpfn; + unsigned long faddr, pfn, fpfn, lpfn, rpfn; unsigned long start, end; pte_t *pte, *orig_pte; - unsigned int max_win, hits, prev_win, win, left; + unsigned int max_win, hits, prev_win, win; #ifndef CONFIG_64BIT pte_t *tpte; #endif @@ -742,16 +729,23 @@ static void swap_ra_info(struct vm_fault *vmf, /* Copy the PTEs because the page table may be unmapped */ orig_pte = pte = pte_offset_map(vmf->pmd, faddr); - if (fpfn == pfn + 1) - swap_ra_clamp_pfn(vma, faddr, fpfn, fpfn + win, &start, &end); - else if (pfn == fpfn + 1) - swap_ra_clamp_pfn(vma, faddr, fpfn - win + 1, fpfn + 1, - &start, &end); - else { - left = (win - 1) / 2; - swap_ra_clamp_pfn(vma, faddr, fpfn - left, fpfn + win - left, - &start, &end); + 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; } + 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)); + ra_info->nr_pte = end - start; ra_info->offset = fpfn - start; pte -= ra_info->offset; From patchwork Mon Dec 19 18:58:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13077057 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 F1AB5C4332F for ; Mon, 19 Dec 2022 18:59:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 93D058E0003; Mon, 19 Dec 2022 13:59:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EDEB8E0001; Mon, 19 Dec 2022 13:59:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B5228E0003; Mon, 19 Dec 2022 13:59:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6C18F8E0001 for ; Mon, 19 Dec 2022 13:59:38 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3B4FC120AB4 for ; Mon, 19 Dec 2022 18:59:38 +0000 (UTC) X-FDA: 80259969636.29.C40442B Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf25.hostedemail.com (Postfix) with ESMTP id 8ED8FA0009 for ; Mon, 19 Dec 2022 18:59:36 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ODbh7AHI; spf=pass (imf25.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671476376; h=from:from:sender:reply-to: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=YXfRuD0XfIija2Fu4Zh5yh7fZwHxF8sos3x+jWEicXs=; b=tkjPM5Puou05TV7CelwJYNtzLIfSyROrhXo1QI6uwWzYTT7rd8CocCHWa8VYrfYq/XZUIk HbFN4aBwCSAd9lzaoQ99uLfLCVz431GObWv5aCg3lFvjQDCnzE1uCITUumYEdD3iTAD9mw TLZ7F+NKwA1j+3G4MgrSzaMIYn5+YSA= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ODbh7AHI; spf=pass (imf25.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671476376; a=rsa-sha256; cv=none; b=PubQOad1UpXnNWuOx3M9uAoEUfy8MWwu4jxydALcJOO3TgcSQKiVSTNKtk4se7FsA3YxYw XxEI2R7W51mqEKvisGX57SxldJWy1EG3y981bbxTKy2jmSsb960T295DUH1f9ijSn4qOLO RwYQ6LrkTukcNzss+Vp8PIKhpQXTDCU= Received: by mail-pl1-f175.google.com with SMTP id 17so9995786pll.0 for ; Mon, 19 Dec 2022 10:59:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=YXfRuD0XfIija2Fu4Zh5yh7fZwHxF8sos3x+jWEicXs=; b=ODbh7AHIyVlbnpVquiBqy8uEqHmm0QDGOQpUJwJz/fbNOgW3c0R/3yhPHQnjBkR/8r tnh0wWulPnf0Wj3awEx8ZPQPMrdKzjq7pV7sN2bCjtjdIQ/LcSbHqSmXvPDtBY+lnfir 7uJGSab0IG9Tx8xG9I7eas8PxE+cPMOmkqpBUFsyjEFMrvfNppiutfoNT7Z/HGBY3IkU alKfJUqOrRLJNjr4lKh3oxDJmmEo59BuF8lOFQlnhMTfyVNZzqAoC3bNX4b+p/FrF92G GWZVZNyfjzrwVX1w9NsKXgGt0W/3iz6L6SvXShaG9dlAeQjPraoMq6bGI2EO7PUtLLRc 4gDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to: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=YXfRuD0XfIija2Fu4Zh5yh7fZwHxF8sos3x+jWEicXs=; b=DWMh43TnMecBUvUiWw5MSPPuMAEIwYh5nlu2HHKpMiR4eL34VrAxFv3FOZPWZqc+9H 2cZNpHx7gSWioxYl5qkNpUoPXmY62rOcrFFO5m5xNWSW/DoJ5VX9hxHPKr0XbQS48ukh AYDw06eqNzJBwnMKYmJDofJskhfQD07ky9Hoofqem/TBS6hodDQJxKB8p4UbIP/udbEB eP02yoDGecgmyiu/Wv9xuNB/6h9YWlWwzhhP/ztom6XTkxvJyM+FjUGc/4kGtdx/lw+c TAVPqKi15FpASzgYKbFtqbOAn+39r6KfOCg/reV7Xi036UGn4+zc8dbroCEvQSlr1i6p h4wA== X-Gm-Message-State: AFqh2ko+6uvASNczD2knt1p65xWQzZU0slUlXedyuRLTS0OTbXLbTYRW OmFMbY/RpHD4AUEH5MZYsDuQ7fQxAJCDow== X-Google-Smtp-Source: AMrXdXulOr2VKgdwgb5ddyFY81acDgesX38ZIxXMTqNqQO11JCVLUIZ2yxFN1GN1A2WIBZRP+TzUWQ== X-Received: by 2002:a17:903:2154:b0:189:5a49:36c3 with SMTP id s20-20020a170903215400b001895a4936c3mr9539682ple.0.1671476374965; Mon, 19 Dec 2022 10:59:34 -0800 (PST) Received: from localhost.localdomain ([198.13.51.166]) by smtp.gmail.com with ESMTPSA id i6-20020a170902c94600b00189847cd4acsm7480790pla.237.2022.12.19.10.59.29 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 19 Dec 2022 10:59:34 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Miaohe Lin , David Hildenbrand , "Huang, Ying" , Hugh Dickins , Matthew Wilcox , Kairui Song Subject: [PATCH v2 4/4] swap: avoid holding swap reference in swap_cache_get_folio Date: Tue, 20 Dec 2022 02:58:40 +0800 Message-Id: <20221219185840.25441-5-ryncsn@gmail.com> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20221219185840.25441-1-ryncsn@gmail.com> References: <20221219185840.25441-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8ED8FA0009 X-Stat-Signature: sr8t9a97obcq4sqfug96sty7no91cxhr X-Rspam-User: X-HE-Tag: 1671476376-4955 X-HE-Meta: U2FsdGVkX1+m940osqql0RdVUKSlIQb301VGeY/fXo2RAUr2TXIOngCupeh+FFSAwJE8miQTL8KkmlVfBVfNqxtVxLrwmEkQAMy68SzKRyyXrF8n3qGOw72Ys5t4FhTXMM+41mUMUA3Oh1Z1XtL3ZS9I4CpQBBSYYA9IbVH7RcWpU5aMMlxdpTg1pUrTrJDOOksA9hwkxOzsXYWzEsipSIf7wnDn2LkYevT78/Sg0Cj+uK0ilX+NN+45y1cf/7OeZzoSpAZB0qnTAFefAdpolq1KDV7QWK0D1SMz4Jm5C/KAaKoUvLKsOy4JnrPihLCX/E4gaGwTezs2bVL7n1hSJn2qHexTI0DUdMDVKL7o4vrrSW8SoUba3D1bKcRqbhMk0eqN282janqh+w1rzc16qXRpmnCBaHKf++ybfSPWpalJYIztrlR5SYGW1DOioafhpYqzFcn264lIbG1PeWl/AMI3kzNQ732M7KQn9asU0/DvDViqqYmjiCVI5TylEQh1KCif9ctUSUQB857jhj+Y/amxU6T6aOfd+VFd8YV/SyW44eHzzUtcRqZAXFQOew6LMEOyqh0zZi1FqwSGgG8ulMMt2MXqlzrvpPH0QSh6YuFbYLYEi725g1WCvMDGfilmxPS6oznPLUbN6LC2J8rCDieOPHTnMCD7uz8MX08FR+1UCpNd15V7k36G3l69FsyqfbD30f3TxaOG/Pnq43Ub93lh+xe75Oh6Zhmv99T5Dp3kuxGxkqpGdXjHLdpMpUOsVxUHMnC51StvnSBh98ZhgFL28g18NfPsmDcVi3H4PjvCZRBlrNEuEUHKY03Be7l9alTPeLfnDLzPCo1qftpa1DPEWVihAAVOvmiLJoBVuUdQnez1r3DvZDSJT6Grdi6vJAPJD29Pz6OXnO08tzjovlOXDd0tIRtCDfIX1IPcUkZ/FZXjXsH3UOcE+DDDiNA6vCiSh38zZ/RU9/xaMtp mFk1QCkV dQLtvrbG3c8L7AqEyBh45gBoat518QtVvlpzbvckTt3kKJAGFSuvcvelrsls6k9yfPGGpVo4JUGDuvGz/RdvhqSsXTgJS6X0oV82Ljk51wmuCD6/UDninNNKEJOE+hgUMWGk7l5uWuPY5ovEnBWD8nGIY0Sa70hyXmI+r49Xjudy/GgvFcKCBaO7DCuJxSMmFjaEE/h8+08tsOcFYMmrDuiGLm/WYzyQfViBdSQLP0n0zl/xj2oLsx0uumzbEJB9Jqf/XPU0zed6Lz3I0Sa/ITs0lTFprASetxC6zNodzk4SBDHnl4NveRssG67OtKNIVsQGNzfk5AB9UuCY= 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: From: Kairui Song All its callers either already hold a reference to, or lock the swap device while calling this function. There is only one exception in shmem_swapin_folio, just make this caller also hold a reference of the swap device, so this helper can be simplified and saves a few cycles. This also provides finer control of error handling in shmem_swapin_folio, on race (with swap off), it can just try again. For invalid swap entry, it can fail with a proper error code. Signed-off-by: Kairui Song --- mm/shmem.c | 11 +++++++++++ mm/swap_state.c | 8 ++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index c301487be5fb..5bdf7298d494 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1735,6 +1735,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, struct address_space *mapping = inode->i_mapping; struct shmem_inode_info *info = SHMEM_I(inode); struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; + struct swap_info_struct *si; struct folio *folio = NULL; swp_entry_t swap; int error; @@ -1746,6 +1747,14 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, if (is_swapin_error_entry(swap)) return -EIO; + si = get_swap_device(swap); + if (!si) { + if (!shmem_confirm_swap(mapping, index, swap)) + return -EEXIST; + else + return -EINVAL; + } + /* Look it up and read it in.. */ folio = swap_cache_get_folio(swap, NULL, 0); if (!folio) { @@ -1806,6 +1815,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, delete_from_swap_cache(folio); folio_mark_dirty(folio); swap_free(swap); + put_swap_device(si); *foliop = folio; return 0; @@ -1819,6 +1829,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, folio_unlock(folio); folio_put(folio); } + put_swap_device(si); return error; } diff --git a/mm/swap_state.c b/mm/swap_state.c index d8d171195a3a..cb9aaa00951d 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -321,19 +321,15 @@ static inline bool swap_use_vma_readahead(void) * unlocked and with its refcount incremented - we rely on the kernel * lock getting page table operations atomic even if we drop the folio * lock before returning. + * + * Caller must lock the swap device or hold a reference to keep it valid. */ struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr) { struct folio *folio; - struct swap_info_struct *si; - si = get_swap_device(entry); - if (!si) - return NULL; folio = filemap_get_folio(swap_address_space(entry), swp_offset(entry)); - put_swap_device(si); - if (folio) { bool vma_ra = swap_use_vma_readahead(); bool readahead;