From patchwork Tue Nov 8 19:41:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Torvalds X-Patchwork-Id: 17187 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2912538wru; Tue, 8 Nov 2022 11:43:39 -0800 (PST) X-Google-Smtp-Source: AMsMyM7xBSvlsHBxfeaOFNudZHdoWW4gCazOTPsixaUngZX29xcEX760b7tQXQxuhPKWAQCNazf3 X-Received: by 2002:a17:902:e84d:b0:186:e777:982a with SMTP id t13-20020a170902e84d00b00186e777982amr58055984plg.83.1667936619141; Tue, 08 Nov 2022 11:43:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667936619; cv=none; d=google.com; s=arc-20160816; b=wgC8Pv/Gizy8tferUSjH3nF8/2Vbn9lE6f4yMPHYvNalevn00SYeGMLDL3RptJOx7L nZWtLilAB1TEI2SMYfs0cqNGSXf96vz0mVAPgwQ+vbw8WOgYH0uyYYYatTCkV+Obuj/x rjyZeIopQmlPWAm6G2d/5BBonjqgIgaHgM6BG+Ys9YLDxKXMfzUcqwBO3mqxzJ3fFobu gq1/EWT+sqCkSMFfFsf+RJb9VO8VTLTIKNgIDnJzBlaP2Lb+KxMP351ZOgpk+jJXjvmQ JWapGDtEtg27pdwY8sNNd2ufHbvG0AN0DwcmzwuMCaisEaF7GXWnjJoTBZEuqGYolKk6 BSjQ== 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=Hmz0BPz5K3mm7/cFxcxshgj5LagSPu177E0OJnHGgRM=; b=UrvPkBryFJb/BlPJq5JmYfn33xXGu/5y9QQjJhoOW5erY4OKRdDC/HKX5+gg9MkMzo JMLeDrX5x11B/Y1XpBFySwq3NLU8fFJ7BMjjT077Wcpig/fHi9E71H1N07Jni1/MnQPp aspcpiiUoVJcdMoDvQXApThAfaRWvwGYLsGtzMEw1D/EMkj+KnKnHr+8dALT9lkOPeVC Uh1qBm1RdmNGSAgKSd9Ua7r+rpq65HWptxFryH4xWLOzAw9rt7qwkdG78AHvL3dUOF5h nsufMPlao3F+VXag1D5BXVtqhFpIHOowmAocPYoDmalbwdom+nO7EdKTwe36RiQ+YZkL WCTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=yqjVAyZd; 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 j1-20020a170902c3c100b0018685c33ec2si13197529plj.7.2022.11.08.11.43.26; Tue, 08 Nov 2022 11:43:39 -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=yqjVAyZd; 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 S229698AbiKHTlz (ORCPT + 99 others); Tue, 8 Nov 2022 14:41:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229492AbiKHTlo (ORCPT ); Tue, 8 Nov 2022 14:41:44 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DB07716CA for ; Tue, 8 Nov 2022 11:41:43 -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 ams.source.kernel.org (Postfix) with ESMTPS id 0B946B81C1F for ; Tue, 8 Nov 2022 19:41:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D899C433D7; 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=bgbt2enUCqI8iRJ3RLKDZTlxwyLbgJ8OtsM1oeqQotc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yqjVAyZd9QMNx7zvF9RtUrYl2DUpE3QM9v79JHlipIOyabYoezYTfg7mvFoKlO5HA u+Be86X/jvDj085UDR3r30GnW1oO8qZQUTKVkgquJotczGNAYXFFbsqWbny7SRK37i dvVjGLO4A8tis77odWALXcqs8tdGEWCYKv256b/U= From: Linus Torvalds To: Hugh Dickins , Johannes Weiner , Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 2/4] mm: teach release_pages() to take an array of encoded page pointers too Date: Tue, 8 Nov 2022 11:41:37 -0800 Message-Id: <20221108194139.57604-2-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?1748958308406208174?= X-GMAIL-MSGID: =?utf-8?q?1748958308406208174?= release_pages() already could take either an array of page pointers, or an array of folio pointers. Expand it to also accept an array of encoded page pointers, which is what both the existing mlock() use and the upcoming mmu_gather use of encoded page pointers wants. Note that release_pages() won't actually use, or react to, any extra encoded bits. Instead, this is very much a case of "I have walked the array of encoded pages and done everything the extra bits tell me to do, now release it all". Also, while the "either page or folio pointers" dual use was handled with a cast of the pointer in "release_folios()", this takes a slightly different approach and uses the "transparent union" attribute to describe the set of arguments to the function: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html which has been supported by gcc forever, but the kernel hasn't used before. That allows us to avoid using various wrappers with casts, and just use the same function regardless of use. Signed-off-by: Linus Torvalds --- include/linux/mm.h | 21 +++++++++++++++++++-- mm/swap.c | 16 ++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8bbcccbc5565..d9fb5c3e3045 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1179,7 +1179,24 @@ static inline void folio_put_refs(struct folio *folio, int refs) __folio_put(folio); } -void release_pages(struct page **pages, int nr); +/** + * release_pages - release an array of pages or folios + * + * This just releases a simple array of multiple pages, and + * accepts various different forms of said page array: either + * a regular old boring array of pages, an array of folios, or + * an array of encoded page pointers. + * + * The transparent union syntax for this kind of "any of these + * argument types" is all kinds of ugly, so look away. + */ +typedef union { + struct page **pages; + struct folio **folios; + struct encoded_page **encoded_pages; +} release_pages_arg __attribute__ ((__transparent_union__)); + +void release_pages(release_pages_arg, int nr); /** * folios_put - Decrement the reference count on an array of folios. @@ -1195,7 +1212,7 @@ void release_pages(struct page **pages, int nr); */ static inline void folios_put(struct folio **folios, unsigned int nr) { - release_pages((struct page **)folios, nr); + release_pages(folios, nr); } static inline void put_page(struct page *page) diff --git a/mm/swap.c b/mm/swap.c index 955930f41d20..596ed226ddb8 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -968,22 +968,30 @@ void lru_cache_disable(void) /** * release_pages - batched put_page() - * @pages: array of pages to release + * @arg: array of pages to release * @nr: number of pages * - * Decrement the reference count on all the pages in @pages. If it + * Decrement the reference count on all the pages in @arg. If it * fell to zero, remove the page from the LRU and free it. + * + * Note that the argument can be an array of pages, encoded pages, + * or folio pointers. We ignore any encoded bits, and turn any of + * them into just a folio that gets free'd. */ -void release_pages(struct page **pages, int nr) +void release_pages(release_pages_arg arg, int nr) { int i; + struct encoded_page **encoded = arg.encoded_pages; LIST_HEAD(pages_to_free); struct lruvec *lruvec = NULL; unsigned long flags = 0; unsigned int lock_batch; for (i = 0; i < nr; i++) { - struct folio *folio = page_folio(pages[i]); + struct folio *folio; + + /* Turn any of the argument types into a folio */ + folio = page_folio(encoded_page_ptr(encoded[i])); /* * Make sure the IRQ-safe lock-holding time does not get