From patchwork Tue Dec 24 14:38:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13920195 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 E57CFE7718D for ; Tue, 24 Dec 2024 14:40:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 78EE26B009B; Tue, 24 Dec 2024 09:40:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7175C6B009C; Tue, 24 Dec 2024 09:40:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56A3C6B009D; Tue, 24 Dec 2024 09:40:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 364046B009B for ; Tue, 24 Dec 2024 09:40:02 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AF41C1A160D for ; Tue, 24 Dec 2024 14:40:01 +0000 (UTC) X-FDA: 82930110396.19.4B18299 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf08.hostedemail.com (Postfix) with ESMTP id 8F4FE16000B for ; Tue, 24 Dec 2024 14:39:33 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=d92uMNdx; spf=pass (imf08.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=ryncsn@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=1735051154; 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=9HF1qACSod1kWAoA/COSAk5nfGEmiGZxS1K9zisyvtY=; b=XVapAGn1ijWDPCirkbvg2AGbM02a1NEWEhTEay1RJoa6hsBG1/GOhnVnSbeFniEH1+FyzB QTF1sbsstyp2zk5X69U+Ik9w6uWG+QgbVu4vWo0XSdw8N54wawWOnmtxpsEq8WYUjJopFy 98V6EZ0bLrENv1VkfM9yJFwgW71+cUI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735051154; a=rsa-sha256; cv=none; b=v7U9lHulgYxIc5hniZE+26OBTo+nH3XqGlXniEnzKy5Has3WNiI8iApoCT4852EXxeqpg7 1c2w2zwtAixr79gP5daRIhElfWwxgUT9to8XA8673Ue7efAmtLVKpnKStDv7MIJRPay+FZ 1Jt7fJKRTT9d/vCnJUmdCiqpAoTUETo= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=d92uMNdx; spf=pass (imf08.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2161eb95317so55246205ad.1 for ; Tue, 24 Dec 2024 06:39:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735051198; x=1735655998; 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=9HF1qACSod1kWAoA/COSAk5nfGEmiGZxS1K9zisyvtY=; b=d92uMNdx9OPR6z5l2clgLQVMeEKSJ0i9lLoQz9QklAI7KlaNwNgZAXmTgM2UIKgGpL m8x9huOqNTjnvPIYoWvZpt/YjXtH1DjpD8+vnaW+Nu9O6Agfq1o9HIcmoQu0vIfdv6yz Msie976MANqjbnLZ/keRjGevfXMn3iYWwhc9A8Ol/B+YP51S6BmZrKRh7g9DD1wl15Jd fPcjEFgNTcfNTdsagbPCo27OXyB9JOOvU3XZR8qtG1b954frLUEwm1G1t/yjlQxBmfuS ZQVbq/TKKOCDV0yXuzClBkDEIA910BGe9HOATUfI5TvtNb0p8ihqu9j+Co0GRmdmCBOS uVDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735051198; x=1735655998; 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=9HF1qACSod1kWAoA/COSAk5nfGEmiGZxS1K9zisyvtY=; b=S9kF/ujSlJAI4bsTOBLivfGxfwC3g+tTsWrtNXq551I1q+e9bGAgFGlphQD5PyTRX2 JiyaXnIlW85q7uT0nq/oMedEjsunYT0Y8riL1epmBlkx+d+A1+kvRyIq1jRU6GNnOXdv HN/mCs08MbhV7P3uFDQ5gDDE2cacSGYc6Mg9gdS8YsuU+5Gp+ztG+tcw8vwUypzmHNDP v7OpCfG240Ukb4DMTIOleGtSgrJv3ljqTKFl8B689KcKsdqd/L5NfquvT4hO8QO9ostA 4F3rHfrh/aKUsEfmm0JqorlHi9CuvoE4b9U6tJ0AxoLcpHZpg754NvnBSkT5KSwhD0t1 BTzw== X-Gm-Message-State: AOJu0YysWhR+QqToEEuD5OZ1LLhqPOMEkChxj7F7M1/Ul0Quc6eWvRmt ske44nBhsyRkPg+LtvT6P9DH2oqQynU3CVi/YV0CEbTYbVDJn3xMRSL/3oxTWAQ= X-Gm-Gg: ASbGncue4I8onqTCbp1X8327drYtLoJYAPXz1YYRthDDwoZLWIvVC6vlYVMBjo3eV8r dnJ53RObx7plKw1IZ163TMYCopkMOtfyHqchwbITtd4cz6KNo5dXq6Bb0cDZAMOf+UiQjMklDUq /i2HzbA9JcwLXuxjm3hgUnCf/UEipNwcMkEEvRDDpADJnS1prhf2JGCPdilP3/lL4OI9PdDGa3n s02IHJkZZO4+Y+Tu0z7JuqXJrRHbbcqiXuxQIBktFKKdxQVaeIzpdf5pxF98lJlNE3aTHQPhZqm Ng== X-Google-Smtp-Source: AGHT+IG6ErdK7jWxaeL/lueO5nmWZSFVm6gM6DX2BhsbGjPUTsi72O0feo489S4NIqCP9JZBLNzB4w== X-Received: by 2002:a17:903:182:b0:216:4943:e575 with SMTP id d9443c01a7336-219e6f28547mr173619465ad.57.1735051198014; Tue, 24 Dec 2024 06:39:58 -0800 (PST) Received: from KASONG-MC4.tencent.com ([115.171.41.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9cdec1sm90598735ad.136.2024.12.24.06.39.54 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 24 Dec 2024 06:39:57 -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 v2 07/13] mm, swap: hold a reference during scan and cleanup flag usage Date: Tue, 24 Dec 2024 22:38:05 +0800 Message-ID: <20241224143811.33462-8-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241224143811.33462-1-ryncsn@gmail.com> References: <20241224143811.33462-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: 8F4FE16000B X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: oshgyg5piueizsfz8rwtqwg6etfo7t4p X-HE-Tag: 1735051173-688127 X-HE-Meta: U2FsdGVkX18LtCNs6opa5XuqNNLCjcD+JXeDCOmXAcGf1fX20+lqkq11bfx/8u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7D9POPvHsjbTk5Lvqicg5wjZMOjkpYU7C1gezE3EDaJcRE/GxhO3yLj03xwV/vjkqveZOEeTAJ5h2qBl62KN74W4gc68kf0J2oVx5UwxS/n2bIcXwFgitbGtRMwusvNVzqlsBlgJ+DJmbXEKoFI3yKZnPqfhLNs//AtxxGCdtjqHORBdL02ChN3Eq8TROUvZuXfcse8lARHlDD85VmJCxavvpAHmsaWQVwH7fV9wfGOIUv1PT9F4bBWfmGbd8rykSlQ/1fMspZVKk0lsfHzTS45gLtjibrZIGXKMF4uTrXxn/B5lbo2LNE0Hsv4/A8d41HUO7837+vOGYPsECkx3HDIthdULf0CQqqjDO/2VFr/FR/z5MBwVfK3qiTSnf+CTs2j1A== 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 ae0f7df06474..0abff343f5f0 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; @@ -1055,8 +1057,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); @@ -1065,8 +1065,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; } @@ -1108,6 +1106,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); @@ -1135,13 +1149,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); /* @@ -1292,16 +1309,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; @@ -1781,10 +1790,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; } @@ -2558,6 +2570,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; @@ -2628,6 +2659,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(); @@ -2670,15 +2703,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;