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;