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=-4.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 autolearn=no 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 1E2D4C4707F for ; Thu, 27 May 2021 22:14:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E67D060FE9 for ; Thu, 27 May 2021 22:14:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235854AbhE0WQE (ORCPT ); Thu, 27 May 2021 18:16:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233563AbhE0WQE (ORCPT ); Thu, 27 May 2021 18:16:04 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F768C061574 for ; Thu, 27 May 2021 15:14:30 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id n2so1397823wrm.0 for ; Thu, 27 May 2021 15:14:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=QpMa8N1CrsMoLXhCnjyLgiknkW1ExK55E/epiU8fJ2Y=; b=Vy+39f3FWYHvpT7rwfsnXe7HlMMaS8f5zAYNMmNb0wMmfkysZWnJDeFAnk9jItzcBR QQGAWoE89BTeY5IuEkJjtv8RhtEXCc7UL7iRMIeepk7uY4M8A16UdnUrRisv62fDlVJJ w4szsnoe6Q6iyRilCmjKklj4fCCZZBknIx/zvqdm8AFYnxxqOEhxT+57lu4RNFklUIBQ fIeMwqDAVby2LASPjMdYtqtwssDKwUuu0s2auSQREZjEqoBq84Qi1AazpBjT1GtK8Ol2 zMHQf6GQrIVMfDyyRViKvju2Yyajqd9f9Q1DiF2TJY200wBfmP/P8cOnclDnNVs4EMm+ a/aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; 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=QpMa8N1CrsMoLXhCnjyLgiknkW1ExK55E/epiU8fJ2Y=; b=LhmiOVN18ZuPJVQkSH9kUJraDRDU+I+bnu+Hx6oqBcGh4VZiCeTYTUh0g74Pd2bcJX gN2vAO7QahquTQY90xd/KPBcxrfDzpf9WCAng5ZaALmsHoTieo9qrtmlY9kVxXu/dfYM uzlz4CdWbbL0fzPq/aGykVKXxbCoFlW76QOQA0hDB9fVKYCqVeVqf1O5vpTuHIoJqhCi uyrlNWdbOZvKhi7JhkXDE+k7TUdA3PMyTV+tKMFHIPlR+fHJKn0HC4aAn6xqR+Fpex+L hN9MC0FQdKU5hmTXHhOhpex6Z46Wd09XZdoEf3ZM0NUEfkv4+p+W4ZcCEroYISWXLfMk 8Lxw== X-Gm-Message-State: AOAM533PNJBf4/w6JFocCaJkp9upuo1Yo5gmehfh6xqqIGUh2217knns dzpsCSBf0Y6hWLhu4W1sgavIv848Pg+zmGOc X-Google-Smtp-Source: ABdhPJzooGmfmZdv9tCoFMg3pDttjnLtuv1MphUTgqmAAEG1kywSq+JVLg2j+ireClzoipkWUBck4w== X-Received: by 2002:a05:6000:551:: with SMTP id b17mr5426750wrf.32.1622153668609; Thu, 27 May 2021 15:14:28 -0700 (PDT) Received: from [192.168.8.197] ([148.252.129.19]) by smtp.gmail.com with ESMTPSA id o129sm12339686wmo.22.2021.05.27.15.14.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 May 2021 15:14:28 -0700 (PDT) Subject: Re: [PATCH 10/13] io_uring: add helpers for 2 level table alloc To: Noah Goldstein Cc: Jens Axboe , "open list:IO_URING" References: <5290fc671b3f5db3ff2a20e2242dd39eba01ec1d.1621899872.git.asml.silence@gmail.com> From: Pavel Begunkov Message-ID: Date: Thu, 27 May 2021 23:14:20 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org On 5/27/21 10:43 PM, Noah Goldstein wrote: > On Mon, May 24, 2021 at 7:51 PM Pavel Begunkov wrote: >> -static void io_free_file_tables(struct io_file_table *table, unsigned nr_files) >> +static void io_free_page_table(void **table, size_t size) >> { >> - unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); >> + unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); >> >> for (i = 0; i < nr_tables; i++) >> - kfree(table->files[i]); >> - kfree(table->files); >> - table->files = NULL; >> + kfree(table[i]); >> + kfree(table); >> +} >> + >> +static void **io_alloc_page_table(size_t size) >> +{ >> + unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); >> + size_t init_size = size; >> + void **table; >> + >> + table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL); >> + if (!table) >> + return NULL; >> + >> + for (i = 0; i < nr_tables; i++) { >> + unsigned int this_size = min(size, PAGE_SIZE); >> + >> + table[i] = kzalloc(this_size, GFP_KERNEL); >> + if (!table[i]) { >> + io_free_page_table(table, init_size); >> + return NULL; > Unless zalloc returns non-NULL for size == 0, you are guranteed to do > this for size <= PAGE_SIZE * (nr_tables - 1). Possibly worth calculating early? Far from being a fast path, so would rather keep it simpler and cleaner > > If you calculate early you could then make the loop: > > for (i = 0; i < nr_tables - 1; i++) { > table[i] = kzalloc(PAGE_SIZE, GFP_KERNEL); > if (!table[i]) { > io_free_page_table(table, init_size); > return NULL; > } > } > > table[i] = kzalloc(size - (nr_tables - 1) * PAGE_SIZE, GFP_KERNEL); > if (!table[i]) { > io_free_page_table(table, init_size); > return NULL; > } > > Which is almost certainly faster. -- Pavel Begunkov