Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp666976ybp; Fri, 11 Oct 2019 02:20:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqybZ7q2Zin7vt3bpZwsvniNYoKDdcFXlKWHcDByX/FE7T6G3S4eNKtkkVXf4G6sBtsbxsjF X-Received: by 2002:a17:906:1f16:: with SMTP id w22mr12859233ejj.5.1570785642294; Fri, 11 Oct 2019 02:20:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570785642; cv=none; d=google.com; s=arc-20160816; b=UeJTycvMPCCrEaMSq2LOJT59hTsR0+6WWsyE59W5PyX72U8NB3tmnpX7XzF+82lk1O 7uZlv9NXFL3e92QgOyiRtpn5OwK7KhrAdmg956P5ArNHJUczfixfDdBJdbKZjpUHSxQQ qUxDI/uJ3Tc38yce8v0YjwrBdZK7Tt8Y33LOKCz+ELT1atPT2fesvnXTGugr3LyXXKSv Yn0yrk7nPNAK6slGk8mvC0RHvzGzlBK0RS1LrGikNBi6w2XW6NhEHnhnDiMzxG5hJXZf INoCYWtqMqGsvVA7Y1zMN7wsqeMfmmR0WeBwi0ombdeMwg1WtfyegoRmWt39lgKP7cQE IB9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=TFXaZXUVnWUo7FUoeyK+pimFP0T4TRsHMwFzrX99AMw=; b=cGYiDX9CXZjcifkv0OUNBkpyh8sx+ZmPR5tlM08cB1rSS7i8Mjd0yDqw9HjYJ2XjAs 7/dwoVlwy1YyxQCdALrcvy3Hsrr+aQt+U9fmR9Y4i5Ml0RkQZXgOFVcJ4vDJwAUx3WkW pi8Q0Xi/Qcudhq2UyAsBEJbAuZYORspcAPhdKytMAEwYdErC6xpRDThPXePMwWyxKk30 C29G8Wlo3a+WG+GuqeKHBJLYfWS3oygntMtWVaI6u4FUtqBokv/cr4GYSBoOULwrQ3Jg ZUDUV2mBJDUJ9Q86p8O0xIQJaTzqAkWfv9OKTK55OHvGBj6tKsB4ll8x/b1c6R0n7jEm cpGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qrLANnnP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id g5si4865216ejp.418.2019.10.11.02.20.19; Fri, 11 Oct 2019 02:20:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qrLANnnP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727653AbfJKJUD (ORCPT + 99 others); Fri, 11 Oct 2019 05:20:03 -0400 Received: from mail-ua1-f68.google.com ([209.85.222.68]:37179 "EHLO mail-ua1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727125AbfJKJUD (ORCPT ); Fri, 11 Oct 2019 05:20:03 -0400 Received: by mail-ua1-f68.google.com with SMTP id w7so2831283uag.4 for ; Fri, 11 Oct 2019 02:20:02 -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=TFXaZXUVnWUo7FUoeyK+pimFP0T4TRsHMwFzrX99AMw=; b=qrLANnnPzdE4J92iqNq5PD6fjNxUOyVwpzFp6BQ5hiBx78tosvQ9MWO+uZ2mjESNV/ 4guvp0g5b5GIxWNCETw0OzGJxu3nigYwbkoaHXJVkm8X8BQWsi1ETz5Hp4ab3h/Y1Pxv 8FqXstBi5Zgpfo4QSwhnhVFaj+/m9NuDX0vh8paTuTg5mmfRNlBSQmdIqeJlxrXv8Dcb zi6cEQhXSnWvu/8W8298JU3aS5Qw5i5NmpINkMf0N5taz58b+IOhMSkrqcvZqGxulcJ7 xkZFN2+7ek3+shHZUc58s+5P2X66/b785Ifhaav6eOeZftbD/dYtOqT9oi1YiQArBQGB mFgA== 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=TFXaZXUVnWUo7FUoeyK+pimFP0T4TRsHMwFzrX99AMw=; b=ItO+y74YVC8hGYJKpg+WbkyoEh+fQKO8vYNA5qBOlCZH1S26KgHlRMYrw3NF9BXW2R gpBHb8OdU9uy9k0ROuKJLEjh2sY0+4PRsADJTy8+JW9G/pacMlN8eKsyJPJh5N+pKEQi 4qJ4hl4CfX/DhYnyjjzIeo8ZMNScdjdgiYmatgTiFMYGdlp/9EQ5Jj24RdT+ag2E0uCj aE938UudR+LOLyAA9aqRAKRNTdc1hWark+qmcn/1fE+dGCdxC/dtytXOvaOM5Ri7VI21 Ld96hd/X22yWJxuNiY6UEcc1fGVoMpaFtOQDsyLjxZ+vEEP7PJBH5UdBBPjz9llif2eN 30Cw== X-Gm-Message-State: APjAAAUzLy1nzbov9gogOZMjqg6BZHb9IrRKNxoKkCgxzfDW5c6OYcs0 Y6bxEzdhQTOk/YvJS0AkYVWeOq8eAUn5XIZxgX9sRHbzaFc= X-Received: by 2002:ab0:d82:: with SMTP id i2mr6008603uak.34.1570785602088; Fri, 11 Oct 2019 02:20:02 -0700 (PDT) MIME-Version: 1.0 References: <20191010140615.26460-1-oshpigelman@habana.ai> <20191010140950.GA27176@infradead.org> <20191011081055.GA9052@infradead.org> In-Reply-To: <20191011081055.GA9052@infradead.org> From: Oded Gabbay Date: Fri, 11 Oct 2019 12:19:36 +0300 Message-ID: Subject: Re: [PATCH 1/2] habanalabs: support vmalloc memory mapping To: Christoph Hellwig Cc: Omer Shpigelman , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 11, 2019 at 11:10 AM Christoph Hellwig wrot= e: > > On Thu, Oct 10, 2019 at 07:54:07PM +0000, Omer Shpigelman wrote: > > The is_vmalloc_addr checks are for user pointers and for memory which w= as allocated by the driver with vmalloc_user. > > This does not make any sense whatsoever. vmalloc_user returns a kernel > address, it just does a GFP_USER instead of GFP_KERNEL allocation, which > is just accounting differences. > > > > > Mapping vmalloc memory is needed for Gaudi ASIC. > > > > > > How does that ASIC pass in the vmalloc memory? I don't fully underst= and > > > the code, but it seems like the addresses are fed from ioctl, which m= eans > > > they only come from userspace. > > > > The user pointers are indeed fed from ioctl for DMA purpose, but as I w= rote above the vmalloc memory is allocated by the driver with vmalloc_user = which will be mmapped later on in order to create a shared buffer between t= he driver and the userspace process. > > Again, you can't pass pointers obtained from vmalloc* to userspace. You > can map the underlying pages into user pagetables, but is_vmalloc_addr > won't know that. I think you guys need to read up on virtual memory 101 > first and then come back and actually explain what you are trying to do. Hi Christoph, I think the confusion here is because this is only a pre-requisite patch, which doesn't show the full code of how we use vmalloc_user. So I want to describe the full flow here and please tell me if and what we are doing wrong. We first allocate, using vmalloc_user, a certain memory block that will be used by the ASIC and the user (ASIC is producer, user is consumer). After we use vmalloc_user, we map the *kernel* pointer we got from the vmalloc_user() to the ASIC MMU. We reuse our driver's generic code path to map host memory to ASIC MMU and that's why we need the patch above. The user does NOT send us the pointer. He doesn't have this pointer. It is internal to the kernel driver. To do this reuse, we added a call to the is_vmalloc_addr(), so the function will know if it is called to work on user pointers, or on vmalloc *kernel* pointers. What the user does get is an opaque handle. Later he calls mmap() with our FD and this handle. In the driver, we do remap_vmalloc_range() on the *kernel* pointer we retrieved using the opaque handle we got from the user, and return a valid user-space process address that points to this memory block, so the user can access it. AFAIK from my GPU days, this is totally valid. The user does NOT see kernel pointers. There is total separation between kernel and user pointers. User get pointers only by calling mmap(). We NEVER return a pointer to a user in an IOCTL. Only through mmap. Please tell me if you see a problem here. If you need more details, I'll happy to provide them. Side note: The reason the driver allocates the memory block and not the user itself (like we did so far in all our code), is because this block is mapped to our ASIC internal MMU using a priviliged ASID. I don't want to give the user a uapi that will enable him to request to map memory to the ASIC using a priviliged ASID. Therefore, the kernel driver does it internally. Thanks, Oded