Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp678204rdb; Thu, 19 Oct 2023 16:26:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEKwGhhHrP9C0gisjSpMfD10uV8yjeMjb65ZTh4KfxkfANIT3fUFBGO33BI3N+mpGHwL/7n X-Received: by 2002:a05:6a00:1a4f:b0:6bc:e7f8:821e with SMTP id h15-20020a056a001a4f00b006bce7f8821emr196150pfv.10.1697757966019; Thu, 19 Oct 2023 16:26:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697757966; cv=none; d=google.com; s=arc-20160816; b=ueNq8qn7Oc+1H05ppm4pMngvnx41ErFBjH/HZuSxGT7/unZCKh0Prrkx4T2KFXpoxZ 95E6ntFEzLXgF75zQeUYEanOZ6QZRV+5wjZ293CE+stc4G+fxKJ0MzR1d06r5TQq5oYu /Q+JnJdK/Yo00DmCbsuyD6TrzUy6f/CZHXi4p5BBS5ngw1lqvRpW0kNVd2oBNblOZO9v mL5UpanwFqpibHYqasLpuea9XBVvScLizgHca/7J/UB7Te77owsVuGKnc4/T3w/HutpV Y78nHvkoKNTOTGEhKqnSB8JorXNnSodnd7EqqVsjT9CjFWOExa9h/SxXirYmtyoZuDeB fpNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=SxadM6dkbOYlTvs8yd6/RgRBOylyunzhgUiG5TSJo8s=; fh=ype5+pa+2YU+nxfYpXqRwEe/DpMxdkJSs0hsCN8R2ok=; b=u/cfQ/zBd1JBPfik0kLVQJvQgUAyVMh8X8+MgMfwL5Q+8gz9GZZzwaTQeEOr3WEwPc /E95RvImCb6/BsMOVJ8mEM0WcoTW8Spfw537ViQj+5QvXBVBsITES1VL8r+YOE8V/0ba P0m+5KxceI33dzQh85gFX0xSMzo9i8UPCFHSrKrJRgngquIsLbJxHgZ9jFRVnt/1PvAZ YdvbvBGx1Tfdmf60iZa+722YPiIGro4QxUfnrqsKRQjjJtExz71QDuoAIXC3J5K0+q3D lyBsvqPWPvBLSsWHC8vbRfd4gwAqLaog1nv4t5d6rN6FSWpU8SbIjy2e4HTskKWHN+cX 6q8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="tFyYNex/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id t64-20020a638143000000b005a9f776c59csi583371pgd.468.2023.10.19.16.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 16:26:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="tFyYNex/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id ADFFD81C70A0; Thu, 19 Oct 2023 16:26:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233356AbjJSXZt (ORCPT + 99 others); Thu, 19 Oct 2023 19:25:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233300AbjJSXZs (ORCPT ); Thu, 19 Oct 2023 19:25:48 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED10E124 for ; Thu, 19 Oct 2023 16:25:46 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9a5836ab12so280765276.2 for ; Thu, 19 Oct 2023 16:25:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697757946; x=1698362746; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=SxadM6dkbOYlTvs8yd6/RgRBOylyunzhgUiG5TSJo8s=; b=tFyYNex/QqOe5/QCcaYL6FLmuy8gZ81cr1w0YxT9RHUXes4Q6tmp5TaK+vEAndWEJw H84sr102FwvYKWkCTgiFFrOfEpUpw0c9EYPRJ6LZo5ElhmjCeItgogxgc3WT9wpPs0iv wI35P0YHMyLL2/Ouj5DZTDXooQlQArRnc4asLFMBPjDP6XlQ4ev3YeBHn+QBpKfz/Z5g OjQzTF6oj9F3DJ5vQxQzzlolqQH7TdP/bbot7BYg+/c4txSii/V4lTU+j/xIKZNfnE1O bEQ3t1h5eSENcpCrJdVhFbvOw5eWVXjsO91MeaFGC4rJGVJc62oDClHJvaYFsG2jPJJg JEGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697757946; x=1698362746; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SxadM6dkbOYlTvs8yd6/RgRBOylyunzhgUiG5TSJo8s=; b=U5Fb49xfnu6FatoEsy/A9X4RlIfA8mzpccbmruN15OvyoJ9cjJZl5YQA605IVV+ES+ tqhWRTdIpjGPRuPQEiLaALqMgEJ2Xy2mJ2Kjv9Asv7annu0gsq3fdBaoXZT7we5IsJgT YsPmHrrrMU5Hry3KckmsGQWIhMHgYx8BUJM1UFGLge00KXBvTpdeg4EcRO96XILJxQVN Lj/y7G9JZR0aKPxuDlv8coNBqFHq+P3YhkbW5qP0jBkuDMtftz6wcTFty0SYZMYXKaoo 403SFeqgVUhghip//ZrH/bV7yfqJMkr4XYPemegdU+G6KthbW4UTU041VtaEdBU0D5WY cznw== X-Gm-Message-State: AOJu0YxAGWgFrqT20c+/UuxzzgQdEc0zB1U1wn3S+CiVilMIu8R8kpqF 9poTzbvoSKZaPl1DNKCQNYVD3tS98Kk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:d9a:c3b8:4274 with SMTP id v3-20020a056902108300b00d9ac3b84274mr10132ybu.7.1697757946202; Thu, 19 Oct 2023 16:25:46 -0700 (PDT) Date: Thu, 19 Oct 2023 16:25:44 -0700 In-Reply-To: <85e332bf-decf-427c-a2e5-95ab872d4ea6@arm.com> Mime-Version: 1.0 References: <20231018173409.1871540-1-seanjc@google.com> <85e332bf-decf-427c-a2e5-95ab872d4ea6@arm.com> Message-ID: Subject: Re: [PATCH] swiotlb: Rewrite comment explaining why the source is preserved on DMA_FROM_DEVICE From: Sean Christopherson To: Robin Murphy Cc: Christoph Hellwig , Marek Szyprowski , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Yan Zhao , Linus Torvalds Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 19 Oct 2023 16:26:03 -0700 (PDT) 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. */