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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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 062F3C433E9 for ; Mon, 11 Jan 2021 18:04:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CB488223E0 for ; Mon, 11 Jan 2021 18:04:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390034AbhAKSES (ORCPT ); Mon, 11 Jan 2021 13:04:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389981AbhAKSES (ORCPT ); Mon, 11 Jan 2021 13:04:18 -0500 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E9CCC061794 for ; Mon, 11 Jan 2021 10:03:37 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id d26so671356wrb.12 for ; Mon, 11 Jan 2021 10:03:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=OQ88+LI5HEr8LP9MECKHb0an/ILY1uHLC7MWc4xKUSQ=; b=nCV1ZS4fAU6hFngRbC7jkRKIcsqPejpew4BRdQ4Ly13ypD++QeZmnDPhiU/2zSCw9+ Yw7jZGzmxtGbszhlZ7gehioXAMjzXypa6avlszOWtWRD5TWhsSiyX35H2teoKthN/ui7 FVrSPZ4/XeoXWDQS/byVZYdAJEjkdbdPwGIcM353b45f6urb2PimnhEhz9b+CsXbnx+c sLXiNwVrm1OcQG4wBdhbBUOPlsIKUTsNFjPPkYNMBaL7Dv981SGRsghzVSVED+fLBYvr N68wnXnivV/CmoxkQ/3xrBdU+nLPuHiJa2WEof02BjcSTON0Afo90YapNzD/LluttZfy enDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=OQ88+LI5HEr8LP9MECKHb0an/ILY1uHLC7MWc4xKUSQ=; b=K4gbdnQG1ht8ozP5JFtuKlcAzYFjkyDTZ9RZz60lnw4ElBqBWWem28WeEL2NpAO7Pf UFKir7yL9k53qmaDR+Et2wDNaFsOT0nLqrrzax+am4hL/IKoBiDmfnii9WtUsQ2LJBf8 5tbViYMSNHLlx1ECsJR6uOSNqlerZBLY1s84JiUYRn9WT6d94Xt1bJtfaqSm/qQ+SNVh wdG60B2BoXvrdV9WgKI1r983YrHGR0zr+8x0tb3EzwpcQguhuW2fc0vpXXlSpGSNgnuv ImjiAkdt9/JhSJD7jl5Kj7Shxijwxeh1HEpip8dilQsqsnwk2esZZDl4HCYWVK1aArYX fEww== X-Gm-Message-State: AOAM532pVj3j1oLlQbyxDKLCZqEwX9ti5NNC+WtyEYw83VBkupy35zyj XhUuKN+FPw9hSflqC8pLLxAJ6cTmwGqMSps/WGmyFA== X-Google-Smtp-Source: ABdhPJzsKB2S8tXBu6i5PVjW2kuyXdlPhHhTEht6hD50Xozy9RUpd2RVduyv0+3+XkChb7CKfLEXBKj0UQa8VJy/PqY= X-Received: by 2002:a5d:5604:: with SMTP id l4mr311015wrv.127.1610388216078; Mon, 11 Jan 2021 10:03:36 -0800 (PST) MIME-Version: 1.0 References: <20210111154335.23388-1-marcorr@google.com> In-Reply-To: From: Marc Orr Date: Mon, 11 Jan 2021 10:03:24 -0800 Message-ID: Subject: Re: [PATCH] dma: mark unmapped DMA scatter/gather invalid To: Robin Murphy Cc: hch@lst.de, m.szyprowski@samsung.com, Jianxiong Gao , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On 2021-01-11 15:43, Marc Orr wrote: minus stable@vger.kernel.org, per gregkh@'s email. > > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > > index 0a4881e59aa7..3d9b17fe5771 100644 > > --- a/kernel/dma/direct.c > > +++ b/kernel/dma/direct.c > > @@ -374,9 +374,11 @@ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, > > struct scatterlist *sg; > > int i; > > > > - for_each_sg(sgl, sg, nents, i) > > + for_each_sg(sgl, sg, nents, i) { > > dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir, > > attrs); > > + sg->dma_address = DMA_MAPPING_ERROR; > > There are more DMA API backends than just dma-direct, so while this > might help paper over bugs when SWIOTLB is in use, it's not going to > have any effect when those same bugs are hit under other circumstances. > Once again, the moral of the story is that effort is better spent just > fixing the bugs ;) Thanks for the quick feedback. What is the correct fix? I understand the first half. The NVMe driver should be updated to not call unmap on an address that has already been unmapped within the DMA direct code. Where I'm less certain is how to communicate to the NVMe driver that the mapping failed. In particular, the NVMe code explicitly checks if the first DMA address in the scatter/gather list is set to DMA_MAPPING_ERROR. Thus, don't we need to update the DMA direct code to propagate DMA_MAPPING_ERROR back up to the driver, via the scatter/gather struct? I skimmed arch/arm/mm/dma-mapping.c, just now. I can see that this code sets the address within the scatter/gather struct to DMA_MAPPING_ERROR before trying to map an IO address and write it into the struct. Is this a good example to follow? Thanks, Marc 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=-8.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham 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 13B88C433DB for ; Mon, 11 Jan 2021 18:39:17 +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 621072250E for ; Mon, 11 Jan 2021 18:39:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 621072250E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lists.linux-foundation.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 hemlock.osuosl.org (Postfix) with ESMTP id D4353870F6; Mon, 11 Jan 2021 18:39:15 +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 XREJak4crG6Q; Mon, 11 Jan 2021 18:39:15 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id 3445086221; Mon, 11 Jan 2021 18:39:15 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 16AABC088B; Mon, 11 Jan 2021 18:39:15 +0000 (UTC) Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 8861DC013A for ; Mon, 11 Jan 2021 18:03:39 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 64D6A227AA for ; Mon, 11 Jan 2021 18:03:39 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id zg61FY7uVg61 for ; Mon, 11 Jan 2021 18:03:38 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by silver.osuosl.org (Postfix) with ESMTPS id F117E204DC for ; Mon, 11 Jan 2021 18:03:37 +0000 (UTC) Received: by mail-wr1-f47.google.com with SMTP id 91so707866wrj.7 for ; Mon, 11 Jan 2021 10:03:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=OQ88+LI5HEr8LP9MECKHb0an/ILY1uHLC7MWc4xKUSQ=; b=nCV1ZS4fAU6hFngRbC7jkRKIcsqPejpew4BRdQ4Ly13ypD++QeZmnDPhiU/2zSCw9+ Yw7jZGzmxtGbszhlZ7gehioXAMjzXypa6avlszOWtWRD5TWhsSiyX35H2teoKthN/ui7 FVrSPZ4/XeoXWDQS/byVZYdAJEjkdbdPwGIcM353b45f6urb2PimnhEhz9b+CsXbnx+c sLXiNwVrm1OcQG4wBdhbBUOPlsIKUTsNFjPPkYNMBaL7Dv981SGRsghzVSVED+fLBYvr N68wnXnivV/CmoxkQ/3xrBdU+nLPuHiJa2WEof02BjcSTON0Afo90YapNzD/LluttZfy enDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=OQ88+LI5HEr8LP9MECKHb0an/ILY1uHLC7MWc4xKUSQ=; b=bKwwyMceTuZ56Y7/YQZ2eKFTzOhLWmDWTz96VuRaDwuQaPGps1SH76E5920g5rXKUP G2hhOdSg2SEl5uAHVb6NzEr1a6qm3G2UrgXs1sHDjtVPLKO3ezpiZ9mUz2HfPlpBvgJR 2S5zxYVm14FsCdZe9DCUrCF2j1GLTCV2i8QgqOVkDAXGhOMNnmFraCqw6UyMtskngp2I u+KFozrPiPn7F1YbMwaDH8+oFSr/fWxEB825qqIvgmEGk8leqWkMGXdLdCQW2ifb6RPJ nAvFEJNZsX6M38z9S9zfeG8yAZQ4tKes4rcc8ryND9ug2DUBEtlsL/pKsAQCv7P2bADK ESgQ== X-Gm-Message-State: AOAM530OSN3EOvDSgDfQHFRwi5mWBcsvGYbnprwGYdgY7IK3Q3EY5dV/ SCyFxYgLfMlpuie7MNpjrU9YIATn1e9jQUS4G9m7+Q== X-Google-Smtp-Source: ABdhPJzsKB2S8tXBu6i5PVjW2kuyXdlPhHhTEht6hD50Xozy9RUpd2RVduyv0+3+XkChb7CKfLEXBKj0UQa8VJy/PqY= X-Received: by 2002:a5d:5604:: with SMTP id l4mr311015wrv.127.1610388216078; Mon, 11 Jan 2021 10:03:36 -0800 (PST) MIME-Version: 1.0 References: <20210111154335.23388-1-marcorr@google.com> In-Reply-To: Date: Mon, 11 Jan 2021 10:03:24 -0800 Message-ID: Subject: Re: [PATCH] dma: mark unmapped DMA scatter/gather invalid To: Robin Murphy X-Mailman-Approved-At: Mon, 11 Jan 2021 18:39:13 +0000 Cc: linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, Jianxiong Gao , hch@lst.de 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: , From: Marc Orr via iommu Reply-To: Marc Orr Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: iommu-bounces@lists.linux-foundation.org Sender: "iommu" > On 2021-01-11 15:43, Marc Orr wrote: minus stable@vger.kernel.org, per gregkh@'s email. > > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > > index 0a4881e59aa7..3d9b17fe5771 100644 > > --- a/kernel/dma/direct.c > > +++ b/kernel/dma/direct.c > > @@ -374,9 +374,11 @@ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, > > struct scatterlist *sg; > > int i; > > > > - for_each_sg(sgl, sg, nents, i) > > + for_each_sg(sgl, sg, nents, i) { > > dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir, > > attrs); > > + sg->dma_address = DMA_MAPPING_ERROR; > > There are more DMA API backends than just dma-direct, so while this > might help paper over bugs when SWIOTLB is in use, it's not going to > have any effect when those same bugs are hit under other circumstances. > Once again, the moral of the story is that effort is better spent just > fixing the bugs ;) Thanks for the quick feedback. What is the correct fix? I understand the first half. The NVMe driver should be updated to not call unmap on an address that has already been unmapped within the DMA direct code. Where I'm less certain is how to communicate to the NVMe driver that the mapping failed. In particular, the NVMe code explicitly checks if the first DMA address in the scatter/gather list is set to DMA_MAPPING_ERROR. Thus, don't we need to update the DMA direct code to propagate DMA_MAPPING_ERROR back up to the driver, via the scatter/gather struct? I skimmed arch/arm/mm/dma-mapping.c, just now. I can see that this code sets the address within the scatter/gather struct to DMA_MAPPING_ERROR before trying to map an IO address and write it into the struct. Is this a good example to follow? Thanks, Marc _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu