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 D07C9C3DA41 for ; Wed, 10 Jul 2024 03:32:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 67A596B009D; Tue, 9 Jul 2024 23:32:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 629596B009E; Tue, 9 Jul 2024 23:32:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F11B6B009F; Tue, 9 Jul 2024 23:32:36 -0400 (EDT) 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 312E06B009D for ; Tue, 9 Jul 2024 23:32:36 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DFAE6121AD0 for ; Wed, 10 Jul 2024 03:32:35 +0000 (UTC) X-FDA: 82322420670.13.04B0FE4 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf09.hostedemail.com (Postfix) with ESMTP id 07B6814000F for ; Wed, 10 Jul 2024 03:32:33 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="cz/G8i0x"; spf=pass (imf09.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=21cnbao@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=1720582323; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xQVH0SfMzB2fGcavBeBUmXLxwFGQclxpF5/YP0r+ic0=; b=kAzWoCu/wbRsjUJt0cux3gwDovqlRalhaC4uxNXHlLXu6oC7dDDbOO57XfTlfqFv4P4Oxe PLukpqcN1GFcczg+rm4VFQ5cWWdvM9rceKiF3EbdChKUxPoaZbLaKCdm/RDaz9UsbkT7sd 6sL4EBTpCyLm+uNtSqOqbu6O5J628Jc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720582323; a=rsa-sha256; cv=none; b=3DaEpqMnXNZmssgsuE2HZKEM5p4uRwqFX1Wwd+ApJUAgCXvh6eupRCfCRSZmMMfsvwcq8y xBRKdwtIEzhU2kDkFZ4MJSWqWM7RksBPdwZByxfmO4ngBWhMwJaTH2f4G1YgBPm1PXEdqZ JIF7rV/Sk46GxooLXfbp9vtG1Hm3/5M= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="cz/G8i0x"; spf=pass (imf09.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-70b09c2ade6so3109208b3a.3 for ; Tue, 09 Jul 2024 20:32:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720582353; x=1721187153; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xQVH0SfMzB2fGcavBeBUmXLxwFGQclxpF5/YP0r+ic0=; b=cz/G8i0xzCQPxrcVqzYIXip0+LN7pq8L9f3+Ze3tfWa8Cvr1A7yAH04JQphthx5Ppe um0B3sHut5nPXTNOEklV3iGMn2XEro0jU6dS0LB6ZJ6RKAmMw9b6uQ5Z3mUhMjXgUAun XsQaHPQZx1erYktZ67LWfow+8lfu2qcnPAG4UL7RPxfxl61Ylyrf2DwbR857KXhTzzuU fw0K+3y0cGVDMEk9sgKbhCquo6krUQC7H2Gn209pZyQaChZ4Rz8/a0EdSa+C9NtdfUO6 PxUa+B560gYKWrtmMwQlneYbpLPRg15NRNrNmoxNRHk0ETgp7pWznQE5Xi8TpojbHWjn 5SxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720582353; x=1721187153; h=content-transfer-encoding:mime-version: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=xQVH0SfMzB2fGcavBeBUmXLxwFGQclxpF5/YP0r+ic0=; b=c6DIOrFIXHpGVDRYlzZvROybHHWe+1D7ZGSp9Zm3JcVDDXdJmSjZ3LpjS8LLd3TgsV Pe/AK2wQfgx98pjvJxiBPf9DVJRMMD4lzn7Feg2AkTt1K7q+SJCHQwJhyJpIl8WQsXK7 AMh3ZVzFoW6VbBJ38iuaqQJpJBE+uXrNZtKcFWW+K+EnTZnJq/jIDWHbKyJj4oHGfUQe EQOcPm9zxRcBnFu/pfT3QATmM6Rjhiv/fIE+X8WYGkcXmP3OST4W4C2Oi2p1WRCRf89g up861HgYW7anr/hWKNzs+yhEg/uDwaG2y/zJ3V2HQYK2bl4JLWf2yG/Kg8HFiM2vJLfB LTaw== X-Forwarded-Encrypted: i=1; AJvYcCWSkTF0laV8YASNkUVRduWrKe2mbHakYV1RFU2EoSto/UIMbYaXI9KRmEEtbfagtyop8u+tjBqoKrjHE6vIPiLFIDQ= X-Gm-Message-State: AOJu0YwwHULfFlXcx2ZnPwkeLqh8stfvPsYj6gqXVi6LHabQLfOWrdo9 Hkb7TME/Nnw6qZPI3BhCkknAhNI24V6AVUFBhppWr3uRoCkPhQ0p7LNkTg== X-Google-Smtp-Source: AGHT+IHZiB3/+XMW56ClRvapdT7LlIo/fbLLW8refL2J+bDXaexjAPms3UOkv0Tm9mcGXkZb9O3ECA== X-Received: by 2002:a05:6a21:328a:b0:1c0:f080:ed51 with SMTP id adf61e73a8af0-1c29820388dmr5084332637.2.1720582352685; Tue, 09 Jul 2024 20:32:32 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fbb6a122a1sm23946845ad.45.2024.07.09.20.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jul 2024 20:32:32 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org Cc: baohua@kernel.org, david@redhat.com, justinjiang@vivo.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, opensource.kernel@vivo.com, willy@infradead.org Subject: Re: [PATCH v7] mm: shrink skip folio mapped by an exiting process Date: Wed, 10 Jul 2024 15:32:12 +1200 Message-Id: <20240710033212.36497-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240709142312.372b20d49c6a97ecd2cd9904@linux-foundation.org> References: <20240709142312.372b20d49c6a97ecd2cd9904@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 07B6814000F X-Stat-Signature: 3z1wsbpu7mb4y6izypuzxe6hgsa4uex9 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1720582353-839509 X-HE-Meta: U2FsdGVkX1/oQXfMeU/WEZBz9VulN9jc0asEmPCvMKpCz5v2WExlGJosQXau8ZJN9WsmGa7nJk6ePfsgfmGIsE02sWuQsznInZVNy4Naju4Nu1mZw5j1gHM2eQ21Pf2GN2ksJykQK4FbLwm77rJs+U1shg7OnoZgd7C5LCPuMJBHqb+yWt3by8r+2tTz0icZbtoIiAF7SxHgT2nQSwDITgEOB0XxUO9l9uvz6HXLsh/IkuM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YiJSVUZIbl2MSsvPqUDG/J/knP1GCybp80vJ54y+Pc+wyRTJa/JWaaRcqhA+hJc0ZTz+LV41CbDthsCmqp0nK3p0TdIqILDvyMdekBBuNQnFaeXkZWydpK5o6JSPa+D9fje1BAAe7K4gjM7oCbRZSkVXfdDdMM450PFjN9VN4ZJFRrvwyj/HoOOzPxd3XpSo= 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: On Wed, Jul 10, 2024 at 9:23 AM Andrew Morton wrote: > > On Tue,  9 Jul 2024 20:31:15 +0800 Zhiguo Jiang wrote: > > > The releasing process of the non-shared anonymous folio mapped solely by > > an exiting process may go through two flows: 1) the anonymous folio is > > firstly is swaped-out into swapspace and transformed into a swp_entry > > in shrink_folio_list; 2) then the swp_entry is released in the process > > exiting flow. This will result in the high cpu load of releasing a > > non-shared anonymous folio mapped solely by an exiting process. > > > > When the low system memory and the exiting process exist at the same > > time, it will be likely to happen, because the non-shared anonymous > > folio mapped solely by an exiting process may be reclaimed by > > shrink_folio_list. > > > > This patch is that shrink skips the non-shared anonymous folio solely > > mapped by an exting process and this folio is only released directly in > > the process exiting flow, which will save swap-out time and alleviate > > the load of the process exiting. > > It would be helpful to provide some before-and-after runtime > measurements, please.  It's a performance optimization so please let's > see what effect it has. Hi Andrew, This was something I was curious about too, so I created a small test program that allocates and continuously writes to 256MB of memory. Using QEMU, I set up a small machine with only 300MB of RAM to trigger kswapd. qemu-system-aarch64 -M virt,gic-version=3,mte=off -nographic \ -smp cpus=4 -cpu max \ -m 300M -kernel arch/arm64/boot/Image The test program will be randomly terminated by its subprocess to trigger the use case of this patch. #include #include #include #include #include #include #include #include #define MEMORY_SIZE (256 * 1024 * 1024) unsigned char *memory; void allocate_and_write_memory() { memory = (unsigned char *)malloc(MEMORY_SIZE); if (memory == NULL) { perror("malloc"); exit(EXIT_FAILURE); } while (1) memset(memory, 0x11, MEMORY_SIZE); } int main() { pid_t pid; srand(time(NULL)); pid = fork(); if (pid < 0) { perror("fork"); exit(EXIT_FAILURE); } if (pid == 0) { int delay = (rand() % 10000) + 10000; usleep(delay * 1000); /* kill parent when it is busy on swapping */ kill(getppid(), SIGKILL); _exit(0); } else { allocate_and_write_memory(); wait(NULL); free(memory); } return 0; } I tracked the number of folios that could be redundantly swapped out by adding a simple counter as shown below: @@ -879,6 +880,9 @@ static bool folio_referenced_one(struct folio *folio, check_stable_address_space(vma->vm_mm)) && folio_test_swapbacked(folio) && !folio_likely_mapped_shared(folio)) { + static long i, size; + size += folio_size(folio); + pr_err("index: %d skipped folio:%lx total size:%d\n", i++, (unsigned long)folio, size); pra->referenced = -1; page_vma_mapped_walk_done(&pvmw); return false; This is what I have observed: / # /home/barry/develop/linux/skip_swap_out_test [ 82.925645] index: 0 skipped folio:fffffdffc0425400 total size:65536 [ 82.925960] index: 1 skipped folio:fffffdffc0425800 total size:131072 [ 82.927524] index: 2 skipped folio:fffffdffc0425c00 total size:196608 [ 82.928649] index: 3 skipped folio:fffffdffc0426000 total size:262144 [ 82.929383] index: 4 skipped folio:fffffdffc0426400 total size:327680 [ 82.929995] index: 5 skipped folio:fffffdffc0426800 total size:393216 ... [ 88.469130] index: 6112 skipped folio:fffffdffc0390080 total size:97230848 [ 88.469966] index: 6113 skipped folio:fffffdffc038d000 total size:97296384 [ 89.023414] index: 6114 skipped folio:fffffdffc0366cc0 total size:97300480 I observed that this patch effectively skipped 6114 folios (either 4KB or 64KB mTHP), potentially reducing the swap-out by up to 92MB (97,300,480 bytes) during the process exit. Despite the numerous mistakes Zhiguo made in sending this patch, it is still quite valuable. Please consider pulling his v9 into the mm tree for testing. Thanks Barry