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 D6322EE0204 for ; Wed, 13 Sep 2023 21:07:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AD796B0296; Wed, 13 Sep 2023 17:07:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 45E1C6B0297; Wed, 13 Sep 2023 17:07:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34D786B0298; Wed, 13 Sep 2023 17:07:55 -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 246386B0296 for ; Wed, 13 Sep 2023 17:07:55 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F2F23C040A for ; Wed, 13 Sep 2023 21:07:54 +0000 (UTC) X-FDA: 81232811268.26.E6EE748 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 29DE280011 for ; Wed, 13 Sep 2023 21:07:52 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=OJieTzkW; dmarc=none; spf=pass (imf30.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694639273; 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=2Gj7JtBbGVJF2PnX+Z44IHAFP5KFsxdqajaxA8LzE3c=; b=RK8yFPbm1J9kFLEhgoAi10N9srTKwnX7ibFonr2DEp1mVcwP83s/iRUF2ZLRtXu367xmj+ 3FX8FC9ed+PVzSKPso9fPp+bS/Z0RRm/vyyJe4WaBg+pJZNMDulDuMCc2I/3aHd+kVM8Mg 7lKBI2AT+VJ6ojxhPG1MYltQlJ2COrM= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=OJieTzkW; dmarc=none; spf=pass (imf30.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694639273; a=rsa-sha256; cv=none; b=B7Y0VFg/Rp/v/DkXlZS5InyI7xy1jMsT0YZk5UPrvNRFOR1YHtbi+Zp1ENclQk7HCIQ8mW +QatKZP7DDn6DkHiIuA0y7tyNs/XrOJtW9b/S1GdUgcX0Q5MyD6rUUNnC4QDKtFVxvn+4W e/uNuD4DRLoCytGcI8YKhlYSPLBMWfY= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 37F4B61001; Wed, 13 Sep 2023 21:07:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6655AC433C8; Wed, 13 Sep 2023 21:07:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1694639271; bh=RNYP9+p+x6ZzoNqDo1JjEqeN5WDavOAss0TMtv0FSd8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=OJieTzkWepHpXecGf8t8Ijddd8IpFJqDWvCMhN8ZxENiV1Rs1sqhQ8mBITiN4qB1o 9n/MSZuMCni9rbepiJz+5kA1EPSxgJkipqfrU7zlJ3PGp7aBm9azwd30YnDrdEsVBb uI2Hp+QSxGL5jkTYgJtafMqpfmJJmWRMAQEVGlfo= Date: Wed, 13 Sep 2023 14:07:50 -0700 From: Andrew Morton To: Stefan Roesch Cc: kernel-team@fb.com, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v1 1/4] mm/ksm: add "smart" page scanning mode Message-Id: <20230913140750.616d3d87fe986a74d870b71f@linux-foundation.org> In-Reply-To: <20230912175228.952039-2-shr@devkernel.io> References: <20230912175228.952039-1-shr@devkernel.io> <20230912175228.952039-2-shr@devkernel.io> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspam-User: X-Stat-Signature: fiz6hykwaicrribeuqyrjz4rf7kywdny X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 29DE280011 X-HE-Tag: 1694639272-632903 X-HE-Meta: U2FsdGVkX1/SXRwZC+IFEooq+p5m0R3OPhrJ6EXVm9MEyYZclhGcIy2KL+Jen7UsL6h47qNapmdXy/J6ZXNGNT4UtElDK2j/LyGCNz9bmg9V5dMw+rufiyNmcVKucRctRSCNAR0d/ntlCrOugI4ddKSI1Y0ajCKH3Dq5iYr2+17N9WAOSKY638uwYvN8i2+czIMug+Cz/OwZ6r9ci0WbrkoY6nV8RgvY2K0JgGbBtQWwhYLTmm+PPTFeBtQOns7CU4nvL7NN8tXf/CVn727tjJ2snLUwjJZ7sBO0/vAt+F0qM0ATqOd8aP9xMEarw+/yQyyXxzzRWBoklGC1+n5/cT/yeQymWpNx5hkBrrgJFs/3Ci9irUBiVdM93uWpoERwIt7Jp9Gv0eLbNii6AHRfntlh/5+U+S7sDzX8oXOkt0g+UOA+SA2T4qP3vM62Yx8Eo8+SEZfVg2aLYtvh7hP8KhUrQri6hsPAVqzwIrtR0Ql+2OTx1hc7BNrfBCMy9cx68wlvrZbe9wm5qWzmn/gsdG+Z408e0CyJZRQE4j5gI4sQLrltIpWBEPY7VP/Kd6tZryspQ/9DZHaZNNRI8/wj15D6VI4w0oDBGgmMnfHx2nGkZrMTeXy3N3kWZWNYNXvIPNdf5ni1HMyXVRX4hlncLOTqj9m6rRUEGKZ0Y9JK5bylxl3yiPsNnf9jSQHpc/vi6V+AwqoXGrLiFyBbRKxuGjddDCEyf15AEXAkKCfIYe3P8z0rc9T9wE2vdLbX4WoALxXjekfT0FSm/pAJkWTkcDRE9nkCa1/lf3v+fw9eZny2qlCavZpmv7B5kwPTFsdICGA+/d+R8qeE1d0F1D2s1VW/5GQXp527Kk+lgkhj0i1TKudzMuueWSyTgTyFhkeR8pjnlabOzOTBrWlQtYN5qTxetQMLxQ4bjqrgYx27aAGiwz7SJ/l++51XAjGjifHi5obnkF0d7KVbIFdZwNH mCiiae+I 16nLYJs+JJY8POdLJck6ci9xhzCPtyn7mpvg+fzkCuL2pDswto7jmQ43tg3gQ6Fq2Co6bd+kxzOefcwYJqyLy5WMrmGiI9Z1P7jqquFLIffvojREsWod0DRXotoqJgYx47nFLBEDvlFICvwx4SZxLWcsQ7Bmbw0fkSnaJD+tYlzZBjZXk2k5GDkwk1G77BR44AdL2Y6yW6dvEA2MtpMzDBFFlZ4CsG59tM09VS/rlaZfYBi36qyGQxerQ2yGnqo4HKOqBqSN8NGZM1VWgQOK5pq9uneKQrJCXaHxWpg/TLV8e4cPFnXQGuLa4dMjMZTUlrY7Im404YVjyRMk= 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: On Tue, 12 Sep 2023 10:52:25 -0700 Stefan Roesch wrote: > This change adds a "smart" page scanning mode for KSM. So far all the > candidate pages are continuously scanned to find candidates for > de-duplication. There are a considerably number of pages that cannot be > de-duplicated. This is costly in terms of CPU. By using smart scanning > considerable CPU savings can be achieved. > > This change takes the history of scanning pages into account and skips > the page scanning of certain pages for a while if de-deduplication for > this page has not been successful in the past. > > To do this it introduces two new fields in the ksm_rmap_item structure: > age and skip_age. age, is the KSM age and skip_page is the age for how s/skip_page/skip_age/ > long page scanning of this page is skipped. The age field is incremented > each time the page is scanned and the page cannot be de-duplicated. > > How often a page is skipped is dependent how often de-duplication has > been tried so far and the number of skips is currently limited to 8. > This value has shown to be effective with different workloads. > > The feature is currently disable by default and can be enabled with the > new smart_scan knob. > > The feature has shown to be very effective: upt to 25% of the page scans > can be eliminated; the pages_to_scan rate can be reduced by 40 - 50% and > a similar de-duplication rate can be maintained. > All seems nice. I'll sit out v1, see what people have to say. Some nits: > --- a/mm/ksm.c > +++ b/mm/ksm.c > > ... > > @@ -2305,6 +2314,45 @@ static struct ksm_rmap_item *get_next_rmap_item(struct ksm_mm_slot *mm_slot, > return rmap_item; > } > > +static unsigned int inc_skip_age(rmap_age_t age) > +{ > + if (age <= 3) > + return 1; > + if (age <= 5) > + return 2; > + if (age <= 8) > + return 4; > + > + return 8; > +} "inc_skip_age" sounds like it increments something. Can we give it a better name? And a nice comment explaining its role in life. > +static bool skip_rmap_item(struct page *page, struct ksm_rmap_item *rmap_item) > +{ > + rmap_age_t age; > + > + if (!ksm_smart_scan) > + return false; > + > + if (PageKsm(page)) > + return false; > + > + age = rmap_item->age++; > + if (age < 3) > + return false; > + > + if (rmap_item->skip_age == age) { > + rmap_item->skip_age = 0; > + return false; > + } > + > + if (rmap_item->skip_age == 0) { > + rmap_item->skip_age = age + inc_skip_age(age); > + remove_rmap_item_from_tree(rmap_item); > + } > + > + return true; > +} Would a better name be should_skip_rmap_item()? But even that name implies that the function is idempotent (has no side-effects). Again, an explanatory comment would be good. And simple comments over each non-obvious `if' statement. > > ... >