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=-2.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no 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 85ED9C432BE for ; Fri, 27 Aug 2021 06:59:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6E51160FC4 for ; Fri, 27 Aug 2021 06:59:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244289AbhH0HAF (ORCPT ); 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-Google-Smtp-Source: ABdhPJzbMPfKsiNwRsmJ6wf35o+bM3Vsllxj/xaKs1Zg3gEKi2+LtrBilr6TwQqyx2Jk+YbMtx83ULLwDM5rxvcqh8o= 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