From patchwork Tue Nov 8 19:41:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Torvalds X-Patchwork-Id: 13036782 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 7A27BC43217 for ; Tue, 8 Nov 2022 19:41:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1054A8E0005; Tue, 8 Nov 2022 14:41:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 063AD8E0001; Tue, 8 Nov 2022 14:41:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E20168E0005; Tue, 8 Nov 2022 14:41:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CE6EE8E0001 for ; Tue, 8 Nov 2022 14:41:43 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 572A0403EA for ; Tue, 8 Nov 2022 19:41:43 +0000 (UTC) X-FDA: 80111294886.23.273A6C2 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id E0BA340002 for ; Tue, 8 Nov 2022 19:41:41 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 086EA6176A; Tue, 8 Nov 2022 19:41:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A040C433D6; Tue, 8 Nov 2022 19:41:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1667936500; bh=RaRVKzz9Se8UHTQHVas3xIYN6fbXJd+owd3hvoJX9dA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YFGak0IIB8Z5v2pn7OjlJtEcV0XXhvmCHnNCNbLCzZ6G1whB9cFSvgeUhqv5SuDma P+4d868qE3obUsgf8P8GfisLxpybgkUMc691B5zG7EOEdcWmOsYtuQvIQMlOSXHEFG nKrvrW3TQmBTfeLCpKJ2Tpw9NnbfS61++EIdjMvA= From: Linus Torvalds To: Hugh Dickins , Johannes Weiner , Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 1/4] mm: introduce 'encoded' page pointers with embedded extra bits Date: Tue, 8 Nov 2022 11:41:36 -0800 Message-Id: <20221108194139.57604-1-torvalds@linux-foundation.org> X-Mailer: git-send-email 2.38.1.284.gfd9468d787 In-Reply-To: References: MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=YFGak0II; spf=pass (imf11.hostedemail.com: domain of torvalds@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=torvalds@linux-foundation.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667936502; a=rsa-sha256; cv=none; b=MLH3JVvbzU5vvoXSBDhmmqzhZPfmzJ036NLyAt7r0G1fX7fkWaQtbZuNGMIBHhbFkkFSwm MlNqGWU3L4P9QaF2aWffTz2xFlxcmV7UbcnziL7LuZF7LJKnTeRb4ulAPwANGCAOdPCcz3 0Oh3MkoFnf8L9I+ykaBxOeYyN7ZbOyM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667936502; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8OI8czoi2mMUk9oMqCzHuAbL+4plIcZ8aIr0PXVvTsQ=; b=v4Pij4B8j59mPoABGdxC4rUM17SSskwy60r3//v5DM3PNPic2PYpt0/nC5m8KI7/1QaINa zLeaKr9rVlby5wEj/qhq+Nof0s8UPkJB2wMRBolSC4pMtEjftwpjFkCgqx/yqOSWQ3Lhk3 BpdvwU5vwg7R85qNPrBtWWdoYwgSFvA= X-Stat-Signature: 3r7xinc16pm3ooygcr5kh8mkpkpcu4ha X-Rspamd-Queue-Id: E0BA340002 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=YFGak0II; spf=pass (imf11.hostedemail.com: domain of torvalds@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=torvalds@linux-foundation.org; dmarc=none X-Rspamd-Server: rspam05 X-Rspam-User: X-HE-Tag: 1667936501-68801 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: We already have this notion in parts of the MM code (see the mlock code with the LRU_PAGE and NEW_PAGE) bits, but I'm going to introduce a new case, and I refuse to do the same thing we've done before where we just put bits in the raw pointer and say it's still a normal pointer. So this introduces a 'struct encoded_page' pointer that cannot be used for anything else than to encode a real page pointer and a couple of extra bits in the low bits. That way the compiler can trivially track the state of the pointer and you just explicitly encode and decode the extra bits. Signed-off-by: Linus Torvalds --- include/linux/mm_types.h | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 500e536796ca..b5cffd250784 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -67,7 +67,7 @@ struct mem_cgroup; #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE #define _struct_page_alignment __aligned(2 * sizeof(unsigned long)) #else -#define _struct_page_alignment +#define _struct_page_alignment __aligned(sizeof(unsigned long)) #endif struct page { @@ -241,6 +241,37 @@ struct page { #endif } _struct_page_alignment; +/** + * struct encoded_page - a nonexistent type marking this pointer + * + * An 'encoded_page' pointer is a pointer to a regular 'struct page', but + * with the low bits of the pointer indicating extra context-dependent + * information. Not super-common, but happens in mmu_gather and mlock + * handling, and this acts as a type system check on that use. + * + * We only really have two guaranteed bits in general, although you could + * play with 'struct page' alignment (see CONFIG_HAVE_ALIGNED_STRUCT_PAGE) + * for more. + * + * Use the supplied helper functions to endcode/decode the pointer and bits. + */ +struct encoded_page; +#define ENCODE_PAGE_BITS 3ul +static inline struct encoded_page *encode_page(struct page *page, unsigned long flags) +{ + return (struct encoded_page *)(flags | (unsigned long)page); +} + +static inline bool encoded_page_flags(struct encoded_page *page) +{ + return ENCODE_PAGE_BITS & (unsigned long)page; +} + +static inline struct page *encoded_page_ptr(struct encoded_page *page) +{ + return (struct page *)(~ENCODE_PAGE_BITS & (unsigned long)page); +} + /** * struct folio - Represents a contiguous set of bytes. * @flags: Identical to the page flags.