From nobody Sun Feb 8 03:07:07 2026 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 081BB1272A6 for ; Mon, 23 Sep 2024 23:11:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727133106; cv=none; b=noqVT+bMtpA0U7ZJ6vcCdXUWcJXTX278CN3rFyCtgZR+vMiidYHqDZK+vvbeTNtuH9UVBInZxEMGN2ixWaf3EPNUo7hHT4iUdptOnSK4StwgkEuU1I2E56GYNCSkr+mVr41+F3WRbDgmZ1jKE9m/MN1+eWyNPMYBLRrMJTNp1/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727133106; c=relaxed/simple; bh=qkaPYRAZSasb6ju7LOEnZOnDHWzzGT8YOnhblSx3BnI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=upA7TUJkoMcXTHVBOthtVrGVk+r5G4GXLMmihVE5b/5/0nU1oBc3eiJx4+vkPN4UrNdl6N5OCpb9Tf/kNxsmu51A13eDPBYaIzTsgog0YM3wMCbZBFpESdxXPN8F70axRN94GsmnK5tMt2WRh+MnUU4tcnVLOLBYyYdmFLsPtww= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Eo90i49e; arc=none smtp.client-ip=209.85.128.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Eo90i49e" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6ddca648c26so40141397b3.1 for ; Mon, 23 Sep 2024 16:11:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727133104; x=1727737904; darn=vger.kernel.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=LPDqT4tYuF6YjiitP1AaWuH6hXZ6wk6au/QNmRqOg+Q=; b=Eo90i49ehGU2QLCAaq/qPf6QCWD69nPLbxwAwRHUM5iWBn+Mq56+Nsmnap0J+Uqhz6 BpNTU0yN9zzn8eCJYCXDA4BiOaSBC9Pp+0GO2OkJJs5h44mBPoWh5ndahvutaewCMyGq 7QQwYiEInKvuvQ3ag8wDmz8Gm4g+QLFEY+QmErm7wJmARtgE1PCo5Aq5k6htvr87sbZ+ 9TTC0WZxk+yp5inJuUCw/0cRiw1+QKWWRLKRnw5JFAfD+Va2vEag6aAXP+aunFDq1Mat 4ejahE8olmFBCmmrmuLx/LPWe0LB7v5L7qpLqko6PXg8OL6Gnz2NrFb3+Guidnh7Xw7k YgQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727133104; x=1727737904; 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=LPDqT4tYuF6YjiitP1AaWuH6hXZ6wk6au/QNmRqOg+Q=; b=SU16dG2B/9SRD7oCKw4n7dRK8QasxrHsUOPIcN01oSJ4MkF5+jnxTNxw94meIgeEh+ h125m20DtHVS/zbc3fSusoU7Txl5d9tUMmvndsJcKMFCTTcZAVBtPbCAKadtfHYUa/Cg 2HdGVKYGmTYZJydbmn0NrAkbiQveejzcrVuy7lXG1i+BjQznBb4OqoFHnxG+xu3z01W6 Kf1KhlPJ4hvft1YwzA8kpKrAW4TjLCwIoU40NOLPJpUipFgYvEk2JD8gY+pmA9qFSg8r NahDc2nQZck6FCuJZXkggpu778g7ameoTGePI/Ev4xhI6gLXlFlAY15PTkc4Cevd0Ixe lBNA== X-Forwarded-Encrypted: i=1; AJvYcCXpMZNgA/qUqlecmG17moUumkRSTlGRq08R4fB59++9ujvMomTqJyFLeMppfBBF4/3z/IQcytTpyzLwc/g=@vger.kernel.org X-Gm-Message-State: AOJu0YzZbrh6TYXapw0MMVdvdck4dsngsXY0s2UnNObOjvqPCs4O5XA0 miVD0BxRJBNFYpxvR8EsSanwu0qvV5aTyz5TlWJUgjanEKiLNwOd X-Google-Smtp-Source: AGHT+IFcssM6pnQQDk/NRr4NihPaeFHiGgxFPtmnmmYpIunNZgYl6WAQdDvridjor3AWUUXZ9mYN5w== X-Received: by 2002:a05:6902:150a:b0:e1f:aec0:4e6d with SMTP id 3f1490d57ef6-e24978b67eemr1107670276.14.1727133103972; Mon, 23 Sep 2024 16:11:43 -0700 (PDT) Received: from localhost (fwdproxy-nha-114.fbsv.net. [2a03:2880:25ff:72::face:b00c]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e2499c6226csm50606276.43.2024.09.23.16.11.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 16:11:43 -0700 (PDT) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, yosryahmed@google.com, hughd@google.com, shakeel.butt@linux.dev, ryan.roberts@arm.com, ying.huang@intel.com, chrisl@kernel.org, david@redhat.com, kasong@tencent.com, willy@infradead.org, viro@zeniv.linux.org.uk, baohua@kernel.org, chengming.zhou@linux.dev, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/2] swapfile: add a batched variant for swap_duplicate() Date: Mon, 23 Sep 2024 16:11:41 -0700 Message-ID: <20240923231142.4155415-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240923231142.4155415-1-nphamcs@gmail.com> References: <20240923231142.4155415-1-nphamcs@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add swap_duplicate_nr(), the batched variant of swap_duplicate(), that operates on multiple contiguous swap entries. This will be used in the following patch to remove SWAP_MAP_SHMEM. Signed-off-by: Nhat Pham Reviewed-by: Yosry Ahmed --- include/linux/swap.h | 10 ++++++++-- mm/swapfile.c | 13 +++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index ca533b478c21..e6ab234be7be 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -483,7 +483,7 @@ extern swp_entry_t get_swap_page_of_type(int); extern int get_swap_pages(int n, swp_entry_t swp_entries[], int order); extern int add_swap_count_continuation(swp_entry_t, gfp_t); extern void swap_shmem_alloc(swp_entry_t, int); -extern int swap_duplicate(swp_entry_t); +extern int swap_duplicate_nr(swp_entry_t, int); extern int swapcache_prepare(swp_entry_t entry, int nr); extern void swap_free_nr(swp_entry_t entry, int nr_pages); extern void swapcache_free_entries(swp_entry_t *entries, int n); @@ -553,7 +553,7 @@ static inline void swap_shmem_alloc(swp_entry_t swp, in= t nr) { } =20 -static inline int swap_duplicate(swp_entry_t swp) +static inline int swap_duplicate_nr(swp_entry_t swp, int nr) { return 0; } @@ -606,6 +606,12 @@ static inline int add_swap_extent(struct swap_info_str= uct *sis, } #endif /* CONFIG_SWAP */ =20 +static inline int swap_duplicate(swp_entry_t entry) +{ + return swap_duplicate_nr(entry, 1); +} + + static inline void free_swap_and_cache(swp_entry_t entry) { free_swap_and_cache_nr(entry, 1); diff --git a/mm/swapfile.c b/mm/swapfile.c index 0cded32414a1..47a2cd5f590d 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3695,18 +3695,23 @@ void swap_shmem_alloc(swp_entry_t entry, int nr) __swap_duplicate(entry, SWAP_MAP_SHMEM, nr); } =20 -/* - * Increase reference count of swap entry by 1. +/** + * swap_duplicate_nr() - Increase reference count of nr contiguous swap en= tries + * by 1. + * + * @entry: first swap entry from which we want to increase the refcount. + * @nr: Number of entries in range. + * * Returns 0 for success, or -ENOMEM if a swap_count_continuation is requi= red * but could not be atomically allocated. Returns 0, just as if it succee= ded, * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), wh= ich * might occur if a page table entry has got corrupted. */ -int swap_duplicate(swp_entry_t entry) +int swap_duplicate_nr(swp_entry_t entry, int nr) { int err =3D 0; =20 - while (!err && __swap_duplicate(entry, 1, 1) =3D=3D -ENOMEM) + while (!err && __swap_duplicate(entry, 1, nr) =3D=3D -ENOMEM) err =3D add_swap_count_continuation(entry, GFP_ATOMIC); return err; } --=20 2.43.5 From nobody Sun Feb 8 03:07:07 2026 Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC693126F0A for ; Mon, 23 Sep 2024 23:11:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727133107; cv=none; b=W3jGKMq9KiVYqU+Is0eCov5DnGfzkWYBo6AlZqd5ybwLovK04WCSpfgawC/2Lbl9OQrGdHauNuewWzYUn3qOZsgnk+L7twESRsGVSBVVa9E4CP5nl/SotZPpw5q1jyj9TadGTF9/kZitXHcuxgJ5SEk8c3BAhf+yUSZrCcALfS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727133107; c=relaxed/simple; bh=KTBAr4CcuZCVEhcseNZsFXERaSMNaJ/qGDbL/SOJQQ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qWgEL821QNFRt9EDv/j/Kr3G9/a0dDbiZQguQh4H8ySQhVLrdkC1+DTqt7Vx/OgDrF6tvdi1amqnTh9ODUE40bR/0xAB9qQX8HxAMk0OKuxpLnn0ILbRU0EmXxap0NpreTxRkrMEkEdztYMdf5xwUkcI7rG4c0TiNhuZ6rNJWcY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EmR1x1FS; arc=none smtp.client-ip=209.85.219.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EmR1x1FS" Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-e201f6eda9fso4396060276.2 for ; Mon, 23 Sep 2024 16:11:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727133105; x=1727737905; darn=vger.kernel.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=8gPbe3KNGXZnnS8+FFZzqNPtSCfvfbRnB6uu/A9bSpc=; b=EmR1x1FSx+PX6RImQE2nH2khiMcH77FpT/NLpqGmi3ZuTg4FQFseEMT04gIim8LcYw zvKiSmusRZfzDNr2kcnYI+zY4BdAhqgInR7UiWzGfeh4Pjn7X633ZXb/UhTBpH5rcjkr yIP8JuyW7v8ep85wmwbV7blboBfPc17ybG9HUAuDWLx7hrTn+OZJ4xwqrVIqBNP4J3f3 oMO+DDDfq/JHJRMxTOxOk5h2+dEzFVpZ1Ji2cZ9VBexXud8XPVChMD72ni6+7xiolpkd Qk1y34eFkIub0uMKf94woYcnPMllGt1N2JIShWsKQp28HwXD4891P9HSwaaek1TWCnGK 69NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727133105; x=1727737905; 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=8gPbe3KNGXZnnS8+FFZzqNPtSCfvfbRnB6uu/A9bSpc=; b=ae6zw6yyPz6lgI124e03nKnRLNRnRSwRzNO8uOgwLetqqE64DUPddsQeG+SGrQbzoD Ej6+dr1SbspQXHF7kPkehqzkB/oPfRL8WG2poifyuZHS/iDsyERUrdb2ywTEtHEc24Na XlZmXYPCr1AmlTJoCxV7FSeEGbc6X++c3xGUqWssqwa44K3+WRlxHwld1R715JCvE12E 7fTgIZcyf7m2FAH8g6396U6k8F9u0WfVR819WCpbIl2mCLBXaP5MYSabSCv6l0WbNQXN NvgORbr15GDj7h4UMVwqz54ZKnyYzqzRoGFHOu8qGRNDcafxPHNSjQvuZZ9SpDRFsHYE 5AiQ== X-Forwarded-Encrypted: i=1; AJvYcCXBHchPSRmK9LD/DJBt+5dkcpk/D+byYmxk5aichsP3/0UvB4NM70nrex+7crGC3QG4i+15VQp9xhTh+HQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yz/q5RfA58EAZfqGA8Dn3znyk15SGgtYGGxN+XAj5MdG8eMIUwD PxxztKSMJVG/6bwfBfH1MsXGnrrV+M2WztgC0v0Bht1I0gwH1nHz X-Google-Smtp-Source: AGHT+IFlyb1PljXDzq/V7UeC9hqJSsT1MOj/S9VfhqqJ+Q0LEEGkENMIw9m5hji2q2AQZgaGNfV2PQ== X-Received: by 2002:a05:6902:102e:b0:e1d:436c:3b4f with SMTP id 3f1490d57ef6-e2250cc504cmr10944928276.50.1727133104733; Mon, 23 Sep 2024 16:11:44 -0700 (PDT) Received: from localhost (fwdproxy-nha-115.fbsv.net. [2a03:2880:25ff:73::face:b00c]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6e20d15d44esm555897b3.100.2024.09.23.16.11.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 16:11:44 -0700 (PDT) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, yosryahmed@google.com, hughd@google.com, shakeel.butt@linux.dev, ryan.roberts@arm.com, ying.huang@intel.com, chrisl@kernel.org, david@redhat.com, kasong@tencent.com, willy@infradead.org, viro@zeniv.linux.org.uk, baohua@kernel.org, chengming.zhou@linux.dev, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/2] swap: shmem: remove SWAP_MAP_SHMEM Date: Mon, 23 Sep 2024 16:11:42 -0700 Message-ID: <20240923231142.4155415-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240923231142.4155415-1-nphamcs@gmail.com> References: <20240923231142.4155415-1-nphamcs@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The SWAP_MAP_SHMEM state was introduced in the commit aaa468653b4a ("swap_info: note SWAP_MAP_SHMEM"), to quickly determine if a swap entry belongs to shmem during swapoff. However, swapoff has since been rewritten in the commit b56a2d8af914 ("mm: rid swapoff of quadratic complexity"). Now having swap count =3D=3D SWAP_MAP_SHMEM value is basically the same as having swap count =3D=3D 1, and swap_shmem_alloc() behaves analogously to swap_duplicate(). Remove this state and the associated helper to simplify the state machine (both mentally and in terms of actual code). We will also have an extra state/special value that can be repurposed (for swap entries that never gets re-duplicated). Signed-off-by: Nhat Pham Reviewed-by: Yosry Ahmed --- include/linux/swap.h | 6 ------ mm/shmem.c | 2 +- mm/swapfile.c | 15 --------------- 3 files changed, 1 insertion(+), 22 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index e6ab234be7be..017f3c03ff7a 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -232,7 +232,6 @@ enum { /* Special value in first swap_map */ #define SWAP_MAP_MAX 0x3e /* Max count */ #define SWAP_MAP_BAD 0x3f /* Note page is bad */ -#define SWAP_MAP_SHMEM 0xbf /* Owned by shmem/tmpfs */ =20 /* Special value in each swap_map continuation */ #define SWAP_CONT_MAX 0x7f /* Max count */ @@ -482,7 +481,6 @@ void put_swap_folio(struct folio *folio, swp_entry_t en= try); extern swp_entry_t get_swap_page_of_type(int); extern int get_swap_pages(int n, swp_entry_t swp_entries[], int order); extern int add_swap_count_continuation(swp_entry_t, gfp_t); -extern void swap_shmem_alloc(swp_entry_t, int); extern int swap_duplicate_nr(swp_entry_t, int); extern int swapcache_prepare(swp_entry_t entry, int nr); extern void swap_free_nr(swp_entry_t entry, int nr_pages); @@ -549,10 +547,6 @@ static inline int add_swap_count_continuation(swp_entr= y_t swp, gfp_t gfp_mask) return 0; } =20 -static inline void swap_shmem_alloc(swp_entry_t swp, int nr) -{ -} - static inline int swap_duplicate_nr(swp_entry_t swp, int nr) { return 0; diff --git a/mm/shmem.c b/mm/shmem.c index 361affdf3990..1875f2521dc6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1559,7 +1559,7 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, NULL) =3D=3D 0) { shmem_recalc_inode(inode, 0, nr_pages); - swap_shmem_alloc(swap, nr_pages); + swap_duplicate_nr(swap, nr_pages); shmem_delete_from_page_cache(folio, swp_to_radix_entry(swap)); =20 mutex_unlock(&shmem_swaplist_mutex); diff --git a/mm/swapfile.c b/mm/swapfile.c index 47a2cd5f590d..cebc244ee60f 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1381,12 +1381,6 @@ static unsigned char __swap_entry_free_locked(struct= swap_info_struct *si, if (usage =3D=3D SWAP_HAS_CACHE) { VM_BUG_ON(!has_cache); has_cache =3D 0; - } else if (count =3D=3D SWAP_MAP_SHMEM) { - /* - * Or we could insist on shmem.c using a special - * swap_shmem_free() and free_shmem_swap_and_cache()... - */ - count =3D 0; } else if ((count & ~COUNT_CONTINUED) <=3D SWAP_MAP_MAX) { if (count =3D=3D COUNT_CONTINUED) { if (swap_count_continued(si, offset, count)) @@ -3686,15 +3680,6 @@ static int __swap_duplicate(swp_entry_t entry, unsig= ned char usage, int nr) return err; } =20 -/* - * Help swapoff by noting that swap entry belongs to shmem/tmpfs - * (in which case its reference count is never incremented). - */ -void swap_shmem_alloc(swp_entry_t entry, int nr) -{ - __swap_duplicate(entry, SWAP_MAP_SHMEM, nr); -} - /** * swap_duplicate_nr() - Increase reference count of nr contiguous swap en= tries * by 1. --=20 2.43.5