Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp66577pxf; Wed, 10 Mar 2021 00:04:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJxgOkWI4Bi1c39eLMOUZzX+VCiQet0zjbEVV5wp3383znhQpXRdfM89Fp0pFZYlYij8+YzT X-Received: by 2002:a17:906:acb:: with SMTP id z11mr2334206ejf.193.1615363493259; Wed, 10 Mar 2021 00:04:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615363493; cv=none; d=google.com; s=arc-20160816; b=qvHjXE0ToVY7oNs4DIDPnZkVklvuTjkx/T7pdSjMSryJ2H1sUyPu7TQ2UCi8QKcAuJ 84LCehlFlRLm7lzXXAxbXo8uCc2Amv6FhC9IFUE8joijpQod3apCH7CzESU7OJ7MOHrK zfpnqf8CqSQSOCFYrJYdSrT9vwq0QQbJCrl1jTKFdEeYKnekaQ+b3/l4l+ePJvK3grGb EnRU17MWTyPewbf6NF+D/6oYNxewM+1XLgPnC5B8peSxld6C8KKNhne8eIlDRWLgmde8 pcbIlSIQXeG2VFJVIRsDNsX7cEyYlTXos65mRy+iQTHGKmV4o18uHVsMMB4VUoMrf8EE hAIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=N8UCzR98Jt6vJfa17f+pjJWD3SJzTxoX5SicoIFhTp0=; b=kENlHd0Dads5FlAlYGB4LbitzJMG0wiOauH3GQS9iosXSub0lMfICQ1wXFmy6DVyht zx+RI+WlInOEEqgJSqEiD1nVt8D/5HihcARTVxEa6cxmBOgHp+jZqxevvnVV0T7ATvCC uqNT0387tErdhiDUF4WcLFqBlPL3ikbN0FqVsqrdSmWGCd8XzXVI3H3HY9B25EDHa/Kd gQ0bhamZyl5hWYem6RxaGv9o2Y8BgrYKThfMpwX7TFJjrl8bLcWVWvqE4ESsTdXiJo6N klUfYgmwpDRoJeT3MCupe85y35gEeXvUNNa/vzQZjerP73S/7PSgbyEQXYViHRXhqYSe ApIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=cMmOgGhX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s14si11248805edy.365.2021.03.10.00.04.30; Wed, 10 Mar 2021 00:04:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=cMmOgGhX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231319AbhCJICO (ORCPT + 99 others); Wed, 10 Mar 2021 03:02:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230458AbhCJICF (ORCPT ); Wed, 10 Mar 2021 03:02:05 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DFBDC06174A; Wed, 10 Mar 2021 00:02:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=N8UCzR98Jt6vJfa17f+pjJWD3SJzTxoX5SicoIFhTp0=; b=cMmOgGhXrV4yOKYPxsK+qohPUM g0O1CVepJCqWPMhHA6YJDvQXdutVWG+8fY5F/+jM55VmxveFTEGAV+nKfQWAWEm9uIGBKs/ZIFle3 Fcvi/FWL4ykwXTpX2t86u5DpzbmV2cb4SDg/80Nib143ygZBaBNaql4qK4hpAzxpkofGFSJiMzJMP hU3QxHtp5RcCXptnJGe+VZmuB1ia6kBu8Yye9+VrJmeCG1CGDlJ26P9OMakeKG35vrnkZF0gg3Ish NTHehMbdFYO48Tmtkf5O2igej0TzV+VMOW+bBMyfUxohJYKqSDigcymXyEynqGLCK6Qulalyr/DOE 98l2CwDA==; Received: from hch by casper.infradead.org with local (Exim 4.94 #2 (Red Hat Linux)) id 1lJtmv-002oEV-H2; Wed, 10 Mar 2021 08:01:52 +0000 Date: Wed, 10 Mar 2021 08:01:49 +0000 From: Christoph Hellwig To: Alex Williamson Cc: cohuck@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, jgg@nvidia.com, peterx@redhat.com Subject: Re: [PATCH v1 09/14] vfio/type1: Refactor pfn_list clearing Message-ID: <20210310080149.GC662265@infradead.org> References: <161523878883.3480.12103845207889888280.stgit@gimli.home> <161524013398.3480.17180657517567370372.stgit@gimli.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <161524013398.3480.17180657517567370372.stgit@gimli.home> X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > +/* Return 1 if iommu->lock dropped and notified, 0 if done */ A bool would be more useful for that pattern. > +static int unmap_dma_pfn_list(struct vfio_iommu *iommu, struct vfio_dma *dma, > + struct vfio_dma **dma_last, int *retries) > +{ > + if (!RB_EMPTY_ROOT(&dma->pfn_list)) { Just return early when it is empty to remove a level of indentation for the whole function? > + struct vfio_iommu_type1_dma_unmap nb_unmap; > + > + if (*dma_last == dma) { > + BUG_ON(++(*retries) > 10); > + } else { > + *dma_last = dma; > + *retries = 0; > + } > + > + nb_unmap.iova = dma->iova; > + nb_unmap.size = dma->size; > + > + /* > + * Notify anyone (mdev vendor drivers) to invalidate and > + * unmap iovas within the range we're about to unmap. > + * Vendor drivers MUST unpin pages in response to an > + * invalidation. > + */ > + mutex_unlock(&iommu->lock); > + blocking_notifier_call_chain(&iommu->notifier, > + VFIO_IOMMU_NOTIFY_DMA_UNMAP, > + &nb_unmap); > + return 1; Conditional locking is a horrible pattern. I'd rather only factor the code until before the unlock into the helper, and then leave the unlock and notify to the caller to avoid that anti-pattern. Also vendor driver isn't really Linux terminology for a subdriver, so I'd suggest to switch to something else while you're at it.