Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E1AACC433F5 for ; Mon, 2 May 2022 05:19:16 +0000 (UTC) Received: from localhost ([::1] helo=shelob.surriel.com) by shelob.surriel.com with esmtp (Exim 4.94.2) (envelope-from ) id 1nlOSp-0003TO-Du for kernelnewbies@archiver.kernel.org; Mon, 02 May 2022 01:19:15 -0400 Received: from mscreen.etri.re.kr ([129.254.9.16]) by shelob.surriel.com with esmtps (TLS1.2) tls TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA (Exim 4.94.2) (envelope-from ) id 1nlOR7-0001Lp-Rq for kernelnewbies@kernelnewbies.org; Mon, 02 May 2022 01:17:30 -0400 Received: from unknown (HELO send001-relay.gov-dooray.com) (211.180.235.152) by 129.254.9.16 with ESMTP; 2 May 2022 14:17:07 +0900 X-Original-SENDERIP: 211.180.235.152 X-Original-MAILFROM: ckim@etri.re.kr X-Original-RCPTTO: kernelnewbies@kernelnewbies.org Received: from [10.162.225.112] (HELO smtp002-imp.gov-dooray.com) ([10.162.225.112]) by send001-relay.gov-dooray.com with SMTP id fe514367626f6952; Mon, 02 May 2022 14:17:06 +0900 DKIM-Signature: a=rsa-sha256; b=kyLqEmVxDh1M2bu66uA8+miw/zpCLoeb1CI4wsdw7T4+DwDdp1D+WEm/yKkh3gyJo2n3sVRrOE IS1Bd9nEedugWLa9AU+tPGjG8gNHH0Mb9YqG2D+tQWL92/Iyk/97WPjyLVKxE1Yjat9tjZ2HkR2Y xrNviC8VODxp8h+0KWO+/E2IcNftxWtLgkPoWsAQj+7HVcWf9PdA3n/8+E8F/RL6Nf4h/Dl9mjAY 0NRApm57z6HhM6zMhQmTGRIAednzlmvCzdjUs8SC6qO9EGol1UN/G54qH+jWrEZPvCp0DlnHfSZp d3xfMjFClM6kpak7/rxM6XOmipNYIFj7P6MiFdYw==; c=relaxed/relaxed; s=selector; d=dooray.com; v=1; bh=WEbF8kuGPSrE7ZI5AFGouUFY/1MfAX1iYb2bupzO1/U=; h=From:To:Subject:Message-ID; Received: from [129.254.132.39] (HELO CHANKIMPC) ([129.254.132.39]) by smtp002-imp.gov-dooray.com with SMTP id 2973b62f626f6952; Mon, 02 May 2022 14:17:06 +0900 From: "Chan Kim" To: Subject: A question about iommu, passing to the device user data containing pointers Date: Mon, 2 May 2022 14:17:05 +0900 Message-ID: <05d601d85de3$dd1219e0$97364da0$@etri.re.kr> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adhd482NMQ0XkOZbRLSRvNt0DMp6vA== Content-Language: ko X-BeenThere: kernelnewbies@kernelnewbies.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Learn about the Linux kernel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kernelnewbies-bounces+kernelnewbies=archiver.kernel.org@kernelnewbies.org Hello linux experts, I'm almost sure my understanding is correct but hope someone could confirm me if it's really correct. Suppose a user program wants to pass to a device some data structures connected to each other using pointers and the device processes the data with dma capability(meaning it accesses the memory on its own). Here is a simple example. I want to add each member of array a and b and put it in array c and I pass the address of the struct which contains the addresses of inputs and output arrays. (This is a code for this question and can have some bugs but it's enough for my question. ) struct _myargs { int *a; // input pointer int *b; // input pointer int *c; // output pointer } myargs; int main() { int *a, *b, *c; a = malloc(100*sizeof(int)); b = malloc(100*sizeof(int)); c = malloc(100*sizeof(int)); ... fill inputs ... myargs.a = a; myargs.b = b; myargs.c = c; ... ioctl(fd, AddIt, &myargs); ... } Then, in the driver, I allocate the input and output buffers in kernel space and I do copy_from_user to copy the input data to kernel space input buffers. (and later do copy_to_user for the buffer c). my_ioctl(struct file *file, unsigned int cmd, unsigned long args_usr) { struct _myargs *args_ker; dma_addr_t args_dev; int *a_ker, *b_ker, *c_ker; dma_addr a_dev, b_dev, c_dev; args = dma_alloc_coherent(dev, sizeof(struct _myargs), &args_dev, GFP_KERNEL); switch(cmd) { case(AddIt): copy_from_user(args_ker, args_usr, sizeof(struct _myargs)); ak = dma_alloc_coherent(dev, 100*sizeof(int), &a_dev, GFP_KERNEL); bk = dma_alloc_coherent(dev, 100*sizeof(int), &b_dev, GFP_KERNEL); ck = dma_alloc_coherent(dev, 100*sizeof(int), &c_dev, GFP_KERNEL); copy_from_user(a_ker, args_ker.a, 100*sizeof(int)); copy_from_user(b_ker, args_ker.b, 100*sizeof(int)); args_ker.a = a_dev; args_ker.b = b_dev; args_ker.c = c_dev; writel(dev, &args_dev, REG_ARGS); // set args writel(dev, 1, REG_TRIGGER); // trigger hardware ... } } So the a, b arrays have been copied to kernel space address a_ker and b_ker but when I pass the args struct to the device, I used virtual io address (dma_addr_t) a_dev, b_dev and c_dev. And I want the device hardware to read the args struct from memory and use the address there to do array addition. I understand the iommu for the device (if correctly set and initialized) will do the (io virtual address) -> (device bus address) translation because the iommu subsystem would have setup the page table for the MMU(for kernel virtual address) and IOMMU(for io virtual address) for the buffers so that they can point to the same physical(or bus) address. I'm almost sure this will work but can't do a test now. Is my understanding correct? can someone well versed in the linux driver confirm me this approach is correct? _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies