From patchwork Thu Dec 8 18:02:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13068646 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 4A860C001B2 for ; Thu, 8 Dec 2022 18:03:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0D758E0008; Thu, 8 Dec 2022 13:03:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D96B28E0001; Thu, 8 Dec 2022 13:03:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C37798E0008; Thu, 8 Dec 2022 13:03:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id ADF158E0001 for ; Thu, 8 Dec 2022 13:03:35 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 730BA1A062E for ; Thu, 8 Dec 2022 18:03:35 +0000 (UTC) X-FDA: 80219911590.14.0CE2453 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf07.hostedemail.com (Postfix) with ESMTP id BEF314001A for ; Thu, 8 Dec 2022 18:03:33 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=fYS6yck+; spf=pass (imf07.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.41 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=1670522613; 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=9xhnDKrjvRgwnAVDsuEDmotQGE1PCbtaJeEpVqamujQ=; b=uHqlkUkolullwyxzj8ioGhoPNos7CPL7e3axu+MnUruvq5tmhrZql/vM1AfRDEBYKZ/UtB 1Q+c2KA5VdJtwkatmHvnXyufY7r78C8yXYIHP069/HlEj2QmvTjVcpzLFkCkZNvfkJe9Aw RWMvI3u/uX25N7Tx0u6g87iYCDLbitc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=fYS6yck+; spf=pass (imf07.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.41 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=1670522613; a=rsa-sha256; cv=none; b=Qaa8Jh6mdlXGloLCKVW5CviwuIdSrVgCoQDD1qpIfWdvnjspdyXXNyOnhrwRGPLp0oqdUs ipQVEO1lTyxIT3SorVz/2wHetdYs5QoVQETCbz7t/85upR0Oh7QFqruyuiuiXoxynz/yoS 2LJQP3+ls0z1U/Ss9kGjX7j48R/WecM= Received: by mail-pj1-f41.google.com with SMTP id fa4-20020a17090af0c400b002198d1328a0so6529966pjb.0 for ; Thu, 08 Dec 2022 10:03:33 -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=9xhnDKrjvRgwnAVDsuEDmotQGE1PCbtaJeEpVqamujQ=; b=fYS6yck+ReWd2uxK6tV27Mwp2zNKPzSSjv+tEjGFN0KC2KzjO1Lrmvll2fXcklu/xD 9vsO4te8pgwd8ikSvVhnd3cwQx6jAaJ+vsCjpJpWAcPWuMpTh4kMHQ9/dylSB1sgEc4o boBlj0P69dZgpYTOUuCtE/bC1caINvJKSvgBOFJhQlKFahGnFN6IKytmc7ep9fmjJmPD 3aA6IRu+YJpaJSgbmFX3HXbLKCae+EEYKDBMiBDanJtFjy9xSdATtmt6iO7JXOaXc9+c EalfcbAgPYUi2n4VnkKdxQR6xvSx4sNswYuOd7MIrzR2KOOO13g8om7jQ1QjQuO11Lmi /Jjg== 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=9xhnDKrjvRgwnAVDsuEDmotQGE1PCbtaJeEpVqamujQ=; b=KnPnfXDTtqnhvu/RZfCOm30RBNjq7qfVoXpmqiwPEk80to7iuudJ8zyFvxFhlRNopA 8nU9skTBAd2CQYNHwWkzUp6/h9XDWk/JiorONH02sNN2th5TpC4nojYuwaRrveIKMboZ Pn8qbyJTRfpIxLyXs/HlIUbcFRZosiPtqaMolp/1YAvRtOeBZFhz77VdsAC2KffCIgTo wHdPtJV4TPf0osaNehaASWYHw29g2ZZnQPW3PN3tCDEepeUcaJ5jy33SftPxIXbNAIus ZOFrP98q5byHxL6f67vI60utSdRhEbN3CsAFUczTeY2VHPzK7DABbUu+RmDgcllkVk4X odkw== X-Gm-Message-State: ANoB5pmnQnkmhZi8+wqm4yybp46VazfKRBeo3B3B2j/VjMVyc3B7n8S8 vxYtFrTgFJg44lOUSSHuItnIk56DboOSMnan X-Google-Smtp-Source: AA0mqf40eA3n9hLVOhVvV4ReS4Fw/NzKyEAVdLHHp5a3pHjwNFExX1/Jxq1YuAjRsdgd/lCwVPtNjw== X-Received: by 2002:a05:6a20:2d29:b0:a3:ba8a:43ea with SMTP id g41-20020a056a202d2900b000a3ba8a43eamr4354489pzl.58.1670522612163; Thu, 08 Dec 2022 10:03:32 -0800 (PST) Received: from localhost.localdomain ([198.13.51.166]) by smtp.gmail.com with ESMTPSA id x23-20020a63db57000000b004785e505bcdsm13377909pgi.51.2022.12.08.10.03.27 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Dec 2022 10:03:31 -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 , Kairui Song Subject: [PATCH 4/5] swap: remove the swap lock in swap_cache_get_folio Date: Fri, 9 Dec 2022 02:02:08 +0800 Message-Id: <20221208180209.50845-5-ryncsn@gmail.com> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20221208180209.50845-1-ryncsn@gmail.com> References: <20221208180209.50845-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: BEF314001A X-Rspam-User: X-Stat-Signature: w75zx595dtryrxtq4qhe35fudgdgxfmw X-HE-Tag: 1670522613-726538 X-HE-Meta: U2FsdGVkX1/XKQlAV99Pq3rqvImWu42P1aSjfYj1d6sZqz9RptBR+Dmf9BeZhwYMIxiKkwEeDQYZcXcu1A9hptoMGvruWISJzmVI1jeItBoij4u1iRm0h/wfRqsSc6c+UTSfEn4DSiyeT58fvc1uN/3Yt4ZXo6p3Vl6VJjbpdv92il29oS77ny17bpfg60wuZK5tggeHnDabw0Ey1LPtarrD3d59ipJnZ8D4IElOhIW+CJAdiH1BrpfKV/wS95DSSqWTeTyNHVMvfgNbHhx+9IBtN6iHHgvF2UCBPu7azMEyRSm0ZvtcNviwKAe3+Cu7rbnRUXHsDBE0Ae1zHxAzg6QpQZnlGVppBqU3IQY3h5+3i3/jno3Qg2UXd2EjdXmNChiXb8ZMdJAR1CXi/gX8BlSmA1AJBjGrussDJqjOytKx8KvDvDF92fjkBqvTQt1CTptyerzYDgMTY/SKoULQaaMz2aKuG2znVDUO7KaOA+0U2zAmMKQhg3pv5pb60mgQjHSt63OJO6T+PgpKN16hE5LSgpNxpNAUmTlfOs+laHN2wEto0Db5VOMmEFfKjhZ7895AxD3Ek/qFSKIa9YJNIF74ZDLZZ72+g0Lp6ngA28ttKKvkak94kf8ctPCWUKG+xmAbwKEg1TjTN/++19xX+QJKxEQjNP/AI33dhiVJUGYsSgZhFh9fG8H9fBOO7le6QB5msG4I60jTT65SuzcxNHLPm2pyttJe3Z6O+v45Al9oxOdPHzLeglsGM1Ogmm3fUvN4AX8wtRdFLYqqjLjdYxKlJJDqg7cee9EHtYVkcKIDeCFIAXMH1H2C0CltFkddZJWVP/8iKNtQlM/IUTrkV4EiZ6ngFwGR2GSVgnKHuH0gBLsmxE7kY44uETb6dhn2Y6Lo2G5dshon4xzJ+WGykar5Olpz3KmuXbpS32XXCtuVTv+DccbChJplsEg4GnhvhYpdpf51nkF5js0UU2J xN6aThAQ KtZ65BfqQk6/pPvM5bcotdt69lGKIVDfZZ+yZJcXrcz/KZ2+vUbW4aEtu68YP538N1SCJRiCbeNar3KqiigxW3WbrxCuaUIj4TwoMlGB89u1xiUUi/kGyH/3jLIMeJ7cuv1UJM49eD2J4xVEdOcLT90g57nJD5F0TXmTwcL6oYSXArNwSVZSCCkoNVKd4ngPUgRpXWS1u1p+op3QsL4HnKRs5y33mo5N/LjHZguF8qBbVWXEBsHSyrhJDvs4hsCIPWJts96BcjeiOrw7cUZZ22bZYhcZU7YeinNd5khdYLk3ZVfXr16zYEK8pi+buPkIvdmUB 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 There is only one caller not keep holding a reference or lock the swap device while calling this function. Just move the lock out of this function, it only used to prevent swapoff, and this helper function is very short so there is no performance regression issue. Help saves a few cycles. Signed-off-by: Kairui Song --- mm/shmem.c | 8 +++++++- mm/swap_state.c | 8 ++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index c1d8b8a1aa3b..0183b6678270 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1725,6 +1725,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; @@ -1737,7 +1738,12 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, return -EIO; /* Look it up and read it in.. */ - folio = swap_cache_get_folio(swap, NULL, 0); + si = get_swap_device(swap); + if (si) { + folio = swap_cache_get_folio(swap, NULL, 0); + put_swap_device(si); + } + if (!folio) { /* Or update major stats only when swapin succeeds?? */ if (fault_type) { diff --git a/mm/swap_state.c b/mm/swap_state.c index 19089417abd1..eba388f67741 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -324,19 +324,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;