From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FA0B23FC41 for ; Sun, 3 May 2026 14:59:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777820390; cv=none; b=RfUpCRjPOe88AGAceooz1xDZSfnn4iQa7Ng2iLe83SQ7Jebko28nu/XOYFaCcX6cHJgTNm6uo0Zq6UrRZbW7XKJCFl3UKoEqpY2xzMb5sb+mK3A/hg3ZWFcI3/VcxAHujS1M/t8hPcnAd5svGxNe5By78yoCvRMvU/rw0Jxade8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777820390; c=relaxed/simple; bh=qd+6m4qTwQ++ioMS8gFbxaokMWHNwz5bq5v1CdNG3LE=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=MiX3rEwdSlHJpgZuTOMkvuCN4pWr4wjI7C36JLbaUKMSBkgbikhYUxUR5yHuMRschJJrp88fgd9Oixi4iloQMWZKDUr1yWlCx9iY87LNGtjCd6+r9G/gt99CsTHwuZHNHbwmkmyvyDXQgol9TDFPj3MVgqJlJnT7vk4dB7cjRcQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=XBKD6Gfd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="XBKD6Gfd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A39A7C2BCB4; Sun, 3 May 2026 14:59:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1777820389; bh=qd+6m4qTwQ++ioMS8gFbxaokMWHNwz5bq5v1CdNG3LE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=XBKD6Gfd5BTPGg8/bdEfNKvUOJkpXmg9TRc6RFjcCMacqkeMHrkrJrC3wlPDm2uJy SZMe6dox0ys34wBPeN6gaXqMPzEYYl5JpMCOWQOISRJBi0GUh+dSok7nnz4WKSJbzi Vm4uWYL5K/qiGniHPJ7j3KdCToV6ksgBKNz1htLY= Date: Sun, 3 May 2026 07:59:44 -0700 From: Andrew Morton To: Cc: , , , , , Subject: Re: [PATCH v4 0/5] KSM: Optimizations for rmap_walk_ksm Message-Id: <20260503075944.0c19e048e7e20f6715d9e86e@linux-foundation.org> In-Reply-To: <20260503203538194jFwVGloy43M1F3sQGaFt7@zte.com.cn> References: <20260503203538194jFwVGloy43M1F3sQGaFt7@zte.com.cn> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Sun, 3 May 2026 20:35:38 +0800 (CST) wrote: > Deep investigation revealed that rmap_walk_ksm's 99.9% of iterations inside > the anon_vma_interval_tree_foreach loop are skipped due to the first check > "if (addr < vma->vm_start || addr >= vma->vm_end)), indicating that a large > number of loop iterations are ineffective. This inefficiency arises because > the pgoff_start and pgoff_end parameters passed to > anon_vma_interval_tree_foreach span the entire address space from 0 to > ULONG_MAX, resulting in very poor loop efficiency. > > An initial immature thought was using the "rmap_item->address >> PAGE_SHIFT" > to be the pgoff passed into anon_vma_interval_tree_foreach(). But this is > flawed because when a range has been mremap-moved, when its anon folio > indexes and anon_vma pgoff correspond to the original user address, > not to the current user address, which was pointed out at: > > https://lore.kernel.org/all/02e1b8df-d568-8cbb-b8f6-46d5476d9d75@google.com/ > > According to the implementation of anon_vma_interval_tree_foreach — > it essentially iterates to find a suitable VMA such that the provided pgoff falls > within the VMA's range [vm_pgoff, vm_pgoff + vma_pages(v) - 1]. > > So the solution is to add vm_pgoff field in ksm_rmap_item and use vm_pgoff instead of > address >> PAGE_SHIFT. Thanks for pushing ahead with this. Regarding the [4/5] changelog: I don't think I understand how much effect this change has upon real-world workloads. Are you able to clarify that? "How useful is this change to Linux users". AI review had a lot to say: https://sashiko.dev/#/patchset/20260503203538194jFwVGloy43M1F3sQGaFt7@zte.com.cn Human review was wondering how much overhead [1/5] would add. I do note that it adds overhead even when CONFIG_TRACING=n - the rmap.o text segment gets a few hundred bytes larger and there's additional runtime overhead.