Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp452412pxb; Wed, 22 Sep 2021 06:00:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzq7fxIE+DAFoJQ1mBUc4bNBT3z9BterpWx7eAxZlsrBM7c607A2hQN1oXExLMdiaO5gqPL X-Received: by 2002:a02:ccb4:: with SMTP id t20mr4695565jap.84.1632315654269; Wed, 22 Sep 2021 06:00:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632315654; cv=none; d=google.com; s=arc-20160816; b=TMb9J49rK8ZPHqpN2MArm4bodX6TfX0WgkGbBOJF5xi3RsNEDeN25FnhtqMGN9SD+x XxNd1L/XPom1CDHRbxmz73c4pVtHHPr6JeDiFo8MHWmivla8/2ME0Mgzgky512G9rHAT qEFz8dkaTQdaZiboXQP9Puk2il6MSdU+XYrOel+pvmh/7/ME2BwnAULrhUhDnIISfvsd s0DgWNLTbTl935tTPtzzSyEsZJ5yTDDKBM9Nqjf+11TUjg3pBY7AGx0QxjHCbBpyxMrH /jM//ogqa3pPZN0jdyb0/xNOgiGcs+0xeWBa/evyISljCZgwluCg1XYad5bFhHmbChjF IN/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=gQmTeeI9SmVhpWXFHOOADd2pL7AYSuP57rY9o9YQggQ=; b=AZwlmyBuD9jiB99IPrC5poUus2D0cJVrZCtodYXT5O1gC5ixoihImwthtXxxdsLstc U9KNWd5pP8soAOrDozdH97SjmlOgAl+hyx6r4H6MSO88r3ZOPEivHeazasDE2wjRftqk 5w1B7VWK6f73Uei0DcECS6Tdd5QpzcT/LJ1nFkSTrjXT5DHjNS+df1uKLk9q9WS+F3Js botmadf5nS+EAR4h9HNCCuZpzjsLkP35Q90QQlDp/5Z0vynBkmbinIlyll4htbuTwTEP 3baolVKxviiENJdrkSTj8nruW15g0L4zOSUDjZ8N8QBcVBLJjYrVghRcJ8rInyHmzV1Y ZIEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20210112.gappssmtp.com header.s=20210112 header.b=Q2bgtXzB; 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 x8si2810718iom.11.2021.09.22.06.00.40; Wed, 22 Sep 2021 06:00:54 -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=@kernel-dk.20210112.gappssmtp.com header.s=20210112 header.b=Q2bgtXzB; 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 S229825AbhIVM7f (ORCPT + 99 others); Wed, 22 Sep 2021 08:59:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229609AbhIVM7f (ORCPT ); Wed, 22 Sep 2021 08:59:35 -0400 Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 176A7C061574 for ; Wed, 22 Sep 2021 05:58:05 -0700 (PDT) Received: by mail-io1-xd32.google.com with SMTP id b10so3169899ioq.9 for ; Wed, 22 Sep 2021 05:58:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.com; s=20210112; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=gQmTeeI9SmVhpWXFHOOADd2pL7AYSuP57rY9o9YQggQ=; b=Q2bgtXzBfU+xjTojiQA8LyZH3763j5d7nj2Ly2NI76so2njbQUvzg65LJZQ092Wy2Q VvI66chJiA5mW8ZKmmQsPO97CZjGqDrKG310YyKphE5+k8Rdf9i4Om2P4W1G37yidir4 UFEJ9aE6eZt/J6i4DDK5xKr3M5sI2V6LnQNKCaaWmfub3P6A/645tcH2MhHLZ0kGIUSL sncKoAU3GnfWsFwnJJ62SEtAdDfoxELXKNZtsRWqQg49LrjqyKg2k7w9qJMFrnVCEQTN kde1kyQFr20CU5Io6gXvfdSeoUqtEjyT8PN7Iy/RJBBqwLwYAW/Kaek+V/Odkc9++H9g h8+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=gQmTeeI9SmVhpWXFHOOADd2pL7AYSuP57rY9o9YQggQ=; b=IdWZT7GDJ38AqlRIlmsve1KqjJfl/W3hAEfDhSVduuOKr0JlRTGoYfdOVVi0dHIOoo APqQ/DbJsVK/35GIfCMoa6z+WEdP+PQbBO8gbqS/ZUdptZQQ3coawHrIy78/FAYo2zse Nv3GJ2bTMc2n4yldGvaCCCC4KkfSiWZ7TgSJ4iLPvpZ6aizV1JjjaZGc0W3kayC/itnQ GfybzQa++59tjDsVElrMMt+ETZ5ojdqgznCFLSoi3UGsAAvZks6mU7GNXsrO5le/RGOp DfmHOcDLl6eqGStXe/aD4nyXiOOTcUgDy48+N+H6No2AwQi7ZiB2M69qfrohXTcIpjH4 J2qQ== X-Gm-Message-State: AOAM531GEpl7rxNCeLMZ26m9U94Vi/inSIqGbX4t7wuGDMNSicivs3O7 vTqpjpJHP7nZUVU18cQLlhxVEA== X-Received: by 2002:a05:6638:1389:: with SMTP id w9mr4665060jad.138.1632315484395; Wed, 22 Sep 2021 05:58:04 -0700 (PDT) Received: from [192.168.1.116] ([66.219.217.159]) by smtp.gmail.com with ESMTPSA id y124sm996408iof.8.2021.09.22.05.58.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Sep 2021 05:58:04 -0700 (PDT) Subject: Re: [RFC v2 PATCH] mm, sl[au]b: Introduce lockless cache 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, Matthew Wilcox , John Garry , linux-block@vger.kernel.org, netdev@vger.kernel.org References: <20210920154816.31832-1-42.hyeyoo@gmail.com> <20210922081906.GA78305@kvm.asia-northeast3-a.c.our-ratio-313919.internal> From: Jens Axboe Message-ID: <688e6750-87e9-fb44-ce40-943bad072e48@kernel.dk> Date: Wed, 22 Sep 2021 06:58:00 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20210922081906.GA78305@kvm.asia-northeast3-a.c.our-ratio-313919.internal> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 9/22/21 2:19 AM, Hyeonggon Yoo wrote: > On Tue, Sep 21, 2021 at 09:37:40AM -0600, Jens Axboe wrote: >>> @@ -424,6 +431,57 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align, >>> } >>> EXPORT_SYMBOL(kmem_cache_create); >>> >>> +/** >>> + * 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); >>> + if (cache->size) >>> + return cache->queue[--cache->size]; >>> + else >>> + return NULL; >>> +} >>> +EXPORT_SYMBOL(kmem_cache_alloc_cached); > > Hello Jens, I'm so happy that you gave comment. > >> What I implemented for IOPOLL doesn't need to care about interrupts, >> hence preemption disable is enough. But we do need that, at least. > > To be honest, that was my mistake. I was mistakenly using percpu API. > it's a shame :> Thank you for pointing that. > > Fixed it in v3 (work in progress now) Another thing to fix from there, just make it: if (cache->size) return cached item return NULL; No need for an if/else with the return. > >> There are basically two types of use cases for this: >> >> 1) Freeing can happen from interrupts >> 2) Freeing cannot happen from interrupts >> > > I considered only case 2) when writing code. Well, To support 1), > I think there are two ways: > > a) internally call kmem_cache_free when in_interrupt() is true > b) caller must disable interrupt when freeing > > I think a) is okay, how do you think? If the API doesn't support freeing from interrupts, then I'd make that the rule. Caller should know better if that can happen, and then just use kmem_cache_free() if in a problematic context. That avoids polluting the fast path with that check. I'd still make it a WARN_ON_ONCE() as described and it can get removed later, hopefully. But note it's not just the freeing side that would be problematic. If you do support from-irq freeing, then the alloc side would need local_irq_save/restore() instead of just basic preempt protection. That would make it more expensive all around. > note that b) can be problematic with kmem_cache_free_bulk > as it says interrupts must be enabled. Not sure that's actually true, apart from being bitrot. >> How does this work for preempt? You seem to assume that the function is >> invoked with preempt disabled, but then it could only be used with >> GFP_ATOMIC. > > I wrote it just same prototype with kmem_cache_alloc, and the gfpflags > parameter is unnecessary as you said. Okay, let's remove it in v3. Please also run some actual comparitative benchmarks on this, with a real workload. You also need an internal user of this, a stand-alone feature isn't really useful. It needs someone using it, and the benchmarks would be based on that (or those) use cases. Another consideration - is it going to be OK to ignore slab pruning for this? Probably. But it needs to be thought about. In terms of API, not sure why these are separate functions. Creating the cache with SLAB_FOO should be enough, and then kmem_cache_alloc() tries the cache first. If nothing there, fallback to the regular path. Something like this would only be used for cases that have a high alloc+free rate, would seem like a shame to need two function calls for the case where you just want a piece of memory and the cache is empty. -- Jens Axboe