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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAA17C433FE for ; Tue, 22 Nov 2022 18:08:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232565AbiKVSIp (ORCPT ); Tue, 22 Nov 2022 13:08:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234420AbiKVSIj (ORCPT ); Tue, 22 Nov 2022 13:08:39 -0500 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74144697E8 for ; Tue, 22 Nov 2022 10:08:38 -0800 (PST) Received: by mail-ej1-x62e.google.com with SMTP id bj12so37540324ejb.13 for ; Tue, 22 Nov 2022 10:08:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=yeE/fvHo7zplB3H0njfvx+KBD+UsXtgVtTcTsJSQKTs=; b=HJQzg3HAQLfej9Lynj1eAMPPLxHBUWytNt/6mGkTn4aeoz+YFCARsIB3utatEXxeDD OyHr1lPIXUf7zx9qQWafuBhFg7CdNJSUeoRB61i9yhQ22q5typ8pT/pxa5z5QY5lUTEh gk+HrYz68CeLLiJM2B8IZENBotgdF7bB+GjpA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yeE/fvHo7zplB3H0njfvx+KBD+UsXtgVtTcTsJSQKTs=; b=RH/6F2lIA1tZc7OU81IfIDeD7ZFmktLQM47NjenpZhJzCIidig+MwEpJf2Rr89ZUue lofe1jPZyNTWmzSPqSprYVoxvhGgkdkSi8rkqzhrLcVM28ocL5HL9xEQ9UQhoxhg54IM LCoRaEbo24iYkXtHi0f/ZsusglTtJn8RBDw/QXB7QUkgsZZZnQltcAFjiCiAhj6yla2V stZwW4BLui5sUQoyPjfvjjx00/dACFKPLVb07U+GZihQmLQ+1VhvrBCkchbLBpXY1lKV V7I1jHPKvT0fxETBO1Kime+4tT3NCsSADv90OW04rYTSxfoKV9R/oQk92ztefd3X8s4Y 0Riw== X-Gm-Message-State: ANoB5pltUX9u08aX055IX2pLwbYGsLatKNJctOTl6vNtaL6fFsF8LYs8 Cc8z90EED3Az2ruU9wU+dDNUgq9iO8PiKu2S7p6MzQ== X-Google-Smtp-Source: AA0mqf5YaIer5B7BWrJ7ocvdT+1R5W8bGrPu5RkAiIG9/MkRdfGUud/7t1Fi3LwOKtyXP+dS2/LSLhbZV2Y6VlJIN58= X-Received: by 2002:a17:906:414c:b0:7a9:ecc1:2bd2 with SMTP id l12-20020a170906414c00b007a9ecc12bd2mr8382937ejk.545.1669140516986; Tue, 22 Nov 2022 10:08:36 -0800 (PST) MIME-Version: 1.0 References: <20221122170801.842766-1-daniel.vetter@ffwll.ch> In-Reply-To: From: Daniel Vetter Date: Tue, 22 Nov 2022 19:08:25 +0100 Message-ID: Subject: Re: [PATCH] dma-buf: Require VM_PFNMAP vma for mmap To: Jason Gunthorpe Cc: DRI Development , Intel Graphics Development , =?UTF-8?Q?Christian_K=C3=B6nig?= , Thomas Zimmermann , Suren Baghdasaryan , Matthew Wilcox , John Stultz , Daniel Vetter , Sumit Semwal , linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org On Tue, 22 Nov 2022 at 19:04, Jason Gunthorpe wrote: > > On Tue, Nov 22, 2022 at 06:08:00PM +0100, Daniel Vetter wrote: > > tldr; DMA buffers aren't normal memory, expecting that you can use > > them like that (like calling get_user_pages works, or that they're > > accounting like any other normal memory) cannot be guaranteed. > > > > Since some userspace only runs on integrated devices, where all > > buffers are actually all resident system memory, there's a huge > > temptation to assume that a struct page is always present and useable > > like for any more pagecache backed mmap. This has the potential to > > result in a uapi nightmare. > > > > To stop this gap require that DMA buffer mmaps are VM_PFNMAP, which > > blocks get_user_pages and all the other struct page based > > infrastructure for everyone. In spirit this is the uapi counterpart to > > the kernel-internal CONFIG_DMABUF_DEBUG. > > > > Motivated by a recent patch which wanted to swich the system dma-buf > > heap to vm_insert_page instead of vm_insert_pfn. > > > > v2: > > > > Jason brought up that we also want to guarantee that all ptes have the > > pte_special flag set, to catch fast get_user_pages (on architectures > > that support this). Allowing VM_MIXEDMAP (like VM_SPECIAL does) would > > still allow vm_insert_page, but limiting to VM_PFNMAP will catch that. > > > > From auditing the various functions to insert pfn pte entires > > (vm_insert_pfn_prot, remap_pfn_range and all it's callers like > > dma_mmap_wc) it looks like VM_PFNMAP is already required anyway, so > > this should be the correct flag to check for. > > I didn't look at how this actually gets used, but it is a bit of a > pain to insert a lifetime controlled object like a struct page as a > special PTE/VM_PFNMAP > > How is the lifetime model implemented here? How do you know when > userspace has finally unmapped the page? The vma has a filp which is the refcounted dma_buf. With dma_buf you never get an individual page it's always the entire object. And it's up to the allocator how exactly it wants to use or not use the page's refcount. So if gup goes in and elevates the refcount, you can break stuff, which is why I'm doing this. -Daniel -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch 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 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 5F402C4332F for ; Tue, 22 Nov 2022 18:08:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8457210E431; Tue, 22 Nov 2022 18:08:45 +0000 (UTC) Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by gabe.freedesktop.org (Postfix) with ESMTPS id 86B1C10E431 for ; Tue, 22 Nov 2022 18:08:38 +0000 (UTC) Received: by mail-ej1-x636.google.com with SMTP id n20so37760272ejh.0 for ; Tue, 22 Nov 2022 10:08:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=yeE/fvHo7zplB3H0njfvx+KBD+UsXtgVtTcTsJSQKTs=; b=HJQzg3HAQLfej9Lynj1eAMPPLxHBUWytNt/6mGkTn4aeoz+YFCARsIB3utatEXxeDD OyHr1lPIXUf7zx9qQWafuBhFg7CdNJSUeoRB61i9yhQ22q5typ8pT/pxa5z5QY5lUTEh gk+HrYz68CeLLiJM2B8IZENBotgdF7bB+GjpA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yeE/fvHo7zplB3H0njfvx+KBD+UsXtgVtTcTsJSQKTs=; b=AVY1CaytyRasjjU2V2Ah1waW/sUwk6ymgtRob7611PseppWUPihLIcWsndeH8/I9ob 7dsAGb4D14+eIvMNOYy/IiZb+vrbHqK9x8FoMk6fRKDl2MNmLzhmTR7PZeALGadinQY1 hgaliAPOwXSXPKT/vTrShyVd12VKoy8wm4N7slw/lvvoglC8nRoiXm7cEaOCBzgIT3Qc 6iTBfOyGND3nXf+vQOWK2ZmK2NastyxTj06sxBXjJEK41Q39YaqV7AD7WO9S+TIUdPdZ oNfJ3WVZfYIrwy0xc6B93DBy3e2RaSzCXmUp7ffs3PD85irCkPamCvmTmmYqfCExaWyt W7xQ== X-Gm-Message-State: ANoB5pkQcCO639EdS28+xWMsO6bm/XPf7oLitY+YVkOBBPzEcuPfYsJa ZuFK4BtsjQQx8MAqDkLFPLqmKgTQ4oW+z+E3/V3UDQ== X-Google-Smtp-Source: AA0mqf5YaIer5B7BWrJ7ocvdT+1R5W8bGrPu5RkAiIG9/MkRdfGUud/7t1Fi3LwOKtyXP+dS2/LSLhbZV2Y6VlJIN58= X-Received: by 2002:a17:906:414c:b0:7a9:ecc1:2bd2 with SMTP id l12-20020a170906414c00b007a9ecc12bd2mr8382937ejk.545.1669140516986; Tue, 22 Nov 2022 10:08:36 -0800 (PST) MIME-Version: 1.0 References: <20221122170801.842766-1-daniel.vetter@ffwll.ch> In-Reply-To: From: Daniel Vetter Date: Tue, 22 Nov 2022 19:08:25 +0100 Message-ID: Subject: Re: [PATCH] dma-buf: Require VM_PFNMAP vma for mmap To: Jason Gunthorpe Content-Type: text/plain; charset="UTF-8" X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Intel Graphics Development , Matthew Wilcox , Sumit Semwal , linaro-mm-sig@lists.linaro.org, John Stultz , DRI Development , Thomas Zimmermann , Daniel Vetter , Suren Baghdasaryan , =?UTF-8?Q?Christian_K=C3=B6nig?= , linux-media@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" On Tue, 22 Nov 2022 at 19:04, Jason Gunthorpe wrote: > > On Tue, Nov 22, 2022 at 06:08:00PM +0100, Daniel Vetter wrote: > > tldr; DMA buffers aren't normal memory, expecting that you can use > > them like that (like calling get_user_pages works, or that they're > > accounting like any other normal memory) cannot be guaranteed. > > > > Since some userspace only runs on integrated devices, where all > > buffers are actually all resident system memory, there's a huge > > temptation to assume that a struct page is always present and useable > > like for any more pagecache backed mmap. This has the potential to > > result in a uapi nightmare. > > > > To stop this gap require that DMA buffer mmaps are VM_PFNMAP, which > > blocks get_user_pages and all the other struct page based > > infrastructure for everyone. In spirit this is the uapi counterpart to > > the kernel-internal CONFIG_DMABUF_DEBUG. > > > > Motivated by a recent patch which wanted to swich the system dma-buf > > heap to vm_insert_page instead of vm_insert_pfn. > > > > v2: > > > > Jason brought up that we also want to guarantee that all ptes have the > > pte_special flag set, to catch fast get_user_pages (on architectures > > that support this). Allowing VM_MIXEDMAP (like VM_SPECIAL does) would > > still allow vm_insert_page, but limiting to VM_PFNMAP will catch that. > > > > From auditing the various functions to insert pfn pte entires > > (vm_insert_pfn_prot, remap_pfn_range and all it's callers like > > dma_mmap_wc) it looks like VM_PFNMAP is already required anyway, so > > this should be the correct flag to check for. > > I didn't look at how this actually gets used, but it is a bit of a > pain to insert a lifetime controlled object like a struct page as a > special PTE/VM_PFNMAP > > How is the lifetime model implemented here? How do you know when > userspace has finally unmapped the page? The vma has a filp which is the refcounted dma_buf. With dma_buf you never get an individual page it's always the entire object. And it's up to the allocator how exactly it wants to use or not use the page's refcount. So if gup goes in and elevates the refcount, you can break stuff, which is why I'm doing this. -Daniel -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch 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 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 3C51CC4332F for ; Tue, 22 Nov 2022 18:08:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F375C10E430; Tue, 22 Nov 2022 18:08:41 +0000 (UTC) Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by gabe.freedesktop.org (Postfix) with ESMTPS id 79B7710E430 for ; Tue, 22 Nov 2022 18:08:38 +0000 (UTC) Received: by mail-ej1-x636.google.com with SMTP id ft34so37541882ejc.12 for ; Tue, 22 Nov 2022 10:08:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=yeE/fvHo7zplB3H0njfvx+KBD+UsXtgVtTcTsJSQKTs=; b=HJQzg3HAQLfej9Lynj1eAMPPLxHBUWytNt/6mGkTn4aeoz+YFCARsIB3utatEXxeDD OyHr1lPIXUf7zx9qQWafuBhFg7CdNJSUeoRB61i9yhQ22q5typ8pT/pxa5z5QY5lUTEh gk+HrYz68CeLLiJM2B8IZENBotgdF7bB+GjpA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yeE/fvHo7zplB3H0njfvx+KBD+UsXtgVtTcTsJSQKTs=; b=IUda6A9MC7s8ya/2ygLlauvlREnSCcrKU4e2WD47eXgk/7BpckZN8HYvQOVAPwJQHt 3JzO+kg/nCFe3LlxYm37SbtMk8tfG9P5u47ltrlTIWwZeLAVUplF5+80tan2Gtnv37zW DXSBYrmd4O2y8tdavqYpFfPCAjJaMS1oBXdcgiveNDCNPaF0yYzEQkfdEcFy0jYWtnWb ifM7WCCg6F0BedD4tmhAiRnRjjjIWUSy+ZvvIgVxtkpl80Yb6UZXCmhccca/mK2WwiXE aCHp77j3qKcTGK0ySUlYXLL8yP1BwwkDam7I+u9jccc7RuV7MUWrpxhRFlGKGJQecWSS 3TlA== X-Gm-Message-State: ANoB5pmA0Le2ivDnfVcU+Aru/ah0WpC1BR3dCywKn7z9nNYKYwIkVkOr rpSmD3+b02Miy5DcPbzy2Klx4UmfMQL25SRumRWkLg== X-Google-Smtp-Source: AA0mqf5YaIer5B7BWrJ7ocvdT+1R5W8bGrPu5RkAiIG9/MkRdfGUud/7t1Fi3LwOKtyXP+dS2/LSLhbZV2Y6VlJIN58= X-Received: by 2002:a17:906:414c:b0:7a9:ecc1:2bd2 with SMTP id l12-20020a170906414c00b007a9ecc12bd2mr8382937ejk.545.1669140516986; Tue, 22 Nov 2022 10:08:36 -0800 (PST) MIME-Version: 1.0 References: <20221122170801.842766-1-daniel.vetter@ffwll.ch> In-Reply-To: From: Daniel Vetter Date: Tue, 22 Nov 2022 19:08:25 +0100 Message-ID: To: Jason Gunthorpe Content-Type: text/plain; charset="UTF-8" Subject: Re: [Intel-gfx] [PATCH] dma-buf: Require VM_PFNMAP vma for mmap X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Intel Graphics Development , Matthew Wilcox , Sumit Semwal , linaro-mm-sig@lists.linaro.org, John Stultz , DRI Development , Thomas Zimmermann , Daniel Vetter , Suren Baghdasaryan , =?UTF-8?Q?Christian_K=C3=B6nig?= , linux-media@vger.kernel.org Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" On Tue, 22 Nov 2022 at 19:04, Jason Gunthorpe wrote: > > On Tue, Nov 22, 2022 at 06:08:00PM +0100, Daniel Vetter wrote: > > tldr; DMA buffers aren't normal memory, expecting that you can use > > them like that (like calling get_user_pages works, or that they're > > accounting like any other normal memory) cannot be guaranteed. > > > > Since some userspace only runs on integrated devices, where all > > buffers are actually all resident system memory, there's a huge > > temptation to assume that a struct page is always present and useable > > like for any more pagecache backed mmap. This has the potential to > > result in a uapi nightmare. > > > > To stop this gap require that DMA buffer mmaps are VM_PFNMAP, which > > blocks get_user_pages and all the other struct page based > > infrastructure for everyone. In spirit this is the uapi counterpart to > > the kernel-internal CONFIG_DMABUF_DEBUG. > > > > Motivated by a recent patch which wanted to swich the system dma-buf > > heap to vm_insert_page instead of vm_insert_pfn. > > > > v2: > > > > Jason brought up that we also want to guarantee that all ptes have the > > pte_special flag set, to catch fast get_user_pages (on architectures > > that support this). Allowing VM_MIXEDMAP (like VM_SPECIAL does) would > > still allow vm_insert_page, but limiting to VM_PFNMAP will catch that. > > > > From auditing the various functions to insert pfn pte entires > > (vm_insert_pfn_prot, remap_pfn_range and all it's callers like > > dma_mmap_wc) it looks like VM_PFNMAP is already required anyway, so > > this should be the correct flag to check for. > > I didn't look at how this actually gets used, but it is a bit of a > pain to insert a lifetime controlled object like a struct page as a > special PTE/VM_PFNMAP > > How is the lifetime model implemented here? How do you know when > userspace has finally unmapped the page? The vma has a filp which is the refcounted dma_buf. With dma_buf you never get an individual page it's always the entire object. And it's up to the allocator how exactly it wants to use or not use the page's refcount. So if gup goes in and elevates the refcount, you can break stuff, which is why I'm doing this. -Daniel -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch