Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752533AbdIVRIb (ORCPT ); Fri, 22 Sep 2017 13:08:31 -0400 Received: from mail-bl2nam02on0050.outbound.protection.outlook.com ([104.47.38.50]:26720 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752196AbdIVRI3 (ORCPT ); Fri, 22 Sep 2017 13:08:29 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Tomasz.Nowicki@cavium.com; Subject: Re: [PATCH v5 3/6] iommu/iova: Extend rbtree node caching To: tn , Robin Murphy , joro@8bytes.org Cc: dwoods@mellanox.com, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org References: <2c352944fb1d20ad4ef6778475006af81a270945.1506007392.git.robin.murphy@arm.com> <692b36cc-9bb0-51f9-a6f9-7d891f6ff78b@semihalf.com> From: tn Date: Fri, 22 Sep 2017 19:08:09 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit X-Originating-IP: [12.108.191.226] X-ClientProxiedBy: VI1PR0102CA0007.eurprd01.prod.exchangelabs.com (2603:10a6:802::20) To CY4PR0701MB3652.namprd07.prod.outlook.com (2603:10b6:910:93::15) Message-ID: X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cd8b5c3c-0eef-4e9b-e755-08d501dc8a23 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:CY4PR0701MB3652; X-Microsoft-Exchange-Diagnostics: 1;CY4PR0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bIgJG1k9W1ZUYwIh1spOTsIhhduC23Pw5kgUMYyz3v4xVAV2TJpAWCQpfjDcFp8xQw5xIrAIwh8CXGOsyVPtFzbwYZuKjJoX+6u0MovRB46iDVhLIFUeO3zVOS+YQneW64AprWXtqRMuFIQRCyW6zisaoJ0zO3VgJu7zzRUsEi3B79kEoSBEYqiKG0ALvTcKbYZcagZCNK3BVJgoXoBtVvfL0l0zRI0DM6UTKrnDCm7CzLx7FzInKRf4Ed7YUwM+m1LKBMgfyhTxcw2TRbNPWXi7Z80yihIWdSqnFbimGB7sSjoUVO/HAMXGw9J93hIutDXhrWFl2eN/7E1zYDubP79hHm2YXmIFV4BLa/Uiy/ub4L+JUtvWfiwVdCRL3Tajpa5rubDzw8UbRZr2AVvs6+MqL9dagppJmHmJxZ4B1x5sWEgzLSkH26qzAS8jyRT6gaDvLkUSTwVkGLypcqGjD8= X-Exchange-Antispam-Report-Test: UriScan:(180628864354917)(50582790962513); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(5005006)(8121501046)(93006095)(3002001)(10201501046)(100000703101)(100105400095)(6041248)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(20161123564025)(20161123560025)(20161123562025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:CY4PR0701MB3652;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:CY4PR0701MB3652; X-Microsoft-Exchange-Diagnostics: 1;CY4PR0701MB3652;4:K8xyB+chQK4ash+CK1FkCoUz1VB7xdYwbwqBbZNQxt/JvuEgdohmrEsLpYbZR/Pu3yy09GYzgMYC/aYwdsenS5N9J4RueyzJH45t9uC/7EFLVIHbtfAdBmeCr9f/Btle2yyKgcgepOWlFhRHv35TvuXHWU+h/zrsqS9KGvyWm1cYpdMzcsVmtvI1Z5Gi+X3pFN+FGymP2QNuM3goc6npBkUvYyvzV0rh6JcHjcFns9ihKz02y7sEp89RdI8kSZksZGliY9xx8XisZfGcB9Nov6TufB2vy9dj32F5ZACecqt5Q3iH34FsaLO7AvsmoYmz0+QGN5nJoJTy87t2h2DbzA== X-Forefront-PRVS: 0438F90F17 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6009001)(6049001)(376002)(346002)(199003)(189002)(24454002)(93886005)(23676002)(4326008)(53546010)(53936002)(478600001)(83506001)(81166006)(7736002)(65806001)(316002)(110136005)(74316002)(117156002)(58126008)(8936002)(97736004)(72206003)(25786009)(65956001)(65826007)(5660300001)(47776003)(2950100002)(16526017)(42882006)(8676002)(16576012)(81156014)(66066001)(6666003)(305945005)(77096006)(189998001)(9686003)(6486002)(33656002)(64126003)(50466002)(68736007)(101416001)(50986999)(2906002)(76176999)(105586002)(3846002)(6116002)(229853002)(2870700001)(106356001)(54356999)(86152003)(6246003)(43062003)(65966003);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR0701MB3652;H:[192.168.2.100];FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTRQUjA3MDFNQjM2NTI7MjM6WGt0YmNJQkV1d0o3Q29QblcvcHNaTUxy?= =?utf-8?B?ZUJzY2pRdmNoZFIvTUNmU0EvV0ZpczkvV0U4Y3BleEEvVVF6cFVERW8rc3o3?= =?utf-8?B?WGpRMHBhVnVpbFZEM3ZrZVpXY0xZTVo0NEl5QStZVG4wNkF6M2U5NHlCbU5a?= =?utf-8?B?Wkp5aGR0d0xUdnZUdFZKK3FkQnBZUzdGM2RxdmJJTDU3RERVbk5SaTFKVjhv?= =?utf-8?B?SWhjTlZyWmUrZGp3NmFBUFBUdFdjaUkzeStuekN1M2NGSUpyNUtDd3Y1ZE05?= =?utf-8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HJZ6STDo9TVaIy/JKGpi5duMDVu/GI2xHiItE+eQVq4N8hrF3Nu0yG1O74af1Gkzvv97YhMKb3IiSOQMFzpLMJzBLy8KknMqpnQ2xThW4Vko2azTQXE368k9JNODUBJ33gjf3kIAbfFkHnVPw2L6B8GaKJh0W7U2A8fg0elWH/MtCEUoLumiRLw1s85FAOLWUQT9PURfO85eyDbXFzwkb3VbiCLlw==;5:52pvVML6LBfEDU0nxlSk8PHpNrqbNVMX2K0G14YM/1R68rDLyNCuV86MqiRvshUrvS9TiJtHZKgXBniq4STnu3/0QeNUOMYS103PKZRL9c096gUy577VDk5Fb+bWA2liXF77ggj8P0LdHkCaXFbpQA==;24:eTms1Mgl/BExB0IEyITsLudZdBlCxMpFMk8rx9t3C+H9NfW6ZfaChR0dCJ/6ALz5oWpLyQuNaf5QwIhWPlrcyevaT1Wb55nEFo5h+xWuDAg=;7:/6OCrSNdx+/8eP2CxBD/iuHzJDjoczK5riUC9eXDePQZ9rWc2Nwy3yCVayuA2S3cDIfd0co7Mg/KumfkUXmjgLkdoT8wllrwGbq0oHiIvHQB0SicRCt8GLbuRgK2/qkdGFmk6E65sthz70N2oM4HRQjOZ9bi9uRsNB73mpyVGxlH1jZu9rhRyVhQ0KRDvfEDlCJacHM41HaRoa3daPt67OrWBhM2HWF99wQMlxLnQFw= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Sep 2017 17:08:21.8896 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR0701MB3652 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4900 Lines: 124 On 22.09.2017 18:50, tn wrote: > On 22.09.2017 18:21, Tomasz Nowicki wrote: >> Hi Robin, >> >> On 21.09.2017 17:52, Robin Murphy wrote: >>> The cached node mechanism provides a significant performance benefit for >>> allocations using a 32-bit DMA mask, but in the case of non-PCI devices >>> or where the 32-bit space is full, the loss of this benefit can be >>> significant - on large systems there can be many thousands of entries in >>> the tree, such that walking all the way down to find free space every >>> time becomes increasingly awful. >>> >>> Maintain a similar cached node for the whole IOVA space as a superset of >>> the 32-bit space so that performance can remain much more consistent. >>> >>> Inspired by work by Zhen Lei . >>> >>> Tested-by: Ard Biesheuvel >>> Tested-by: Zhen Lei >>> Tested-by: Nate Watterson >>> Signed-off-by: Robin Murphy >>> --- >>> >>> v5: Fixed __cached_rbnode_delete_update() logic to update both nodes >>>      when necessary >>> >>>   drivers/iommu/iova.c | 60 >>> ++++++++++++++++++++++++---------------------------- >>>   include/linux/iova.h |  3 ++- >>>   2 files changed, 30 insertions(+), 33 deletions(-) >>> >>> diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c >>> index 20be9a8b3188..c6f5a22f8d20 100644 >>> --- a/drivers/iommu/iova.c >>> +++ b/drivers/iommu/iova.c >>> @@ -48,6 +48,7 @@ init_iova_domain(struct iova_domain *iovad, >>> unsigned long granule, >>>       spin_lock_init(&iovad->iova_rbtree_lock); >>>       iovad->rbroot = RB_ROOT; >>> +    iovad->cached_node = NULL; >>>       iovad->cached32_node = NULL; >>>       iovad->granule = granule; >>>       iovad->start_pfn = start_pfn; >>> @@ -110,48 +111,44 @@ EXPORT_SYMBOL_GPL(init_iova_flush_queue); >>>   static struct rb_node * >>>   __get_cached_rbnode(struct iova_domain *iovad, unsigned long >>> *limit_pfn) >>>   { >>> -    if ((*limit_pfn > iovad->dma_32bit_pfn) || >>> -        (iovad->cached32_node == NULL)) >>> +    struct rb_node *cached_node = NULL; >>> +    struct iova *curr_iova; >>> + >>> +    if (*limit_pfn <= iovad->dma_32bit_pfn) >>> +        cached_node = iovad->cached32_node; >>> +    if (!cached_node) >>> +        cached_node = iovad->cached_node; >>> +    if (!cached_node) >>>           return rb_last(&iovad->rbroot); >>> -    else { >>> -        struct rb_node *prev_node = rb_prev(iovad->cached32_node); >>> -        struct iova *curr_iova = >>> -            rb_entry(iovad->cached32_node, struct iova, node); >>> -        *limit_pfn = curr_iova->pfn_lo; >>> -        return prev_node; >>> -    } >>> + >>> +    curr_iova = rb_entry(cached_node, struct iova, node); >>> +    *limit_pfn = min(*limit_pfn, curr_iova->pfn_lo); >> >> I guess this it the fix for stale pointer in iovad->cached32_node from >> previous series but I think this is something more. >> >> With this min() here we have real control over the limit_pfn we would >> like to allocate at most. In other works, without your series two >> subsequent calls can give us: >> iova (ffff) = alloc_iova_fast(iovad, 1, DMA_BIT_MASK(32) >> shift); >> >> iova (fffe)= alloc_iova_fast(iovad, 1, DMA_BIT_MASK(16) >> shift); >> >> We do not see this since nobody uses limit_pfn below DMA_BIT_MASK(32) >> now. That might be done intentionally so I ask for your opinion. >> >> Also, with your patch and two the same alloc_iova_fast() calls in >> iteration may get 32-bit space full much faster. Please correct me if >> I messed things up. > > After few more thoughts, I think this is unrealistic case. > > In any case, please consider below fix: >  static void > -__cached_rbnode_insert_update(struct iova_domain *iovad, > -    unsigned long limit_pfn, struct iova *new) > +__cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new) >  { > -    if (limit_pfn != iovad->dma_32bit_pfn) > -        return; > -    iovad->cached32_node = &new->node; > +    if (new->pfn_hi == iovad->dma_32bit_pfn) > +        iovad->cached32_node = &new->node; > +    else > +        iovad->cached_node = &new->node; >  } Sorry :(, this is still wrong. It should look like this: static void __cached_rbnode_insert_update(struct iova_domain *iovad, unsigned long limit_pfn, struct iova *new) { - if (limit_pfn != iovad->dma_32bit_pfn) - return; - iovad->cached32_node = &new->node; + if (limit_pfn == iovad->dma_32bit_pfn) + iovad->cached32_node = &new->node; + else if (new->pfn_hi > iovad->dma_32bit_pfn) + iovad->cached_node = &new->node; } but then we still need to save initial limit_pfn in __alloc_and_insert_iova_range() for this decision. Tomasz