From patchwork Mon Aug 12 07:42:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13760275 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 052EDC52D7C for ; Mon, 12 Aug 2024 07:42:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFE4B6B00A8; Mon, 12 Aug 2024 03:42:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A7FB06B00AB; Mon, 12 Aug 2024 03:42:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 855646B00A8; Mon, 12 Aug 2024 03:42:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 580076B00AA for ; Mon, 12 Aug 2024 03:42:32 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0D1991A08AD for ; Mon, 12 Aug 2024 07:42:32 +0000 (UTC) X-FDA: 82442800944.24.2BACE08 Received: from out30-97.freemail.mail.aliyun.com (out30-97.freemail.mail.aliyun.com [115.124.30.97]) by imf02.hostedemail.com (Postfix) with ESMTP id CE2718001C for ; Mon, 12 Aug 2024 07:42:29 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=sDCzgibt; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf02.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.97 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723448539; a=rsa-sha256; cv=none; b=USySwocakmTOCN13HiHALj5bcdSselJxZ+HfeLeXSgz1uIr+oRO/E7ywB8lNNwJc6b2Kra kzm8wPCLCV6r4cGNad5eXl4pjg+gEq+kS6knjPWyLjDpDV0hN057Oa9v8k4KrSGPgvNxXO ejDGFhyHGWoS/rWl5lhAHb6MKQ3fVxw= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=sDCzgibt; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf02.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.97 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723448539; h=from:from:sender: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=PZslh3xDjv8KO1NsIQv9gTqVJj0244TUIWHfHI4JVZY=; b=VkYU142QSrzBVMVUZsBq7lDAlrO5S1x4CayJB1Jc2JFMjQKXja/gWqJT/BYSVgM9KRPgkI fOEW1br57SYGjn/eEwTGepZPOHYivTdqNp4wrcpKhCWOAW/j5oRm3bCo2K+tOblrNQLIO4 srnj96lNsMewPFTEh8ZnY9vTcj3VmVk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1723448547; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=PZslh3xDjv8KO1NsIQv9gTqVJj0244TUIWHfHI4JVZY=; b=sDCzgibtglDth7yEb06ZF+JPV3a7P8kXlyjen9puTazzWqLXGurUo8JQLXMwluUBT2ZI2zlqp6a2wd2202r6sJhrj95PZg2es7ua5U+8+ZzeZALsl7bVluIDLiYwHcl9Z5vC4mBgPexIiw4XxvUHBXd3x3pUf1SgzJ2VMIFzXZs= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WCbU5vC_1723448545) by smtp.aliyun-inc.com; Mon, 12 Aug 2024 15:42:26 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, wangkefeng.wang@huawei.com, chrisl@kernel.org, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, ioworker0@gmail.com, da.gomez@samsung.com, p.raghav@samsung.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 8/9] mm: shmem: split large entry if the swapin folio is not large Date: Mon, 12 Aug 2024 15:42:09 +0800 Message-Id: <4a0f12f27c54a62eb4d9ca1265fed3a62531a63e.1723434324.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: CE2718001C X-Rspamd-Server: rspam01 X-Stat-Signature: jgoa4jzg5gz4z7kcf4pqnfwwqy937poe X-HE-Tag: 1723448549-348797 X-HE-Meta: U2FsdGVkX1/mpEDtY23zsYDp6yo5C8zeNMXSzSJpOlT2hDR5sOr1mEjiTbd/8CyBxrl2ajUKsXi/+HE7H92n5Is+iCF7bIzRFJzBZ3dbC0UJrGDsufmzyN+2OgFTvdxxViONwQWp3pmUdR0JYu5X2fGPiQXyL19hCP5ML4UKQbsV87xKKkpDDRLzAa62W3Kx4GJszpTHkpk5BW16upOK1/H/eEeky017OI5wBNBM26wktCQh/8GxFylY6BMkDZ0dwKH/rrNW1xWqVic1pvRLNMJRxUIwZlK7wxUf3g8EzU8FIBxsx0iGpbIXzoyiBb3HOZ0t/BzCBfJaz2GEo7roB0WzD6ycKJft1LCH0tan+0G5rG4B8059EH3W1QDAaxa7eViwWwquRXgBGSN1szO+VtiLmMhoARJgL5amc3x8EnrAKi1iAAqFWf70bWmwHLQMpX/KSAOCXvxSArE8gFvrIPbUlyEwGQrDuaN1S1Dp01Sg19mOOycfQaMHJNOojV5BXbwXdu6bTr5bsCmBYgpg6PCL4IwlmSNW2sKmf7iz5PDYBXdFJm7O7VhANG1IykJZtGhKBG9op6+bZj9OCSpD4KnYiwOQps4JGSjJO3VzrJshQ/6EZQ8fR/ALaKggLm26zY02RBxuoPBC2NL5Io8NC4JWQGczm02AxRaCWvkhiiE05LZKGnUId6REqQGs+XgDnVZdslSUJBHo+TfSY0mjohGDejBJQF858C7bKPGNvc4EQWZ2+JyLVNhm8WA9GCewxE6QSDssMEM6ubFcpRMVZTrzN5SgVLEhR+KZhaBXXAYZbKoKpSXGTQ/pEd8OUHmRQl3wwKC/EpqtXn//5EyqwfIbNImal7H51RVVqCWj7biWim/W/VCXWkLjM8IGOhvU2neqgZspF5k4BpwwsRke3SSepzjrl1u/srdywsEaUZ3D3J/o2oGnAn/2GpZ/FpkWK1C3dY9UyzVy9qiw9ug A89UBa0o pMtHyUB53CIITdukXwzRPDXEMnmYPhXi4g6CEo1uq9YBxp6VdJhRQxg4BgbTuX0EDm/mFppM3Pk+ixFg/30HhBmX/N7BqB1xkSOiJxyzgwdRbpIylavUS7WWvGygQv+tVM77fk6gSq1FUqigBL3526ZVSt0RCDX5zYP7+0SXJ6yPTzKEVqnuSIZd1tmeQsAbNRGTRBsz0cd1Ae7Y= 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: Now the swap device can only swap-in order 0 folio, even though a large folio is swapped out. This requires us to split the large entry previously saved in the shmem pagecache to support the swap in of small folios. Signed-off-by: Baolin Wang Signed-off-by: Hugh Dickins Signed-off-by: Baolin Wang --- mm/shmem.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/mm/shmem.c b/mm/shmem.c index 345e25425e37..996062dc196b 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1990,6 +1990,81 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, swap_free_nr(swap, nr_pages); } +static int shmem_split_large_entry(struct inode *inode, pgoff_t index, + swp_entry_t swap, int new_order, gfp_t gfp) +{ + struct address_space *mapping = inode->i_mapping; + XA_STATE_ORDER(xas, &mapping->i_pages, index, new_order); + void *alloced_shadow = NULL; + int alloced_order = 0, i; + + for (;;) { + int order = -1, split_order = 0; + void *old = NULL; + + xas_lock_irq(&xas); + old = xas_load(&xas); + if (!xa_is_value(old) || swp_to_radix_entry(swap) != old) { + xas_set_err(&xas, -EEXIST); + goto unlock; + } + + order = xas_get_order(&xas); + + /* Swap entry may have changed before we re-acquire the lock */ + if (alloced_order && + (old != alloced_shadow || order != alloced_order)) { + xas_destroy(&xas); + alloced_order = 0; + } + + /* Try to split large swap entry in pagecache */ + if (order > 0 && order > new_order) { + if (!alloced_order) { + split_order = order; + goto unlock; + } + xas_split(&xas, old, order); + + /* + * Re-set the swap entry after splitting, and the swap + * offset of the original large entry must be continuous. + */ + for (i = 0; i < 1 << order; i += (1 << new_order)) { + pgoff_t aligned_index = round_down(index, 1 << order); + swp_entry_t tmp; + + tmp = swp_entry(swp_type(swap), swp_offset(swap) + i); + __xa_store(&mapping->i_pages, aligned_index + i, + swp_to_radix_entry(tmp), 0); + } + } + +unlock: + xas_unlock_irq(&xas); + + /* split needed, alloc here and retry. */ + if (split_order) { + xas_split_alloc(&xas, old, split_order, gfp); + if (xas_error(&xas)) + goto error; + alloced_shadow = old; + alloced_order = split_order; + xas_reset(&xas); + continue; + } + + if (!xas_nomem(&xas, gfp)) + break; + } + +error: + if (xas_error(&xas)) + return xas_error(&xas); + + return alloced_order; +} + /* * Swap in the folio pointed to by *foliop. * Caller has to make sure that *foliop contains a valid swapped folio. @@ -2026,12 +2101,37 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, /* Look it up and read it in.. */ folio = swap_cache_get_folio(swap, NULL, 0); if (!folio) { + int split_order; + /* Or update major stats only when swapin succeeds?? */ if (fault_type) { *fault_type |= VM_FAULT_MAJOR; count_vm_event(PGMAJFAULT); count_memcg_event_mm(fault_mm, PGMAJFAULT); } + + /* + * Now swap device can only swap in order 0 folio, then we + * should split the large swap entry stored in the pagecache + * if necessary. + */ + split_order = shmem_split_large_entry(inode, index, swap, 0, gfp); + if (split_order < 0) { + error = split_order; + goto failed; + } + + /* + * If the large swap entry has already been split, it is + * necessary to recalculate the new swap entry based on + * the old order alignment. + */ + if (split_order > 0) { + pgoff_t offset = index - round_down(index, 1 << split_order); + + swap = swp_entry(swp_type(swap), swp_offset(swap) + offset); + } + /* Here we actually start the io */ folio = shmem_swapin_cluster(swap, gfp, info, index); if (!folio) {