Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp2033200rdb; Wed, 31 Jan 2024 17:54:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IF7pWEnVBum01OTk0Gxa/4waePcBG/w023BOWJ5BINVe22E9sap2VV5jANd46g7eXZQ6Jvz X-Received: by 2002:aa7:804c:0:b0:6db:d59c:a5b7 with SMTP id y12-20020aa7804c000000b006dbd59ca5b7mr702509pfm.20.1706752443936; Wed, 31 Jan 2024 17:54:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706752443; cv=pass; d=google.com; s=arc-20160816; b=xJ/EYYTU3Xoy9RWc4BYf9SkrNBSjQCtaqioXLmEB21tjwFS4yNlAKUi/pV4kMwaiPG qmA4o3fduetPlwsIiNjyENrQdsmpKuvbX3hfI5FII+Pptzsk/aiDnRv0ICGG2Q6INAKR ouR2ztbHtx2NJqeXdZSPI3SlTgaO8ousaZLD2hErdA2Z3o0J2yM+OaWq9qsvVkaurk4T IWmmbXpHhCOoUuhqBixgl53BA84+6OBDACHNlrOHL8xudEMEvCdaduVd9i1hpl0O08dE 7Wyw+9a+keTV3VpshqmEsfFMfbVMsfxojJX5U/cqcH4StRvhvcATpv5calmj0iFsTi7K ylzg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=R1i2UN1ZAwaudGKHw526hiiniRq9FbmCBbcGNJI+D8E=; fh=IN+ccbh4oy2RWrac9gUp2iCHVT1eGjVLkeyQGJIYKho=; b=uvdIIPdT8fMCGOSuTfos6DR16W5vW523CJcAeQN2rNkkYtWOiHXKuYv4Gr72RG9/NR DlK5rqt0kWexQzeYsLuXf8Mlxsi3F09wPx8cufRYAkp1NbxD807J1gKaE9/fz2hFVOIT 8B7RDwNxsPdUfoNR4EePx+tXlS4wbET7dM+svaEwZ12k4OYsI9Hoh1hjmaxzFCxjxEIr eHUIleuGDTR2zBr/Mw3HWn+Drq6dzlg/khHdzQg8dacGBSuQIIkM1GC+ui3F7WWgfeWz 7xD4BpDuY7K+cGEsZXs9M9moi5A9TjHT3ZXgVo5o/UE8+zjhs+rnOQfnCDfm01shfOZI NWwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=okgUgo8B; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47494-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47494-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCXM9XB+IcxKCTCQQ4evw1nVjyOeZNyHVB5O9UvXgHGCJpD0ODV9/LCkDJtyxNtM57IqU0cDLTyfqrn047mATWJc26t1ayYknoZUUWuZLQ== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d67-20020a633646000000b005cdba9aa36esi11037150pga.733.2024.01.31.17.54.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 17:54:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47494-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=okgUgo8B; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47494-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47494-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 98D8BB27AFF for ; Thu, 1 Feb 2024 01:22:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D167B4417; Thu, 1 Feb 2024 01:22:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="okgUgo8B" Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D3C12116 for ; Thu, 1 Feb 2024 01:22:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706750553; cv=none; b=LmBPlyv69LR7JUHk1x1zJn0gosEWWwWRzWwCsvCjyvsdTVAEwGTkbbHodSaEFrtzKLaGKPZ9QhPwWO2DQCR5nFHErkfkam44J6pTrkAwf3Gd/MczBH1AdSg5kxCKVnAa9GvKitjzTEvjiBL+YCTmNOyoVhZrNE38Xrl8qV05QtY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706750553; c=relaxed/simple; bh=aEKufm4NNdv5kiYkiKhX+BFZ1Ds7FoO/aoWc/2QOVlI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=EzDBVpdwb2N+w9hj5dPVgWizYlbsEVt6Lpt1OgYqAhZJ9jZfw69lg7XbJ0S4G1Lx7RPyE1zJwpw3CeBXfU60C7CORjHXfJccp7Q/9cx7Gjb0puud/qorVn2D1xx8lfIIXJ/WKvn03dIe7oSA3KqGBB6OhhmWObQd39q/Y1dBkhw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=okgUgo8B; arc=none smtp.client-ip=209.85.167.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-5112b1a795aso528045e87.0 for ; Wed, 31 Jan 2024 17:22:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706750550; x=1707355350; darn=vger.kernel.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=R1i2UN1ZAwaudGKHw526hiiniRq9FbmCBbcGNJI+D8E=; b=okgUgo8BLDUzlavskTd4/B6sP+P8PNHMmeGAM6cl+QL2uLYrRLJuVrGQ5TQPUepKiJ EmuenIoG2uuuFSHBRStwDHxSlmGCmm2XDX2rd5gxBWi2xMlUYpWERKsUXY0RSYniL6UO hOiuWDMcivSWtk0hgVatbmvEotgYWshDJJdkW1A6JTZVLzhTnZAwKWtFl8YD0jkre3wy V/JsOzsa2OK+MbZN9Agt/TYtdDZlCozTLuRrKOP7ZfHqmw5JmCyS7m86XWcXgNxTbwMO L2bXSeHRdBJ3qMJE7e25jYA2t0fokM/q8v4CdcMSQDYDW4IRmp0hdA3HNuLDGd0QjWpo HDPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706750550; x=1707355350; 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=R1i2UN1ZAwaudGKHw526hiiniRq9FbmCBbcGNJI+D8E=; b=ZJvhdIHfLnOWB0jySXy1AmRoYXL/bY7hsVxT5hQ7GRJEx0skOH5EGXavvKMy2GCWt1 s3DBIDY2xgUaIxZKXucdPYxIIt4w+Uf/jtRx3hpEyq9v3aD9tUDsQNxuTi/wd/ISaH7M PAJaki/xYzfsacP9LA+iHbLaF8hl/GEDuJvL4Ix2d3p0BH7oOWi/9u5rF5twTMeA3cWe o/8vGdqUU4wEPczTQlmL0XeT6by1XxI94ccDI/bXBnprRTcAJI9odHCQggaoZ62Jv93k KCEC3MDzccGXxPrBm4TZx1HlPXPIpOmHpM28n6CaBkNhj6vCKI+zbdOafLbvtd1fLdem Cn7w== X-Gm-Message-State: AOJu0YzRq8dW7mCtxQiRG6MuInc4+iQOvyZKFyWICbM907P+WxB6erfj PO0L6sNF/meelxuiZwEuw0fB/Vpu0sxMsjo1v5BqyBSkgdkuIEwJnr/L82HSykd6xrqagDLs8tx lJgyZMKBHGGDlQTelhM2DeEyL6ZNZIZRr42rB X-Received: by 2002:ac2:551c:0:b0:510:2582:5591 with SMTP id j28-20020ac2551c000000b0051025825591mr482052lfk.25.1706750549821; Wed, 31 Jan 2024 17:22:29 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231221-async-free-v1-1-94b277992cb0@kernel.org> In-Reply-To: From: Yosry Ahmed Date: Wed, 31 Jan 2024 17:21:52 -0800 Message-ID: Subject: Re: [PATCH] mm: swap: async free swap slot cache entries To: Chris Li Cc: Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, =?UTF-8?B?V2VpIFh177+8?= , =?UTF-8?B?WXUgWmhhb++/vA==?= , Greg Thelen , Chun-Tse Shao , =?UTF-8?Q?Suren_Baghdasaryan=EF=BF=BC?= , Brain Geffon , Minchan Kim , Michal Hocko , Mel Gorman , Huang Ying , Nhat Pham , Johannes Weiner , Kairui Song , Zhongkun He , Kemeng Shi , Barry Song Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jan 31, 2024 at 4:57=E2=80=AFPM Chris Li wrote: > > Hi Yosry, > > On Thu, Dec 28, 2023 at 7:34=E2=80=AFAM Yosry Ahmed wrote: > > > > On Thu, Dec 21, 2023 at 10:25=E2=80=AFPM Chris Li w= rote: > > > > > > We discovered that 1% swap page fault is 100us+ while 50% of > > > the swap fault is under 20us. > > > > > > Further investigation show that a large portion of the time > > > spent in the free_swap_slots() function for the long tail case. > > > > > > The percpu cache of swap slots is freed in a batch of 64 entries > > > inside free_swap_slots(). These cache entries are accumulated > > > from previous page faults, which may not be related to the current > > > process. > > > > > > Doing the batch free in the page fault handler causes longer > > > tail latencies and penalizes the current process. > > > > > > Move free_swap_slots() outside of the swapin page fault handler into = an > > > async work queue to avoid such long tail latencies. > > > > > > Testing: > > > > > > Chun-Tse did some benchmark in chromebook, showing that > > > zram_wait_metrics improve about 15% with 80% and 95% confidence. > > > > > > I recently ran some experiments on about 1000 Google production > > > machines. It shows swapin latency drops in the long tail > > > 100us - 500us bucket dramatically. > > > > > > platform (100-500us) (0-100us) > > > A 1.12% -> 0.36% 98.47% -> 99.22% > > > B 0.65% -> 0.15% 98.96% -> 99.46% > > > C 0.61% -> 0.23% 98.96% -> 99.38% > > > > I recall you mentioning that mem_cgroup_uncharge_swap() is the most > > expensive part of the batched freeing. If that's the case, I am > > curious what happens if we move that call outside of the batching > > (i.e. once the swap entry is no longer used and will be returned to > > the cache). This should amortize the cost of memcg uncharging and > > reduce the tail fault latency without extra work. Arguably, it could > > increase the average fault latency, but not necessarily in a > > significant way. > > > > Ying pointed out something similar if I understand correctly (and > > other operations that can be moved). > > If the goal is to let the swap fault return as soon as possible. Then > the current approach is better. > mem_cgroup_uncarge_swap() is only part of it. Not close to all of it. I think there are a lot of operations that we can move out of swapcache_free_entries(): - mem_cgroup_uncharge_swap() - arch_swap_invalidate_page() - zswap_invalidate() - clear_shadow_from_swap_cache() , and maybe others. I am curious, if we move these operations from the batched freeing, would this remove the increased tail latency and make it more consistent, without doing extra work? I believe this is what Ying was also asking about. > > > > > Also, if we choose to follow this route, I think there we should flush > > the async worker in drain_slots_cache_cpu(), right? > Not sure I understand this part. The drain_slots_cache_cpu(), will > free the entries already. The current lock around cache->free_lock > should protect async workers accessing the entries. What do you mean > by flushing? Never mind. I just realized that the percpu caches are static, so they are not freed in drain_slots_cache_cpu(). The NULL check in the async worker should be enough protection.