Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp745007lqp; Thu, 21 Mar 2024 14:22:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXwGMmGuAaCf/v2LJwSfUBAgEHcwu+mU6FfXLB/SIKlR79lA6iWKt3T5JwHH+hNu9AuxxPdTS98KwUdT+hMnqEUSw+uUVDARnZX5QKnKQ== X-Google-Smtp-Source: AGHT+IGhF6lg5CRzFxUbPFKm/2QoreVVnIgHX7GF8UtPQJxdPKGV9NttezVJ72AOowzkfpKyOhRg X-Received: by 2002:a05:6358:260b:b0:17b:dc2a:1d7b with SMTP id l11-20020a056358260b00b0017bdc2a1d7bmr585169rwc.0.1711056177076; Thu, 21 Mar 2024 14:22:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711056177; cv=pass; d=google.com; s=arc-20160816; b=mFGwDXz05y2r2+TIL1G2OTN8E86/NOsb4F9HNMdnAe0gac2Q+nfDkQ4H01b8JBh6Jq WYPaqAzDjA+jhn/DERnQUP8HqUakwNaZfM003SF4WasdslHYQKOgvI+SNqTdWmPlGR2p Rv0P/zu5bIBxLQ7JUvr4OLe2rJceEbNO+m7b45+o9hHrtk+VbitkIJSOIcjNDtGhKVpi VI8vS3Q28IlmNwnAjWLjWVxyX70iCGpXIu6/uMKXgeEOKNd4nsWGrXjmj7/QEJhiFtHc NRYv5u0vnETWhC3CF6Tu3CH7LgedyeSU4qcciT4mBZKwwL9Yxp5lPsfOzjGhqj0i7ag0 UAOA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=BYPwtyCYd5a4JYAUK9MQluNVapG0iz3U/+Y3po5f7DY=; fh=C2+oy2o8Rtf+ufUj+H6O9pn0vvGhHItULclPxyRzZ+U=; b=0GfC0a+BaYxPmLNB3FQRZPCbxBMN8cnySoyx5VwP4ImCzni+q4VKGd/y6QJz/KJLRt tllcHle+qCicTpF2q+N0ZrtJZvVQJsOKI2O2W2sbG/8JamFADCr2ygN04Cdel8j+8B9+ sNHrk2tqOYe94ChMksv1LdJn6Tn0UIozA4eh2CVm+nTYxVVaVF7GNqIRhOG1hj27LFrS QPFenmjoraTvp6OIDhnDIDzxglav//h+3x6EmCM4ktxb/4mZryjJ1f9ZPUbAWBSrmpal 2t4y5gM1FWFIwkWi/KMsqaZoIogq5/BXDwP5+DT0S+vkePrGPbjSLv1edHj8YtfAdy0g GhtA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="Ff7/4oH4"; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-110623-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110623-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b7-20020a631b47000000b005dc80f256ccsi537523pgm.849.2024.03.21.14.22.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 14:22:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110623-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="Ff7/4oH4"; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-110623-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110623-linux.lists.archive=gmail.com@vger.kernel.org" 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id CDCFB285CAE for ; Thu, 21 Mar 2024 20:00:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 82187134432; Thu, 21 Mar 2024 19:59:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="Ff7/4oH4" Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 2449613441F for ; Thu, 21 Mar 2024 19:59:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711051194; cv=none; b=atkUaQ5BJiVxPZKQgDqNWgU27ddIswvhh7m3pcabCwADosUUZuPgP5OFp4VQkKJklCXVbD7rQa8+iAVz5LZYvtIzaK5SS5wxb8rFZcIdwq49H/1ME4SwcHMeAXyO7k6cf7VlA3trFay+nwvVLQ3DXgFO21wQOCbtO3792cepsNs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711051194; c=relaxed/simple; bh=z0wA0KLIzdreobgSK4RcmRycyOJQ6P5PXTREgfAUx3U=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VPaEunieX/QVrlNtx4VL7qAB8pt42SmwSpmVPydYI7fXQOnsxDbf9DnVP+x8Hx5P9B+mlJE+znAwn8xFGX9B8LzVKMoNugcMlJIgL2oUQKpqv9Yti9Z+o/QuRsfsoyHHQQzsXpUTv8VbdVuJRzE2Krjnlp3wJi+ovf0CXAi8Vvc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=Ff7/4oH4; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=BYPwtyCYd5a4JYAUK9MQluNVapG0iz3U/+Y3po5f7DY=; b=Ff7/4oH45A2jTNadCdQVtTteKx GvDjUzmbI7lEkAktx9oWHQ81RLamkG6ExAICJUUhg155LXm0ylzDEHKTR9u0rI3DAEpkBIVGyZr4P NJe28imfM7bV3S2h8MfNysnm3habPiFSxr5enFrID713fhv8sUVVroA2OKSIsUQ7tsezkJK0d+gtp F3tPZwVPVxo7QQahLjompHhMnHd8jHkNjYGbg/X5c1QvJZT7EdNGzBkKXHBQ8XWs7ISCJKpY5vIiW ALjGJNPK+9zDOdGzkqj0fAtug+lqv9JTolICvWh99R/7Q4GE3vWl1bCP4GCXJSjcKFPT7ZSgHRadF NcIql1Ng==; Received: from willy by casper.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1rnOZn-00000007Tdd-1WHx; Thu, 21 Mar 2024 19:59:47 +0000 Date: Thu, 21 Mar 2024 19:59:47 +0000 From: Matthew Wilcox To: Svetly Todorov Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, gregory.price@memverge.com, wangkefeng.wang@huawei.com, akpm@linux-foundation.org, david@redhat.com, vbabka@suse.cz, naoya.horiguchi@linux.dev Subject: Re: [PATCH v3] kpageflags: respect folio head-page flag placement Message-ID: References: <20240320-kpageflags-svetly-v3-1-b6725843bfa7@memverge.com> 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=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Mar 21, 2024 at 12:08:01PM -0700, Svetly Todorov wrote: > > > > - if (PageKsm(page)) > > > > + if (mapping & PAGE_MAPPING_KSM) > > > > u |= 1 << KPF_KSM; > > > This might need an #ifdef? > > > Say mapping is movable and anon -- then (mapping & PAGE_MAPPING_KSM) is > > > true. Before, we called PageKsm, which falls through to a PG_ksm check. > > > If !CONFIG_KSM then that flag is always false. But now, we're liable to > > > report KPF_KSM even if !CONFIG_KSM. > > > > I'm not sure where you see a PG_ksm check: > > > > static __always_inline bool folio_test_ksm(const struct folio *folio) > > { > > return ((unsigned long)folio->mapping & PAGE_MAPPING_FLAGS) == > > PAGE_MAPPING_KSM; > > } > > > > static __always_inline bool PageKsm(const struct page *page) > > { > > return folio_test_ksm(page_folio(page)); > > } > My bad. What I meant was, if CONFIG_KSM is undefined, then > > > #ifdef CONFIG_KSM > > ... > > static __always_inline bool PageKsm(struct page *page) > > { > > return folio_test_ksm(page_folio(page)); > > } > > will fall through to > > > # else > > TESTPAGEFLAG_FALSE(Ksm, ksm) > > #endif > > And you're right -- there is no PG_ksm comparison -- > but the autogenerated PageKsm will always return false: Yes, that's true. Usually we care about this because we can optimise out large chunks of code if a config option (eg CONFIG_KSM) is disabled. In this case, we're talking about a couple of instructions, and it's generally not worth optimising those out in order to add an ifdef in the code. We've got quite a long way with Linux without it becoming overrun with ifdefs (compare, eg, the Mach source code), and long may that continue ;-) > > 00 file (or NULL) > > 01 anon > > 10 movable > > 11 KSM > > > > Perhaps it might be clearer to say that anon pages are inherently > > movable; the movable type really means that the reset of the mapping > > pointer refers to a movable_operations instead of a mapping or anon_vma. > I see. I misunderstood how the flags are applied. > I thought that 11 == (01 | 10) -- i.e. that KSM was an intersection of > MOVABLE and ANON. But they're more like mutually-exclusive states. And > I doubt that a page will end up in the KSM "state" if CONFIG_KSM is > disabled. So we don't need to rely on PageKsm() for the CONFIG_KSM > check. > > That said, won't > > if (mapping & PAGE_MAPPING_KSM) > > return true even if a mapping is ANON (01) or MOVABLE (10) > but not KSM (11)? Shouldn't this at least be > > if (mapping & PAGE_MAPPING_KSM == PAGE_MAPPING_KSM) Uh, yeah, that was a mistake. This should do the trick: if (is_anon) { u |= 1 << KPF_ANON; if (mapping & PAGE_MAPPING_KSM) u |= 1 << KPF_KSM; } (all KSM pages are reported as anon pages as well, both before and after this patch; see how folio_test_anon() only checks the bottom bit) > > I see your confusion. We have three cases; head, tail and neither > > (obviously a page is never both head & tail). If a page is neither, > > it's order-0 and it is the only page in the folio. So we handle head > > or neither in the first leg of the 'if' where we set KPF_COMPOUND_HEAD > > if PG_head is set, and tail in the 'else' leg. > > Dumb mistake on my part. For some reason, I thought that every > folio->page had its PG_head set. At this point, it's bad naming, but it's not worth the churn of fixing it; we have a better destination in mind, and we'll get there soon enough. > Cool! Thanks for bearing with me. Beyond the KSM stuff, my only > hangup is that this patch doesn't account for the handful of > remaining per-page flags (KPF_HWPOISON, KPF_ARCH_*). Should I > take this diff, tack those on in a second commit, and then put > up a v4? Forgive me, I'm very green to the kernel dev process... Oh, yes, that's a bug on my part. HWPOISON is definitely per-page, not per-folio (although the handling of it differs for hugetlb) and I haven't looked at the PG_arch gunk yet. We are trying to sliminate the per-page flags, because there's no space for them in the future (we'll have special handling for hwpoison because that really is very special)