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 X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69AC4C433C1 for ; Mon, 29 Mar 2021 17:21:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E23F061927 for ; Mon, 29 Mar 2021 17:21:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E23F061927 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5E78F6B007B; Mon, 29 Mar 2021 13:21:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5986A6B007E; Mon, 29 Mar 2021 13:21:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 438836B0080; Mon, 29 Mar 2021 13:21:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0199.hostedemail.com [216.40.44.199]) by kanga.kvack.org (Postfix) with ESMTP id 2624F6B007B for ; Mon, 29 Mar 2021 13:21:24 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id D355112E7 for ; Mon, 29 Mar 2021 17:21:23 +0000 (UTC) X-FDA: 77973578046.11.813F68F Received: from mail-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) by imf22.hostedemail.com (Postfix) with ESMTP id 8C0CEC000C61 for ; Mon, 29 Mar 2021 17:21:01 +0000 (UTC) Received: by mail-io1-f54.google.com with SMTP id b10so13551535iot.4 for ; Mon, 29 Mar 2021 10:21:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=iMWTErVQxa6Br5eKuQTUrDw3Akw8/5fDwCd4vZ5RQb8=; b=eeZ7HFfn0mYGmqZDr/HKDBcGvBCpORBR79OfJsTaN27WepgSnf6CNW29l82Olrfm2c yK5sSFFr4ZPIZjtq/r8T8jDALaxOQY/CLlq+v3wWPKso4LIeB6Y6uzcK8FQE1au9OM2Z 1+ePCgoQtI5VSfW9N2ABpSnggZJlZ4U4ltUJV08Wfmm9hKkTp4MebOiALzopMi9pJXrn ANsyK32jHaPEGKsKYT/dqIbuBTGWjL3BbtudGoePPlTgYReLwR7KZy5sXHYccDWA4eJ3 PDlwlOZ2U4GMjf04TG5oOSiau2+GqBcjp5a+2+VC6cRhljWPfOPew3CFmqq7/uakTBNh sNoA== X-Gm-Message-State: AOAM533qIrDEGQKVymU2rYhHCaKX7OXgxh97AkjenwKvqnR/O+uH0Pq9 jnKAvC21O1CdqfjMQxr3D5k= X-Google-Smtp-Source: ABdhPJxpxNa7tBKLkU+8bOLFCusW0dMTKwreFmJoWNbcB3LeiDx4h9RarXnIyWZYTp1hAbjohLSu4w== X-Received: by 2002:a6b:7501:: with SMTP id l1mr20733718ioh.92.1617038457633; Mon, 29 Mar 2021 10:20:57 -0700 (PDT) Received: from google.com (243.199.238.35.bc.googleusercontent.com. [35.238.199.243]) by smtp.gmail.com with ESMTPSA id x4sm4424915ilo.43.2021.03.29.10.20.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Mar 2021 10:20:57 -0700 (PDT) Date: Mon, 29 Mar 2021 17:20:55 +0000 From: Dennis Zhou To: Roman Gushchin Cc: Tejun Heo , Christoph Lameter , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH rfc 1/4] percpu: implement partial chunk depopulation Message-ID: References: <20210324190626.564297-1-guro@fb.com> <20210324190626.564297-2-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210324190626.564297-2-guro@fb.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8C0CEC000C61 X-Stat-Signature: rdm8ap7hcooq1q4hkky9po8xmo1q8iru Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf22; identity=mailfrom; envelope-from=""; helo=mail-io1-f54.google.com; client-ip=209.85.166.54 X-HE-DKIM-Result: none/none X-HE-Tag: 1617038461-956298 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: On Wed, Mar 24, 2021 at 12:06:23PM -0700, Roman Gushchin wrote: > This patch implements partial depopulation of percpu chunks. > > As now, a chunk can be depopulated only as a part of the final > destruction, when there are no more outstanding allocations. However > to minimize a memory waste, it might be useful to depopulate a > partially filed chunk, if a small number of outstanding allocations > prevents the chunk from being reclaimed. > > This patch implements the following depopulation process: it scans > over the chunk pages, looks for a range of empty and populated pages > and performs the depopulation. To avoid races with new allocations, > the chunk is previously isolated. After the depopulation the chunk is > returned to the original slot (but is appended to the tail of the list > to minimize the chances of population). > > Because the pcpu_lock is dropped while calling pcpu_depopulate_chunk(), > the chunk can be concurrently moved to a different slot. So we need > to isolate it again on each step. pcpu_alloc_mutex is held, so the > chunk can't be populated/depopulated asynchronously. > > Signed-off-by: Roman Gushchin > --- > mm/percpu.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 90 insertions(+) > > diff --git a/mm/percpu.c b/mm/percpu.c > index 6596a0a4286e..78c55c73fa28 100644 > --- a/mm/percpu.c > +++ b/mm/percpu.c > @@ -2055,6 +2055,96 @@ static void __pcpu_balance_workfn(enum pcpu_chunk_type type) > mutex_unlock(&pcpu_alloc_mutex); > } > > +/** > + * pcpu_shrink_populated - scan chunks and release unused pages to the system > + * @type: chunk type > + * > + * Scan over all chunks, find those marked with the depopulate flag and > + * try to release unused pages to the system. On every attempt clear the > + * chunk's depopulate flag to avoid wasting CPU by scanning the same > + * chunk again and again. > + */ > +static void pcpu_shrink_populated(enum pcpu_chunk_type type) > +{ > + struct list_head *pcpu_slot = pcpu_chunk_list(type); > + struct pcpu_chunk *chunk; > + int slot, i, off, start; > + > + spin_lock_irq(&pcpu_lock); > + for (slot = pcpu_nr_slots - 1; slot >= 0; slot--) { > +restart: > + list_for_each_entry(chunk, &pcpu_slot[slot], list) { > + bool isolated = false; > + > + if (pcpu_nr_empty_pop_pages < PCPU_EMPTY_POP_PAGES_HIGH) > + break; > + Deallocation makes me a little worried for the atomic case as now we could in theory pathologically scan deallocated chunks before finding a populated one. I wonder if we should do something like once a chunk gets depopulated, it gets deprioritized and then only once we exhaust looking through allocated chunks we then find a depopulated chunk and add it back into the rotation. Possibly just add another set of slots? I guess it adds a few dimensions to pcpu_slots after the memcg change. > + for (i = 0, start = -1; i < chunk->nr_pages; i++) { > + if (!chunk->nr_empty_pop_pages) > + break; > + > + /* > + * If the page is empty and populated, start or > + * extend the [start, i) range. > + */ > + if (test_bit(i, chunk->populated)) { > + off = find_first_bit( > + pcpu_index_alloc_map(chunk, i), > + PCPU_BITMAP_BLOCK_BITS); > + if (off >= PCPU_BITMAP_BLOCK_BITS) { > + if (start == -1) > + start = i; > + continue; > + } Here instead of looking at the alloc_map, you can look at the pcpu_block_md and look for a fully free contig_hint. > + } > + > + /* > + * Otherwise check if there is an active range, > + * and if yes, depopulate it. > + */ > + if (start == -1) > + continue; > + > + /* > + * Isolate the chunk, so new allocations > + * wouldn't be served using this chunk. > + * Async releases can still happen. > + */ > + if (!list_empty(&chunk->list)) { > + list_del_init(&chunk->list); > + isolated = true; Maybe when freeing a chunk, we should consider just isolating it period and preventing pcpu_free_area() from being able to add the chunk back to a pcpu_slot. > + } > + > + spin_unlock_irq(&pcpu_lock); > + pcpu_depopulate_chunk(chunk, start, i); > + cond_resched(); > + spin_lock_irq(&pcpu_lock); > + > + pcpu_chunk_depopulated(chunk, start, i); > + > + /* > + * Reset the range and continue. > + */ > + start = -1; > + } > + > + if (isolated) { > + /* > + * The chunk could have been moved while > + * pcpu_lock wasn't held. Make sure we put > + * the chunk back into the slot and restart > + * the scanning. > + */ > + if (list_empty(&chunk->list)) > + list_add_tail(&chunk->list, > + &pcpu_slot[slot]); > + goto restart; > + } > + } > + } > + spin_unlock_irq(&pcpu_lock); > +} > + > /** > * pcpu_balance_workfn - manage the amount of free chunks and populated pages > * @work: unused > -- > 2.30.2 >