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 67677C38150 for ; Wed, 10 Jul 2024 04:02:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED2A66B008C; Wed, 10 Jul 2024 00:02:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E81F66B0092; Wed, 10 Jul 2024 00:02:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D22796B009E; Wed, 10 Jul 2024 00:02:48 -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 B3FF26B008C for ; Wed, 10 Jul 2024 00:02:48 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 66E2C121ACA for ; Wed, 10 Jul 2024 04:02:48 +0000 (UTC) X-FDA: 82322496816.19.D6E7EC9 Received: from mail-vk1-f178.google.com (mail-vk1-f178.google.com [209.85.221.178]) by imf21.hostedemail.com (Postfix) with ESMTP id 9F6A01C0018 for ; Wed, 10 Jul 2024 04:02:46 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Bygaw6Ry; spf=pass (imf21.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.178 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=1720584151; 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=wUOc1zhvUATC2lNy1DissAy7dipktC0MCYjVDZzv/QQ=; b=BGoaGmgPFnJvKu+I8Q51WfPKIbbM/rfnYu0Zt/CP3+ETcFUjpvGzBBpdTXHgOwAl5RUxhy R1m5TW05kJc+D62lK6kblkKMraOg52H01FST4gbnJdop4McM5h4+U3wWXv6G1M8lRtX5lm uhuTbZQqzt4lSbcqxctPqRAOcOfD2t0= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Bygaw6Ry; spf=pass (imf21.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.178 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720584151; a=rsa-sha256; cv=none; b=G8zrlGVeH7omIpMPop1onaFHH5xDkuR49Y3aodUD1JIjy+ynKLrOgjWQSVxUbch9nHNHE5 bIVW5hTD7BT6vjToodkD+IUGBvxv9nmDIy1ab8VmbM3TpJTmNj++Z6Ne5IObHqx4AXlmai q4kNeo8cuKmF5h1JqGeLAN9yvZxwaAs= Received: by mail-vk1-f178.google.com with SMTP id 71dfb90a1353d-4f2e1093abeso2200043e0c.1 for ; Tue, 09 Jul 2024 21:02:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720584166; x=1721188966; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=wUOc1zhvUATC2lNy1DissAy7dipktC0MCYjVDZzv/QQ=; b=Bygaw6Ryn8lZsFcurL4tQmCeOJttpRY0ghXaS53yGEhG+RIKI9JaK8QB7fXp9MRpjn wS+PN3sQhwIXljsCSCpPpy2QRGOevjqepQvy9ISG7hsx/Eup/ADR4qWJBAY3tkKP2s/2 cjfuGDN9Bd5NYrseJ/5vHKF9XeyqJk9dyIK6lkZ7oGABhKgnps4x8Eht37srf2u4aGJ6 +79bGFZqyxrACebmYbrGrEUwP3b6+bNFZd/RCLN1rcNv6bpWnCr1zOwfjh288znzAjww naqFzzEVU+99Z52/QNUW8nOa4LC1YqWmdbvzb3b3zy80qGisXAUpLUUfq+EhxaktMzkG PcUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720584166; x=1721188966; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wUOc1zhvUATC2lNy1DissAy7dipktC0MCYjVDZzv/QQ=; b=Gb8SdoMBAdCMJ13LkRtElwonclDnJQ6E+MOohiIW352lEscND2XDfKzh/6WPG3WKJQ idgzFXmNhYulGcfNSSCkphxSJwjRwe3vQKgc1NWJYcBl8wa/X23FHkd0NIEoKj8fBvDP hyxO5Uhto+rQi/dMH7Ft+LgPxl31yVfpERG+GdmlT+ZPeyJzIxNPiBdASa6Q7WiEjtxL fbWBzUTs/IdECC259Mu9TOY6vhNJsr1rCTvdkb5czBHgCY4LqF8J8TowZeEsUNooZkSZ Hoyh4X7vwx2UBV2xhpxh41QLfbZiQ3QtA1SE5yALJajkoWhEWCcFd0ZDlTau1Tf9apeS qmMA== X-Forwarded-Encrypted: i=1; AJvYcCW+dwdsr5jwgcuk8h+kl7yT0SwI/YcnQ6mB8RUomIqW6RppOVE4ovB6uT5pq9RzA1AMYOBugTxk78XiLAtWtOdmGhc= X-Gm-Message-State: AOJu0YwiKibx1fI98+7WD/OD+0CCgPp1ZzhMplCH9B66AEMvkQjRDI43 y+wLYzGHMrS5YpXVirkQSUiKbNuJ5B1rHwDf/v7QsTWz8rGOCG7wVvv7awb6p0fBi5gZXzs6mkr ZzQXhd0GTUJXXlumF3nUOZKMsFTE= X-Google-Smtp-Source: AGHT+IEh1Tawc5szKRk5EjPSIbWX9sPL9WCeZ4VwMrOypjuvXV29AUsg6QhjnpXc2d3n9IG1X4qAhr14XzJ08u7ISbo= X-Received: by 2002:a05:6122:910:b0:4f2:ebd9:8e12 with SMTP id 71dfb90a1353d-4f33f24fa48mr5619965e0c.5.1720584164223; Tue, 09 Jul 2024 21:02:44 -0700 (PDT) MIME-Version: 1.0 References: <20240709142312.372b20d49c6a97ecd2cd9904@linux-foundation.org> <20240710033212.36497-1-21cnbao@gmail.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Wed, 10 Jul 2024 16:02:32 +1200 Message-ID: Subject: Re: [PATCH v7] mm: shrink skip folio mapped by an exiting process To: David Hildenbrand Cc: akpm@linux-foundation.org, justinjiang@vivo.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, opensource.kernel@vivo.com, willy@infradead.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 9F6A01C0018 X-Stat-Signature: a7ib3pzm8zzeu7qm5jmi7fw8ac4o9m55 X-HE-Tag: 1720584166-996349 X-HE-Meta: U2FsdGVkX18lRWzTPB3kiycNlCw3JsIs0da4bXv++Y3e7hsHuvA3OhkP3EDmLySWOr7ALN2u9HhFlTLYn0XApBdMK2Y+ShQ2UilqhWKclyBEK6Z29SwhIhVWe8NHKTm1A0Vgbj0Qezc7BHfQHKwfqxJ+/o9jc2b1kAH+zKW/2BicDnn+NyFUWrPf4Pdnn/WaRO3tfuCsb5ySIHMrd1LM8Huo4F2DsbYpdoWNB4o8alaTggme5WtIVwv6c4gKD90b5RSM+eOO6yX06vw4dFzEbSRL1F6h99vOsrF6kU3if0UeZNHNbk9FZ955omo/YsaCmjoCAGvvMm0EAt7ShyMEOhfQArme7Q4TTSXI4ZfeLPOoRKzzjJrmY4QXOio4/5LzYBlfaQdjiECTFIkPLvfTzFI0myOR+mjcULIp3wwMeNhTepouo0oBB/rmGsHbt7YHB6uvz2Yb+5QuQydlQjm7aEbogPY/3DCUpB/IoX6iggdlOBrlwNLKHG4hb7ROchNPvgAxUNxS/qMCirwMtLqO2VduT5C1FL/MZoW0aC/sYvOz6sfLy0NLuTN83J/3flBHW8D133XmhXrq7vyj/FCZv66VaonGcau250XrAdcbtDEmnEBKSK5cBrGAbuamoH2E1vBZeoggTD0qiAGjX3qy7SoxlA0TOzMXxk/67lkIelUYYX+gPmsOGlOJjmgFRGWndEQb0MItyk7mK2UDFepdZFyisCP9uo48gcbtPhzcCn3yQnB5nbBqE5tuCFP6SHvl5Np1H+RoMRNArqfydGDBxe1GppkkXfO3yrm/vqDZQOBPSoOhWiQQUaTkY2uW06j8DtUx0VqjHoYCc3ymzfoZshZBeAjnA/J1KEPm/Rk1nF8qfmgudziMqtwgDDH+wILRPECo7MsawdSImktk+L0k58ux6Me+QPwhS2phEIGesfQK9WZNbi3FoBwpWzrYcVKmGflqlU4Do4YoqqGhw4+ pvadJFEU MNWpa7BoD8PKb9M29BDNGxmgQJpYcmeDeISIeXRTytzGDs3M4h87RXOAacUFKz0ZlCbSTia6SzcH78wXN8kAp5BZ4Lqm99OHjKq64B3aXTu6rQDqiJBG7TeEPdcOtrAsp0sJIZSCzBCv4k5mR00SXnaVBm8kDwBwc9VuBJJsE1FhKQqsgDfqjOG+GDzDk2bgBioCdPglaUWKUbZqLWSwo6yh1cK6qJxRCpUJcLDDSwWlvVkdOvzBSbrmFAnNl5KkbV1eXTKBMhCu22Or3nGccqTZBObcJi17g3kGx3XhC080izDEwhS2aVeTvfvMxuivGxIXJ6YrGLsnxaU5zUL1lA6bJlcVBRfk1tjj/d1+agWWzPVOV6aik+huLG8PAnIOxoxj48qD1nKlNWbIUZXREd6PRQ5I9TjQo6ZjbgwafrIhCO7F/idL3MwQ7eg== 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 3:59=E2=80=AFPM David Hildenbrand wrote: > > On 10.07.24 05:32, Barry Song wrote: > > On Wed, Jul 10, 2024 at 9:23=E2=80=AFAM 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 i= s > >>> 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 proces= s > >>> 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 p= rogram > > 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=3D3,mte=3Doff -nographic \ > > -smp cpus=3D4 -cpu max \ > > -m 300M -kernel arch/arm64/boot/Image > > > > The test program will be randomly terminated by its subprocess to trigg= er > > 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 =3D (unsigned char *)malloc(MEMORY_SIZE); > > if (memory =3D=3D NULL) { > > perror("malloc"); > > exit(EXIT_FAILURE); > > } > > > > while (1) > > memset(memory, 0x11, MEMORY_SIZE); > > } > > > > int main() > > { > > pid_t pid; > > srand(time(NULL)); > > > > pid =3D fork(); > > > > if (pid < 0) { > > perror("fork"); > > exit(EXIT_FAILURE); > > } > > > > if (pid =3D=3D 0) { > > int delay =3D (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 *foli= o, > > check_stable_address_space(vma->vm_mm)) && > > folio_test_swapbacked(folio) && > > !folio_likely_mapped_shared(folio)) { > > + static long i, size; > > + size +=3D folio_size(folio); > > + pr_err("index: %d skipped folio:%lx total size:= %d\n", i++, (unsigned long)folio, size); > > pra->referenced =3D -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:13107= 2 > > [ 82.927524] index: 2 skipped folio:fffffdffc0425c00 total size:19660= 8 > > [ 82.928649] index: 3 skipped folio:fffffdffc0426000 total size:26214= 4 > > [ 82.929383] index: 4 skipped folio:fffffdffc0426400 total size:32768= 0 > > [ 82.929995] index: 5 skipped folio:fffffdffc0426800 total size:39321= 6 > > ... > > [ 88.469130] index: 6112 skipped folio:fffffdffc0390080 total size:97= 230848 > > [ 88.469966] index: 6113 skipped folio:fffffdffc038d000 total size:97= 296384 > > [ 89.023414] index: 6114 skipped folio:fffffdffc0366cc0 total size:97= 300480 > > > > 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 byte= s) 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 tes= ting. > > BTW, we dropped the folio_test_anon() check, but what about shmem? They > also do __folio_set_swapbacked()? my point is that the purpose is skipping redundant swap-out, if shmem is si= ngle mapped, they could be also skipped. > > -- > Cheers, > > David / dhildenb > Thanks Barry