From patchwork Thu Feb 16 17:47:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikulas Patocka X-Patchwork-Id: 13143580 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E814C61DA4 for ; Thu, 16 Feb 2023 17:47:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEF676B0071; Thu, 16 Feb 2023 12:47:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9FC06B0072; Thu, 16 Feb 2023 12:47:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D67BC6B0073; Thu, 16 Feb 2023 12:47:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C66236B0071 for ; Thu, 16 Feb 2023 12:47:15 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9C74281283 for ; Thu, 16 Feb 2023 17:47:15 +0000 (UTC) X-FDA: 80473886430.30.DEEAED5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf19.hostedemail.com (Postfix) with ESMTP id C39F21A0013 for ; Thu, 16 Feb 2023 17:47:13 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E5yauxGy; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of mpatocka@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mpatocka@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676569633; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=blWK0SRcDdae8keGRRM8FpEp32/QxHaadHljMinb1BI=; b=S6f0ow6u8kI0GP9Qhg45qDi+uK1i+bB44R7AP6KNqX59erZ1zFDTggKPemAl2/L7pkEBjm 7jRBFMiqDsRBZPNwb/Vh+j6PpwVKrn+El+xIZpHnmyYbEJ1a4AAQLA1Q40B6lZw6528LbM FEAhpq437BOG05MDtjsYPqqZmEm4v9E= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E5yauxGy; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of mpatocka@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mpatocka@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676569633; a=rsa-sha256; cv=none; b=DENId3Wqn+jlSlCn7YraMCM9o5sfsvEZ0aZkzTrhU3ZQlvywkWwbltsLKAsLWfHdoFQ+Vr VHZj88gBX84D3TObmOJk4aW0HLM5zUnHL+TsGJM+l43Venap3idqXUvMCOy3RsbNy0HwYw R737jibM3hjC/GUKwFEy7oEDIWtDLbE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676569633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=blWK0SRcDdae8keGRRM8FpEp32/QxHaadHljMinb1BI=; b=E5yauxGyGBqwVVz+eQ07j2ojzitg6vANjdTyenCHIe6084qe6BR8oN8hCW/WpDY7rPjbyN K1JGqztkbAwwNwGIylp3e3U0tfTvtwRwrMFYMNMIaXVn7QIYSDNTri3YrradIxiLbFLCor kSnVtBR3F3hrxzA+5B5vJ0zvvGzVLl8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-453-pr65g2nCNQuTPbjqXP78rA-1; Thu, 16 Feb 2023 12:47:10 -0500 X-MC-Unique: pr65g2nCNQuTPbjqXP78rA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4DA0985A5A3; Thu, 16 Feb 2023 17:47:08 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3CE0A2166B30; Thu, 16 Feb 2023 17:47:08 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 31GHl8ID024130; Thu, 16 Feb 2023 12:47:08 -0500 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 31GHl8mk024126; Thu, 16 Feb 2023 12:47:08 -0500 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Thu, 16 Feb 2023 12:47:08 -0500 (EST) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: snitzer@kernel.org, Yang Shi cc: mgorman@techsingularity.net, agk@redhat.com, dm-devel@redhat.com, akpm@linux-foundation.org, linux-block@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] dm-crypt: allocate compound pages if possible Message-ID: User-Agent: Alpine 2.21 (LRH 202 2017-01-01) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspamd-Queue-Id: C39F21A0013 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: swx44c41pctsgxix7fozpzwh4suer341 X-HE-Tag: 1676569633-194453 X-HE-Meta: U2FsdGVkX19Qg95ftExfZm8gEDaUZZ+PL0dkA+cuW1Di3gPQEMVSLfA1LuO9iMxQlAa4TKXsdjl8vqb8cHEISx3XC7iUQy2mDzLtHiRT3OkwzPbtG2o+KNKLodlR00FFNTJ+s6R8n7aQmx+kYpYOIkUPjwBzOSdKCTvJCU4CAdUfDjFJ9+fNYUguSb35zGrx+LqXGRK1WxuxX0S+Ick6ko0WZ4wFkAcaOV5IiURVaeG00OQNy61YgzgYXWzcsVTyU1GudSVfAydHlMILzFkSoPAsHShsromJVquH0Tnbr0/sVZxb5RumcxBOeUCN/fmkBxfCSubkjZGylIgkwwz0MF+oyeLRP8UuQlUnyutQBDJxjx72RrdJuEZdsoxYCedJcVJY5YtuHbqIUoNdViebR5TTA1TlO2h1v9WBxrB0WL/avsp4xkHgEW30a++jAPnewOMkEBZjvYd7v1FiX3gHNSxUFJzQtFRaayFJk7Nj/FKo4aVfg6vGVWMJ74QGZwEsLFrD4WgmwpxEU5TrYBqNiqHLe6WmbKzczQFvgN0fZl4xlxOlQK2QQaZDVjLullCJa0rh5I65dGZGOuqXO/rBjd/9dag6gmVOuOT4CnQ1qB2A5eLpxSYrTmJdGCRYyTG8XH9ojXbPTmQenWwt5CEqtv2z06aiHNtBjdq/5BtYWTc5J0JJdufuroIGJpyWWybnCDWoD8eREvEMXDiI9BkLuNmpM6aj+BPmKxJ32WztlseZN6iwcNHUF3hGUBlQygakXejU19vrHZEDZsePm3YKoXjpN29kXfH/fg2QEa6TYmh+ftinI6duE0Aqvg0VgWw8BCHwhvuffsCFab+2nO9szCRPGJiQ8uYxujq82Z608mNQ4Q3t1kPyInfGkgYApTaXuwnpidpoY4owZbNzca4V+v9Dp571Ewqk4ux2GtyB9RiX+elkk8fW4mLiZQn9Hx9fBBQRDeHcKmbPEiOPEuC j8Nusjde XrqvMHOcCKRVxGHAzwAaUyPTCuzTkqBF8lwccjqWNExmwrUvQFdwMswpOTEyfC3uObPGH0DxQZRcYtdBsYiCHZchtexIiSYh78vdBHl9DK5jyZ5eHstiWmtKOB8iX6asx1BHlAZHrH5iQN41e5qS/zroDyweB6+h78odyfmuB1YlVv4lv0hS6vzhmaefcWejhurU4O4f2SUaHISaFtlrdoWO1lbW/nMxzZb2qJtHzMOyS3+2WKVruL0A3xrmZBy3mh2MW 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: It was reported that allocating pages for the write buffer in dm-crypt causes measurable overhead [1]. This patch changes dm-crypt to allocate compound pages if they are available. If not, we fall back to the mempool. [1] https://listman.redhat.com/archives/dm-devel/2023-February/053284.html Signed-off-by: Mikulas Patocka --- drivers/md/dm-crypt.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) Index: linux-2.6/drivers/md/dm-crypt.c =================================================================== --- linux-2.6.orig/drivers/md/dm-crypt.c 2023-01-20 13:22:38.000000000 +0100 +++ linux-2.6/drivers/md/dm-crypt.c 2023-02-16 18:33:42.000000000 +0100 @@ -1657,6 +1657,9 @@ static void crypt_free_buffer_pages(stru * In order to not degrade performance with excessive locking, we try * non-blocking allocations without a mutex first but on failure we fallback * to blocking allocations with a mutex. + * + * In order to reduce allocation overhead, we try to allocate compound pages in + * the first pass. If they are not available, we fall back to the mempool. */ static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size) { @@ -1664,8 +1667,9 @@ static struct bio *crypt_alloc_buffer(st struct bio *clone; unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM; - unsigned i, len, remaining_size; + unsigned remaining_size; struct page *page; + unsigned order = MAX_ORDER - 1; retry: if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) @@ -1678,20 +1682,37 @@ retry: remaining_size = size; - for (i = 0; i < nr_iovecs; i++) { + while (remaining_size) { + unsigned o; + unsigned remaining_order = __fls((remaining_size + PAGE_SIZE - 1) >> PAGE_SHIFT); + order = min(order, remaining_order); + + while (order > 0) { + page = alloc_pages(gfp_mask + | __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN, order); + if (likely(page != NULL)) + goto have_pages; + order--; + } + page = mempool_alloc(&cc->page_pool, gfp_mask); if (!page) { crypt_free_buffer_pages(cc, clone); bio_put(clone); gfp_mask |= __GFP_DIRECT_RECLAIM; + order = 0; goto retry; } - len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size; - - bio_add_page(clone, page, len, 0); +have_pages: + page->compound_order = order; - remaining_size -= len; + for (o = 0; o < 1U << order; o++) { + unsigned len = min((unsigned)PAGE_SIZE, remaining_size); + bio_add_page(clone, page, len, 0); + remaining_size -= len; + page++; + } } /* Allocate space for integrity tags */ @@ -1711,10 +1732,23 @@ static void crypt_free_buffer_pages(stru { struct bio_vec *bv; struct bvec_iter_all iter_all; + unsigned skip_entries = 0; bio_for_each_segment_all(bv, clone, iter_all) { - BUG_ON(!bv->bv_page); - mempool_free(bv->bv_page, &cc->page_pool); + unsigned order; + struct page *page = bv->bv_page; + BUG_ON(!page); + if (skip_entries) { + skip_entries--; + continue; + } + order = page->compound_order; + if (order) { + __free_pages(page, order); + skip_entries = (1U << order) - 1; + } else { + mempool_free(page, &cc->page_pool); + } } }