From patchwork Mon Jan 13 17:57:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13937873 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 0F503C02184 for ; Mon, 13 Jan 2025 18:00:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FEE56B009E; Mon, 13 Jan 2025 13:00:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 987756B009F; Mon, 13 Jan 2025 13:00:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 801B46B00A0; Mon, 13 Jan 2025 13:00:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 5E89E6B009E for ; Mon, 13 Jan 2025 13:00:22 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0EFE81401EE for ; Mon, 13 Jan 2025 18:00:22 +0000 (UTC) X-FDA: 83003193084.06.DC1ACE4 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf30.hostedemail.com (Postfix) with ESMTP id 1EE0C80027 for ; Mon, 13 Jan 2025 18:00:19 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UNix00e0; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736791220; a=rsa-sha256; cv=none; b=3TL0RLF5WgKvgMI9EUFSxr0b3Nn6xo4soVhG4de4WAJE1EdZ9JMjHB39rzASINtnX8gDXP +3U1P/SkSKJ5lSoh7lVQ4XIeqGSMGWkx6NX0Y0ZuiW6AdBGdpCJrfC1me+Y4N1sOLdpBu9 YxVyIZPhX7t6stvj5EyUJY9PLyhWcs4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UNix00e0; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736791220; 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=dWh63O4GHmxsuBKSbBliKUVuioOhHJ78YLSnvJ5urEw=; b=jaxn/TaF/IPe6QSOZ5pDbrP4VMpJneWpJA+FmsPxvXGTg/y3pskAYQ2+hiP7/LObXooQYW P1nTYQGx3FSVfQkZjeWG/Mg/dvsX1cLTjJxBCTIIdaVErkVQtLB/pSA4jBaM8dCvg33nwO HFsLnnNRdeOxoB2iwvlp+M6saPp3W9w= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2165cb60719so82362585ad.0 for ; Mon, 13 Jan 2025 10:00:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736791218; x=1737396018; darn=kvack.org; 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=dWh63O4GHmxsuBKSbBliKUVuioOhHJ78YLSnvJ5urEw=; b=UNix00e0U9iJAxL1zq/OJSXLKp+CJoIx45sN7R59i5okEyWznaojZBDDJfesG3kbVq uaTISa0xX7DIuig5RiEQJ+hhQvIUMBE5xp1RzLIuIGyjVJ5wQkbxjKv7IL4K8eldvBE+ 22T6GEKsqDH6mFC2k3ko8z9G32scvZBjwzE00T0cw2/nXUbSL7QsgL7iMLDMf4VVNvN7 rHHJfHVYznf2tf/luXEHvQN8Xc9lVoa09zsnoMmefAw5Ikowc0akw5Y4xbweOFcGRBfb vUZaTjAvOy4T+QmwxKSvwXi5q254vC1gl4n/f/qkMR15xac7A5QgjhRYWTcKup9AZg7i juRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736791218; x=1737396018; 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=dWh63O4GHmxsuBKSbBliKUVuioOhHJ78YLSnvJ5urEw=; b=tjILilRkwH/p4GzlAch1+Hc9GseLCwk1V4nQPy+9v+K3jqPGVu7P2N5dwMedqwG2kb WS4/S/SNCEDAoHDXbe59mhCzt4FumUygimtz1p9DmD/3UnzgLaxNA/ofIdEgP2Xius17 b3KhKUTLtEgah4GxkOcOKS8p03/lJYRz8GxGyxI47Ynch9JzAL2SU1CTaN5Iez89kulO FYJQjAuYTRMaBI38eoYLLb5jfxQ19bLCsbQ3ZL6AtrrJSV4RbWVX7+I2N/hjfxfDuuij Xnoen49WDGpb4w+HQqr/V/yQTu/1HJqNUKXjA8RIc6A2kmSPvkNtp0TJe0zrM8NKBj8K Q2oA== X-Gm-Message-State: AOJu0YzCDwhh8WthgkYU8UrxP104zG8H4kZ7OAdcgaKQPdkmRTxigq4n /NyscQoX8Lu6NdFYmH74jKYcaWObwOGiUea1e3btGf4E41ppGP61UbV/y9VJqkY= X-Gm-Gg: ASbGncvetFFsL6i6AX5JhBaPndlOn3bZxQzR/ccQM7NFAW+I/XmF9NAQpzY+/GD8nzo yYwEFQuhjz1x1f1kHHYo1DQ88Tg0nxJbEcNYJRTEsYzB3xum44HoFFUVeLCMHnsW7fDIIwNjEMA FXxwnOspUj2U31vzQizuN4/5i+rIo0mnZ6eMak/Dlv1MwmXMp71lpEzbOcFjMDQa+Ep7XYoZJp2 FVOJWBRL1G3bvbc/euz+nzh2qE/+GguX9NV5Qun7YnbbPrTO8Z547Llg93KbKppH1F64K//WAEl yQ== X-Google-Smtp-Source: AGHT+IFkbMojpX7s6VN8Lz/QQdWEvSeMZ/ugRu8uwONPdYOHeJ6nn0l7HLskgv5eoQxu2cxPIS9zAg== X-Received: by 2002:a17:903:2311:b0:215:a2e2:53ff with SMTP id d9443c01a7336-21a83f36e1dmr352526495ad.11.1736791218117; Mon, 13 Jan 2025 10:00:18 -0800 (PST) Received: from KASONG-MC4.tencent.com ([115.171.41.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f21aba7sm57023635ad.113.2025.01.13.10.00.14 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 10:00:17 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Ryan Roberts , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v4 07/13] mm, swap: hold a reference during scan and cleanup flag usage Date: Tue, 14 Jan 2025 01:57:26 +0800 Message-ID: <20250113175732.48099-8-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250113175732.48099-1-ryncsn@gmail.com> References: <20250113175732.48099-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1EE0C80027 X-Stat-Signature: xc6nhmqzab5e81xje6dqqqzei7bcn997 X-HE-Tag: 1736791219-622357 X-HE-Meta: U2FsdGVkX18w9qrWqssp4L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WuRnjVj0CXBuF2I+tV+0CsBo4c1LjbzdkNKTFteOkNDcqQeRPBJ0/F1b75efB7hVv3HXKbmgQZ8N1U/QiG2/KBhFRa1BLJuTMCeCu7Umti5p+6lrykmCYM6UBWA3jiw5xvuTCEEhLz3rBcUChZPKKWh/f+9NUnqVAru+/h6oT9ZHWrDkxw7I7WKmi9KDITEY3bSi9X5Q9IToe9K38RgTLUTK2V3zv2rY6YpU66f+X/JFle0HuJIOI1PJQpYpeayTBxgxFsKSj+ykETE/PKqTDQAcheVjBCxt1AHUI9JeLQzVmK4xq2sBH2no6mqL/tFKrkKRt+5xQOMomMNfxoEVNU2VtzTg7AIXcn5HzefOKEEqq67y/7TmzfjXqCLl7RqbODIjNTELNUXpD3T96dC4NAE9yzXBw== 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: Kairui Song The flag SWP_SCANNING was used as an indicator of whether a device is being scanned for allocation, and prevents swapoff. Combined with SWP_WRITEOK, they work as a set of barriers for a clean swapoff: 1. Swapoff clears SWP_WRITEOK, allocation requests will see ~SWP_WRITEOK and abort as it's serialized by si->lock. 2. Swapoff unuses all allocated entries. 3. Swapoff waits for SWP_SCANNING flag to be cleared, so ongoing allocations will stop, preventing UAF. 4. Now swapoff can free everything safely. This will make the allocation path have a hard dependency on si->lock. Allocation always have to acquire si->lock first for setting SWP_SCANNING and checking SWP_WRITEOK. This commit removes this flag, and just uses the existing per-CPU refcount instead to prevent UAF in step 3, which serves well for such usage without dependency on si->lock, and scales very well too. Just hold a reference during the whole scan and allocation process. Swapoff will kill and wait for the counter. And for preventing any allocation from happening after step 1 so the unuse in step 2 can ensure all slots are free, swapoff will acquire the ci->lock of each cluster one by one to ensure all allocations see ~SWP_WRITEOK and abort. This way these dependences on si->lock are gone. And worth noting we can't kill the refcount as the first step for swapoff as the unuse process have to acquire the refcount. Signed-off-by: Kairui Song --- include/linux/swap.h | 1 - mm/swapfile.c | 90 ++++++++++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index e1eeea6307cd..02120f1005d5 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -219,7 +219,6 @@ enum { SWP_STABLE_WRITES = (1 << 11), /* no overwrite PG_writeback pages */ SWP_SYNCHRONOUS_IO = (1 << 12), /* synchronous IO is efficient */ /* add others here before... */ - SWP_SCANNING = (1 << 14), /* refcount in scan_swap_map */ }; #define SWAP_CLUSTER_MAX 32UL diff --git a/mm/swapfile.c b/mm/swapfile.c index 91faf2073006..3898576f947a 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -658,6 +658,8 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster { unsigned int nr_pages = 1 << order; + lockdep_assert_held(&ci->lock); + if (!(si->flags & SWP_WRITEOK)) return false; @@ -1059,8 +1061,6 @@ static int cluster_alloc_swap(struct swap_info_struct *si, { int n_ret = 0; - si->flags += SWP_SCANNING; - while (n_ret < nr) { unsigned long offset = cluster_alloc_swap_entry(si, order, usage); @@ -1069,8 +1069,6 @@ static int cluster_alloc_swap(struct swap_info_struct *si, slots[n_ret++] = swp_entry(si->type, offset); } - si->flags -= SWP_SCANNING; - return n_ret; } @@ -1112,6 +1110,22 @@ static int scan_swap_map_slots(struct swap_info_struct *si, return cluster_alloc_swap(si, usage, nr, slots, order); } +static bool get_swap_device_info(struct swap_info_struct *si) +{ + if (!percpu_ref_tryget_live(&si->users)) + return false; + /* + * Guarantee the si->users are checked before accessing other + * fields of swap_info_struct, and si->flags (SWP_WRITEOK) is + * up to dated. + * + * Paired with the spin_unlock() after setup_swap_info() in + * enable_swap_info(), and smp_wmb() in swapoff. + */ + smp_rmb(); + return true; +} + int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) { int order = swap_entry_order(entry_order); @@ -1139,13 +1153,16 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) /* requeue si to after same-priority siblings */ plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); spin_unlock(&swap_avail_lock); - spin_lock(&si->lock); - n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, - n_goal, swp_entries, order); - spin_unlock(&si->lock); - if (n_ret || size > 1) - goto check_out; - cond_resched(); + if (get_swap_device_info(si)) { + spin_lock(&si->lock); + n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, + n_goal, swp_entries, order); + spin_unlock(&si->lock); + put_swap_device(si); + if (n_ret || size > 1) + goto check_out; + cond_resched(); + } spin_lock(&swap_avail_lock); /* @@ -1296,16 +1313,8 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry) si = swp_swap_info(entry); if (!si) goto bad_nofile; - if (!percpu_ref_tryget_live(&si->users)) + if (!get_swap_device_info(si)) goto out; - /* - * Guarantee the si->users are checked before accessing other - * fields of swap_info_struct. - * - * Paired with the spin_unlock() after setup_swap_info() in - * enable_swap_info(). - */ - smp_rmb(); offset = swp_offset(entry); if (offset >= si->max) goto put_out; @@ -1785,10 +1794,13 @@ swp_entry_t get_swap_page_of_type(int type) goto fail; /* This is called for allocating swap entry, not cache */ - spin_lock(&si->lock); - if ((si->flags & SWP_WRITEOK) && scan_swap_map_slots(si, 1, 1, &entry, 0)) - atomic_long_dec(&nr_swap_pages); - spin_unlock(&si->lock); + if (get_swap_device_info(si)) { + spin_lock(&si->lock); + if ((si->flags & SWP_WRITEOK) && scan_swap_map_slots(si, 1, 1, &entry, 0)) + atomic_long_dec(&nr_swap_pages); + spin_unlock(&si->lock); + put_swap_device(si); + } fail: return entry; } @@ -2562,6 +2574,25 @@ bool has_usable_swap(void) return ret; } +/* + * Called after clearing SWP_WRITEOK, ensures cluster_alloc_range + * see the updated flags, so there will be no more allocations. + */ +static void wait_for_allocation(struct swap_info_struct *si) +{ + unsigned long offset; + unsigned long end = ALIGN(si->max, SWAPFILE_CLUSTER); + struct swap_cluster_info *ci; + + BUG_ON(si->flags & SWP_WRITEOK); + + for (offset = 0; offset < end; offset += SWAPFILE_CLUSTER) { + ci = lock_cluster(si, offset); + unlock_cluster(ci); + offset += SWAPFILE_CLUSTER; + } +} + SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) { struct swap_info_struct *p = NULL; @@ -2632,6 +2663,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) spin_unlock(&p->lock); spin_unlock(&swap_lock); + wait_for_allocation(p); + disable_swap_slots_cache_lock(); set_current_oom_origin(); @@ -2674,15 +2707,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) spin_lock(&p->lock); drain_mmlist(); - /* wait for anyone still in scan_swap_map_slots */ - while (p->flags >= SWP_SCANNING) { - spin_unlock(&p->lock); - spin_unlock(&swap_lock); - schedule_timeout_uninterruptible(1); - spin_lock(&swap_lock); - spin_lock(&p->lock); - } - swap_file = p->swap_file; p->swap_file = NULL; p->max = 0;