From mboxrd@z Thu Jan 1 00:00:00 1970 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 DC527C71136 for ; Thu, 12 Jun 2025 10:38:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 583FF6B007B; Thu, 12 Jun 2025 06:38:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 534266B0088; Thu, 12 Jun 2025 06:38:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44A7B6B0089; Thu, 12 Jun 2025 06:38:36 -0400 (EDT) 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 277306B007B for ; Thu, 12 Jun 2025 06:38:36 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id BAC531210FF for ; Thu, 12 Jun 2025 10:38:35 +0000 (UTC) X-FDA: 83546399790.10.954A684 Received: from lgeamrelo07.lge.com (lgeamrelo07.lge.com [156.147.51.103]) by imf21.hostedemail.com (Postfix) with ESMTP id 46D251C0009 for ; Thu, 12 Jun 2025 10:38:31 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=lge.com; spf=pass (imf21.hostedemail.com: domain of youngjun.park@lge.com designates 156.147.51.103 as permitted sender) smtp.mailfrom=youngjun.park@lge.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1749724713; h=from:from:sender: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:references; bh=91DkARWmwXYNbXszvyLkWP9TQPdyF1Xt/4xNXzDtMh0=; b=eBWvxP140N9A24AaU5SQTR5TCPfazgKgFd2mBDrHYJ8HnALT2ZD+9UerVrqw1v/MmjpcOb sulsWX8D2WqHiUwZ+sb1Tmyl2CbDbgDL6XZnMaVpyk4p4bwlP8HyvDd+lIXb7Xgs9QKvnF c6XRjg7d9n4pR3PEPLzn0nNAEEA61xI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1749724713; a=rsa-sha256; cv=none; b=nUJw+MzckGy7vbfpODuxSmcchbAYUHBxqQJepOVT9QTkHuszQzg+o4UnK/dONIjv0GGEpN UYFKh8CRyUC5hhvv2LY1hquk7/tl0Epa3rfER4jNa/b0RWWPgoU+SDI9h9xNkpwqUpXG/Y 6h42zGxx3EeheCmlI/z/0LcXXjwf03s= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=lge.com; spf=pass (imf21.hostedemail.com: domain of youngjun.park@lge.com designates 156.147.51.103 as permitted sender) smtp.mailfrom=youngjun.park@lge.com Received: from unknown (HELO yjaykim-PowerEdge-T330.lge.net) (10.177.112.156) by 156.147.51.103 with ESMTP; 12 Jun 2025 19:38:28 +0900 X-Original-SENDERIP: 10.177.112.156 X-Original-MAILFROM: youngjun.park@lge.com From: youngjun.park@lge.com To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, shikemeng@huaweicloud.com, kasong@tencent.com, nphamcs@gmail.com, bhe@redhat.com, baohua@kernel.org, chrisl@kernel.org, muchun.song@linux.dev, iamjoonsoo.kim@lge.com, taejoon.song@lge.com, gunho.lee@lge.com, Youngjun Park Subject: [RFC PATCH 0/2] mm/swap, memcg: Support per-cgroup swap device prioritization Date: Thu, 12 Jun 2025 19:37:42 +0900 Message-Id: <20250612103743.3385842-1-youngjun.park@lge.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: anhzitkts8sszj38otop53pmm8xbc4xw X-Rspamd-Queue-Id: 46D251C0009 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1749724711-397981 X-HE-Meta: U2FsdGVkX1/TpeBKnFL8J6JELPqeECVfba7yO5MXQterDA/IquOiF3YHwAZDkei0LDcwosOTznTq32c4Oyn9KXnfxak7ZM01VtcIJzpmBQnUUCkTcR5jPgLhLmd/JVI8xs/5VumnSAdDqLJ0goNpmGVJiDeEQevPAu1vEYWiRuSWnEZ2HljcafyZZr4kprs+f4HA0cRFi1xGMp6jaAHVnDaxz/TQNRE9Wkf7ObHwyhpoCS2xjRcFLNCgbS3LPIGFAON0n6sFyskNp/ce4dgMRLN1ar1z+IFImh/eiYQjaUsUHy7G5rECbU58bWuA3rDwH6ge+NU6zCG9g2zTLEjU5D49gscRpw90geYIob5tIkhU9CeugAWC7MiHFJNODrqt8TzeoAw5X3GRv85pSsN228FJNp4kvUfj8+eNe8IFfJ4KqefxwU6O38aA4g/duBJz3rnfYwptLjj7RV9XVGwN5ReccaW5BV7skILAm9yCH+3Gj3OtMfGpxz7pCNcQ7GVioze4YvU8TuuqXtgAXKwHFfM+5Enr3rhQEVwk8kzIf2u5BXdnp8rKxLLwacy8W3DhjW+1/lgZhc9FrY6kF1E7vDbKv/TcssUnOH3UhYj3JYnsR+bkAnIdsUk4OQSip3CS2Pu/KYPypajf9cmvwkM/d9jWpIZZB/+8oKWh5Gs+rd+SbU4liQTgvGYw8AFJfAltLSxz6Cqi5WdJlW9NPiaIcnNHoR8K06RNzTTlCzLmlXmGMVfilb2sTGjf0s/THexRhI/CvwQNcwxrRKZg2NoY+noxhWqafKooVBPAmzqthSqCzGk/T+ZRW08qDaYlVuLQMo5YUoJtPGbwPtHdZmNxPtTtsUfDwrZMQQcgt6pYLVyejALbejJJrbdtvWYuUMK07IJqDxNd2beJ8dfbAQnAHqROw1aoJ76fcU2ym6bR1nKBly39I3/ipeWEAWUFQH/7hU7YKQowvcokXQt1CcQ F/o4+aPv +OhkvyGf1/a40kRtpvH4+fevL4YIvhokURU/WLOX0VjvFhdtbsaoXe3OMr1AUUS6JBvJKpYyp/VfO1StXBn5uDK5GuwiJhRq2dElAMf2PwQcK5BFVHpELcDyxuZZ8OkiJ1MZ4nvDQHx5Is2Cs3Me80frOMUlhYYQ5vksBRw3PGP7ZNfX/ixtK9P8AegQoygZmvZihE4ZlwyVV0lJK2zSqZWXdaARGrxxKxon5eEcNmxZ0Th3d4/q9Mlj183ioisw2Tj+2 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: Youngjun Park Introduction ============ I am a kernel developer working on platforms deployed on commercial consumer devices. Due to real-world product requirements, needed to modify the Linux kernel to support a new swap management mechanism. The proposed mechanism allows assigning different swap priorities to swap devices per cgroup. I believe this mechanism can be generally useful for similar constrained-device scenarios and would like to propose it for upstream inclusion and solicit feedback from the community. Motivation ========== Core requirement was to improve application responsiveness and loading time, especially for latency critical applications, without increasing RAM or storage hardware resources. Device constraints: - Linux-based embedded platform - Limited system RAM - Small local swap - No option to expand RAM or local swap To mitigate this, we explored utilizing idle RAM and storage from nearby devices as remote swap space. To maximize its effectiveness, we needed the ability to control which swap devices were used by different cgroups: - Assign faster local swap devices to latency critical apps - Assign remote swap devices to background apps However, current Linux kernel swap infrastructure does not support per-cgroup swap device assignment. To solve this, I propose a mechanism to allow each cgroup to specify its own swap device priorities. Evaluated Alternatives ====================== 1. **Per-cgroup dedicated swap devices** - Previously proposed upstream [1] - Challenges in managing global vs per-cgroup swap state - Difficult to integrate with existing memory.limit / swap.max semantics 2. **Multi-backend swap device with cgroup-aware routing** - Considered sort of layering violation (block device cgroup awareness) - Swap devices are commonly meant to be physical block devices. - Similar idea mentioned in [2] 3. **Per-cgroup swap device enable/disable with swap usage contorl** - Expand swap.max with zswap.writeback usage - Discussed in context of zswap writeback [3] - Cannot express arbitrary priority orderings (e.g. swap priority A-B-C on cgroup C-A-B impossible) - Less flexible than per-device priority approach 4. **Per-namespace swap priority configuration** - In short, make swap namespace for swap device priority - Overly complex for our use case - Cgroups are the natural scope for this mechanism Based on these findings, we chose to prototype per-cgroup swap priority configuration as the most natural, least invasive extension of the existing kernel mechanisms. Design and Semantics ==================== - Each swap device gets a unique ID at `swapon` time - Each cgroup has a `memory.swap.priority` interface: - Show unique ID by memory.swap.priority interface - Format: `unique_id:priority,unique_id:priority,...` - All currently-active swap devices must be listed - Priorities follow existing swap infrastructure semantics - The interface is writeable and updatable at runtime - A priority configuration can be reset via `echo "" > memory.swap.priority` - Swap on/off events propagate to all cgroups with priority configurations Example Usage ------------- # swap device on $ swapon NAME TYPE SIZE USED PRIO /dev/sdb partition 300M 0B 10 /dev/sdc partition 300M 0B 5 # assign custom priorities in a cgroup $ echo "1:5,2:10" > memory.swap.priority $ cat memory.swap.priority Active /dev/sdb unique:1 prio:5 /dev/sdc unique:2 prio:10 # adding new swap device later $ swapon /dev/sdd --priority -1 $ cat memory.swap.priority Active /dev/sdb unique:1 prio:5 /dev/sdc unique:2 prio:10 /dev/sdd unique:3 prio:-2 # reset cgroup priority $ echo "" > memory.swap.priority $ cat memory.swap.priority Inactive /dev/sdb unique:1 prio:10 /dev/sdc unique:2 prio:5 /dev/sdd unique:3 prio:-2 Implementation Notes ==================== The items mentioned below are to be considered during the next patch work. - Workaround using per swap cpu cluster as before - Priority propgation of child cgroup - And other TODO, XXX - Refactoring for reviewability and maintainability, comprehensive testing and performance evaluation Future Work =========== These are items that would benefit from further consideration and potential implementation. - Support for per-process or anything else swap prioritization - Optional usage limits per swap device (e.g., ratio, max bytes) - Generalizing the interface beyond cgroups References ========== [1] https://lkml.iu.edu/hypermail/linux/kernel/1404.0/02530.html [2] https://lore.kernel.org/linux-mm/CAMgjq7DGMS5A4t6nOQmwyLy5Px96aoejBkiwFHgy9uMk-F8Y-w@mail.gmail.com [3] https://lore.kernel.org/lkml/CAF8kJuN-4UE0skVHvjUzpGefavkLULMonjgkXUZSBVJrcGFXCA@mail.gmail.com All comments and feedback are greatly appreciated. Patch will follow. Sincerely, Youngjun Park youngjun.park (2): mm/swap, memcg: basic structure and logic for per cgroup swap priority control mm: swap: apply per cgroup swap priority mechansim on swap layer include/linux/memcontrol.h | 3 + include/linux/swap.h | 11 ++ mm/Kconfig | 7 + mm/memcontrol.c | 55 ++++++ mm/swap.h | 18 ++ mm/swap_cgroup_priority.c | 335 +++++++++++++++++++++++++++++++++++++ mm/swapfile.c | 129 ++++++++++---- 7 files changed, 523 insertions(+), 35 deletions(-) create mode 100644 mm/swap_cgroup_priority.c base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494 -- 2.34.1