From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA9B6C3F2CD for ; Fri, 28 Feb 2020 14:44:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 73642246A3 for ; Fri, 28 Feb 2020 14:44:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Eyk6vY0A" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726990AbgB1OoN (ORCPT ); Fri, 28 Feb 2020 09:44:13 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:35195 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726682AbgB1OoM (ORCPT ); Fri, 28 Feb 2020 09:44:12 -0500 Received: by mail-wr1-f68.google.com with SMTP id r7so3259360wro.2 for ; Fri, 28 Feb 2020 06:44:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=QMl+VBaGMao7gyBNUaUbW1tLbhMPTZJMdvofJak/bmM=; b=Eyk6vY0Af0pgRHDvnoOLJKEE9DaGoa+4z0NJ0t/WaE+0vf6ERLTI5+gCSTL4JKeEc7 arJUIl26vcb7lA32WK3mE1Z9pObCrTyBu370u//nTiHn54Qsk9vtBAlncLuZo9K0V9Bo few19oXa3fshNwMeLE1X36r0Kl70yyi9UUOxcD7HXOQk57UJt7R/uONrD0jw5QwBKUEP XNLUCyENocPiBEBMYBsa65NNpXfx1bwVRSoKhuPD+Y1BFMRjqLFWjj7Bjg+11i79QgS1 AuuGvqPiln8Z0uHDmfdS0ONpDj/sxIKSFG/mkvrKTjD2nZHB7B4UWE1XulydcVQDQVxM 5WkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=QMl+VBaGMao7gyBNUaUbW1tLbhMPTZJMdvofJak/bmM=; b=VR8kCxeC6ioyGcPXUPonUK2tTPsxsMZJ1ArAc/SCw3B545qaIlHq7BkQEaoPUo8Px1 66RDfCvgcJkI1q3HtMAaxIQiPrgpKEHoeGQpwhdEPanlCE57BENSI6QrsV5+m+WBp8/Y FkOW0a1VmxFIeI8U8O+ZeLOrb3LfAzJazCgpH/o+id+mnuv82zekyTv1cFgXya/3kgq7 hnOJiSb7h4uDpwbvjvmp404YBYh7XMpNfAZZDBKglQUNAW+UPRu5IZ/YR1NOv0vjlfmx 3ETFHwLpyrUJ/kvU3AbIGwhPer1c4Vgv0qdl9l3y4wEPXGGwhWuZsMwPbYuf+sD/7nxd r1Yw== X-Gm-Message-State: APjAAAVbwllbt3kEmfayH9SPU1KUhXlHwYK5g8VKXaMdcILZIkwpxoql LM+sqXooloCmynPGCugOUDr1lw== X-Google-Smtp-Source: APXvYqw6DykiBE32SqEJ+HnRwk8TuEFmQqrz2Fb4Wvuo7oVY88B0FHH4nfUvdWL5pnDIWaqbqIxz+g== X-Received: by 2002:adf:f648:: with SMTP id x8mr5477186wrp.198.1582901051368; Fri, 28 Feb 2020 06:44:11 -0800 (PST) Received: from myrica ([2001:171b:c9a8:fbc0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id o27sm13045012wro.27.2020.02.28.06.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Feb 2020 06:44:10 -0800 (PST) Date: Fri, 28 Feb 2020 15:44:04 +0100 From: Jean-Philippe Brucker To: Jonathan Cameron Cc: iommu@lists.linux-foundation.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, joro@8bytes.org, robh+dt@kernel.org, mark.rutland@arm.com, catalin.marinas@arm.com, will@kernel.org, robin.murphy@arm.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, jacob.jun.pan@linux.intel.com, christian.koenig@amd.com, yi.l.liu@intel.com, zhangfei.gao@linaro.org, Jean-Philippe Brucker Subject: Re: [PATCH v4 03/26] iommu: Add a page fault handler Message-ID: <20200228144404.GD2156@myrica> References: <20200224182401.353359-1-jean-philippe@linaro.org> <20200224182401.353359-4-jean-philippe@linaro.org> <20200226135933.000061a0@Huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200226135933.000061a0@Huawei.com> Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org On Wed, Feb 26, 2020 at 01:59:33PM +0000, Jonathan Cameron wrote: > > +static int iopf_complete(struct device *dev, struct iopf_fault *iopf, > > + enum iommu_page_response_code status) > > This is called once per group. Should name reflect that? Ok [...] > > +/** > > + * iommu_queue_iopf - IO Page Fault handler > > + * @evt: fault event > > + * @cookie: struct device, passed to iommu_register_device_fault_handler. > > + * > > + * Add a fault to the device workqueue, to be handled by mm. > > + * > > + * Return: 0 on success and <0 on error. > > + */ > > +int iommu_queue_iopf(struct iommu_fault *fault, void *cookie) > > +{ > > + int ret; > > + struct iopf_group *group; > > + struct iopf_fault *iopf, *next; > > + struct iopf_device_param *iopf_param; > > + > > + struct device *dev = cookie; > > + struct iommu_param *param = dev->iommu_param; > > + > > + if (WARN_ON(!mutex_is_locked(¶m->lock))) > > + return -EINVAL; > > Just curious... > > Why do we always need a runtime check on this rather than say, > using lockdep_assert_held or similar? I probably didn't know about lockdep_assert at the time :) > > + /* > > + * It is incredibly easy to find ourselves in a deadlock situation if > > + * we're not careful, because we're taking the opposite path as > > + * iommu_queue_iopf: > > + * > > + * iopf_queue_flush_dev() | PRI queue handler > > + * lock(¶m->lock) | iommu_queue_iopf() > > + * queue->flush() | lock(¶m->lock) > > + * wait PRI queue empty | > > + * > > + * So we can't hold the device param lock while flushing. Take a > > + * reference to the device param instead, to prevent the queue from > > + * going away. > > + */ > > + mutex_lock(¶m->lock); > > + iopf_param = param->iopf_param; > > + if (iopf_param) { > > + queue = param->iopf_param->queue; > > + iopf_param->busy = true; > > Describing this as taking a reference is not great... > I'd change the comment to set a flag or something like that. > > Is there any potential of multiple copies of this running against > each other? I've not totally gotten my head around when this > might be called yet. Yes it's allowed, this should be a refcount [...] > > +int iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev) > > +{ > > + int ret = -EINVAL; > > + struct iopf_fault *iopf, *next; > > + struct iopf_device_param *iopf_param; > > + struct iommu_param *param = dev->iommu_param; > > + > > + if (!param || !queue) > > + return -EINVAL; > > + > > + do { > > + mutex_lock(&queue->lock); > > + mutex_lock(¶m->lock); > > + iopf_param = param->iopf_param; > > + if (iopf_param && iopf_param->queue == queue) { > > + if (iopf_param->busy) { > > + ret = -EBUSY; > > + } else { > > + list_del(&iopf_param->queue_list); > > + param->iopf_param = NULL; > > + ret = 0; > > + } > > + } > > + mutex_unlock(¶m->lock); > > + mutex_unlock(&queue->lock); > > + > > + /* > > + * If there is an ongoing flush, wait for it to complete and > > + * then retry. iopf_param isn't going away since we're the only > > + * thread that can free it. > > + */ > > + if (ret == -EBUSY) > > + wait_event(iopf_param->wq_head, !iopf_param->busy); > > + else if (ret) > > + return ret; > > + } while (ret == -EBUSY); > > I'm in two minds about the next comment (so up to you)... > > Currently this looks a bit odd. Would you be better off just having a separate > parameter for busy and explicit separate handling for the error path? > > bool busy; > int ret = 0; > > do { > mutex_lock(&queue->lock); > mutex_lock(¶m->lock); > iopf_param = param->iopf_param; > if (iopf_param && iopf_param->queue == queue) { > busy = iopf_param->busy; > if (!busy) { > list_del(&iopf_param->queue_list); > param->iopf_param = NULL; > } > } else { > ret = -EINVAL; > } > mutex_unlock(¶m->lock); > mutex_unlock(&queue->lock); > if (ret) > return ret; > if (busy) > wait_event(iopf_param->wq_head, !iopf_param->busy); > > } while (busy); > > .. Sure, I think it looks better Thanks, Jean From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.6 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B01CCC3F2CD for ; Fri, 28 Feb 2020 14:44:17 +0000 (UTC) Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8545B246AE for ; Fri, 28 Feb 2020 14:44:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Eyk6vY0A" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8545B246AE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=iommu-bounces@lists.linux-foundation.org Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 57524833C6; Fri, 28 Feb 2020 14:44:17 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6moR1lTxg3z8; Fri, 28 Feb 2020 14:44:16 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by whitealder.osuosl.org (Postfix) with ESMTP id 25D2E826E6; Fri, 28 Feb 2020 14:44:16 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 0BB9BC08A0; Fri, 28 Feb 2020 14:44:16 +0000 (UTC) Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id D8FEDC0177 for ; Fri, 28 Feb 2020 14:44:13 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id D50C584CBD for ; Fri, 28 Feb 2020 14:44:13 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7zQ37WJMZi8B for ; Fri, 28 Feb 2020 14:44:13 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by hemlock.osuosl.org (Postfix) with ESMTPS id DC5D2815BC for ; Fri, 28 Feb 2020 14:44:12 +0000 (UTC) Received: by mail-wr1-f67.google.com with SMTP id v2so3162906wrp.12 for ; Fri, 28 Feb 2020 06:44:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=QMl+VBaGMao7gyBNUaUbW1tLbhMPTZJMdvofJak/bmM=; b=Eyk6vY0Af0pgRHDvnoOLJKEE9DaGoa+4z0NJ0t/WaE+0vf6ERLTI5+gCSTL4JKeEc7 arJUIl26vcb7lA32WK3mE1Z9pObCrTyBu370u//nTiHn54Qsk9vtBAlncLuZo9K0V9Bo few19oXa3fshNwMeLE1X36r0Kl70yyi9UUOxcD7HXOQk57UJt7R/uONrD0jw5QwBKUEP XNLUCyENocPiBEBMYBsa65NNpXfx1bwVRSoKhuPD+Y1BFMRjqLFWjj7Bjg+11i79QgS1 AuuGvqPiln8Z0uHDmfdS0ONpDj/sxIKSFG/mkvrKTjD2nZHB7B4UWE1XulydcVQDQVxM 5WkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=QMl+VBaGMao7gyBNUaUbW1tLbhMPTZJMdvofJak/bmM=; b=efAZ7VEFxOImM9zXwIqGZZhmJy8XH3/Zop94y9ioo5DEPXrx2JZqPaVuc+o/7CD+2X s4O83N1kX9ntSwSn3Hq2YM5cNyJ3rC8Jh5hStdt2njvvcoBCgigbgcbIurM2/i6uxrDF 24UvsNAKAuJBF5qMCXJLMejnnqd3URpUcXjy5QT6jO0gmcqCmTOgiZIXXZH09kHkhxT3 6+MbQ30J395ZihqVdx4MEbLWcSfN6YXTLnqJ2HTgMd+1RufVKOJGSTPeWZ1U1kO1Eq7d 72RKIuiGY5xMp/WI6NCxc3BxCweRffQrO4ALtxHBrP2kLN5zA1q36zz0GIEZUNZpj0II fV4Q== X-Gm-Message-State: APjAAAWRA2PlHKA36Zt9UoHhrT+SU6MaD1FnTt1stue6uyBTtQAXG3Yw kqSZ2T0pbtc1fZv6UoOXtBU5qQ== X-Google-Smtp-Source: APXvYqw6DykiBE32SqEJ+HnRwk8TuEFmQqrz2Fb4Wvuo7oVY88B0FHH4nfUvdWL5pnDIWaqbqIxz+g== X-Received: by 2002:adf:f648:: with SMTP id x8mr5477186wrp.198.1582901051368; Fri, 28 Feb 2020 06:44:11 -0800 (PST) Received: from myrica ([2001:171b:c9a8:fbc0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id o27sm13045012wro.27.2020.02.28.06.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Feb 2020 06:44:10 -0800 (PST) Date: Fri, 28 Feb 2020 15:44:04 +0100 From: Jean-Philippe Brucker To: Jonathan Cameron Subject: Re: [PATCH v4 03/26] iommu: Add a page fault handler Message-ID: <20200228144404.GD2156@myrica> References: <20200224182401.353359-1-jean-philippe@linaro.org> <20200224182401.353359-4-jean-philippe@linaro.org> <20200226135933.000061a0@Huawei.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200226135933.000061a0@Huawei.com> Cc: mark.rutland@arm.com, devicetree@vger.kernel.org, kevin.tian@intel.com, Jean-Philippe Brucker , linux-pci@vger.kernel.org, robin.murphy@arm.com, linux-mm@kvack.org, iommu@lists.linux-foundation.org, robh+dt@kernel.org, catalin.marinas@arm.com, zhangfei.gao@linaro.org, will@kernel.org, christian.koenig@amd.com, linux-arm-kernel@lists.infradead.org X-BeenThere: iommu@lists.linux-foundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Development issues for Linux IOMMU support List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: iommu-bounces@lists.linux-foundation.org Sender: "iommu" On Wed, Feb 26, 2020 at 01:59:33PM +0000, Jonathan Cameron wrote: > > +static int iopf_complete(struct device *dev, struct iopf_fault *iopf, > > + enum iommu_page_response_code status) > > This is called once per group. Should name reflect that? Ok [...] > > +/** > > + * iommu_queue_iopf - IO Page Fault handler > > + * @evt: fault event > > + * @cookie: struct device, passed to iommu_register_device_fault_handler. > > + * > > + * Add a fault to the device workqueue, to be handled by mm. > > + * > > + * Return: 0 on success and <0 on error. > > + */ > > +int iommu_queue_iopf(struct iommu_fault *fault, void *cookie) > > +{ > > + int ret; > > + struct iopf_group *group; > > + struct iopf_fault *iopf, *next; > > + struct iopf_device_param *iopf_param; > > + > > + struct device *dev = cookie; > > + struct iommu_param *param = dev->iommu_param; > > + > > + if (WARN_ON(!mutex_is_locked(¶m->lock))) > > + return -EINVAL; > > Just curious... > > Why do we always need a runtime check on this rather than say, > using lockdep_assert_held or similar? I probably didn't know about lockdep_assert at the time :) > > + /* > > + * It is incredibly easy to find ourselves in a deadlock situation if > > + * we're not careful, because we're taking the opposite path as > > + * iommu_queue_iopf: > > + * > > + * iopf_queue_flush_dev() | PRI queue handler > > + * lock(¶m->lock) | iommu_queue_iopf() > > + * queue->flush() | lock(¶m->lock) > > + * wait PRI queue empty | > > + * > > + * So we can't hold the device param lock while flushing. Take a > > + * reference to the device param instead, to prevent the queue from > > + * going away. > > + */ > > + mutex_lock(¶m->lock); > > + iopf_param = param->iopf_param; > > + if (iopf_param) { > > + queue = param->iopf_param->queue; > > + iopf_param->busy = true; > > Describing this as taking a reference is not great... > I'd change the comment to set a flag or something like that. > > Is there any potential of multiple copies of this running against > each other? I've not totally gotten my head around when this > might be called yet. Yes it's allowed, this should be a refcount [...] > > +int iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev) > > +{ > > + int ret = -EINVAL; > > + struct iopf_fault *iopf, *next; > > + struct iopf_device_param *iopf_param; > > + struct iommu_param *param = dev->iommu_param; > > + > > + if (!param || !queue) > > + return -EINVAL; > > + > > + do { > > + mutex_lock(&queue->lock); > > + mutex_lock(¶m->lock); > > + iopf_param = param->iopf_param; > > + if (iopf_param && iopf_param->queue == queue) { > > + if (iopf_param->busy) { > > + ret = -EBUSY; > > + } else { > > + list_del(&iopf_param->queue_list); > > + param->iopf_param = NULL; > > + ret = 0; > > + } > > + } > > + mutex_unlock(¶m->lock); > > + mutex_unlock(&queue->lock); > > + > > + /* > > + * If there is an ongoing flush, wait for it to complete and > > + * then retry. iopf_param isn't going away since we're the only > > + * thread that can free it. > > + */ > > + if (ret == -EBUSY) > > + wait_event(iopf_param->wq_head, !iopf_param->busy); > > + else if (ret) > > + return ret; > > + } while (ret == -EBUSY); > > I'm in two minds about the next comment (so up to you)... > > Currently this looks a bit odd. Would you be better off just having a separate > parameter for busy and explicit separate handling for the error path? > > bool busy; > int ret = 0; > > do { > mutex_lock(&queue->lock); > mutex_lock(¶m->lock); > iopf_param = param->iopf_param; > if (iopf_param && iopf_param->queue == queue) { > busy = iopf_param->busy; > if (!busy) { > list_del(&iopf_param->queue_list); > param->iopf_param = NULL; > } > } else { > ret = -EINVAL; > } > mutex_unlock(¶m->lock); > mutex_unlock(&queue->lock); > if (ret) > return ret; > if (busy) > wait_event(iopf_param->wq_head, !iopf_param->busy); > > } while (busy); > > .. Sure, I think it looks better Thanks, Jean _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10844C3F2D2 for ; Fri, 28 Feb 2020 14:44:27 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D373B246A3 for ; Fri, 28 Feb 2020 14:44:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="t4PxkgD0"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Eyk6vY0A" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D373B246A3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=wS+9ktkEdJHPJjyFqmpVpFAQpav6KsTMeVsMb85d6BI=; b=t4PxkgD0gKtgn6 3jRDyyeeENaS0zn4EykMKDZY9uC7Y3aDmhIDQfckG2IK6zA6CWuQTiX7xswUGxEGNlvN6pSoZPWmT lwz0QYDy4pPTlHxOKzspjunYIicQ2n2YUc7/hkmUk7gdF5a2zrqgLgOBEOu+pxo1fM/vHeNengH/S w2ixTXhUnu0OX+b6EP9M/aDAzY7h8wY6bZeM6iYcGHGcHoyaTEHfKlES74cw7oFsX3y9Yuni/wNfd lTDcB1rdVpFDXZ9eSFBEFE3vxCUmVlIAoovaYZaHoiBYPs1oqs/ofOiptT3oLI4Q+ByrptLOY7xLG s5bFDuUbT1MwEc2sjKGw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1j7gsE-0001LB-9t; Fri, 28 Feb 2020 14:44:18 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1j7gs8-0001K9-Hr for linux-arm-kernel@lists.infradead.org; Fri, 28 Feb 2020 14:44:16 +0000 Received: by mail-wr1-x444.google.com with SMTP id e10so1783829wrr.10 for ; Fri, 28 Feb 2020 06:44:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=QMl+VBaGMao7gyBNUaUbW1tLbhMPTZJMdvofJak/bmM=; b=Eyk6vY0Af0pgRHDvnoOLJKEE9DaGoa+4z0NJ0t/WaE+0vf6ERLTI5+gCSTL4JKeEc7 arJUIl26vcb7lA32WK3mE1Z9pObCrTyBu370u//nTiHn54Qsk9vtBAlncLuZo9K0V9Bo few19oXa3fshNwMeLE1X36r0Kl70yyi9UUOxcD7HXOQk57UJt7R/uONrD0jw5QwBKUEP XNLUCyENocPiBEBMYBsa65NNpXfx1bwVRSoKhuPD+Y1BFMRjqLFWjj7Bjg+11i79QgS1 AuuGvqPiln8Z0uHDmfdS0ONpDj/sxIKSFG/mkvrKTjD2nZHB7B4UWE1XulydcVQDQVxM 5WkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=QMl+VBaGMao7gyBNUaUbW1tLbhMPTZJMdvofJak/bmM=; b=njGWUdAdyj2NQBXiS0ynXTspMT/ubLFbceRRSAr0Jk1ln8Q0GwYHdPIYd+oYxR0I5x 6igYVCs0AJabZwLlwg8/ktOm8kA0/KcY4l+cpN37U1HcZtRhJxmZuTKHGiu9XQTs/PYm UNz7j0GbXi2bzX/iefIS4UtuLapotaI/ahKT2lHA18Fvf1byGfGaXmgwX6YYMRRrHUrQ yOR+8DRJtWwPNI5adgAiSIOW6e0BdJDenrgmQOKpDw2kR9E8vGmvGM5JjTLeB7Mk6VR3 nFVYXLY2HhYyoG6N6sqQLGNVKXdVBeEuqz3UsX8sQWndAES0AxLfLlKduusY8jy98q5l y1ug== X-Gm-Message-State: APjAAAVc4pMam0VCJDkk/MH/8lqxNalH4xVwgSgIAoySlhCA0L/Y/FlK vHE203x8344INOFvsJKNqhIDVw== X-Google-Smtp-Source: APXvYqw6DykiBE32SqEJ+HnRwk8TuEFmQqrz2Fb4Wvuo7oVY88B0FHH4nfUvdWL5pnDIWaqbqIxz+g== X-Received: by 2002:adf:f648:: with SMTP id x8mr5477186wrp.198.1582901051368; Fri, 28 Feb 2020 06:44:11 -0800 (PST) Received: from myrica ([2001:171b:c9a8:fbc0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id o27sm13045012wro.27.2020.02.28.06.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Feb 2020 06:44:10 -0800 (PST) Date: Fri, 28 Feb 2020 15:44:04 +0100 From: Jean-Philippe Brucker To: Jonathan Cameron Subject: Re: [PATCH v4 03/26] iommu: Add a page fault handler Message-ID: <20200228144404.GD2156@myrica> References: <20200224182401.353359-1-jean-philippe@linaro.org> <20200224182401.353359-4-jean-philippe@linaro.org> <20200226135933.000061a0@Huawei.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200226135933.000061a0@Huawei.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200228_064415_119310_E9515577 X-CRM114-Status: GOOD ( 23.78 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, devicetree@vger.kernel.org, kevin.tian@intel.com, jacob.jun.pan@linux.intel.com, Jean-Philippe Brucker , linux-pci@vger.kernel.org, joro@8bytes.org, robin.murphy@arm.com, linux-mm@kvack.org, iommu@lists.linux-foundation.org, robh+dt@kernel.org, yi.l.liu@intel.com, catalin.marinas@arm.com, zhangfei.gao@linaro.org, will@kernel.org, christian.koenig@amd.com, linux-arm-kernel@lists.infradead.org, baolu.lu@linux.intel.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Feb 26, 2020 at 01:59:33PM +0000, Jonathan Cameron wrote: > > +static int iopf_complete(struct device *dev, struct iopf_fault *iopf, > > + enum iommu_page_response_code status) > > This is called once per group. Should name reflect that? Ok [...] > > +/** > > + * iommu_queue_iopf - IO Page Fault handler > > + * @evt: fault event > > + * @cookie: struct device, passed to iommu_register_device_fault_handler. > > + * > > + * Add a fault to the device workqueue, to be handled by mm. > > + * > > + * Return: 0 on success and <0 on error. > > + */ > > +int iommu_queue_iopf(struct iommu_fault *fault, void *cookie) > > +{ > > + int ret; > > + struct iopf_group *group; > > + struct iopf_fault *iopf, *next; > > + struct iopf_device_param *iopf_param; > > + > > + struct device *dev = cookie; > > + struct iommu_param *param = dev->iommu_param; > > + > > + if (WARN_ON(!mutex_is_locked(¶m->lock))) > > + return -EINVAL; > > Just curious... > > Why do we always need a runtime check on this rather than say, > using lockdep_assert_held or similar? I probably didn't know about lockdep_assert at the time :) > > + /* > > + * It is incredibly easy to find ourselves in a deadlock situation if > > + * we're not careful, because we're taking the opposite path as > > + * iommu_queue_iopf: > > + * > > + * iopf_queue_flush_dev() | PRI queue handler > > + * lock(¶m->lock) | iommu_queue_iopf() > > + * queue->flush() | lock(¶m->lock) > > + * wait PRI queue empty | > > + * > > + * So we can't hold the device param lock while flushing. Take a > > + * reference to the device param instead, to prevent the queue from > > + * going away. > > + */ > > + mutex_lock(¶m->lock); > > + iopf_param = param->iopf_param; > > + if (iopf_param) { > > + queue = param->iopf_param->queue; > > + iopf_param->busy = true; > > Describing this as taking a reference is not great... > I'd change the comment to set a flag or something like that. > > Is there any potential of multiple copies of this running against > each other? I've not totally gotten my head around when this > might be called yet. Yes it's allowed, this should be a refcount [...] > > +int iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev) > > +{ > > + int ret = -EINVAL; > > + struct iopf_fault *iopf, *next; > > + struct iopf_device_param *iopf_param; > > + struct iommu_param *param = dev->iommu_param; > > + > > + if (!param || !queue) > > + return -EINVAL; > > + > > + do { > > + mutex_lock(&queue->lock); > > + mutex_lock(¶m->lock); > > + iopf_param = param->iopf_param; > > + if (iopf_param && iopf_param->queue == queue) { > > + if (iopf_param->busy) { > > + ret = -EBUSY; > > + } else { > > + list_del(&iopf_param->queue_list); > > + param->iopf_param = NULL; > > + ret = 0; > > + } > > + } > > + mutex_unlock(¶m->lock); > > + mutex_unlock(&queue->lock); > > + > > + /* > > + * If there is an ongoing flush, wait for it to complete and > > + * then retry. iopf_param isn't going away since we're the only > > + * thread that can free it. > > + */ > > + if (ret == -EBUSY) > > + wait_event(iopf_param->wq_head, !iopf_param->busy); > > + else if (ret) > > + return ret; > > + } while (ret == -EBUSY); > > I'm in two minds about the next comment (so up to you)... > > Currently this looks a bit odd. Would you be better off just having a separate > parameter for busy and explicit separate handling for the error path? > > bool busy; > int ret = 0; > > do { > mutex_lock(&queue->lock); > mutex_lock(¶m->lock); > iopf_param = param->iopf_param; > if (iopf_param && iopf_param->queue == queue) { > busy = iopf_param->busy; > if (!busy) { > list_del(&iopf_param->queue_list); > param->iopf_param = NULL; > } > } else { > ret = -EINVAL; > } > mutex_unlock(¶m->lock); > mutex_unlock(&queue->lock); > if (ret) > return ret; > if (busy) > wait_event(iopf_param->wq_head, !iopf_param->busy); > > } while (busy); > > .. Sure, I think it looks better Thanks, Jean _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel