Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp651599pxt; Thu, 5 Aug 2021 08:26:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxj4xzEhqKpmr1NxCT2ayTZYMDvoLOfG0xsOF8fdjrLQN9kOzgWEFhcnCcDdlmvph+HPRqC X-Received: by 2002:aa7:cd18:: with SMTP id b24mr7167435edw.171.1628177196723; Thu, 05 Aug 2021 08:26:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628177196; cv=none; d=google.com; s=arc-20160816; b=TuumA1BjF9JiyAPdvn/C2R1kfJlQ08f7mGuWub6IysoJMGY34l1W0IFJCeYlJIdfIu UIN9srPH5aChFjZgdvb+Ps9egh1zzn055n1bv6HP09BBqafglROgdi9sBX1yHKXtutCL srW10HHF55tq7QfINOC5FWzgjjUWeMWWoBz5nUCnfbDNZZ+7mTAObiL+vRdK+M1IFHB0 i63uBZHiYQ2jV6eCSJXn9FFJcsC6izsiceMFgyB/Xe1ja1fbnAyVT7vXbs7VG0TrJvNn B0aYOHcpBNuWzqsOz9z/s+FNPIMhd2ydwNv89zhfnGg+cnIEe9P0RLetLbapJL5qQxwF FNJg== 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=Mp7va9uuzrf1g813mTixCnoAulhXUk7Ef+KNQhzo+iI=; b=rzMFH0dNKFAzGTVziZznWLZ/CWWoMKQpF5WdjJJ1aiVIhXh+oTTqemVMsdCT5voR5R To+HNY6nVpJlM4DeBDN2NG2Yw4qcY4SqPyxPrJw/Gxz18/790nkP0sfCRIdi+/VFz9eW GKuy3AYqdUt6QIq7dDHnqGs96fJNVoaeFX2FNvNrzPnT0ONOrGSfMbm1cMArbpiMiyf8 Hv2ZSXDQC7PnX9TdO8kLd8eTdUOn2dIGRrsMOu59vVqI11T0nE3pRzWz8TXi/E4FqNVr lQRpSPtjzRFFV/W2ghpu9Y55Bc96mTmV8g0IVh/19xhZrWA7JrJ50pFINJIb/Iitca94 q3Fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=DdPwE4xC; 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 cx8si5735902edb.288.2021.08.05.08.26.12; Thu, 05 Aug 2021 08:26:36 -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=DdPwE4xC; 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 S242222AbhHEPYz (ORCPT + 99 others); Thu, 5 Aug 2021 11:24:55 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:34012 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241957AbhHEPUW (ORCPT ); Thu, 5 Aug 2021 11:20:22 -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 3AC3A223DC; Thu, 5 Aug 2021 15:20:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1628176806; 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=Mp7va9uuzrf1g813mTixCnoAulhXUk7Ef+KNQhzo+iI=; b=DdPwE4xCyKxqnAQHcaGImsIjD8YtlmON+ZLUKIBQTH+8Byuyr4rkXQFEaqrx2NqQPlJN5X lQdMLIAyY1POkQhOQatTrjfj+R22zhZMt6OLkZP5DbpPvHdJit9Sl3Zx8i3r+OkMUth/LP y1JutXUA3yk3EYh6YXlKKJOROntPwmc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1628176806; 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=Mp7va9uuzrf1g813mTixCnoAulhXUk7Ef+KNQhzo+iI=; b=AVWkjMMMkc0KwJ2qxJXDpV3kRc4XclSR/1mVazFtbIRodzfGGfdFGVTcRHRI0YlEE9B5Ws wM6X5iHvWT6KKEAA== 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 0EAB713DAC; Thu, 5 Aug 2021 15:20:06 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id kASWAqYBDGFDdQAAMHmgww (envelope-from ); Thu, 05 Aug 2021 15:20:06 +0000 From: Vlastimil Babka To: Andrew Morton , Christoph Lameter , David Rientjes , Pekka Enberg , Joonsoo Kim Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Mike Galbraith , Sebastian Andrzej Siewior , Thomas Gleixner , Mel Gorman , Jesper Dangaard Brouer , Jann Horn , Vlastimil Babka Subject: [PATCH v4 11/35] mm, slub: simplify kmem_cache_cpu and tid setup Date: Thu, 5 Aug 2021 17:19:36 +0200 Message-Id: <20210805152000.12817-12-vbabka@suse.cz> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210805152000.12817-1-vbabka@suse.cz> References: <20210805152000.12817-1-vbabka@suse.cz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In slab_alloc_node() and do_slab_free() fastpaths we need to guarantee that our kmem_cache_cpu pointer is from the same cpu as the tid value. Currently that's done by reading the tid first using this_cpu_read(), then the kmem_cache_cpu pointer and verifying we read the same tid using the pointer and plain READ_ONCE(). This can be simplified to just fetching kmem_cache_cpu pointer and then reading tid using the pointer. That guarantees they are from the same cpu. We don't need to read the tid using this_cpu_read() because the value will be validated by this_cpu_cmpxchg_double(), making sure we are on the correct cpu and the freelist didn't change by anyone preempting us since reading the tid. Signed-off-by: Vlastimil Babka Acked-by: Mel Gorman --- mm/slub.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index c32048353645..e2d803b7e3b5 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2865,15 +2865,14 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, * reading from one cpu area. That does not matter as long * as we end up on the original cpu again when doing the cmpxchg. * - * We should guarantee that tid and kmem_cache are retrieved on - * the same cpu. It could be different if CONFIG_PREEMPTION so we need - * to check if it is matched or not. + * We must guarantee that tid and kmem_cache_cpu are retrieved on the + * same cpu. We read first the kmem_cache_cpu pointer and use it to read + * the tid. If we are preempted and switched to another cpu between the + * two reads, it's OK as the two are still associated with the same cpu + * and cmpxchg later will validate the cpu. */ - do { - tid = this_cpu_read(s->cpu_slab->tid); - c = raw_cpu_ptr(s->cpu_slab); - } while (IS_ENABLED(CONFIG_PREEMPTION) && - unlikely(tid != READ_ONCE(c->tid))); + c = raw_cpu_ptr(s->cpu_slab); + tid = READ_ONCE(c->tid); /* * Irqless object alloc/free algorithm used here depends on sequence @@ -3147,11 +3146,8 @@ static __always_inline void do_slab_free(struct kmem_cache *s, * data is retrieved via this pointer. If we are on the same cpu * during the cmpxchg then the free will succeed. */ - do { - tid = this_cpu_read(s->cpu_slab->tid); - c = raw_cpu_ptr(s->cpu_slab); - } while (IS_ENABLED(CONFIG_PREEMPTION) && - unlikely(tid != READ_ONCE(c->tid))); + c = raw_cpu_ptr(s->cpu_slab); + tid = READ_ONCE(c->tid); /* Same with comment on barrier() in slab_alloc_node() */ barrier(); -- 2.32.0