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 4A772C2BD09 for ; Thu, 27 Jun 2024 23:19:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1F346B00A1; Thu, 27 Jun 2024 19:19:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ACD516B00A2; Thu, 27 Jun 2024 19:19:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96DAE6B00A3; Thu, 27 Jun 2024 19:19:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 79BCC6B00A1 for ; Thu, 27 Jun 2024 19:19:48 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2A4AAA446B for ; Thu, 27 Jun 2024 23:19:48 +0000 (UTC) X-FDA: 82278238056.15.A418D8B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf16.hostedemail.com (Postfix) with ESMTP id 0D2A9180005 for ; Thu, 27 Jun 2024 23:19:45 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="O0SxAQ/5"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719530372; a=rsa-sha256; cv=none; b=HZX4vY7xQSYSxmnRDIGhhEhpIx4z2STSrKUr8Zflp9HoMY8Aq5Jvmj58EbhnPWtWpTf/l0 hENBK5E2WLPSg/Bgy3GWoBzzE8BS4Ib4mDMEVkmRDYZWIDFpraNV6e8D3dP4AapeyTlsk6 3ryDZsOZle5iXI4tjKLn1cxvQCdq+f8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="O0SxAQ/5"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719530372; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=D1YXe5ol6L04xULXPymw3GJypJtsSF73vsgstdp57Og=; b=D6TbJcrRFk1+CDN4LgqthCFGUo8URXdAn63jMC06Vcp81mGTHMbaA2AshGliU9cQ2nTYqF /49lU0/BSue3+gRCTHUDSyUk5MWT0QIXZwIpwp4RXPuuSKqhr6ZEAfb9iHvD3NJ2QThWTy Ckjz6IfWnC5n/TQhkEhgDkKjLccREyU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719530385; 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: in-reply-to:in-reply-to:references:references; bh=D1YXe5ol6L04xULXPymw3GJypJtsSF73vsgstdp57Og=; b=O0SxAQ/5BGxPQGTjLAG6MfEv5DdTuK3RlMQrb77R5g3xQ9eVMxDqYXRclH7FGej5cNSF29 9O+Bkn6KVER7O0JRNS1h8zjL1771UJEOrQcrCRmw2sTPkX99NGcBu2byALxEPLe5vmNKbQ gEnu9DKwFkV/tY8z5Nkl/iZFJ1RJBbI= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-623-odSdNVqiN-uwPmJq22t9nw-1; Thu, 27 Jun 2024 19:19:43 -0400 X-MC-Unique: odSdNVqiN-uwPmJq22t9nw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6b50433ada9so212926d6.3 for ; Thu, 27 Jun 2024 16:19:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719530383; x=1720135183; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=D1YXe5ol6L04xULXPymw3GJypJtsSF73vsgstdp57Og=; b=AojNwDwBSOT9li0MGelI/FSSYPMG0NO6tu+Bss1FKWeQGbTtjHey37sUGN0rrKw3vl XWv880pUaUPkWDp+LuTdJF85or1zZcBoAYRkCFlO2ANXsdCkSZ11a9UJE9qp04s0nj6g 88TIqdXgLemaOHHY/oDpvKi28NZzXa4FdTagrOI2/InfhbU7HIVrBZ8eMOKopfSpaSUb ykLjCChKY6XFV/sgXvsaCc4MkW8/vwCtKFWqNTfO1H99zpzw304PfbTyy2ttizMmD/0b CjGgcE72TQmcutbrMqAo96tdrc71XVi88xpWrUXC4sy2o3MNQS4ovv613vZmpE+WgnMm o+rw== X-Forwarded-Encrypted: i=1; AJvYcCUaLpIbzxbas62RyemENrc2jIED/ujuKeCX6QTLgvG2dbz+UgXAqdDzuciiuw1QgsDO4zHMwMr7z2aCTECi2/ZT7V8= X-Gm-Message-State: AOJu0YzXmdlqzxWcfsbKRxuPbJ7DzXuWwOOUX7kMjV/0ai/MKUeYEkXp GhqRXUBjkdBBoyi8leZsvD9towt7IK+uFFGgEuRc2Sl8TN5bzNkhJmPDS+Xzm4EZkbKiSdmvhFk Spw9hAyhLquh6f2Gg0HzJGY5BFTZGsoGKeSvXKHkIGe4Ek6s6 X-Received: by 2002:a05:620a:2905:b0:79c:12a4:538b with SMTP id af79cd13be357-79c12a458a7mr732288685a.2.1719530383186; Thu, 27 Jun 2024 16:19:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFINKkJ5VHiTiiGezMh5c/t5IqK3PUIBzUnqYyYCVAOhNr45JVSecJ4xWQgBsizyziOSOPc1w== X-Received: by 2002:a05:620a:2905:b0:79c:12a4:538b with SMTP id af79cd13be357-79c12a458a7mr732286585a.2.1719530382525; Thu, 27 Jun 2024 16:19:42 -0700 (PDT) Received: from x1n (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79d692ea2ccsm22783185a.88.2024.06.27.16.19.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 16:19:42 -0700 (PDT) Date: Thu, 27 Jun 2024 19:19:40 -0400 From: Peter Xu To: Yang Shi Cc: yangge1116@126.com, david@redhat.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [v2 PATCH] mm: gup: do not call try_grab_folio() in slow path Message-ID: References: <20240627221413.671680-1-yang@os.amperecomputing.com> MIME-Version: 1.0 In-Reply-To: <20240627221413.671680-1-yang@os.amperecomputing.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspamd-Queue-Id: 0D2A9180005 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: md9zkmoaj5bdokiu9xadowedyh4axaxq X-HE-Tag: 1719530385-366254 X-HE-Meta: U2FsdGVkX19BFufoRt7LzNwFNPKcHz/5S7bMEPPoSDKzQfDHGmeVeHElsOUpMPMEjBbtXPBiovzeg11DLnefLVCwGKK0fko9nD6mcCMmQyX+xYbPtMToTr9oVNSTQIfvnrVxmnSfJLu0gbAc7wGaH0dGXlYC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sSmKJKQB49awkOmCQuGxhhwT23ziGZ/odpJikvg7mD7WGCyx2Pca3IKOL1EK0W6AdIi4iHyGfD3ThvAtEvot2DuT8yS3SjfI0mKoUt4G0Y2mqdfDiQkHRnEE6855mWVRtqh7cx2DxZR1DEx5e4d0rf4rHXbZxcASLzgLipsPqkYKh0gkBXXP0KPxIVENQ6ndZ+HSWM9vq76ZhG373iiFQwXlSKiH0L76JWgQn3qMW+XNmucFExYIpSxYhTM19ObUVn9Vmv8FzLIkwR+VSmWbwDDGGd41/vDhu8AOFjUBdBU2Wlkczibw== 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: List-Subscribe: List-Unsubscribe: Yang, On Thu, Jun 27, 2024 at 03:14:13PM -0700, Yang Shi wrote: > The try_grab_folio() is supposed to be used in fast path and it elevates > folio refcount by using add ref unless zero. We are guaranteed to have > at least one stable reference in slow path, so the simple atomic add > could be used. The performance difference should be trivial, but the > misuse may be confusing and misleading. This first paragraph is IMHO misleading itself.. I think we should mention upfront the important bit, on the user impact. Here IMO the user impact should be: Linux may fail longterm pin in some releavnt paths when applied over CMA reserved blocks. And if to extend a bit, that include not only slow-gup but also the new memfd pinning, because both of them used try_grab_folio() which used to be only for fast-gup. It's great this patch renamed try_grab_folio() to try_grab_folio_fast(), I think that definitely helps on reducing the abuse in the future. However then with that the subject becomes misleading, because it says "do not call try_grab_folio()" however after this patch we keep using it. Maybe rename the subject to "mm: Fix longterm pin on slow gup and memfd pin regress"? > > In another thread [1] a kernel warning was reported when pinning folio > in CMA memory when launching SEV virtual machine. The splat looks like: > > [ 464.325306] WARNING: CPU: 13 PID: 6734 at mm/gup.c:1313 __get_user_pages+0x423/0x520 > [ 464.325464] CPU: 13 PID: 6734 Comm: qemu-kvm Kdump: loaded Not tainted 6.6.33+ #6 > [ 464.325477] RIP: 0010:__get_user_pages+0x423/0x520 > [ 464.325515] Call Trace: > [ 464.325520] > [ 464.325523] ? __get_user_pages+0x423/0x520 > [ 464.325528] ? __warn+0x81/0x130 > [ 464.325536] ? __get_user_pages+0x423/0x520 > [ 464.325541] ? report_bug+0x171/0x1a0 > [ 464.325549] ? handle_bug+0x3c/0x70 > [ 464.325554] ? exc_invalid_op+0x17/0x70 > [ 464.325558] ? asm_exc_invalid_op+0x1a/0x20 > [ 464.325567] ? __get_user_pages+0x423/0x520 > [ 464.325575] __gup_longterm_locked+0x212/0x7a0 > [ 464.325583] internal_get_user_pages_fast+0xfb/0x190 > [ 464.325590] pin_user_pages_fast+0x47/0x60 > [ 464.325598] sev_pin_memory+0xca/0x170 [kvm_amd] > [ 464.325616] sev_mem_enc_register_region+0x81/0x130 [kvm_amd] > > Per the analysis done by yangge, when starting the SEV virtual machine, > it will call pin_user_pages_fast(..., FOLL_LONGTERM, ...) to pin the > memory. But the page is in CMA area, so fast GUP will fail then > fallback to the slow path due to the longterm pinnalbe check in > try_grab_folio(). > The slow path will try to pin the pages then migrate them out of CMA > area. But the slow path also uses try_grab_folio() to pin the page, > it will also fail due to the same check then the above warning > is triggered. > > [1] https://lore.kernel.org/linux-mm/1719478388-31917-1-git-send-email-yangge1116@126.com/ > > Fixes: 57edfcfd3419 ("mm/gup: accelerate thp gup even for "pages != NULL"") > Cc: [6.6+] > Reported-by: yangge > Signed-off-by: Yang Shi The patch itself looks mostly ok to me. There's still some "cleanup" part mangled together, e.g., the real meat should be avoiding the folio_is_longterm_pinnable() check in relevant paths. The rest (e.g. switch slow-gup / memfd pin to use folio_ref_add() not try_get_folio(), and renames) could be good cleanups. So a smaller fix might be doable, but again I don't have a strong opinion here. Thanks, -- Peter Xu