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 E70A3E7717D for ; Wed, 11 Dec 2024 08:48:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47BCB8D0015; Wed, 11 Dec 2024 03:48:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 42C318D0013; Wed, 11 Dec 2024 03:48:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A5B68D0015; Wed, 11 Dec 2024 03:48:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0D2D68D0013 for ; Wed, 11 Dec 2024 03:48:39 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 679751C7426 for ; Wed, 11 Dec 2024 08:48:38 +0000 (UTC) X-FDA: 82882051560.21.E3BEBEC Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf15.hostedemail.com (Postfix) with ESMTP id 1EE10A0003 for ; Wed, 11 Dec 2024 08:48:10 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uFw6dxAw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="+nDOf/Yv"; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uFw6dxAw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="+nDOf/Yv"; spf=pass (imf15.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733906901; 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=WmwO8372NEjoF9AYeZ7LQ4p5KZ1MGCXRkHdnZ590kQI=; b=nd6C1yv7EG3Iy3uHdbFkCk8cXXnJUfO9jkoNGH/x2pA35FnVz/81vFR0Npd1c6FDF6DcC9 1/Fr3eXJDa6r54fbNHv1bIogF/rWqpbJKPv8BwA+8cOjQrCdn2U65WzHiGWfCwLe9e0/bb 1jssFWIol4mpqxbvcGMVjDBPQ7Eu5xw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733906901; a=rsa-sha256; cv=none; b=lY8Oc/sy8pOmnwKyoxxlCJbjkGZNKBisc8x1wHAOaZtttzA5mEEyJZYs2QwoleR9Co733z eAM/Hc4pJZ3drZyRlM2yThqF1iv+K5OCgsq3OVhQYR6qAIH5O7OlaJslWTXaZwjnANklq3 iMYZxdYeMXTQcO6FJycUJ8Xh2Pa+aG4= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uFw6dxAw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="+nDOf/Yv"; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uFw6dxAw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b="+nDOf/Yv"; spf=pass (imf15.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id F22B21F38F; Wed, 11 Dec 2024 08:48:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1733906914; h=from:from:reply-to: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=WmwO8372NEjoF9AYeZ7LQ4p5KZ1MGCXRkHdnZ590kQI=; b=uFw6dxAwSRLbjnrRgvwV+lv4vEc7lqDKFzOYEL7IHzTa/aVKExc21Bc2P/7GPiiF6v0kTZ xABZib2Y5GolAnkBcP0dI/XuJJf12azphBbeRRyvAv62J2Kgd0dY7Zi0Sgbmy01n9X0QhM EskvbqdGBGfBC0FH1B1xCwmgF8Oh1xg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1733906914; h=from:from:reply-to: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=WmwO8372NEjoF9AYeZ7LQ4p5KZ1MGCXRkHdnZ590kQI=; b=+nDOf/Yvg6XU0SApiJPanf0sn0+RIY2KV0DZg7h3avpBUMH+qFvnggcTVxDVBfozzCMF/x WoCgzmSjEMGGjPCg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1733906914; h=from:from:reply-to: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=WmwO8372NEjoF9AYeZ7LQ4p5KZ1MGCXRkHdnZ590kQI=; b=uFw6dxAwSRLbjnrRgvwV+lv4vEc7lqDKFzOYEL7IHzTa/aVKExc21Bc2P/7GPiiF6v0kTZ xABZib2Y5GolAnkBcP0dI/XuJJf12azphBbeRRyvAv62J2Kgd0dY7Zi0Sgbmy01n9X0QhM EskvbqdGBGfBC0FH1B1xCwmgF8Oh1xg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1733906914; h=from:from:reply-to: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=WmwO8372NEjoF9AYeZ7LQ4p5KZ1MGCXRkHdnZ590kQI=; b=+nDOf/Yvg6XU0SApiJPanf0sn0+RIY2KV0DZg7h3avpBUMH+qFvnggcTVxDVBfozzCMF/x WoCgzmSjEMGGjPCg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id CC8EE1344A; Wed, 11 Dec 2024 08:48:33 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id w3FpL+FRWWdcPQAAD6G6ig (envelope-from ); Wed, 11 Dec 2024 08:48:33 +0000 Message-ID: <7e16528b-466f-43f3-88e2-38fc3648a693@suse.cz> Date: Wed, 11 Dec 2024 09:48:33 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf-next v2 1/6] mm, bpf: Introduce __GFP_TRYLOCK for opportunistic page allocation Content-Language: en-US To: Alexei Starovoitov Cc: bpf , Andrii Nakryiko , Kumar Kartikeya Dwivedi , Andrew Morton , Peter Zijlstra , Sebastian Sewior , Steven Rostedt , Hou Tao , Johannes Weiner , shakeel.butt@linux.dev, Michal Hocko , Matthew Wilcox , Thomas Gleixner , Tejun Heo , linux-mm , Kernel Team References: <20241210023936.46871-1-alexei.starovoitov@gmail.com> <20241210023936.46871-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wm5VI EurRWKqENcDRjyo+ol59j0FViYysjQQeobXBDDE31t5SBg++veI6tXfpco/UiKEsDswL1WAr tEAZaruo7254TyH+gydURl2wJuzo/aZ7Y7PpqaODbYv727Dvm5eX64HCyyAH0s6sOCyGF5/p eIhrOn24oBf67KtdAN3H9JoFNUVTYJc1VJU3R1JtVdgwEdr+NEciEfYl0O19VpLE/PZxP4wX PWnhf5WjdoNI1Xec+RcJ5p/pSel0jnvBX8L2cmniYnmI883NhtGZsEWj++wyKiS4NranDFlA HdDM3b4lUth1pTtABKQ1YuTvehj7EfoWD3bv9kuGZGPrAeFNiHPdOT7DaXKeHpW9homgtBxj 8aX/UkSvEGJKUEbFL9cVa5tzyialGkSiZJNkWgeHe+jEcfRT6pJZOJidSCdzvJpbdJmm+eED w9XOLH1IIWh7RURU7G1iOfEfmImFeC3cbbS73LQEFGe1urxvIH5K/7vX+FkNcr9ujwWuPE9b 1C2o4i/yZPLXIVy387EjA6GZMqvQUFuSTs/GeBcv0NjIQi8867H3uLjz+mQy63fAitsDwLmR EP+ylKVEKb0Q2A== In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1EE10A0003 X-Stat-Signature: iw6hi465wip1xo1ihao77gi1cbfk1be9 X-Rspam-User: X-HE-Tag: 1733906890-270020 X-HE-Meta: U2FsdGVkX1/t2lHOH711vSjhuuGQuzVJt3xFBe1mPH+KOvbcoYfDGeKmwBKpKrDZWZzq/RYEGyR82SbVBViIC0b38JZK6HXFY9nAyN1SCdvAT5f7cBxw/O0pMmQXwL39pNcpSHSCQp4a4Ew9P5ORNSjpJyDb27iygwsAUZ9aZn1AcxbS0T8BhlwsNMKV4qd7iYwPf5fICY8hIRHlvKz81NEDmFqLQe1AFkCTgDY8XgDB47CUrVEG/FB3kSIFdpk6VDDGPIEC42HiGJsfyMzvlqaZbYf3/4/eaBZOskCMLDAJa2ato59L/VRt+b0j6x1RhAMUo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YthTyZHfSt1sc5zXE4oC1fGgdsFTUNUcqRcDwLbsOQByQvBuBgD7ZEbMqDTZyaGTLSiyMehj2tkkUYgefVreCmT3NmaRIJfuREy1D+1URzSSFknzI= 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 12/10/24 23:42, Alexei Starovoitov wrote: > On Tue, Dec 10, 2024 at 10:39 AM Vlastimil Babka wrote: >> >> On 12/10/24 03:39, Alexei Starovoitov wrote: >> > From: Alexei Starovoitov >> > >> > Tracing BPF programs execute from tracepoints and kprobes where running >> > context is unknown, but they need to request additional memory. >> > The prior workarounds were using pre-allocated memory and BPF specific >> > freelists to satisfy such allocation requests. Instead, introduce >> > __GFP_TRYLOCK flag that makes page allocator accessible from any context. >> > It relies on percpu free list of pages that rmqueue_pcplist() should be >> > able to pop the page from. If it fails (due to IRQ re-entrancy or list >> > being empty) then try_alloc_pages() attempts to spin_trylock zone->lock >> > and refill percpu freelist as normal. >> > BPF program may execute with IRQs disabled and zone->lock is sleeping in RT, >> > so trylock is the only option. >> > In theory we can introduce percpu reentrance counter and increment it >> > every time spin_lock_irqsave(&zone->lock, flags) is used, >> > but we cannot rely on it. Even if this cpu is not in page_alloc path >> > the spin_lock_irqsave() is not safe, since BPF prog might be called >> > from tracepoint where preemption is disabled. So trylock only. >> > >> > Note, free_page and memcg are not taught about __GFP_TRYLOCK yet. >> > The support comes in the next patches. >> > >> > This is a first step towards supporting BPF requirements in SLUB >> > and getting rid of bpf_mem_alloc. >> > That goal was discussed at LSFMM: https://lwn.net/Articles/974138/ >> > >> > Signed-off-by: Alexei Starovoitov >> >> I think there might be more non-try spin_locks reachable from page allocations: >> >> - in reserve_highatomic_pageblock() which I think is reachable unless this >> is limited to order-0 > > Good point. I missed this bit: > if (order > 0) > alloc_flags |= ALLOC_HIGHATOMIC; > > In bpf use case it will be called with order == 0 only, > but it's better to fool proof it. > I will switch to: > __GFP_NOMEMALLOC | __GFP_TRYLOCK | __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT Should work, yeah. >> - try_to_accept_memory_one() > > when I studied the code it looked to me that there should be no > unaccepted_pages. > I think you're saying that there could be unaccepted memory > from the previous allocation and trylock attempt just got unlucky > to reach that path? The unaccepted memory can exist after boot in confidential computing guests to speed up their boot, and then is accepted (encrypted for the guest) on demand. > What do you think of the following: > - cond_accept_memory(zone, order); > + cond_accept_memory(zone, order, alloc_flags); > > /* > * Detect whether the number of free pages is below high > @@ -7024,7 +7024,8 @@ static inline bool has_unaccepted_memory(void) > return static_branch_unlikely(&zones_with_unaccepted_pages); > } > > -static bool cond_accept_memory(struct zone *zone, unsigned int order) > +static bool cond_accept_memory(struct zone *zone, unsigned int order, > + unsigned int alloc_flags) > { > long to_accept; > bool ret = false; > @@ -7032,6 +7033,9 @@ static bool cond_accept_memory(struct zone > *zone, unsigned int order) > if (!has_unaccepted_memory()) > return false; > > + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) > + return false; > + Yeah that should be the straightforward fix. >> >> hope I didn't miss anything else especially in those other debugging hooks >> (KASAN etc) > > I looked through them and could be missing something, of course. > kasan usage in alloc_page path seems fine. > But for slab I found kasan_quarantine logic which needs a special treatment. > Other slab debugging bits pose issues too. > The rough idea is to do kmalloc_nolock() / kfree_nolock() that > don't call into any pre/post hooks (including slab_free_hook, > slab_pre_alloc_hook). > kmalloc_nolock() will pretty much call __slab_alloc_node() directly > and do basic kasan poison stuff that needs no locks. You'll probably want the memcg handling though? Alloc tagging would be good too. Both IIRC propagate the gfp flags when trying to allocate their metadata and can deal with failure so maybe it will work in a straightfoward way. Slab debugging will have to likely be limited, but it's also not handled in the pre/post hooks but deeper. > I will be going over all the paths again, of course. > > Thanks for the reviews so far!