Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp6095067pxv; Thu, 29 Jul 2021 06:25:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrZvG75Of7VmPJv2qBB+6k+SWQdns6bvt1i1mTmn2d71pjwCIAv16t/iTn2GErq9qLfMFz X-Received: by 2002:a6b:2b44:: with SMTP id r65mr4041425ior.99.1627565129627; Thu, 29 Jul 2021 06:25:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627565129; cv=none; d=google.com; s=arc-20160816; b=HWmjQXiVTmu0tVePe7DiSuNK6D6qF1Cna3d74xjhjisv2T+SOx2ZEMKYGuHzE0b1ad 85rPtoT4hF2usnaHgwODtOhHs/kwWdJxD82UTYJK7Opsvhf3o1+mITJefeTFMdLxkYWR StvJoOeOcH1vM1YKrFt+DlBv8rgiGYLfNrI83ZjyjFI+DwhNPF28YN1aXk2drDJ9NUE8 bIIIaq+toH0xqKroRwtOJlRUROo5OpsZJ+w7VIgduhZ4iCzsw1kujzuvDl5SZ6YEbZZe hIcxJQsHrgzVzoAQjBrg4vwiou9lEnwpyhy3A+BLFLE7A89cPYvF2X0/tP8hPJn8TZRA Jm+g== 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:dkim-signature; bh=3gUPVUIRlB3Nzjm1zu5Oz0Bg1A+iOdg6QIHPOdB9S2c=; b=PLwhuoCFDkdyESdWepexeP3V2oJMY9TFIiDKHhNE/aLEIRK1uWeDfmCACiKE5kGbZo GQ+26lO4F2JI3ot/Fab8ZG2N0gtYwhQt++HIOJaPRSR/iCdjnDZvt0S/fJoz+jJKpWoG h6cfI7s+jUQaLptxiFVlkay8Cio2bJitT/99+SbVCi9LoOMOykkqUDzVwlLHen9NfNC0 b+52m6F7GG15KAC49eOc0RZ8wNsrszaeDWThNVMnPL+am1pzZuqCGgGI4McPogEgbviu lUOLoIlcIOIQJh7/dJraWHiKYpqPt+ItNQZgt+BidP1F9Fm/tftl3rlbHsLSZKm6uYib UXfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=bp5OfFeC; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z7si3293205ilq.32.2021.07.29.06.25.17; Thu, 29 Jul 2021 06:25:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=bp5OfFeC; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237922AbhG2NYJ (ORCPT + 99 others); Thu, 29 Jul 2021 09:24:09 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:38500 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237567AbhG2NVu (ORCPT ); Thu, 29 Jul 2021 09:21:50 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id EEDB4223D4; Thu, 29 Jul 2021 13:21:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1627564906; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3gUPVUIRlB3Nzjm1zu5Oz0Bg1A+iOdg6QIHPOdB9S2c=; b=bp5OfFeCiWWShjra8cx0OaS/SM4KFCAnd4Hhi8REnrd6tVu9+14czs1vp5MCGN+UbkPV8N y+gRUD75RAtT2ctvmdWkB/iOLTD8BHQohCLJf8/CgaZXl0RIqhy4Jf5QBagdilxthaCv9x +SOBelBDIif2+CSGhqc8OKoVMTwXVWw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1627564906; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3gUPVUIRlB3Nzjm1zu5Oz0Bg1A+iOdg6QIHPOdB9S2c=; b=iXncFUBby56FY3LbbbrGrLTbmc3oI/8lVZMGsA+SD+kuWNiG0cYs75uw1wahgo6azirCtg MUbuTp9MdPyMtJBQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C157C13AF4; Thu, 29 Jul 2021 13:21:46 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id SI2ILmqrAmF9AwAAMHmgww (envelope-from ); Thu, 29 Jul 2021 13:21:46 +0000 From: Vlastimil Babka To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Christoph Lameter , David Rientjes , Pekka Enberg , Joonsoo Kim Cc: Mike Galbraith , Sebastian Andrzej Siewior , Thomas Gleixner , Mel Gorman , Jesper Dangaard Brouer , Jann Horn , Vlastimil Babka Subject: [PATCH v3 34/35] mm, slub: use migrate_disable() on PREEMPT_RT Date: Thu, 29 Jul 2021 15:21:31 +0200 Message-Id: <20210729132132.19691-35-vbabka@suse.cz> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210729132132.19691-1-vbabka@suse.cz> References: <20210729132132.19691-1-vbabka@suse.cz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We currently use preempt_disable() (directly or via get_cpu_ptr()) to stabilize the pointer to kmem_cache_cpu. On PREEMPT_RT this would be incompatible with the list_lock spinlock. We can use migrate_disable() instead, but that increases overhead on !PREEMPT_RT as it's an unconditional function call. In order to get the best available mechanism on both PREEMPT_RT and !PREEMPT_RT, introduce private slub_get_cpu_ptr() and slub_put_cpu_ptr() wrappers and use them. Signed-off-by: Vlastimil Babka --- mm/slub.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 0fd60d9ca27e..91e04e20cf60 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -118,6 +118,26 @@ * the fast path and disables lockless freelists. */ +/* + * We could simply use migrate_disable()/enable() but as long as it's a + * function call even on !PREEMPT_RT, use inline preempt_disable() there. + */ +#ifndef CONFIG_PREEMPT_RT +#define slub_get_cpu_ptr(var) get_cpu_ptr(var) +#define slub_put_cpu_ptr(var) put_cpu_ptr(var) +#else +#define slub_get_cpu_ptr(var) \ +({ \ + migrate_disable(); \ + this_cpu_ptr(var); \ +}) +#define slub_put_cpu_ptr(var) \ +do { \ + (void)(var); \ + migrate_enable(); \ +} while (0) +#endif + #ifdef CONFIG_SLUB_DEBUG #ifdef CONFIG_SLUB_DEBUG_ON DEFINE_STATIC_KEY_TRUE(slub_debug_enabled); @@ -2805,7 +2825,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, if (unlikely(!pfmemalloc_match(page, gfpflags))) goto deactivate_slab; - /* must check again c->page in case IRQ handler changed it */ + /* must check again c->page in case we got preempted and it changed */ local_irq_save(flags); if (unlikely(page != c->page)) { local_irq_restore(flags); @@ -2864,7 +2884,8 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, } if (unlikely(!slub_percpu_partial(c))) { local_irq_restore(flags); - goto new_objects; /* stolen by an IRQ handler */ + /* we were preempted and partial list got empty */ + goto new_objects; } page = c->page = slub_percpu_partial(c); @@ -2880,9 +2901,9 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, if (freelist) goto check_new_page; - put_cpu_ptr(s->cpu_slab); + slub_put_cpu_ptr(s->cpu_slab); page = new_slab(s, gfpflags, node); - c = get_cpu_ptr(s->cpu_slab); + c = slub_get_cpu_ptr(s->cpu_slab); if (unlikely(!page)) { slab_out_of_memory(s, gfpflags, node); @@ -2965,12 +2986,12 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, * cpu before disabling preemption. Need to reload cpu area * pointer. */ - c = get_cpu_ptr(s->cpu_slab); + c = slub_get_cpu_ptr(s->cpu_slab); #endif p = ___slab_alloc(s, gfpflags, node, addr, c); #ifdef CONFIG_PREEMPT_COUNT - put_cpu_ptr(s->cpu_slab); + slub_put_cpu_ptr(s->cpu_slab); #endif return p; } @@ -3491,7 +3512,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, * IRQs, which protects against PREEMPT and interrupts * handlers invoking normal fastpath. */ - c = get_cpu_ptr(s->cpu_slab); + c = slub_get_cpu_ptr(s->cpu_slab); local_irq_disable(); for (i = 0; i < size; i++) { @@ -3537,7 +3558,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, } c->tid = next_tid(c->tid); local_irq_enable(); - put_cpu_ptr(s->cpu_slab); + slub_put_cpu_ptr(s->cpu_slab); /* * memcg and kmem_cache debug support and memory initialization. @@ -3547,7 +3568,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, slab_want_init_on_alloc(flags, s)); return i; error: - put_cpu_ptr(s->cpu_slab); + slub_put_cpu_ptr(s->cpu_slab); slab_post_alloc_hook(s, objcg, flags, i, p, false); __kmem_cache_free_bulk(s, i, p); return 0; -- 2.32.0