From patchwork Wed Sep 20 19:02:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13393224 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 BACC0C04FED for ; Wed, 20 Sep 2023 19:03:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D6E06B019B; Wed, 20 Sep 2023 15:03:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4852A6B019D; Wed, 20 Sep 2023 15:03:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FFA66B019C; Wed, 20 Sep 2023 15:03:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1C7CD6B019A for ; Wed, 20 Sep 2023 15:03:12 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B7F1F40A95 for ; Wed, 20 Sep 2023 19:03:11 +0000 (UTC) X-FDA: 81257898582.09.D217A44 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf27.hostedemail.com (Postfix) with ESMTP id D12AB40030 for ; Wed, 20 Sep 2023 19:03:09 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lZeEgruS; spf=pass (imf27.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695236589; a=rsa-sha256; cv=none; b=COC2moICIiMRHqQenK0K2uuaPGJw8U4My9DEfXFl7cb3jnhRxN83Ls3WnAQg4Hp51mxs+4 xso2XfOq8aWkbmPZtuemS/8yg+0Q/P5+12mYnb6M8NLz8+6g+s1mH2ZzfBlg+cmVoj7YgY gWcBchHQuvQZrnTvwZKGB+mzDzG4Pi0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lZeEgruS; spf=pass (imf27.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.210.179 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=1695236589; 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=DHOkw8Z/lNeirjsVQWHKGHEmHDrpMBtO7DHMUSjWf4M=; b=oukbwGw1RDlTmzyGle9Q+tG5O2e5+PPKlx73IMIiLb5iigDSMDvgV8cirgK651cacnDbIy Sv8Sn/Ywwe/cCqhYmqnQUjhlenwgGIVDbsM5zVGiDIlai9B6obXggXuK++gzRiZIIDbU6N h5DrkRlaw5Z0f+scvUr2zpaurWAsZ0I= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-69101022969so99962b3a.3 for ; Wed, 20 Sep 2023 12:03:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695236588; x=1695841388; 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=DHOkw8Z/lNeirjsVQWHKGHEmHDrpMBtO7DHMUSjWf4M=; b=lZeEgruS3rNYs3tb8AFayOL8OMACgCcNQTpKFpfVCMvxEUBmfxlKWOl+qFF6Jnhwvh i8hgC3zic0dkOVCLojnsdKr0sto+FeqjQ3BHGb/9ornLVc20tF/5zSw29UJI/7PqpJM4 BI5wJf6K5UdM6L04SvPmRWLLheKKJtT2h8rnJOyakRn2jSC2+L7aCu+pP5XvacTMPjWA F8FogA2DTeSszsl1RQ9HKPkDMt2HxwGrptNZD8gsVm9vMEzfK4H7RoRlzlw9pfp1Ui9m BG67qkmI2Td1eRyKR8timCqp27N9cO4aVGwwIRc/xYzge2kEKZ+bkNgYUz1HgI4hNPAS DOJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695236588; x=1695841388; 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=DHOkw8Z/lNeirjsVQWHKGHEmHDrpMBtO7DHMUSjWf4M=; b=BgHK/mTC2gRymlBtqoDZiluIEt+i7y8fKyOYZfPKUGzyXgKzo5XCKHyww4UPvd3rfN YzkYnCyss2kwBqqOU4Rehi36KjB7+et6b07fg02brVUlPe4uOyHzMaLi5Wax/uehsw5X CsmuJmAxco8bgUzz6hjvIPmCfQMVVyvtCIPBfgbhb1XDzcLA1rIELdjZoNoWT5BLKqqG RA216gHBYGXgReSBGRtUXHu27nNE0rJxrXFulhjdssMn51ktY8U5rD96qBNAuqYbU9k1 /HyPWZR7imIPefh0Mp/d0nxV+7IFWKY6Tc5mqntpxm5eigjrwNAG6vZIaunjvD6Pl7Ar OmBA== X-Gm-Message-State: AOJu0YxFGFnyyvTR8sytO9YECFBf+0Gul4aTtCJG+8ZGSL62l72QuZSb S7ck22qtezxCi/MuaT4S19E2XWWo9Kw7NWjBAZ0= X-Google-Smtp-Source: AGHT+IELrwFtAaNAZSj3RuUKBX/6bRnBeBoGpeeG0jWHkgt6ISLm+F8uT2D8srjZchaOyGv6pWaVgg== X-Received: by 2002:a05:6a20:8e1f:b0:137:23a2:2b3c with SMTP id y31-20020a056a208e1f00b0013723a22b3cmr3637857pzj.49.1695236587894; Wed, 20 Sep 2023 12:03:07 -0700 (PDT) Received: from KASONG-MB2.tencent.com ([124.127.145.18]) by smtp.gmail.com with ESMTPSA id m5-20020aa78a05000000b006871fdde2c7sm423935pfa.110.2023.09.20.12.03.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 20 Sep 2023 12:03:07 -0700 (PDT) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Yu Zhao , Roman Gushchin , Johannes Weiner , Michal Hocko , Hugh Dickins , Nhat Pham , Yuanchu Xie , Kalesh Singh , Suren Baghdasaryan , "T . J . Mercier" , linux-kernel@vger.kernel.org, Kairui Song Subject: [RFC PATCH v3 2/6] workingset: move refault distance checking into to a helper Date: Thu, 21 Sep 2023 03:02:40 +0800 Message-ID: <20230920190244.16839-3-ryncsn@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230920190244.16839-1-ryncsn@gmail.com> References: <20230920190244.16839-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: D12AB40030 X-Stat-Signature: 1isit6qgejbm83adnkpkcpgiaoqqtdjz X-Rspam-User: X-HE-Tag: 1695236589-785163 X-HE-Meta: U2FsdGVkX1+CSbtZhyk72RwGHkvpg6jrqFlUIempwPOpiVhxTbLHOPEi10ACvzqYrehauQN/wyWOChW0CcocaW2eF5kV+cGGbY+U7OW9ByL0T9fP08yLTd50qH/u3elmASdwHyE/MU9BeYIBBqWRP1KnbAgH/pNgaNkCWppJzWqQYu3nRShpTq9ioFoghYbcRoV4xtqZa1KBsfAZnU+c+Od/bqs7B7VbquUcZdsN3DlERi4iFIby+0XFbO2ziGottRqs6FuZGW5f5TPymMgpAiA7h/6YG667pVWbdLegkC/iKkgQgZwP2D3JPBu+GvozzFEclDMw5sRkwcw5Q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4jgEnovy2eEzxaIKD9+Z5TrdUDUNb7BPGojjihUMptAHc0tMsbLyPhciRI5s8xfj3sFRs4i/R7QqNhs/0k6rqEqZ7cbQbHtCwAiEblLy+VBTukTSdXsSWcUY7lqL3BWiXUyo65n2NffbUQgGTVWHXiGEkKP/rvQPCEgH0+43OwCbl44oDG315klszm/883OC9gzLc84ZF0 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: From: Kairui Song There isn't any feature change, just move the refault distance checking logic into a standalone helper so it can be reused later. Signed-off-by: Kairui Song --- mm/workingset.c | 137 ++++++++++++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 58 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index 8613945fc66e..b0704cbfc667 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -170,9 +170,10 @@ */ #define WORKINGSET_SHIFT 1 -#define EVICTION_SHIFT ((BITS_PER_LONG - BITS_PER_XA_VALUE) + \ +#define EVICTION_SHIFT ((BITS_PER_LONG - BITS_PER_XA_VALUE) + \ WORKINGSET_SHIFT + NODES_SHIFT + \ MEM_CGROUP_ID_SHIFT) +#define EVICTION_BITS (BITS_PER_LONG - (EVICTION_SHIFT)) #define EVICTION_MASK (~0UL >> EVICTION_SHIFT) /* @@ -216,6 +217,79 @@ static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat, *workingsetp = workingset; } +/* + * Get the refault distance timestamp reading at eviction time. + */ +static inline unsigned long lru_eviction(struct lruvec *lruvec, + int bits, int bucket_order) +{ + unsigned long eviction = atomic_long_read(&lruvec->nonresident_age); + + eviction >>= bucket_order; + eviction &= ~0UL >> (BITS_PER_LONG - bits); + + return eviction; +} + +/* + * Calculate and test refault distance. + */ +static inline bool lru_test_refault(struct mem_cgroup *memcg, + struct lruvec *lruvec, + unsigned long eviction, bool file, + int bits, int bucket_order) +{ + unsigned long refault, distance; + unsigned long active, inactive_file, inactive_anon; + + eviction <<= bucket_order; + refault = atomic_long_read(&lruvec->nonresident_age); + + /* + * The unsigned subtraction here gives an accurate distance + * across nonresident_age overflows in most cases. There is a + * special case: usually, shadow entries have a short lifetime + * and are either refaulted or reclaimed along with the inode + * before they get too old. But it is not impossible for the + * nonresident_age to lap a shadow entry in the field, which + * can then result in a false small refault distance, leading + * to a false activation should this old entry actually + * refault again. However, earlier kernels used to deactivate + * unconditionally with *every* reclaim invocation for the + * longest time, so the occasional inappropriate activation + * leading to pressure on the active list is not a problem. + */ + distance = (refault - eviction) & (~0UL >> (BITS_PER_LONG - bits)); + + /* + * Compare the distance to the existing workingset size. We + * don't activate pages that couldn't stay resident even if + * all the memory was available to the workingset. Whether + * workingset competition needs to consider anon or not depends + * on having free swap space. + */ + active = lruvec_page_state(lruvec, NR_ACTIVE_FILE); + inactive_file = lruvec_page_state(lruvec, NR_INACTIVE_FILE); + + if (mem_cgroup_get_nr_swap_pages(memcg) > 0) { + active += lruvec_page_state(lruvec, NR_ACTIVE_ANON); + inactive_anon = lruvec_page_state(lruvec, NR_INACTIVE_ANON); + } else { + inactive_anon = 0; + } + + /* + * When there are already enough active pages, be less aggressive + * on reactivating pages, challenge an large set of established + * active pages with one time refaulted page may not be a good idea. + */ + if (active >= inactive_anon + inactive_file) + return distance < inactive_anon + inactive_file; + else + return distance < active + \ + (file ? inactive_anon : inactive_file); +} + #ifdef CONFIG_LRU_GEN static void *lru_gen_eviction(struct folio *folio) @@ -386,11 +460,10 @@ void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg) lruvec = mem_cgroup_lruvec(target_memcg, pgdat); /* XXX: target_memcg can be NULL, go through lruvec */ memcgid = mem_cgroup_id(lruvec_memcg(lruvec)); - eviction = atomic_long_read(&lruvec->nonresident_age); - eviction >>= bucket_order; + eviction = lru_eviction(lruvec, EVICTION_BITS, bucket_order); workingset_age_nonresident(lruvec, folio_nr_pages(folio)); return pack_shadow(memcgid, pgdat, eviction, - folio_test_workingset(folio)); + folio_test_workingset(folio)); } /** @@ -408,11 +481,6 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) { struct mem_cgroup *eviction_memcg; struct lruvec *eviction_lruvec; - unsigned long refault_distance; - unsigned long inactive_file; - unsigned long inactive_anon; - unsigned long refault; - unsigned long active; int memcgid; struct pglist_data *pgdat; unsigned long eviction; @@ -421,7 +489,6 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) return lru_gen_test_recent(shadow, file, &eviction_lruvec, &eviction, workingset); unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset); - eviction <<= bucket_order; /* * Look up the memcg associated with the stored ID. It might @@ -442,56 +509,10 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) eviction_memcg = mem_cgroup_from_id(memcgid); if (!mem_cgroup_disabled() && !eviction_memcg) return false; - eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat); - refault = atomic_long_read(&eviction_lruvec->nonresident_age); - /* - * Calculate the refault distance - * - * The unsigned subtraction here gives an accurate distance - * across nonresident_age overflows in most cases. There is a - * special case: usually, shadow entries have a short lifetime - * and are either refaulted or reclaimed along with the inode - * before they get too old. But it is not impossible for the - * nonresident_age to lap a shadow entry in the field, which - * can then result in a false small refault distance, leading - * to a false activation should this old entry actually - * refault again. However, earlier kernels used to deactivate - * unconditionally with *every* reclaim invocation for the - * longest time, so the occasional inappropriate activation - * leading to pressure on the active list is not a problem. - */ - refault_distance = (refault - eviction) & EVICTION_MASK; - - /* - * Compare the distance to the existing workingset size. We - * don't activate pages that couldn't stay resident even if - * all the memory was available to the workingset. Whether - * workingset competition needs to consider anon or not depends - * on having free swap space. - */ - active = lruvec_page_state(eviction_lruvec, NR_ACTIVE_FILE); - inactive_file = lruvec_page_state(eviction_lruvec, NR_INACTIVE_FILE); - - if (mem_cgroup_get_nr_swap_pages(eviction_memcg) > 0) { - active += lruvec_page_state(eviction_lruvec, - NR_ACTIVE_ANON); - inactive_anon = lruvec_page_state(eviction_lruvec, - NR_INACTIVE_ANON); - } else { - inactive_anon = 0; - } - - /* - * When there are already enough active pages, be less aggressive - * on reactivating pages, challenge an large set of established - * active pages with one time refaulted page may not be a good idea. - */ - if (active >= inactive_anon + inactive_file) - return refault_distance < inactive_anon + inactive_file; - else - return refault_distance < active + (file ? inactive_anon : inactive_file); + return lru_test_refault(eviction_memcg, eviction_lruvec, eviction, + file, EVICTION_BITS, bucket_order); } /**