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.7 required=3.0 tests=FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,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 C3F50C433E0 for ; Mon, 6 Jul 2020 14:08:32 +0000 (UTC) Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.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 62DB420715 for ; Mon, 6 Jul 2020 14:08:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 62DB420715 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sina.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=iommu-bounces@lists.linux-foundation.org Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id F385988F87; Mon, 6 Jul 2020 14:08:28 +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 hCkHsYH7E22P; Mon, 6 Jul 2020 14:08:28 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id 2A2EE8807C; Mon, 6 Jul 2020 14:08:28 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 129E5C0893; Mon, 6 Jul 2020 14:08:28 +0000 (UTC) Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id B0AA2C016F for ; Mon, 6 Jul 2020 14:08:26 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id 9C7D187700 for ; Mon, 6 Jul 2020 14:08:26 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4ljJ1orc-1Ro for ; Mon, 6 Jul 2020 14:08:24 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail3-166.sinamail.sina.com.cn (mail3-166.sinamail.sina.com.cn [202.108.3.166]) by fraxinus.osuosl.org (Postfix) with SMTP id EDC91876F8 for ; Mon, 6 Jul 2020 14:08:23 +0000 (UTC) Received: from unknown (HELO localhost.localdomain)([221.219.171.29]) by sina.com with ESMTP id 5F0330510000077E; Mon, 6 Jul 2020 22:08:20 +0800 (CST) X-Sender: hdanton@sina.com X-Auth-ID: hdanton@sina.com X-SMAIL-MID: 2017854919597 From: Hillf Danton To: Markus Elfring Subject: Re: [RFC PATCH] vfio: type1: fix kthread use case Date: Mon, 6 Jul 2020 22:08:06 +0800 Message-Id: <20200706140806.6028-1-hdanton@sina.com> In-Reply-To: <7ed25c44-bb51-2d8d-82d9-f11272f56424@web.de> References: <20200706104915.11460-1-hdanton@sina.com> <20200706124241.4392-1-hdanton@sina.com> MIME-Version: 1.0 Cc: Hillf Danton , Yan Zhao , kernel-janitors@vger.kernel.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Kevin Tian , Alex Williamson , Christoph Hellwig 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 Mon, 6 Jul 2020 15:33:58 +0200 Markus Elfring wrote: > > I would prefer the following variable declarations then. > > + bool kthread, use_mm; > > > > size_t offset; > > > > + kthread =3D current->flags & PF_KTHREAD; > > + use_mm =3D current->mm =3D=3D NULL; > > I propose to move such assignments directly before the corresponding check= > . Got it. See below. > > > if (!mm) > > return -EPERM; > > > + kthread =3D current->flags & PF_KTHREAD; > + use_mm =3D !current->mm; > > > - if (kthread) > > + if (kthread && use_mm) > > kthread_use_mm(mm); --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2798,7 +2798,7 @@ static int vfio_iommu_type1_dma_rw_chunk struct mm_struct *mm; unsigned long vaddr; struct vfio_dma *dma; - bool kthread = current->mm == NULL; + bool kthread, use_mm; size_t offset; *copied = 0; @@ -2812,11 +2812,13 @@ static int vfio_iommu_type1_dma_rw_chunk return -EPERM; mm = get_task_mm(dma->task); - if (!mm) return -EPERM; - if (kthread) + kthread = current->flags & PF_KTHREAD; + use_mm = current->mm == NULL; + + if (kthread && use_mm) kthread_use_mm(mm); else if (current->mm != mm) goto out; @@ -2843,7 +2845,7 @@ static int vfio_iommu_type1_dma_rw_chunk } else *copied = copy_from_user(data, (void __user *)vaddr, count) ? 0 : count; - if (kthread) + if (kthread && use_mm) kthread_unuse_mm(mm); out: mmput(mm); _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu