Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2817221iog; Mon, 20 Jun 2022 05:33:01 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vi2iiLgCU4SfWUjpCyYXDU+WuopDqTIj09S647pJkhn8WrLtyQu5BmSbDk+94DguEj3x9k X-Received: by 2002:a17:90b:4f89:b0:1ec:a57c:eee8 with SMTP id qe9-20020a17090b4f8900b001eca57ceee8mr6327351pjb.220.1655728381196; Mon, 20 Jun 2022 05:33:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655728381; cv=none; d=google.com; s=arc-20160816; b=Ie377rcDHlgdXfL+em41+q9I4vruHRN00njYZzww0GjmhCzRqZe/lDGtKW5J0a5nSb Ue78wOVxDGiBvP5Cu3EAIKNkC+xmy+4AD6927MxFqgne6TGxHXouC8NZR4dmA/uOimou 4rCGy5OovSBYmSzhgfX5+M761yDvO9X5ascNjuWckhnpHVuIXA99NTMconfaGHGFEfxn 0HQ6e61aWvqsW97x3eWVBO9tUl9wpAP5XlzGXYdcjykcPu+DMRHGUTPo8f227Gda6rXc ICL9jAAyNoMgjXsGrhCsZ/9VKpOtV0xPO2NdKBfH7/sYE+w59eC6HpCTl2ADtnY4H0NJ dHRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=/5JxyhKk/uMc2fQhNdOt2zyaowNO+vOl5nRe37m7DAA=; b=CKti8zoTWwz6ugJf30/m0FMxxxEMcz5KkhP7eL4HwLHpMohTfUTwDbp2ZZ3jNlBTN5 B48+v3gsB5rD8zEedNk7VoZ2ADQD2fsgDKGnhFhCyz9pWk/qd2ka4bVnxALKfy9LVxG7 yNgQRi/ctmL8vcgcIk/vSbSn25ujD5QcnHVXKotzI8Tt9cXnIrpn/7RGCT8uoTLuCAln DZkSvcqdZOB0xmNrPVidwFsEektReiQTuStOrToA5R7Z/GQgn4RWQ1CLgvWkoB7BpQhE CetW+Ewix9dv67s6gUU4tVQU5dCvcIIl/XRx3zdCfhg7rEAfypF6ct5G25Vmed+v6drO /tPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PE6NpoyZ; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 33-20020a630e61000000b003fdb58c86b4si14242506pgo.675.2022.06.20.05.32.40; Mon, 20 Jun 2022 05:33:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PE6NpoyZ; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 S241607AbiFTMY2 (ORCPT + 99 others); Mon, 20 Jun 2022 08:24:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237517AbiFTMY2 (ORCPT ); Mon, 20 Jun 2022 08:24:28 -0400 Received: from mail-yb1-xb36.google.com (mail-yb1-xb36.google.com [IPv6:2607:f8b0:4864:20::b36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0C6217A83; Mon, 20 Jun 2022 05:24:26 -0700 (PDT) Received: by mail-yb1-xb36.google.com with SMTP id t1so18660230ybd.2; Mon, 20 Jun 2022 05:24:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=/5JxyhKk/uMc2fQhNdOt2zyaowNO+vOl5nRe37m7DAA=; b=PE6NpoyZLTwUcLsqzDbtJnrnxAIyaVdtaXAicCknBnBwh8ZdqPc+LdCbBV9G0aYWTp OH08dpop/cUwHHDAwRoOZn8NuJbm7em0mCvXsUn5KrkE2uLBzx4qEpmZlCmJ1JT+Dfo2 L2g+1N68ykc71e7QB9eNTR6L/SfFYKD49pmyUm+IL+kgwtlzsAFOFxYmtVjO901MBCa0 FXrtKRIwLIRyoFGhr+u6OUEnyG6aXt9C5BbkXb3gIq/00R90GPJyncy94IdAi1QYzzKR vgiDWLdFFfvrAAZFOdl0BFPX5q6CTFy21BUmyoGXj99JPg6vLckZ2H/SPEJs5BrMF/te qm2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/5JxyhKk/uMc2fQhNdOt2zyaowNO+vOl5nRe37m7DAA=; b=ulzaQyXilwMJyLSdhMr6e4xUI1Vr+aw6nnUIb/XhMNhwgK/0XpAFOgQJTDgSnyt2vn f7QZcYD5JTTvMojt1fTO7Tu1T4Inh4NWHO2pzfjpu2aTjzDN1D+/GbdMxa/fYIPitHl0 vUbAmktK0YtedM3Z242nsDsqxNBIoiEohcthW26QAMyaie9mk3cmAmDTpfY+Al5hMm8U ewZFsW8D6BRu0mbpn6B+yClICdpXgQXAl3UayDe5SjP/5vdBKuvLjLaY+cMeoOF8kusz VRUcfaflLBb8ylgK7v5SLDDvUOiKn3lxiCKerQ7Xv4EsQrfSXj4dibiHqar3L2IbziD/ AGaw== X-Gm-Message-State: AJIora+uELfFxKY17BIj0KIqHh7XGgPBuWKgikp2xWMJpeMyypRIw5Hv FhUlHVs8BXpOMCa4NueLe/MFlJDNoPWzblN4KYrCqJXhcJk1IAZW X-Received: by 2002:a25:c08f:0:b0:668:fcfc:1b2a with SMTP id c137-20020a25c08f000000b00668fcfc1b2amr6724569ybf.242.1655727866015; Mon, 20 Jun 2022 05:24:26 -0700 (PDT) MIME-Version: 1.0 References: <20220531200041.24904-1-alex.sierra@amd.com> <20220531200041.24904-2-alex.sierra@amd.com> <3ac89358-2ce0-7d0d-8b9c-8b0e5cc48945@redhat.com> <02ed2cb7-3ad3-8ffc-6032-04ae1853e234@amd.com> <87bkuo898d.fsf@nvdebian.thelocal> <877d5b90sp.fsf@nvdebian.thelocal> In-Reply-To: <877d5b90sp.fsf@nvdebian.thelocal> From: Oded Gabbay Date: Mon, 20 Jun 2022 15:23:59 +0300 Message-ID: Subject: Re: [PATCH v5 01/13] mm: add zone device coherent type memory support To: Alistair Popple Cc: "Sierra Guiza, Alejandro (Alex)" , David Hildenbrand , Jason Gunthorpe , rcampbell@nvidia.com, Matthew Wilcox , "Kuehling, Felix" , amd-gfx list , linux-xfs@vger.kernel.org, linux-mm , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Maling list - DRI developers , Andrew Morton , linux-ext4@vger.kernel.org, Christoph Hellwig Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Mon, Jun 20, 2022 at 11:50 AM Alistair Popple wrote: > > > Oded Gabbay writes: > > > On Mon, Jun 20, 2022 at 3:33 AM Alistair Popple wrote: > >> > >> > >> Oded Gabbay writes: > >> > >> > On Fri, Jun 17, 2022 at 8:20 PM Sierra Guiza, Alejandro (Alex) > >> > wrote: > >> >> > >> >> > >> >> On 6/17/2022 4:40 AM, David Hildenbrand wrote: > >> >> > On 31.05.22 22:00, Alex Sierra wrote: > >> >> >> Device memory that is cache coherent from device and CPU point of view. > >> >> >> This is used on platforms that have an advanced system bus (like CAPI > >> >> >> or CXL). Any page of a process can be migrated to such memory. However, > >> >> >> no one should be allowed to pin such memory so that it can always be > >> >> >> evicted. > >> >> >> > >> >> >> Signed-off-by: Alex Sierra > >> >> >> Acked-by: Felix Kuehling > >> >> >> Reviewed-by: Alistair Popple > >> >> >> [hch: rebased ontop of the refcount changes, > >> >> >> removed is_dev_private_or_coherent_page] > >> >> >> Signed-off-by: Christoph Hellwig > >> >> >> --- > >> >> >> include/linux/memremap.h | 19 +++++++++++++++++++ > >> >> >> mm/memcontrol.c | 7 ++++--- > >> >> >> mm/memory-failure.c | 8 ++++++-- > >> >> >> mm/memremap.c | 10 ++++++++++ > >> >> >> mm/migrate_device.c | 16 +++++++--------- > >> >> >> mm/rmap.c | 5 +++-- > >> >> >> 6 files changed, 49 insertions(+), 16 deletions(-) > >> >> >> > >> >> >> diff --git a/include/linux/memremap.h b/include/linux/memremap.h > >> >> >> index 8af304f6b504..9f752ebed613 100644 > >> >> >> --- a/include/linux/memremap.h > >> >> >> +++ b/include/linux/memremap.h > >> >> >> @@ -41,6 +41,13 @@ struct vmem_altmap { > >> >> >> * A more complete discussion of unaddressable memory may be found in > >> >> >> * include/linux/hmm.h and Documentation/vm/hmm.rst. > >> >> >> * > >> >> >> + * MEMORY_DEVICE_COHERENT: > >> >> >> + * Device memory that is cache coherent from device and CPU point of view. This > >> >> >> + * is used on platforms that have an advanced system bus (like CAPI or CXL). A > >> >> >> + * driver can hotplug the device memory using ZONE_DEVICE and with that memory > >> >> >> + * type. Any page of a process can be migrated to such memory. However no one > >> >> > Any page might not be right, I'm pretty sure. ... just thinking about special pages > >> >> > like vdso, shared zeropage, ... pinned pages ... > >> >> > >> >> Hi David, > >> >> > >> >> Yes, I think you're right. This type does not cover all special pages. > >> >> I need to correct that on the cover letter. > >> >> Pinned pages are allowed as long as they're not long term pinned. > >> >> > >> >> Regards, > >> >> Alex Sierra > >> > > >> > What if I want to hotplug this device's coherent memory, but I do > >> > *not* want the OS > >> > to migrate any page to it ? > >> > I want to fully-control what resides on this memory, as I can consider > >> > this memory > >> > "expensive". i.e. I don't have a lot of it, I want to use it for > >> > specific purposes and > >> > I don't want the OS to start using it when there is some memory pressure in > >> > the system. > >> > >> This is exactly what MEMORY_DEVICE_COHERENT is for. Device coherent > >> pages are only allocated by a device driver and exposed to user-space by > >> a driver migrating pages to them with migrate_vma. The OS can't just > >> start using them due to memory pressure for example. > >> > >> - Alistair > > Thanks for the explanation. > > > > I guess the commit message confused me a bit, especially these two sentences: > > > > "Any page of a process can be migrated to such memory. However no one should be > > allowed to pin such memory so that it can always be evicted." > > > > I read them as if the OS is free to choose which pages are migrated to > > this memory, > > and anything is eligible for migration to that memory (and that's why > > we also don't > > allow it to pin memory there). > > > > If we are not allowed to pin anything there, can the device driver > > decide to disable > > any option for oversubscription of this memory area ? > > I'm not sure I follow your thinking on how oversubscription would work > here, however all allocations are controlled by the driver. So if a > device's coherent memory is full a driver would be unable to migrate > pages to that device until pages are freed by the OS due to being > unmapped or the driver evicts pages by migrating them back to normal CPU > memory. > > Pinning of pages is allowed, and could prevent such migrations. However > this patch series prevents device coherent pages from being pinned > longterm (ie. with FOLL_LONGTERM), so it should always be able to evict > pages eventually. > > > Let's assume the user uses this memory area for doing p2p with other > > CXL devices. > > In that case, I wouldn't want the driver/OS to migrate pages in and > > out of that memory... > > The OS will not migrate pages in or out (although it may free them if no > longer required), but a driver might choose to. So at the moment it's > really up to the driver to implement what you want in this regards. I see. In other words, we don't want to allow long-term pinning but the driver can decide it doesn't want to evict pages out of that memory, until they are freed. Thanks, Oded > > > So either I should let the user pin those pages, or prevent him from > > doing (accidently or not) > > oversubscription in this memory area. > > As noted above pages can be pinned, but not long-term. > > - Alistair > > > wdyt ? > > > >> > >> > Oded > >> > > >> >> > >> >> > > >> >> >> + * should be allowed to pin such memory so that it can always be evicted. > >> >> >> + * > >> >> >> * MEMORY_DEVICE_FS_DAX: > >> >> >> * Host memory that has similar access semantics as System RAM i.e. DMA > >> >> >> * coherent and supports page pinning. In support of coordinating page > >> >> >> @@ -61,6 +68,7 @@ struct vmem_altmap { > >> >> >> enum memory_type { > >> >> >> /* 0 is reserved to catch uninitialized type fields */ > >> >> >> MEMORY_DEVICE_PRIVATE = 1, > >> >> >> + MEMORY_DEVICE_COHERENT, > >> >> >> MEMORY_DEVICE_FS_DAX, > >> >> >> MEMORY_DEVICE_GENERIC, > >> >> >> MEMORY_DEVICE_PCI_P2PDMA, > >> >> >> @@ -143,6 +151,17 @@ static inline bool folio_is_device_private(const struct folio *folio) > >> >> > In general, this LGTM, and it should be correct with PageAnonExclusive I think. > >> >> > > >> >> > > >> >> > However, where exactly is pinning forbidden? > >> >> > >> >> Long-term pinning is forbidden since it would interfere with the device > >> >> memory manager owning the > >> >> device-coherent pages (e.g. evictions in TTM). However, normal pinning > >> >> is allowed on this device type. > >> >> > >> >> Regards, > >> >> Alex Sierra > >> >> > >> >> >