Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1069418pxb; Fri, 27 Aug 2021 00:01:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQha9IWs6zdshN8GXsXhC2i+6g4pu2bj8mBqes+A2CoEVaoHyndcp/A6SyYRudgwxlhXjU X-Received: by 2002:a17:907:1b1b:: with SMTP id mp27mr8287299ejc.538.1630047672568; Fri, 27 Aug 2021 00:01:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630047672; cv=none; d=google.com; s=arc-20160816; b=P5QZZRRZDdsaiJOoRcXLtxKYERPZyO7PDXNohFajocS9xki/hQAKJw8TFUgVOIhA1l 7u7OhEh2w8r7uVIzA/fiUSHGFm79j95LvlWfpuEBKuV5JNynSGeefzGKKYZHARsZjwoB CPsZzPvpPrzZGmWZKoysDHRZvC7i5tpJ4e0NDFZXMpj4fzgq921SJHZMf28TvZ6iRHcr Xfq+HZuRSzvgCMQpoGgi1Q3LsOKgYqtAe4isRc+kNonNiA4Y5JpN8wGzmQ5NPgmcQBR0 DgoO96V4iu96Yv1aq9J9+h8RI4HvXIaALFTTrkRzMrtBsKfWzPsU267HTJY/wzuz/y/o 9/xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Rqn+GrCddwb17438Eb7/acjTBeKWly1ebyK4GCadnFc=; b=jsRJ6/udeTj6NvGDCE/qGR39KhPiSiScuRyurRxH3oudw/0mCquQaNv55glP3t4cna WngwjXg6xdM6FEhKIB6xRpoqzCg8te+tQ80IPFOiATdtorqNYPNmmJeo4cEy90r8doDV 1jrFYlrjP/3J7zTZ7I6QKnPgS0zP5c4QBvfOR9qCMPXriJjgQvnF8BuN/B1oVk/6V8oi YO1WHM6zPaGnowt6juVV+4wDz0VfDrnhEOw28hfQGPbnzDxw0HkjKAUrliFvChkB6Cr4 FHkF8SN98dHoMIenvHs94RHjdq4s99qP6tezp3A5fVA8v51j+gDY0dcKdV6ni5vczR6S kzfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Ahob/aCa"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb13si6823828edb.420.2021.08.27.00.00.48; Fri, 27 Aug 2021 00:01:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Ahob/aCa"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235220AbhH0HAF (ORCPT + 99 others); Fri, 27 Aug 2021 03:00:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232463AbhH0G75 (ORCPT ); Fri, 27 Aug 2021 02:59:57 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E63A3C0613CF; Thu, 26 Aug 2021 23:59:08 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id j4-20020a17090a734400b0018f6dd1ec97so4160984pjs.3; Thu, 26 Aug 2021 23:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Rqn+GrCddwb17438Eb7/acjTBeKWly1ebyK4GCadnFc=; b=Ahob/aCaoxGGmBX3uT/2Oo+coR/T1tys8xgPUYlJZHQ+79x1W7tMhNYHihEb3g00Pw m+dgL0BLb9DG6XuqQrGIlpqK11vUr+Y2ytYHZ7LdTBFa1tFBJE3STgSjF41FypNGQroh EB33JFOQSvpQcvOA/KVG6FqtXl7O0+BT+q4qQg5vtIojCX29E2kJoRY1MRvb3ovAgDbp aFXAm0ssP0g7n3TLr2XOHHUaLQbsPjFKeMCjJaff6OnyliY0dDUUm9ACwqxN5BizOny5 6o6R1k1p3THp4VGKPD1gPgbF9N5SJmHFccehHvyHptaqGZoYeq1bOsTJYfzYeYbXFcTi n8+w== 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:content-transfer-encoding; bh=Rqn+GrCddwb17438Eb7/acjTBeKWly1ebyK4GCadnFc=; b=ToFay4Mj171KSgAatRKbwrZRLxjM1j14IwtUx4af3DJaHCEo7VueRmcCerm9u0R0h7 zMCIJxbIoFyCGq1qzK4YABwtmkqF0PntLqY09nZRgYRD8RzB9A/X3p1GJhNsUc5RCKP6 uWk1J5WaqRFqUQCCUG5PzoZaZKipTGQmvXcn9XX0C8/ZNhuZUMwB4Dhj4ACyr4ZYdx2e ymc/kiobx37ur98IjTzBoTdMD1xC6h9aVzW8ILceizEer8qFS6k9bbQK7p4aq2EPljxf kruW11Lf/cldZpSMD+3+2JxfkC+7+PxuoFUxevHwltYku8u/woi7+Z9YNDIecECvB1h+ dTnw== X-Gm-Message-State: AOAM532go+s8Liqomo/NuCHFO1Ipw7T2YHTQsQ4tMwZ/Rsm+vHZFNOeq +gL7u+jikXdELWQEo7dO/mZWzY4dfUMGg+NmOGw= X-Received: by 2002:a17:902:bb81:b0:12d:a7ec:3d85 with SMTP id m1-20020a170902bb8100b0012da7ec3d85mr7349833pls.17.1630047548297; Thu, 26 Aug 2021 23:59:08 -0700 (PDT) MIME-Version: 1.0 References: <20210624155526.2775863-1-tientzu@chromium.org> <20210624155526.2775863-11-tientzu@chromium.org> In-Reply-To: <20210624155526.2775863-11-tientzu@chromium.org> From: Andy Shevchenko Date: Fri, 27 Aug 2021 09:58:32 +0300 Message-ID: Subject: Re: [PATCH v15 10/12] swiotlb: Add restricted DMA pool initialization To: Claire Chang Cc: Rob Herring , Michael Ellerman , Joerg Roedel , Will Deacon , Frank Rowand , Konrad Rzeszutek Wilk , Boris Ostrovsky , Juergen Gross , Christoph Hellwig , Marek Szyprowski , benh@kernel.crashing.org, paulus@samba.org, "list@263.net:IOMMU DRIVERS" , sstabellini@kernel.org, Robin Murphy , grant.likely@arm.com, xypron.glpk@gmx.de, Thierry Reding , mingo@kernel.org, bauerman@linux.ibm.com, peterz@infradead.org, Greg KH , Saravana Kannan , "Rafael J . Wysocki" , heikki.krogerus@linux.intel.com, Andy Shevchenko , Randy Dunlap , Dan Williams , Bartosz Golaszewski , linux-devicetree , lkml , linuxppc-dev@lists.ozlabs.org, xen-devel@lists.xenproject.org, Nicolas Boichat , Jim Quinlan , tfiga@chromium.org, bskeggs@redhat.com, bhelgaas@google.com, chris@chris-wilson.co.uk, daniel@ffwll.ch, airlied@linux.ie, dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, jani.nikula@linux.intel.com, jxgao@google.com, joonas.lahtinen@linux.intel.com, linux-pci@vger.kernel.org, maarten.lankhorst@linux.intel.com, matthew.auld@intel.com, rodrigo.vivi@intel.com, thomas.hellstrom@linux.intel.com, thomas.lendacky@amd.com, quic_qiancai@quicinc.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 24, 2021 at 6:59 PM Claire Chang wrote: > > Add the initialization function to create restricted DMA pools from > matching reserved-memory nodes. > > Regardless of swiotlb setting, the restricted DMA pool is preferred if > available. > > The restricted DMA pools provide a basic level of protection against the > DMA overwriting buffer contents at unexpected times. However, to protect > against general data leakage and system memory corruption, the system > needs to provide a way to lock down the memory access, e.g., MPU. > +static int rmem_swiotlb_device_init(struct reserved_mem *rmem, > + struct device *dev) > +{ > + struct io_tlb_mem *mem =3D rmem->priv; > + unsigned long nslabs =3D rmem->size >> IO_TLB_SHIFT; > + > + /* > + * Since multiple devices can share the same pool, the private da= ta, > + * io_tlb_mem struct, will be initialized by the first device att= ached > + * to it. > + */ > + if (!mem) { Can it be rather if (mem) goto out_assign; or so? > + mem =3D kzalloc(struct_size(mem, slots, nslabs), GFP_KERN= EL); > + if (!mem) > + return -ENOMEM; > + > + set_memory_decrypted((unsigned long)phys_to_virt(rmem->ba= se), > + rmem->size >> PAGE_SHIFT); Below you are using a macro from pfn.h, but not here, I think it's PFN_DOWN= (). > + swiotlb_init_io_tlb_mem(mem, rmem->base, nslabs, false); > + mem->force_bounce =3D true; > + mem->for_alloc =3D true; > + > + rmem->priv =3D mem; > + > + if (IS_ENABLED(CONFIG_DEBUG_FS)) { > + mem->debugfs =3D > + debugfs_create_dir(rmem->name, debugfs_di= r); > + swiotlb_create_debugfs_files(mem); > + } > + } > + > + dev->dma_io_tlb_mem =3D mem; > + > + return 0; > +} > + > +static void rmem_swiotlb_device_release(struct reserved_mem *rmem, > + struct device *dev) > +{ > + dev->dma_io_tlb_mem =3D io_tlb_default_mem; > +} > + > +static const struct reserved_mem_ops rmem_swiotlb_ops =3D { > + .device_init =3D rmem_swiotlb_device_init, > + .device_release =3D rmem_swiotlb_device_release, > +}; > + > +static int __init rmem_swiotlb_setup(struct reserved_mem *rmem) > +{ > + unsigned long node =3D rmem->fdt_node; > + > + if (of_get_flat_dt_prop(node, "reusable", NULL) || > + of_get_flat_dt_prop(node, "linux,cma-default", NULL) || > + of_get_flat_dt_prop(node, "linux,dma-default", NULL) || > + of_get_flat_dt_prop(node, "no-map", NULL)) > + return -EINVAL; > + > + if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) { > + pr_err("Restricted DMA pool must be accessible within the= linear mapping."); > + return -EINVAL; > + } > + > + rmem->ops =3D &rmem_swiotlb_ops; > + pr_info("Reserved memory: created restricted DMA pool at %pa, siz= e %ld MiB\n", > + &rmem->base, (unsigned long)rmem->size / SZ_1M); Oh l=C3=A0 l=C3=A0, besides explicit casting that I believe can be avoided,= %ld !=3D unsigned long. Can you check the printk-formats.rst document? > + return 0; > +} > + > +RESERVEDMEM_OF_DECLARE(dma, "restricted-dma-pool", rmem_swiotlb_setup); > #endif /* CONFIG_DMA_RESTRICTED_POOL */ --=20 With Best Regards, Andy Shevchenko