From mboxrd@z Thu Jan 1 00:00:00 1970 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 31408C021B1 for ; Thu, 20 Feb 2025 10:59:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB1D14401CC; Thu, 20 Feb 2025 05:59:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B62304401AE; Thu, 20 Feb 2025 05:59:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B4A64401CC; Thu, 20 Feb 2025 05:59:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7E3274401AE for ; Thu, 20 Feb 2025 05:59:23 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0025B120F78 for ; Thu, 20 Feb 2025 10:59:19 +0000 (UTC) X-FDA: 83140026480.29.4965BCE Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 6E6CF4000C for ; Thu, 20 Feb 2025 10:59:17 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KRPc4sLP; spf=pass (imf11.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740049157; a=rsa-sha256; cv=none; b=0C1rMYoA4yd0r3et7EtUAm9IVIjKweUuvM6UsNxqpYmvEdMPyE8M8qi+t/gCp0ULGIOpoh VseALUs0Qjx0byl3049SMh4i6bqBQmTmBJfR6kpOA7aMhknXL9qK9EOaHJSRRFiiSMATkz ZDaG8+xsrR4uSyfXwFh8+lpTAAIDtTk= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KRPc4sLP; spf=pass (imf11.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740049157; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vnURZeq1DMt7nmY5gkvj+YkPNwMtqgNMrdcMQ0ZzI5Y=; b=bveAmzPcZVHbg6XyZ1RLeXXMK8jAwEeZPMcws3YWLtmWfRQ9Cxt0ZkMssl9nB/zBWXurVB StUeNn8wJkwmA6CPXLfTByzrEzC2D+U5IHpwNTMMg72sMZWZWPkDXjISXIbmyR8hnTtEI+ n7dnhRdKy4t/HAnD9Sxlw1PIujAJZ7M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740049156; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=vnURZeq1DMt7nmY5gkvj+YkPNwMtqgNMrdcMQ0ZzI5Y=; b=KRPc4sLPbXllax2HXkS8Uoh2IzFBOxCmJAXMG4QXDwpcn5JRTfpp0VxFbBr6lIb4SNtIcH IXVzFCBioW/dBc7MMwHkXjpOmBpRIZT0PJRA/jC4Ydgf4m8EUq1I0/KDro6DnKn9StlpyB nx6pTsK3Axiikm2bL7xI38hD6zycYXY= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-42-CbCiwbZsPUOW0bislVR_rw-1; Thu, 20 Feb 2025 05:59:15 -0500 X-MC-Unique: CbCiwbZsPUOW0bislVR_rw-1 X-Mimecast-MFC-AGG-ID: CbCiwbZsPUOW0bislVR_rw_1740049154 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4394b8bd4e1so3953495e9.0 for ; Thu, 20 Feb 2025 02:59:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740049154; x=1740653954; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=vnURZeq1DMt7nmY5gkvj+YkPNwMtqgNMrdcMQ0ZzI5Y=; b=qFPy/tERr2iH/ZJQwyfnbg6MRZKYMnpLbccd83Nz3b891kQJBLU2wIVjtBrf/wbUD3 /3rW3KLJvglVtzcGpphBMq/8aAzsGxiu9EBRw5jjAxxX/lEU2mXMvu4gvmp27rr2gAb1 hxMBUllJPuddhpessOpqk8B8jvxDbrlIrPzgNUzPAT0KtAd9L60Zl7acB8eOaKbZgaWl BRQn4jr+Ort2waZY7AVqxmJ54kZBbN/Dw/P8PSYxvta9/GJ0DH0J59+h1pStlSTzeFRQ nayo7FvrAa9ts3w06big/g0thMXZcUgoIJhJqhguOFJnnNWm9pkm//5KimcFwAyAIe5N YKTg== X-Forwarded-Encrypted: i=1; AJvYcCWHKyTjFyURvhVGGJCDSsnmc1mpnLqY2bzludl/GUKIo+47LsB/bQQb/jTm2r+MGleWgROTqduE8A==@kvack.org X-Gm-Message-State: AOJu0YymykXLYRfEVcVe9eqWiCeL08YN4H4/5Irm/w9GOe5TLA/gxDdz B7gLjaz0MaiBLQwGPAuC0pWjopfR1AtweibEU8INg0vTTZl6bDcyCM7YNBtEKsd3CkidbofUnDC SnSCIC3E5Vkq325yDFQPHxuyxyrK3RGF2n16aGvNLpCFCDZNt X-Gm-Gg: ASbGncvRFrrN9IwhN9RAcVCGiUZdZZ6Nn6F4fjsYtKCY5EkkewzmLbULWD6RpwnVpGp RXsVZIynuvG5+p1fIyxRaU+nvfDUAcKbweSlqtA++U7kc2cppWmtOgqVp8lpEfuA/u7mIbHp7bT xrLqC88KC+kmzwW5G1hdeUxOSqQFeA/wIBHBeAmVMqaT1I+sD0LHSLhcY7oeD9wVF+K53M+Gef+ 1ySVpcl/XNQ7F2Dr79yR/o0aMIneNAzlIWzYfSSZlC+zZ7l37mJbByEhdOWKGKv4gh2yvftX2NL 3lucSDr0/owUfWz0za3nT1YhTcGOTWgTBdrTijvEkkiASnjWrTexyHXvvGowdY/AeEuuRVId5AQ X+AeoErNNIstSD954hH+W3CYhtWkDyA== X-Received: by 2002:a5d:64a7:0:b0:38f:28a1:501e with SMTP id ffacd0b85a97d-38f33f1155fmr19019028f8f.8.1740049154363; Thu, 20 Feb 2025 02:59:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IHU1aZcxXD7wcuW3MDI2uNnpwIQ9SbwWHEXKdK9he1xcEMC4J1NN0hnygig+Cl0CN1Jbm7HTQ== X-Received: by 2002:a5d:64a7:0:b0:38f:28a1:501e with SMTP id ffacd0b85a97d-38f33f1155fmr19019010f8f.8.1740049153998; Thu, 20 Feb 2025 02:59:13 -0800 (PST) Received: from ?IPV6:2003:cb:c706:2000:e44c:bc46:d8d3:be5? (p200300cbc7062000e44cbc46d8d30be5.dip0.t-ipconnect.de. [2003:cb:c706:2000:e44c:bc46:d8d3:be5]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38f259d5e92sm20586344f8f.66.2025.02.20.02.59.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 20 Feb 2025 02:59:13 -0800 (PST) Message-ID: <4cb93166-29fd-4aea-965b-5dfb62d4dc8c@redhat.com> Date: Thu, 20 Feb 2025 11:59:11 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/4] mm: page_ext: add an iteration API for page extensions To: Luiz Capitulino , linux-kernel@vger.kernel.org, linux-mm@kvack.org, yuzhao@google.com, pasha.tatashin@soleen.com Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, muchun.song@linux.dev References: <3f0e058aef3951b39cf6bb4259c247352d4fe736.1739931468.git.luizcap@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5ic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TyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <3f0e058aef3951b39cf6bb4259c247352d4fe736.1739931468.git.luizcap@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 7VF4KSJQx-PBw-JwiL2vWZBu4Zt8fw2r-vzhYH3lTlk_1740049154 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 6E6CF4000C X-Stat-Signature: cuszz58mo4sx1hwzcr6sekn4ai6o9wy7 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1740049157-688738 X-HE-Meta: U2FsdGVkX1+Tv148IEenZ4vZt1BsdREGpz//l36MInjR72ouNyirJuIUxqcXcewvxw3QkPf2lZU2qMENhfUYdF2exoSt6tk9jSu4WjPi+9Oqx2/w3KQNmdFn0EAcdjBy+1o3v7XGxPSIGEin2mphw3mv7iNqkpg2N1zGW2nXmAGBLRdnNr22bcGy274xYLZ/hIC3iwhar8TiPauW3jh1feyKWjRDMxgSBqiflWPnsd7zOX1S4/ux9w7QM1PEnxtdB5oKAwVITL98VL1aif55bv24waEykCMM+E2cXnKPhpHn1hjuVgZHgnQjeRFd3R/8B60dJazwYaExs2rJ831Idy64Mv3B0wxDFByiYPRs/9Rns7096KEwttCZKG7QzyYR9XOB3C+GjznpBZIwLMBNq9CCoaI1EIRXMyfgfWdSN3foekm88T17wrcXKzAWOHlTkGIdH9AmX6h7mcmkHorIcx0VMHpFcCwJUveMBWRuMlMs970UotaIEi2/CxrWX8AlpE5DYBIVbRLFFZ/0vfyXojlRUzgThGUxsBQSzMmxXktOnB1yy/wckms4t/GOYzvCoMxDrSz57i/6nYfp6drr+5bMCXttk8/CMw/4m4A+YSEdVfSpA1hgtVWEJl8PcKPFNMpBZZxdkHv72zOqKq26r1fCvsqYEvWUdKf4AlOwb7YlQ2DEUFiA16Qmbobv8SjkcNDHMY0iS3uf77y6lhbmvXjd+/qzRG2AjF70VYrrzpFC6HqXkt187zjjxtxO4JbnpPyXSsDgAi5TzgL115076t97T/rJoR80RV7H+rB7ihrTf8RQMtxBwwKJgJLrwiNeOAq10LARLxsyOC1UeyUnWlpmYAgaK7ZqpUdFoEsRQQk5F8uetIGRjiN9maSoMOYUZUdevYaFOs9E0ssbmHx+A2AvCsNFXW9UVxdnR2eHgJRTSxzAHxmn9bJY7sbw4bitwL47+qfEhYusAcX8L1b n9nbSBzL J1Uf5YXJpaLJ6itCWO6YsYLaVOYAQORzRyIzMk34rBvpsApIyDds6bCH7YyMZw01Wz31tcxZTtHaNGqzz2OuaUnGohPsXi2kCsFzhuXW3R957gBVEOfbQkyzpyoxYCyxmxsF/YWbccsk/9WxQ97GQW/Lg+ruIvX4syqaTBQufR3o4UPVfxlA4yu25E8Yc+yYBSrlXwlh6yriDOAiAsxKKQYdt3F2WKLzTjK2/YpnQcUsUQ8AjmLjrNd9sUJAW6jqEX8sL2oHVR/2L5fdnUN9dknZJWDLJPm79j+hRb+21SmGNOzHsc5pv4odDSXOZgNrcp9X2g/vMXwD6Yji7wXSEe30YIEmsSDfmtrvFYP54fNEdwvYrFtqlSvv1ThHRXoSG3knWW/pYRLCpCxnenNhZce+IQ8ui95U1fO4rknMXoA8mfL0YabwErIdsl5p858L911AE 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: List-Subscribe: List-Unsubscribe: On 19.02.25 03:17, Luiz Capitulino wrote: > The page extension implementation assumes that all page extensions of > a given page order are stored in the same memory section. The function > page_ext_next() relies on this assumption by adding an offset to the > current object to return the next adjacent page extension. > > This behavior works as expected for flatmem but fails for sparsemem when > using 1G pages. The commit cf54f310d0d3 ("mm/hugetlb: use __GFP_COMP for > gigantic folios") exposes this issue, making it possible for a crash when > using page_owner or page_table_check page extensions. > > The problem is that for 1G pages, the page extensions may span memory > section boundaries and be stored in different memory sections. This issue > was not visible before commit cf54f310d0d3 ("mm/hugetlb: use __GFP_COMP > for gigantic folios") because alloc_contig_pages() never passed more than > MAX_PAGE_ORDER to post_alloc_hook(). However, the series introducing > mentioned commit changed this behavior allowing the full 1G page order > to be passed. > > Reproducer: > > 1. Build the kernel with CONFIG_SPARSEMEM=y and table extensions > support > 2. Pass 'default_hugepagesz=1 page_owner=on' in the kernel command-line > 3. Reserve one 1G page at run-time, this should crash (backtrace below) > > To address this issue, this commit introduces a new API for iterating > through page extensions. The main iteration loops are for_each_page_ext() > and for_each_page_ext_order(). Both must be called with the RCU read > lock taken. Here's an usage example: > > """ > struct page_ext_iter iter; > struct page_ext *page_ext; > > ... > > rcu_read_lock(); > for_each_page_ext_order(page, order, page_ext, iter) { > struct my_page_ext *obj = get_my_page_ext_obj(page_ext); > ... > } > rcu_read_unlock(); > """ > [...] > +struct page_ext *page_ext_iter_begin(struct page_ext_iter *iter, struct page *page); > +struct page_ext *page_ext_iter_next(struct page_ext_iter *iter); > + > +/** > + * page_ext_iter_get() - Get current page extension > + * @iter: page extension iterator. > + * > + * Return: NULL if no page_ext exists for this iterator. > + */ > +static inline struct page_ext *page_ext_iter_get(const struct page_ext_iter *iter) > +{ > + return iter->page_ext; > +} > + > +/** > + * for_each_page_ext(): iterate through page_ext objects. > + * @__page: the page we're interested in > + * @__pgcount: how many pages to iterate through > + * @__page_ext: struct page_ext pointer where the current page_ext > + * object is returned > + * @__iter: struct page_ext_iter object (defined in the stack) > + * > + * IMPORTANT: must be called with RCU read lock taken. > + */ > +#define for_each_page_ext(__page, __pgcount, __page_ext, __iter) \ > + __page_ext = page_ext_iter_begin(&__iter, __page); \ Doing stuff out of the loop is dangerous. Assume something does if (xxx) for_each_page_ext() Just move that inside the for(). for (__page_ext = page_ext_iter_begin(&__iter, __page), __iter.index = 0) > + for (__iter.index = 0; \ > + __page_ext && __iter.index < __pgcount; \ > + __page_ext = page_ext_iter_next(&__iter), \ > + __iter.index++) Hm, if we now have an index, why not turn iter.pfn -> iter.start_pfn, and only adjust the index in page_ext_iter_next? Then you can set the index to 0 in page_ext_iter_begin() and have here for (__page_ext = page_ext_iter_begin(&__iter, __page), __page_ext && __iter.index < __pgcount, __page_ext = page_ext_iter_next(&__iter);) A page_ext_iter_reset() could then simply reset the index=0 and lookup the page_ext(start_pfn + index) == page_ext(start_pfn) > + > +/** > + * for_each_page_ext_order(): iterate through page_ext objects > + * for a given page order > + * @__page: the page we're interested in > + * @__order: page order to iterate through > + * @__page_ext: struct page_ext pointer where the current page_ext > + * object is returned > + * @__iter: struct page_ext_iter object (defined in the stack) > + * > + * IMPORTANT: must be called with RCU read lock taken. > + */ > +#define for_each_page_ext_order(__page, __order, __page_ext, __iter) \ > + for_each_page_ext(__page, (1UL << __order), __page_ext, __iter) > + > #else /* !CONFIG_PAGE_EXTENSION */ > struct page_ext; > > diff --git a/mm/page_ext.c b/mm/page_ext.c > index 641d93f6af4c1..508deb04d5ead 100644 > --- a/mm/page_ext.c > +++ b/mm/page_ext.c > @@ -549,3 +549,44 @@ void page_ext_put(struct page_ext *page_ext) > > rcu_read_unlock(); > } > + > +/** > + * page_ext_iter_begin() - Prepare for iterating through page extensions. > + * @iter: page extension iterator. > + * @page: The page we're interested in. > + * > + * Must be called with RCU read lock taken. > + * > + * Return: NULL if no page_ext exists for this page. > + */ > +struct page_ext *page_ext_iter_begin(struct page_ext_iter *iter, struct page *page) > +{ > + iter->pfn = page_to_pfn(page); > + iter->page_ext = lookup_page_ext(page); > + > + return iter->page_ext; > +} > + > +/** > + * page_ext_iter_next() - Get next page extension > + * @iter: page extension iterator. > + * > + * Must be called with RCU read lock taken. > + * > + * Return: NULL if no next page_ext exists. > + */ > +struct page_ext *page_ext_iter_next(struct page_ext_iter *iter) > +{ > + if (WARN_ON_ONCE(!iter->page_ext)) > + return NULL; > + > + iter->pfn++; > +> + if (page_ext_iter_next_fast_possible(iter->pfn)) { > + iter->page_ext = page_ext_next(iter->page_ext); > + } else { > + iter->page_ext = lookup_page_ext(pfn_to_page(iter->pfn)); > + } > + > + return iter->page_ext; > +} We now always have a function call when calling into page_ext_iter_next(). Could we move that to the header and rather expose lookup_page_ext() ? -- Cheers, David / dhildenb