Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3340429imw; Wed, 6 Jul 2022 23:18:10 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vq/kwhnCJV3uDY8pod4/mkuCJGfZUSImi+x+pHXUU0VBfKHYSvYkLaXBAOSHhHhrTGyIkW X-Received: by 2002:a63:fc0e:0:b0:412:9d58:d235 with SMTP id j14-20020a63fc0e000000b004129d58d235mr5750776pgi.112.1657174689931; Wed, 06 Jul 2022 23:18:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657174689; cv=none; d=google.com; s=arc-20160816; b=kXV4NO0aTUzop6NfdPDlhtlwGZgVdYUH7IsDYcjKecq4aChko4ZeIBilw5emKMwWvZ JSCT20Asb3kW5o3r1BO8VuA9CyNvzzQOcaq8l8L3WmFri4SaeTNXV2Pi7MTMMfS+rvZr C/ToN0/xBvSHpeXhNSLbbQoaTy31QPJAc/kvPNsUkzv48YNtmVqpziWlHHHyxjBpnsk9 KCjnUN46vFGezJslTY9IgczGqRTrv8SCimsG6mjKfYUhgVCpM1M4YiiBhqY8JhjeQL8n cYWqgd3uw3zbP5MdT9m1GNUyTS6PTBfM3iEASGDDFfjfqV2eaFs7vyCXiN3e/zvDK7yz gyRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=3PkUgqPPw33+dbJ22WBEwo0kMgIo4AB+2lJ2PQBqADY=; b=bO+nJ7I6B6knGZc8amApRM8ITLoIOIEG1FB0HTmzflNosMYTDy8bWivpc7izj0VE/S 8+uuNAu0mM5Nc5sJUhHLRSPduJWqiqk5YnO1P7JDfY/dx/NIBVvHVAHbrwDG9xVwmpZ8 UYiDFXJZUWFmho2PAlTDnZ4cAqLxergOLDk3e/ULqY6xqHHlyHF4FGxi+aH949FklK+/ C6uUP/ptVRTgS5+gU/61Qx6PUCvZVzlw/BKJwtn9MIRoCv1sIZc8KYrBOelzCGkkzBgV 74hIV4NH03JUo1gTOerPwseP3AXfrzpNXMMh1wVHlvyy6pDHj3WpEekW1JiBwuCT9DUD bzSA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m11-20020a170902f64b00b00163dc4774b9si21279475plg.39.2022.07.06.23.17.54; Wed, 06 Jul 2022 23:18:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234273AbiGGF6r (ORCPT + 99 others); Thu, 7 Jul 2022 01:58:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229927AbiGGF6p (ORCPT ); Thu, 7 Jul 2022 01:58:45 -0400 Received: from verein.lst.de (verein.lst.de [213.95.11.211]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E967B2F013; Wed, 6 Jul 2022 22:58:44 -0700 (PDT) Received: by verein.lst.de (Postfix, from userid 2407) id 96BA568AA6; Thu, 7 Jul 2022 07:58:40 +0200 (CEST) Date: Thu, 7 Jul 2022 07:58:40 +0200 From: Christoph Hellwig To: "Andrea Parri (Microsoft)" Cc: Christoph Hellwig , Marek Szyprowski , Robin Murphy , KY Srinivasan , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Michael Kelley , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Peter Anvin , linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, iommu@lists.linux.dev, linux-hyperv@vger.kernel.org, x86@kernel.org Subject: Re: [RFC PATCH 2/2] dma-direct: Fix dma_direct_{alloc,free}() for Hyperv-V IVMs Message-ID: <20220707055840.GA13401@lst.de> References: <20220706195027.76026-1-parri.andrea@gmail.com> <20220706195027.76026-3-parri.andrea@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220706195027.76026-3-parri.andrea@gmail.com> User-Agent: Mutt/1.5.17 (2007-11-01) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 06, 2022 at 09:50:27PM +0200, Andrea Parri (Microsoft) wrote: > @@ -305,6 +306,21 @@ void *dma_direct_alloc(struct device *dev, size_t size, > ret = page_address(page); > if (dma_set_decrypted(dev, ret, size)) > goto out_free_pages; > +#ifdef CONFIG_HAS_IOMEM > + /* > + * Remap the pages in the unencrypted physical address space > + * when dma_unencrypted_base is set (e.g., for Hyper-V AMD > + * SEV-SNP isolated guests). > + */ > + if (dma_unencrypted_base) { > + phys_addr_t ret_pa = virt_to_phys(ret); > + > + ret_pa += dma_unencrypted_base; > + ret = memremap(ret_pa, size, MEMREMAP_WB); > + if (!ret) > + goto out_encrypt_pages; > + } > +#endif So: this needs to move into dma_set_decrypted, otherwise we don't handle the dma_alloc_pages case (never mind that this is pretty unreadable). Which then again largely duplicates the code in swiotlb. So I think what we need here is a low-level helper that does the set_memory_decrypted and memremap. I'm not quite sure where it should go, but maybe some of the people involved with memory encryption might have good ideas. unencrypted_base should go with it and then both swiotlb and dma-direct can call it. > + /* > + * If dma_unencrypted_base is set, the virtual address returned by > + * dma_direct_alloc() is in the vmalloc address range. > + */ > + if (!dma_unencrypted_base && is_vmalloc_addr(cpu_addr)) { > vunmap(cpu_addr); > } else { > if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED)) > arch_dma_clear_uncached(cpu_addr, size); > +#ifdef CONFIG_HAS_IOMEM > + if (dma_unencrypted_base) { > + memunmap(cpu_addr); > + /* re-encrypt the pages using the original address */ > + cpu_addr = page_address(pfn_to_page(PHYS_PFN( > + dma_to_phys(dev, dma_addr)))); > + } > +#endif > if (dma_set_encrypted(dev, cpu_addr, size)) Same on the unmap side. It might also be worth looking into reordering the checks in some form instead o that raw dma_unencrypted_base check before the unmap.