From patchwork Wed Oct 25 16:47:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Christie X-Patchwork-Id: 10026995 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8FA3760375 for ; Wed, 25 Oct 2017 16:47:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 80F6628BEC for ; Wed, 25 Oct 2017 16:47:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 75C9A28BEE; Wed, 25 Oct 2017 16:47:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0F0E628BEC for ; Wed, 25 Oct 2017 16:47:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751991AbdJYQrv (ORCPT ); Wed, 25 Oct 2017 12:47:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51258 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751920AbdJYQrv (ORCPT ); Wed, 25 Oct 2017 12:47:51 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 136734B0F3; Wed, 25 Oct 2017 16:47:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 136734B0F3 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=mchristi@redhat.com Received: from rh2.redhat.com (ovpn-124-200.rdu2.redhat.com [10.10.124.200]) by smtp.corp.redhat.com (Postfix) with ESMTP id 47D9118C52; Wed, 25 Oct 2017 16:47:50 +0000 (UTC) From: Mike Christie To: target-devel@vger.kernel.org, nab@linux-iscsi.org Cc: Mike Christie Subject: [PATCH 09/20] tcmu: split unmap_thread_fn Date: Wed, 25 Oct 2017 11:47:19 -0500 Message-Id: <1508950050-10120-10-git-send-email-mchristi@redhat.com> In-Reply-To: <1508950050-10120-1-git-send-email-mchristi@redhat.com> References: <1508950050-10120-1-git-send-email-mchristi@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 25 Oct 2017 16:47:51 +0000 (UTC) Sender: target-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: target-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Separate unmap_thread_fn to make it easier to read. Signed-off-by: Mike Christie --- drivers/target/target_core_user.c | 149 +++++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 68 deletions(-) diff --git a/drivers/target/target_core_user.c b/drivers/target/target_core_user.c index 9add6a3..91dfc19 100644 --- a/drivers/target/target_core_user.c +++ b/drivers/target/target_core_user.c @@ -2047,6 +2047,84 @@ static struct target_backend_ops tcmu_ops = { .tb_dev_attrib_attrs = NULL, }; +static uint32_t find_free_blocks(void) +{ + struct tcmu_dev *udev; + loff_t off; + uint32_t start, end, block, free_blocks = 0; + + mutex_lock(&root_udev_mutex); + list_for_each_entry(udev, &root_udev, node) { + mutex_lock(&udev->cmdr_lock); + + /* Try to complete the finished commands first */ + tcmu_handle_completions(udev); + + /* Skip the udevs waiting the global pool or in idle */ + if (is_in_waiter_list(udev) || !udev->dbi_thresh) { + mutex_unlock(&udev->cmdr_lock); + continue; + } + + end = udev->dbi_max + 1; + block = find_last_bit(udev->data_bitmap, end); + if (block == udev->dbi_max) { + /* + * The last bit is dbi_max, so there is + * no need to shrink any blocks. + */ + mutex_unlock(&udev->cmdr_lock); + continue; + } else if (block == end) { + /* The current udev will goto idle state */ + udev->dbi_thresh = start = 0; + udev->dbi_max = 0; + } else { + udev->dbi_thresh = start = block + 1; + udev->dbi_max = block; + } + + /* Here will truncate the data area from off */ + off = udev->data_off + start * DATA_BLOCK_SIZE; + unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); + + /* Release the block pages */ + tcmu_blocks_release(&udev->data_blocks, start, end); + mutex_unlock(&udev->cmdr_lock); + + free_blocks += end - start; + } + mutex_unlock(&root_udev_mutex); + return free_blocks; +} + +static void run_cmdr_queues(uint32_t *free_blocks) +{ + struct tcmu_dev *udev, *tmp; + + /* + * Try to wake up the udevs who are waiting + * for the global data pool blocks. + */ + mutex_lock(&root_udev_waiter_mutex); + list_for_each_entry_safe(udev, tmp, &root_udev_waiter, waiter) { + mutex_lock(&udev->cmdr_lock); + if (udev->waiting_blocks < *free_blocks) { + mutex_unlock(&udev->cmdr_lock); + break; + } + + *free_blocks -= udev->waiting_blocks; + udev->waiting_blocks = 0; + mutex_unlock(&udev->cmdr_lock); + + list_del(&udev->waiter); + + wake_up(&udev->wait_cmdr); + } + mutex_unlock(&root_udev_waiter_mutex); +} + static void check_timedout_devices(void) { struct tcmu_dev *udev, *tmp_dev; @@ -2070,10 +2148,7 @@ static void check_timedout_devices(void) static int unmap_thread_fn(void *data) { - struct tcmu_dev *udev, *tmp; - loff_t off; - uint32_t start, end, block; - static uint32_t free_blocks; + uint32_t free_blocks = 0; bool has_timed_out_devs; while (!kthread_should_stop()) { @@ -2097,70 +2172,8 @@ static int unmap_thread_fn(void *data) check_timedout_devices(); - mutex_lock(&root_udev_mutex); - list_for_each_entry(udev, &root_udev, node) { - mutex_lock(&udev->cmdr_lock); - - /* Try to complete the finished commands first */ - tcmu_handle_completions(udev); - - /* Skip the udevs waiting the global pool or in idle */ - if (is_in_waiter_list(udev) || !udev->dbi_thresh) { - mutex_unlock(&udev->cmdr_lock); - continue; - } - - end = udev->dbi_max + 1; - block = find_last_bit(udev->data_bitmap, end); - if (block == udev->dbi_max) { - /* - * The last bit is dbi_max, so there is - * no need to shrink any blocks. - */ - mutex_unlock(&udev->cmdr_lock); - continue; - } else if (block == end) { - /* The current udev will goto idle state */ - udev->dbi_thresh = start = 0; - udev->dbi_max = 0; - } else { - udev->dbi_thresh = start = block + 1; - udev->dbi_max = block; - } - - /* Here will truncate the data area from off */ - off = udev->data_off + start * DATA_BLOCK_SIZE; - unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); - - /* Release the block pages */ - tcmu_blocks_release(&udev->data_blocks, start, end); - mutex_unlock(&udev->cmdr_lock); - - free_blocks += end - start; - } - mutex_unlock(&root_udev_mutex); - - /* - * Try to wake up the udevs who are waiting - * for the global data pool blocks. - */ - mutex_lock(&root_udev_waiter_mutex); - list_for_each_entry_safe(udev, tmp, &root_udev_waiter, waiter) { - mutex_lock(&udev->cmdr_lock); - if (udev->waiting_blocks < free_blocks) { - mutex_unlock(&udev->cmdr_lock); - break; - } - - free_blocks -= udev->waiting_blocks; - udev->waiting_blocks = 0; - mutex_unlock(&udev->cmdr_lock); - - list_del(&udev->waiter); - - wake_up(&udev->wait_cmdr); - } - mutex_unlock(&root_udev_waiter_mutex); + free_blocks += find_free_blocks(); + run_cmdr_queues(&free_blocks); } return 0;