From patchwork Sun Nov 19 19:47:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13460672 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 F3FFCC072A2 for ; Sun, 19 Nov 2023 19:48:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 865BE6B03B7; Sun, 19 Nov 2023 14:48:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 813AF6B03B9; Sun, 19 Nov 2023 14:48:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 666556B03BA; Sun, 19 Nov 2023 14:48:57 -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 523966B03B7 for ; Sun, 19 Nov 2023 14:48:57 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2EBBB1CB22E for ; Sun, 19 Nov 2023 19:48:57 +0000 (UTC) X-FDA: 81475741914.23.51ED7D3 Received: from mail-il1-f171.google.com (mail-il1-f171.google.com [209.85.166.171]) by imf07.hostedemail.com (Postfix) with ESMTP id 5B72B4000E for ; Sun, 19 Nov 2023 19:48:55 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JqretcdF; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf07.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.166.171 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700423335; 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=9XjpTi/p1jP45iq6KrQ/NXIY9Wo9hQ4YnGSNNoLyAOA=; b=D/X2c+iGLbdHg0i6ZttdRQgVDa5gwppvWhTt0XkCfq21wrupWjxWDb3RAq4Erj9f0eVyTh mKtMa5drz7qVt1aquELcdF3bXiq56X7r6u3V+wowM6AogfZ4FITIiBogo9xxINVJanCHRT pxCjlWyixjDvJLvXwrrTsHqweClPVU0= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JqretcdF; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf07.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.166.171 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700423335; a=rsa-sha256; cv=none; b=ybRQM/dxFNKOkMvR93lT+QSNEPKOQCMAGvKvM/9uHfMHp/4esSlXG2uncyZR3C/5780vjg V5/FLMwRDIS1WeBscdx2uxMFC+XWw+cqjkdPQ41kf+Je66v3UbM+EUQIX+SsltGrW8oXtF dj4TcbaQMzUNSVlqWx7xH2l/+Q2jZE0= Received: by mail-il1-f171.google.com with SMTP id e9e14a558f8ab-3574f99d236so14084335ab.2 for ; Sun, 19 Nov 2023 11:48:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700423333; x=1701028133; darn=kvack.org; 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=9XjpTi/p1jP45iq6KrQ/NXIY9Wo9hQ4YnGSNNoLyAOA=; b=JqretcdFCbd26sLOZwSm9C947vWmoV4Hr03eI+coYAnOOQ9gw4wl9YXgonzVXkFPYY htW8l7Z9xNWTwSjv6RgKdzUsoFYdpT9Gc48A510Ff05fDtmR5LM0aXfSKPYrnPBmzG1T wnH6cmOzpU+Yc56WaL6jtGWQ8atrJyPjb4Oo20fjpWKSbiwTLUMosFi9GugbvgYWbUB7 4v8oeNkSIwlpvlP7WzI0sbba1lHLYt6aIcy10bjPChdKTDAcsNOmYEAM1PPr+7ka+NkF ux8RrYZXbJJvvf81fx06WvzXYaZeVu61WH/Kt/fN0WCPRIQmsmM1gWTX6usiTPrYiYJO EBlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700423333; x=1701028133; 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=9XjpTi/p1jP45iq6KrQ/NXIY9Wo9hQ4YnGSNNoLyAOA=; b=DY5MleMDRgV3dM/A/JX9lMbXPI/hVxIHT80QoItsi1FhtibxrOfpyw+5//SQcS4Wdf /MqX7Tdtyi4Tn3HU7npTNFjd2Jz2FCTcAkFEKP3fy04aIiW/RHYnpzu8OLNF1HrArgpc ZwZortYIz6oihYJSzBp1Npz2dR0eojOTq+pF+BrB3DbwZ7ZGA+ju+YbRJQRyYDN5nPjW 6ifZK3esQKDDVBDydLuAPLg8GsUdnNNNEg84etZev79l+9t5zEXlRb3bLtHlZbEr5nqg V+71YRFDrq435sXNcOtEDgNbNe5kKgAnfz8YDv6o490KSPz6iV/m+NQjPGgfnzN8VF+J pL6Q== X-Gm-Message-State: AOJu0Yy6lvn/gBwVNIcccPn+7m2bgrPi8daM5HvRVVMBhqBGatnHvaN+ k0+Yd2WEDuD+DiCEsxS0YLl02YrMqC9oxssD X-Google-Smtp-Source: AGHT+IFar6VHQuFklPrfy7vOCI8aqHj+K8WskagViLl4BbbPZwTP7uc43B3VnuoR6l/B/N6mqJ6dzw== X-Received: by 2002:a92:7a06:0:b0:359:5389:c0dc with SMTP id v6-20020a927a06000000b003595389c0dcmr6674915ilc.7.1700423333536; Sun, 19 Nov 2023 11:48:53 -0800 (PST) Received: from KASONG-MB2.tencent.com ([115.171.40.79]) by smtp.gmail.com with ESMTPSA id a6-20020aa78646000000b006cb7feae74fsm1237140pfo.164.2023.11.19.11.48.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 19 Nov 2023 11:48:52 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , "Huang, Ying" , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH 16/24] mm/swap: reduce scope of get_swap_device in swapin path Date: Mon, 20 Nov 2023 03:47:32 +0800 Message-ID: <20231119194740.94101-17-ryncsn@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231119194740.94101-1-ryncsn@gmail.com> References: <20231119194740.94101-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: 5B72B4000E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: p79ui1urz1ghdwkjzb1kney8hhqpw8yc X-HE-Tag: 1700423335-293519 X-HE-Meta: U2FsdGVkX1/1lYOhELNETQulmwvHKhcN5cCC2g44XQXutCfbApJ9qPk2G/IRw2F99MSJFGRAbwwuM9FIZ/H8W5SzUheexvfkYB/jhO1UL1lI5MrdB0fsDCxguxJU0bo2Vyz/GXa6IGHiKIt7NbZL6+SlNkxeIiPRS2apGrdVgaPiF21upydbiuJbzzoyKft46hnPEDUNtdLvfl/3rSSM630zrhdx1/t7BbRSH7/qoQuUKdOPsM2bGHOrB/VaZv5eMpXmEUGfQWWLSnULJXa5HwroJFuPlnauM7yHgVVcpV3GFXv42+F0SlS/H5EC5iahEYaTTZaOfyrloNGdGY9pRuYEfUPOEZBmM3sIxAsMQHIOUUI/wm5b5oV60RjqqW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zaxj05jiMcNIoHFYYQyi7Qug84aFg3u7029343xKOKegF3kzTfGi1/6UodozUKLmJGg7U8+DSAFv8GY0RXbSya371rvQWh9JFhmwxlggohDHZ2/0TDF4Lf4WedRD4eo2UF3qYKwqaxwimc22Zxhv1yBTvGGFbbZX+4MNGx1tW1qW29fEIKy8+YdijFluRy/d6uiZyUcTOjNQ5zleoRP7XCwOZ3+FzIVAQs69N4E= 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: From: Kairui Song Move get_swap_device into swapin_readahead, simplify the code and prepare for follow up commits. For the later part in do_swap_page, using swp_swap_info directly is fine since in that context, the swap device is pinned by swapcache reference. Signed-off-by: Kairui Song --- mm/memory.c | 16 ++++------------ mm/swap_state.c | 8 ++++++-- mm/swapfile.c | 4 +++- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 22af9f3e8c75..e399b37ef395 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3789,7 +3789,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) struct folio *swapcache = NULL, *folio = NULL; enum swap_cache_result cache_result; struct page *page; - struct swap_info_struct *si = NULL; rmap_t rmap_flags = RMAP_NONE; bool exclusive = false; swp_entry_t entry; @@ -3845,14 +3844,11 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out; } - /* Prevent swapoff from happening to us. */ - si = get_swap_device(entry); - if (unlikely(!si)) - goto out; - page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf, &cache_result); - if (page) { + if (PTR_ERR(page) == -EBUSY) { + goto out; + } else if (page) { folio = page_folio(page); if (cache_result != SWAP_CACHE_HIT) { /* Had to read the page from swap area: Major fault */ @@ -3964,7 +3960,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) */ exclusive = true; } else if (exclusive && folio_test_writeback(folio) && - data_race(si->flags & SWP_STABLE_WRITES)) { + (swp_swap_info(entry)->flags & SWP_STABLE_WRITES)) { /* * This is tricky: not all swap backends support * concurrent page modifications while under writeback. @@ -4068,8 +4064,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); out: - if (si) - put_swap_device(si); return ret; out_nomap: if (vmf->pte) @@ -4082,8 +4076,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_unlock(swapcache); folio_put(swapcache); } - if (si) - put_swap_device(si); return ret; } diff --git a/mm/swap_state.c b/mm/swap_state.c index 51de2a0412df..ff8a166603d0 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -922,6 +922,11 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, struct page *page; pgoff_t ilx; + /* Prevent swapoff from happening to us */ + si = get_swap_device(entry); + if (unlikely(!si)) + return ERR_PTR(-EBUSY); + folio = swap_cache_get_folio(entry, vmf, &shadow); if (folio) { page = folio_file_page(folio, swp_offset(entry)); @@ -929,7 +934,6 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, goto done; } - si = swp_swap_info(entry); mpol = get_vma_policy(vmf->vma, vmf->address, 0, &ilx); if (swap_use_no_readahead(si, swp_offset(entry))) { page = swapin_no_readahead(entry, gfp_mask, mpol, ilx, vmf->vma->vm_mm); @@ -944,8 +948,8 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, cache_result = SWAP_CACHE_MISS; } mpol_cond_put(mpol); - done: + put_swap_device(si); if (result) *result = cache_result; diff --git a/mm/swapfile.c b/mm/swapfile.c index b6d57fff5e21..925ad92486a4 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1857,7 +1857,9 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, pte = NULL; page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, &vmf, NULL); - if (page) + if (IS_ERR(page)) + return PTR_ERR(page); + else if (page) folio = page_folio(page); if (!folio) { /*