Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1428543rwb; Thu, 8 Dec 2022 10:29:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf6B+JZNaEcEH4AYu0xniNeRdOx9f99QnaZ8kNHKinROEa9qsLybCLPVCGTqXY0/TLvKhwB3 X-Received: by 2002:a05:6402:142:b0:461:7fe6:9ea7 with SMTP id s2-20020a056402014200b004617fe69ea7mr4285715edu.94.1670524148927; Thu, 08 Dec 2022 10:29:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670524148; cv=none; d=google.com; s=arc-20160816; b=LUM/IaJLRzJ4qNOcKQ5NRNS4bEkYaz+N2aHDywdmhjWNBEGaifwgEi9hF9Ij7tMLl2 Ga4gfJ3ai0dJ8maSHvQhnecW3BhiKAbUG4isTpMvxoO2OaD0BH+k/hqOuRtswi0+gXkz jam02SuJHTgVCVsO9AE0MWHL9EqDQZMehA0md0n2GIpdB2o2IgdmMQZI17zb875DaGK9 HD5sa1O+nshld+WRaNibBan72iCaAj6vsI5NXQ/KxbOLeetebnD7T9z33X7quYjKnLnx Nm6DieT9YGpyveYBCDQg1RcOugacMYdDfThxp1LFPq6nVoklV0xGtkD2N39w7nNiXoJE m0Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version:reply-to :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9xhnDKrjvRgwnAVDsuEDmotQGE1PCbtaJeEpVqamujQ=; b=BdFUGxJ6DqEH7kiF3SnfxaV7qz9EI+uqC/MPPDbATm5K5KLFHqc0uvyAYJKjFn0CXH PkaeZeLPsQpf5iXVqmlDwxh0lqQmufzuKcbBTD0CgA6KyFyIK8EfefPce48gaD3ZwIm/ wwgO3Tdlm08HJuT6EDIpU8Ym0Tu4MkEwzt1C/sGarRzHhCQ19R0eP/6AVTydlhoSeT7u FknJIWQ/GNidrq/oELyPoJStHZvJu+VoHa036endhvIeSiu7OAgy41nrzgJtsV1coeLW 6/vCt9Nu6/No6oqrP6HXccpYmZO7FelcSUqWLd7QmAPX4n2bGxGHOwRhHFHNAGHSbDkU 7/IQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=fYS6yck+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hr10-20020a1709073f8a00b007c0fa820f08si3029708ejc.203.2022.12.08.10.28.51; Thu, 08 Dec 2022 10:29:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=fYS6yck+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229954AbiLHSDt (ORCPT + 72 others); Thu, 8 Dec 2022 13:03:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229948AbiLHSDf (ORCPT ); Thu, 8 Dec 2022 13:03:35 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD8B1AD99D for ; Thu, 8 Dec 2022 10:03:32 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id y4so2276709plb.2 for ; Thu, 08 Dec 2022 10:03:32 -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=0p5DgSfURVcPNlhHE7WBVAI9QDd2gexfawfrqC6/rM4QIcoe7iHNWUYahJ+tF8wsqP ZhK08EfMWQei/UelzFXalXzMfnDOXHsQn5rgcgPby3bRq9zGzoUvzJApJmjW4Mvnrhpo hBMaeMvDXbIIMm6SymfmPCnWVSAGL6CmIORqt5Pu4cuEY0p20/YK1qYs+lc0d4LLFF2r 2yCaX89nKjiEVkJnv9Mar3NNkDVB9obGw5bkdQ9D3fQMh9I0ZChDR1vJVSiwrWa+m1MH PGIt2lxerxriMRoBBskhx7SZqSd6zy8cjo9oBTHt5lvW0uf3BCHhGf+wa9UJyOs/H0wn Cgbg== X-Gm-Message-State: ANoB5pnFWgvyPhNxFpkndM9xYyQHMEibp9NoY62tQOCm2V0bYy9472mg Ro/r6A1LnPXOVOXddaMSjEw= 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.35.2