From patchwork Mon Dec 30 17:46:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923379 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 310A3E77188 for ; Mon, 30 Dec 2024 17:46:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A83F66B0085; Mon, 30 Dec 2024 12:46:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A33106B0088; Mon, 30 Dec 2024 12:46:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8ACE46B0089; Mon, 30 Dec 2024 12:46:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6593C6B0085 for ; Mon, 30 Dec 2024 12:46:43 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 237DEA01B4 for ; Mon, 30 Dec 2024 17:46:43 +0000 (UTC) X-FDA: 82952354016.10.9605238 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf22.hostedemail.com (Postfix) with ESMTP id 902A2C0005 for ; Mon, 30 Dec 2024 17:45:52 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BQgilxEB; spf=pass (imf22.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.52 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=1735580780; 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=ByQEC8FsvBO2uOH/tvc24OCSxXwsTLkJSMXrTAjB9ew=; b=CKKPEspAgmXT62DhFYvzEwVVoT1S0K4F8g1Ll4+zL2hj2SeP9bGZg/wrmVyhWYB62WJ6mG 19bU1Bvo0z3d17+NS7COvmF2nv+LWvpR4erex8R3HI7BdOEaEnfzHgwzr+QitIzIw6JTqT cFd4ACSN5Ve2Cjdvcji1soer2P1x9D4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BQgilxEB; spf=pass (imf22.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580780; a=rsa-sha256; cv=none; b=mZlzqxDH9NkA0VzNs4PycJqwBuAp68XaMztw6C60yBuLzaZJcR7wQ3tBeZMzBIZajfHxZ4 zgGbLrk4NR4G2BUn+k59OuQ12slxsVMgQ7qN5OqkcZfe6M95aRNEYgZCq+gvOaxZ9If6KV OKnzNRZswEWaPsDCoQNBNElFPcVWbss= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2ee67e9287fso12466495a91.0 for ; Mon, 30 Dec 2024 09:46:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580799; x=1736185599; 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=ByQEC8FsvBO2uOH/tvc24OCSxXwsTLkJSMXrTAjB9ew=; b=BQgilxEBUs51+Kpw53/YBbEIq2aawPLytFErJozsCKpZdx7cejQzFcj//KOK7FiomS glIdoUhhg9XZ71aE+vjuMuNJeBccuQ2csW1Gw3Z/B1VuUcUe/ZEZ7Izjg4lL4imQkom+ PIA19VMRYxCwOp+GGv3Ot/sh2ZstBtmScj/HgbmfEfpcR/fdFlcarXyO/ukb12vaIVac CQwmr3mizr/U/UM/x3BfgDGtGVemDxI6IohKRxjp4Z/2caXvDDcFkXiK9UhFFGEGfzIC 5SS9Zy8muQIj/dDwKQE/TlGucTODD86Be6W1Y+IhkkEtTMEUN3DIhfbMpF8/cJW0I8N1 RwPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580799; x=1736185599; 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=ByQEC8FsvBO2uOH/tvc24OCSxXwsTLkJSMXrTAjB9ew=; b=T3mCWzMyVbNpKRC8XwZy1XEEX2eafzag3C6t+z9yH+BBrSbsPWsZID4VWcKEyce25w 2liW+ku3mnFkrLWQOH6bHjZLsjp6x0UakOGFfkLNZA/TjxCDhnHYDTcPIYszsS2RMTLL RS0hV8+8vwFV6/aI3GTpRLTiAXPlzv+jLeON+Ms4dbrXompj5ioUuZiuO2eoSm5zoM+u I6VpL0YO8Qv3M2paup1RA2H5WG/5+rPCeG1DLGPoFK/AX3X7SRhQu5QRCOQNPTng4ti6 qHKmTYrlfM9Da1R0hOmlHIVSPur3KPtPkdps4WbxLDg79w6i0sGfwn0N1SxQOy5d7vRr lJkA== X-Gm-Message-State: AOJu0YzT8RRp+EZ54pYRjIH8G11C0s1C8HKr0iyiivjaOqfX1VAc+ePW KNpSc/jsfKcapbZQ27eQLQDgjRnO/+WcVgz3yrBKtY/40K0s2xBPJNou8/Qvxch3mw== X-Gm-Gg: ASbGncs8XbAP+IKgHwCW/LQ4CENdzCJ+ceBFVjCjwhFH6alnBLBBAovWHJJcNDvFn+4 YdpB49I6sCa+B34bXrbPFXwp0wYTNxnwUkBgC4pSjZWLKwytvQIu0mLU/7JffrdvIaLvQqd7+EH mIvteYMfesWX1xYaE1AOxAhchfrGg4T8BQFtd1Jt0HL3gmd4xw6d9bI8ie11djncVgY6PsZvaXt uqBPFQQZjdsMkfSNa8yHtP8JYvdJF6KCAQa/Mv3W1AaVL8eoj9CNg9YJTH6CZ35t3h5cb83Q0+8 X-Google-Smtp-Source: AGHT+IEpwr2sGmtXS7EYPdFpiPU2k4iF8zS23kwgmM7X2KfZ2TC2ulrhiLKT3yI/wIFjfHbUR/OxOA== X-Received: by 2002:a17:90a:d448:b0:2ee:5edc:489 with SMTP id 98e67ed59e1d1-2f452ee6dddmr47363487a91.26.1735580798893; Mon, 30 Dec 2024 09:46:38 -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.35 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:46:38 -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 01/13] mm, swap: minor clean up for swap entry allocation Date: Tue, 31 Dec 2024 01:46:09 +0800 Message-ID: <20241230174621.61185-2-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 MIME-Version: 1.0 X-Rspamd-Queue-Id: 902A2C0005 X-Stat-Signature: 4xfbw7ae5dnweww4cxm7wkmjz1mbex8q X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1735580752-161925 X-HE-Meta: U2FsdGVkX1+nZM6kw/LN3IHS/K4yy8KHlgXhfwIq5z2Weo8IWfYEAoB/9olLSyVurtgCTcdcBxCtIoandKp5xGUnmxgQs5BQ8efVdDxxNk/XEBYBZnfuPY728xVpPvgJ3rrMPo/tXlA2iyrl99/rdwxiVvEIx83Ital8Ij6vlkjP06gHB6WXSIKYVsdvNXgcC/wqWhOU1ta8QdktOD7qjB7AXWuXxSuU5O4xPkjRA+y8mhjMwt7KrQ3cSR3yT9JkHJghZ9cdaBdcjD6wawvYmPUwjrs8ZupzoGPXPUdts+4MGnS5jVSE61lUNRUZSvrccIQCl3QeG+gm7JtXcw3WZRLL2kN/i7FcRY7QKKDshkCYGjtYqYCKinW3sv1ZldHhorq+PFLkOyCy/9oYD31r1LQWaJOrdYfwON5St5ORMJPR4dtrsI28cTFwlwXRA/j9vfPjU+ZkpVclSYi9HxsEmrhobFKKl+c8mwtLo6y83OsjntZKUqT6TZ58y6gnfdKj2+1ursvxkieqXphuCAt8Sq5xPzicaehJvZdsBWNJ75FoPNELZZ80lQ29yNGFbZ8wfXf5WATVV+peS1p+310f9Yjiq1vTmyhhQUcvvx20R1bCgBHZ9noIJY22fa2vDI22MGFE2zxWM4ejI23mSlE7K09nG4pnTguPKGW71PRMUGN4Cx70CYnZDnjB+DbpNBN1zo1jFjDhN1c+JBNBEOQGmPaKIHSzFu+9RRYRWJKiq6IcGAch5Uju1CuFEBMwOc9TzbF5dz13TKiZoafOSCrawD0Ewwt5Sxx8TGmsRef2XgxBADas+R16s4BphDkOFQH+wiKuXRSGGDxHAte7Zy/AxcVRRVH7LSuMlbAU0t5bEcWk/v370D5SHOW0auz4KItzwVn4ZAHIkBeR/33NOcbWHp1MD3sXU0KLKcrZU8QOO+9/vScmMAb0shB6mWf4064Yy5RnEPafDfBhfR55OsS BReEXtQ2 nsPY+jTANs9EGsIl15cnKCIXHFfFXaX3p8OPOOxyMzlqcxSV3iEhq2ZCpEyfPtuDkDosOcMk1VelU8G60wWY/xg122X1OgRzmunIuMXLE8BMIFxLahQkGkdEVFO2jWbziD78OBkftI+csk/TlcmLIT+93ig/V/ah6be0qjupE75VRIAEu4TlkweFfF+lNK8NV1130Ej3Kn1UOnuWIaK/ym5O+4GpnMtZ7tbPqjpGz8fSgtPvpTBDFlVbJ7PhbWkzDFhxaqFtKmKzLLDhFiU/q4t4995+csG96gcz+9QYCrzXYhKU1zkcAsvuKfu1rLdUh8QSxDXAeMotbOe2RlPecx8Hv+4o/t6H4+kPFLRHjOP9UsRSF3lM6ixjlISb9cPDwu4+NwxRwnxKexDRSRxqQCqpjJCgYSTJLo+rERESvl/maIseQyGhi5FxU7aonKABYrmNyjjL9bqzrKbz2QOShJXfiTYT2bImz7OkHAfpnEu7QERs/ZruAj9bkhwL++NxwgD2u6iW84W0LyybsM6gymOm2XM5Avw2T7vo7G6ZK8OruEWBCkv5nq/3wew== 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 Direct reclaim can skip the whole folio after reclaimed a set of folio based slots. Also simplify the code for allocation, reduce indention. Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- mm/swapfile.c | 59 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index b0a9071cfe1d..f8002f110104 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -604,23 +604,28 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, unsigned long start, unsigned long end) { unsigned char *map = si->swap_map; - unsigned long offset; + unsigned long offset = start; + int nr_reclaim; spin_unlock(&ci->lock); spin_unlock(&si->lock); - for (offset = start; offset < end; offset++) { + do { switch (READ_ONCE(map[offset])) { case 0: - continue; + offset++; + break; case SWAP_HAS_CACHE: - if (__try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_DIRECT) > 0) - continue; - goto out; + nr_reclaim = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_DIRECT); + if (nr_reclaim > 0) + offset += nr_reclaim; + else + goto out; + break; default: goto out; } - } + } while (offset < end); out: spin_lock(&si->lock); spin_lock(&ci->lock); @@ -838,35 +843,30 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o &found, order, usage); frags++; if (found) - break; + goto done; } - if (!found) { + /* + * Nonfull clusters are moved to frag tail if we reached + * here, count them too, don't over scan the frag list. + */ + while (frags < si->frag_cluster_nr[order]) { + ci = list_first_entry(&si->frag_clusters[order], + struct swap_cluster_info, list); /* - * Nonfull clusters are moved to frag tail if we reached - * here, count them too, don't over scan the frag list. + * Rotate the frag list to iterate, they were all failing + * high order allocation or moved here due to per-CPU usage, + * this help keeping usable cluster ahead. */ - while (frags < si->frag_cluster_nr[order]) { - ci = list_first_entry(&si->frag_clusters[order], - struct swap_cluster_info, list); - /* - * Rotate the frag list to iterate, they were all failing - * high order allocation or moved here due to per-CPU usage, - * this help keeping usable cluster ahead. - */ - list_move_tail(&ci->list, &si->frag_clusters[order]); - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); - frags++; - if (found) - break; - } + list_move_tail(&ci->list, &si->frag_clusters[order]); + offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), + &found, order, usage); + frags++; + if (found) + goto done; } } - if (found) - goto done; - if (!list_empty(&si->discard_clusters)) { /* * we don't have free cluster but have some clusters in @@ -904,7 +904,6 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o goto done; } } - done: cluster->next[order] = offset; return found; From patchwork Mon Dec 30 17:46:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923380 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 81E64E77188 for ; Mon, 30 Dec 2024 17:46:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04BE26B0089; Mon, 30 Dec 2024 12:46:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3C1E6B008A; Mon, 30 Dec 2024 12:46:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDD676B008C; Mon, 30 Dec 2024 12:46:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BDD6C6B0089 for ; Mon, 30 Dec 2024 12:46:46 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 71BADB060E for ; Mon, 30 Dec 2024 17:46:46 +0000 (UTC) X-FDA: 82952354142.23.70BF82D Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf22.hostedemail.com (Postfix) with ESMTP id E7568C0015 for ; Mon, 30 Dec 2024 17:45:55 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="jHNz/891"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580760; a=rsa-sha256; cv=none; b=pm3pdoaguJ+ymfPZMlkd5NWtbm0BLSi+/ksD7Cqj0PUKhsnFFpPk0LUJxcabX0ohmEDr2F YlGEoFgWZDXs+5caekONaQDSy0Mirs5MHXIiImDGLTcZVb9+HHMW6NXxyHFeRSSMP/feMv pHyz8N2gX0u10dHzzVOgPi7eRSaPJ/A= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="jHNz/891"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.170 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=1735580760; 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=rZtcJl9C6ncR1PQN3HhaQn7BWhIvEYupFW/4U59A+e4=; b=bnmX3pmtZN4TB/lRiiXwijqsAF4BuYTnZ6pQ/n2rFv7qTXnhhUnKwtYjBKoawvhREeXFfe VZum8HjP9+gSLbz1wghDazySRcB4jXH/nuRln5ft5Tfgh+O2qc7yh3/jBfLcMHUgqEUEEd /HTdGK1zoOoHL3AxnxJVIzu/QPoyqsg= Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-21636268e43so25560285ad.2 for ; Mon, 30 Dec 2024 09:46:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580802; x=1736185602; 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=rZtcJl9C6ncR1PQN3HhaQn7BWhIvEYupFW/4U59A+e4=; b=jHNz/891BcSxox2YsNUnt8jXWHBhBnyvUBGB4Tr+2H4vSqIR9FDWvOWBuUulSnkp7t Qqv5dWW4r2r3HdwBHBQahc/fzZNCqxiCejEAltfSGxhq1xabmHVqU6I8jFSPTcNPssm+ NeSp0QPj4GMXNAnTh+YO/wCdbb3BsLY2Wp/Zsv57EXhoWtzsX320soT4m8R33TRFQzhZ QRl+60pwBLnjy3ES1GSFjkBjSBZ/n8n4owgpaPb0UgE5K7xOtg3cQ1DqskxL0VIZROm1 SJs9cmR+Eyo8WIAA1OooTh+axWp5/go9alkAoFRl6nL0u8mZAcc0sNgy83SpGCAjXT6e kT7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580802; x=1736185602; 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=rZtcJl9C6ncR1PQN3HhaQn7BWhIvEYupFW/4U59A+e4=; b=miAsC7ARQCz3Ff0Fl/bk14KHAXdfUknUrrHLykCyxKYx5McR8PWgujDy9MQvzAqCwd nslg18U+/OcyeFnaICAW6Xz4+GNlshjHP9YJ1lI2k/HUWtiGiuoPJef32m5VxSBGwe/N mfPa/O1m0W6OZnxXDDEbcXiF048N8VvpwfAUMGnKdKO/c5p3r8lyzwBr1yQGbAwoYLWg HserIM+H7XfgJdGV8AahTXRctbT5B1PorBnZP49Yo9vk8oWBCWL7WB3MWmveeu62/DA0 Nqv3P175eLVz2cgYYp0cMDmyLeEs2G5+iBDYPL6CCRXOGwqhdeAvNR/FkFgesZhKsDUv xlvA== X-Gm-Message-State: AOJu0Yz7aw46+VDKEt2be1eEco/qSLWy4DNvyTPRidA7c289sFUBvr0Q vIKyxvaQCPKR5jhRi3UOYHTafVitvk8lmjzae7dD4yBTH1bHyoIptNuX6YXNQLCozA== X-Gm-Gg: ASbGncsElDtORSsT0G0toCecylI7oiQmJtCrcQwNXpKDfKYGBFK3HN8JCHhs3M2Q72A WMupIEiDsw3YrSvMrpO/FRuspe89nBJJpd2OfQjXIbPg1r9hncn4YGi0icMvKquSBAnFNXjcdld uHIkIi/pPZPy91TpX4wtwVXSzQbQrE4VkoynTGb8gb/GCFEXSjJI1mWbixPzQwATAM+n4zjxxv/ sGN2y/OWoVTtiOlN4HHWaEpUpyu6myppEjjcrqlxhCTaqRQoHZaYYpDRD3LiMHp0RKSxkXHiwmy X-Google-Smtp-Source: AGHT+IGH7NBcYgvpsSru6Cqjs4ALQbwGu05N3l2ACnPnzFiwqgWBlvKXNj5Nr+qZiwuAszQVoGrkOw== X-Received: by 2002:a17:903:22c3:b0:216:6769:9eea with SMTP id d9443c01a7336-219e6f133ccmr516396175ad.37.1735580802454; Mon, 30 Dec 2024 09:46:42 -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.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:46:42 -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 02/13] mm, swap: fold swap_info_get_cont in the only caller Date: Tue, 31 Dec 2024 01:46:10 +0800 Message-ID: <20241230174621.61185-3-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 MIME-Version: 1.0 X-Stat-Signature: kffma5zdxb6wuemwxs8r8femt34zu65m X-Rspam-User: X-Rspamd-Queue-Id: E7568C0015 X-Rspamd-Server: rspam08 X-HE-Tag: 1735580755-941276 X-HE-Meta: U2FsdGVkX1+xmZPWZBD/LKESK6x7s3SMl8Nq2fy4sSKPN+h7dzS92RsSpZ/aiomwoxU5XDDe4TTl+7Obf86rnTvmFiooTIvHS7pQ7m/yzgmeRIgWyXbRG+muw8vklYz9wMSbvhALNDcnUHzjOpQ4lScTLFdPS/Utg6K5LoWG0D+3NEa8SymiHd9goevRGM/CfsIyRnaaNEiVqE/LYqlUsj5pU8iMSoUWrbunEvtWFjRL1UtDucvwYlB8sU3p+QahUeKwm8Ui/dxuMoiTKqZExVNlX/+CAmIU3O6Vx3XhPIM8+B5Q6Oj+POzyfOqRPI5Q9vXDhCl+tcdxZUgdErQjZYcDtr/cYOKSi2TGGIgMJKinBbxSz2t++gDIGM5QQ8jtAIHcxvfLpodK2n0o3quhq8y+cKQutcD5t/ohlM4eTRJxcwCxOG4JbfaCvDZsro1FCw48+/YrJVuDivaXqsQ0RD+9j43Qq1RW2vr6v7+EP8xxNKcnWSd2sqzi5f6exO5qkDBn4kePpk5TKn8iYILq3XAqNTJEvpGAEvC84zHzS3fLpiUBkoYbsN/3jvo555eHWJUOtBA4JDc/fyRo9397m6Cnrml3m93E9siEtvo7jUwnqWAcHLzcjKsbtY+gMvi5cvqTuzRGpS3bpLxC1Yo7MyrvOXxwJCDK/o6HILirTVllUOdhirwJwK4c0tzBPV5o7kVFqaPhKqJFHadoA7m+8ZAmj9kHV4A2nFOj5W0uHK/BKFX2TBxeVHvhE0fa4Ve+xc8a5155d4eVGVi4dGD4IEg5MGh3GdXx+HGozv/F2vbL3ORD9s9NGMnEL+bDyFoxqVhjUAeL6cxX5n4zvj0iCmmEEUBEkD2v8qY451doqHBSuLnCmAmVyvL9qRDAUFA9CZ5cWG2ovYwBNMrJv+iwMSzRKA2ObgyaUyY3ijZt7nKtUsSze8tVePqPl1cFYycsQMfZr7KRf+waunkY4ty WdMxngrR 196Fozz49bIg9jJRDHtCfNYSNBynn7BONcBXEbxaBrX8lUyboygVOMwbERQkxgi9gX3cN1C/MYF//x19MQiSRpT8dgPsjZ7zN/O/3JfCqYnHyW9Z4IhdyqK7mMzM5H+aGoP6Q5rPzPmYTrD8KHaLhA5e18g8Lh5+v/ADHuDOijy3COzsokFoyhw29HbCUKwdH/rNTMPcV0dmuhGj0+dCkNpQQd1jofvVHjg7xxWTchQ8y93pAI82emlQkf22RuQbE75HYoVr2UORWYcTtWJ9iGZ+6J5lGghpoOM/Myvq7iXp2Pakx5XqE+XIwwMCj50aM8Pfv0QymaaVPoOrimYBoZ+3Zpn45o5cVrH91HXkZeRx4FsK9D1AthavDo6gnBbqizsJacuR1Ba7OnEf0tvj0Ym3VlySz2XW9BCjg5pufzX9yG138WacVosUY0CiHl+2/tLndxIgqq2VResE7mXWw5WmR3p0yakAUQcphchFRDV4s4R2FjxnMKd/r9qAtOPu03U53Tb039T3S5OXvAf09+ZmeOYNjjcE3febN/M1NKtF92uNL8BHEjdrbhup7nbana04i X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 name of the function is confusing, and the code is much easier to follow after folding, also rename the confusing naming "p" to more meaningful "si". Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- mm/swapfile.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index f8002f110104..574059158627 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1375,22 +1375,6 @@ static struct swap_info_struct *_swap_info_get(swp_entry_t entry) return NULL; } -static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry, - struct swap_info_struct *q) -{ - struct swap_info_struct *p; - - p = _swap_info_get(entry); - - if (p != q) { - if (q != NULL) - spin_unlock(&q->lock); - if (p != NULL) - spin_lock(&p->lock); - } - return p; -} - static unsigned char __swap_entry_free_locked(struct swap_info_struct *si, unsigned long offset, unsigned char usage) @@ -1687,14 +1671,14 @@ static int swp_entry_cmp(const void *ent1, const void *ent2) void swapcache_free_entries(swp_entry_t *entries, int n) { - struct swap_info_struct *p, *prev; + struct swap_info_struct *si, *prev; int i; if (n <= 0) return; prev = NULL; - p = NULL; + si = NULL; /* * Sort swap entries by swap device, so each lock is only taken once. @@ -1704,13 +1688,20 @@ void swapcache_free_entries(swp_entry_t *entries, int n) if (nr_swapfiles > 1) sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); - if (p) - swap_entry_range_free(p, entries[i], 1); - prev = p; + si = _swap_info_get(entries[i]); + + if (si != prev) { + if (prev != NULL) + spin_unlock(&prev->lock); + if (si != NULL) + spin_lock(&si->lock); + } + if (si) + swap_entry_range_free(si, entries[i], 1); + prev = si; } - if (p) - spin_unlock(&p->lock); + if (si) + spin_unlock(&si->lock); } int __swap_count(swp_entry_t entry) From patchwork Mon Dec 30 17:46:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923381 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 EB7F3E7718F for ; Mon, 30 Dec 2024 17:46:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79D296B008C; Mon, 30 Dec 2024 12:46:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 74D2A6B0092; Mon, 30 Dec 2024 12:46:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C6D36B0093; Mon, 30 Dec 2024 12:46:50 -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 35F4D6B008C for ; Mon, 30 Dec 2024 12:46:50 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D87131C8EF6 for ; Mon, 30 Dec 2024 17:46:49 +0000 (UTC) X-FDA: 82952354856.04.79FEDA0 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf10.hostedemail.com (Postfix) with ESMTP id 64A0EC0006 for ; Mon, 30 Dec 2024 17:46:27 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=f5kY65XT; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.170 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=1735580786; 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=7vSxPva/IzAffauQhXhMBUxAGj3ydaL87rJATVq3axg=; b=S9lATRWOvdVO7fWz7rZmwYxqrc1LqfQZ++nG2l/m7s58RoKr9LryQHyH2B8VwZw9V8ua2Z BJvYrjpOnl5WXtZv+l5SP5NLk+qOr3OopOp3yvxRUwTCxJxczZpyYdZdzHYidHs7Jq/A5+ Qjl85KipyZbK80vfXYIkWqb6Xcqc6WA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580786; a=rsa-sha256; cv=none; b=UBHp+9MJP5Gri2L/u79jT0jxcmyT4nCuhUrFfDlYbBhdnDmGQocO7uidVLYUaVJyij2VGg fqfNtAQoK47ZKCGZ0g0OfjAKXXjUHqfgo62nPX3GXxojdz/EErzYV1y1J0L8MyCPfYh+R0 +VEzd66lrHQ/gPswauCMkY+Vhp4CjeU= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=f5kY65XT; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=ryncsn@gmail.com Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2162c0f6a39so130804425ad.0 for ; Mon, 30 Dec 2024 09:46:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580806; x=1736185606; 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=7vSxPva/IzAffauQhXhMBUxAGj3ydaL87rJATVq3axg=; b=f5kY65XTvSB6jjgwnNo3lrfz8zfbZqYvvXxHY80WkWe1UB+9yjYe6BZyJnrk9Aa+l6 mWzNAQ6qBXE8p2FCGg1xvZiLF+JiLcMVOIFAXP73R0Z5AubWuyQfsyj1ICV44pGAKLYz 9UFVhjC1zjgu+dTyBlR2KpPHPf3YU1IaGo76qG1B8kexa4qQjirdHAgTJp7WEIZgAdI9 HVgYZ242eyP+P0QQK8sDrFrl0SrSlaoI23NR8dCzSgBP6BcphFRjVWE5KE12NhqXi3lE oWk67pL5sZHC1uZWh+Dul0L1VSW1cEwtUcqsPq+KNCwFi6CDVRlpw7AQUJazZACo7Maw Nzyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580806; x=1736185606; 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=7vSxPva/IzAffauQhXhMBUxAGj3ydaL87rJATVq3axg=; b=i8eC8b1XCWrvdPQtzicAcyiJdE3kNrZI8/wj9n8Cj9c3652b5VzyNBEkA4HkK5hjgZ NX+P/nY/YxjpB+SwIWGo7sFTEeXYFfk262IkgcN5SdmqWJSH07pCcAYgqURyvKZVeUq2 prXkYeUh7LYUqOIwq6jXmtQXLJojVED6srOkyOOnmrAu7zG0mRlhlsugtylDfJo184qS j5mvp0i2N1sMhBlmAfs/jfyYNcTX5TIP671jlC9S64ti2K0fajujlDVj/SW7PKEjZCPZ XJGLCMmyjjStDpw2oU2Ac0FkjN5kSjUQo2n+n4Zfgfpz3BRk3J4PhZDJVEtg/MfpW2WY 46Kg== X-Gm-Message-State: AOJu0YxujzofVE7lUrXLgCYbNfMorN4O9yZt7vhBDjbRqJOZPeeDwhm3 rSxSG6pdXjPlgE89+Xf1RgTlYp/osSJQ7/XfmcOo89eWB+lTrXmVSfnQqkfBJT5z/w== X-Gm-Gg: ASbGncvOFH3lY0fIq/C6Mte3LVcM7l7KC4voNy89vbRkID5EHvpuIwyiLnseh8bS5S2 GKOYvl85AmaGlI/3ODZ/XAAK6e/DAOR+JEEoOEAyEl9fh8bjKomtI7AkvcaQUkAGbYLHjVE7uj7 GEDMRrxxnaNgbTjuF3ukYat0bhoNuCsIBd0vJZf19YDOQLLNcBLFOKa7nM/PYL+zJJjKvzhCSy1 bqKCWub7vKbd7PHOVYksq9L97yNZrVYZ8lA6TjqfzUq4jczpnZj5F2FSuBnXK9X05k/XJhi8xRh X-Google-Smtp-Source: AGHT+IFGaaYMv+n81CLFKNy2U6RQJLAKYWZcBTWhY3HPrrCkiA2zuLxNbiksN4cwq8D8+FBJMwFWwg== X-Received: by 2002:a17:90b:2545:b0:2e2:c2b0:d03e with SMTP id 98e67ed59e1d1-2f453633e61mr48968055a91.5.1735580806162; Mon, 30 Dec 2024 09:46:46 -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.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:46:45 -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 03/13] mm, swap: remove old allocation path for HDD Date: Tue, 31 Dec 2024 01:46:11 +0800 Message-ID: <20241230174621.61185-4-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 MIME-Version: 1.0 X-Stat-Signature: p5eecomgdm3gmpxxy1tq8kutptwn316r X-Rspamd-Queue-Id: 64A0EC0006 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1735580787-732163 X-HE-Meta: U2FsdGVkX189fmldRKyToTr6fJyNVUYCmu1koDxnXT+je6a4Zvr4vOcaWTfoZ8rXp9CcfupVHicF4ZhaylRKoaYYxUpKZUiaQQCa+beCBz9709XwpbhKaJWLTNvCJUvpwHDTmXrOT1XOgzZdbNmROrT8voHWRZm+aKN1zy2QbgnAV/4BKW8O/lP62ksu/dPkFiHK1CV5OVLo4ULZCme7RoQVTKOvHSH8t5vUNkOQJ5Mslh5h6iGN/g/ffZC6k4LNoNTDn3LP1YTUJYaz04t4e4yFifFbksqpMfFXkG70YbLHqXItn4Ts2H8udy+in91PT7Lw/Xa7kmgJ8cLuox7Hu+YE0Dq8UO8PMOQ8gHcqs4CG4tgKyhWIyxiuK01gqXJpRvJP9h47D5Nwq1jxrniIiKGXWzqTk+hQLagWq266gXW95I4BXJ18FwbBUTRYyXkUyExzqb8h67r+DCSzhd3H4QCHfJV3znMir5QNImUWsJT9GagZ/SuVuQWQa5VpQH1Mem8k03vFayTpS+XV29QVqMSa4ieV4niFqWGqkzMByYcaZ9jX0+0sMOBGKPao+Aiw+qCkVlk8td5Nn89RjN4Gg7fzPIOdiob29eFL4RdAh69/W3BPFM3lA7ZgD281G8m64gHn4OcPdumsuJpda5vx8/ZgGuJ+diLO0V+0f6jHHhZyr1G0zHlU80gDr3EJP6ogifQgXSKX29UnaN20vfydLRV/fTE/rWKX7fQg+Jq8wy4hG/5AemLwgoh4h5KXRlhNuQ6qfgFrbxQdv8InAD1FgnqN7cCoEbyomaT0Z9OvRRMuLszRUCydcXgOlL5DZYuHCM4DvkUsgSAKxAlPfQTn4G+BVD2TRl7LkqtlOLQmPZaw1DJSLd8Lx1x5q9AE3wmA5fmdQZRfNmlZwOHacqbNbkiahqRV+2DWbxop5QjYsoQo8Ke+fJs+Nx94x4DwbetPQOgNevphlsqXc0DpcEF 8g6ALptv vtfUNdQ7Zcswtdror2vp4JRUiXo/+ojFFZ12206NHmvVoCtn3GysVq4YSxjUgbpqJ4gMMATDGYwDZl9EMFPlCtQF7LjiGI2c2kM0lIZC/QUTPlXlfdZL3u8x3Qa/VO7K4GXBckWP3rqg6OlfhYEkZFn8Z+5W+LTV/paO0qkDqgypyGQ6fNPlPkVXJU/FO5SVr9EBljLahLB4mfKIN+9wXUAOMcXEoZDKMLzKiLaBJwxXumNxpM0n/klWt8LcOyXLcIu55IGShpl9Ciud3H3IV/e8OCAbY1vgpp1cpF30C8rYZiWhyssuMg8MEhGkjO1u1WfVk89FxVF6VzU9Bkx8rNoIdX8gTYmCC9JFsZgW6z2CCUddupCFPzRawQfHQ6qhs0wcYjTd1RhcJQua6QeoxPhaFFwftP6X2Nj/ejGLZqUVjbjJfEq0AqdnxaVeRPX/GCuZ9gzl+pcb8e3PdnP5Jr1EWCSHpWulvkGOj48jenJuoweWMiVQogsUcPZHCX68aCxwraqUNR+O4l0E9QLue01gO71ksyaCiRReruvWLaYCoXUzHAW6ck17FnrSUqYf4YJqI 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 We are currently using different swap allocation algorithm for HDD and non-HDD. This leads to the existence of a different set of locks, and the code path is heavily bloated, causing difficulties for further optimization and maintenance. This commit removes all HDD swap allocation and related dead code, and uses the cluster allocation algorithm instead. The performance may drop temporarily, but this should be negligible: The main advantage of the legacy HDD allocation algorithm is that it tends to use continuous slots, but swap device gets fragmented quickly anyway, and the attempt to use continuous slots will fail easily. This commit also enables mTHP swap on HDD, which is expected to be beneficial, and following commits will adapt and optimize the cluster allocator for HDD. Suggested-by: Chris Li Suggested-by: "Huang, Ying" Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- include/linux/swap.h | 3 - mm/swapfile.c | 235 ++----------------------------------------- 2 files changed, 9 insertions(+), 229 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 187715eec3cb..0c681aa5cb98 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -310,9 +310,6 @@ struct swap_info_struct { unsigned int highest_bit; /* index of last free in swap_map */ unsigned int pages; /* total of usable pages of swap */ unsigned int inuse_pages; /* number of those currently in use */ - unsigned int cluster_next; /* likely index for next allocation */ - unsigned int cluster_nr; /* countdown to next cluster search */ - unsigned int __percpu *cluster_next_cpu; /*percpu index for next allocation */ struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap location */ struct rb_root swap_extent_root;/* root of the swap extent rbtree */ struct block_device *bdev; /* swap device or bdev of swap file */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 574059158627..fca58d43b836 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1001,49 +1001,6 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries); } -static void set_cluster_next(struct swap_info_struct *si, unsigned long next) -{ - unsigned long prev; - - if (!(si->flags & SWP_SOLIDSTATE)) { - si->cluster_next = next; - return; - } - - prev = this_cpu_read(*si->cluster_next_cpu); - /* - * Cross the swap address space size aligned trunk, choose - * another trunk randomly to avoid lock contention on swap - * address space if possible. - */ - if ((prev >> SWAP_ADDRESS_SPACE_SHIFT) != - (next >> SWAP_ADDRESS_SPACE_SHIFT)) { - /* No free swap slots available */ - if (si->highest_bit <= si->lowest_bit) - return; - next = get_random_u32_inclusive(si->lowest_bit, si->highest_bit); - next = ALIGN_DOWN(next, SWAP_ADDRESS_SPACE_PAGES); - next = max_t(unsigned int, next, si->lowest_bit); - } - this_cpu_write(*si->cluster_next_cpu, next); -} - -static bool swap_offset_available_and_locked(struct swap_info_struct *si, - unsigned long offset) -{ - if (data_race(!si->swap_map[offset])) { - spin_lock(&si->lock); - return true; - } - - if (vm_swap_full() && READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) { - spin_lock(&si->lock); - return true; - } - - return false; -} - static int cluster_alloc_swap(struct swap_info_struct *si, unsigned char usage, int nr, swp_entry_t slots[], int order) @@ -1071,13 +1028,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si, unsigned char usage, int nr, swp_entry_t slots[], int order) { - unsigned long offset; - unsigned long scan_base; - unsigned long last_in_cluster = 0; - int latency_ration = LATENCY_LIMIT; unsigned int nr_pages = 1 << order; - int n_ret = 0; - bool scanned_many = false; /* * We try to cluster swap pages by allocating them sequentially @@ -1089,7 +1040,6 @@ static int scan_swap_map_slots(struct swap_info_struct *si, * But we do now try to find an empty cluster. -Andrea * And we let swap pages go all over an SSD partition. Hugh */ - if (order > 0) { /* * Should not even be attempting large allocations when huge @@ -1109,158 +1059,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si, return 0; } - if (si->cluster_info) - return cluster_alloc_swap(si, usage, nr, slots, order); - - si->flags += SWP_SCANNING; - - /* For HDD, sequential access is more important. */ - scan_base = si->cluster_next; - offset = scan_base; - - if (unlikely(!si->cluster_nr--)) { - if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { - si->cluster_nr = SWAPFILE_CLUSTER - 1; - goto checks; - } - - spin_unlock(&si->lock); - - /* - * If seek is expensive, start searching for new cluster from - * start of partition, to minimize the span of allocated swap. - */ - scan_base = offset = si->lowest_bit; - last_in_cluster = offset + SWAPFILE_CLUSTER - 1; - - /* Locate the first empty (unaligned) cluster */ - for (; last_in_cluster <= READ_ONCE(si->highest_bit); offset++) { - if (si->swap_map[offset]) - last_in_cluster = offset + SWAPFILE_CLUSTER; - else if (offset == last_in_cluster) { - spin_lock(&si->lock); - offset -= SWAPFILE_CLUSTER - 1; - si->cluster_next = offset; - si->cluster_nr = SWAPFILE_CLUSTER - 1; - goto checks; - } - if (unlikely(--latency_ration < 0)) { - cond_resched(); - latency_ration = LATENCY_LIMIT; - } - } - - offset = scan_base; - spin_lock(&si->lock); - si->cluster_nr = SWAPFILE_CLUSTER - 1; - } - -checks: - if (!(si->flags & SWP_WRITEOK)) - goto no_page; - if (!si->highest_bit) - goto no_page; - if (offset > si->highest_bit) - scan_base = offset = si->lowest_bit; - - /* reuse swap entry of cache-only swap if not busy. */ - if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { - int swap_was_freed; - spin_unlock(&si->lock); - swap_was_freed = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_DIRECT); - spin_lock(&si->lock); - /* entry was freed successfully, try to use this again */ - if (swap_was_freed > 0) - goto checks; - goto scan; /* check next one */ - } - - if (si->swap_map[offset]) { - if (!n_ret) - goto scan; - else - goto done; - } - memset(si->swap_map + offset, usage, nr_pages); - - swap_range_alloc(si, offset, nr_pages); - slots[n_ret++] = swp_entry(si->type, offset); - - /* got enough slots or reach max slots? */ - if ((n_ret == nr) || (offset >= si->highest_bit)) - goto done; - - /* search for next available slot */ - - /* time to take a break? */ - if (unlikely(--latency_ration < 0)) { - if (n_ret) - goto done; - spin_unlock(&si->lock); - cond_resched(); - spin_lock(&si->lock); - latency_ration = LATENCY_LIMIT; - } - - if (si->cluster_nr && !si->swap_map[++offset]) { - /* non-ssd case, still more slots in cluster? */ - --si->cluster_nr; - goto checks; - } - - /* - * Even if there's no free clusters available (fragmented), - * try to scan a little more quickly with lock held unless we - * have scanned too many slots already. - */ - if (!scanned_many) { - unsigned long scan_limit; - - if (offset < scan_base) - scan_limit = scan_base; - else - scan_limit = si->highest_bit; - for (; offset <= scan_limit && --latency_ration > 0; - offset++) { - if (!si->swap_map[offset]) - goto checks; - } - } - -done: - if (order == 0) - set_cluster_next(si, offset + 1); - si->flags -= SWP_SCANNING; - return n_ret; - -scan: - VM_WARN_ON(order > 0); - spin_unlock(&si->lock); - while (++offset <= READ_ONCE(si->highest_bit)) { - if (unlikely(--latency_ration < 0)) { - cond_resched(); - latency_ration = LATENCY_LIMIT; - scanned_many = true; - } - if (swap_offset_available_and_locked(si, offset)) - goto checks; - } - offset = si->lowest_bit; - while (offset < scan_base) { - if (unlikely(--latency_ration < 0)) { - cond_resched(); - latency_ration = LATENCY_LIMIT; - scanned_many = true; - } - if (swap_offset_available_and_locked(si, offset)) - goto checks; - offset++; - } - spin_lock(&si->lock); - -no_page: - si->flags -= SWP_SCANNING; - return n_ret; + return cluster_alloc_swap(si, usage, nr, slots, order); } int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) @@ -2871,8 +2670,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) mutex_unlock(&swapon_mutex); free_percpu(p->percpu_cluster); p->percpu_cluster = NULL; - free_percpu(p->cluster_next_cpu); - p->cluster_next_cpu = NULL; vfree(swap_map); kvfree(zeromap); kvfree(cluster_info); @@ -3184,8 +2981,6 @@ static unsigned long read_swap_header(struct swap_info_struct *si, } si->lowest_bit = 1; - si->cluster_next = 1; - si->cluster_nr = 0; maxpages = swapfile_maximum_size; last_page = swap_header->info.last_page; @@ -3271,7 +3066,6 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, unsigned long maxpages) { unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); - unsigned long col = si->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS; struct swap_cluster_info *cluster_info; unsigned long i, j, k, idx; int cpu, err = -ENOMEM; @@ -3283,15 +3077,6 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, for (i = 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); - si->cluster_next_cpu = alloc_percpu(unsigned int); - if (!si->cluster_next_cpu) - goto err_free; - - /* Random start position to help with wear leveling */ - for_each_possible_cpu(cpu) - per_cpu(*si->cluster_next_cpu, cpu) = - get_random_u32_inclusive(1, si->highest_bit); - si->percpu_cluster = alloc_percpu(struct percpu_cluster); if (!si->percpu_cluster) goto err_free; @@ -3333,7 +3118,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, * sharing same address space. */ for (k = 0; k < SWAP_CLUSTER_COLS; k++) { - j = (k + col) % SWAP_CLUSTER_COLS; + j = k % SWAP_CLUSTER_COLS; for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) { struct swap_cluster_info *ci; idx = i * SWAP_CLUSTER_COLS + j; @@ -3483,18 +3268,18 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) if (si->bdev && bdev_nonrot(si->bdev)) { si->flags |= SWP_SOLIDSTATE; - - cluster_info = setup_clusters(si, swap_header, maxpages); - if (IS_ERR(cluster_info)) { - error = PTR_ERR(cluster_info); - cluster_info = NULL; - goto bad_swap_unlock_inode; - } } else { atomic_inc(&nr_rotate_swap); inced_nr_rotate_swap = true; } + cluster_info = setup_clusters(si, swap_header, maxpages); + if (IS_ERR(cluster_info)) { + error = PTR_ERR(cluster_info); + cluster_info = NULL; + goto bad_swap_unlock_inode; + } + if ((swap_flags & SWAP_FLAG_DISCARD) && si->bdev && bdev_max_discard_sectors(si->bdev)) { /* @@ -3575,8 +3360,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) bad_swap: free_percpu(si->percpu_cluster); si->percpu_cluster = NULL; - free_percpu(si->cluster_next_cpu); - si->cluster_next_cpu = NULL; inode = NULL; destroy_swap_extents(si); swap_cgroup_swapoff(si->type); From patchwork Mon Dec 30 17:46:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923382 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 275C9E7718F for ; Mon, 30 Dec 2024 17:46:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD1486B0093; Mon, 30 Dec 2024 12:46:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A823A6B0095; Mon, 30 Dec 2024 12:46:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9223D6B0096; Mon, 30 Dec 2024 12:46:54 -0500 (EST) 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 6CF106B0093 for ; Mon, 30 Dec 2024 12:46:54 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DAFF51A0855 for ; Mon, 30 Dec 2024 17:46:53 +0000 (UTC) X-FDA: 82952353596.03.98B9B10 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf03.hostedemail.com (Postfix) with ESMTP id A461C20007 for ; Mon, 30 Dec 2024 17:46:30 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hr63nGIa; spf=pass (imf03.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.47 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=1735580790; 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=OUd7wiP5I69QsBZ6QEaFt/A5eIkrt9OzZ0llQ5gAam8=; b=IR2LfccFV1+VROSrx+9CiDsmtU0pNd6mKmU1reoF85cfJcIZ8p7koDcBL8qYzCuLSig/2O KfoqDfkiZcBnQTkF2fNtFHhYHJ603cUykPfZ9Q6xCHQA8JZy8cl8JLv7J9QTh3fE5+Al/7 HBfw3JC17rRoGVmlZV1io6w/6av5sAc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580790; a=rsa-sha256; cv=none; b=zKPknBsWEhO98uqWkV3steeEqNpIO77eDlPSmSzmTkvs8WMuWOqZxX4JjRoH282HyxWqua vNnvkEHEX+XQ5466XyKsCuNQ5LF7vU+I2NzP8dqBQBMC5UZ54U5m5vJA9cF2Uu/sVUYePR fCVwPdIH4sCla1xXV9xqemmCJwyLQZo= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hr63nGIa; spf=pass (imf03.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2f4448bf96fso9565585a91.0 for ; Mon, 30 Dec 2024 09:46:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580810; x=1736185610; 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=OUd7wiP5I69QsBZ6QEaFt/A5eIkrt9OzZ0llQ5gAam8=; b=hr63nGIa3m6TDRsXUPFjjFTO5/u/3DGRzVQ7FmMjg7F1cA93S5VbrVxx3FgyVZd0y+ 3AFN1uIuHHJB8fe6ezYkTO+XcpSpaXydU7Mwd/8E7iU3sw6ar8V8kir36+7aOcLb1Un8 0TRPpSPiRxuOh6JnC3a/js2dJdAh9kz3h/U4kxKfAHphfweRO+YVQptFNGF/j0MTEpFb sGtHua8agptvLowhFUJqfnLGew/TO6LIcgz5LwY+EHpYiG2UaByDvkHK/DxB11gO+oM1 WJrmJYSfnghHpjHfS2IpBop5XJy2tpA4uQ72vMiXhh0zEOAJXPaZlwMAJZ6wYiu09qh4 yXEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580810; x=1736185610; 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=OUd7wiP5I69QsBZ6QEaFt/A5eIkrt9OzZ0llQ5gAam8=; b=ZByhlj+yhoMH8HlEhL/dRU2V7UIAsaNG04IMbvMqQtKbTKCdrA/wQbzi0RRhB8o4GZ BLrccazt7oUPzCfv+VdNoe2AsxInlgh5wyGUTtSDOZiuFtXpLliqisM+UlpNx7ORkeIB nyon5QF5eL4ivCDtVb2iA+p8hYY2tdGZhjjs3MJkAE5CI9UIEKW7IApLUtSpvfXl7OLS 0tzCMVn612rUsmGC3aprZ+/WLMdem38Fx7kx/zQQqLNgNlpxNkdNZ7wLd6w1h1H95Y/b eck0Y3M6RJuPk98Cnstk6JKN7F9+ciGIqGFaaMM1xTL8d64DkNrw6m5KuslYhUbng1Az aRfQ== X-Gm-Message-State: AOJu0YwhlnZ4V3krq5b74fha3n72+NJIwRoS04ygh8ZvxBOgGitYm4Ee hQ4MElpkP4sDatIM1M+BjBkAqhmHOhtameGcA+Z1WabT5F8wRXNE1dJX773dJTl1kg== X-Gm-Gg: ASbGncusQNjtEGPGHXQiX5iiLSaRi+F8RkfkQbbZOLCeECJBk+Bh7tZKMP9Yl8ogC6s QGeSlaQh3AZIoJ9ZMUjBc1v2A47Yb9GQRhhvyxMEcTrE340u/Xo4h2nNOYz7j3le3i6GAD/DC3q 3knAmUXiryFA3FrlGio9bSWoUiZUHSwxYC9DvwxCmX35Ek8LC4RHhUonBItZGBIBzlrfG7o1UK1 Ky2OvaKrFoVp4PO1nh+0ImdxkWtjdl8LhYs2LA4zCAYan0UjKpDqLVmqWr0RXT4tahAmBf8KWYS X-Google-Smtp-Source: AGHT+IFASpgLcnv5ruOz0GXq6zDz/azXtKgcI9+WP8UHO+QEYfltUE4koJjsFKEEQFJ+OZ7gXzQA0w== X-Received: by 2002:a17:90b:2e4a:b0:2ee:ab04:1037 with SMTP id 98e67ed59e1d1-2f452e36956mr56778141a91.17.1735580809678; Mon, 30 Dec 2024 09:46:49 -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.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:46:49 -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 04/13] mm, swap: use cluster lock for HDD Date: Tue, 31 Dec 2024 01:46:12 +0800 Message-ID: <20241230174621.61185-5-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 MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: A461C20007 X-Stat-Signature: 7uku1jwzmjpu5f7ggbo3tk9fqbnwphr7 X-Rspam-User: X-HE-Tag: 1735580790-318738 X-HE-Meta: U2FsdGVkX18f61FzoAFfc0JyFEtPyhkge93cls8gJ+CN0dfr96anQQTRCmLfP6Rl6yozP+V13UPHsXVJKaIM0bQvnopJPLNzQbvH5HpSYHYwkNRYg5UjJ6mCrYlvHDoCwBrSYvkylc1wKRigmIGruZxiQ+8XkhAlz4BeGcTGE4fQttPAiTwlEoz2EBpEEtFULce7yXJWrljbgUoPsdmB3O51D+0iRNqe5G+jwyNGV/04AGd14hhkbVgaOboZ4AooVSBWjrz6uAHnSFveLWowTDtZXpzQ1lPCxX1BT+h9jMSFpln4RrDQbwXjWj+MGMchotTMW6r9y7/sfFVzA3m3cdxv4wl+QpTMJJCdF9EpAsahOO8crYwefEV0vrdF2HjwSf3fabBfZUYLJDi3iXTo8uO/FBqZmrvx/iFL94omYX1v2e0P2X6tsnpJWOoNkSMzJEld+rV6jqAUPP4RxaGb/WA8qqVSjxOQuZgGFxbdYpy7PPAuO3qlP/ShD29WSn1jfrDlGtxOQGDb6WsIMfggj18ipHZ+DzoRGvAnoOldW/OwXGleqmt5YEmJ1MQy0P0jvyBNO4fbHTBouTTJJ2cs185N/i+YF4aOrxFnJOHNnuyaXosRp35ARBkMdaLFcxjujcVdluL0Qa3Rpw8yQIl4rJ5ZTlbqGaXb5t0izAIQN6TJVMZRLJ3PZ2bK981DIkGEkvYn/niL2jplwRZVh7guuI/Tc7mksPETvJ9y2l8taG/boN9Sm0Fdpw8qKidCIiZF26DrGvNB7pzNFQ1cj5N9xu8Ceh3yuRmyxZvJi++RpzWwW03zTsLAvQVUYHrdiBtdLrv+7H3DH8ShOiaD7Qp/Nh0+P/uj9ZE/dz5GLrx5WP00dJL/grzEgU806hQ617R9cTWBHa1et6nmuYPnfFK7wlJTHO8uDewIVbWivdwX+8UXhZha0e8yT3OkAJGStt4xyDuKYfJcmqV8PuTjtPB 16oNPIiv amdOWOpoyTzp8TRwfwewoUEf9/HjnsKVsuLF8G2TiTCwXv/gVFoW4AcpmDputTR56RcPWpSjr+hfCJDC9yffViWRSI7Wg3yylpHNYsmVh0rxB7N1TV62PUcsUIa1T8+i5fQWlxtpPiZBV7jPHKJnF+4DTHGZjG0Sftujh+oAkKFcqsGmdDFThKKfndkpSS3Oth5oBGn5OfOQymY5n7Q0ajdwqRxKia0blUuVCfbbW5B0wFNqvZxlQtIvlsoCgVtVD1ZB+Krbr6DEw3JBplcovTUD6FhpoIyVfegU3J7bhR109XobFZuBJHna8pARb/3f03nlfwtNR2SbokkquA6eZwtyZHikbS/T9KMxs8VrlEPpRSBhlHjLvWA4ewtysZgqacesBRakyFt2FMGsHrwKzplJ7DKOQmJ885RLDqL1KLvCWDOROF9ymEwC9D57qsSQ4PZGi/rjp3yPJRAh7z7ngR9xpt+KDi6Z6oHvd2m7cRGc80c4o4K0VvkQe/2ofJ2w0yfk5D3rnJHx0KDkz9UbqiQgHWafPpkemomhzFyCNNFGRKfw= 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 Cluster lock (ci->lock) was introduce to reduce contention for certain operations. Using cluster lock for HDD is not helpful as HDD have a poor performance, so locking isn't the bottleneck. But having different set of locks for HDD / non-HDD prevents further rework of device lock (si->lock). This commit just changed all lock_cluster_or_swap_info to lock_cluster, which is a safe and straight conversion since cluster info is always allocated now, also removed all cluster_info related checks. Suggested-by: Chris Li Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- mm/swapfile.c | 107 ++++++++++++++++---------------------------------- 1 file changed, 34 insertions(+), 73 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index fca58d43b836..d0e5b9fa0c48 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -58,10 +58,9 @@ static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset, unsigned int nr_entries); static bool folio_swapcache_freeable(struct folio *folio); -static struct swap_cluster_info *lock_cluster_or_swap_info( - struct swap_info_struct *si, unsigned long offset); -static void unlock_cluster_or_swap_info(struct swap_info_struct *si, - struct swap_cluster_info *ci); +static struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, + unsigned long offset); +static void unlock_cluster(struct swap_cluster_info *ci); static DEFINE_SPINLOCK(swap_lock); static unsigned int nr_swapfiles; @@ -222,9 +221,9 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, * swap_map is HAS_CACHE only, which means the slots have no page table * reference or pending writeback, and can't be allocated to others. */ - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); need_reclaim = swap_is_has_cache(si, offset, nr_pages); - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); if (!need_reclaim) goto out_unlock; @@ -404,45 +403,15 @@ static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si { struct swap_cluster_info *ci; - ci = si->cluster_info; - if (ci) { - ci += offset / SWAPFILE_CLUSTER; - spin_lock(&ci->lock); - } - return ci; -} - -static inline void unlock_cluster(struct swap_cluster_info *ci) -{ - if (ci) - spin_unlock(&ci->lock); -} - -/* - * Determine the locking method in use for this device. Return - * swap_cluster_info if SSD-style cluster-based locking is in place. - */ -static inline struct swap_cluster_info *lock_cluster_or_swap_info( - struct swap_info_struct *si, unsigned long offset) -{ - struct swap_cluster_info *ci; - - /* Try to use fine-grained SSD-style locking if available: */ - ci = lock_cluster(si, offset); - /* Otherwise, fall back to traditional, coarse locking: */ - if (!ci) - spin_lock(&si->lock); + ci = &si->cluster_info[offset / SWAPFILE_CLUSTER]; + spin_lock(&ci->lock); return ci; } -static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si, - struct swap_cluster_info *ci) +static inline void unlock_cluster(struct swap_cluster_info *ci) { - if (ci) - unlock_cluster(ci); - else - spin_unlock(&si->lock); + spin_unlock(&ci->lock); } /* Add a cluster to discard list and schedule it to do discard */ @@ -558,9 +527,6 @@ static void inc_cluster_info_page(struct swap_info_struct *si, unsigned long idx = page_nr / SWAPFILE_CLUSTER; struct swap_cluster_info *ci; - if (!cluster_info) - return; - ci = cluster_info + idx; ci->count++; @@ -576,9 +542,6 @@ static void inc_cluster_info_page(struct swap_info_struct *si, static void dec_cluster_info_page(struct swap_info_struct *si, struct swap_cluster_info *ci, int nr_pages) { - if (!si->cluster_info) - return; - VM_BUG_ON(ci->count < nr_pages); VM_BUG_ON(cluster_is_free(ci)); lockdep_assert_held(&si->lock); @@ -1007,8 +970,6 @@ static int cluster_alloc_swap(struct swap_info_struct *si, { int n_ret = 0; - VM_BUG_ON(!si->cluster_info); - si->flags += SWP_SCANNING; while (n_ret < nr) { @@ -1052,10 +1013,10 @@ static int scan_swap_map_slots(struct swap_info_struct *si, } /* - * Swapfile is not block device or not using clusters so unable + * Swapfile is not block device so unable * to allocate large entries. */ - if (!(si->flags & SWP_BLKDEV) || !si->cluster_info) + if (!(si->flags & SWP_BLKDEV)) return 0; } @@ -1295,9 +1256,9 @@ static unsigned char __swap_entry_free(struct swap_info_struct *si, unsigned long offset = swp_offset(entry); unsigned char usage; - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); usage = __swap_entry_free_locked(si, offset, 1); - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); if (!usage) free_swap_slot(entry); @@ -1320,14 +1281,14 @@ static bool __swap_entries_free(struct swap_info_struct *si, if (nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER) goto fallback; - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); if (!swap_is_last_map(si, offset, nr, &has_cache)) { - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); goto fallback; } for (i = 0; i < nr; i++) WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE); - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); if (!has_cache) { for (i = 0; i < nr; i++) @@ -1383,7 +1344,7 @@ static void cluster_swap_free_nr(struct swap_info_struct *si, DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 }; int i, nr; - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); while (nr_pages) { nr = min(BITS_PER_LONG, nr_pages); for (i = 0; i < nr; i++) { @@ -1391,18 +1352,18 @@ static void cluster_swap_free_nr(struct swap_info_struct *si, bitmap_set(to_free, i, 1); } if (!bitmap_empty(to_free, BITS_PER_LONG)) { - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); for_each_set_bit(i, to_free, BITS_PER_LONG) free_swap_slot(swp_entry(si->type, offset + i)); if (nr == nr_pages) return; bitmap_clear(to_free, 0, BITS_PER_LONG); - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); } offset += nr; nr_pages -= nr; } - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); } /* @@ -1441,9 +1402,9 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) if (!si) return; - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); if (size > 1 && swap_is_has_cache(si, offset, size)) { - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); spin_lock(&si->lock); swap_entry_range_free(si, entry, size); spin_unlock(&si->lock); @@ -1451,14 +1412,14 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) } for (int i = 0; i < size; i++, entry.val++) { if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) { - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); free_swap_slot(entry); if (i == size - 1) return; - lock_cluster_or_swap_info(si, offset); + lock_cluster(si, offset); } } - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); } static int swp_entry_cmp(const void *ent1, const void *ent2) @@ -1522,9 +1483,9 @@ int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) struct swap_cluster_info *ci; int count; - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); count = swap_count(si->swap_map[offset]); - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); return count; } @@ -1547,7 +1508,7 @@ int swp_swapcount(swp_entry_t entry) offset = swp_offset(entry); - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); count = swap_count(si->swap_map[offset]); if (!(count & COUNT_CONTINUED)) @@ -1570,7 +1531,7 @@ int swp_swapcount(swp_entry_t entry) n *= (SWAP_CONT_MAX + 1); } while (tmp_count & COUNT_CONTINUED); out: - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); return count; } @@ -1585,8 +1546,8 @@ static bool swap_page_trans_huge_swapped(struct swap_info_struct *si, int i; bool ret = false; - ci = lock_cluster_or_swap_info(si, offset); - if (!ci || nr_pages == 1) { + ci = lock_cluster(si, offset); + if (nr_pages == 1) { if (swap_count(map[roffset])) ret = true; goto unlock_out; @@ -1598,7 +1559,7 @@ static bool swap_page_trans_huge_swapped(struct swap_info_struct *si, } } unlock_out: - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); return ret; } @@ -3428,7 +3389,7 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) offset = swp_offset(entry); VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); VM_WARN_ON(usage == 1 && nr > 1); - ci = lock_cluster_or_swap_info(si, offset); + ci = lock_cluster(si, offset); err = 0; for (i = 0; i < nr; i++) { @@ -3483,7 +3444,7 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) } unlock_out: - unlock_cluster_or_swap_info(si, ci); + unlock_cluster(ci); return err; } From patchwork Mon Dec 30 17:46:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923383 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 14A38E77188 for ; Mon, 30 Dec 2024 17:46:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9E74C6B0095; Mon, 30 Dec 2024 12:46:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 996AE6B0096; Mon, 30 Dec 2024 12:46:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83A336B0098; Mon, 30 Dec 2024 12:46:57 -0500 (EST) 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 5D5BB6B0095 for ; Mon, 30 Dec 2024 12:46:57 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D86FD141A42 for ; Mon, 30 Dec 2024 17:46:56 +0000 (UTC) X-FDA: 82952355612.29.4DDA685 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf06.hostedemail.com (Postfix) with ESMTP id 04D42180004 for ; Mon, 30 Dec 2024 17:46:21 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TEPlt6v4; spf=pass (imf06.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.42 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=1735580760; 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=Idy65dfr5WN3najE9anQu5aYPiPB7lVQ4jR/dYnd8YQ=; b=JvtJUOFv87WPQuStiEKAWKnIPmrBnbZwOpN96NQ+jlceYF63rX29aqSvEgIaI6VrRUJQUA QfPvfNmcgklswNqu+fxcVEgG8BHQdBy0aw8IpWc09mHyJmH1uZ7bXERT9YrtsmE6LRl/sk Ftt/2hfkQGeYtetX67U4seHNiKh/npQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580760; a=rsa-sha256; cv=none; b=nsUdEFW7XfKNGcn6VVQwsaTE1yJprhkc9MwCXCC1tzN5ENmXmftK1RedCJeqDNvj7q6VNE srU4NyqP1qjY7il9DiMt5fUYUPN7xQwGBJD8pQlDBImxf+piQbx1H2DQhi6yiq1OHD7ikG gJsV0MCBBiuxsXGEG14WH1BDbqqF4+E= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TEPlt6v4; spf=pass (imf06.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2f42992f608so9860737a91.0 for ; Mon, 30 Dec 2024 09:46:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580813; x=1736185613; 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=Idy65dfr5WN3najE9anQu5aYPiPB7lVQ4jR/dYnd8YQ=; b=TEPlt6v44qBu8WgzU3XhfqEQRzCJMJL4ZUHSll6A1VG3erW0ZmDp4VHvK9sdc6fTgb MexW+vVp4ApAHg9e27rahj7+D+kuldjKiZRjbd+FsHeGPKu5/vMlfSts+2cm93UoPAF9 9BBR9vaozw+Wc7EBb17HgfphiLWdPsAWVQQk3aLLOffS6yPWHDtDbtSU8aNTWKBJycSG C+CqAYUNp3810vSZQHcO0MTfEGa0TQRPMWYevWnOlCUjG2tE4XEFk6QKAm2bmUVL6//P eSs+Aao0q+r9yHFDYcEhoUOl5cL+xnWjsEXnrl15GNDWZ3OqZ3jT0MyEBBS03rHJH/Zd 4ltQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580813; x=1736185613; 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=Idy65dfr5WN3najE9anQu5aYPiPB7lVQ4jR/dYnd8YQ=; b=MAcBI9OEaYO+83pXN0dRr4NpnYr3e4x1cQA2tEby5U72exkRys1zRzIuY5Hep9hX2z 7TXauWmg2+MQE7zhBbpPPhEbQGwRm0TRVYWj4cm65+Rvlzr8eOU8z1vkpQP8/A1O5U0L wzculmsu+R/swjQ8brmcyLZvOY/1oOzFWFzrKGJOKfgneFF1J3b64cI59tbY1acEuEdO u5CY6/ga2wloigdZ3hlthK+GPU+k7AHvNMtk2K2u17atUV4vN+96HoiKSPLv4/TtYN3E NOETbddryUloxteljByQOz2C0K8Z/akYHVfy8r4Ue1O8ptN5/E07FfTwMQfH7yUjet+3 x2Zg== X-Gm-Message-State: AOJu0YwVSeDvx3rMn2quBYxgrvzc5VCeTl+vo02X4rnykj4JngBW6TUf ILy/aUJk1Q6cx50NvMbiX1oMHc34khfgD4PJfntArYFNBJ5aj4KJYxPK3V3Loap/0A== X-Gm-Gg: ASbGncvShz8BQ+Ztay9Jrl8Pc2WxalmDSwDnczlI7rXovh6VYIjAEPRLS/LPqS5izQR mvchyK3qZzJ1b5BnI49wKRPDrRIbncH3nkU3ZApJ2dsZoPC19ywH8hoMMm1QIWJ0x+M+kJgvmmy MCSBLEXTyYTGwQ+WM9uoIAjS8zgjLYE2ROtSZejn1SQ1FgHf94Ldfzh9409j53a5gDJLvE4hKfW ExntP3YGwDehGt5iUXRnvIL7TOKHf7WzMen93ikV2YpAggZvIOv+eZLnjEq0g2so0zQ4HiRIFWI X-Google-Smtp-Source: AGHT+IEQMORLmqDUOV9cIFC9JLDz+Se8etjGzr6PUsuS8hxQ6iulv6lXBZdEsO+Isww9JZz3ariPsQ== X-Received: by 2002:a17:90b:538e:b0:2ee:f80c:6884 with SMTP id 98e67ed59e1d1-2f452eed77emr55167864a91.33.1735580813257; Mon, 30 Dec 2024 09:46:53 -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.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:46:52 -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 05/13] mm, swap: clean up device availability check Date: Tue, 31 Dec 2024 01:46:13 +0800 Message-ID: <20241230174621.61185-6-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 MIME-Version: 1.0 X-Rspamd-Queue-Id: 04D42180004 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: gxnuqtpwn4nz1ccojh6eai6dbg3rkydn X-HE-Tag: 1735580781-259222 X-HE-Meta: U2FsdGVkX1/7BKQ31GT5Rx5p6SvuWq4WMhjqFCxnqZhm0MB/RQis9/oB7OSPUPWhg2LE1cm1+dGJr/u4d5yBMaNtCKuFV14VQ6SOulw6tXWNgC3EYMJHvAD69iQdPik8dwpSBt1ow5FdGTJ6XRszlzRZ3z5P3/wWCIRTuek603r5l1BO9EQOys4u4fXFYT91r6cCA3qCvmGYCCFpepYdCqUBqOnGzKFVMRD43Dq0NIuBMAFF8svZxFChMrJyLDBnB80UwCVibILeL40jQ0mor4cm1fFgULWK/RmBZFs/l0bU7pcPlkrTpDqBFgAeYcOsxd35+cqhKB7GNVwiwOUtzFE5POSKSXW6YnCwlGbPani7fFg0vqBsaPJBKp0gQHyyGIvgXgygtvGvnHYRON4vg7zDIzosl3UZw7nNGZu5Y+5rNPS522xNfDAi5NI2pvUDcTBrZ6Lz1iX4bfL9uLi6WEroM8o3OOYIF/uSaPFLfTjUginwH8m6wHGOoWTDsB7YF47IXiiSm2pl+TAArRNfkJyCC1/Q7lg6xT3OLlHB8ly0UzWE1qItsW2L28PleFT32NiqtKwUDQeKrKcWFNmC3DJ/UBTtUKssxcy1nLr9cI+U7NyWwVUnTNEdziSSf3WOIf7sOoj3MNew+nruZHS+idT2RV6pyLel0QdMEvP8zvSzkmxnvrwFGlc/rqng9u/lNJeJ0s4yMOSzVD2CR25GTTvfleIz6ujd18wOPGCRAmBKi/lZgTxTKehYw8SKsf3E1/DGDY60q6bCUkMD8uleAZc2jD9Z4q1jvGJdyznLOA+q+hmuGfeEuASt9Qs1M5xbvTmrHSwQjZCtawagROnKcT005Gu8bpKgYAtgdmwUYwDDHPfVVXvmKmwmZ5QCR6+R7SHaODT1eP/JZH+pNN3NE5vZlNQiQbn2ZvtnnnGTy4uhss9rb8kWB/CnftnYIGn3OmtEoEEnsNX39VNPtzT 7kQI2W2y 4cZ4/AyBq5STB9f87LY0zivLtwtDtojo/OBvtJTYEroLd6g3O3jR9oxgFmb7iEUQmVqoLeIE1+uN/Lfs7Vd0lx9W0ViQDnV4smK3vv3LkXoHsSHBa/QQ1ijDypzbuoDM53+7lDqe01VPnGQacDkSqfy4fSAgiWyEy7+JKINgFXRX+mtEFVGZFkYW5+DRi+rZppwbWXaaqvLNPzN+NT8qTHc2i4thv/UbdHvIAyQYhd/kkpyysSg4cNJ4K00Rh9xIprW/cuHAnn+orN+P6rZKIBmL4y7eRT7fPu4Ief6tLLGJVlgmi6Hp6XvtjLB49UVJFhl3IPHOtnRTAEX8n6HodbVYtGhGaUwxHYiGJJLIUpSadC43TaE6bH8QcPet/awTS0lgGwsnipd2fXS9P+gaFF61X/tqAJkPkz6CzSx7VyWql8I78QItrx203xPMH0/vVJLlToKw2VcLRmmeX95TfK3bEhZSZXEwM0deB8DNddf9lCJA4MxtY8qfsTDTd3FigWcqcQf0A9FqGKhDbWZy+0eKjZA== 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 Remove highest_bit and lowest_bit. After the HDD allocation path has been removed, the only purpose of these two fields is to determine whether the device is full or not, which can instead be determined by checking the inuse_pages. Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- fs/btrfs/inode.c | 1 - fs/f2fs/data.c | 1 - fs/iomap/swapfile.c | 1 - include/linux/swap.h | 2 -- mm/page_io.c | 1 - mm/swapfile.c | 38 ++++++++------------------------------ 6 files changed, 8 insertions(+), 36 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 488edca8333a..a1ba78afab2c 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -10044,7 +10044,6 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, *span = bsi.highest_ppage - bsi.lowest_ppage + 1; sis->max = bsi.nr_pages; sis->pages = bsi.nr_pages - 1; - sis->highest_bit = bsi.nr_pages - 1; return bsi.nr_extents; } #else diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index a2478c2afb3a..a9eddd782dbc 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -4043,7 +4043,6 @@ static int check_swap_activate(struct swap_info_struct *sis, cur_lblock = 1; /* force Empty message */ sis->max = cur_lblock; sis->pages = cur_lblock - 1; - sis->highest_bit = cur_lblock - 1; out: if (not_aligned) f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)", diff --git a/fs/iomap/swapfile.c b/fs/iomap/swapfile.c index 5fc0ac36dee3..b90d0eda9e51 100644 --- a/fs/iomap/swapfile.c +++ b/fs/iomap/swapfile.c @@ -189,7 +189,6 @@ int iomap_swapfile_activate(struct swap_info_struct *sis, *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage; sis->max = isi.nr_pages; sis->pages = isi.nr_pages - 1; - sis->highest_bit = isi.nr_pages - 1; return isi.nr_extents; } EXPORT_SYMBOL_GPL(iomap_swapfile_activate); diff --git a/include/linux/swap.h b/include/linux/swap.h index 0c681aa5cb98..0c222017b5c6 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -306,8 +306,6 @@ struct swap_info_struct { struct list_head frag_clusters[SWAP_NR_ORDERS]; /* list of cluster that are fragmented or contented */ unsigned int frag_cluster_nr[SWAP_NR_ORDERS]; - unsigned int lowest_bit; /* index of first free in swap_map */ - unsigned int highest_bit; /* index of last free in swap_map */ unsigned int pages; /* total of usable pages of swap */ unsigned int inuse_pages; /* number of those currently in use */ struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap location */ diff --git a/mm/page_io.c b/mm/page_io.c index 4b4ea8e49cf6..9b983de351f9 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -163,7 +163,6 @@ int generic_swapfile_activate(struct swap_info_struct *sis, page_no = 1; /* force Empty message */ sis->max = page_no; sis->pages = page_no - 1; - sis->highest_bit = page_no - 1; out: return ret; bad_bmap: diff --git a/mm/swapfile.c b/mm/swapfile.c index d0e5b9fa0c48..7963a0c646a4 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -55,7 +55,7 @@ static bool swap_count_continued(struct swap_info_struct *, pgoff_t, static void free_swap_count_continuations(struct swap_info_struct *); static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry, unsigned int nr_pages); -static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset, +static void swap_range_alloc(struct swap_info_struct *si, unsigned int nr_entries); static bool folio_swapcache_freeable(struct folio *folio); static struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, @@ -650,7 +650,7 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster } memset(si->swap_map + start, usage, nr_pages); - swap_range_alloc(si, start, nr_pages); + swap_range_alloc(si, nr_pages); ci->count += nr_pages; if (ci->count == SWAPFILE_CLUSTER) { @@ -888,19 +888,11 @@ static void del_from_avail_list(struct swap_info_struct *si) spin_unlock(&swap_avail_lock); } -static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset, +static void swap_range_alloc(struct swap_info_struct *si, unsigned int nr_entries) { - unsigned int end = offset + nr_entries - 1; - - if (offset == si->lowest_bit) - si->lowest_bit += nr_entries; - if (end == si->highest_bit) - WRITE_ONCE(si->highest_bit, si->highest_bit - nr_entries); WRITE_ONCE(si->inuse_pages, si->inuse_pages + nr_entries); if (si->inuse_pages == si->pages) { - si->lowest_bit = si->max; - si->highest_bit = 0; del_from_avail_list(si); if (si->cluster_info && vm_swap_full()) @@ -933,15 +925,8 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, for (i = 0; i < nr_entries; i++) clear_bit(offset + i, si->zeromap); - if (offset < si->lowest_bit) - si->lowest_bit = offset; - if (end > si->highest_bit) { - bool was_full = !si->highest_bit; - - WRITE_ONCE(si->highest_bit, end); - if (was_full && (si->flags & SWP_WRITEOK)) - add_to_avail_list(si); - } + if (si->inuse_pages == si->pages) + add_to_avail_list(si); if (si->flags & SWP_BLKDEV) swap_slot_free_notify = si->bdev->bd_disk->fops->swap_slot_free_notify; @@ -1051,15 +1036,12 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); spin_unlock(&swap_avail_lock); spin_lock(&si->lock); - if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) { + if ((si->inuse_pages == si->pages) || !(si->flags & SWP_WRITEOK)) { spin_lock(&swap_avail_lock); if (plist_node_empty(&si->avail_lists[node])) { spin_unlock(&si->lock); goto nextsi; } - WARN(!si->highest_bit, - "swap_info %d in list but !highest_bit\n", - si->type); WARN(!(si->flags & SWP_WRITEOK), "swap_info %d in list but !SWP_WRITEOK\n", si->type); @@ -2441,8 +2423,8 @@ static void _enable_swap_info(struct swap_info_struct *si) */ plist_add(&si->list, &swap_active_head); - /* add to available list iff swap device is not full */ - if (si->highest_bit) + /* add to available list if swap device is not full */ + if (si->inuse_pages < si->pages) add_to_avail_list(si); } @@ -2606,7 +2588,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) drain_mmlist(); /* wait for anyone still in scan_swap_map_slots */ - p->highest_bit = 0; /* cuts scans short */ while (p->flags >= SWP_SCANNING) { spin_unlock(&p->lock); spin_unlock(&swap_lock); @@ -2941,8 +2922,6 @@ static unsigned long read_swap_header(struct swap_info_struct *si, return 0; } - si->lowest_bit = 1; - maxpages = swapfile_maximum_size; last_page = swap_header->info.last_page; if (!last_page) { @@ -2959,7 +2938,6 @@ static unsigned long read_swap_header(struct swap_info_struct *si, if ((unsigned int)maxpages == 0) maxpages = UINT_MAX; } - si->highest_bit = maxpages - 1; if (!maxpages) return 0; From patchwork Mon Dec 30 17:46:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923384 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 09018E77188 for ; Mon, 30 Dec 2024 17:47:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 811F86B0099; Mon, 30 Dec 2024 12:47:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C23F6B009A; Mon, 30 Dec 2024 12:47:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63B7D6B009B; Mon, 30 Dec 2024 12:47:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 408D16B0099 for ; Mon, 30 Dec 2024 12:47:01 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id ACBBC141CA1 for ; Mon, 30 Dec 2024 17:47:00 +0000 (UTC) X-FDA: 82952355696.28.60B7BCC Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf06.hostedemail.com (Postfix) with ESMTP id CC0A0180005 for ; Mon, 30 Dec 2024 17:46:25 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QNeSMpXy; spf=pass (imf06.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.44 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=1735580797; 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=Doyl8vlzndLuJUSibGhSy11BH/2aXPWDa6ojLDfUXzc=; b=m1Ros6TTZfdL+8oFeDfPkgM1HxzbXgMJjtg0OFMSrpc20FLECn8KjFYnhr+e+TVIEEnKU7 hBKj2wCGW6gWHf5zWxeV6aZV1KIBC1qGsoFgthaQEy3bWyS7F+VE2FVDejlYlzWfCPasOu 2teClSwBTecIseYGs/Aq/6XMoHorzS8= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QNeSMpXy; spf=pass (imf06.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580797; a=rsa-sha256; cv=none; b=ZrgOKwreptgez7RReferAog214ArndX4CdA5iQnL66ywxHNymQv0PBFnao5MCE/SfUeQ19 5K3LDXODW+8SVcRJKsbHTG8Dzma/RoN8Hi3T2/apZik1kTL1aY3fADTAov3ny80V882MYG wQnTrDl1cpmElOCLX2mJd2hPGu+4Cd0= Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2ee9a780de4so9130943a91.3 for ; Mon, 30 Dec 2024 09:46:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580817; x=1736185617; 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=Doyl8vlzndLuJUSibGhSy11BH/2aXPWDa6ojLDfUXzc=; b=QNeSMpXy9WsEfX9Xt65dEHGOQhuWYMRAZgAwF0RPL9TcrkWSp/FFxUqBCs5Xu62U3s ahpE5Axt2Rk5EKJ62ug5N3lI8C2LBa6EGh3AiMhu5r8ruTwGlshw8jEAQPij2zZXk9ms O0U2qbG35Pa6Z4jpDNH6ncqpxTYIdcd540epf+HYK3O45H6s9KR/tMg2rohaptB3hLMM IQ9HEQcNTMQC517W3Gmg2yVz9B3EQnfPJDqkjAVzxNzIykSR1sgU3xLqp2wwYvip8tNT UGZcYyz7IM3ZUAoVWrntM9AiTjMmnAvxjV0ZsynNv9HclxFFFxb8qZaKPTCxjSfYStMI PL5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580817; x=1736185617; 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=Doyl8vlzndLuJUSibGhSy11BH/2aXPWDa6ojLDfUXzc=; b=Iqg+u5jdIknDpJ7UOkKGad0UbRLWC5692b0Sed+eOmNE21EmTwDRFZ4oPeiQCv+/pm A3YD5m7Kby3uH7UqL692NxS2gw9UmCpDdhfyNIKPsZohIYjR5FbuR8e6aKujYfRPmA8w +KYjINUXw8Qoyr1aFpZenXoX6TE+8s5ofA+B3ASbb3LlmewRBGcFST4/YJerNdeifHI1 L5cBkTUDbSZ2C2Scn5iZTFQhjqEKX0LJYM/OBMUChk+nfJLZt9o3qQoxmiF+yJppzzYt r5K5hrm9+AV0PH3g1o6oV+wIdlcWJ9oxeVA9u2PJzz9+7Lb2sxcXvRFiFjdEaFSTFufc J3vA== X-Gm-Message-State: AOJu0Yx4HrHf2DKgkf8/phR2ITnidz8EhHqK/xVAmf6SmvvEX7E6i/oO Or6FI6LG8yiKVmWLtiXXAhDyb/zaZLFnHY97SralLfvo0ne07iTASuhjJyaSgm7QSA== X-Gm-Gg: ASbGncty0NymCuX1Tt8grb3ezyeBvgE3KPArl/1erVmXQg7ISokYt2zNDaZJrqqT75c Ztxg3D25IlFyqMZIEkphyhjeoYq67zgAt4w5UugtjhJKTcsn19GcuCtgd0Rzbz8VkRIkrDD+41d ctQeSPsGIVcyQXEXkJXk1/UjZc1Yjv3tBqPk4GeJ9UX2LYIsqnzeuxYb8OD5gsbGgZu7F6M4zlG r0VPGjdSanN6FdJwj9u7RsWS3td9rqYFHkoAcGjC2BhrYc8PHERycgtiGIz/ZcZ9RHnd6zJBjQF X-Google-Smtp-Source: AGHT+IEQCRDdPRMDf5+KaUHNnqYVgW+MsNaTPV4FGBEKTqRoTkDO1ySzMl4v5+j0v2zilPtNA/JWpA== X-Received: by 2002:a17:90b:51c4:b0:2ef:114d:7bf8 with SMTP id 98e67ed59e1d1-2f452dfcadfmr51154287a91.6.1735580816950; Mon, 30 Dec 2024 09:46:56 -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.53 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:46:56 -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 06/13] mm, swap: clean up plist removal and adding Date: Tue, 31 Dec 2024 01:46:14 +0800 Message-ID: <20241230174621.61185-7-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 MIME-Version: 1.0 X-Rspamd-Queue-Id: CC0A0180005 X-Stat-Signature: 9n5br6hd7it5eyrr718mjgwzq5f78fmk X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1735580785-789170 X-HE-Meta: U2FsdGVkX1+qVk85/0JbJKNlAxT7+li/JCpRl1hqi7Xe0Dhp/UiXMDDq1qfIHHAVUzbcHyVYb15iNJSvN1K8/D4YQFXak4akCDUDqSfE1EsP6JqT0c0IwXQRqGnbKvbPOIjwVYainzi7/z2I8sFTiXxKnkytQzFUFSgPzorKzGhOOw4IxPjDm/J4wHcay3gUSTPvq2ef8L041kCxfg5IiEuSPpsv/OcjFYyjjDaMRrnI64+S9vgdy0fnOkTRmStH42c50c2Gji80OZ6iVOKJfXzb4Vrx5B7IZEB4WEtKjp1+3k+G0S6rmiE4ShEsbdBegbBj/S36gNGzzEggG0ZzQV9t+fRn5SBDhVccWoeujiyIelJDL0Z5WhWauXEcSmNTwhgKZvHtRD9Bh/F/OA1DfLYidjjweoYv00PYZzD7D8s6SJIA1HGwri/rzflfzSC5QY3FxC//OlYVhLn79gcdBcZxmEitU8oYhVzDAIt82//mjJzfCCiNtnMMhTaBsloDPyQwWYnEvjv08Lck/U11abZVg+SLhNv7e2YSDAkeTbDx/c/UMOZibQXjuYj244KErHOl2BUKx6baMHBPXPyJ8EeBGuPap467JKo9qsS4cLZIiGjMBEKbnmBN5FFLgcSeFX+fv4o8XKK0HGJokrLVbaN58PbPIa70gWxkpfi6WF6rhNez+XsT5RDwFCnPr3/oA/kTjlp6FXbYzKM1nqP89gAJ1n9+P35/9F3OEG3a7Ay0Rr1ozwnuTvlfWRhduY27iVd0rriKvhWuiIrcy4fE5l4jb1CD6eZDZryHj/x5hvcRc7nkMda4WoG1KbA3PALqx0kOe8mC0TMhC7aWFCwCfgz15J+3Z0DdG1Ny7bOAOWtJfByGhExHHPFtthU+2Xvs0Sj5P/R8BbKkrEEmLo2dDjPrVXinM4jap4jCvItNOjALyXzUikXtGstzrc6hJuT07La2BwWObIYmobRbaV9 YsMUebmt piNJG5xM59JuLUKxrcy+4gz2JtnoPNcFE43ZSuQeHpe1ZOliLOpRceHSJkYhnqNTVBYgK8M0knV1bgke5vHKsgf3HHqKM5a/KqmQz7de8vZ7f9IKLoiHkOQYuNLw+AukKybv5GQmXj4fOGdh6RzH4QZFFFQbQ9wY/CEVY2WJsYDyubqgV+7sGfiwriXvAqEL/6Yr48gNUvTR174xq3Jw4ILGi4XLw9Ll4c8KMbvPH2MSKvdVEjT/AQ9G+e4DUASpPoJQQdAZ659LhcnNhmTuwA/9HcpDj+Z6fGmgnvbxM8SN8Dz2NpJGxtqg/3b+fRXdxWBBLt4GnJhFwBm9wX9wMC51la6HoddkRWM1DPMPL6DznnLmP+i6T9VT7lsZqkpgumeMyxSG0q0nMVenMTLrOhWFRkCAm2eMmeQwom8Ozmtfhxk/p8wGMx+vOZiGTWGWuNBIUPe4gFMUD/GHUU3RH9fYcnBogcywV8Zl7FTS+s6GtZTogI7IGzAn49VVUZQYpP1FN4i5Ba+DCHEnE8owSpW4LXg== 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 When the swap device is full (inuse_pages == pages), it should be removed from the allocation available plist. If any slot is freed, the swap device should be added back to the plist. Additionally, during swapon or swapoff, the swap device is forcefully added or removed. Currently, the condition (inuse_pages == pages) is checked after every counter update, then remove or add the device accordingly. This is serialized by si->lock. This commit decouples it from the protection of si->lock and reworked plist removal and adding, making it possible to get rid of the hard dependency on si->lock in allocation path in later commits. To achieve this, simply using another lock is not an optimal approach, as the overhead is observable for a hot counter, and may cause complex locking issues. Thus, this commit manages to make it a lock-free atomic operation, by embedding the plist state into the second highest bit of the atomic counter. Simply making the counter an atomic will not work, if the update and plist status check are not performed atomically, we may miss an addition or removal. With the embedded info we can update the counter and check the plist status with single atomic operations, and avoid any extra overheads: If the counter is full (inuse_pages == pages) and the off-list bit is unset, we attempt to remove it from the plist. If the counter is not full (inuse_pages != pages) and the off-list bit is set, we attempt to add it to the plist. Removing, adding and bit update is serialized with a lock, which is a cold path. Ordinary counter updates will be lock-free. Signed-off-by: Kairui Song --- include/linux/swap.h | 2 +- mm/swapfile.c | 188 +++++++++++++++++++++++++++++++------------ 2 files changed, 139 insertions(+), 51 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 0c222017b5c6..e1eeea6307cd 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -307,7 +307,7 @@ struct swap_info_struct { /* list of cluster that are fragmented or contented */ unsigned int frag_cluster_nr[SWAP_NR_ORDERS]; unsigned int pages; /* total of usable pages of swap */ - unsigned int inuse_pages; /* number of those currently in use */ + atomic_long_t inuse_pages; /* number of those currently in use */ struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap location */ struct rb_root swap_extent_root;/* root of the swap extent rbtree */ struct block_device *bdev; /* swap device or bdev of swap file */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 7963a0c646a4..e6e58cfb5178 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -128,6 +128,26 @@ static inline unsigned char swap_count(unsigned char ent) return ent & ~SWAP_HAS_CACHE; /* may include COUNT_CONTINUED flag */ } +/* + * Use the second highest bit of inuse_pages counter as the indicator + * of if one swap device is on the available plist, so the atomic can + * still be updated arithmetic while having special data embedded. + * + * inuse_pages counter is the only thing indicating if a device should + * be on avail_lists or not (except swapon / swapoff). By embedding the + * on-list bit in the atomic counter, updates no longer need any lock + * to check the list status. + * + * This bit will be set if the device is not on the plist and not + * usable, will be cleared if the device is on the plist. + */ +#define SWAP_USAGE_OFFLIST_BIT (1UL << (BITS_PER_TYPE(atomic_t) - 2)) +#define SWAP_USAGE_COUNTER_MASK (~SWAP_USAGE_OFFLIST_BIT) +static long swap_usage_in_pages(struct swap_info_struct *si) +{ + return atomic_long_read(&si->inuse_pages) & SWAP_USAGE_COUNTER_MASK; +} + /* Reclaim the swap entry anyway if possible */ #define TTRS_ANYWAY 0x1 /* @@ -717,7 +737,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) int nr_reclaim; if (force) - to_scan = si->inuse_pages / SWAPFILE_CLUSTER; + to_scan = swap_usage_in_pages(si) / SWAPFILE_CLUSTER; while (!list_empty(&si->full_clusters)) { ci = list_first_entry(&si->full_clusters, struct swap_cluster_info, list); @@ -872,42 +892,128 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o return found; } -static void __del_from_avail_list(struct swap_info_struct *si) +/* SWAP_USAGE_OFFLIST_BIT can only be cleared by this helper. */ +static void del_from_avail_list(struct swap_info_struct *si, bool swapoff) { int nid; + unsigned long pages; + + spin_lock(&swap_avail_lock); + + if (swapoff) { + /* + * Forcefully remove it. Clear the SWP_WRITEOK flags for + * swapoff here so it's synchronized by both si->lock and + * swap_avail_lock, to ensure the result can be seen by + * add_to_avail_list. + */ + lockdep_assert_held(&si->lock); + si->flags &= ~SWP_WRITEOK; + atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages); + } else { + /* + * If not called by swapoff, take it off-list only if it's + * full and SWAP_USAGE_OFFLIST_BIT is not set (strictly + * si->inuse_pages == pages), any concurrent slot freeing, + * or device already removed from plist by someone else + * will make this return false. + */ + pages = si->pages; + if (!atomic_long_try_cmpxchg(&si->inuse_pages, &pages, + pages | SWAP_USAGE_OFFLIST_BIT)) + goto skip; + } - assert_spin_locked(&si->lock); for_each_node(nid) plist_del(&si->avail_lists[nid], &swap_avail_heads[nid]); + +skip: + spin_unlock(&swap_avail_lock); } -static void del_from_avail_list(struct swap_info_struct *si) +/* SWAP_USAGE_OFFLIST_BIT can only be set by this helper. */ +static void add_to_avail_list(struct swap_info_struct *si, bool swapon) { + int nid; + long val; + unsigned long pages; + spin_lock(&swap_avail_lock); - __del_from_avail_list(si); + + /* Corresponding to SWP_WRITEOK clearing in del_from_avail_list */ + if (swapon) { + lockdep_assert_held(&si->lock); + si->flags |= SWP_WRITEOK; + } else { + if (!(READ_ONCE(si->flags) & SWP_WRITEOK)) + goto skip; + } + + if (!(atomic_long_read(&si->inuse_pages) & SWAP_USAGE_OFFLIST_BIT)) + goto skip; + + val = atomic_long_fetch_and_relaxed(~SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages); + + /* + * When device is full and device is on the plist, only one updater will + * see (inuse_pages == si->pages) and will call del_from_avail_list. If + * that updater happen to be here, just skip adding. + */ + pages = si->pages; + if (val == pages) { + /* Just like the cmpxchg in del_from_avail_list */ + if (atomic_long_try_cmpxchg(&si->inuse_pages, &pages, + pages | SWAP_USAGE_OFFLIST_BIT)) + goto skip; + } + + for_each_node(nid) + plist_add(&si->avail_lists[nid], &swap_avail_heads[nid]); + +skip: spin_unlock(&swap_avail_lock); } -static void swap_range_alloc(struct swap_info_struct *si, - unsigned int nr_entries) +/* + * swap_usage_add / swap_usage_sub of each slot are serialized by ci->lock + * within each cluster, so the total contribution to the global counter should + * always be positive and cannot exceed the total number of usable slots. + */ +static bool swap_usage_add(struct swap_info_struct *si, unsigned int nr_entries) { - WRITE_ONCE(si->inuse_pages, si->inuse_pages + nr_entries); - if (si->inuse_pages == si->pages) { - del_from_avail_list(si); + long val = atomic_long_add_return_relaxed(nr_entries, &si->inuse_pages); - if (si->cluster_info && vm_swap_full()) - schedule_work(&si->reclaim_work); + /* + * If device is full, and SWAP_USAGE_OFFLIST_BIT is not set, + * remove it from the plist. + */ + if (unlikely(val == si->pages)) { + del_from_avail_list(si, false); + return true; } + + return false; } -static void add_to_avail_list(struct swap_info_struct *si) +static void swap_usage_sub(struct swap_info_struct *si, unsigned int nr_entries) { - int nid; + long val = atomic_long_sub_return_relaxed(nr_entries, &si->inuse_pages); - spin_lock(&swap_avail_lock); - for_each_node(nid) - plist_add(&si->avail_lists[nid], &swap_avail_heads[nid]); - spin_unlock(&swap_avail_lock); + /* + * If device is not full, and SWAP_USAGE_OFFLIST_BIT is set, + * remove it from the plist. + */ + if (unlikely(val & SWAP_USAGE_OFFLIST_BIT)) + add_to_avail_list(si, false); +} + +static void swap_range_alloc(struct swap_info_struct *si, + unsigned int nr_entries) +{ + if (swap_usage_add(si, nr_entries)) { + if (si->cluster_info && vm_swap_full()) + schedule_work(&si->reclaim_work); + } } static void swap_range_free(struct swap_info_struct *si, unsigned long offset, @@ -925,8 +1031,6 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, for (i = 0; i < nr_entries; i++) clear_bit(offset + i, si->zeromap); - if (si->inuse_pages == si->pages) - add_to_avail_list(si); if (si->flags & SWP_BLKDEV) swap_slot_free_notify = si->bdev->bd_disk->fops->swap_slot_free_notify; @@ -946,7 +1050,7 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, */ smp_wmb(); atomic_long_add(nr_entries, &nr_swap_pages); - WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries); + swap_usage_sub(si, nr_entries); } static int cluster_alloc_swap(struct swap_info_struct *si, @@ -1036,19 +1140,6 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); spin_unlock(&swap_avail_lock); spin_lock(&si->lock); - if ((si->inuse_pages == si->pages) || !(si->flags & SWP_WRITEOK)) { - spin_lock(&swap_avail_lock); - if (plist_node_empty(&si->avail_lists[node])) { - spin_unlock(&si->lock); - goto nextsi; - } - WARN(!(si->flags & SWP_WRITEOK), - "swap_info %d in list but !SWP_WRITEOK\n", - si->type); - __del_from_avail_list(si); - spin_unlock(&si->lock); - goto nextsi; - } n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, n_goal, swp_entries, order); spin_unlock(&si->lock); @@ -1057,7 +1148,6 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) cond_resched(); spin_lock(&swap_avail_lock); -nextsi: /* * if we got here, it's likely that si was almost full before, * and since scan_swap_map_slots() can drop the si->lock, @@ -1789,7 +1879,7 @@ unsigned int count_swap_pages(int type, int free) if (sis->flags & SWP_WRITEOK) { n = sis->pages; if (free) - n -= sis->inuse_pages; + n -= swap_usage_in_pages(sis); } spin_unlock(&sis->lock); } @@ -2124,7 +2214,7 @@ static int try_to_unuse(unsigned int type) swp_entry_t entry; unsigned int i; - if (!READ_ONCE(si->inuse_pages)) + if (!swap_usage_in_pages(si)) goto success; retry: @@ -2137,7 +2227,7 @@ static int try_to_unuse(unsigned int type) spin_lock(&mmlist_lock); p = &init_mm.mmlist; - while (READ_ONCE(si->inuse_pages) && + while (swap_usage_in_pages(si) && !signal_pending(current) && (p = p->next) != &init_mm.mmlist) { @@ -2165,7 +2255,7 @@ static int try_to_unuse(unsigned int type) mmput(prev_mm); i = 0; - while (READ_ONCE(si->inuse_pages) && + while (swap_usage_in_pages(si) && !signal_pending(current) && (i = find_next_to_unuse(si, i)) != 0) { @@ -2200,7 +2290,7 @@ static int try_to_unuse(unsigned int type) * folio_alloc_swap(), temporarily hiding that swap. It's easy * and robust (though cpu-intensive) just to keep retrying. */ - if (READ_ONCE(si->inuse_pages)) { + if (swap_usage_in_pages(si)) { if (!signal_pending(current)) goto retry; return -EINTR; @@ -2209,7 +2299,7 @@ static int try_to_unuse(unsigned int type) success: /* * Make sure that further cleanups after try_to_unuse() returns happen - * after swap_range_free() reduces si->inuse_pages to 0. + * after swap_range_free() reduces inuse_pages to 0. */ smp_mb(); return 0; @@ -2227,7 +2317,7 @@ static void drain_mmlist(void) unsigned int type; for (type = 0; type < nr_swapfiles; type++) - if (swap_info[type]->inuse_pages) + if (swap_usage_in_pages(swap_info[type])) return; spin_lock(&mmlist_lock); list_for_each_safe(p, next, &init_mm.mmlist) @@ -2406,7 +2496,6 @@ static void setup_swap_info(struct swap_info_struct *si, int prio, static void _enable_swap_info(struct swap_info_struct *si) { - si->flags |= SWP_WRITEOK; atomic_long_add(si->pages, &nr_swap_pages); total_swap_pages += si->pages; @@ -2423,9 +2512,8 @@ static void _enable_swap_info(struct swap_info_struct *si) */ plist_add(&si->list, &swap_active_head); - /* add to available list if swap device is not full */ - if (si->inuse_pages < si->pages) - add_to_avail_list(si); + /* Add back to available list */ + add_to_avail_list(si, true); } static void enable_swap_info(struct swap_info_struct *si, int prio, @@ -2523,7 +2611,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) goto out_dput; } spin_lock(&p->lock); - del_from_avail_list(p); + del_from_avail_list(p, true); if (p->prio < 0) { struct swap_info_struct *si = p; int nid; @@ -2541,7 +2629,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) plist_del(&p->list, &swap_active_head); atomic_long_sub(p->pages, &nr_swap_pages); total_swap_pages -= p->pages; - p->flags &= ~SWP_WRITEOK; spin_unlock(&p->lock); spin_unlock(&swap_lock); @@ -2721,7 +2808,7 @@ static int swap_show(struct seq_file *swap, void *v) } bytes = K(si->pages); - inuse = K(READ_ONCE(si->inuse_pages)); + inuse = K(swap_usage_in_pages(si)); file = si->swap_file; len = seq_file_path(swap, file, " \t\n\\"); @@ -2838,6 +2925,7 @@ static struct swap_info_struct *alloc_swap_info(void) } spin_lock_init(&p->lock); spin_lock_init(&p->cont_lock); + atomic_long_set(&p->inuse_pages, SWAP_USAGE_OFFLIST_BIT); init_completion(&p->comp); return p; @@ -3335,7 +3423,7 @@ void si_swapinfo(struct sysinfo *val) struct swap_info_struct *si = swap_info[type]; if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) - nr_to_be_unused += READ_ONCE(si->inuse_pages); + nr_to_be_unused += swap_usage_in_pages(si); } val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; val->totalswap = total_swap_pages + nr_to_be_unused; From patchwork Mon Dec 30 17:46:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923385 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 8FAD8E77188 for ; Mon, 30 Dec 2024 17:47:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1AB076B009B; Mon, 30 Dec 2024 12:47:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 15C3A6B009D; Mon, 30 Dec 2024 12:47:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEE3A6B009C; Mon, 30 Dec 2024 12:47:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CE52E6B009A for ; Mon, 30 Dec 2024 12:47:04 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 72D561219C4 for ; Mon, 30 Dec 2024 17:47:04 +0000 (UTC) X-FDA: 82952354520.06.5F72186 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf08.hostedemail.com (Postfix) with ESMTP id 43158160005 for ; Mon, 30 Dec 2024 17:46:30 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=DxzDMnuT; spf=pass (imf08.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.180 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=1735580774; 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=L7fSMJo0m5jtrmJwyl3l9osZUE7hgdb18JM8bl2HsT4=; b=LP03B7KBo28Z0EYnIP3aV3MeUfKEW2EtD11sIglt3onw4BM4jEPO4pnExPIRMODnksixSL ojDsNGBQ3txhHgbF2S7auiG5FwOjiDYFujrJV1E4EKcntjVDlC3odYNUk9YtGTrATDNSKF pIRG4bwqL0RlmiKJcY4Tuxo2N1QOIvE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=DxzDMnuT; spf=pass (imf08.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580774; a=rsa-sha256; cv=none; b=g6VIi3S7HnjFWxK0ugWKXUL3rV6vDmvKN/y7rqlmqv4Umdm6THmeEC9J0/PiKMYR48F3BK cNJJQxPjC4vzo6oKJpHGYNYRhOpe/wEmnSrKvXsDO7fkcS3qOwpVnHfnKxmI9uMbyJDxYH 7UcVKRIxsRMifmx3Mi3O67/EuTwezfc= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-216426b0865so119510995ad.0 for ; Mon, 30 Dec 2024 09:47:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580821; x=1736185621; 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=L7fSMJo0m5jtrmJwyl3l9osZUE7hgdb18JM8bl2HsT4=; b=DxzDMnuT3C9XpO887+rfIsYKPHMmPUSWlGXJl53koT0pL38XO2JsONiS+bixUxNVMa 5M3hETbeaS7evnM9Sz6OlCJSH8okzABaF+5IPLo7h5/n2pAr31YIYlcT9ALu3lhhybLe 03xR3BKVZwh8v8R0AwunkR6Wh+XHt8D4Pg48n6RL5/VospsD7n6J/ElTb9q0qddam76X 4Gg9P4TsZy48ks1JUpxHoiTfpdkKcIyx6zrFt4WKqEI1GqpMGopA+h8MlW7yA2YN8aoY IaYiUEmViQVGbpvDtNSi4Ui8OYSlNuTCbfW1eQHLRokKq8bSCc5ugDo3af6pyYNSQ/Ge Pvng== 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=whjKgfJHubj3VqpSUyaXR2M3gY33cJihXczXZAj4+8UD8Qyr5v6iRecsNDzt7AAtaK XvgRUjxc8TmxkBXU8VrOrf7gC+sSUVjd/bwbEhFYU8sYe4O9jSciqtIGxpco38IqePPQ d1Z5tWb3orzac3I5GixJ1grr1Fvenw5afRMBfv7Xky17YcqBLTf2Veb04+9L4MQMUQnP WRZIzofB1bA4EH2HRnovrsmT4tJPBenV+8BA8UrtOr6nca1NfihK3tXClDzS89o7RYpM LE5xZ5R9X378J+1kHpOu0iY7UJmo+GkSUkqYdShF914nOpGDQwKDTIo8U2dWj8n7zqF+ 68HQ== X-Gm-Message-State: AOJu0YzB4EHGL41N3GiicdNrh/kJcpzLNEwyHxer8FsJrAsdY+uWKMZC qrbXhrTCGglvs+h/EsWCaHwUgK01bkbTPM96ugdFYaNBqqRPEcJXY9pFf8BiHre3QA== X-Gm-Gg: ASbGnctF+beSxrycWCkkD9HlVS2DRoqpxomYFyiXXtqvZ2XOvBs0VndKQMfjYFKKt1Z hb/XIFnwfEeiaOgTyp+YWhxK1AKRz3XW50bVugXpv3Yh6x6WRZ6vZTME9q0jpf6IBxrt8VDw//m QYrGnxnC0srXW+DfMpd6ZhOwBYrRFgojyu9PAMz4Od6nJ1HbHTmGd7JU8jwgXJFPtsYZyoGIHZA 3IW7eM89MNolJRySAqm9mmJ1wVmm3ZLcabP/UWoxdrkn6R0sFqICDpbM7+0Fw+ex4q6G7oRUE7g 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 MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 43158160005 X-Rspam-User: X-Stat-Signature: hwjqzbwq7f4k5k8gjz3agfuk64wn3dux X-HE-Tag: 1735580790-438995 X-HE-Meta: U2FsdGVkX18j4vmrby7fqTfOh2NyipRP/bdX7mFrc2e7cFKipNocNu2U2ES9RWyTKQ6novAzRfI2KVQFPnmWcOP1QkqrSTGw42w4mOKgaTJU/ncmwCv16aPsirEY5aXuKn7wxquOoHJNfIlh5N1kI1MvzLbP6AMp/xf+mOCAJxy+bvX51lxPjbdGGlj0/hCcCQMsnzyT0eqa5ttVYkFEm7MxOlN9Uh14XO8NUZSeH5V2Xs3LnQJY6tL8/hzLHZULkR1J7NkOTMb5juZFtJFlYKp+RaLw2jDA12EoIMcbcEH9JGEK5moIVfHvHpPVWlcBFVKVcXaOf/R17+js87D8JvVNfF5iWsHFpCihGxt+c8mpXjErjJZ5C6KboI5v6bhz1LqS0NfrmdhpC7QtQhdVQhgtq6p7uDGdxOryPFdjocNiGrDpeSUvvhvIf9YxKl5+493mW4ron6zqxrmgVeDAfenIZfV42L9jrK03gOoBUCdx/vEdDvafti8m+PjAzvDLljGQ/t2+Q5zCBz928Th+1VAQNB4g8VaTmdgDvpf/71tGdWTbpUS5nCKQgg2GJggT4RwzsbHLiUiqEPy7Z3XtrVzysaJKqBPwZ6DmdPHYSXF86FqF9yEiVUNd7BsZebzCqiteV/F0AbgnFnPcAxNZtaVzaAAevfAJ0Z0TEahR8JuJ0/xa2ei/Q2YfEApAXmOmunsx0Hxktaq4VuKvY1MbsMSUgQ8wKNjW/M/4qnp0780gqejoDRXHKNiBZm6sfe8YDo5HhL2U/EORPFXOYezip7mLUZmRz7bhDE5Qhfaq/Rldjj5PX5M3uKIjrw8liKWeDBkExEED7ywkfwzXpSVelIroh/srpb70JOSt7EAu11beOc1vIw6UxZO3EFBTMNnjLpdSLQU0xnc5b027YpFmTImFO5ni+jPjjjWcJRMdNOJMbqFSYDXd3Q+pM1wgsBp1tQFhN0SQuC39tKVVWpr KJ62QnPD LTTKbXCE5cxuYUtk8Mw0tgf1fXxx3QDJa7UtKWY7bOcEhxNp/qHcSiEWoBQRB0P73kQhd0KS/XY+EbshZagRXVHE+KaYmaJWgxBh613TOCTNrvIkPejeNy04lnWLpjbPEsc9G0r8TM/2HuPgP2BcDhIibl9a8y+0lqGM6j+Y67DNP2d+CkE/YvZCUmcMUs3t0y05bZNfXnhk2v0npi1zh7oMQvGudn2C+kabTRF9OSDBDdbnbmbnNgXpmekfdS/SsjJMzxB3NrFSInaqrrUB4ijSafkQ8JUxDY+39+qAahrhuviyH2vVWf+N22VZIsbxZDlWwySENqEa0fBZSGcVvY95e76vrT+twaesSUfRK8BbnsQMIjYd3/mNaCuKYisikCbCWjD65trYKXRz8Z3LJAmf25mFQ4tmJ6eilb1hTxUOPs/EW4pugQTla2fSkyKiIV0jRaBcF2g+/uQIdlI0kwHz+Qv7gMXJgPFH22YLorYWIlilafqzXdMGSgXoOyqD/4krOf3Rqsy98a4k= 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 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; From patchwork Mon Dec 30 17:46:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923386 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 D0F96E77188 for ; Mon, 30 Dec 2024 17:47:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 68E696B009D; Mon, 30 Dec 2024 12:47:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 63B236B009E; Mon, 30 Dec 2024 12:47:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DD4C6B009F; Mon, 30 Dec 2024 12:47:08 -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 2D13A6B009D for ; Mon, 30 Dec 2024 12:47:08 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D228D81877 for ; Mon, 30 Dec 2024 17:47:07 +0000 (UTC) X-FDA: 82952355612.04.DBDCC84 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf29.hostedemail.com (Postfix) with ESMTP id 8AECF120018 for ; Mon, 30 Dec 2024 17:45:59 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZGBWz6cO; spf=pass (imf29.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.181 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=1735580771; 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=d8LDMWyXVRzEi285CAskB0L73KwDzS/oh9m3eSsJwqE=; b=IweVPCBUN0aJhh0Y/LBSY6e/nM0sTvnhqh/hKBqj1uHtWC0DqLjkuFOcbSIbmAIfGTsbdG DHm47vl8R6m0MZJ+6fvDXRd6ZwWQl1qG/7NsLsk41g91CoqrorTiO4xPJvokDF0Ap52ODe qRPJfkrRFAFSd7JMkwsI3lfo8G5RTrU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580771; a=rsa-sha256; cv=none; b=1yHJk8TrdlRodKyFj4CjKMOyODWH9GP0qKETUJrUvGvlbya+u8pfgvY7rU7sOkB7W2kdLQ SUe2fCjozlOrO67AHShDPcTkxP2UkYdAu26ThQWQ4rZkrVWXLfsp2a735mx6ZvNK/uqfKI G+tXoK73JYQB55aKRkCEDJO4whyyxQY= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZGBWz6cO; spf=pass (imf29.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-21675fd60feso162323115ad.2 for ; Mon, 30 Dec 2024 09:47:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580824; x=1736185624; 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=d8LDMWyXVRzEi285CAskB0L73KwDzS/oh9m3eSsJwqE=; b=ZGBWz6cOVcAjs1jVzQ4k3kBGAgJe5V+QVGjbY0f29P9yLNeoCvh5dC3/VJgKqyyF0r 4Eacr/Si+5mpt1xWmmim5W/0geLhr2myqhHm2bcGrykbFL2szgRCmQ1c6mw06bdso+Au l+pJVJxegHAB70anG1aZGhADI2ulokVe7gmoY1yhdsEBcvi9WT0c5gmIAQdfmU1/6sio 8Qpo4LaXbeWgBihmYvwZAWaaxNGhasS4BdRVBpgyb9RcyYSNRHLyyZS2ikxET9uK97i/ 0w+n/FAq78uEvzvRQN+eh715JetMhyNMzHsdX36nBzIhjUy8GVLD7kc4eV+OHSiduUCk kSpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580824; x=1736185624; 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=d8LDMWyXVRzEi285CAskB0L73KwDzS/oh9m3eSsJwqE=; b=Gt7c6DlqxZ89u4fyft9ZHcuXRwxroKHByw7w+Z/N5eeKfv2y/SEmUP9WU5FOVkSNmx R0XagrGrur+J07gTiv7w4ju5okTc9mTNCQOh53/PckWGpnrGuX63zYMSzb7HAIcH4ps/ UgB/iN9HesCViESHkSJxxoEoI3eePq07VPb9ULlTEQi5MQhWskmesZkcQuAdKNWJJA7n LTiHmI4IbLGnXrShsCg0PeXWO9oHKX2IaYpmESwHITIvIpp84XFzXP4yASMO8s9sBZDV LeQSPw21XImXitoe+ELl6/2BPSI/q/CooqZvAlyqQh4g1lkrIPF4gZBQ6vjNZjksnfca G8tQ== X-Gm-Message-State: AOJu0Ywi4UIuMyPTNY9GFpduGj12NxWxLZu9oDNm56Uh6mXh4hs9NhCm GjqOUUweoVJyvDOe73ElPUayiKNIucQBRvTCaUOSuzevmY+q03jmlVeoYpiHz5LgMQ== X-Gm-Gg: ASbGncsyNFxrEsHhRF8NwiF/xAh+TbmqjUYgCbFBoNnemWI1ceplS9TpMAn049TLPPX QDxEeHz1Ry4ayvS92yO6c2lgnxwQ/bSuG9pvjTZw98O/SIf83Z0jfX5rMAAH2cneXrJRxfh9Zzb BMvRo3+8MNjXm14GAYcl/lOKHGWlszh7TIOLVKZsnNQB/eRkYAE00mQw3tZzQNeYB3+Q0TW6yoh fHXkXIJAn7sVCZZUakUQTxeTrDDZn0Fr3bl+nqQaoYfMTtU5u28LMl4cDRuWxmwwWR9Rbbi2WsE X-Google-Smtp-Source: AGHT+IHZ5DCh6KVE5R2ZCZg3rTHhMcp5OqH2JEHSIszfsjlqtv779Bl7KqhDZc1t3ohzPurCBWNErg== X-Received: by 2002:a17:902:f607:b0:216:7cd8:e964 with SMTP id d9443c01a7336-219e6ea1b79mr601419485ad.22.1735580824330; Mon, 30 Dec 2024 09:47:04 -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.47.01 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:03 -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 08/13] mm, swap: use an enum to define all cluster flags and wrap flags changes Date: Tue, 31 Dec 2024 01:46:16 +0800 Message-ID: <20241230174621.61185-9-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 MIME-Version: 1.0 X-Rspamd-Queue-Id: 8AECF120018 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: 1w8xmdz7kfmcfwphzc11itpt34sdkpc5 X-HE-Tag: 1735580759-486813 X-HE-Meta: U2FsdGVkX1991p8o4UvR6+j2OUqZlIEZJUThSfuPpKc399lPDgR8ffAjtSbPOglasNWLM4BTY75dYDJPj6a90kJhTj9UQRRjSofJ+awK7NyXySbOBgbipKXKHkwMABk2NHO5RWZONV3kKHYy5wiDcRbRrQDOHn8r0a9kcTNb2rtpubNrhO44+faV9vOpIf810YwaSU/4+51VQPG6thJlFZUgMu8MW0MIEVTrsHNF8O99B3lbN+WAMTLtXfqFRi4YtlHvci8JQay3Wg+Lhz6ymJHeCJYVv5hlZokhe+4BeZrGnVt+DqPIFDvQMydvMp0Oj5iB94bjL61X0HE/DpsRmedrlJghJDfrSfc38MpoyMOHOtMTeUI4JLMSHBsT2W4e5e0XxvA2PYy23JlmEw6aBhpeSrScGb3QXcUvhhCMZGBdYczItz3OrZisPCjCzU3h9ROrnZu1+iuRrm/9ePyrgiDa22sY9s6fF5kJ/SV33eSEjgyQeuKmyuEfqsSZdtRFowYlB29BI5t1dT09sxYUqJJ8++AGXikHmR8ktTEoCrcCNBz3d8dI9lJ7NhB91pnOBfA0Cvz1zCiRPLF0q0FuLE67FUdztGghXaPayIB1aKDnxmM0M/aNcNpdmbXkBcws3wPeQ+9oU1HJEvxVu5E4qEuVw223E5BpMJEHfmCFyeaJlon0ryIZ8js5P126sS+P27a32KuQPjfeb10gPtfCfMKPGXXLeMXy6iNPQO0I9/3w5IA0V2uBvpJjznQdzg7ymY1/+CiaN2SeNi/zDVW74iQudNmulR8fyavtyV+eqsBy+lJHiiVkjXZ9BaRRcK69zNLb7T9In33kQPqRwQyN2YTEAmm3cHy9KBq8tSb0hSuPTngqljNUXLvl8VfysAyoqY1ExG9YqrJwrQKfOd6YFer+ufVNog85fTTewjhLszEvynN6rgFHJWs4mI8xEkH09xxOxJ3uyoSPOh1B9f+ Jwlm67Xy VV3+6r2DlPC7Z1jDjPirHCa0rRgsaJuZUUBvulx9ZBwj4SlgA7UltSgYn6sZ55BfQSk5anlxxFC9gWdEb3NOUWDd0yqyvWC7uluCGQXw6xf1N/w/tMI9bewTpkwvhLNLlfQnmNT8BuQLcvrgeUVzdKw+qLk3lb7q4g0/V3o904wFbV0aLuW+jnVV9pAOqm7O9ZitjB8iIjmwXbXEZRFAH2kcTcxXtSzzFuvR8UM7ENXLWvhYscN4p8dqiOzI+gR9c8l7UKOxEYX2W+1LB4hFpO42k9s8GyjScI1DXaR7QjPfiIjD+CTqxPpwQ25Rn/GkO/jh749KGWodZDH7BY2wxeL5ZrA/eAALlj3kkuY2Rz6M1x+JaSwRJCl6aBKk6wVd2Q1taIByxfUyyFl4VrIyG7EgKJQLDaKdcSfTmametJzRhS9yS0a1pS3LDQ4Df2fKnkehx7UdFbyzns5RqgdQ4vfIRQnUdKw+uFPkvDXpggx4ApzQH5vRyu5r37Wb2F9ByJt1Mq2FlX6jk8FUGnWajyk9F2SHakSXftpiA6SNP7wqEhMw= 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 Currently, we are only using flags to indicate which list the cluster is on. Using one bit for each list type might be a waste, as the list type grows, we will consume too many bits. Additionally, the current mixed usage of '&' and '==' is a bit confusing. Make it clean by using an enum to define all possible cluster statuses. Only an off-list cluster will have the NONE (0) flag. And use a wrapper to annotate and sanitize all flag settings and list movements. Suggested-by: Chris Li Signed-off-by: Kairui Song --- include/linux/swap.h | 17 +++++++--- mm/swapfile.c | 75 +++++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 02120f1005d5..339d7f0192ff 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -257,10 +257,19 @@ struct swap_cluster_info { u8 order; struct list_head list; }; -#define CLUSTER_FLAG_FREE 1 /* This cluster is free */ -#define CLUSTER_FLAG_NONFULL 2 /* This cluster is on nonfull list */ -#define CLUSTER_FLAG_FRAG 4 /* This cluster is on nonfull list */ -#define CLUSTER_FLAG_FULL 8 /* This cluster is on full list */ + +/* All on-list cluster must have a non-zero flag. */ +enum swap_cluster_flags { + CLUSTER_FLAG_NONE = 0, /* For temporary off-list cluster */ + CLUSTER_FLAG_FREE, + CLUSTER_FLAG_NONFULL, + CLUSTER_FLAG_FRAG, + /* Clusters with flags above are allocatable */ + CLUSTER_FLAG_USABLE = CLUSTER_FLAG_FRAG, + CLUSTER_FLAG_FULL, + CLUSTER_FLAG_DISCARD, + CLUSTER_FLAG_MAX, +}; /* * The first page in the swap file is the swap header, which is always marked diff --git a/mm/swapfile.c b/mm/swapfile.c index 99fd0b0d84a2..7795a3d27273 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -403,7 +403,7 @@ static void discard_swap_cluster(struct swap_info_struct *si, static inline bool cluster_is_free(struct swap_cluster_info *info) { - return info->flags & CLUSTER_FLAG_FREE; + return info->flags == CLUSTER_FLAG_FREE; } static inline unsigned int cluster_index(struct swap_info_struct *si, @@ -434,6 +434,27 @@ static inline void unlock_cluster(struct swap_cluster_info *ci) spin_unlock(&ci->lock); } +static void cluster_move(struct swap_info_struct *si, + struct swap_cluster_info *ci, struct list_head *list, + enum swap_cluster_flags new_flags) +{ + VM_WARN_ON(ci->flags == new_flags); + BUILD_BUG_ON(1 << sizeof(ci->flags) * BITS_PER_BYTE < CLUSTER_FLAG_MAX); + + if (ci->flags == CLUSTER_FLAG_NONE) { + list_add_tail(&ci->list, list); + } else { + if (ci->flags == CLUSTER_FLAG_FRAG) { + VM_WARN_ON(!si->frag_cluster_nr[ci->order]); + si->frag_cluster_nr[ci->order]--; + } + list_move_tail(&ci->list, list); + } + ci->flags = new_flags; + if (new_flags == CLUSTER_FLAG_FRAG) + si->frag_cluster_nr[ci->order]++; +} + /* Add a cluster to discard list and schedule it to do discard */ static void swap_cluster_schedule_discard(struct swap_info_struct *si, struct swap_cluster_info *ci) @@ -447,10 +468,8 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si, */ memset(si->swap_map + idx * SWAPFILE_CLUSTER, SWAP_MAP_BAD, SWAPFILE_CLUSTER); - - VM_BUG_ON(ci->flags & CLUSTER_FLAG_FREE); - list_move_tail(&ci->list, &si->discard_clusters); - ci->flags = 0; + VM_BUG_ON(ci->flags == CLUSTER_FLAG_FREE); + cluster_move(si, ci, &si->discard_clusters, CLUSTER_FLAG_DISCARD); schedule_work(&si->discard_work); } @@ -458,12 +477,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info { lockdep_assert_held(&si->lock); lockdep_assert_held(&ci->lock); - - if (ci->flags) - list_move_tail(&ci->list, &si->free_clusters); - else - list_add_tail(&ci->list, &si->free_clusters); - ci->flags = CLUSTER_FLAG_FREE; + cluster_move(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order = 0; } @@ -479,6 +493,8 @@ static void swap_do_scheduled_discard(struct swap_info_struct *si) while (!list_empty(&si->discard_clusters)) { ci = list_first_entry(&si->discard_clusters, struct swap_cluster_info, list); list_del(&ci->list); + /* Must clear flag when taking a cluster off-list */ + ci->flags = CLUSTER_FLAG_NONE; idx = cluster_index(si, ci); spin_unlock(&si->lock); @@ -519,9 +535,6 @@ static void free_cluster(struct swap_info_struct *si, struct swap_cluster_info * lockdep_assert_held(&si->lock); lockdep_assert_held(&ci->lock); - if (ci->flags & CLUSTER_FLAG_FRAG) - si->frag_cluster_nr[ci->order]--; - /* * If the swap is discardable, prepare discard the cluster * instead of free it immediately. The cluster will be freed @@ -573,13 +586,9 @@ static void dec_cluster_info_page(struct swap_info_struct *si, return; } - if (!(ci->flags & CLUSTER_FLAG_NONFULL)) { - VM_BUG_ON(ci->flags & CLUSTER_FLAG_FREE); - if (ci->flags & CLUSTER_FLAG_FRAG) - si->frag_cluster_nr[ci->order]--; - list_move_tail(&ci->list, &si->nonfull_clusters[ci->order]); - ci->flags = CLUSTER_FLAG_NONFULL; - } + if (ci->flags != CLUSTER_FLAG_NONFULL) + cluster_move(si, ci, &si->nonfull_clusters[ci->order], + CLUSTER_FLAG_NONFULL); } static bool cluster_reclaim_range(struct swap_info_struct *si, @@ -663,11 +672,13 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster if (!(si->flags & SWP_WRITEOK)) return false; + VM_BUG_ON(ci->flags == CLUSTER_FLAG_NONE); + VM_BUG_ON(ci->flags > CLUSTER_FLAG_USABLE); + if (cluster_is_free(ci)) { - if (nr_pages < SWAPFILE_CLUSTER) { - list_move_tail(&ci->list, &si->nonfull_clusters[order]); - ci->flags = CLUSTER_FLAG_NONFULL; - } + if (nr_pages < SWAPFILE_CLUSTER) + cluster_move(si, ci, &si->nonfull_clusters[order], + CLUSTER_FLAG_NONFULL); ci->order = order; } @@ -675,14 +686,8 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster swap_range_alloc(si, nr_pages); ci->count += nr_pages; - if (ci->count == SWAPFILE_CLUSTER) { - VM_BUG_ON(!(ci->flags & - (CLUSTER_FLAG_FREE | CLUSTER_FLAG_NONFULL | CLUSTER_FLAG_FRAG))); - if (ci->flags & CLUSTER_FLAG_FRAG) - si->frag_cluster_nr[ci->order]--; - list_move_tail(&ci->list, &si->full_clusters); - ci->flags = CLUSTER_FLAG_FULL; - } + if (ci->count == SWAPFILE_CLUSTER) + cluster_move(si, ci, &si->full_clusters, CLUSTER_FLAG_FULL); return true; } @@ -821,9 +826,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o while (!list_empty(&si->nonfull_clusters[order])) { ci = list_first_entry(&si->nonfull_clusters[order], struct swap_cluster_info, list); - list_move_tail(&ci->list, &si->frag_clusters[order]); - ci->flags = CLUSTER_FLAG_FRAG; - si->frag_cluster_nr[order]++; + cluster_move(si, ci, &si->frag_clusters[order], CLUSTER_FLAG_FRAG); offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), &found, order, usage); frags++; From patchwork Mon Dec 30 17:46:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923387 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 97418E7718F for ; Mon, 30 Dec 2024 17:47:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 295A66B009F; Mon, 30 Dec 2024 12:47:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 245E86B00A0; Mon, 30 Dec 2024 12:47:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 070636B00A1; Mon, 30 Dec 2024 12:47:12 -0500 (EST) 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 D58A96B009F for ; Mon, 30 Dec 2024 12:47:12 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 93B6280804 for ; Mon, 30 Dec 2024 17:47:12 +0000 (UTC) X-FDA: 82952355318.22.FEAEDA6 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf30.hostedemail.com (Postfix) with ESMTP id 8A3FC80002 for ; Mon, 30 Dec 2024 17:45:40 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=R61ZKW6g; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.177 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=1735580809; 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=MMVqBvReaM3py2xin+zw3SxNN57mRSkMO4GMwuv7t/w=; b=RGFDXChmH4QblyCNzS1Mvp8srhy+vWuEZvR8q05X+6gk79cQSWh+bok5Le4VmbnbUEK5Jo gGuQOB2bCRjM40ResNXWmLe/DRsf9YN2Cj7+hxTPAj0r8iPgXYNEhgB4vswpW57RMvTqyl dpo7hlVR9zsYjvCjZruw4udSb8MDzbc= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=R61ZKW6g; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580809; a=rsa-sha256; cv=none; b=G+aCo8LALQL8r3/419bmrnE31nbcoQSDHsSW4vQGHyoV8OEEfp6DczLxFe0tF2pD8YiQDH ijVcQDMZFd/RPIplf14nna7Aw3F/DGriW5F0s5XkqG5Y+D23kBD+MmYbenywQYwZY3ubJ9 MegwARt2mgvjdclqVc+v+uQcQDj6+mU= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-21634338cfdso176663805ad.2 for ; Mon, 30 Dec 2024 09:47:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580828; x=1736185628; 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=MMVqBvReaM3py2xin+zw3SxNN57mRSkMO4GMwuv7t/w=; b=R61ZKW6gGd3A5ztsIwygD7CJOyflfxSdJbI6ZqeQ/rBpBfk3QStDKG1QE11fp7dHjc TjO6Mq5dfpnWqpjiPg0tFtNwJjxO04B7KjV/TJpulpB1qrnphBMLSfTta48wS4Nx1V6D v+um4E3tvLxyhChXuzZ9TH2Pp+JW7MNcSyRYczYyrpu07jdYZRghi8ou0vScQJMG9psO iCpnUoAJvRV5wwKobIUw+DbydWNxpKIlKEQ4CyliGz6aAl1PeObrRfOofO5CWM4gSsJm 544AXkKVb87fQ43qBG+1yHzbQO86zElZc8O41/VFE9IgsMCqUrtGX2nabJLg0mylYTLk 6KoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580828; x=1736185628; 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=MMVqBvReaM3py2xin+zw3SxNN57mRSkMO4GMwuv7t/w=; b=aL1Ww5lkNdk9sbFOepPU7fMDotqKULC5mAiWRrTQnq0cHi6Bp+xhh9IXEhaza3+eyv YqyT6OM5ivGgihwdu3WHeuMI7iqPACIvSh27o+tjuw9APuy1fk+YeyBA4kjUCqIZ6KuR Fw9w63Zj7sOEW9FCaWxu9tZw+3UQiNwP8CQ4yTIHmxRfauP5n5LbCgjW3z1VerfaXvX9 FzMbZJQfxeiuW2PeDT2V7FGIrK3KoSysfAqQ1gFFcFhhT12/nBhBFxcuQhhUgH427UZ1 KX6nn2dBfoQ7BBq+PgbClB6TdbyVzkqP/PTrjc+1XI7HCOP+p5nB2UgLVhiZMwC0Pzvf q8uw== X-Gm-Message-State: AOJu0YxQ3mPwBW80kVvqOZu9IA9Ag2yUIIjQtQPEU5zVFbX7PTFe4w4i FHGiziM/A5sty/O+SOpWloXkSFPWJmqiuLaKNwKLAFi3AZWj3XGew7pg68Cwy+ZMQA== X-Gm-Gg: ASbGncs8rO1HEPUIPVtCH97BWLQnh7HDRhl3QT5st9Nn7gt37uhQCwKyCKMkD7cem34 I5zxpVctwH2W5sm81lqIvO34NgefFEUlZZnIImF3Jk/kfVFpb6VGMlTO2DIlWprA3r4EAUSOgQi S9nZQgbq5n6/cjTHAA6kiQQv2UmNx3RUtZ7VCelQdusN+f0v3yo8w1VlEddfBpC0ZDi1YHLWmNG +Tvx53mUYb4PX6ipcJcuJkXqaJrQatkBNzYqB7oTJW04hGJeEV6q+wUZwHmB9TYYk2qPCZtoNQt X-Google-Smtp-Source: AGHT+IFcZD734AOxLa21wZwdhM3y4zrXqiNPdFWPQswbdFiKbDRrDVSKPq1OkQ97NHnaspouwe6/Eg== X-Received: by 2002:a17:903:22c6:b0:216:2474:3c9f with SMTP id d9443c01a7336-219e6f38177mr524847175ad.52.1735580827940; Mon, 30 Dec 2024 09:47:07 -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.47.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:07 -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 09/13] mm, swap: reduce contention on device lock Date: Tue, 31 Dec 2024 01:46:17 +0800 Message-ID: <20241230174621.61185-10-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 MIME-Version: 1.0 X-Rspamd-Queue-Id: 8A3FC80002 X-Stat-Signature: pess39b95jnuqcb55zu5cywjsixnmzsu X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1735580740-737682 X-HE-Meta: U2FsdGVkX1/VtLLuplJALm7woLW+kZvC6wQM5YHjKOxoXjbxS1rNk2W6w+zQCUjc/IunLK2CvLevA4nTc/nM6PS19Xvw7mIoOdROA756BeE03RvQL4XvAOWcAJgfRKS90J0Q/WljKOpfbORYNbpV3HkaWNyKltJioimaaS+iMhrDI4xieeVughHOH2pXGwpFGRMvc3+vjQieTLGdkdwi+diZLKl9F2ITs4Yb+MDvydNocU9SJINQXXGwOjR8ZBLn+zY3DIqHgEn/T3WXKuJdooCNhYmxawaob9Sni3x/fakyuwnj6WysZjqrv2yN+FC88Ow4KDJQmQ4mwdDa69CAUB9+u1SJ22EkZSGE5dWsKV0pvOsxF+sZrWZRhMMD26XMzXfeZTvFkLQJ/FpaoSOteqvQDYoZFXpri3I6HjZWZeTglv11jPeGJrvLCmKjvS0UqaMxiorMI3lvdBkLwOkvLkKHg6kFIDi4vgIAJkKppo3hw3yq6YG959S1pX9slEBh+UglCSXN5Uj/tUz+R2/lvrY7tqzVlp6u8+3zxYlNcFh82nQ1te3TSX51vRiqpbWzvzG7gBTuqFPqcdsgejJOTY2d2jyatWj4jJhhT9XFLrySTS/5EqamI7Oc5a9Ju5vKe2u+Ku3W0ZQpg2yXwxQqirvEzHYW+lnQ8XlcDSsuLzYMpQUgGvSNuWomBS7QqRx5DZlSVsu+s1hpXHeQp4M1fESzuOeYb3IcvfDhbPBn58g+jbQOQlf2S6XZe9JomqITvDm0S7tZWBIlG2CaQLOKZipwdyR9faXx4Pdiy4sxZxebOO2/1CqsaJnyXqiJ1wewz1eohSViuL7+7yh3PYhkXUm4VmyoNAAzsRcdrx3sYpBgrNqspJMmr6jYjaRHv6TahpB3eK0RxODExuVvmiXAL+VTy8U/2wrUY99RT4KoeUCOHdHMXwjvqKQijm25cuXaoVw7a9+GE88Hcb5Yxpc FU0pF+OT xYZVs4H9DezmZUNcSH6tOqBnLOJM6YC3uhil6jUTt+CdbO8wevf49N7nyYVoUa7D4YvZI+O9LIgXmlt0Vb/NUCwPx/iHof+2+hx3Tw+fccQ2a38Fl28hkPsJjvJuE6ubDtnDZxUYc+BoRzs48dl+LKNemjwoc5B7VWqriX5E0iISO7gnkxyUWZtj+6+hbT7vulzrP2vxSQEe0Lt978dbzcPqgYWzy+mmQ5klFucKbjBrr1j/XDs2zCRpKaBylA3/HNoBh+uqsiw87dcWvkVwZWL6yerOmIJKWXtMeM4I2HgY82X9meOnJ1R44kA9EFQGJDR7uf6rIbc0yBvKfQuOdp+WG/vqYewUfYcUkdwsq2HRUTc0sg6MKrs8qQOsWZfxNGEJZuyYoulJQZa/M57jhHE3Sj7iJA/ZU4Z/5U/ZB+KpFVnMpVB+DqXKH251hjAT3jX7EoBjiqCm6ecVpDXOqTtTmG6S3i1hawwOkGr3zFck98kvqyxhRnLVTKMj6UBKAwsLUVoSDSn/im/NE93AF5tcWelGOfLllPYQ42/qMjvCHGdo= 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 Currently, swap locking is mainly composed of two locks: the cluster lock (ci->lock) and the device lock (si->lock). The cluster lock is much more fine-grained, so it is best to use ci->lock instead of si->lock as much as possible. We have cleaned up other hard dependencies on si->lock. Following the new cluster allocator design, most operations don't need to touch si->lock at all. In practice, we only need to take si->lock when moving clusters between lists. To achieve this, this commit reworks the locking pattern of all si->lock and ci->lock users, eliminates all usage of ci->lock inside si->lock, and introduces a new design to avoid touching si->lock unless needed. For minimal contention and easier understanding of the system, two ideas are introduced with the corresponding helpers: isolation and relocation. - Clusters will be `isolated` from the list when iterating the list to search for an allocatable cluster. This ensures other CPUs won't walk into the same cluster easily, and it releases si->lock after acquiring ci->lock, providing the only place that handles the inversion of two locks, and avoids contention. Iterating the cluster list almost always moves the cluster (free -> nonfull, nonfull -> frag, frag -> frag tail), but it doesn't know where the cluster should be moved to until scanning is done. So keeping the cluster off-list is a good option with low overhead. The off-list time window of a cluster is also minimal. In the worst case, one CPU will return the cluster after scanning the 512 entries on it, which we used to busy wait with a spin lock. This is done with the new helper `cluster_isolate_lock`. - Clusters will be `relocated` after allocation or freeing, according to their usage count and status. Allocations no longer hold si->lock now, and may drop ci->lock for reclaim, so the cluster could be moved to any location while no lock is held. Besides, isolation clears all flags when it takes the cluster off the list (the flags must be in sync with the list status, so cluster users don't need to touch si->lock for checking its list status). So the cluster has to be relocated to the right list according to its usage after allocation or freeing. Relocation is optional, if the cluster flags indicate it's already on the right list, it will skip touching the list or si->lock. This is done with relocate_cluster after allocation or with [partial_]free_cluster after freeing. This handled usage of all kinds of clusters in a clean way. Scanning and allocation by iterating the cluster list is handled by "isolate - - relocate". Scanning and allocation of per-CPU clusters will only involve " - relocate", as it knows which cluster to lock and use. Freeing will only involve "relocate". Each CPU will keep using its per-CPU cluster until the 512 entries are all consumed. Freeing also has to free 512 entries to trigger cluster movement in the best case, so si->lock is rarely touched. Testing with building the Linux kernel with defconfig showed huge improvement: tiem make -j96 / 768M memcg, 4K pages, 10G ZRAM, on Intel 8255C: Before: Sys time: 73578.30, Real time: 864.05 After: (-50.7% sys time, -44.8% real time) Sys time: 36227.49, Real time: 476.66 time make -j96 / 1152M memcg, 64K mTHP, 10G ZRAM, on Intel 8255C: (avg of 4 test run) Before: Sys time: 74044.85, Real time: 846.51 hugepages-64kB/stats/swpout: 1735216 hugepages-64kB/stats/swpout_fallback: 430333 After: (-40.4% sys time, -37.1% real time) Sys time: 44160.56, Real time: 532.07 hugepages-64kB/stats/swpout: 1786288 hugepages-64kB/stats/swpout_fallback: 243384 time make -j32 / 512M memcg, 4K pages, 5G ZRAM, on AMD 7K62: Before: Sys time: 8098.21, Real time: 401.3 After: (-22.6% sys time, -12.8% real time ) Sys time: 6265.02, Real time: 349.83 The allocation success rate also slightly improved as we sanitized the usage of clusters with new defined helpers, previously dropping si->lock or ci->lock during scan will cause cluster order shuffle. Suggested-by: Chris Li Signed-off-by: Kairui Song --- include/linux/swap.h | 3 +- mm/swapfile.c | 435 ++++++++++++++++++++++++------------------- 2 files changed, 246 insertions(+), 192 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 339d7f0192ff..c4ff31cb6bde 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -291,6 +291,7 @@ enum swap_cluster_flags { * throughput. */ struct percpu_cluster { + local_lock_t lock; /* Protect the percpu_cluster above */ unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */ }; @@ -313,7 +314,7 @@ struct swap_info_struct { /* list of cluster that contains at least one free slot */ struct list_head frag_clusters[SWAP_NR_ORDERS]; /* list of cluster that are fragmented or contented */ - unsigned int frag_cluster_nr[SWAP_NR_ORDERS]; + atomic_long_t frag_cluster_nr[SWAP_NR_ORDERS]; unsigned int pages; /* total of usable pages of swap */ atomic_long_t inuse_pages; /* number of those currently in use */ struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap location */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 7795a3d27273..dadd4fead689 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -261,12 +261,10 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, folio_ref_sub(folio, nr_pages); folio_set_dirty(folio); - spin_lock(&si->lock); /* Only sinple page folio can be backed by zswap */ if (nr_pages == 1) zswap_invalidate(entry); swap_entry_range_free(si, entry, nr_pages); - spin_unlock(&si->lock); ret = nr_pages; out_unlock: folio_unlock(folio); @@ -403,7 +401,21 @@ static void discard_swap_cluster(struct swap_info_struct *si, static inline bool cluster_is_free(struct swap_cluster_info *info) { - return info->flags == CLUSTER_FLAG_FREE; + return info->count == 0; +} + +static inline bool cluster_is_discard(struct swap_cluster_info *info) +{ + return info->flags == CLUSTER_FLAG_DISCARD; +} + +static inline bool cluster_is_usable(struct swap_cluster_info *ci, int order) +{ + if (unlikely(ci->flags > CLUSTER_FLAG_USABLE)) + return false; + if (!order) + return true; + return cluster_is_free(ci) || order == ci->order; } static inline unsigned int cluster_index(struct swap_info_struct *si, @@ -440,19 +452,20 @@ static void cluster_move(struct swap_info_struct *si, { VM_WARN_ON(ci->flags == new_flags); BUILD_BUG_ON(1 << sizeof(ci->flags) * BITS_PER_BYTE < CLUSTER_FLAG_MAX); + lockdep_assert_held(&ci->lock); - if (ci->flags == CLUSTER_FLAG_NONE) { + spin_lock(&si->lock); + if (ci->flags == CLUSTER_FLAG_NONE) list_add_tail(&ci->list, list); - } else { - if (ci->flags == CLUSTER_FLAG_FRAG) { - VM_WARN_ON(!si->frag_cluster_nr[ci->order]); - si->frag_cluster_nr[ci->order]--; - } + else list_move_tail(&ci->list, list); - } + spin_unlock(&si->lock); + + if (ci->flags == CLUSTER_FLAG_FRAG) + atomic_long_dec(&si->frag_cluster_nr[ci->order]); + else if (new_flags == CLUSTER_FLAG_FRAG) + atomic_long_inc(&si->frag_cluster_nr[ci->order]); ci->flags = new_flags; - if (new_flags == CLUSTER_FLAG_FRAG) - si->frag_cluster_nr[ci->order]++; } /* Add a cluster to discard list and schedule it to do discard */ @@ -475,39 +488,90 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si, static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci) { - lockdep_assert_held(&si->lock); lockdep_assert_held(&ci->lock); cluster_move(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order = 0; } +/* + * Isolate and lock the first cluster that is not contented on a list, + * clean its flag before taken off-list. Cluster flag must be in sync + * with list status, so cluster updaters can always know the cluster + * list status without touching si lock. + * + * Note it's possible that all clusters on a list are contented so + * this returns NULL for an non-empty list. + */ +static struct swap_cluster_info *cluster_isolate_lock( + struct swap_info_struct *si, struct list_head *list) +{ + struct swap_cluster_info *ci, *ret = NULL; + + spin_lock(&si->lock); + + if (unlikely(!(si->flags & SWP_WRITEOK))) + goto out; + + list_for_each_entry(ci, list, list) { + if (!spin_trylock(&ci->lock)) + continue; + + /* We may only isolate and clear flags of following lists */ + VM_BUG_ON(!ci->flags); + VM_BUG_ON(ci->flags > CLUSTER_FLAG_USABLE && + ci->flags != CLUSTER_FLAG_FULL); + + list_del(&ci->list); + ci->flags = CLUSTER_FLAG_NONE; + ret = ci; + break; + } +out: + spin_unlock(&si->lock); + + return ret; +} + /* * Doing discard actually. After a cluster discard is finished, the cluster - * will be added to free cluster list. caller should hold si->lock. -*/ -static void swap_do_scheduled_discard(struct swap_info_struct *si) + * will be added to free cluster list. Discard cluster is a bit special as + * they don't participate in allocation or reclaim, so clusters marked as + * CLUSTER_FLAG_DISCARD must remain off-list or on discard list. + */ +static bool swap_do_scheduled_discard(struct swap_info_struct *si) { struct swap_cluster_info *ci; + bool ret = false; unsigned int idx; + spin_lock(&si->lock); while (!list_empty(&si->discard_clusters)) { ci = list_first_entry(&si->discard_clusters, struct swap_cluster_info, list); + /* + * Delete the cluster from list but don't clear its flags until + * discard is done, so isolation and relocation will skip it. + */ list_del(&ci->list); - /* Must clear flag when taking a cluster off-list */ - ci->flags = CLUSTER_FLAG_NONE; idx = cluster_index(si, ci); spin_unlock(&si->lock); - discard_swap_cluster(si, idx * SWAPFILE_CLUSTER, SWAPFILE_CLUSTER); - spin_lock(&si->lock); spin_lock(&ci->lock); - __free_cluster(si, ci); + /* + * Discard is done, clear its flags as it's now off-list, + * then return the cluster to allocation list. + */ + ci->flags = CLUSTER_FLAG_NONE; memset(si->swap_map + idx * SWAPFILE_CLUSTER, 0, SWAPFILE_CLUSTER); + __free_cluster(si, ci); spin_unlock(&ci->lock); + ret = true; + spin_lock(&si->lock); } + spin_unlock(&si->lock); + return ret; } static void swap_discard_work(struct work_struct *work) @@ -516,9 +580,7 @@ static void swap_discard_work(struct work_struct *work) si = container_of(work, struct swap_info_struct, discard_work); - spin_lock(&si->lock); swap_do_scheduled_discard(si); - spin_unlock(&si->lock); } static void swap_users_ref_free(struct percpu_ref *ref) @@ -529,10 +591,14 @@ static void swap_users_ref_free(struct percpu_ref *ref) complete(&si->comp); } +/* + * Must be called after freeing if ci->count == 0, moves the cluster to free + * or discard list. + */ static void free_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci) { VM_BUG_ON(ci->count != 0); - lockdep_assert_held(&si->lock); + VM_BUG_ON(ci->flags == CLUSTER_FLAG_FREE); lockdep_assert_held(&ci->lock); /* @@ -549,6 +615,48 @@ static void free_cluster(struct swap_info_struct *si, struct swap_cluster_info * __free_cluster(si, ci); } +/* + * Must be called after freeing if ci->count != 0, moves the cluster to + * nonfull list. + */ +static void partial_free_cluster(struct swap_info_struct *si, + struct swap_cluster_info *ci) +{ + VM_BUG_ON(!ci->count || ci->count == SWAPFILE_CLUSTER); + lockdep_assert_held(&ci->lock); + + if (ci->flags != CLUSTER_FLAG_NONFULL) + cluster_move(si, ci, &si->nonfull_clusters[ci->order], + CLUSTER_FLAG_NONFULL); +} + +/* + * Must be called after allocation, moves the cluster to full or frag list. + * Note: allocation doesn't acquire si lock, and may drop the ci lock for + * reclaim, so the cluster could be any where when called. + */ +static void relocate_cluster(struct swap_info_struct *si, + struct swap_cluster_info *ci) +{ + lockdep_assert_held(&ci->lock); + + /* Discard cluster must remain off-list or on discard list */ + if (cluster_is_discard(ci)) + return; + + if (!ci->count) { + free_cluster(si, ci); + } else if (ci->count != SWAPFILE_CLUSTER) { + if (ci->flags != CLUSTER_FLAG_FRAG) + cluster_move(si, ci, &si->frag_clusters[ci->order], + CLUSTER_FLAG_FRAG); + } else { + if (ci->flags != CLUSTER_FLAG_FULL) + cluster_move(si, ci, &si->full_clusters, + CLUSTER_FLAG_FULL); + } +} + /* * The cluster corresponding to page_nr will be used. The cluster will not be * added to free cluster list and its usage counter will be increased by 1. @@ -567,30 +675,6 @@ static void inc_cluster_info_page(struct swap_info_struct *si, VM_BUG_ON(ci->flags); } -/* - * The cluster ci decreases @nr_pages usage. If the usage counter becomes 0, - * which means no page in the cluster is in use, we can optionally discard - * the cluster and add it to free cluster list. - */ -static void dec_cluster_info_page(struct swap_info_struct *si, - struct swap_cluster_info *ci, int nr_pages) -{ - VM_BUG_ON(ci->count < nr_pages); - VM_BUG_ON(cluster_is_free(ci)); - lockdep_assert_held(&si->lock); - lockdep_assert_held(&ci->lock); - ci->count -= nr_pages; - - if (!ci->count) { - free_cluster(si, ci); - return; - } - - if (ci->flags != CLUSTER_FLAG_NONFULL) - cluster_move(si, ci, &si->nonfull_clusters[ci->order], - CLUSTER_FLAG_NONFULL); -} - static bool cluster_reclaim_range(struct swap_info_struct *si, struct swap_cluster_info *ci, unsigned long start, unsigned long end) @@ -600,8 +684,6 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, int nr_reclaim; spin_unlock(&ci->lock); - spin_unlock(&si->lock); - do { switch (READ_ONCE(map[offset])) { case 0: @@ -619,9 +701,7 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, } } while (offset < end); out: - spin_lock(&si->lock); spin_lock(&ci->lock); - /* * Recheck the range no matter reclaim succeeded or not, the slot * could have been be freed while we are not holding the lock. @@ -635,11 +715,11 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, static bool cluster_scan_range(struct swap_info_struct *si, struct swap_cluster_info *ci, - unsigned long start, unsigned int nr_pages) + unsigned long start, unsigned int nr_pages, + bool *need_reclaim) { unsigned long offset, end = start + nr_pages; unsigned char *map = si->swap_map; - bool need_reclaim = false; for (offset = start; offset < end; offset++) { switch (READ_ONCE(map[offset])) { @@ -648,16 +728,13 @@ static bool cluster_scan_range(struct swap_info_struct *si, case SWAP_HAS_CACHE: if (!vm_swap_full()) return false; - need_reclaim = true; + *need_reclaim = true; continue; default: return false; } } - if (need_reclaim) - return cluster_reclaim_range(si, ci, start, end); - return true; } @@ -672,23 +749,13 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster if (!(si->flags & SWP_WRITEOK)) return false; - VM_BUG_ON(ci->flags == CLUSTER_FLAG_NONE); - VM_BUG_ON(ci->flags > CLUSTER_FLAG_USABLE); - - if (cluster_is_free(ci)) { - if (nr_pages < SWAPFILE_CLUSTER) - cluster_move(si, ci, &si->nonfull_clusters[order], - CLUSTER_FLAG_NONFULL); + if (cluster_is_free(ci)) ci->order = order; - } memset(si->swap_map + start, usage, nr_pages); swap_range_alloc(si, nr_pages); ci->count += nr_pages; - if (ci->count == SWAPFILE_CLUSTER) - cluster_move(si, ci, &si->full_clusters, CLUSTER_FLAG_FULL); - return true; } @@ -699,37 +766,55 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, unsigne unsigned long start = offset & ~(SWAPFILE_CLUSTER - 1); unsigned long end = min(start + SWAPFILE_CLUSTER, si->max); unsigned int nr_pages = 1 << order; + bool need_reclaim, ret; struct swap_cluster_info *ci; - if (end < nr_pages) - return SWAP_NEXT_INVALID; - end -= nr_pages; + ci = &si->cluster_info[offset / SWAPFILE_CLUSTER]; + lockdep_assert_held(&ci->lock); - ci = lock_cluster(si, offset); - if (ci->count + nr_pages > SWAPFILE_CLUSTER) { + if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER) { offset = SWAP_NEXT_INVALID; - goto done; + goto out; } - while (offset <= end) { - if (cluster_scan_range(si, ci, offset, nr_pages)) { - if (!cluster_alloc_range(si, ci, offset, usage, order)) { - offset = SWAP_NEXT_INVALID; - goto done; - } - *foundp = offset; - if (ci->count == SWAPFILE_CLUSTER) { + for (end -= nr_pages; offset <= end; offset += nr_pages) { + need_reclaim = false; + if (!cluster_scan_range(si, ci, offset, nr_pages, &need_reclaim)) + continue; + if (need_reclaim) { + ret = cluster_reclaim_range(si, ci, start, end); + /* + * Reclaim drops ci->lock and cluster could be used + * by another order. Not checking flag as off-list + * cluster has no flag set, and change of list + * won't cause fragmentation. + */ + if (!cluster_is_usable(ci, order)) { offset = SWAP_NEXT_INVALID; - goto done; + goto out; } - offset += nr_pages; - break; + if (cluster_is_free(ci)) + offset = start; + /* Reclaim failed but cluster is usable, try next */ + if (!ret) + continue; + } + if (!cluster_alloc_range(si, ci, offset, usage, order)) { + offset = SWAP_NEXT_INVALID; + goto out; + } + *foundp = offset; + if (ci->count == SWAPFILE_CLUSTER) { + offset = SWAP_NEXT_INVALID; + goto out; } offset += nr_pages; + break; } if (offset > end) offset = SWAP_NEXT_INVALID; -done: +out: + relocate_cluster(si, ci); unlock_cluster(ci); return offset; } @@ -746,18 +831,17 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) if (force) to_scan = swap_usage_in_pages(si) / SWAPFILE_CLUSTER; - while (!list_empty(&si->full_clusters)) { - ci = list_first_entry(&si->full_clusters, struct swap_cluster_info, list); - list_move_tail(&ci->list, &si->full_clusters); + while ((ci = cluster_isolate_lock(si, &si->full_clusters))) { offset = cluster_offset(si, ci); end = min(si->max, offset + SWAPFILE_CLUSTER); to_scan--; - spin_unlock(&si->lock); while (offset < end) { if (READ_ONCE(map[offset]) == SWAP_HAS_CACHE) { + spin_unlock(&ci->lock); nr_reclaim = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_DIRECT); + spin_lock(&ci->lock); if (nr_reclaim) { offset += abs(nr_reclaim); continue; @@ -765,8 +849,8 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) } offset++; } - spin_lock(&si->lock); + unlock_cluster(ci); if (to_scan <= 0) break; } @@ -778,9 +862,7 @@ static void swap_reclaim_work(struct work_struct *work) si = container_of(work, struct swap_info_struct, reclaim_work); - spin_lock(&si->lock); swap_reclaim_full_clusters(si, true); - spin_unlock(&si->lock); } /* @@ -791,29 +873,34 @@ static void swap_reclaim_work(struct work_struct *work) static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int order, unsigned char usage) { - struct percpu_cluster *cluster; struct swap_cluster_info *ci; unsigned int offset, found = 0; -new_cluster: - lockdep_assert_held(&si->lock); - cluster = this_cpu_ptr(si->percpu_cluster); - offset = cluster->next[order]; + /* Fast path using per CPU cluster */ + local_lock(&si->percpu_cluster->lock); + offset = __this_cpu_read(si->percpu_cluster->next[order]); if (offset) { - offset = alloc_swap_scan_cluster(si, offset, &found, order, usage); + ci = lock_cluster(si, offset); + /* Cluster could have been used by another order */ + if (cluster_is_usable(ci, order)) { + if (cluster_is_free(ci)) + offset = cluster_offset(si, ci); + offset = alloc_swap_scan_cluster(si, offset, &found, + order, usage); + } else { + unlock_cluster(ci); + } if (found) goto done; } - if (!list_empty(&si->free_clusters)) { - ci = list_first_entry(&si->free_clusters, struct swap_cluster_info, list); - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), &found, order, usage); - /* - * Either we didn't touch the cluster due to swapoff, - * or the allocation must success. - */ - VM_BUG_ON((si->flags & SWP_WRITEOK) && !found); - goto done; +new_cluster: + ci = cluster_isolate_lock(si, &si->free_clusters); + if (ci) { + offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), + &found, order, usage); + if (found) + goto done; } /* Try reclaim from full clusters if free clusters list is drained */ @@ -821,49 +908,45 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o swap_reclaim_full_clusters(si, false); if (order < PMD_ORDER) { - unsigned int frags = 0; + unsigned int frags = 0, frags_existing; - while (!list_empty(&si->nonfull_clusters[order])) { - ci = list_first_entry(&si->nonfull_clusters[order], - struct swap_cluster_info, list); - cluster_move(si, ci, &si->frag_clusters[order], CLUSTER_FLAG_FRAG); + while ((ci = cluster_isolate_lock(si, &si->nonfull_clusters[order]))) { offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), &found, order, usage); - frags++; + /* + * With `fragmenting` set to true, it will surely take + * the cluster off nonfull list + */ if (found) goto done; + frags++; } - /* - * Nonfull clusters are moved to frag tail if we reached - * here, count them too, don't over scan the frag list. - */ - while (frags < si->frag_cluster_nr[order]) { - ci = list_first_entry(&si->frag_clusters[order], - struct swap_cluster_info, list); + frags_existing = atomic_long_read(&si->frag_cluster_nr[order]); + while (frags < frags_existing && + (ci = cluster_isolate_lock(si, &si->frag_clusters[order]))) { + atomic_long_dec(&si->frag_cluster_nr[order]); /* - * Rotate the frag list to iterate, they were all failing - * high order allocation or moved here due to per-CPU usage, - * this help keeping usable cluster ahead. + * Rotate the frag list to iterate, they were all + * failing high order allocation or moved here due to + * per-CPU usage, but they could contain newly released + * reclaimable (eg. lazy-freed swap cache) slots. */ - list_move_tail(&ci->list, &si->frag_clusters[order]); offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), &found, order, usage); - frags++; if (found) goto done; + frags++; } } - if (!list_empty(&si->discard_clusters)) { - /* - * we don't have free cluster but have some clusters in - * discarding, do discard now and reclaim them, then - * reread cluster_next_cpu since we dropped si->lock - */ - swap_do_scheduled_discard(si); + /* + * We don't have free cluster but have some clusters in + * discarding, do discard now and reclaim them, then + * reread cluster_next_cpu since we dropped si->lock + */ + if ((si->flags & SWP_PAGE_DISCARD) && swap_do_scheduled_discard(si)) goto new_cluster; - } if (order) goto done; @@ -874,26 +957,25 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o * Clusters here have at least one usable slots and can't fail order 0 * allocation, but reclaim may drop si->lock and race with another user. */ - while (!list_empty(&si->frag_clusters[o])) { - ci = list_first_entry(&si->frag_clusters[o], - struct swap_cluster_info, list); + while ((ci = cluster_isolate_lock(si, &si->frag_clusters[o]))) { + atomic_long_dec(&si->frag_cluster_nr[o]); offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, 0, usage); + &found, order, usage); if (found) goto done; } - while (!list_empty(&si->nonfull_clusters[o])) { - ci = list_first_entry(&si->nonfull_clusters[o], - struct swap_cluster_info, list); + while ((ci = cluster_isolate_lock(si, &si->nonfull_clusters[o]))) { offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, 0, usage); + &found, order, usage); if (found) goto done; } } done: - cluster->next[order] = offset; + __this_cpu_write(si->percpu_cluster->next[order], offset); + local_unlock(&si->percpu_cluster->lock); + return found; } @@ -1157,14 +1239,11 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); spin_unlock(&swap_avail_lock); 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); @@ -1377,9 +1456,7 @@ static bool __swap_entries_free(struct swap_info_struct *si, if (!has_cache) { for (i = 0; i < nr; i++) zswap_invalidate(swp_entry(si->type, offset + i)); - spin_lock(&si->lock); swap_entry_range_free(si, entry, nr); - spin_unlock(&si->lock); } return has_cache; @@ -1408,16 +1485,27 @@ static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry unsigned char *map_end = map + nr_pages; struct swap_cluster_info *ci; + /* It should never free entries across different clusters */ + VM_BUG_ON((offset / SWAPFILE_CLUSTER) != ((offset + nr_pages - 1) / SWAPFILE_CLUSTER)); + ci = lock_cluster(si, offset); + VM_BUG_ON(cluster_is_free(ci)); + VM_BUG_ON(ci->count < nr_pages); + + ci->count -= nr_pages; do { VM_BUG_ON(*map != SWAP_HAS_CACHE); *map = 0; } while (++map < map_end); - dec_cluster_info_page(si, ci, nr_pages); - unlock_cluster(ci); mem_cgroup_uncharge_swap(entry, nr_pages); swap_range_free(si, offset, nr_pages); + + if (!ci->count) + free_cluster(si, ci); + else + partial_free_cluster(si, ci); + unlock_cluster(ci); } static void cluster_swap_free_nr(struct swap_info_struct *si, @@ -1489,9 +1577,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) ci = lock_cluster(si, offset); if (size > 1 && swap_is_has_cache(si, offset, size)) { unlock_cluster(ci); - spin_lock(&si->lock); swap_entry_range_free(si, entry, size); - spin_unlock(&si->lock); return; } for (int i = 0; i < size; i++, entry.val++) { @@ -1506,46 +1592,19 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) unlock_cluster(ci); } -static int swp_entry_cmp(const void *ent1, const void *ent2) -{ - const swp_entry_t *e1 = ent1, *e2 = ent2; - - return (int)swp_type(*e1) - (int)swp_type(*e2); -} - void swapcache_free_entries(swp_entry_t *entries, int n) { - struct swap_info_struct *si, *prev; int i; + struct swap_info_struct *si = NULL; if (n <= 0) return; - prev = NULL; - si = NULL; - - /* - * Sort swap entries by swap device, so each lock is only taken once. - * nr_swapfiles isn't absolutely correct, but the overhead of sort() is - * so low that it isn't necessary to optimize further. - */ - if (nr_swapfiles > 1) - sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); for (i = 0; i < n; ++i) { si = _swap_info_get(entries[i]); - - if (si != prev) { - if (prev != NULL) - spin_unlock(&prev->lock); - if (si != NULL) - spin_lock(&si->lock); - } if (si) swap_entry_range_free(si, entries[i], 1); - prev = si; } - if (si) - spin_unlock(&si->lock); } int __swap_count(swp_entry_t entry) @@ -1797,13 +1856,8 @@ swp_entry_t get_swap_page_of_type(int type) goto fail; /* This is called for allocating swap entry, not cache */ - 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); - } + if ((si->flags & SWP_WRITEOK) && scan_swap_map_slots(si, 1, 1, &entry, 0)) + atomic_long_dec(&nr_swap_pages); fail: return entry; } @@ -3141,6 +3195,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, cluster = per_cpu_ptr(si->percpu_cluster, cpu); for (i = 0; i < SWAP_NR_ORDERS; i++) cluster->next[i] = SWAP_NEXT_INVALID; + local_lock_init(&cluster->lock); } /* @@ -3164,7 +3219,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, for (i = 0; i < SWAP_NR_ORDERS; i++) { INIT_LIST_HEAD(&si->nonfull_clusters[i]); INIT_LIST_HEAD(&si->frag_clusters[i]); - si->frag_cluster_nr[i] = 0; + atomic_long_set(&si->frag_cluster_nr[i], 0); } /* @@ -3646,7 +3701,6 @@ int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) */ goto outer; } - spin_lock(&si->lock); offset = swp_offset(entry); @@ -3711,7 +3765,6 @@ int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) spin_unlock(&si->cont_lock); out: unlock_cluster(ci); - spin_unlock(&si->lock); put_swap_device(si); outer: if (page) From patchwork Mon Dec 30 17:46:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923388 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 7769CE7718F for ; Mon, 30 Dec 2024 17:47:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04D1D6B00A0; Mon, 30 Dec 2024 12:47:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3DEB6B00A1; Mon, 30 Dec 2024 12:47:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB6B86B00A2; Mon, 30 Dec 2024 12:47:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B562F6B00A0 for ; Mon, 30 Dec 2024 12:47:16 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 69026444E6 for ; Mon, 30 Dec 2024 17:47:16 +0000 (UTC) X-FDA: 82952354520.03.2FEA500 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf04.hostedemail.com (Postfix) with ESMTP id E478F40006 for ; Mon, 30 Dec 2024 17:46:25 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=SKAk8kD+; spf=pass (imf04.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.169 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=1735580813; 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=kMKA8ivFUMjT0SUJNutwCBcko1EjPqMzoc0GZ2wQpbM=; b=zNLxowPykmT/YAnk8RUPw/ZAkE6nVxMXSY2sFSPPd64OrI8qSba5LSIMH98vhpfcYz7ZXc NGkRcvnQrMsyR7f3EhTD9b/Xlo3bDmGSeS2OdTiYcN8k1igLhDwMMhk7dBN2qUARbctxnA NPGesYQoGSPMr11B/GT+xRrQXvWpHKg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=SKAk8kD+; spf=pass (imf04.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580813; a=rsa-sha256; cv=none; b=cWxj7k7Md52qtceZU7HrolduthkiKnXxcR7zuidO3ACUFQIWYIOa4RlkSdAwN3UL5bqvPB mtFl2DsJDEYVDNRWhDz/8wEsYfhuL5bV6XYbwmggmAiByA7VHWgIHmx9vDZR6OH/lBRTNq qCm9rdtWW9HzMeccXigeWxuDraCmIz4= Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2163b0c09afso120032285ad.0 for ; Mon, 30 Dec 2024 09:47:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580832; x=1736185632; 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=kMKA8ivFUMjT0SUJNutwCBcko1EjPqMzoc0GZ2wQpbM=; b=SKAk8kD+9tmN422//upaHvd8bEDlZlg0/bANpTcO73X6Juu+WHQ+yxR632yn80aLN9 1MTJyRKKuvy+15//3mFnjF6ktfl28xNbLMK7lgdi2BQgaz39+MrEMPtXQ+X2LraQw6xw F+C9NpIdQUlcgi15TRkjMP7/uph1Ucdn2hn36MtuXF3bUVZpe+fObaf0RU7VeM1yosoa WgRvDt+u0aSsV5UJ2j6FFwtSoXDTgMIRCAxF+DHvN7EWa0f6f4l1RvUQ8Yy0zLjJP+Hv l5BNL7NZTSD2YzYSwb/EvfupZnbCReDNxlPZudJG66CfOctOFNguRWe4IHJ5EFD2dHtO 9ZFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580832; x=1736185632; 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=kMKA8ivFUMjT0SUJNutwCBcko1EjPqMzoc0GZ2wQpbM=; b=lkB6GmXcjF+g5htsjkUNHlxXvJr5ggujcljLl0w7TnZahRsgrJoLshbLWKcgd2yaLk iDdMaA4BVSZnSqYbN3eA6sftRkYTRVeKieWBufI2qj631XardP3hDfn9v3oOGt+riXKX NGkiu0yJEJsDMrwF7/VASCMS3ywAgNoz5QyNCaChKGCYe5UqhAQu3QzMDnjg9hp+L53V 4uRYPhQqHowb6ih6679We4DtskQUzjPVT7sNfi3uY2lGJOw1NmqLZlCkkYoxjxnuwEms vMR0XAVV9AM3BJvs8O2egWWAU22CFl0DwGhQ8Gs8GQ+7HEe0hu+yiH7/FIkRakFp50ob wYxw== X-Gm-Message-State: AOJu0Yxl9PyUODVCBNx4iVVc01u4VSQPM1tfXQEuWVrKxj+C4ngsmUKX YwDKx6DDCKk/YJKAAnhDa7WRN1cJgyyh1ZrFITXZEsn+o80sskWZby8CFjhkSamGEA== X-Gm-Gg: ASbGncuBbG3Ou/L8/vLQvCDMVyUDrZtZu0mSBkiu2pIIrfMRAXCtPBLpofu8lBr5b+J NrcsJ2su74E1dN6dQr7O1GMD89r9JeDKAlT+KxLuW35XMyt3ORhLqXUJvunJESI+pCWXhS9ph/U i+cCG0KRjSnx+WwMFKFf9KP7D4K7zJAmpRdhYG/zyosNKqj0bOPzKfDYObCsMnnRYXBSnULuDgq XucddIEewdg+I7LKIzqEROCQhGTdPhadI3KBHu5pBzgez8LZCrNRXvJdsNHi++9/FtezOltX0dU X-Google-Smtp-Source: AGHT+IH4UroL9TLEghz5+DxcxLZh6o0DshITcoANLbD1cbTnpb/W/I4XNhbFO6b4xObMW4xleBerCA== X-Received: by 2002:a17:902:c947:b0:20c:9821:6998 with SMTP id d9443c01a7336-219e6e858b4mr465264605ad.10.1735580832386; Mon, 30 Dec 2024 09:47:12 -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.47.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:11 -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 10/13] mm, swap: simplify percpu cluster updating Date: Tue, 31 Dec 2024 01:46:18 +0800 Message-ID: <20241230174621.61185-11-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 MIME-Version: 1.0 X-Rspamd-Queue-Id: E478F40006 X-Stat-Signature: iao859ru5aksg1qhdhqdr5te9xp6xkqn X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1735580785-493802 X-HE-Meta: U2FsdGVkX19uUFMDMQFC53BKO7d8cNOPnIBvEIeI7gFLDRd5robpCdi3z1Ufb1I0bR7uXyep8iJl4uDAGceiKEt39jYlog2jhnPJ/T7t917l58enIGxyIFEnAtP4Q2YSHKwaapn0qziNVFtTAmIzuGROJ5IkRVQYNikkFerZYXjcfIbcgyOwM+4UUwCSQQkWQ9tBU8QUd6JaFAI3fxI8ZT8jyzt6JCjiAU6Lk2DNSdHs2ao3W57TiyaTnFZAb+aEbE2VkW3gI/bhgT578099hUDRaJxdI4+q/43dj6HXJBcCKUi/Cx+W3vp7Dhn9IHBxjMSKCCFvk07p6yUUVk4tnSBdqjl80iEd5tpqRiqRIIUs1Kr8DrdD8ecksLfwtC6a885Lgp7FRzXmE90yUzEXMxTB5Jk+dPRSHh74/vgybIw6qS2Wl/KmLlB9/1oQyVhjSLXcy1n3aGcKGorGu/LdWLvAQUgGL1ApsdhzBxhrhlC4RQACz3haRHKD26ASEtcHtxAkGMjCjtD7tTlcJ5Mh9wHGPT9ncOYLhE+F6JeGnvoXAHhT3pfZejtHuhCvMJnOIWDtMkUOWnmtbb0h4zZhHvlDCyk1/sHoUc6XIJw6MmcJj9zvsnNuvTNANzvNDXzkWzAxLNi8ALmU09JESMVVMatCjUzSFkezRfMBW/6IJqa6dDhVXd2X0mdhoy1hH3clj22vlgby1I4pmdkVl0PlYqPyONt0JbBcsiots/uXiHpFV/SyIub/1kP8smULA5Q5sYFfSe4d2fJ5KXCC66OakzMQfgRM6OLtyckaSYUvcw4nifqIy9bjdOpSqgCQfMva+ZHCnEYi0XDAFPGKPgSFfsvQhy8OyclMuJKdX+or4coAY5k75TXHmTuKHdXkaER9JC9kYc+gkDhVBxoD+RbDTeccUgImAoY2S1ddYklxdkTMIEG30czzzzBhWFg1feyHNrrioRtc38zmjNkDbQp QV9vmDla gtPkeXqlSe7MAYt96ykibNDNKvd0aoCzWpkidaH8HG8KoEE5n9taTEliaf+8lM803skeHEDoQDw97u3XXzqPFHjai/wNK0tTv8z0deZzikTm9Un+BxiLnZCxjlaLJC9SmqP+IGumQ+sxJeW9jEHre5OVxBiCBOUqWKZiaRetFFPuaBQRuK/3jTTRLI8PrwTvQXOAbjypVOsh8aNKuRLQTG3TnzollZWoOfeuqfWlAyWEMxQa3v8golah4zxVuEaGeJw126/tSs+jEnAb+eAOqnt3rCjhtr1J9nCtq7L+TFQweBfctwwZxjvf0Nvi4+Ys21U4j0nQRIPA7jkFFMA4Zdbz5FIAZcAq6cxypdFo+4f9XQIxV3d+wwlgBpI/W8YBDU72aNCHEQwPMKoN5tMHSLozQvCpnV3vaB1Zh7WmXtUhlsHHueyz6d1b2N92k5drIKw02+CYbhkd2JIUUzjyHVCIWC77GKLn1FK0zoTCTKX2cqk3XZOqvgYEfD1KC7zDw1Cb9krNROzic9ejtBf31AZ00LA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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 Instead of using a returning argument, we can simply store the next cluster offset to the fixed percpu location, which reduce the stack usage and simplify the function: Object size: ./scripts/bloat-o-meter mm/swapfile.o mm/swapfile.o.new add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-271 (-271) Function old new delta get_swap_pages 2847 2733 -114 alloc_swap_scan_cluster 894 737 -157 Total: Before=30833, After=30562, chg -0.88% Stack usage: Before: swapfile.c:1190:5:get_swap_pages 240 static After: swapfile.c:1185:5:get_swap_pages 216 static Signed-off-by: Kairui Song --- include/linux/swap.h | 4 +-- mm/swapfile.c | 66 +++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index c4ff31cb6bde..4c1d2e69689f 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -275,9 +275,9 @@ enum swap_cluster_flags { * The first page in the swap file is the swap header, which is always marked * bad to prevent it from being allocated as an entry. This also prevents the * cluster to which it belongs being marked free. Therefore 0 is safe to use as - * a sentinel to indicate next is not valid in percpu_cluster. + * a sentinel to indicate an entry is not valid. */ -#define SWAP_NEXT_INVALID 0 +#define SWAP_ENTRY_INVALID 0 #ifdef CONFIG_THP_SWAP #define SWAP_NR_ORDERS (PMD_ORDER + 1) diff --git a/mm/swapfile.c b/mm/swapfile.c index dadd4fead689..60a650ba88fd 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -759,23 +759,23 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster return true; } -static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, unsigned long offset, - unsigned int *foundp, unsigned int order, +/* Try use a new cluster for current CPU and allocate from it. */ +static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, + struct swap_cluster_info *ci, + unsigned long offset, + unsigned int order, unsigned char usage) { - unsigned long start = offset & ~(SWAPFILE_CLUSTER - 1); + unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID; + unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER); unsigned long end = min(start + SWAPFILE_CLUSTER, si->max); unsigned int nr_pages = 1 << order; bool need_reclaim, ret; - struct swap_cluster_info *ci; - ci = &si->cluster_info[offset / SWAPFILE_CLUSTER]; lockdep_assert_held(&ci->lock); - if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER) { - offset = SWAP_NEXT_INVALID; + if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER) goto out; - } for (end -= nr_pages; offset <= end; offset += nr_pages) { need_reclaim = false; @@ -789,34 +789,27 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, unsigne * cluster has no flag set, and change of list * won't cause fragmentation. */ - if (!cluster_is_usable(ci, order)) { - offset = SWAP_NEXT_INVALID; + if (!cluster_is_usable(ci, order)) goto out; - } if (cluster_is_free(ci)) offset = start; /* Reclaim failed but cluster is usable, try next */ if (!ret) continue; } - if (!cluster_alloc_range(si, ci, offset, usage, order)) { - offset = SWAP_NEXT_INVALID; - goto out; - } - *foundp = offset; - if (ci->count == SWAPFILE_CLUSTER) { - offset = SWAP_NEXT_INVALID; - goto out; - } + if (!cluster_alloc_range(si, ci, offset, usage, order)) + break; + found = offset; offset += nr_pages; + if (ci->count < SWAPFILE_CLUSTER && offset <= end) + next = offset; break; } - if (offset > end) - offset = SWAP_NEXT_INVALID; out: relocate_cluster(si, ci); unlock_cluster(ci); - return offset; + __this_cpu_write(si->percpu_cluster->next[order], next); + return found; } /* Return true if reclaimed a whole cluster */ @@ -885,8 +878,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o if (cluster_is_usable(ci, order)) { if (cluster_is_free(ci)) offset = cluster_offset(si, ci); - offset = alloc_swap_scan_cluster(si, offset, &found, - order, usage); + found = alloc_swap_scan_cluster(si, ci, offset, + order, usage); } else { unlock_cluster(ci); } @@ -897,8 +890,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o new_cluster: ci = cluster_isolate_lock(si, &si->free_clusters); if (ci) { - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + order, usage); if (found) goto done; } @@ -911,8 +904,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o unsigned int frags = 0, frags_existing; while ((ci = cluster_isolate_lock(si, &si->nonfull_clusters[order]))) { - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + order, usage); /* * With `fragmenting` set to true, it will surely take * the cluster off nonfull list @@ -932,8 +925,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o * per-CPU usage, but they could contain newly released * reclaimable (eg. lazy-freed swap cache) slots. */ - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + order, usage); if (found) goto done; frags++; @@ -959,21 +952,20 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o */ while ((ci = cluster_isolate_lock(si, &si->frag_clusters[o]))) { atomic_long_dec(&si->frag_cluster_nr[o]); - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + 0, usage); if (found) goto done; } while ((ci = cluster_isolate_lock(si, &si->nonfull_clusters[o]))) { - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + 0, usage); if (found) goto done; } } done: - __this_cpu_write(si->percpu_cluster->next[order], offset); local_unlock(&si->percpu_cluster->lock); return found; @@ -3194,7 +3186,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, cluster = per_cpu_ptr(si->percpu_cluster, cpu); for (i = 0; i < SWAP_NR_ORDERS; i++) - cluster->next[i] = SWAP_NEXT_INVALID; + cluster->next[i] = SWAP_ENTRY_INVALID; local_lock_init(&cluster->lock); } From patchwork Mon Dec 30 17:46:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923389 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 945F1E77188 for ; Mon, 30 Dec 2024 17:47:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 26DB96B00A2; Mon, 30 Dec 2024 12:47:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21E556B00A3; Mon, 30 Dec 2024 12:47:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0225C6B00A4; Mon, 30 Dec 2024 12:47:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D21B16B00A2 for ; Mon, 30 Dec 2024 12:47:19 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 86211C085D for ; Mon, 30 Dec 2024 17:47:19 +0000 (UTC) X-FDA: 82952356578.29.5AACF2B Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf07.hostedemail.com (Postfix) with ESMTP id 05C6040005 for ; Mon, 30 Dec 2024 17:45:53 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nTaXpKoW; spf=pass (imf07.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.171 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=1735580782; 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=OsT4sLdVe9hCMPuRvzlfY9IxFp2eLdf3+7xkxYCm7K8=; b=6VsTfg4pjsohRKU16W9LcYYKuw7wH/Mi0hYcHDkHcF46Sfgh7onQfLG9TYiPhdb0jPx6ND R9w19vfaJO1WF4/r8atHirdpZ1pbVM9ekyXv9rWVOEE237vBV6XGzEY194RUFfjy/FwVfX tJ1XtqIXxNWcjJ7x1VOZmlFoYeDZewI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580782; a=rsa-sha256; cv=none; b=afrWirfdkZIridRexf/NKo91YZMYOq+DEFPO/RbO5mnCJzzvcoEaNiuNh37rMU0sVUol3O 7HehERWY/YA3JROsX5fJoxFdIanMXjjtKp0V9QR5R4+m37YV12ZYVX3EnWbQNmvYYJ/rxv wTeyrNVwdOA0EHTiak4P3fcIOWxAeLM= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nTaXpKoW; spf=pass (imf07.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-21680814d42so106494685ad.2 for ; Mon, 30 Dec 2024 09:47:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580836; x=1736185636; 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=OsT4sLdVe9hCMPuRvzlfY9IxFp2eLdf3+7xkxYCm7K8=; b=nTaXpKoWxS08Xl6+Pw+D2hUQ80XJkTvJdFky1/+UeAEwPJas63INeSHlqhFvlB2VJB s87h0v0hEWkN3Oh/XKlEV2ksdDI7CYp7UcMtpQJvuSsCng1XDgrSM6gRjX/3EQ8Ip3dK ZXarnP7ZpJN/JQTtHmDWmIxOI/MLt0VXFKKaUln+2TXdc4E5Zq3g+NQ5O6as7ymnJ8ts 67lEsdqj/ElkIwQ43/ypXv1aHaFFSa7Gu8YFsxwLvhw9YbF6AHNFvuAqi7Q1bGCZXehD FXrTMh1XArpnT3+YK8HJBQ+Mq4DLMwYtxBuVhYh8XWF3+TBUbma2gU8buDgh62gcumzX HpMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580836; x=1736185636; 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=OsT4sLdVe9hCMPuRvzlfY9IxFp2eLdf3+7xkxYCm7K8=; b=lm5COAQyr4/CbwWiMRQ+E+k6w6zH5mBSU7GiORISZEWVcGfnqs6xA7Ts/Kv1HuA65e OOwie31KwX743omLUjZoGWfv8xJKU3b2GZWcZRuIgH6Q+Laaq0fGvX+MqBrLYMj5Dlwy 13nu6l4HPHm7PS7ROZNHehU/XZT6JejqsWkXwsFPAVrJimWpRvac03oV+YyUqqmV4Ank swrHNCcvIGWMLQpclfjwBwr77eGJWJuHVkGQFy4ppt2efsb3AEjPbVSEum+0OBiHN6iH 5aqgs44rGUjaY+E8JMrfOzY0wyC9muQ4DS9xq9SovaEgo4lYllmfFNA/zza8O2FHC/VI DsxQ== X-Gm-Message-State: AOJu0YybGo1oKIK0LT7KhOvRAwttiOkzBqU0VTDwa/DpqQ2TDxB/M4Gi Y3cmphuoPW8xQw+eqxoViJmRZsLG0I1r72hnXiZQgkx79lz2qAuLDwU20p/h7cLZ4A== X-Gm-Gg: ASbGncu08FxKHT5WMcTO/u0HZ75OBuyD6GpZGMExpTPtJVOqWm72lcCdansKL2TWYd3 O/9NFdbbj6P1kZ8hp7iypmXbcUj5PyQPXvPJXW28NLZEyOfClV97EvMl54AwHAf5UuGimJlck26 0/yx0PGX5iljr8yN4TY00IvwHFNNuSiDQWmZhDjR9j+dKFVuOJLniKPQrdyMi2wOeLfONgtv102 TwMAir/tpQIGl/cz3xpIdVGzw8G57szcgzp2jnF41cAExOFBxAry/lzqTKns//19g+N+Szn+kr8 X-Google-Smtp-Source: AGHT+IGPvpYXhXtnW4rHEEOtWEli3yi6N/9jP3Hzf2YcEEXLkgTW0xIcXQNV1qxoFOuaB/N2UyZHvQ== X-Received: by 2002:a17:902:e80b:b0:215:7cd0:758d with SMTP id d9443c01a7336-219e70bf2femr561117935ad.36.1735580835940; Mon, 30 Dec 2024 09:47:15 -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.47.12 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:15 -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 11/13] mm, swap: introduce a helper for retrieving cluster from offset Date: Tue, 31 Dec 2024 01:46:19 +0800 Message-ID: <20241230174621.61185-12-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 MIME-Version: 1.0 X-Rspamd-Queue-Id: 05C6040005 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: czpy83m9x1ie116duf1bubyr8pji7j8p X-HE-Tag: 1735580753-717814 X-HE-Meta: U2FsdGVkX18/y4+7vHo4Krdo/yh0BaZ8lXazR+Lt9FEOJZN+LgdY2aaIeG93Pjm9mm35t6GPET7q4mTqPoeJB5Mlw8gx5EgquSw1EpiWWKpfL6iwB8WNpJs7bDW/ZxNoWWKuE64eWGBsRbwZKL9BQZsR8mm7KFotrG4MvaowSypb6M2pGTtv7+GGf4Y4vVmHdAWT1shETMDLi/61v9Oogql/gpUZnqb9cawMy2SHmHcqwj4chfdA3jVOZ2UqEP9Ppq8Sf7OA6ij7t+IrA+NFnvfV7vCSlA+ABEvrppCQuQjS4s4IcvAD8I9wmXltu3A44Cmi6mOiJCvFTdyTsyGucJZG5NZ/HKzOkptp0peMO4+P/AhZvFyzLfz0VFAYZ2zSTauNoxTOkQdyDbKCmxzhMWSwQ2YPTNqFJj1QSQAWzOkTBEKI57CNa2Wjt/p8KTZpCxW4SkK/9JAP6y1wioaEL/0uzyZ5ygHRMBsliYxU7ixVU7KXqRwPuCOPxuT3oW84N0BNQsZsNLtUg2Qy7xNJSdKHPBTFvEkKx09DWHBaF3L0AK6yn/EbQfcd0KPKpp5VRR9JWlK1boz5UCmFjqRRVplZC9EhLLhcQfBxZUKp7kTBL012HpDjZpSMiBmqlKoCB4L/68gcusvOImEzBu2WrIK6KrP6nithNvkin/7fHoFv/o7EMhMrm2tDWxWOdvIKEg3hIe14UbtdUrvLNY3yvtua1S/gfV5SHmWEPgxjCjJL+0JOuV+kIVuLJHDDtLWXH6KbM72r8QKti+S/v/7XbOs+wr6v52hZCGT7iyBCmMZsnB2jp2LoZGq6cwf53+7wM1yR0td5WJiKnee9NXVYRXHzA2DQAV2P1Es2vw4d97Pe6Ch/YdZY0ei2pO36+rNrBsA6Z+jv7cCujZZRuMSPMu+B3OQuvdFnOA9Hl9w+toPsrkoGz3/rtx0b4jzepbT8h6O3quccjjn2td/e14g DTeVZmGx tFoGwcdX9IT7QU+uGXTlz9aYvpjuQgOxYRzTzNf1fduLkjumOrFbwLB4pBda3CnmD7Cm9X7CHSOZJZUVJQXOmZCD2JWwACxlfYP0IEO6BJ9R/HdTBDInvaLttR0yhywOgGthZpVZSzGKCLSemmVlBQEZd0xXSffuTAhG5OmLWySoaIWZjnX7VNcLvBOjfSN52sZXhHr1NE2trLE/sxM4AMPT3uqPXmJmJa30bIPlTnhMjESN+OotKHIrfX2o12uzy/cemzgbr+LErVjhqPQSSPzQiosJk+WIA2b7VGlPKVIdWK2AIAbTf9w5seE/Qqb0UZTRW7zXby7tLl4rm9hTj7DA9jrYvt2BxultfV+osCZUGyb2roCpJ0GbCIPCjiTBdeVt1LuVb00i8LIPwSgUEzq4xV8CW0+GNuGqfWOH3KA+35Eh6P3aqyrcP/cY1VtOOhUr3pNwOuoicpZvKOSxHsPmoeqf16QjChvvUNqLKWjUY81Sq8yZedlJwiXiqtv6wGyHKt6cAzLVSoDfJwQ54MNM2JAKcVTWmda6bI1L6EeXAVAY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000004, 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 It's a common operation to retrieve the cluster info from offset, introduce a helper for this. Suggested-by: Chris Li Signed-off-by: Kairui Song --- mm/swapfile.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 60a650ba88fd..a3d1239d944b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -424,6 +424,12 @@ static inline unsigned int cluster_index(struct swap_info_struct *si, return ci - si->cluster_info; } +static inline struct swap_cluster_info *offset_to_cluster(struct swap_info_struct *si, + unsigned long offset) +{ + return &si->cluster_info[offset / SWAPFILE_CLUSTER]; +} + static inline unsigned int cluster_offset(struct swap_info_struct *si, struct swap_cluster_info *ci) { @@ -435,7 +441,7 @@ static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si { struct swap_cluster_info *ci; - ci = &si->cluster_info[offset / SWAPFILE_CLUSTER]; + ci = offset_to_cluster(si, offset); spin_lock(&ci->lock); return ci; @@ -1477,10 +1483,10 @@ static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry unsigned char *map_end = map + nr_pages; struct swap_cluster_info *ci; - /* It should never free entries across different clusters */ - VM_BUG_ON((offset / SWAPFILE_CLUSTER) != ((offset + nr_pages - 1) / SWAPFILE_CLUSTER)); - ci = lock_cluster(si, offset); + + /* It should never free entries across different clusters */ + VM_BUG_ON(ci != offset_to_cluster(si, offset + nr_pages - 1)); VM_BUG_ON(cluster_is_free(ci)); VM_BUG_ON(ci->count < nr_pages); From patchwork Mon Dec 30 17:46:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923390 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 0C07AE77188 for ; Mon, 30 Dec 2024 17:47:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7ED0A6B00A4; Mon, 30 Dec 2024 12:47:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 79BC76B00A5; Mon, 30 Dec 2024 12:47:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 617946B00A6; Mon, 30 Dec 2024 12:47:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3BAB96B00A4 for ; Mon, 30 Dec 2024 12:47:23 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 048911C937F for ; Mon, 30 Dec 2024 17:47:22 +0000 (UTC) X-FDA: 82952355780.22.507DBB2 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf15.hostedemail.com (Postfix) with ESMTP id 74EE2A0003 for ; Mon, 30 Dec 2024 17:45:57 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=icQSunYs; spf=pass (imf15.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=1735580792; 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=2T/sNFK+FLf9Dy0NmIMHzxBPbHbk3WZ2pHgL/qbky98=; b=X9wmLwDi/vXJVkt5HiFEmlONpXFlYNcp6GSHZCAnQXetq4guTtir3H6Jk6QF06aWavEO5m 0PzdD3fGzjd53gtWqnZp5jgdWBrpUu+GBieeQKOdM97SiK+tWTdJXScz6FRdzSc/vcWtb+ vvVO/J+jlGxX8etazji18WlQiPQ1ypg= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=icQSunYs; spf=pass (imf15.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580792; a=rsa-sha256; cv=none; b=e7kKg6p5csZBSL30Cf5Mynu5YYM/UtUOwUG7Tky5ADo1mgbxEVez3jsM0ktd+VhA3pTqsA bKybG7mLzwAEP7XUsVd7YXhTULrOxYgt5NQrVZ4EiJ7AmmyvRL5VSElxNqNGHnCqmbSbpV 5itDobbySXkjoENVgEneyV4+gxTWKl8= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-21644aca3a0so26835015ad.3 for ; Mon, 30 Dec 2024 09:47:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580839; x=1736185639; 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=2T/sNFK+FLf9Dy0NmIMHzxBPbHbk3WZ2pHgL/qbky98=; b=icQSunYscphA/aSg2jC6Q3cALeUOMaNQOY7lO/1rnMsNPv44ubfJd6qJ+70OJUp3DK hYs2VPGb6BECxKDWpDcrfeHqzp9H7S7pIjV/nyKSRBRu4DZqJ3yxWROA3GtLTKjFl9UU uqczYzXKr+ZnD6wXdCWlCe/VvJTtCgHXDL12If60VgOmvJqlnNjIaqM3ptx8NstpLKZJ tvEln27gGYM1tMvK2yy02p6Dt7oS2vqGzZYkhOxLUtyBQX7yt7mhny9QAm5LdghU+zlA IiBrbrIUiJeEY7rbZq1LeUKMAF/1sQl44PwN9ffyvikodoEhj25eECn0rl7+PygZmouc JrRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580839; x=1736185639; 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=2T/sNFK+FLf9Dy0NmIMHzxBPbHbk3WZ2pHgL/qbky98=; b=wkwNeuEYJFN0XMxlPHjHG/2keLlPLSnqOzChVrLokZ8jyqjWAk7dMsL6Ur0b/3ieAp d9ZgqXe8w2G04mhLB2+5v8r4WdTfLLJyVYKEs3cSaOVM7kGJk9fy7+tWyJiWVrHTocWC f1aFeEaRpg8pFf7O644ba1qImqY52Ld0x4lutdDIia3Yc0ww6qamnx9eFBCiFT6rsGqy H08JfkmU66tFFSCV5Ank09r3CxdMT+La+7KYtbCVn/JFaMuXn715WGL2fchmKTg2YGaR 9Im3+m+S5JHKobdKM+O8wWbXIWy6mVdPAzDLZX8boVlxGqF1zcZ09HG+OF77C7qn267k eKDg== X-Gm-Message-State: AOJu0YxyIaQkELGvtV4/qzN3NSDZrQhp2oQPDvmzBUL8hm3nmakmIN28 Grr4Cfkhtv8FLnObWwIYcgel3dqlPctcMX5aS8jeOtgS5w0g/comWjX1s9AQhXg72g== X-Gm-Gg: ASbGncvIKdtVrIp72T9qK4ABxIabiuOqxl1VEmHFqW3mj4f93cvnlg6n2sssyb6l6Ii 8QMiAHfKOzQNn3Mj+iuWaeO6obphJwMpQ9k8PP29qE9pIFwr/MCHxzCbxBFBZKmfD1kFK89CruP LIGKAozO74XnpLSrPYQkecLT0GVStZ0X6ju0gYdg0v5qBjXRxwyg07EHcVRuadjG4P7Ys+0YEbB EtGfrjcEBjomTG+tjRfxOFADOGE09JfdbvehmtcQzYnreTBOy62aVlDh+67aEMXhtPt1WmBXMU1 X-Google-Smtp-Source: AGHT+IG1L+YlopKpf4F4WRcFOfSGe7YaoMXyGz516XDuI+LioGKwgnFwv7jBKhNwXjyeA8fR5b+skg== X-Received: by 2002:a17:903:2444:b0:215:e98c:c5d9 with SMTP id d9443c01a7336-219e6e9fb2emr458467365ad.18.1735580839480; Mon, 30 Dec 2024 09:47:19 -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.47.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:19 -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 12/13] mm, swap: use a global swap cluster for non-rotation devices Date: Tue, 31 Dec 2024 01:46:20 +0800 Message-ID: <20241230174621.61185-13-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 MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 74EE2A0003 X-Rspam-User: X-Stat-Signature: inwgtwqmby8n1n9owe9tfdz3sdtxdmgo X-HE-Tag: 1735580757-327573 X-HE-Meta: U2FsdGVkX1+UVQ2O34UhDmsswGI1WpTwyLE/iPad2Pj7t1hKFPtttq1QZZi7z4EQ72KUNFtwTgVB28uFBiGrBocpYBquRWozhphw1ZQgtfc8lCp8+BYV+Ze6FX2HtKJZLQtQv8QFfTRsgB1+nVz954OXihTVAY2YyjLGaN25waRunMm/0hSblJW65ji6xHEQ7BF1NYPCP+uB+lPE7Ie9cXp8XOymTGMNc40pOZvAX0N08ogPc0FZkJUBC1aa1U+JN8A2TWJyE4FO14lPlSLo3on78vxCgZ7Y/d8LU6jU4ZkmbgCSszme1ZLhjlnnn/N7hwsfllVysyTbL2K/wbo8puuO4+kSZZCB4WsGIGVuKaY5QjevKS0hrG5wxalvPhPEWbaesHAbP3ylZfNQP3hTV2LgPg70KMOXATr56np7mwN7ic/0TD4iDi/IT9wu6UeKuJHICPBbBzX1e7cVb8ZTQ849xG4MW/mGkVXrukGp0L/eIAh6JIl9sU5Jxe2swIFVimtFefe2vpxxJH/AL7+ePe0UjIR3OrNXRp4gZLXVYiu96dBfHccKou6zUwfukGOU4kpBjDUrgUG/HZDcrufn3qIZdvxcnbySD+HJi1++LU2uv+Hl1SxTXJVaSuoPqEFrRQmd/vT1DDV76P9M2qLSEM9dLWif5eC5VnaLrcOBegthxSN+QHwZP/DOpxsuotnZZVO8RwWl4dNWTliGSbIWf7S6DpG7z6pcL2C4i36NI2kyif9+i7y0JfNKwgMlBShzc6hIApRmvDs7pRustVZ/V813Ubro/WjHFD1rFB43F+rhC3SHsG+g8drTiR/47MHMyY9XtIiMrdNffypEylQ6KtP1Oe1zkcw3c0nJW6Bi7wy+F2hJIqFo29EfQrVpJDtcZMjVMrtB549QEWTlRUAxmEHOZonJSFLrf/oMF8ocCX9XBGzU3QlKysg7zxhMKsngmZ+AIBjbCxX5vp0unXN j427180w zwirhGQ0d35VVBp8dG0Sl6cBAtp9pBjUclovHFw/eZxvNAL3du9YYPfjckKu/2UkL33zCQkR5uDvUniEAqbENKzlvTVIu9W38PSwSdKyzP1/EfTcavC8MU5ZuLwmkfP2S5igT3iIT8FBTfO1PmVul9YI3pk03JYmdOPg2cYOQOX2sGYSe2FybcBTcMNugYRhFZ/wng1pid76EkeYDPvsc+JPGn4z3APFPiqPebKPDLtSltxUkHJbYYcn863KWsbyb223MY/l0DXIKfDeWU/+B4aGJWGRPohPjd9Rgkmg66pGzBqnyQBI31K7tXgwDbVPGZdM3MDaIv1Oag/x8+H79CK0XrTZtepodZgBSXTf9VFAhdFuRuHg+3iAcwOOXDPlExRd+ct9NwEsWqHj1sTmTazfA4AaazyQGhxvdFVcwhAQOIGOquhtmsahS+kc6d9Lj04qFdrTfzszsX9qS6Grzg/XfCo5uGi0UsuVGHBs64oR1C1FGUNiCwiUQu+DttxLLW8rJTRMX6ol8ZaAl1nljNPuFZMp+idFySeDhuR8kaYiazdo= 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 Non-rotational devices (SSD / ZRAM) can tolerate fragmentation, so the goal of the SWAP allocator is to avoid contention for clusters. It uses a per-CPU cluster design, and each CPU will use a different cluster as much as possible. However, HDDs are very sensitive to fragmentation, contention is trivial in comparison. Therefore, we use one global cluster instead. This ensures that each order will be written to the same cluster as much as possible, which helps make the I/O more continuous. This ensures that the performance of the cluster allocator is as good as that of the old allocator. Tests after this commit compared to those before this series: Tested using 'make -j32' with tinyconfig, a 1G memcg limit, and HDD swap: make -j32 with tinyconfig, using 1G memcg limit and HDD swap: Before this series: 114.44user 29.11system 39:42.90elapsed 6%CPU (0avgtext+0avgdata 157284maxresident)k 2901232inputs+0outputs (238877major+4227640minor)pagefaults After this commit: 113.90user 23.81system 38:11.77elapsed 6%CPU (0avgtext+0avgdata 157260maxresident)k 2548728inputs+0outputs (235471major+4238110minor)pagefaults Suggested-by: Chris Li Signed-off-by: Kairui Song --- include/linux/swap.h | 2 ++ mm/swapfile.c | 51 ++++++++++++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 4c1d2e69689f..b13b72645db3 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -318,6 +318,8 @@ struct swap_info_struct { unsigned int pages; /* total of usable pages of swap */ atomic_long_t inuse_pages; /* number of those currently in use */ struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap location */ + struct percpu_cluster *global_cluster; /* Use one global cluster for rotating device */ + spinlock_t global_cluster_lock; /* Serialize usage of global cluster */ struct rb_root swap_extent_root;/* root of the swap extent rbtree */ struct block_device *bdev; /* swap device or bdev of swap file */ struct file *swap_file; /* seldom referenced */ diff --git a/mm/swapfile.c b/mm/swapfile.c index a3d1239d944b..e57e5453a25b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -814,7 +814,10 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, out: relocate_cluster(si, ci); unlock_cluster(ci); - __this_cpu_write(si->percpu_cluster->next[order], next); + if (si->flags & SWP_SOLIDSTATE) + __this_cpu_write(si->percpu_cluster->next[order], next); + else + si->global_cluster->next[order] = next; return found; } @@ -875,9 +878,16 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o struct swap_cluster_info *ci; unsigned int offset, found = 0; - /* Fast path using per CPU cluster */ - local_lock(&si->percpu_cluster->lock); - offset = __this_cpu_read(si->percpu_cluster->next[order]); + if (si->flags & SWP_SOLIDSTATE) { + /* Fast path using per CPU cluster */ + local_lock(&si->percpu_cluster->lock); + offset = __this_cpu_read(si->percpu_cluster->next[order]); + } else { + /* Serialize HDD SWAP allocation for each device. */ + spin_lock(&si->global_cluster_lock); + offset = si->global_cluster->next[order]; + } + if (offset) { ci = lock_cluster(si, offset); /* Cluster could have been used by another order */ @@ -972,8 +982,10 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o } } done: - local_unlock(&si->percpu_cluster->lock); - + if (si->flags & SWP_SOLIDSTATE) + local_unlock(&si->percpu_cluster->lock); + else + spin_unlock(&si->global_cluster_lock); return found; } @@ -2778,6 +2790,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) mutex_unlock(&swapon_mutex); free_percpu(p->percpu_cluster); p->percpu_cluster = NULL; + kfree(p->global_cluster); + p->global_cluster = NULL; vfree(swap_map); kvfree(zeromap); kvfree(cluster_info); @@ -3183,17 +3197,24 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, for (i = 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); - si->percpu_cluster = alloc_percpu(struct percpu_cluster); - if (!si->percpu_cluster) - goto err_free; + if (si->flags & SWP_SOLIDSTATE) { + si->percpu_cluster = alloc_percpu(struct percpu_cluster); + if (!si->percpu_cluster) + goto err_free; - for_each_possible_cpu(cpu) { - struct percpu_cluster *cluster; + for_each_possible_cpu(cpu) { + struct percpu_cluster *cluster; - cluster = per_cpu_ptr(si->percpu_cluster, cpu); + cluster = per_cpu_ptr(si->percpu_cluster, cpu); + for (i = 0; i < SWAP_NR_ORDERS; i++) + cluster->next[i] = SWAP_ENTRY_INVALID; + local_lock_init(&cluster->lock); + } + } else { + si->global_cluster = kmalloc(sizeof(*si->global_cluster), GFP_KERNEL); for (i = 0; i < SWAP_NR_ORDERS; i++) - cluster->next[i] = SWAP_ENTRY_INVALID; - local_lock_init(&cluster->lock); + si->global_cluster->next[i] = SWAP_ENTRY_INVALID; + spin_lock_init(&si->global_cluster_lock); } /* @@ -3467,6 +3488,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) bad_swap: free_percpu(si->percpu_cluster); si->percpu_cluster = NULL; + kfree(si->global_cluster); + si->global_cluster = NULL; inode = NULL; destroy_swap_extents(si); swap_cgroup_swapoff(si->type); From patchwork Mon Dec 30 17:46:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13923391 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 D2D3EE77188 for ; Mon, 30 Dec 2024 17:47:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 618B76B00A6; Mon, 30 Dec 2024 12:47:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C7246B00A7; Mon, 30 Dec 2024 12:47:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41A256B00A8; Mon, 30 Dec 2024 12:47:27 -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 1A7106B00A6 for ; Mon, 30 Dec 2024 12:47:27 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CB1ED81951 for ; Mon, 30 Dec 2024 17:47:26 +0000 (UTC) X-FDA: 82952354982.03.88BC7A2 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf11.hostedemail.com (Postfix) with ESMTP id C0FCA40017 for ; Mon, 30 Dec 2024 17:46:41 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Wj0Z7nkA; spf=pass (imf11.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.173 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=1735580823; 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=ixG1z0DI2xzvsIxDfP6EypWP22FNN2ivx8hCEgTlMv0=; b=l8xm/aF8RNkkG2VmGlzlasZO8gbVHDP/PZsb2pOiebCE2S9NVA2tmtqHkFq5SeawD9qyXV AJBmOJ/KegtVbr8qltsR3e2IijVmuQ+WNQ5aKerhNjBLEis2yzYEPcatGCLRH5eQB6KgB8 CiAW5I9/S2AgB4MdWgXmvgt3IXM4c+Q= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Wj0Z7nkA; spf=pass (imf11.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580823; a=rsa-sha256; cv=none; b=sVIsIU2zltUCGhKnxN4aIEOm9AVLC7jrcRKACcpnJYCdm6np9R7OwRhAYQQD+zd+Si5BQw UbB50u2WPdS5Nbv3cC41vEReMGBMksWjAmzXTx/5qBJWxdci1m4wbFxawRUovGeDSZrJFc en5urMZgUvm1gjX4sMos8QPPypK3cvg= Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2163b0c09afso120034025ad.0 for ; Mon, 30 Dec 2024 09:47:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580843; x=1736185643; 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=ixG1z0DI2xzvsIxDfP6EypWP22FNN2ivx8hCEgTlMv0=; b=Wj0Z7nkAPE02KdJTacGGgPy2r/UajkXfLfCewLO7iGZl3T9u1P3w9WCIQK6kPW/HXX G9LpOFSSPlqrS/O7S1qm8rWpuflDW3AX0sYo1TdcBOofGGWCRqKQyfssFAidEyJdB36O 5H3WwbSnneAu1tvL4zIzRCAb5ALBF0iVewzl4OmRj7aHJFwhIOp357rvtrihLoOSB4nF PIw1X3qHHl3NqleKIkdQ7pn4G9gq332yEDcbLABebVqcnJsukn8pHRS9n0Vzv2wA5V4u RqUBPCbHbVSJ1ZzbJRSqyuqlsv2wPsAmpG3gJc5kFaIAgA5bIhduDMt6wiNyIC/MMopb viCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580843; x=1736185643; 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=ixG1z0DI2xzvsIxDfP6EypWP22FNN2ivx8hCEgTlMv0=; b=bwTytB1904EBYZMWRRSSR7ZG20eXMgoSHpc5uPL6o+A8LdPbbHYN39lC18RG/PbLuR qgH/tWHFle28UY8hVJknSvOjbc0PsA46n/4dg+5r4TztjPA+EqEMyo4KsgNYXD8Tg6p9 V8FEr0IwzurEOZ9SYpxi8rFPPjYrJVDRBzPxmeFD+t2vvSo7+9raMJJyj6wvDXnkYQqp QZlekUIkJn8GpyWNpgnIYYmte5kzmNo/AUlFRILtEVCEi5Yrd93MuoubgAZE+Y3K9Epr DDVEwYQ10Stt5xa33w1N8cJSgbWva6JHFS+LicqaEtk3+Mabf6FP2M4GysiL9icR+RtL Kpkg== X-Gm-Message-State: AOJu0YwzIpbJqAKfQcpGTlpPPBWNa1zGXAGkghsmTaUhoOnEL4t5U2oi QTwpEq94V02Dv6//C2D4RZIByi+u1kGdxJnFTpcqppI8+685EnLNpmgO/YT97GpK2g== X-Gm-Gg: ASbGncuMVvb2JAHYQMVsKP8/0wW/4fVgpgsR42bSQLWK/QuMoNJK7IyEDwK3bjt59GO o26vTpFGDMTKvxXm47xWI3tXBclVV0Zw6npX6gWNDAPc0Q/puwJ0uwpArBE7OEsyU9ek3GMMt0M uVI0vcVb3wjN6/h59yf7dnLOHdQTES3IQZmA7dSyIg5Di7uvxE+XaVnVMAXQ/Af3xZJBi89HE64 wBp4mstgbs9ZFx50Odp+HO5CBwzILiaoCBTrmfU/+/WWN7Rruie+Elxi8o9uPLAdwSzWCFECho+ X-Google-Smtp-Source: AGHT+IFTPLqAurs/FG+CnxzXJMSklhchhlzwmJ/iejwXl+pptqXzgKFWQYwbBdGHMm6+KyVR0VVzug== X-Received: by 2002:a17:903:320a:b0:216:539d:37c3 with SMTP id d9443c01a7336-219e6e9e8d0mr524521965ad.24.1735580843135; Mon, 30 Dec 2024 09:47:23 -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.47.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:22 -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 13/13] mm, swap_slots: remove slot cache for freeing path Date: Tue, 31 Dec 2024 01:46:21 +0800 Message-ID: <20241230174621.61185-14-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 MIME-Version: 1.0 X-Rspamd-Queue-Id: C0FCA40017 X-Stat-Signature: wc44qc49uzakz5zibnuiisw5uyqwftz6 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1735580801-98089 X-HE-Meta: U2FsdGVkX19hQU0XGa25bn8tHjQYdn98EdUJlRr/wisM58V7bmM0eVHKWQAIFbkwui42nUP/wjEj33CopvmZQ6i0en6R41wY7oE4arZZtnivoyUxjOwrVej8xY3xQBrtGLr53YDD+yYFv1FFm4SlWrL7qDAnAt8yNtWT1tWX/0UJAwiNxDIGhHo54ad7dbWWMY7cvYo6NWoOeGzjEUufcg8InxY6cqMibiqMrGUpRG07/bwNawbK24679Rc0a7FsQ5nNGZmDqunVwZqaadskJVFfFulghN8cXsPVUAHVXi8vGtM+17YUbv2ggqauLfd2o58bJmZ+Ff2Wj+24RsK0mBQwu+2/RQNNNwJDGtjwgvjRri+09d2LIoaZzv0gSyknW8dvnPTSyETMmKBef/RwoAmbEYrc7dIxwoM3nqbkN94vnxaQA4+3DU1pneJok36ye55okGo/a0bg0mzSa1LrL+XvNsexR6s2hp4WDtCcZ9akbwxH8JSihNMsbmTXuX/izOoGM3rgrQSbxYvioJDNTMiQigzQYXZX+ti1lEvT/T6T93VWerp0E+CUwYyomBTVGE9Hg4gmq9q2faaaq+rd+F7zLSH0pV0ju+xpL4xpLXmVajaD0dqoiHOv8DahAw4vrB5SSJ4x545GDx7UBTOWMiXebcbqt+FmPnchok5lPLU2q7i2KNGyoVRGEBiTulVRoKcaO3Gz5GGOH/QE2/zw7022cAlqWLZIyZx3YVj66Hc/XWHVi8ijcUmCsI9Mvbbi0rGirjMFjrTEdbegTKNyqE2f/Ud1Y75AKzFYtoZyCnRyJTjP1JyrOWHvhRyQyuIr8cbGK5q9Bk/s0FJHaeAJMhx1bAyg9j5OgHhjXJImJvfLn5AYt0OKa2CIV8YLuipkhxcZlrbb55kXtKvRG5YH7yO9uP4JjnWdOnV5jeio2oRdMb7jRsJv+Fm1+dOGeZ60MP99lRqzLknRYnUea68 lYyYfvxC XvgCjEJmnDFzZHO0tA92wqZdqH27x+xjQBuwqyCdyqFVkEJKEPyL7sqvct819zbw6RnZsjPrVcl92xMcRwnGO1aIk80DJVi7IFGzNgw4y5sEfTba3gOE+NUVJ1t36NH7e1oPNMYv6b1rx5L/jaSw+XoFRczW1fwZZ11nUZa3ss3ui4My/b3+1kZDbSAysesKOQJnLdp9DJeXXPKKagTaRpj00H9suJBwwvYkNWHNjY8Hz9ZPnQV2oY2EGWlh0HegoOU8miAI9oYOxNl87u6zJ1SpbFZtDYMptYSOBhIpsObC4inRbmYu4SBIvbE64f8IMnxp1KiXv093f7mI/9b9AIQ9WDmeKFNNeGAQ2jfKoreOWZM8ZGSfixV2YYJDFMVTfLlUrmMmU6OZTQJwAyAQRbpaIKs3zAW3MB3MP5Xx6wBon1XsZ9IksX3MT1ogJOk0d3nNY2F3kxZXfuYf1tX2GDp+BkBNobiNsfuwGKmHe0oHt5kC5zF/cvfzDl+jHgVNm+5BV55IpXgLhP56lV/70f0vozp9+dD3QkDdsRGNOJ4IQmjLa1MnVsCFD811yE4FKhBOyAOA84Zqio+rJRdCNJnRxYIo9jUEVf9B8OnP0FHth09v44UHI9Yq8RA== 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 slot cache for freeing path is mostly for reducing the overhead of si->lock. As we have basically eliminated the si->lock usage for freeing path, it can be removed. This helps simplify the code, and avoids swap entries from being hold in cache upon freeing. The delayed freeing of entries have been causing trouble for further optimizations for zswap [1] and in theory will also cause more fragmentation, and extra overhead. Test with build linux kernel showed both performance and fragmentation is better without the cache: tiem make -j96 / 768M memcg, 4K pages, 10G ZRAM, avg of 4 test run:: Before: Sys time: 36047.78, Real time: 472.43 After: (-7.6% sys time, -7.3% real time) Sys time: 33314.76, Real time: 437.67 time make -j96 / 1152M memcg, 64K mTHP, 10G ZRAM, avg of 4 test run: Before: Sys time: 46859.04, Real time: 562.63 hugepages-64kB/stats/swpout: 1783392 hugepages-64kB/stats/swpout_fallback: 240875 After: (-23.3% sys time, -21.3% real time) Sys time: 35958.87, Real time: 442.69 hugepages-64kB/stats/swpout: 1866267 hugepages-64kB/stats/swpout_fallback: 158330 Sequential SWAP should be also slightly faster, tests didn't show a measurable difference though, at least no regression: Swapin 4G zero page on ZRAM (time in us): Before (avg. 1923756) 1912391 1927023 1927957 1916527 1918263 1914284 1934753 1940813 1921791 After (avg. 1922290): 1919101 1925743 1916810 1917007 1923930 1935152 1917403 1923549 1921913 Link: https://lore.kernel.org/all/CAMgjq7ACohT_uerSz8E_994ZZCv709Zor+43hdmesW_59W1BWw@mail.gmail.com/[1] Suggested-by: Chris Li Signed-off-by: Kairui Song --- include/linux/swap_slots.h | 3 -- mm/swap_slots.c | 78 +++++---------------------------- mm/swapfile.c | 89 +++++++++++++++----------------------- 3 files changed, 44 insertions(+), 126 deletions(-) diff --git a/include/linux/swap_slots.h b/include/linux/swap_slots.h index 15adfb8c813a..840aec3523b2 100644 --- a/include/linux/swap_slots.h +++ b/include/linux/swap_slots.h @@ -16,15 +16,12 @@ struct swap_slots_cache { swp_entry_t *slots; int nr; int cur; - spinlock_t free_lock; /* protects slots_ret, n_ret */ - swp_entry_t *slots_ret; int n_ret; }; void disable_swap_slots_cache_lock(void); void reenable_swap_slots_cache_unlock(void); void enable_swap_slots_cache(void); -void free_swap_slot(swp_entry_t entry); extern bool swap_slot_cache_enabled; diff --git a/mm/swap_slots.c b/mm/swap_slots.c index 13ab3b771409..9c7c171df7ba 100644 --- a/mm/swap_slots.c +++ b/mm/swap_slots.c @@ -43,17 +43,15 @@ static DEFINE_MUTEX(swap_slots_cache_mutex); /* Serialize swap slots cache enable/disable operations */ static DEFINE_MUTEX(swap_slots_cache_enable_mutex); -static void __drain_swap_slots_cache(unsigned int type); +static void __drain_swap_slots_cache(void); #define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled) -#define SLOTS_CACHE 0x1 -#define SLOTS_CACHE_RET 0x2 static void deactivate_swap_slots_cache(void) { mutex_lock(&swap_slots_cache_mutex); swap_slot_cache_active = false; - __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET); + __drain_swap_slots_cache(); mutex_unlock(&swap_slots_cache_mutex); } @@ -72,7 +70,7 @@ void disable_swap_slots_cache_lock(void) if (swap_slot_cache_initialized) { /* serialize with cpu hotplug operations */ cpus_read_lock(); - __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET); + __drain_swap_slots_cache(); cpus_read_unlock(); } } @@ -113,7 +111,7 @@ static bool check_cache_active(void) static int alloc_swap_slot_cache(unsigned int cpu) { struct swap_slots_cache *cache; - swp_entry_t *slots, *slots_ret; + swp_entry_t *slots; /* * Do allocation outside swap_slots_cache_mutex @@ -125,28 +123,19 @@ static int alloc_swap_slot_cache(unsigned int cpu) if (!slots) return -ENOMEM; - slots_ret = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t), - GFP_KERNEL); - if (!slots_ret) { - kvfree(slots); - return -ENOMEM; - } - mutex_lock(&swap_slots_cache_mutex); cache = &per_cpu(swp_slots, cpu); - if (cache->slots || cache->slots_ret) { + if (cache->slots) { /* cache already allocated */ mutex_unlock(&swap_slots_cache_mutex); kvfree(slots); - kvfree(slots_ret); return 0; } if (!cache->lock_initialized) { mutex_init(&cache->alloc_lock); - spin_lock_init(&cache->free_lock); cache->lock_initialized = true; } cache->nr = 0; @@ -160,19 +149,16 @@ static int alloc_swap_slot_cache(unsigned int cpu) */ mb(); cache->slots = slots; - cache->slots_ret = slots_ret; mutex_unlock(&swap_slots_cache_mutex); return 0; } -static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, - bool free_slots) +static void drain_slots_cache_cpu(unsigned int cpu, bool free_slots) { struct swap_slots_cache *cache; - swp_entry_t *slots = NULL; cache = &per_cpu(swp_slots, cpu); - if ((type & SLOTS_CACHE) && cache->slots) { + if (cache->slots) { mutex_lock(&cache->alloc_lock); swapcache_free_entries(cache->slots + cache->cur, cache->nr); cache->cur = 0; @@ -183,20 +169,9 @@ static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, } mutex_unlock(&cache->alloc_lock); } - if ((type & SLOTS_CACHE_RET) && cache->slots_ret) { - spin_lock_irq(&cache->free_lock); - swapcache_free_entries(cache->slots_ret, cache->n_ret); - cache->n_ret = 0; - if (free_slots && cache->slots_ret) { - slots = cache->slots_ret; - cache->slots_ret = NULL; - } - spin_unlock_irq(&cache->free_lock); - kvfree(slots); - } } -static void __drain_swap_slots_cache(unsigned int type) +static void __drain_swap_slots_cache(void) { unsigned int cpu; @@ -224,13 +199,13 @@ static void __drain_swap_slots_cache(unsigned int type) * There are no slots on such cpu that need to be drained. */ for_each_online_cpu(cpu) - drain_slots_cache_cpu(cpu, type, false); + drain_slots_cache_cpu(cpu, false); } static int free_slot_cache(unsigned int cpu) { mutex_lock(&swap_slots_cache_mutex); - drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true); + drain_slots_cache_cpu(cpu, true); mutex_unlock(&swap_slots_cache_mutex); return 0; } @@ -269,39 +244,6 @@ static int refill_swap_slots_cache(struct swap_slots_cache *cache) return cache->nr; } -void free_swap_slot(swp_entry_t entry) -{ - struct swap_slots_cache *cache; - - /* Large folio swap slot is not covered. */ - zswap_invalidate(entry); - - cache = raw_cpu_ptr(&swp_slots); - if (likely(use_swap_slot_cache && cache->slots_ret)) { - spin_lock_irq(&cache->free_lock); - /* Swap slots cache may be deactivated before acquiring lock */ - if (!use_swap_slot_cache || !cache->slots_ret) { - spin_unlock_irq(&cache->free_lock); - goto direct_free; - } - if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) { - /* - * Return slots to global pool. - * The current swap_map value is SWAP_HAS_CACHE. - * Set it to 0 to indicate it is available for - * allocation in global pool - */ - swapcache_free_entries(cache->slots_ret, cache->n_ret); - cache->n_ret = 0; - } - cache->slots_ret[cache->n_ret++] = entry; - spin_unlock_irq(&cache->free_lock); - } else { -direct_free: - swapcache_free_entries(&entry, 1); - } -} - swp_entry_t folio_alloc_swap(struct folio *folio) { swp_entry_t entry; diff --git a/mm/swapfile.c b/mm/swapfile.c index e57e5453a25b..d623f5b6dc4c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -53,14 +53,15 @@ static bool swap_count_continued(struct swap_info_struct *, pgoff_t, unsigned char); static void free_swap_count_continuations(struct swap_info_struct *); -static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry, - unsigned int nr_pages); +static void swap_entry_range_free(struct swap_info_struct *si, + struct swap_cluster_info *ci, + swp_entry_t entry, unsigned int nr_pages); static void swap_range_alloc(struct swap_info_struct *si, unsigned int nr_entries); static bool folio_swapcache_freeable(struct folio *folio); static struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, unsigned long offset); -static void unlock_cluster(struct swap_cluster_info *ci); +static inline void unlock_cluster(struct swap_cluster_info *ci); static DEFINE_SPINLOCK(swap_lock); static unsigned int nr_swapfiles; @@ -261,10 +262,9 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, folio_ref_sub(folio, nr_pages); folio_set_dirty(folio); - /* Only sinple page folio can be backed by zswap */ - if (nr_pages == 1) - zswap_invalidate(entry); - swap_entry_range_free(si, entry, nr_pages); + ci = lock_cluster(si, offset); + swap_entry_range_free(si, ci, entry, nr_pages); + unlock_cluster(ci); ret = nr_pages; out_unlock: folio_unlock(folio); @@ -1125,8 +1125,10 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, * Use atomic clear_bit operations only on zeromap instead of non-atomic * bitmap_clear to prevent adjacent bits corruption due to simultaneous writes. */ - for (i = 0; i < nr_entries; i++) + for (i = 0; i < nr_entries; i++) { clear_bit(offset + i, si->zeromap); + zswap_invalidate(swp_entry(si->type, offset + i)); + } if (si->flags & SWP_BLKDEV) swap_slot_free_notify = @@ -1431,9 +1433,9 @@ static unsigned char __swap_entry_free(struct swap_info_struct *si, ci = lock_cluster(si, offset); usage = __swap_entry_free_locked(si, offset, 1); - unlock_cluster(ci); if (!usage) - free_swap_slot(entry); + swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); + unlock_cluster(ci); return usage; } @@ -1461,13 +1463,10 @@ static bool __swap_entries_free(struct swap_info_struct *si, } for (i = 0; i < nr; i++) WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE); + if (!has_cache) + swap_entry_range_free(si, ci, entry, nr); unlock_cluster(ci); - if (!has_cache) { - for (i = 0; i < nr; i++) - zswap_invalidate(swp_entry(si->type, offset + i)); - swap_entry_range_free(si, entry, nr); - } return has_cache; fallback: @@ -1487,15 +1486,13 @@ static bool __swap_entries_free(struct swap_info_struct *si, * Drop the last HAS_CACHE flag of swap entries, caller have to * ensure all entries belong to the same cgroup. */ -static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry, - unsigned int nr_pages) +static void swap_entry_range_free(struct swap_info_struct *si, + struct swap_cluster_info *ci, + swp_entry_t entry, unsigned int nr_pages) { unsigned long offset = swp_offset(entry); unsigned char *map = si->swap_map + offset; unsigned char *map_end = map + nr_pages; - struct swap_cluster_info *ci; - - ci = lock_cluster(si, offset); /* It should never free entries across different clusters */ VM_BUG_ON(ci != offset_to_cluster(si, offset + nr_pages - 1)); @@ -1515,7 +1512,6 @@ static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry free_cluster(si, ci); else partial_free_cluster(si, ci); - unlock_cluster(ci); } static void cluster_swap_free_nr(struct swap_info_struct *si, @@ -1523,28 +1519,13 @@ static void cluster_swap_free_nr(struct swap_info_struct *si, unsigned char usage) { struct swap_cluster_info *ci; - DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 }; - int i, nr; + unsigned long end = offset + nr_pages; ci = lock_cluster(si, offset); - while (nr_pages) { - nr = min(BITS_PER_LONG, nr_pages); - for (i = 0; i < nr; i++) { - if (!__swap_entry_free_locked(si, offset + i, usage)) - bitmap_set(to_free, i, 1); - } - if (!bitmap_empty(to_free, BITS_PER_LONG)) { - unlock_cluster(ci); - for_each_set_bit(i, to_free, BITS_PER_LONG) - free_swap_slot(swp_entry(si->type, offset + i)); - if (nr == nr_pages) - return; - bitmap_clear(to_free, 0, BITS_PER_LONG); - ci = lock_cluster(si, offset); - } - offset += nr; - nr_pages -= nr; - } + do { + if (!__swap_entry_free_locked(si, offset, usage)) + swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); + } while (++offset < end); unlock_cluster(ci); } @@ -1585,18 +1566,12 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) return; ci = lock_cluster(si, offset); - if (size > 1 && swap_is_has_cache(si, offset, size)) { - unlock_cluster(ci); - swap_entry_range_free(si, entry, size); - return; - } - for (int i = 0; i < size; i++, entry.val++) { - if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) { - unlock_cluster(ci); - free_swap_slot(entry); - if (i == size - 1) - return; - lock_cluster(si, offset); + if (swap_is_has_cache(si, offset, size)) + swap_entry_range_free(si, ci, entry, size); + else { + for (int i = 0; i < size; i++, entry.val++) { + if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) + swap_entry_range_free(si, ci, entry, 1); } } unlock_cluster(ci); @@ -1605,6 +1580,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) void swapcache_free_entries(swp_entry_t *entries, int n) { int i; + struct swap_cluster_info *ci; struct swap_info_struct *si = NULL; if (n <= 0) @@ -1612,8 +1588,11 @@ void swapcache_free_entries(swp_entry_t *entries, int n) for (i = 0; i < n; ++i) { si = _swap_info_get(entries[i]); - if (si) - swap_entry_range_free(si, entries[i], 1); + if (si) { + ci = lock_cluster(si, swp_offset(entries[i])); + swap_entry_range_free(si, ci, entries[i], 1); + unlock_cluster(ci); + } } }