Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2385750pxb; Mon, 20 Sep 2021 20:59:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpa8zIcmSNDgOJ/nNJqVtNRH5m8Eq5gJe2Ciu/MfLMGPgDiiAr8gyKW5XxRjnt+3cnCZ/a X-Received: by 2002:a05:6402:1241:: with SMTP id l1mr32537480edw.123.1632196793858; Mon, 20 Sep 2021 20:59:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632196793; cv=none; d=google.com; s=arc-20160816; b=mG5DiMafFW9zI/nbyBP09DsdpRSdSbqL7ZLjZwWcL5hhGcFTnZQHj7mMDMl4kcKOk3 mFExbJEpcYdu+k3hBg1dh7t65RWPybjQlXq/8B6lJdOkO9Do8V/cWUhI+GLXhC0Q9Lu5 Qmbi6T3Ls/hFXvdTqoh0jsi0NglR7kwC9xC/JjUrDCjIj+06GuVLXnQZLoNwk9lThh4D N14bzqfHxExziHzi5HWz6TCE00PigFHOkG0kRhtfamOz1+jFAw48yfvbIQgjnamImt6v ng+4eIXddF4kICZWLtlYqDEGhzAr+9X3mr2zHjuQ8aKfdn06qginK8fVclOX+w68q78z k4RQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=yy0EwQ7J0sMHzSOPWcbFHFKHWeUukuqykjyt5I+NDUo=; b=c/urXQAJVWvVhpIujfzqkg+dMuccpRavKxFDY5ZJMG3YRsRO/QhlJOZeL3NDavaqOi 2qT7tyETtN8qdRUqTym7/GDQbnN4ML4y48tu3rZuy+pTHFMmkVJYQRRmw+00MptWOHX7 /7VsF13ZS2JgWNlhkg+S9nJXUoYsI3OWV4ThGiMiVTQ87B166ic/4Gk8bG32hUlbdOLF Yc1NlNU9gOMt6mgfaXyvbMAe+8I+1Fks1nFmKi4ZQhcLKknoLs/0HmqV8SB66pHW0fPS qtuxz5mud8jkokW6/aYQ8zf5HId69Pn50oFMOT/0req8VR1t2agYuLIasOa+cyxtoDWn AOIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=c8ZwljTj; 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 f3si5675031edc.98.2021.09.20.20.59.28; Mon, 20 Sep 2021 20:59:53 -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=@infradead.org header.s=casper.20170209 header.b=c8ZwljTj; 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 S243328AbhIUCGo (ORCPT + 99 others); Mon, 20 Sep 2021 22:06:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236374AbhIUBuK (ORCPT ); Mon, 20 Sep 2021 21:50:10 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 404BBC0698FE; Mon, 20 Sep 2021 15:02:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=yy0EwQ7J0sMHzSOPWcbFHFKHWeUukuqykjyt5I+NDUo=; b=c8ZwljTjtslpLLghi95Cqy1/Hk +Yu0waLRiRkg6H9rbFRN+VMHIJtVt7AIv0NX7aD2Zxg+WTo4aIrL6ma+oPWOwgeGusYXsw7OwAlkx IhrswmJoiAvUAOKqgARM9OaT+CpqaAzyIT2BvFmlN7YW3WBIlGIk0WIBo9/Hf/FH1h1W4s1o9ci1P Z6m7moIAwWub//99wTuoNA6EABZh8wGAsrjxhRp9aClp7olPq89oJmR2XyGPoBvK/TkysYHTP+R0d S5shbYSilOSOaPGSP+4crgkPbescejHhV9c5BIg0rX+4+BEgPCjsyo907BEdHB6bR7DnYGcILYA9x 9yOZyurw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mSRLg-003IEY-VT; Mon, 20 Sep 2021 22:01:23 +0000 Date: Mon, 20 Sep 2021 23:01:16 +0100 From: Matthew Wilcox To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , linux-kernel@vger.kernel.org, Jens Axboe , John Garry , linux-block@vger.kernel.org, netdev@vger.kernel.org Subject: Re: [RFC v2 PATCH] mm, sl[au]b: Introduce lockless cache Message-ID: References: <20210920154816.31832-1-42.hyeyoo@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210920154816.31832-1-42.hyeyoo@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 20, 2021 at 03:48:16PM +0000, Hyeonggon Yoo wrote: > +#define KMEM_LOCKLESS_CACHE_QUEUE_SIZE 64 I would suggest that, to be nice to the percpu allocator, this be one less than 2^n. > +struct kmem_lockless_cache { > + void *queue[KMEM_LOCKLESS_CACHE_QUEUE_SIZE]; > + unsigned int size; > +}; I would also suggest that 'size' be first as it is going to be accessed every time, and then there's a reasonable chance that queue[size - 1] will be in the same cacheline. CPUs will tend to handle that better. > +/** > + * kmem_cache_alloc_cached - try to allocate from cache without lock > + * @s: slab cache > + * @flags: SLAB flags > + * > + * Try to allocate from cache without lock. If fails, fill the lockless cache > + * using bulk alloc API > + * > + * Be sure that there's no race condition. > + * Must create slab cache with SLAB_LOCKLESS_CACHE flag to use this function. > + * > + * Return: a pointer to free object on allocation success, NULL on failure. > + */ > +void *kmem_cache_alloc_cached(struct kmem_cache *s, gfp_t gfpflags) > +{ > + struct kmem_lockless_cache *cache = this_cpu_ptr(s->cache); > + > + BUG_ON(!(s->flags & SLAB_LOCKLESS_CACHE)); > + > + if (cache->size) /* fastpath without lock */ > + return cache->queue[--cache->size]; > + > + /* slowpath */ > + cache->size = kmem_cache_alloc_bulk(s, gfpflags, > + KMEM_LOCKLESS_CACHE_QUEUE_SIZE, cache->queue); Go back to the Bonwick paper and look at the magazine section again. You have to allocate _half_ the size of the queue, otherwise you get into pathological situations where you start to free and allocate every time. > +void kmem_cache_free_cached(struct kmem_cache *s, void *p) > +{ > + struct kmem_lockless_cache *cache = this_cpu_ptr(s->cache); > + > + BUG_ON(!(s->flags & SLAB_LOCKLESS_CACHE)); > + > + /* Is there better way to do this? */ > + if (cache->size == KMEM_LOCKLESS_CACHE_QUEUE_SIZE) > + kmem_cache_free(s, cache->queue[--cache->size]); Yes. if (cache->size == KMEM_LOCKLESS_CACHE_QUEUE_SIZE) { kmem_cache_free_bulk(s, KMEM_LOCKLESS_CACHE_QUEUE_SIZE / 2, &cache->queue[KMEM_LOCKLESS_CACHE_QUEUE_SIZE / 2)); cache->size = KMEM_LOCKLESS_CACHE_QUEUE_SIZE / 2; (check the maths on that; it might have some off-by-one)