From patchwork Sun Sep 8 23:21:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13795689 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 5F916ECE577 for ; Sun, 8 Sep 2024 23:21:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5DC36B00EA; Sun, 8 Sep 2024 19:21:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE7016B00EC; Sun, 8 Sep 2024 19:21:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A393E6B00ED; Sun, 8 Sep 2024 19:21:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 827806B00EA for ; Sun, 8 Sep 2024 19:21:46 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 10C451A0A33 for ; Sun, 8 Sep 2024 23:21:46 +0000 (UTC) X-FDA: 82543145412.26.19BB777 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf24.hostedemail.com (Postfix) with ESMTP id 3906218001B for ; Sun, 8 Sep 2024 23:21:44 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=SBF1obHn; spf=pass (imf24.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=21cnbao@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=1725837570; 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=R2xC+tIjHn2wWvMR2T1OflcDy+oEzjuKFqq/trb+O1U=; b=RutU+ai8P6zwOtR9hCoiN3YM7og21ZgsbCbSjh/hWuQp7L9MgEY2Ai3f8S9Fryhn5lR/SX frk2oaWC3pIs54B/WmYuvfrKsccPzWMiou/eqeRdTzyeahWsn9c/X4c9cNfWjWH37EC3ZM iFTEfLtbtXn/vPHmaZ1nziUx5aNgpFM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=SBF1obHn; spf=pass (imf24.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725837570; a=rsa-sha256; cv=none; b=3tuM6N7Md+RZLzh33+cWBiu8QinvreRoPPoFqYJuWVZvPa6t8s/1ZlA9pOqVu4aYQGqJuA 46by8oZQ0s9lkaDApLbWWA31Qb1hfy8nmnZrvGRZqSVWi8aX7vK8pCz3kGZ6b4S3wk3h9W lAqtJOkyR6rDqXUnodZr7A3myyEsfis= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20696938f86so31433465ad.3 for ; Sun, 08 Sep 2024 16:21:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725837703; x=1726442503; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=R2xC+tIjHn2wWvMR2T1OflcDy+oEzjuKFqq/trb+O1U=; b=SBF1obHnsAhKR1FQy8TRWjwyLoN+nGB0vgq9oNZKVaVQEWr5KWHStjCyGBEnqs5XLn pspFqky5GYT/w27OOWn3YDo0ccYP1aVw7rge8H0+cKFWOzV78RN6tAWJuvoScYQvkyI2 Q9aA4HuZTpjU6eX+JtO75Ej0oDZ2qHKDTSwt/C4VpuNESP9kjTBByxIL5wVah7KC4s/m uAqvGu1WKC5k3Dl3X/Va4baU9lNBme0JxzCqcHFGr0ORxja9/1WVEhU505T0Ank+treG I5QWWhXIWadf2lVyk8gA1Pdlswt4t3lcrffZ/ZpZ1T7JNl7togGhdwNe41yWA8PHEey4 ubjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725837703; x=1726442503; h=content-transfer-encoding:mime-version: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=R2xC+tIjHn2wWvMR2T1OflcDy+oEzjuKFqq/trb+O1U=; b=DlguhM2aZ8yYpo5Fp9D1kJUpGt/FuRW5KHAwupP3ZfLyMAs90uyYlahQS4fGZHEqaQ UuSPqmoWYI1DFR/QaIQUV2C9Q7Dn6Br9YGycfm3s9fLeFANj4cA9O2K1CIMNVHLdUMx2 d4q9fYbtB386NtcNgHskkxGeX7IWPpm6VdvMM21+Oo/1pCADdmqPH6sn9EUdkdK7/Spg 2aSiOQxWB4oU6/t7HtoTKvvb/T+a4wmxsO3g+a2PWYQsvTdenvE645TM0iiVTP6RIXw0 wZ9lCiHOp2JC86cWNdWVI+YaDQYz/0lLa+QFG/qRoaT9cyPpS3h5iFyOZxeJJz+FSPjx Z3BA== X-Forwarded-Encrypted: i=1; AJvYcCXv03WmD/nBrtvuvfcHvhlTZRtXQShnYjjt36qibPybZEdQA38bV11fNqz9FLTJkmQmzVd2bRl1VA==@kvack.org X-Gm-Message-State: AOJu0Yz545NyOfmhQHcIFdv3Ej4q+G5YkeJ/O8WZT/E0Jh2WFy1e4ycv zcAehXnfrx4EYoF+gfCQZr4953uUaN0YUsGwdKSdQOp4P7KUqWgE X-Google-Smtp-Source: AGHT+IFgSDJb5dN2/2g631ifqwqWtDRQhZpgCd9etl+bEZOuXVcAb2DQHY5prsg3HDa8T4Pw/lnH/g== X-Received: by 2002:a05:6a21:4581:b0:1cf:3838:1ec5 with SMTP id adf61e73a8af0-1cf38381efdmr4465870637.5.1725837702701; Sun, 08 Sep 2024 16:21:42 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:ecbd:b95f:f7b2:8158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58c703asm2447643b3a.82.2024.09.08.16.21.33 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 08 Sep 2024 16:21:42 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org, hch@infradead.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, ryncsn@gmail.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com, Usama Arif Subject: [PATCH v9 1/3] mm: Fix swap_read_folio_zeromap() for large folios with partial zeromap Date: Mon, 9 Sep 2024 11:21:17 +1200 Message-Id: <20240908232119.2157-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240908232119.2157-1-21cnbao@gmail.com> References: <20240908232119.2157-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 3906218001B X-Stat-Signature: 1nmk6bso33ndt58qr9fx4oypz436qp39 X-Rspam-User: X-HE-Tag: 1725837704-856591 X-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0yZ4r7CKLnHeWhrPODl4sqFfDEGc3Vq2Kp32jlKyXNmn8WFOO/c1sXxZBNkkGKMpjbhxpZVvEgrbhr/RBaXXlOXOvgK0cp+KTsxuGNVMulCYcxQ1QgzAYnvqPs1AcK1gTw8V/EaVdnbJL5qZrqeWl1r77zTdz8dU4ce7ylR1HXtiWqFS8DYmFaDu7iTRNS7yzwyXpkESQTjgIJNGIHIAxaCqfGaBAvvgqoANq4Xa1RXVL70bfgCtgV0ok1b0Pyu4JEzE/nsuW7/0MxgguOI3ze1sOQPK8G+GeQt9KEEs47t159AhK7J7HMUsiUPxamKEKKEbO2k+r3zILByL8t2y02UL5fBwHYiikol5kGxdl4AxKXnfJh7x2yAmacZ/hMjGSPudidSFpowvjy 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: Barry Song There could be a corner case where the first entry is non-zeromap, but a subsequent entry is zeromap. In this case, we should not let swap_read_folio_zeromap() return false since we will still read corrupted data. Additionally, the iteration of test_bit() is unnecessary and can be replaced with bitmap operations, which are more efficient. We can adopt the style of swap_pte_batch() and folio_pte_batch() to introduce swap_zeromap_batch() which seems to provide the greatest flexibility for the caller. This approach allows the caller to either check if the zeromap status of all entries is consistent or determine the number of contiguous entries with the same status. Since swap_read_folio() can't handle reading a large folio that's partially zeromap and partially non-zeromap, we've moved the code to mm/swap.h so that others, like those working on swap-in, can access it. Fixes: 0ca0c24e3211 ("mm: store zero pages to be swapped out in a bitmap") Cc: Usama Arif Reviewed-by: Yosry Ahmed Signed-off-by: Barry Song Reviewed-by: Usama Arif --- mm/page_io.c | 32 +++++++------------------------- mm/swap.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index 4bc77d1c6bfa..bc1183299a7d 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -226,26 +226,6 @@ static void swap_zeromap_folio_clear(struct folio *folio) } } -/* - * Return the index of the first subpage which is not zero-filled - * according to swap_info_struct->zeromap. - * If all pages are zero-filled according to zeromap, it will return - * folio_nr_pages(folio). - */ -static unsigned int swap_zeromap_folio_test(struct folio *folio) -{ - struct swap_info_struct *sis = swp_swap_info(folio->swap); - swp_entry_t entry; - unsigned int i; - - for (i = 0; i < folio_nr_pages(folio); i++) { - entry = page_swap_entry(folio_page(folio, i)); - if (!test_bit(swp_offset(entry), sis->zeromap)) - return i; - } - return i; -} - /* * We may have stale swap cache pages in memory: notice * them here and get rid of the unnecessary final write. @@ -524,19 +504,21 @@ static void sio_read_complete(struct kiocb *iocb, long ret) static bool swap_read_folio_zeromap(struct folio *folio) { - unsigned int idx = swap_zeromap_folio_test(folio); - - if (idx == 0) - return false; + int nr_pages = folio_nr_pages(folio); + bool is_zeromap; /* * Swapping in a large folio that is partially in the zeromap is not * currently handled. Return true without marking the folio uptodate so * that an IO error is emitted (e.g. do_swap_page() will sigbus). */ - if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) + if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages, + &is_zeromap) != nr_pages)) return true; + if (!is_zeromap) + return false; + folio_zero_range(folio, 0, folio_size(folio)); folio_mark_uptodate(folio); return true; diff --git a/mm/swap.h b/mm/swap.h index f8711ff82f84..ad2f121de970 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -80,6 +80,32 @@ static inline unsigned int folio_swap_flags(struct folio *folio) { return swp_swap_info(folio->swap)->flags; } + +/* + * Return the count of contiguous swap entries that share the same + * zeromap status as the starting entry. If is_zeromap is not NULL, + * it will return the zeromap status of the starting entry. + */ +static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr, + bool *is_zeromap) +{ + struct swap_info_struct *sis = swp_swap_info(entry); + unsigned long start = swp_offset(entry); + unsigned long end = start + max_nr; + bool first_bit; + + first_bit = test_bit(start, sis->zeromap); + if (is_zeromap) + *is_zeromap = first_bit; + + if (max_nr <= 1) + return max_nr; + if (first_bit) + return find_next_zero_bit(sis->zeromap, end, start) - start; + else + return find_next_bit(sis->zeromap, end, start) - start; +} + #else /* CONFIG_SWAP */ struct swap_iocb; static inline void swap_read_folio(struct folio *folio, struct swap_iocb **plug) @@ -171,6 +197,13 @@ static inline unsigned int folio_swap_flags(struct folio *folio) { return 0; } + +static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr, + bool *has_zeromap) +{ + return 0; +} + #endif /* CONFIG_SWAP */ #endif /* _MM_SWAP_H */ From patchwork Sun Sep 8 23:21:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13795690 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 E9C6FCD4F4C for ; Sun, 8 Sep 2024 23:21:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B7B66B00EC; Sun, 8 Sep 2024 19:21:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 641536B00EE; Sun, 8 Sep 2024 19:21:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BA8C6B00EF; Sun, 8 Sep 2024 19:21:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2A3696B00EC for ; Sun, 8 Sep 2024 19:21:56 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E3AE940A4E for ; Sun, 8 Sep 2024 23:21:55 +0000 (UTC) X-FDA: 82543145790.02.81CE1DE Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf10.hostedemail.com (Postfix) with ESMTP id 08E15C0002 for ; Sun, 8 Sep 2024 23:21:52 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bMwt8RG7; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725837686; a=rsa-sha256; cv=none; b=RIIAQ1gvtHCYNhuhrWawa1BVZhAZH9WNCLUNmnlYIDtQ2mgTiuLaSCTAWWS/0ZdYuPxscm HxjmklFzCO+OFgU50kx9Wnor1v/KMeARR22aNen769ffqRJnzGWWxDNr2h9Jn7F9ySd/jT Up/TZEV5EMS1Q++ZkmvqS/2ZB3KiIxU= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bMwt8RG7; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725837686; 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=OlYXV3Fv3CqOaRAKUuC/QzTZ9pL0veLk2T3qfKeSJ+c=; b=L8DB35w1waBsvSoTWVmUBk92v7oI0+beaUJssW8w8q9wOI5Ok+ryJtUt67/mXXuvCMDc3h bLJZ6ZUlGe1UJFyHxYmrMkssjAVD9yTmhh4HpqBp/BVvDzPy4Qv8AqiMY+SkCxd/zEBNIJ sZtc6ndMs/TAAbkyAM1SauCZDyF6Z3o= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-71798661a52so2109676b3a.0 for ; Sun, 08 Sep 2024 16:21:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725837712; x=1726442512; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OlYXV3Fv3CqOaRAKUuC/QzTZ9pL0veLk2T3qfKeSJ+c=; b=bMwt8RG7TODIQDo3z7YI8fiCkGLEb1zXKpiZiZRyRCJmYEdIOGhF92SvkRvF3hstnD G28NCYYjgnTV2P/t40d0GJPrrwoHLUO6R5gIpJeJPH5GkSEL75rdBOzvxEE+OTO7HQT8 /AWb9j9XHJxJqiXUi7WvboxQToJKiNO6I+oZxpj0oipeMSeeWMDOw+iH8gN11TVz/vKs K32fQh8JYvrikMbQjS64eaz/YwvaNxZVQw/JvqifzPvd+Finhsnx2xov/mzGQF/d1dSF 0D41covrFo1arU9ejYueO8eCdBmkgjIm58hJ5SBrspFSZekHKs5Htu/R3t5wA+NVMeac 2WfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725837712; x=1726442512; h=content-transfer-encoding:mime-version: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=OlYXV3Fv3CqOaRAKUuC/QzTZ9pL0veLk2T3qfKeSJ+c=; b=emRVdoV1nXlyELdvgC7vNq5Jq1Sao56DprL3doNoxzek3ehTXSICxqe+sfg1z+6Olu +GVx+97hoNkI7LBW48i5ZJV2aDQabizeq9qmXvVOMsUrV8M+73tv7Sy/wViCmCpvgD+C 61Ft3aEwmcg4t1rFTINEZ5GYEUSWWZBUGNk6dOc/GsASAlMnDOjj4MLcYoZ2jGOm765x O/lXioPrXtoB3SWHirdnf/AxUPcC7NXHnJLpTcfwfvVQURes+jliDERX2UROcRs/hdw+ skOvfY/YwGsznqgPVddX6/P4S00zOitxeYQiE/pbB7ENK/IDjIlHlcjNYl7dk112M9/O z5tQ== X-Forwarded-Encrypted: i=1; AJvYcCW1M1FgYaFFa5RFRqbIUheudw9bvRdO3ZWJZiJCc8vZa6Xi/BLJ0sE2FA2mwQhPGqlW4TsgoXmeKg==@kvack.org X-Gm-Message-State: AOJu0YyXJxIBVhCFTbRSI6xa7z63zLke59rIw2c5jAT+UjADMzURzO+z DLqpXq5KcCf8/wsrF2AxVQyqCBrMKCqsWMcoxBLGJgU3Dtb1ocZX X-Google-Smtp-Source: AGHT+IGL5o84rj87mdfLAxsPpLUvBk6PXR77RV9OsfYtgvZ14Unfm2nHOrZAzsQDfp012KISjhaCqw== X-Received: by 2002:a62:a503:0:b0:717:92d8:ca5c with SMTP id d2e1a72fcca58-71792d8cadamr17178636b3a.3.1725837711653; Sun, 08 Sep 2024 16:21:51 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:ecbd:b95f:f7b2:8158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58c703asm2447643b3a.82.2024.09.08.16.21.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 08 Sep 2024 16:21:51 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org, hch@infradead.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, ryncsn@gmail.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com Subject: [PATCH v9 2/3] mm: add nr argument in mem_cgroup_swapin_uncharge_swap() helper to support large folios Date: Mon, 9 Sep 2024 11:21:18 +1200 Message-Id: <20240908232119.2157-3-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240908232119.2157-1-21cnbao@gmail.com> References: <20240908232119.2157-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 08E15C0002 X-Rspamd-Server: rspam01 X-Stat-Signature: uxqo8sw9hpgeuy5xw7wuottzmopd5i1o X-HE-Tag: 1725837712-756069 X-HE-Meta: U2FsdGVkX1/wy2IyeAOlE7LENN8k/WKjDtIEaHuiDi8CBo1xSi+RCJVpIKTYrqpceDT1lnqLDyR4mzDtP9MGnrYcEaHJ3ixsQTL+Z9qFsXoHWmh1bdkXcKlYDDoNIf3WCTaV96BlCeL1zY1qx3KAsNqCSooZhAzhE0ZrD9QeeuNQBEL/6XMW7v+t/7tAoLo5SEYY9E5v7e4cdXWKpJEWUOTek+9MuCxOWAPX8IUArMbMPBSHkDLl5PYdXqRDNmrev/8y1CP77beha4flXHrIwN+uULdkyz4rIKTP081D6YW2HJxdTq8qamqKQzyUaqIK66cBgdBMyE8WuvHbxS2NTPimjrkEP3FXp9F4o82pZHa/g9OgOnnL2TDmeLdy5s6dvZ6num8pJoiKWg4KeG4p7zSjWhWtR/BJSkROkt4EVkn0XN4/Jk+S7WJ0rj2sAFDd4kmjLRM3g9Za3FJVzzRURj9pyQTYBCMKBFKe3cBH0SzIzsFUDKHSlBnx1pTbnduc1kwdOeH2MY5iTDPdwTZ5jXPAAs77vZDvFB/wdWUqYNSkZ+b4McBgcgaD3lvVIvxONxSkQFajzB7Es8Ypr9eayfRTf4nyDk6RVgEx/DMwdAJ2CZv/9NYzuO/Fbo7Vp/gGLuBQ4NkzL/iBiFG5vLTns+16RoQxxU+uyHrbInpR6noYupXEsbb010CNW0BcPvBHqWAMergh0BvkhpFDIBpmdIH/5/iowilKMI2qc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ZP/R2OenJXt+JEl9nPnlWuVToC1tb4KQHyWJRlS+gio 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: Barry Song With large folios swap-in, we might need to uncharge multiple entries all together, add nr argument in mem_cgroup_swapin_uncharge_swap(). For the existing two users, just pass nr=1. Signed-off-by: Barry Song Acked-by: Chris Li Reviewed-by: Yosry Ahmed Cc: Shakeel Butt Cc: Baolin Wang Cc: Christoph Hellwig Cc: David Hildenbrand Cc: Gao Xiang Cc: "Huang, Ying" Cc: Hugh Dickins Cc: Johannes Weiner Cc: Kairui Song Cc: Kairui Song Cc: Kalesh Singh Cc: Matthew Wilcox (Oracle) Cc: Michal Hocko Cc: Minchan Kim Cc: Nhat Pham Cc: Ryan Roberts Cc: Sergey Senozhatsky Cc: Suren Baghdasaryan Cc: Yang Shi --- include/linux/memcontrol.h | 5 +++-- mm/memcontrol.c | 7 ++++--- mm/memory.c | 2 +- mm/swap_state.c | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 2ef94c74847d..34d2da05f2f1 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -699,7 +699,8 @@ int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, gfp_t gfp, swp_entry_t entry); -void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry); + +void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry, unsigned int nr_pages); void __mem_cgroup_uncharge(struct folio *folio); @@ -1206,7 +1207,7 @@ static inline int mem_cgroup_swapin_charge_folio(struct folio *folio, return 0; } -static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) +static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry, unsigned int nr) { } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index bda6f75d22ff..c0d36ca20332 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4559,14 +4559,15 @@ int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, /* * mem_cgroup_swapin_uncharge_swap - uncharge swap slot - * @entry: swap entry for which the page is charged + * @entry: the first swap entry for which the pages are charged + * @nr_pages: number of pages which will be uncharged * * Call this function after successfully adding the charged page to swapcache. * * Note: This function assumes the page for which swap slot is being uncharged * is order 0 page. */ -void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) +void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry, unsigned int nr_pages) { /* * Cgroup1's unified memory+swap counter has been charged with the @@ -4586,7 +4587,7 @@ void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) * let's not wait for it. The page already received a * memory+swap charge, drop the swap entry duplicate. */ - mem_cgroup_uncharge_swap(entry, 1); + mem_cgroup_uncharge_swap(entry, nr_pages); } } diff --git a/mm/memory.c b/mm/memory.c index 42674c0748cb..cdf03b39a92c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4100,7 +4100,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) ret = VM_FAULT_OOM; goto out_page; } - mem_cgroup_swapin_uncharge_swap(entry); + mem_cgroup_swapin_uncharge_swap(entry, 1); shadow = get_shadow_from_swap_cache(entry); if (shadow) diff --git a/mm/swap_state.c b/mm/swap_state.c index a042720554a7..4669f29cf555 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -522,7 +522,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, if (add_to_swap_cache(new_folio, entry, gfp_mask & GFP_RECLAIM_MASK, &shadow)) goto fail_unlock; - mem_cgroup_swapin_uncharge_swap(entry); + mem_cgroup_swapin_uncharge_swap(entry, 1); if (shadow) workingset_refault(new_folio, shadow); From patchwork Sun Sep 8 23:21:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13795691 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 A0D88ECE577 for ; Sun, 8 Sep 2024 23:22:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3140B6B00EE; Sun, 8 Sep 2024 19:22:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 270A96B00F0; Sun, 8 Sep 2024 19:22:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09C0C6B00F1; Sun, 8 Sep 2024 19:22:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D70986B00EE for ; Sun, 8 Sep 2024 19:22:04 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9863A80AC4 for ; Sun, 8 Sep 2024 23:22:04 +0000 (UTC) X-FDA: 82543146168.20.FCB7A17 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf26.hostedemail.com (Postfix) with ESMTP id A9853140012 for ; Sun, 8 Sep 2024 23:22:02 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WKxioXLy; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=21cnbao@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=1725837622; 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=vV2tR4Goy2aJJyrtdQU+NgrOzgVlBcOxf/RbnCByfrk=; b=ddR2aoAQ+E/N1OxwqIAaXJpogtpLD57cz3GWc5qckyZKx9kqBCGQy0TBwz5ehV6PagXCW1 0s9BEkt/XUC1aMuu18qfyQveBCWH1x3BSddvWrW824yOVuyblzUrLuZJVXgJbc7+mhMiaI 0zFXhA7o+uag6ytjCPepD/ZivztnZKI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725837622; a=rsa-sha256; cv=none; b=tYdpAHZ7KohnehABF+os7ZPawHQxyJWOnrfAIFU4EAX+U/xGvF6DT/Fmyj+v2Oe+tl2l+o +3LG7YyQUXagGhavpQjd5PczXp1NQY1yKsRRJX4fVpqKc9sNhhodikQsnJhS/ODTxyvjez PyUuH+iVxr8rkMCJqrCEPnFfj+UbJRM= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WKxioXLy; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-718d79fb6f7so2032874b3a.3 for ; Sun, 08 Sep 2024 16:22:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725837721; x=1726442521; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vV2tR4Goy2aJJyrtdQU+NgrOzgVlBcOxf/RbnCByfrk=; b=WKxioXLyEesKqHm1WmcalMGpVxzfkvd17zo5kmwRAJycczJGyF4/lKUSAGvO4S1lxE PlosZVzi7810p+frFoG9qWrFId4A+TqOGA1HOlc8AZIG6yet+XxtXPYJqcGEWu0JDfYo wtLLe1BPNpnWpE5AE6UWHBZjnR11E2R8GRCnrxHakGL0AOOZMj+xDhvZgS2j2DvvzZW2 xsfLwuo8q5FQ7CCbfw2QGa6oNRJytN9Pc+hipdC6h/jO8+XD/F2VSggOqXirc2DECDel qjJMmY9lywFotsHPP1ffMeLhyjyJzg3im1yB4Lp9c7xf3uz6CerlaviztVW1ynnfL5nK uAwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725837721; x=1726442521; h=content-transfer-encoding:mime-version: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=vV2tR4Goy2aJJyrtdQU+NgrOzgVlBcOxf/RbnCByfrk=; b=DaKX52R9lSXtY5KwYQmKaG+SVXy4a44Fj57puYPHTeiB7r3hKQgdMVEUo5J5059hnQ ytc+/RvFZukfuRrxN1h7wgVyqDGW05G2a0E97zwmw0s0XtQ+niGQ9/qRbT/IH7/A7Btf wEukG6X5txot61M8cWonw2Iz0m5wdYWRMBLfH0W5oFs6sQevbTFBAw5hGcCfKgtARIS2 xF+scv4RGUUdhr7rEQwKOVYUb22QbvPw/mnm62ewyBHsYxfqE44XTRet0ErzTcZUqp59 sd2EWtWQXcc05roT4wcRNsOwFblSMRdkbhusX1oEdKGcQqwsFLl1OhjPcoCb1fHGQfnP h4Rw== X-Forwarded-Encrypted: i=1; AJvYcCWpyUivO1+JEQVdbcqZCqocnGwVN9G98X6zZWZv0N41urV0KfJPiE4M0x8eYsHyHJTqf+iGDG0Kew==@kvack.org X-Gm-Message-State: AOJu0Yzmx4Ag2PUtSBT5PHbdFRW+TUBFM+xWg1BUe0eeYo5Cu7+Sm1WU jIEkNPiLAmwAQpHlUSUq7TmpaxY1tQ1el+G1XxS5lyEkJKky6oFM8ZSHfIZB X-Google-Smtp-Source: AGHT+IHRJq5JYuctycrfpKYXeZj++jFBYwPbiiUe8eU/NW1tJMIzK2yvOhZETJo0+EJD3FXhIFsKrw== X-Received: by 2002:a05:6a00:1993:b0:714:3de8:a616 with SMTP id d2e1a72fcca58-718d5ef9415mr9444174b3a.19.1725837721272; Sun, 08 Sep 2024 16:22:01 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:ecbd:b95f:f7b2:8158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58c703asm2447643b3a.82.2024.09.08.16.21.52 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 08 Sep 2024 16:22:00 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org, hch@infradead.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, ryncsn@gmail.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com, Usama Arif , Kanchana P Sridhar Subject: [PATCH v9 3/3] mm: support large folios swap-in for sync io devices Date: Mon, 9 Sep 2024 11:21:19 +1200 Message-Id: <20240908232119.2157-4-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240908232119.2157-1-21cnbao@gmail.com> References: <20240908232119.2157-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A9853140012 X-Stat-Signature: hpyj48mww19mqf7etm66y65hkggk33z9 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1725837722-678265 X-HE-Meta: U2FsdGVkX19jeN/2HgMzL+auPAgkCKoNzbsK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aNin8SUqxqSyZTPQx0Xtp1eNHhgI5M4DWh/ZoAJGCKDPe62bgHC17EeQeWxTNT9Cmex5c0reFg7Tj7m97MRtagL8BqeIWxSkjzkhdqk5sN0pyU4+CwZzM4jb8/XdyNkjQuE8keEPbIyguOKs1LD+qqb9++wuILZQ5MNwUaRTybMrUjrGO2Vmt1fOX4tOxCGewPzZPAa6WqqLbxOHJvHBFveIz5V7SlkRCtjo4BMj+9+G3npicTdqtgP5sXbLnRGGi8qdKg5MGylXEUmt+fJmJih/+xG0hz+X0NJE5Bi0zEXXkeEUcO2PGtcvR0FHq6JVYWdyoW4e8xT2ss3qzsGR7Gy+ruU6l9dA40V3NkLp9Hq+za4S61iEZo4I9f8ISDfKVoYNYu/U4SChmbZPMsQ7IT0/xYkhOSnJBDzQcnRQKyfB4j9GgnDSwUJfFBTbYyWRy7ipX4khkl1mZvwG62oLbm/ln/l5i2+1o8S+iq6SvoS9sHmjiC+19IeiPn4qcr0Ay+CMiD2RPTU/oD6qmMlBsMfpu5PupJJTqTjlcSRW2lYnp1a3Wz4E+tMTL0eM6QLCAZx8WFQlBC0wk2Uwdbkc4fTPSvjVVCzMsavzec1Qxy06NPpK+WA+jDZnr0uthuGSMSVRySknpzrUOEqyylRv3xJ1IsJMXPVmvz23uUPlGTw6hsoj7OXSn6TzlprmukkuuVK/LfmSOGkkeKEjvTSOURxnrJMeLp2273lgO93T+V+LhRINlCB+8dpLNrnKkb/fqx3gwIYavi1lUGZ7SGNqnfy98L90AmIouz0itmzl8= 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: Chuanhua Han Currently, we have mTHP features, but unfortunately, without support for large folio swap-ins, once these large folios are swapped out, they are lost because mTHP swap is a one-way process. The lack of mTHP swap-in functionality prevents mTHP from being used on devices like Android that heavily rely on swap. This patch introduces mTHP swap-in support. It starts from sync devices such as zRAM. This is probably the simplest and most common use case, benefiting billions of Android phones and similar devices with minimal implementation cost. In this straightforward scenario, large folios are always exclusive, eliminating the need to handle complex rmap and swapcache issues. It offers several benefits: 1. Enables bidirectional mTHP swapping, allowing retrieval of mTHP after swap-out and swap-in. Large folios in the buddy system are also preserved as much as possible, rather than being fragmented due to swap-in. 2. Eliminates fragmentation in swap slots and supports successful THP_SWPOUT. w/o this patch (Refer to the data from Chris's and Kairui's latest swap allocator optimization while running ./thp_swap_allocator_test w/o "-a" option [1]): ./thp_swap_allocator_test Iteration 1: swpout inc: 233, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 2: swpout inc: 131, swpout fallback inc: 101, Fallback percentage: 43.53% Iteration 3: swpout inc: 71, swpout fallback inc: 155, Fallback percentage: 68.58% Iteration 4: swpout inc: 55, swpout fallback inc: 168, Fallback percentage: 75.34% Iteration 5: swpout inc: 35, swpout fallback inc: 191, Fallback percentage: 84.51% Iteration 6: swpout inc: 25, swpout fallback inc: 199, Fallback percentage: 88.84% Iteration 7: swpout inc: 23, swpout fallback inc: 205, Fallback percentage: 89.91% Iteration 8: swpout inc: 9, swpout fallback inc: 219, Fallback percentage: 96.05% Iteration 9: swpout inc: 13, swpout fallback inc: 213, Fallback percentage: 94.25% Iteration 10: swpout inc: 12, swpout fallback inc: 216, Fallback percentage: 94.74% Iteration 11: swpout inc: 16, swpout fallback inc: 213, Fallback percentage: 93.01% Iteration 12: swpout inc: 10, swpout fallback inc: 210, Fallback percentage: 95.45% Iteration 13: swpout inc: 16, swpout fallback inc: 212, Fallback percentage: 92.98% Iteration 14: swpout inc: 12, swpout fallback inc: 212, Fallback percentage: 94.64% Iteration 15: swpout inc: 15, swpout fallback inc: 211, Fallback percentage: 93.36% Iteration 16: swpout inc: 15, swpout fallback inc: 200, Fallback percentage: 93.02% Iteration 17: swpout inc: 9, swpout fallback inc: 220, Fallback percentage: 96.07% w/ this patch (always 0%): Iteration 1: swpout inc: 948, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 2: swpout inc: 953, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 3: swpout inc: 950, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 4: swpout inc: 952, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 5: swpout inc: 950, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 6: swpout inc: 950, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 7: swpout inc: 947, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 8: swpout inc: 950, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 9: swpout inc: 950, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 10: swpout inc: 945, swpout fallback inc: 0, Fallback percentage: 0.00% Iteration 11: swpout inc: 947, swpout fallback inc: 0, Fallback percentage: 0.00% ... 3. With both mTHP swap-out and swap-in supported, we offer the option to enable zsmalloc compression/decompression with larger granularity[2]. The upcoming optimization in zsmalloc will significantly increase swap speed and improve compression efficiency. Tested by running 100 iterations of swapping 100MiB of anon memory, the swap speed improved dramatically: time consumption of swapin(ms) time consumption of swapout(ms) lz4 4k 45274 90540 lz4 64k 22942 55667 zstdn 4k 85035 186585 zstdn 64k 46558 118533 The compression ratio also improved, as evaluated with 1 GiB of data: granularity orig_data_size compr_data_size 4KiB-zstd 1048576000 246876055 64KiB-zstd 1048576000 199763892 Without mTHP swap-in, the potential optimizations in zsmalloc cannot be realized. 4. Even mTHP swap-in itself can reduce swap-in page faults by a factor of nr_pages. Swapping in content filled with the same data 0x11, w/o and w/ the patch for five rounds (Since the content is the same, decompression will be very fast. This primarily assesses the impact of reduced page faults): swp in bandwidth(bytes/ms) w/o w/ round1 624152 1127501 round2 631672 1127501 round3 620459 1139756 round4 606113 1139756 round5 624152 1152281 avg 621310 1137359 +83% 5. With both mTHP swap-out and swap-in supported, we offer the option to enable hardware accelerators(Intel IAA) to do parallel decompression with which Kanchana reported 7X improvement on zRAM read latency[3]. [1] https://lore.kernel.org/all/20240730-swap-allocator-v5-0-cb9c148b9297@kernel.org/ [2] https://lore.kernel.org/all/20240327214816.31191-1-21cnbao@gmail.com/ [3] https://lore.kernel.org/all/cover.1714581792.git.andre.glover@linux.intel.com/ Signed-off-by: Chuanhua Han Co-developed-by: Barry Song Signed-off-by: Barry Song Cc: Baolin Wang Cc: Chris Li Cc: Christoph Hellwig Cc: David Hildenbrand Cc: Gao Xiang Cc: "Huang, Ying" Cc: Hugh Dickins Cc: Johannes Weiner Cc: Kairui Song Cc: Kalesh Singh Cc: Matthew Wilcox (Oracle) Cc: Michal Hocko Cc: Minchan Kim Cc: Nhat Pham Cc: Ryan Roberts Cc: Sergey Senozhatsky Cc: Shakeel Butt Cc: Suren Baghdasaryan Cc: Yang Shi Cc: Yosry Ahmed Cc: Usama Arif Cc: Kanchana P Sridhar --- mm/memory.c | 261 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 234 insertions(+), 27 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index cdf03b39a92c..d35dd8d99c8a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3985,6 +3985,194 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf) return VM_FAULT_SIGBUS; } +static struct folio *__alloc_swap_folio(struct vm_fault *vmf) +{ + struct vm_area_struct *vma = vmf->vma; + struct folio *folio; + swp_entry_t entry; + + folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, + vmf->address, false); + if (!folio) + return NULL; + + entry = pte_to_swp_entry(vmf->orig_pte); + if (mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, + GFP_KERNEL, entry)) { + folio_put(folio); + return NULL; + } + + return folio; +} + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) +{ + struct swap_info_struct *si = swp_swap_info(entry); + pgoff_t offset = swp_offset(entry); + int i; + + /* + * While allocating a large folio and doing swap_read_folio, which is + * the case the being faulted pte doesn't have swapcache. We need to + * ensure all PTEs have no cache as well, otherwise, we might go to + * swap devices while the content is in swapcache. + */ + for (i = 0; i < max_nr; i++) { + if ((si->swap_map[offset + i] & SWAP_HAS_CACHE)) + return i; + } + + return i; +} + +/* + * Check if the PTEs within a range are contiguous swap entries + * and have consistent swapcache, zeromap. + */ +static bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int nr_pages) +{ + unsigned long addr; + swp_entry_t entry; + int idx; + pte_t pte; + + addr = ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE); + idx = (vmf->address - addr) / PAGE_SIZE; + pte = ptep_get(ptep); + + if (!pte_same(pte, pte_move_swp_offset(vmf->orig_pte, -idx))) + return false; + entry = pte_to_swp_entry(pte); + if (swap_pte_batch(ptep, nr_pages, pte) != nr_pages) + return false; + + /* + * swap_read_folio() can't handle the case a large folio is hybridly + * from different backends. And they are likely corner cases. Similar + * things might be added once zswap support large folios. + */ + if (unlikely(swap_zeromap_batch(entry, nr_pages, NULL) != nr_pages)) + return false; + if (unlikely(non_swapcache_batch(entry, nr_pages) != nr_pages)) + return false; + + return true; +} + +static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset, + unsigned long addr, + unsigned long orders) +{ + int order, nr; + + order = highest_order(orders); + + /* + * To swap in a THP with nr pages, we require that its first swap_offset + * is aligned with that number, as it was when the THP was swapped out. + * This helps filter out most invalid entries. + */ + while (orders) { + nr = 1 << order; + if ((addr >> PAGE_SHIFT) % nr == swp_offset % nr) + break; + order = next_order(&orders, order); + } + + return orders; +} + +static struct folio *alloc_swap_folio(struct vm_fault *vmf) +{ + struct vm_area_struct *vma = vmf->vma; + unsigned long orders; + struct folio *folio; + unsigned long addr; + swp_entry_t entry; + spinlock_t *ptl; + pte_t *pte; + gfp_t gfp; + int order; + + /* + * If uffd is active for the vma we need per-page fault fidelity to + * maintain the uffd semantics. + */ + if (unlikely(userfaultfd_armed(vma))) + goto fallback; + + /* + * A large swapped out folio could be partially or fully in zswap. We + * lack handling for such cases, so fallback to swapping in order-0 + * folio. + */ + if (!zswap_never_enabled()) + goto fallback; + + entry = pte_to_swp_entry(vmf->orig_pte); + /* + * Get a list of all the (large) orders below PMD_ORDER that are enabled + * and suitable for swapping THP. + */ + orders = thp_vma_allowable_orders(vma, vma->vm_flags, + TVA_IN_PF | TVA_ENFORCE_SYSFS, BIT(PMD_ORDER) - 1); + orders = thp_vma_suitable_orders(vma, vmf->address, orders); + orders = thp_swap_suitable_orders(swp_offset(entry), + vmf->address, orders); + + if (!orders) + goto fallback; + + pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, + vmf->address & PMD_MASK, &ptl); + if (unlikely(!pte)) + goto fallback; + + /* + * For do_swap_page, find the highest order where the aligned range is + * completely swap entries with contiguous swap offsets. + */ + order = highest_order(orders); + while (orders) { + addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); + if (can_swapin_thp(vmf, pte + pte_index(addr), 1 << order)) + break; + order = next_order(&orders, order); + } + + pte_unmap_unlock(pte, ptl); + + /* Try allocating the highest of the remaining orders. */ + gfp = vma_thp_gfp_mask(vma); + while (orders) { + addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); + folio = vma_alloc_folio(gfp, order, vma, addr, true); + if (folio) { + if (!mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, + gfp, entry)) + return folio; + folio_put(folio); + } + order = next_order(&orders, order); + } + +fallback: + return __alloc_swap_folio(vmf); +} +#else /* !CONFIG_TRANSPARENT_HUGEPAGE */ +static inline bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int nr_pages) +{ + return false; +} + +static struct folio *alloc_swap_folio(struct vm_fault *vmf) +{ + return __alloc_swap_folio(vmf); +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + /* * We enter with non-exclusive mmap_lock (to exclude vma changes, * but allow concurrent faults), and pte mapped but not yet locked. @@ -4073,34 +4261,34 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (!folio) { if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && __swap_count(entry) == 1) { - /* - * Prevent parallel swapin from proceeding with - * the cache flag. Otherwise, another thread may - * finish swapin first, free the entry, and swapout - * reusing the same entry. It's undetectable as - * pte_same() returns true due to entry reuse. - */ - if (swapcache_prepare(entry, 1)) { - /* Relax a bit to prevent rapid repeated page faults */ - schedule_timeout_uninterruptible(1); - goto out; - } - need_clear_cache = true; - /* skip swapcache */ - folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, - vma, vmf->address, false); + folio = alloc_swap_folio(vmf); if (folio) { __folio_set_locked(folio); __folio_set_swapbacked(folio); - if (mem_cgroup_swapin_charge_folio(folio, - vma->vm_mm, GFP_KERNEL, - entry)) { - ret = VM_FAULT_OOM; + nr_pages = folio_nr_pages(folio); + if (folio_test_large(folio)) + entry.val = ALIGN_DOWN(entry.val, nr_pages); + /* + * Prevent parallel swapin from proceeding with + * the cache flag. Otherwise, another thread + * may finish swapin first, free the entry, and + * swapout reusing the same entry. It's + * undetectable as pte_same() returns true due + * to entry reuse. + */ + if (swapcache_prepare(entry, nr_pages)) { + /* + * Relax a bit to prevent rapid + * repeated page faults. + */ + schedule_timeout_uninterruptible(1); goto out_page; } - mem_cgroup_swapin_uncharge_swap(entry, 1); + need_clear_cache = true; + + mem_cgroup_swapin_uncharge_swap(entry, nr_pages); shadow = get_shadow_from_swap_cache(entry); if (shadow) @@ -4206,6 +4394,24 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out_nomap; } + /* allocated large folios for SWP_SYNCHRONOUS_IO */ + if (folio_test_large(folio) && !folio_test_swapcache(folio)) { + unsigned long nr = folio_nr_pages(folio); + unsigned long folio_start = ALIGN_DOWN(vmf->address, nr * PAGE_SIZE); + unsigned long idx = (vmf->address - folio_start) / PAGE_SIZE; + pte_t *folio_ptep = vmf->pte - idx; + pte_t folio_pte = ptep_get(folio_ptep); + + if (!pte_same(folio_pte, pte_move_swp_offset(vmf->orig_pte, -idx)) || + swap_pte_batch(folio_ptep, nr, folio_pte) != nr) + goto out_nomap; + + page_idx = idx; + address = folio_start; + ptep = folio_ptep; + goto check_folio; + } + nr_pages = 1; page_idx = 0; address = vmf->address; @@ -4337,11 +4543,12 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_add_lru_vma(folio, vma); } else if (!folio_test_anon(folio)) { /* - * We currently only expect small !anon folios, which are either - * fully exclusive or fully shared. If we ever get large folios - * here, we have to be careful. + * We currently only expect small !anon folios which are either + * fully exclusive or fully shared, or new allocated large + * folios which are fully exclusive. If we ever get large + * folios within swapcache here, we have to be careful. */ - VM_WARN_ON_ONCE(folio_test_large(folio)); + VM_WARN_ON_ONCE(folio_test_large(folio) && folio_test_swapcache(folio)); VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); folio_add_new_anon_rmap(folio, vma, address, rmap_flags); } else { @@ -4384,7 +4591,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) out: /* Clear the swap cache pin for direct swapin after PTL unlock */ if (need_clear_cache) - swapcache_clear(si, entry, 1); + swapcache_clear(si, entry, nr_pages); if (si) put_swap_device(si); return ret; @@ -4400,7 +4607,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_put(swapcache); } if (need_clear_cache) - swapcache_clear(si, entry, 1); + swapcache_clear(si, entry, nr_pages); if (si) put_swap_device(si); return ret;