From patchwork Mon Dec 2 18:41:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13891177 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 2B53BE69E81 for ; Mon, 2 Dec 2024 18:42:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AAE5F6B0099; Mon, 2 Dec 2024 13:42:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A5E216B009A; Mon, 2 Dec 2024 13:42:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D8316B009B; Mon, 2 Dec 2024 13:42:28 -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 6E1F46B0099 for ; Mon, 2 Dec 2024 13:42:28 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2706716026C for ; Mon, 2 Dec 2024 18:42:28 +0000 (UTC) X-FDA: 82850889282.05.86B79D7 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf12.hostedemail.com (Postfix) with ESMTP id DA0ED40010 for ; Mon, 2 Dec 2024 18:42:20 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NWo4LASc; spf=pass (imf12.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.210.176 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=1733164937; 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=n486kN9w2/p4kQRIxzmOz9BLiT1ewnt6ZG6JQNAAsl4=; b=T6Fvv5IFrJ4xL8Znapbr0p6eoCLfl5x4+KkmEo3bvgPWUjMyzrruk0O6vZBYA6sq/7L546 P2Kv9CQYnXDgeFkUDFbXUy0YphMoJovBw4E09cUFketJIxwoMVAPvzlmpywwFqA8x1rbLa CjreHdd4LEwC/LeH+JzH0BOLMvoBlj8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NWo4LASc; spf=pass (imf12.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.210.176 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=1733164937; a=rsa-sha256; cv=none; b=8AmTGj/WxED6AxbVildiN6bciC4knDM7hWz1cfw+8ZRUQhgncNF4E/eOCq3/RHKKQpKwyE 3mE6qytV4b1OGIEetY5ainhzrE1wjBOE6hXHeZk+SpYx1Ftt+oqXH9xz98vGM0c0mNMOzb TFnaOpEjs7VTcSCxkZ7r8e01MNzhdQo= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-724f41d550cso2744172b3a.2 for ; Mon, 02 Dec 2024 10:42:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733164944; x=1733769744; 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=n486kN9w2/p4kQRIxzmOz9BLiT1ewnt6ZG6JQNAAsl4=; b=NWo4LAScWyOVlAwR0vHxtv/2Ea/mR0hTZnmYYhBS8gbYaSuxZ33oCNMXhfY26K3Jzr DBtaXZw7Ma21SGHj3nY0QIhTVEiUex1h55+M6SjQLAQFlVEukls+ULUCaybJbEwsQgBr VuL0889cphZzOXdnXp9yWAouQig+Pjf2zMB01levzWaH45cNjFw3bb8oUVMjXi2l2kDK 3ssYB8thc5pyVN/TNKiYfhB2cjTEuF6HqzeB0QwzbiFmcdlxOioGsWdXENB+wcsFrT9/ YYSjKYQTFilWZDYjTAtgS2dq3DYLofmgF647G2DTWbfnH7l29QxCcfBXb18NCa0mLsil 2Nyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733164944; x=1733769744; 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=n486kN9w2/p4kQRIxzmOz9BLiT1ewnt6ZG6JQNAAsl4=; b=hijnroH47FA6gyAnrzglKvt0OzdtiARO+KE/nSmIdiEGKZAkShSmN7zprSwlok+3O0 h1IEojqi5JFMtXudBkiu9wHAdrkBqi9C1efO7bbq2492nxInnX8kDsd3GRMaPjHoxPgU 4PEXdzhst/bbVaERf1106oAuIx2EXGH8SIS8Le9Vi9IPZSQ9XMHIAW16PdCgn2Fn8eLJ O3fXB+okKWA3yhDuEdCrDZkGD55/yFQHfLwpi3otyZ1spdksYDHBlJ3YjpMHE7NTgX7f z7QsPwznlspLiakiXvRcgVZowSLRLtNpPDVrS/YfCbh5HAz9HgcOY8+sTISc5qytr3Yf uFeA== X-Gm-Message-State: AOJu0Yy3KOAd5n8NDKkuJjCFxI3aBWm847vnkYEgORoe16kUTOacQ7LV eor2KMmAhNYtERDDgFjUzC+4v49SsS5zhXqTmFnmXDJpjCvZnmnyn3QDOI1Jluv1Fw== X-Gm-Gg: ASbGnctDWO/q0fiGCl9VPwWUOabEMZbYok0u/TuD8zGL4Nw6AO8aYnscQLC+yqUjOtJ aXrbUe7NRKWJN1j1jF0rr7X1IZqOs2LvJtw5C0tIkL6TnMHadcURdnkz9jQvzgkNzQsBu37jRcn rwJbMTVGGY2E3yAoiCaUwrDo02avb5QEopiD7f1BrN1zFMxGbv3okDcUkrrhgJRCfE76dFEaoeY 1FRRwVmaYuhVX8L7OH2/r6WJGaSg70drZEqSiOyhcCAUys6It9n3KzMRqUpUB9R59Tj0zY= X-Google-Smtp-Source: AGHT+IE6F7f6lJrUVeUD4BRVPb3PuBX80oqzE3+WCYBtL1H232mPA2ctT8z6mSlYDWpdAGJGBit8ww== X-Received: by 2002:a05:6a00:4f81:b0:71d:eb7d:20d5 with SMTP id d2e1a72fcca58-72530045b6emr37194340b3a.8.1733164944304; Mon, 02 Dec 2024 10:42:24 -0800 (PST) Received: from KASONG-MC4.tencent.com ([106.37.120.120]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-725417fb7d5sm8849781b3a.94.2024.12.02.10.42.20 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 02 Dec 2024 10:42:23 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Hugh Dickins , "Huang, Ying" , Yosry Ahmed , Roman Gushchin , Shakeel Butt , Johannes Weiner , Barry Song , Michal Hocko , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH 4/4] mm, swap_cgroup: remove global swap cgroup lock Date: Tue, 3 Dec 2024 02:41:54 +0800 Message-ID: <20241202184154.19321-5-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241202184154.19321-1-ryncsn@gmail.com> References: <20241202184154.19321-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: DA0ED40010 X-Rspamd-Server: rspam12 X-Stat-Signature: 3i7j8b6cuxuymaaj1jywockz5kn1kbsx X-Rspam-User: X-HE-Tag: 1733164940-849934 X-HE-Meta: U2FsdGVkX18SZKBkPFkboQwMA+ILOOluIRRoerML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2EOmdBICmF+fEHYniEfuYXbSlH9M6gkVDnfL6vm3mdxwxdTlktwVHX59yG2P3ceGeYAF+jAjQBNQAamVxXzP5jBST0SU+3Tw2gNbKAI0XJArRDEjy182zh4SUimKeJ2xVfGYkvMgZu8NXwMtLR4FofFF3k2rYO0WpK6uGB7DnVZjPUw2YD/eunlNQDz9Ldiew1cVbszgPq7TOpa1Il3Zc8/1k0t+Ds7fIpaRyxqo/3NNaKO8ibZxOZtJB/476VnGX9EnGIEeO8IE0CJk0NdQz5RzsPrRj7+gGvxOkXOr53H+nYd1y+YDcfpBmJjdx2MEZqpRNJf2KthkRIUUiR3srgJz2BTEydu3LnQePDmV1Ks5tvrR4MzbyKouO4dcPSE0e6IViVYC3ErLK+IrKtPMm47fe2YUMeL/X7OMslGspvOpsjhHXCgFgs70KErH5ed7 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 commit e9e58a4ec3b1 ("memcg: avoid use cmpxchg in swap cgroup maintainance") replaced the cmpxchg/xchg with a global irq spinlock because some archs doesn't support 2 bytes cmpxchg/xchg. Clearly this won't scale well. And as commented in swap_cgroup.c, this lock is not needed for map synchronization. Emulation of 2 bytes cmpxchg/xchg with atomic isn't hard, so implement it to get rid of this lock. Testing using 64G brd and build with build kernel with make -j96 in 1.5G memory cgroup using 4k folios showed below improvement (10 test run): Before this series: Sys time: 10730.08 (stdev 49.030728) Real time: 171.03 (stdev 0.850355) After this commit: Sys time: 9612.24 (stdev 66.310789), -10.42% Real time: 159.78 (stdev 0.577193), -6.57% With 64k folios and 2G memcg: Before this series: Sys time: 7626.77 (stdev 43.545517) Real time: 136.22 (stdev 1.265544) After this commit: Sys time: 6936.03 (stdev 39.996280), -9.06% Real time: 129.65 (stdev 0.880039), -4.82% Sequential swapout of 8G 4k zero folios (24 test run): Before this series: 5461409.12 us (stdev 183957.827084) After this commit: 5420447.26 us (stdev 196419.240317) Sequential swapin of 8G 4k zero folios (24 test run): Before this series: 19736958.916667 us (stdev 189027.246676) After this commit: 19662182.629630 us (stdev 172717.640614) Performance is better or at least not worse for all tests above. Signed-off-by: Kairui Song --- mm/swap_cgroup.c | 56 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/mm/swap_cgroup.c b/mm/swap_cgroup.c index a76afdc3666a..028f5e6be3f0 100644 --- a/mm/swap_cgroup.c +++ b/mm/swap_cgroup.c @@ -5,6 +5,15 @@ #include /* depends on mm.h include */ +#define ID_PER_UNIT (sizeof(atomic_t) / sizeof(unsigned short)) +struct swap_cgroup_unit { + union { + int raw; + atomic_t val; + unsigned short __id[ID_PER_UNIT]; + }; +}; + static DEFINE_MUTEX(swap_cgroup_mutex); struct swap_cgroup { @@ -12,8 +21,10 @@ struct swap_cgroup { }; struct swap_cgroup_ctrl { - unsigned short *map; - spinlock_t lock; + union { + struct swap_cgroup_unit *units; + unsigned short *map; + }; }; static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; @@ -31,6 +42,24 @@ static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; * * TODO: we can push these buffers out to HIGHMEM. */ +static unsigned short __swap_cgroup_xchg(void *map, + pgoff_t offset, + unsigned int new_id) +{ + unsigned int old_id; + struct swap_cgroup_unit *units = map; + struct swap_cgroup_unit *unit = &units[offset / ID_PER_UNIT]; + struct swap_cgroup_unit new, old = { .raw = atomic_read(&unit->val) }; + + do { + new.raw = old.raw; + old_id = old.__id[offset % ID_PER_UNIT]; + new.__id[offset % ID_PER_UNIT] = new_id; + } while (!atomic_try_cmpxchg(&unit->val, &old.raw, new.raw)); + + return old_id; +} + /** * swap_cgroup_record - record mem_cgroup for a set of swap entries * @ent: the first swap entry to be recorded into @@ -44,22 +73,19 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id, unsigned int nr_ents) { struct swap_cgroup_ctrl *ctrl; - unsigned short *map; - unsigned short old; - unsigned long flags; pgoff_t offset = swp_offset(ent); pgoff_t end = offset + nr_ents; + unsigned short old, iter; + unsigned short *map; ctrl = &swap_cgroup_ctrl[swp_type(ent)]; map = ctrl->map; - spin_lock_irqsave(&ctrl->lock, flags); - old = map[offset]; + old = READ_ONCE(map[offset]); do { - VM_BUG_ON(map[offset] != old); - map[offset] = id; + iter = __swap_cgroup_xchg(map, offset, id); + VM_BUG_ON(iter != old); } while (++offset != end); - spin_unlock_irqrestore(&ctrl->lock, flags); return old; } @@ -85,20 +111,20 @@ unsigned short lookup_swap_cgroup_id(swp_entry_t ent) int swap_cgroup_swapon(int type, unsigned long max_pages) { - void *map; + struct swap_cgroup_unit *units; struct swap_cgroup_ctrl *ctrl; if (mem_cgroup_disabled()) return 0; - map = vzalloc(max_pages * sizeof(unsigned short)); - if (!map) + units = vzalloc(DIV_ROUND_UP(max_pages, ID_PER_UNIT) * + sizeof(struct swap_cgroup_unit)); + if (!units) goto nomem; ctrl = &swap_cgroup_ctrl[type]; mutex_lock(&swap_cgroup_mutex); - ctrl->map = map; - spin_lock_init(&ctrl->lock); + ctrl->units = units; mutex_unlock(&swap_cgroup_mutex); return 0;