From patchwork Mon Dec 2 18:41:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13891176 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 0B72BE69E81 for ; Mon, 2 Dec 2024 18:42:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9EA226B0096; Mon, 2 Dec 2024 13:42:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 99A4E6B0098; Mon, 2 Dec 2024 13:42:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83B976B0099; Mon, 2 Dec 2024 13:42:24 -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 65B5B6B0096 for ; Mon, 2 Dec 2024 13:42:24 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D0C618025E for ; Mon, 2 Dec 2024 18:42:23 +0000 (UTC) X-FDA: 82850889030.24.F182BDE Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf16.hostedemail.com (Postfix) with ESMTP id 88CC6180010 for ; Mon, 2 Dec 2024 18:42:09 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=R7vR7Sza; spf=pass (imf16.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.210.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=1733164933; 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=1aeU1SawrmUs4i99mcy6Gmav34YptGDoV51/MymT7EU=; b=W2Dz6Tz4pooC4+LAtPlVh/4j27uRFVYAEwAMB4z94tyxMawLPIzCzPpan44vOYGNGU/TvQ aB3d0puoIULs9hjwsM+04N5azoQgFQy3cveoVCPZ6eJMVEMMgZBMbHQl003CW6Xa1aT7WK zuXka+urqlnXFQ2rNAotxOFF/QabXGA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733164933; a=rsa-sha256; cv=none; b=xsy7ss7gbP1MlbTFd7E6/9xLtdR8JhcaA73/W7uwxraYL2tp3fj9stfVqfR04E7BQizQo3 4assw82BfKcvf6jmj2UEUBUaWe55pEaPCw0yT5eA/NNy07uDe+nifXGhgQlOvy1IhMd/Ig zszj54cXQQfyb7hqweI814bL5SbAHHs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=R7vR7Sza; spf=pass (imf16.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-724d57a9f7cso3895492b3a.3 for ; Mon, 02 Dec 2024 10:42:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733164940; x=1733769740; 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=1aeU1SawrmUs4i99mcy6Gmav34YptGDoV51/MymT7EU=; b=R7vR7SzawNGkvYwvpmxta6R34AfZJuzXS0E2kGYrTyuut3h43a27/9NUsJFI4rxqgK V6EK67boApN5aJ0a5EhneFuyIwwjkRQjOIvY9alE1CfPOd+8RpHoEPxQhnnFqT7FzjHp +/QLjBljsUxmh8AGBxHtci1EgXf3K8DduBkQwgUhbrVSBIBUQQGb+U/B9yQZfNJIPGqM kVnTWAJd4UaZOYuy0V5Ig+zM/0kn8oRhz0nbJ109cL4IDzustx0/J2oOBAgm82jalI2k qbLS4r2KGo4stf8NXHtw6tfMGPBprPcZRBOO5gRBwdDwO+bKBURs2c1m/6m6/bk9lROB MRpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733164940; x=1733769740; 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=1aeU1SawrmUs4i99mcy6Gmav34YptGDoV51/MymT7EU=; b=D4YHps3OKlgDdgjiwbjvqxj+Z0B/S0HZNnRUYCMYyGZ1XMS6m/u+I3rbaw9sRL1UNQ 5W2LluMzS2+9/m0FFtBLc27fwoM6SM89fdunAdA7TFlitNsJ9koQxnq/M29PP0KDpjAJ r9Rg6dKJJFzTqFE30w5afsXt79rnZ9R7L2hudhpW4W7Cm5q4mMBIpvKkPsFBvaZDoQmi 5f9ur8BK+mVKWeNgFo+Wp+46KFCyqqtUDIUq0QZySLYKQU1GF7x61O37ksi2YNti0r80 9Na5zVyP3pW7tNqrvIn8bPX2HzyG9csyP8vxSfMjqFpePUj+iB+t2X9we8Myx0APp1xY aR3w== X-Gm-Message-State: AOJu0YzTKF7ihlHDXkSWXf1CbeCK/+6dbtXZ+5Hgs0UB8DJbuuWuO2HY 9eCuoHk2kSDRmRZ3ui03l1WfyyFKS4KegNT0J54sV56uwnvkqJAWjWyXn6M6NiwpYg== X-Gm-Gg: ASbGncvx+emovRDi1edlz832EvYhMxL61BtZWSwUHEvl6bguEBSyVlk9mepOtjImlRa PpT+j+M8K20SWBEsXifQULl2XGBW1eb4t4Crln3hlQ28mqFy00ql/zvZ8GcvQLFl9b8wRNve//j xWsiVZABIJ5Seq4P0SY1Ri3czyc0+gfmoRL2jP56mMZByjcukDg7kSMwUSri/BTEA4NXeL252ke 3ZtJH1XC7uVmXge1RsdVU4WpkZgpMFvQKmvST6q40Z+CIeLTlefVJAxdubVs/ExTS9HR+o= X-Google-Smtp-Source: AGHT+IGfiyQ6y7KACpekf7bdygxRsz/ROBwam32XtlPd4Ct4vBnneUD3+Zdg4Z2+z0w6o72q8gMZxw== X-Received: by 2002:a05:6a00:1a8b:b0:724:fcff:505b with SMTP id d2e1a72fcca58-7253007218fmr32371685b3a.14.1733164940196; Mon, 02 Dec 2024 10:42:20 -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.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 02 Dec 2024 10:42:19 -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 3/4] mm/swap_cgroup: simplify swap cgroup definitions Date: Tue, 3 Dec 2024 02:41:53 +0800 Message-ID: <20241202184154.19321-4-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-Server: rspam02 X-Rspamd-Queue-Id: 88CC6180010 X-Stat-Signature: 3x4dnthf6xhet5gqk6u5mzaajxehjb99 X-Rspam-User: X-HE-Tag: 1733164929-724962 X-HE-Meta: U2FsdGVkX19mrCw8AIeuj/N0UrFLXzfcPmXfoMr/gnaaenKEbfXLFxVycJU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JwKkB7mD+5T24/bW0EbPX8b1ua6GRqChWqLyLw6wrPcsr1UHr84ec4IJYVKDfoSiUyfr9h7bYCDc/l4uiN9fEmV+Dbf8oNB+j3mYLhxbjeqMXaruTK5QBjoQyE2OOcRI5DSZ3Ns/WAO8GA/FIGxJ3Dg6ppBvHrbTJ7fnxpRCw6XsQnnIhAqXcC9rMmXHwWTL+IwI95TPOcRiXBIL1qcdBLo7b+LyHYHA1xRP3Kn1e1E0Pdw4af3yOxEt1ZvC4niIAxMH1v4eMsysMjgRmeNV7usMUZuVu5omPc3jcybo42C00Ak3R9SxkpnzdfecgT4AZLb972Cs5Dj4fYQmph2aLo/C7LW7mWEm+jtRRAsfgcQLXbtilQDVQ3Ff/Skc0T33IDDPyyJ+w7d3p7tJTz//wBMmEx5Bs4Z4j 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 the intermediate struct swap_cgroup, it just a unsigned short wrapper, simplify the code. Also zero the map on initialization to prevent unexpected behaviour as swap cgroup helpers are suppose to return 0 on error. Signed-off-by: Kairui Song --- mm/swap_cgroup.c | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/mm/swap_cgroup.c b/mm/swap_cgroup.c index 1770b076f6b7..a76afdc3666a 100644 --- a/mm/swap_cgroup.c +++ b/mm/swap_cgroup.c @@ -12,14 +12,12 @@ struct swap_cgroup { }; struct swap_cgroup_ctrl { - struct swap_cgroup *map; + unsigned short *map; spinlock_t lock; }; static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; -#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup)) - /* * SwapCgroup implements "lookup" and "exchange" operations. * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge @@ -33,18 +31,6 @@ static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; * * TODO: we can push these buffers out to HIGHMEM. */ -static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent, - struct swap_cgroup_ctrl **ctrlp) -{ - pgoff_t offset = swp_offset(ent); - struct swap_cgroup_ctrl *ctrl; - - ctrl = &swap_cgroup_ctrl[swp_type(ent)]; - if (ctrlp) - *ctrlp = ctrl; - return &ctrl->map[offset]; -} - /** * swap_cgroup_record - record mem_cgroup for a set of swap entries * @ent: the first swap entry to be recorded into @@ -58,20 +44,21 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id, unsigned int nr_ents) { struct swap_cgroup_ctrl *ctrl; - struct swap_cgroup *sc; + unsigned short *map; unsigned short old; unsigned long flags; pgoff_t offset = swp_offset(ent); pgoff_t end = offset + nr_ents; - sc = lookup_swap_cgroup(ent, &ctrl); + ctrl = &swap_cgroup_ctrl[swp_type(ent)]; + map = ctrl->map; spin_lock_irqsave(&ctrl->lock, flags); - old = sc->id; - for (; offset < end; offset++, sc++) { - VM_BUG_ON(sc->id != old); - sc->id = id; - } + old = map[offset]; + do { + VM_BUG_ON(map[offset] != old); + map[offset] = id; + } while (++offset != end); spin_unlock_irqrestore(&ctrl->lock, flags); return old; @@ -85,20 +72,26 @@ unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id, */ unsigned short lookup_swap_cgroup_id(swp_entry_t ent) { + struct swap_cgroup_ctrl *ctrl; + if (mem_cgroup_disabled()) return 0; - return lookup_swap_cgroup(ent, NULL)->id; + + ctrl = &swap_cgroup_ctrl[swp_type(ent)]; + pgoff_t offset = swp_offset(ent); + + return READ_ONCE(ctrl->map[offset]); } int swap_cgroup_swapon(int type, unsigned long max_pages) { - struct swap_cgroup *map; + void *map; struct swap_cgroup_ctrl *ctrl; if (mem_cgroup_disabled()) return 0; - map = vcalloc(max_pages, sizeof(struct swap_cgroup)); + map = vzalloc(max_pages * sizeof(unsigned short)); if (!map) goto nomem; @@ -117,7 +110,7 @@ int swap_cgroup_swapon(int type, unsigned long max_pages) void swap_cgroup_swapoff(int type) { - struct swap_cgroup *map; + void *map; struct swap_cgroup_ctrl *ctrl; if (mem_cgroup_disabled())