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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFCA2C4332F for ; Mon, 17 Oct 2022 11:27:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E4416B0072; Mon, 17 Oct 2022 07:27:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 494506B0074; Mon, 17 Oct 2022 07:27:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 35BE86B0075; Mon, 17 Oct 2022 07:27:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 224B56B0072 for ; Mon, 17 Oct 2022 07:27:33 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B30A2140E9B for ; Mon, 17 Oct 2022 11:27:32 +0000 (UTC) X-FDA: 80030215944.02.7D15AD6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 9FC5F180035 for ; Mon, 17 Oct 2022 11:27:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666006050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bGH9Zdddmn1G3Br6JkTmmzZUWrMZ3aJCiDnziiYbJRc=; b=L93wMjjuL1nGutriHGhRwkftunX6UwlvGJdiW533WAXVi5oZfQg4TS+C6wvL6u8wyLGZ34 Qt+VLGmJlZnOTo3W0rZJPYncKqpvYNnRvy/dTEgwbGmuyb0DiS5GekqzBsUpdEw+4xNNhg 1KZZMoukA3lkeSmyhBPSgciGjCqGiV8= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-145-kGuELhRWPwueHJ0uK664iA-1; Mon, 17 Oct 2022 07:27:27 -0400 X-MC-Unique: kGuELhRWPwueHJ0uK664iA-1 Received: by mail-wr1-f71.google.com with SMTP id p7-20020adfba87000000b0022cc6f805b1so3623240wrg.21 for ; Mon, 17 Oct 2022 04:27:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bGH9Zdddmn1G3Br6JkTmmzZUWrMZ3aJCiDnziiYbJRc=; b=2SSwdJmtjfZJyI1qpYDbhN5GcrEdIT48TUf3h/KhZNECHoaHW0ygr1ASpVF9LMoeNs 7Z7e3X+4wZamJ/fLzFzJ3MlRhxGaPNZUuIZEHDRabjp7unmfE4Aw2rUGbG9gLGQ6knS+ f19P7k3UD9vt+qLkHcX3t+mY0ShympuJEU1u4x71nxlWovUUcpBM24XHNMyzp5b0pEh8 JzHCcM02Dfw145f4Dc0ixOGZK5rB7Yc2+F9AWh6bVjUmfVXxPXq/PruKV/OruCvWr5Pm iGXG0uyM+82DdJcl8UVTvEroR+1B7G6pcIbmYu4IZFQiskjYeeAy3wGApBibawn4tBHH yPJA== X-Gm-Message-State: ACrzQf1AHVr61omVNuDlOYxwUXGb/Pm/Q2+4VXKgvsxYIUVPpNoU+aeu bd4lvflEnzz3OFoqV5J4n79YLf1i/vOXGqwt0xubS7q9cRNS+uuiZ/+4sztpGfPuoRhX7UPv8dN CCR9IxAjVjfM= X-Received: by 2002:adf:d842:0:b0:22e:33e2:f379 with SMTP id k2-20020adfd842000000b0022e33e2f379mr5758279wrl.23.1666006046482; Mon, 17 Oct 2022 04:27:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Kvz5id6HZOC9QTd4Wc7N8RB+Y9HN7WUbMZCORphKnB3Rk0JATW+xIueW/y00yJUTBR4GOgQ== X-Received: by 2002:adf:d842:0:b0:22e:33e2:f379 with SMTP id k2-20020adfd842000000b0022e33e2f379mr5758262wrl.23.1666006046160; Mon, 17 Oct 2022 04:27:26 -0700 (PDT) Received: from ?IPV6:2003:cb:c70a:a00:37ed:519:6c33:4dc8? (p200300cbc70a0a0037ed05196c334dc8.dip0.t-ipconnect.de. [2003:cb:c70a:a00:37ed:519:6c33:4dc8]) by smtp.gmail.com with ESMTPSA id s16-20020a5d4250000000b0022e47b57735sm8105509wrr.97.2022.10.17.04.27.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 17 Oct 2022 04:27:25 -0700 (PDT) Message-ID: Date: Mon, 17 Oct 2022 13:27:24 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 To: Baolin Wang , akpm@linux-foundation.org Cc: arnd@arndb.de, jingshan@linux.alibaba.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org References: <6227ba4c-9455-9652-7434-7842b2b3edcb@redhat.com> <8007f4fc-d2e6-7aae-7297-805326adce2a@linux.alibaba.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [RFC PATCH] mm: Introduce new MADV_NOMOVABLE behavior In-Reply-To: <8007f4fc-d2e6-7aae-7297-805326adce2a@linux.alibaba.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666006052; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bGH9Zdddmn1G3Br6JkTmmzZUWrMZ3aJCiDnziiYbJRc=; b=F6Kg7JeF0Lw6/1eOYRtzsV9vWhMjE7N/DAspSazGBa1Ra+NdFDrOO8L0LjfAQr6RXPAa75 tsKLb2j2RhtNq8I4FUfsGEyBFrhCjsBpR2/p9LxLEBgOBUUL2TdoKtYhPhc2bAIuJHeqEA giS2PefpkxtKwm2UYJJ0jdz2zZroa6k= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=L93wMjju; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666006052; a=rsa-sha256; cv=none; b=gS4rIn+ot8yhI4iPkvN4ZyRwpHDdol0P1sKOX1Enoqd4KWpLPL/+AMfYOcETELXqT27qsZ zcwRDg/Xji0vLQpQmIZvnsjcFpDzSIwnMJmNdCnGNKDzTJyADq10PG87Bp8/eMj19uXVDU QB6UOgV8QFFWyK6sFqNPgPVSZk4YD30= X-Rspamd-Server: rspam12 X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=L93wMjju; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: miorch6sjtogtarq8ipsyudku713ne9z X-Rspamd-Queue-Id: 9FC5F180035 X-HE-Tag: 1666006051-147796 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 17.10.22 11:09, Baolin Wang wrote: > > > On 10/17/2022 4:41 PM, David Hildenbrand wrote: >> On 17.10.22 09:32, Baolin Wang wrote: >>> When creating a virtual machine, we will use memfd_create() to get >>> a file descriptor which can be used to create share memory mappings >>> using the mmap function, meanwhile the mmap() will set the MAP_POPULATE >>> flag to allocate physical pages for the virtual machine. >>> >>> When allocating physical pages for the guest, the host can fallback to >>> allocate some CMA pages for the guest when over half of the zone's free >>> memory is in the CMA area. >>> >>> In guest os, when the application wants to do some data transaction with >>> DMA, our QEMU will call VFIO_IOMMU_MAP_DMA ioctl to do longterm-pin and >>> create IOMMU mappings for the DMA pages. However, when calling >>> VFIO_IOMMU_MAP_DMA ioctl to pin the physical pages, we found it will be >>> failed to longterm-pin sometimes. >>> >>> After some invetigation, we found the pages used to do DMA mapping can >>> contain some CMA pages, and these CMA pages will cause a possible >>> failure of the longterm-pin, due to failed to migrate the CMA pages. >>> The reason of migration failure may be temporary reference count or >>> memory allocation failure. So that will cause the VFIO_IOMMU_MAP_DMA >>> ioctl returns error, which makes the application failed to start. >>> >>> To fix this issue, this patch introduces a new madvise behavior, named >>> as MADV_NOMOVABLE, to avoid allocating CMA pages and movable pages if >>> the users want to do longterm-pin, which can remove the possible failure >>> of movable or CMA pages migration. >> >> Sorry to say, but that sounds like a hack to work around a kernel >> implementation detail (how often we retry to migrate pages). > > IMO, in our case one migration failure will make our application failed > to start, which is not a trival problem. So mitigate the failure of > migration can be important in this case. The right thing to do is to understand why these migrations fail and see if we can improve the migration code. > >> If there are CMA/ZONE_MOVABLE issue, please fix them instead, and avoid >> leaking these details to user space. > > Now we can not forbid the fallback to CMA allocation if there are enough > free CMA in the zone, right? So adding a hint to help to diable > ALLOC_CMA flag seems reasonable? > > For CMA/ZONE_MOVABLE details, yes, not suitable to leak to user space. > so how about rename the madvise as MADV_PINNABLE, which means we will do > longterm-pin after allocation, and no CMA/ZONE_MOVABLE pages will be > allocated. > I really don't think any of these new user-visible madv modes with questionable semantics to workaround kernel implementation issues are a good idea. Especially MADV_PINNABLE has a *very* misleading name. I understand that something like "MADV_MIGHT_PIN" *might* be helpful to minimize page migration. But IMHO that could only be a pure optimization, but wouldn't stop us from allocating (or migrating to) CMA/ZONE_MOVABLE in the kernel on all code paths. It would be best effort only. It's not user space decision how/where the kernel allocates memory. No hacking around that. > Or do you have any good idea? Thanks. Investigate why migration of these pages fails and how we can improve the code to make migration of these pages work more reliably. I am not completely against having a kernel parameter that would disable allocating from CMA areas completely, even though it defeats the purpose of CMA. But it wouldn't apply to ZONE_MOVABLE, so it would be just another hackish approach. > >> ALSO, with MAP_POPULATE as described by you this madvise flag doesn't >> make too much sense, because it will gets et after all memory already >> was allocated ... > > This is not a problem I think, we can change to use MADV_POPULATE_XXX to > preallocate the physical pages after MADV_NOMOVABLE madvise. Yes, I know; I'm pointing out that your patch description is inconsistent. -- Thanks, David / dhildenb