From patchwork Wed Dec 6 00:41:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 10094335 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 32CB360329 for ; Wed, 6 Dec 2017 00:47:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 233C12989A for ; Wed, 6 Dec 2017 00:47:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1787029915; Wed, 6 Dec 2017 00:47:34 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable 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 7CCAE2989A for ; Wed, 6 Dec 2017 00:47:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753773AbdLFAqj (ORCPT ); Tue, 5 Dec 2017 19:46:39 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:33794 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753405AbdLFAmR (ORCPT ); Tue, 5 Dec 2017 19:42:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=FbGSACjrV2kjnKvzF7kysZLHaiIXtOsSj5KvDsEx6qY=; b=IgtisLAnxfbuBubnRkwUNjDJA fnswfKkCgXz3XPhkwa7bM81i2Zd+BThM5e7R4XrCoR0pg8TSGNjeYqMb4ib7vo9/bcAIrO+52aOI6 mucbfD43fC+c/IRq9VxqNVEnom+p319h4dv/X82J48ohPRz82MCHkGIy4ncNJlpQlPD2RXSZKtN8+ fe6XlHK9goTlBfDey5DMtn0x+sy5oYefwZzp3cDA4wghvnzgRuEYrnSgsKfJjlQVgfvENxzEffjAZ dz/yFZlWojN6pmQD4pFYz+wh3801BOKfAZSJNoJAYhY5dxqjJyLQ0lEPoSWchyLsxf0tvF4gz2mmv QNZtB/R5Q==; Received: from willy by bombadil.infradead.org with local (Exim 4.87 #1 (Red Hat Linux)) id 1eMNmw-0001DD-SZ; Wed, 06 Dec 2017 00:42:14 +0000 From: Matthew Wilcox Cc: Matthew Wilcox , Ross Zwisler , Jens Axboe , Rehas Sachdeva , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-nilfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 73/73] usb: Convert xhci-mem to XArray Date: Tue, 5 Dec 2017 16:41:59 -0800 Message-Id: <20171206004159.3755-74-willy@infradead.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20171206004159.3755-1-willy@infradead.org> References: <20171206004159.3755-1-willy@infradead.org> To: unlisted-recipients:; (no To-header on input) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Matthew Wilcox The XArray API is a slightly better fit for xhci_insert_segment_mapping() than the radix tree API was. Signed-off-by: Matthew Wilcox --- drivers/usb/host/xhci-mem.c | 70 +++++++++++++++++++++------------------------ drivers/usb/host/xhci.h | 6 ++-- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c index e1fba4688509..533d813bdc52 100644 --- a/drivers/usb/host/xhci-mem.c +++ b/drivers/usb/host/xhci-mem.c @@ -149,70 +149,64 @@ static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring, } /* - * We need a radix tree for mapping physical addresses of TRBs to which stream - * ID they belong to. We need to do this because the host controller won't tell + * We need to map physical addresses of TRBs to the stream ID they belong to. + * We need to do this because the host controller won't tell * us which stream ring the TRB came from. We could store the stream ID in an * event data TRB, but that doesn't help us for the cancellation case, since the * endpoint may stop before it reaches that event data TRB. * - * The radix tree maps the upper portion of the TRB DMA address to a ring + * The xarray maps the upper portion of the TRB DMA address to a ring * segment that has the same upper portion of DMA addresses. For example, say I * have segments of size 1KB, that are always 1KB aligned. A segment may * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the - * key to the stream ID is 0x43244. I can use the DMA address of the TRB to - * pass the radix tree a key to get the right stream ID: + * index of the stream ID is 0x43244. I can use the DMA address of the TRB as + * the xarray index to get the right stream ID: * * 0x10c90fff >> 10 = 0x43243 * 0x10c912c0 >> 10 = 0x43244 * 0x10c91400 >> 10 = 0x43245 * * Obviously, only those TRBs with DMA addresses that are within the segment - * will make the radix tree return the stream ID for that ring. + * will make the xarray return the stream ID for that ring. * - * Caveats for the radix tree: + * Caveats for the xarray: * - * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an + * The xarray uses an unsigned long for the index. On 32-bit systems, an * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the - * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit - * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit - * extended systems (where the DMA address can be bigger than 32-bits), + * index on 32-bit or 64-bit systems (it would also be fine if we asked for + * 64-bit PCI DMA addresses on a 64-bit system). There might be a problem on + * 32-bit extended systems (where the DMA address can be bigger than 32-bits), * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that. */ -static int xhci_insert_segment_mapping(struct radix_tree_root *trb_address_map, + +static unsigned long trb_index(dma_addr_t dma) +{ + return (unsigned long)(dma >> TRB_SEGMENT_SHIFT); +} + +static int xhci_insert_segment_mapping(struct xarray *trb_address_map, struct xhci_ring *ring, struct xhci_segment *seg, - gfp_t mem_flags) + gfp_t gfp) { - unsigned long key; - int ret; - - key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT); /* Skip any segments that were already added. */ - if (radix_tree_lookup(trb_address_map, key)) - return 0; + void *entry = xa_cmpxchg(trb_address_map, trb_index(seg->dma), NULL, + ring, gfp); - ret = radix_tree_maybe_preload(mem_flags); - if (ret) - return ret; - ret = radix_tree_insert(trb_address_map, - key, ring); - radix_tree_preload_end(); - return ret; + if (IS_ERR(entry)) + return PTR_ERR(entry); + return 0; } -static void xhci_remove_segment_mapping(struct radix_tree_root *trb_address_map, +static void xhci_remove_segment_mapping(struct xarray *trb_address_map, struct xhci_segment *seg) { - unsigned long key; - - key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT); - if (radix_tree_lookup(trb_address_map, key)) - radix_tree_delete(trb_address_map, key); + xa_erase(trb_address_map, trb_index(seg->dma)); } static int xhci_update_stream_segment_mapping( - struct radix_tree_root *trb_address_map, + struct xarray *trb_address_map, struct xhci_ring *ring, struct xhci_segment *first_seg, struct xhci_segment *last_seg, @@ -574,8 +568,8 @@ struct xhci_ring *xhci_dma_to_transfer_ring( u64 address) { if (ep->ep_state & EP_HAS_STREAMS) - return radix_tree_lookup(&ep->stream_info->trb_address_map, - address >> TRB_SEGMENT_SHIFT); + return xa_load(&ep->stream_info->trb_address_map, + trb_index(address)); return ep->ring; } @@ -654,10 +648,10 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci, if (!stream_info->free_streams_command) goto cleanup_ctx; - INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC); + xa_init(&stream_info->trb_address_map); /* Allocate rings for all the streams that the driver will use, - * and add their segment DMA addresses to the radix tree. + * and add their segment DMA addresses to the map. * Stream 0 is reserved. */ @@ -2362,7 +2356,7 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) * Initialize the ring segment pool. The ring must be a contiguous * structure comprised of TRBs. The TRBs must be 16 byte aligned, * however, the command ring segment needs 64-byte aligned segments - * and our use of dma addresses in the trb_address_map radix tree needs + * and our use of dma addresses in the trb_address_map xarray needs * TRB_SEGMENT_SIZE alignment, so we pick the greater alignment need. */ xhci->segment_pool = dma_pool_create("xHCI ring segments", dev, diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index 054ce74524af..e8208a3eee3c 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include @@ -837,7 +837,7 @@ struct xhci_stream_info { unsigned int num_stream_ctxs; dma_addr_t ctx_array_dma; /* For mapping physical TRB addresses to segments in stream rings */ - struct radix_tree_root trb_address_map; + struct xarray trb_address_map; struct xhci_command *free_streams_command; }; @@ -1584,7 +1584,7 @@ struct xhci_ring { unsigned int bounce_buf_len; enum xhci_ring_type type; bool last_td_was_short; - struct radix_tree_root *trb_address_map; + struct xarray *trb_address_map; }; struct xhci_erst_entry {