Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp902036rdb; Fri, 20 Oct 2023 02:53:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGVbUXxfWULMW2RW77H6Zd7XZDtfMQFkFGWRhCzc0uRlJ2W62dRWLka6l+3U2tvUq8PJDRw X-Received: by 2002:a05:6808:3086:b0:3af:e451:9e6 with SMTP id bl6-20020a056808308600b003afe45109e6mr1650962oib.15.1697795611539; Fri, 20 Oct 2023 02:53:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697795611; cv=none; d=google.com; s=arc-20160816; b=Hszf2dIMc4P6LrlYbVXFxl2crKSMyskSPx2LxPbDy391Y7/SLbB97LVUTEDUkcAtbn 9cmJNFWrOP7+In45m4kb4OO+DhwRT1vUJtvqXLuZpIT45zzjg5iuR6xx27KdcVCpk6A0 +xBBe3E5bIuY32kF7fCQNg9pTTdpEvQ2B8nbyD8pBCMw/B5eNuAEQRQwbOTtVdxBu+bG uhXWdL7vx6KxqP1eoHfCVHPDDba91Qj5Wu+IIRSGyEAw9sDnvEtHHh2caU+ERwm5kM/7 /Az7wW+ep4cFvsFmXJJTLSDcp+Aa9LU4N5k49DI7hozkaNnxclsSD4Ap3XCI/1ozTg5T SbAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=rVH73LrzyFDdS7ELIQ8FD6jJyX2jdMSQrOl40S+2zws=; fh=sQPfJtS9yvR3g0UGFnzZoBuuRnP9HcyDS0iRop4SDmU=; b=aYB/73B7l99hnW5mM+eYgMXtJ1Tpa7Z5gOfc+/7rLG94OSfRUePE4Kyz+/EvPsv3W+ pP8HAeBZwsxAN4F3W+Jebyv+UBK8yQ23AdFgGkqLEVmtDX2/06qNMF9VnhoQQ46EANBG utQjX8qv3J98EKqskd9r864XWK7AfbosOfnaU2DFTr97gRhPrEjY7y/z4wcoS9cRpdh/ g+yDE0NmqzXfZvmndj4a7OOjo0H91HlFg9sDCo+1XB4DVLXDEM9gDu4pec+Z7v+h4/tj W4MEoMqUC1iI/NvMkm/0/qP9N9Cuu/+ucQvxgX3Oz64F9RLmtLBRvlfGBV97xRdC2e/C UPuA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id bk13-20020a056a02028d00b0056433b221b9si1446767pgb.477.2023.10.20.02.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 02:53:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id EFEB583C238F; Fri, 20 Oct 2023 02:51:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376810AbjJTJva (ORCPT + 99 others); Fri, 20 Oct 2023 05:51:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376755AbjJTJvV (ORCPT ); Fri, 20 Oct 2023 05:51:21 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B5714D57 for ; Fri, 20 Oct 2023 02:51:15 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 68F292F4; Fri, 20 Oct 2023 02:51:56 -0700 (PDT) Received: from [10.57.68.58] (unknown [10.57.68.58]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5C9C13F5A1; Fri, 20 Oct 2023 02:51:14 -0700 (PDT) Message-ID: Date: Fri, 20 Oct 2023 10:51:12 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] swiotlb: Rewrite comment explaining why the source is preserved on DMA_FROM_DEVICE Content-Language: en-GB To: Sean Christopherson Cc: Christoph Hellwig , Marek Szyprowski , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Yan Zhao , Linus Torvalds References: <20231018173409.1871540-1-seanjc@google.com> <85e332bf-decf-427c-a2e5-95ab872d4ea6@arm.com> From: Robin Murphy In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 20 Oct 2023 02:51:49 -0700 (PDT) On 2023-10-20 00:25, Sean Christopherson wrote: > On Wed, Oct 18, 2023, Robin Murphy wrote: >> On 2023-10-18 18:34, Sean Christopherson wrote: >>> diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c >>> index 01637677736f..e071415a75dc 100644 >>> --- a/kernel/dma/swiotlb.c >>> +++ b/kernel/dma/swiotlb.c >>> @@ -1296,11 +1296,13 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, >>> pool->slots[index + i].orig_addr = slot_addr(orig_addr, i); >>> tlb_addr = slot_addr(pool->start, index) + offset; >>> /* >>> - * When dir == DMA_FROM_DEVICE we could omit the copy from the orig >>> - * to the tlb buffer, if we knew for sure the device will >>> - * overwrite the entire current content. But we don't. Thus >>> - * unconditional bounce may prevent leaking swiotlb content (i.e. >>> - * kernel memory) to user-space. >>> + * When the device is writing memory, i.e. dir == DMA_FROM_DEVICE, copy >>> + * the original buffer to the TLB buffer before initiating DMA in order >>> + * to preserve the original's data if the device does a partial write, >>> + * i.e. if the device doesn't overwrite the entire buffer. Preserving >>> + * the original data, even if it's garbage, is necessary to match >>> + * hardware behavior (use of swiotlb is supposed to be transparent) and >> >> Super-nit: I think that last "and" is superfluous (i.e. unwritten memory not >> magically corrupting itself *is* the aforementioned hardware behaviour). > > Ah yeah, agreed. How about this? > > /* > * When the device is writing memory, i.e. dir == DMA_FROM_DEVICE, copy > * the original buffer to the TLB buffer before initiating DMA in order > * to preserve the original's data if the device does a partial write, > * i.e. if the device doesn't overwrite the entire buffer. Preserving > * the original data, even if it's garbage, is necessary to match > * hardware behavior. Use of swiotlb is supposed to be transparent, > * i.e. swiotlb must not corrupt memory by clobbering unwritten bytes. > */ Nice, that reads even more clearly IMO. Cheers, Robin.