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: 17185 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2912360wru; Tue, 8 Nov 2022 11:43:05 -0800 (PST) X-Google-Smtp-Source: AMsMyM4/TQetrGJCjzMfORI2ipj9QdCgB4os+MUvIaxiCaixKrhEj61V4e63LDbpuRjs9A5fTyXO X-Received: by 2002:a17:902:e84f:b0:187:11a7:b1b with SMTP id t15-20020a170902e84f00b0018711a70b1bmr53290675plg.110.1667936585424; Tue, 08 Nov 2022 11:43:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667936585; cv=none; d=google.com; s=arc-20160816; b=nuOqEyMJJTBACuasKNppeFrMWjmikNkoGMxdQb3DcicOyhqH6IPr4ygKBJT/nJuRuv HHBZtttE5I8UIF4ExbNd2RA2raQFhxxbGVP8HjSGek7nQgMImdv6LtgKVfGSeSillMdx sIW3/j1Lqs4wwxEputGALaj8Ar5G7FdPnavRWnqFnQXaNXX2aSItomzy/Ip9qdavEgjV Cgs7vQyeFBO+i04fCxkrCSdm9Q5Rx0/+PS+Tkue8ovbry5+t1b+YB0BW0gv14e5t/+8F L1uEl12RNFJptkWuXBNeGWyyE7klibVnsUl37TXUBGnwG5TOKeMmDN86QOaCyHLwz5R3 pTMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8OI8czoi2mMUk9oMqCzHuAbL+4plIcZ8aIr0PXVvTsQ=; b=xN6HWa7SDtHgCAElQD2eLPfmsiAEPUvbdQnvMIAHNnDuKU6tA9OCvewPYCJ0yOHirS Yfc7XCKTk2/fn5kKMkVIgImM7gkvZnmCRun9/rzPgY9KmPOyHctrqLTL4g/TDTcMC3aY guIFBGttbZ32FQjHL9PSiHt8P+cBK2Cf49xJKE3/5gNJBeQ+n0w8Rg64q+xesmcBDql5 ePpAKASeQ5z3zWib7Mb30yRCpIl9Wkps14xcIs1vPzJ8iLQtA1WfkEa5Qa1s77BzWwEp gvtsddp3gJgWB2qdNwKz8aRbGk29YjzW3fG0mGEiJo/Gd5FqR2zso3hwJvNtGN8AscM0 rSFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=YFGak0II; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h1-20020a170902f54100b00186ef861484si18388833plf.500.2022.11.08.11.42.51; Tue, 08 Nov 2022 11:43:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=YFGak0II; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229593AbiKHTln (ORCPT + 99 others); Tue, 8 Nov 2022 14:41:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229492AbiKHTlm (ORCPT ); Tue, 8 Nov 2022 14:41:42 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C9B27057F for ; Tue, 8 Nov 2022 11:41:41 -0800 (PST) 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 12F3661778 for ; 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 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748958273313708342?= X-GMAIL-MSGID: =?utf-8?q?1748958273313708342?= 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.