From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 7B95F1B0434 for ; Mon, 30 Dec 2024 17:47:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735580823; cv=none; b=d5HldV8hEs8MCL8eS1+V8G2e84inlj7Rwlk+PBL8v3rjIl/HUilN1UD3deJ9uiyKlDANVQIfCEDnEhpfVp+yXT21ayhzsrAN+HDGuBpZwuuISkLN0jD+09ia1utbOHlxEFenUFg8FUg3yUrLXYwCDaVP7dE/tpF43xC6wgpmwLM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735580823; c=relaxed/simple; bh=LmzwFFurv0kqij7huLnhTmBpYng3HXUBo0wIAKh8OfI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VoVJVtzbMe/wkvtZSdxgwoWpIRfTsZ7DBP3MQws06A2vjBFM97BlBL2x8ECLYz+cU2eHvENqoTDrE9eXBJtycIoA2WlsYqgnaZea/8DTPiFnOOcK2XfvfUpbKyw8gS3ZaDXS6Nkrex9UeG4rFUhuw5SsrpP+pohz94iIN9ezw1E= 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=kOx6+LeA; arc=none smtp.client-ip=209.85.214.173 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="kOx6+LeA" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-216401de828so116481765ad.3 for ; Mon, 30 Dec 2024 09:47:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580821; x=1736185621; darn=vger.kernel.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=L7fSMJo0m5jtrmJwyl3l9osZUE7hgdb18JM8bl2HsT4=; b=kOx6+LeA8d1NntOGoBAxDjjb5XmMW8iLFHiArSfGP+W83ul4FsCfPlmAY8o8tpnsqf jRlzs5Ao/4m72Nz4DvgDrK5Zb8dmwXfUvgTGgLpCVMxO7A9K9/6JNpeV/t63RYUGI8kL AyZX2I43dh5Yfw4iCyAM5kwlK0SzH2gY7J9ueK8tiX0MEUk7c7AtpiOoyg9X9om/AIzI akag2TLv8u4oGlZpNHcPNtBAqHHf8VzUm/POXap/wmSJF1ZzfdpOkUE1Rx54r1g+rEmi hJYgRQ8u0QPpd5QAbxLqFrB3PowvaIw8DmgwUenBPB+AEuEs4ElAcRb8Xb2WPxIAak7+ CEpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580821; x=1736185621; 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=L7fSMJo0m5jtrmJwyl3l9osZUE7hgdb18JM8bl2HsT4=; b=KbsTTSt7yCdjX15iPbMEi2b6dkGhsu9HS2H7bc7G8f5ckEJ0/kAZeVaz/RKpECErax JtV7/5sWc6kue53Km2mJkELwf/qWel0ZAZn8kSNaQjBgzgzlHR2mQ+koOsFq+McQExrV rBE41j4gc8scUK236w/wgILSRLrDB62lrke7n6SzWP7eVySX1Y8dDOz44CC8+zG2W9b6 lJRV1S/lpUQQ2ZXDwSoIvG5MguVnS05RCOFNDbLG9RN4nP+zWj/YKMXOdzXM9W+HR+OZ j3GO0RDTO91lmvPZWnpVgNpS0kuBsUaKXVnhrKZsP9rJnIIGgjwPO9pPlwx7HRdVnRRO 0H3w== X-Forwarded-Encrypted: i=1; AJvYcCWnTO8aKk9eXWqd+ixfJLsK2cPLsTVdU76bDac0f/pePFwrep4jnOA4qEv8S+bqiAse2yy2NXfX/PzOSjw=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1ObaOa4p6BdV1na7Cf4w9S1DmaTlb9pjr32ns7hHnCafsfYgl f7ApzQMVh1Ad3H+fq0Eie24IJf1aZcqtxOsdP5eEgqFAdATi8SeR X-Gm-Gg: ASbGncuW3E2hQsSQjXBlTBnL0UyHdJNGk0nQuTSleRdthGkRtiBlTI8pKz6M5KJQUJC 5lA0Wo1E3eBmeGXCWdtA37qzTdXPbdhc9K60SVWYprxbscoc8wQ4c60j/0yjEgWA3bmLvN4WGsJ 8/Ilgbhyr3+jGIAla2ruSfhYG9FBWeTKSCi2iv3/688n1Nb9/4AUWDJMPR6xfoJBgmUCJQHjP9V 6OCDNryxw5wUKc1Vjzx7/cCImYB4D6l53egmX+FctqXVNFi5UvTZKReUHNxg+VBx16FqHgATJrT X-Google-Smtp-Source: AGHT+IEaklwHFUSAvxBX58uwXC3bT8F5Ixs3unjPgxjsAOrETxagUzSGxkOOR/d+DYAMJriw3/P6zw== X-Received: by 2002:a17:902:f607:b0:212:5786:7bbe with SMTP id d9443c01a7336-219e6ea1baamr557334885ad.24.1735580820747; Mon, 30 Dec 2024 09:47:00 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.231]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9cdf25sm180687695ad.118.2024.12.30.09.46.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:00 -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" , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v3 07/13] mm, swap: hold a reference during scan and cleanup flag usage Date: Tue, 31 Dec 2024 01:46:15 +0800 Message-ID: <20241230174621.61185-8-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241230174621.61185-1-ryncsn@gmail.com> References: <20241230174621.61185-1-ryncsn@gmail.com> Reply-To: Kairui Song Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 e6e58cfb5178..99fd0b0d84a2 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; -- 2.47.1