From nobody Sun Feb 8 05:42:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 996AF29B239 for ; Tue, 4 Mar 2025 15:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741103342; cv=none; b=snZTceF021Qh79oK8pwgQw9OLLxbbP2qynm9wmDqotAWkNxkw1AU+WP5MS26m8Qx+4iySCwyej2W5H8196X5zbKjsNvM0ZlHLT9PoOlZjzFxERUA8b8mAlFt3jDbCzUJe4owwS0vSqg0T3oFhBAQkuyRzeJWIWsO+aIXt7mMzhA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741103342; c=relaxed/simple; bh=uyHJhziojFGk/tam8soDl9qH4ruNaNMd8RrIguscx8M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZTZfONBGuz2hIYztal3RZ1oPxadXg5sqy5NvaJYq5nUa+MlVjR7w9i1FiFZ4EuLv9yHTOo8WG1wHaZaORij9lwtMwWnM+ywW7xiCv7Hn4W0OsiqwNB0/3BD+TSchoBnNT8BpXp1n4icggANclxJNsPTeJJzirf3Pyr0KNyIdLG0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=X7CuhkuP; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="X7CuhkuP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741103338; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2RHlzy1ROEWhQbxxG4Yv+rRoMty5ce9tc18f8MaHZsU=; b=X7CuhkuPbYBTda83fxPRGYXpLuA18mEFewMgmujGYSXITIA0iduxHZcwucFd0ChYVv2SGw 7QoJa89QhMOoDBsnbr2rTq7uXK0UMM73h9bwDLCSsEPFtL9I1sFkYB+jipljRW6bzvcW1t IwmabXd3s2NQlW+so3X5FGcCC9I3S5o= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-499-dSnJZsPzNOaAeEpmfwhw9A-1; Tue, 04 Mar 2025 10:48:52 -0500 X-MC-Unique: dSnJZsPzNOaAeEpmfwhw9A-1 X-Mimecast-MFC-AGG-ID: dSnJZsPzNOaAeEpmfwhw9A_1741103331 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-390f3652842so2401386f8f.1 for ; Tue, 04 Mar 2025 07:48:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741103331; x=1741708131; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2RHlzy1ROEWhQbxxG4Yv+rRoMty5ce9tc18f8MaHZsU=; b=Hl6G4aF2B2+uqlVm2KmvaFEo/9JbqMvIyL5NAFVU04LzKXk5keNTbLPmWcUHG4skyH knwbYNMaAbXNdWZ0myQYp5lJ/d6+gAZjqV8KR3dtOgPwcuTbmhvwql01zbhjBkGiQqAL bVPiFRI5+QLuff51yMcgFceF4rvxZEFkAx6B+XoZMRbp88NZuCHkBAu69ESVncLld+pN ohUmTHP3OQA1VtravR2T+xmII3mCCeaHUhWTTd0IMwNikEVZie7euhczjL4DoKij9k8p LEGfSqfl8k8R48m48jhLpgXZCkUnztKA8lOTPgcqGJaOfeA3yzi1bol1khPjryY0E0U9 DAXw== X-Gm-Message-State: AOJu0YwoXjAX/cTm5PS7lqw3lRix4nspgIe5ZZnjftUgdGxl+ZsNb5kG QWzdVIMUSdBES/ukd2DB+s6LXSX8tmJ8SoJFdeHhM8t05T3pd8vsUuW8RQjQnQsJlyb25dGDdgK uDTWi7XxC0Ei2uaUBCwnnNMdL5ITujYZeatJa5b/+rJM3xj7PF1o5pf0d+Scyek4N3DFJChBMCj BxKWdfDZX0Oyj0qKN03FPuzBIdyHimtm0cE6ivMPcWLuTS X-Gm-Gg: ASbGncuAu7zv6voB+sTzOyPiKwdl49u+U60+URqBqGSXQRdd431lDg8OBZzOH6dQTN0 HuguZnIAnumGw3pUjY1toe8IwntNPydF4BpJzeHIOyJlOCA/mecXSz+yYBPCjw7aUCjvr/Zbpij LQSCAV75gynP++//dJIJYJJwl3OPWsfz6Q8ulzJYjVtIxEdN0B5v4DEC/pOf3VC5EGeWGGPeAsf AB1WWngvEXYwqMk6cYpn0rTcH8ogYwN9SOyqpXd2ObDP6fZjkVZcpiTBruXPj7qy8JVkdBCELhW MuLqXEhVLdkKOPD5f0wRfVA0QXflylB9FCpllVgQOfa1pNQ4pnkvlZB7Xf/KvlRjVFprfLq3JGy B X-Received: by 2002:a5d:5847:0:b0:390:e94c:453e with SMTP id ffacd0b85a97d-390eca070b2mr17839177f8f.39.1741103331137; Tue, 04 Mar 2025 07:48:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IFS2yQ2nIm92Fqm8oP87QtIsIbeZe5pQ0O0ho/Z0iCYCjljpWAZZP5v8ry/LGiAu8oiIQK1RQ== X-Received: by 2002:a5d:5847:0:b0:390:e94c:453e with SMTP id ffacd0b85a97d-390eca070b2mr17839137f8f.39.1741103330643; Tue, 04 Mar 2025 07:48:50 -0800 (PST) Received: from localhost (p200300cbc73610009e302a8acd3d419c.dip0.t-ipconnect.de. [2003:cb:c736:1000:9e30:2a8a:cd3d:419c]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-390e485d906sm18206420f8f.90.2025.03.04.07.48.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Mar 2025 07:48:50 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, David Hildenbrand , Andrew Morton , Matthew Wilcox , Russell King , Masami Hiramatsu , Oleg Nesterov , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Tong Tiangen Subject: [PATCH -next v1 1/3] kernel/events/uprobes: pass VMA instead of MM to remove_breakpoint() Date: Tue, 4 Mar 2025 16:48:44 +0100 Message-ID: <20250304154846.1937958-2-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250304154846.1937958-1-david@redhat.com> References: <20250304154846.1937958-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ... and remove the "MM" argument from install_breakpoint(), because it can easily be derived from the VMA. Signed-off-by: David Hildenbrand --- kernel/events/uprobes.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 8fc53813779a4..991aacc80d0e0 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -1134,10 +1134,10 @@ static bool filter_chain(struct uprobe *uprobe, str= uct mm_struct *mm) return ret; } =20 -static int -install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, - struct vm_area_struct *vma, unsigned long vaddr) +static int install_breakpoint(struct uprobe *uprobe, struct vm_area_struct= *vma, + unsigned long vaddr) { + struct mm_struct *mm =3D vma->vm_mm; bool first_uprobe; int ret; =20 @@ -1162,9 +1162,11 @@ install_breakpoint(struct uprobe *uprobe, struct mm_= struct *mm, return ret; } =20 -static int -remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned lo= ng vaddr) +static int remove_breakpoint(struct uprobe *uprobe, struct vm_area_struct = *vma, + unsigned long vaddr) { + struct mm_struct *mm =3D vma->vm_mm; + set_bit(MMF_RECALC_UPROBES, &mm->flags); return set_orig_insn(&uprobe->arch, mm, vaddr); } @@ -1296,10 +1298,10 @@ register_for_each_vma(struct uprobe *uprobe, struct= uprobe_consumer *new) if (is_register) { /* consult only the "caller", new consumer. */ if (consumer_filter(new, mm)) - err =3D install_breakpoint(uprobe, mm, vma, info->vaddr); + err =3D install_breakpoint(uprobe, vma, info->vaddr); } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) { if (!filter_chain(uprobe, mm)) - err |=3D remove_breakpoint(uprobe, mm, info->vaddr); + err |=3D remove_breakpoint(uprobe, vma, info->vaddr); } =20 unlock: @@ -1472,7 +1474,7 @@ static int unapply_uprobe(struct uprobe *uprobe, stru= ct mm_struct *mm) continue; =20 vaddr =3D offset_to_vaddr(vma, uprobe->offset); - err |=3D remove_breakpoint(uprobe, mm, vaddr); + err |=3D remove_breakpoint(uprobe, vma, vaddr); } mmap_read_unlock(mm); =20 @@ -1610,7 +1612,7 @@ int uprobe_mmap(struct vm_area_struct *vma) if (!fatal_signal_pending(current) && filter_chain(uprobe, vma->vm_mm)) { unsigned long vaddr =3D offset_to_vaddr(vma, uprobe->offset); - install_breakpoint(uprobe, vma->vm_mm, vma, vaddr); + install_breakpoint(uprobe, vma, vaddr); } put_uprobe(uprobe); } --=20 2.48.1 From nobody Sun Feb 8 05:42:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68A4729DB8C for ; Tue, 4 Mar 2025 15:49:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741103348; cv=none; b=WiykHNf1CvvzwsEVBxe4OxwVjX02vtr0g8c7zCxi6ntc/svMAbbLImXomIzBwKqHZp+Z9IXALvi5giYKU+zCQNXjxLNO+ec4PBY9HSZyOsEfqNTDb2DB7kXwSJBbX+kFiVldnYTyZaAr4vnqym/nfqxPvyM/BrXWMwToJi/3vP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741103348; c=relaxed/simple; bh=Ah9D1oaYdgLAYU43bf6vQIZvtJeF1OpFbII7FyU6mlU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lrklmz+nW++iA13a9s621k8mhZrvRsFLcS0/xuo+K8DxMjwPkdV8VRwjygelbuKjR8btDQk7MbCCZhZ9iVJgJSv7CHQDQAR8usdDtuZuDCAYBGLTl7KdAan9LqK48S1WoQ05UCy22FuFia9i0Ohu06jcdI+TxIl44mD2diRKB/M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UfBxpJe1; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UfBxpJe1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741103345; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ffON9SnaOWg9oFQtm+MueKUsNfzxra0dWfxUL8seX24=; b=UfBxpJe1uXfrul9A9aXrqFX49eXvgsgAQibV76CrxxyJ9/ZxvOgcNtTBkfpepF/bF12Qwf fgrEP7eD1lO90d1tY2D3H5ryg3YGQc1dWq8plOzjNdBlKFPYc/e+nCryvW9rczwNnpafan UCQD7k+yJl3HCk8iY3kUGTd0aU58B4w= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-3-pvYxw9kuPDKHlUtIo1Xh-Q-1; Tue, 04 Mar 2025 10:48:54 -0500 X-MC-Unique: pvYxw9kuPDKHlUtIo1Xh-Q-1 X-Mimecast-MFC-AGG-ID: pvYxw9kuPDKHlUtIo1Xh-Q_1741103333 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-43bcb061704so5291345e9.0 for ; Tue, 04 Mar 2025 07:48:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741103333; x=1741708133; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ffON9SnaOWg9oFQtm+MueKUsNfzxra0dWfxUL8seX24=; b=DkTuHu0RqAGfzQCsbY+DkV5XBpAD9lSEN2lwdxx8YoDSOya5V1p7rZrp1YOAB1hqgT QHCZb9cZSa64f1XeLdW5/sWK4S+29cUWuhIg8ykQwgCaYc+/r45vLJA5qoSdzkuKpC1L l66WTPWaX/RKPHQONviv2HxI7Hsf2qxkdNiY1+wxjtNld7IdpWNynGne+E6rVU/olMFN Vsvf9/bMcV/Vl7dob60yLMDCu6cf2HMyHTp3UaeR8jODrDQo8LVib6eKe5e/9MJ45+XF KjNcTg0F/ddPb8V4r0R/Uip3/67BiiMACzwDFpPDBm4wkn7uscwCaomN6UMeZrTgjBzG sGoA== X-Gm-Message-State: AOJu0Yw77w8dWG541reo1ODL1uW1ejsBxjqFv9qRoMYqN4uvfGFB52oo tymjzWLq8lsROjsFRT0bhB5Yhl+VVRPsH7+EvPOtA5Ciw8T2FJ33m3rW4sOezFFLcenY3gPXWBS Z4LtsPvoPkw6HuAK7s90Vib2ZOXZ0WEL6mKccQsUxz/VcUfYY+Tk9hxUkLI2Bw5hLFnp0Gh17W5 YOQA02BbsHbtNcVwW9QZdTpiW9SAyDIVu8rJmCR4p1COIF X-Gm-Gg: ASbGncvB8w3WY1dXK0w/oPkMMzxxrW0z6JUx1q+Eom0XkH8EFQTs1v/WJowTjsD0g7S QbWJIeMg2G2EzBMMx4VQ3+tvjE2nYyA72Iq/iAjbVe32HscpalP4DdDJCLmMEHMYMcuoN0OUmSn 8NsvsPlvasEHT+MYq8nAwkzto/x+/Ouhf1DbroNcxbIiLP/sjX/KmQF7PMdhjvvTHVJP09h5Bbe Z8rot3AXMbl2ysSZ65zi17XOGIKWfq79fb2QJOTgZhUv0Pa8FU3G+Ys3unQEQmN+cEQ2fPtYIfb ZCLUTqYqfxdyGS+co5ovBdlx4LCCcnX3T7s28SViWHAfTXY1BdKgJwQEXQj2vYzOuTUgoZWYvFQ D X-Received: by 2002:a05:600c:3c9d:b0:439:8a62:db42 with SMTP id 5b1f17b1804b1-43ba66e19d9mr153515945e9.8.1741103333183; Tue, 04 Mar 2025 07:48:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IH4Gjl+jrn0ypwJ4W8O9mKURosa8cKWKJgfr05HkBSuQdnMp7DSreUXUU7A6eFr/8nx/dTcdQ== X-Received: by 2002:a05:600c:3c9d:b0:439:8a62:db42 with SMTP id 5b1f17b1804b1-43ba66e19d9mr153515575e9.8.1741103332777; Tue, 04 Mar 2025 07:48:52 -0800 (PST) Received: from localhost (p200300cbc73610009e302a8acd3d419c.dip0.t-ipconnect.de. [2003:cb:c736:1000:9e30:2a8a:cd3d:419c]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-43bca26676esm43128935e9.8.2025.03.04.07.48.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Mar 2025 07:48:52 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, David Hildenbrand , Andrew Morton , Matthew Wilcox , Russell King , Masami Hiramatsu , Oleg Nesterov , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Tong Tiangen Subject: [PATCH -next v1 2/3] kernel/events/uprobes: pass VMA to set_swbp(), set_orig_insn() and uprobe_write_opcode() Date: Tue, 4 Mar 2025 16:48:45 +0100 Message-ID: <20250304154846.1937958-3-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250304154846.1937958-1-david@redhat.com> References: <20250304154846.1937958-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We already have the VMA, no need to look it up using get_user_page_vma_remote(). We can now switch to get_user_pages_remote(). Signed-off-by: David Hildenbrand --- arch/arm/probes/uprobes/core.c | 4 ++-- include/linux/uprobes.h | 6 +++--- kernel/events/uprobes.c | 33 +++++++++++++++++---------------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/arch/arm/probes/uprobes/core.c b/arch/arm/probes/uprobes/core.c index f5f790c6e5f89..885e0c5e8c20d 100644 --- a/arch/arm/probes/uprobes/core.c +++ b/arch/arm/probes/uprobes/core.c @@ -26,10 +26,10 @@ bool is_swbp_insn(uprobe_opcode_t *insn) (UPROBE_SWBP_ARM_INSN & 0x0fffffff); } =20 -int set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, +int set_swbp(struct arch_uprobe *auprobe, struct vm_area_struct *vma, unsigned long vaddr) { - return uprobe_write_opcode(auprobe, mm, vaddr, + return uprobe_write_opcode(auprobe, vma, vaddr, __opcode_to_mem_arm(auprobe->bpinsn)); } =20 diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h index a40efdda9052b..4da3bce5e062d 100644 --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -186,13 +186,13 @@ struct uprobes_state { }; =20 extern void __init uprobes_init(void); -extern int set_swbp(struct arch_uprobe *aup, struct mm_struct *mm, unsigne= d long vaddr); -extern int set_orig_insn(struct arch_uprobe *aup, struct mm_struct *mm, un= signed long vaddr); +extern int set_swbp(struct arch_uprobe *aup, struct vm_area_struct *vma, u= nsigned long vaddr); +extern int set_orig_insn(struct arch_uprobe *aup, struct vm_area_struct *v= ma, unsigned long vaddr); extern bool is_swbp_insn(uprobe_opcode_t *insn); extern bool is_trap_insn(uprobe_opcode_t *insn); extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs); extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs); -extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_stru= ct *mm, unsigned long vaddr, uprobe_opcode_t); +extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area= _struct *vma, unsigned long vaddr, uprobe_opcode_t); extern struct uprobe *uprobe_register(struct inode *inode, loff_t offset, = loff_t ref_ctr_offset, struct uprobe_consumer *uc); extern int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc,= bool); extern void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_= consumer *uc); diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 991aacc80d0e0..0276defd6fbfa 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -474,19 +474,19 @@ static int update_ref_ctr(struct uprobe *uprobe, stru= ct mm_struct *mm, * * uprobe_write_opcode - write the opcode at a given virtual address. * @auprobe: arch specific probepoint information. - * @mm: the probed process address space. + * @vma: the probed virtual memory area. * @vaddr: the virtual address to store the opcode. * @opcode: opcode to be written at @vaddr. * * Called with mm->mmap_lock held for read or write. * Return 0 (success) or a negative errno. */ -int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, - unsigned long vaddr, uprobe_opcode_t opcode) +int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct= *vma, + unsigned long vaddr, uprobe_opcode_t opcode) { + struct mm_struct *mm =3D vma->vm_mm; struct uprobe *uprobe; struct page *old_page, *new_page; - struct vm_area_struct *vma; int ret, is_register, ref_ctr_updated =3D 0; bool orig_page_huge =3D false; unsigned int gup_flags =3D FOLL_FORCE; @@ -498,9 +498,9 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, st= ruct mm_struct *mm, if (is_register) gup_flags |=3D FOLL_SPLIT_PMD; /* Read the page with vaddr into memory */ - old_page =3D get_user_page_vma_remote(mm, vaddr, gup_flags, &vma); - if (IS_ERR(old_page)) - return PTR_ERR(old_page); + ret =3D get_user_pages_remote(mm, vaddr, 1, gup_flags, &old_page, NULL); + if (ret !=3D 1) + return ret; =20 ret =3D verify_opcode(old_page, vaddr, &opcode); if (ret <=3D 0) @@ -590,30 +590,31 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, = struct mm_struct *mm, /** * set_swbp - store breakpoint at a given address. * @auprobe: arch specific probepoint information. - * @mm: the probed process address space. + * @vma: the probed virtual memory area. * @vaddr: the virtual address to insert the opcode. * * For mm @mm, store the breakpoint instruction at @vaddr. * Return 0 (success) or a negative errno. */ -int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, uns= igned long vaddr) +int __weak set_swbp(struct arch_uprobe *auprobe, struct vm_area_struct *vm= a, + unsigned long vaddr) { - return uprobe_write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN); + return uprobe_write_opcode(auprobe, vma, vaddr, UPROBE_SWBP_INSN); } =20 /** * set_orig_insn - Restore the original instruction. - * @mm: the probed process address space. + * @vma: the probed virtual memory area. * @auprobe: arch specific probepoint information. * @vaddr: the virtual address to insert the opcode. * * For mm @mm, restore the original opcode (opcode) at @vaddr. * Return 0 (success) or a negative errno. */ -int __weak -set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned = long vaddr) +int __weak set_orig_insn(struct arch_uprobe *auprobe, + struct vm_area_struct *vma, unsigned long vaddr) { - return uprobe_write_opcode(auprobe, mm, vaddr, + return uprobe_write_opcode(auprobe, vma, vaddr, *(uprobe_opcode_t *)&auprobe->insn); } =20 @@ -1153,7 +1154,7 @@ static int install_breakpoint(struct uprobe *uprobe, = struct vm_area_struct *vma, if (first_uprobe) set_bit(MMF_HAS_UPROBES, &mm->flags); =20 - ret =3D set_swbp(&uprobe->arch, mm, vaddr); + ret =3D set_swbp(&uprobe->arch, vma, vaddr); if (!ret) clear_bit(MMF_RECALC_UPROBES, &mm->flags); else if (first_uprobe) @@ -1168,7 +1169,7 @@ static int remove_breakpoint(struct uprobe *uprobe, s= truct vm_area_struct *vma, struct mm_struct *mm =3D vma->vm_mm; =20 set_bit(MMF_RECALC_UPROBES, &mm->flags); - return set_orig_insn(&uprobe->arch, mm, vaddr); + return set_orig_insn(&uprobe->arch, vma, vaddr); } =20 struct map_info { --=20 2.48.1 From nobody Sun Feb 8 05:42:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40AC329B23F for ; Tue, 4 Mar 2025 15:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741103342; cv=none; b=cBCi5E116yacZHdMb5xNzt+go58PpNsT+yqMIpRNaKz4K5jepG8+prPMDfzLu2WgMzGgdPX299owHDAX7ntGOmVb9QzrhydqfQYjtqEtSOvUbpvjlsmD8xvNv8r54gmpmnJkEfJedb2/eaxadXh+teILVV3TeOHqe88WVl2Z06Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741103342; c=relaxed/simple; bh=nsp+uT9TiQsWVra7pS0tTd3IXrvabNIr1AHBlduKTTU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YB5yXxGojcCx7sHKucmvkPQ+pj+vffeM+GQXdc//uM0ApH2adf4+AgKFmVktCYCayOjHu6o72BUm3DFckPogLSIH0iwp7W9QIZ7A8yC6G9K2Nih3da9NbyiKkoMgFj77dAo5c4863K2hWxmWzhs6MYh1S1GJ4XTiYCC9pZC18gs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=PNlPlB31; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PNlPlB31" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741103339; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wgfV1BFqVf16XcSyPrOW+mvJosHJK6PFcKJVrOW03zU=; b=PNlPlB31H18WlJPpmueadNoze99fUHJO4NbsdCQ5vLIST9HL6CNTxPiak1LJAFk9APK8KL Bj++Dy9kawqv1gY6Tbf4nuVFJZjpjSB6JOlCYQmXDP/aqkbkVm20Yj/7o/d84443KlXnb7 sEjK6d/gdntwfGLmelvQQD1IVdDAX+M= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-364-gLBqkiaaMWqkPud3ai1B_Q-1; Tue, 04 Mar 2025 10:48:58 -0500 X-MC-Unique: gLBqkiaaMWqkPud3ai1B_Q-1 X-Mimecast-MFC-AGG-ID: gLBqkiaaMWqkPud3ai1B_Q_1741103336 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3910cd78330so959072f8f.1 for ; Tue, 04 Mar 2025 07:48:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741103336; x=1741708136; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wgfV1BFqVf16XcSyPrOW+mvJosHJK6PFcKJVrOW03zU=; b=Hj5/a1XefSeTKHfnYfXYRkLcLdarHjMXZCL2QX0mti4kVVqWR++b0yypbuvPm7urgB vBEsQYs3mA41gr5DK+M+R9eYa95ofwVQEx9RLr1Twqjv7Sx+750u+YoQLH5TRmB1KMkR kSL9gWOp7XeLkcx53uV0FbJiFq8m3Vuvll3R0t066rBrE8Rl90YSviB0om3PKOoXhV0g qp0LWwZu7DBy5zAkMtYitLc6XYWmFMEu5TyFiT2YEeiBkTWGom/2jY6CFHEHlm2FNkCC K7YFON1ePGfHaDmSe097As3/yxaSpnN6z9phe57sc/LfsEp2Mu8hhGyJW+/X46LM2kZu f2Cg== X-Gm-Message-State: AOJu0Yz2x8McvexLCUgYg6taOamJ1X8ZSuSWNVmm0BptHak1Iw5+G7eP 27kWaZoV8EaZ2rsIf17EnjWcnkduYVvfHYxr3bzh9sukPRC0Vs/jUlr9JX3NZSQ2EwmIQ40BYdY FVh19JfUctvMC2/dkRSnpeqgIzBfSYyRvYh9VINhvynzb+zVUjmCC55fwTiG63rubQnKjD4D2/k mUYRIGNUZbgN2F1N0raEJ7FG1a/xItiSAEwPuig+whowL3 X-Gm-Gg: ASbGncuHTjFQhWmCPaXwo/I2/6N94Ql76CrxhFRXGNSZDVy+mzqZfrgkkHIDyj9JVMn 9VSB5iOthqPwwBNQNXHZc1dlQttvZxhJjBnkd5Z5KPlHx5OuM3WZhROkSwJJrwDDe0gzrNYgLen iMgzdYb7XpyQgH/HG9V8cebnWJvA6SvbPfeuUyCznVTYo7kkMtVhypQPdPRx71IKBmJDHTAB4TH Ofo9Y4+X4dPisexHQTY3ewA6OkkN0aKBH/D6FZ+cWiNDgEIPX9F5+AeFr9xf9rag8gmHq8XEQ0m KQoxarohLaX29D39y3gIDtjRKTknSjKlzivMDZLR1Xs0XhKtNRfdRRfqvtML5odT/jQij8+TR3o K X-Received: by 2002:a05:6000:188c:b0:38f:3224:65ff with SMTP id ffacd0b85a97d-390ec7c6a8emr13964827f8f.5.1741103335480; Tue, 04 Mar 2025 07:48:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEpnVEdNPdrzCOWtxMEX5R8OAMo0fpxMs1MbCedgfb3wOXycePEGo2BYRlgc01bVfM8qeQFCg== X-Received: by 2002:a05:6000:188c:b0:38f:3224:65ff with SMTP id ffacd0b85a97d-390ec7c6a8emr13964780f8f.5.1741103334841; Tue, 04 Mar 2025 07:48:54 -0800 (PST) Received: from localhost (p200300cbc73610009e302a8acd3d419c.dip0.t-ipconnect.de. [2003:cb:c736:1000:9e30:2a8a:cd3d:419c]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-390e47a66adsm17850100f8f.25.2025.03.04.07.48.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Mar 2025 07:48:54 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, David Hildenbrand , Andrew Morton , Matthew Wilcox , Russell King , Masami Hiramatsu , Oleg Nesterov , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Tong Tiangen Subject: [PATCH -next v1 3/3] kernel/events/uprobes: uprobe_write_opcode() rewrite Date: Tue, 4 Mar 2025 16:48:46 +0100 Message-ID: <20250304154846.1937958-4-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250304154846.1937958-1-david@redhat.com> References: <20250304154846.1937958-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" uprobe_write_opcode() does some pretty low-level things that really, it shouldn't be doing: for example, manually breaking COW by allocating anonymous folios and replacing mapped pages. Further, it does seem to do some shaky things: for example, writing to possible COW-shared anonymous pages or zapping anonymous pages that might be pinned. We're also not taking care of uffd, uffd-wp, softdirty ... although rather corner cases here. Let's just get it right like ordinary ptrace writes would. Let's rewrite the code, leaving COW-breaking to core-MM, triggered by FOLL_FORCE|FOLL_WRITE (note that the code was already using FOLL_FORCE). We'll use GUP to lookup/faultin the page and break COW if required. Then, we'll walk the page tables using a folio_walk to perform our page modification atomically by temporarily unmap the PTE + flushing the TLB. Likely, we could avoid the temporary unmap in case we can just atomically write the instruction, but that will be a separate project. Unfortunately, we still have to implement the zapping logic manually, because we only want to zap in specific circumstances (e.g., page content identical). Note that we can now handle large folios (compound pages) and the shared zeropage just fine, so drop these checks. Signed-off-by: David Hildenbrand --- kernel/events/uprobes.c | 316 ++++++++++++++++++++-------------------- 1 file changed, 160 insertions(+), 156 deletions(-) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 0276defd6fbfa..4e39280f8f424 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -29,6 +29,7 @@ #include #include #include /* check_stable_address_space */ +#include =20 #include =20 @@ -151,91 +152,6 @@ static loff_t vaddr_to_offset(struct vm_area_struct *v= ma, unsigned long vaddr) return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start); } =20 -/** - * __replace_page - replace page in vma by new page. - * based on replace_page in mm/ksm.c - * - * @vma: vma that holds the pte pointing to page - * @addr: address the old @page is mapped at - * @old_page: the page we are replacing by new_page - * @new_page: the modified page we replace page by - * - * If @new_page is NULL, only unmap @old_page. - * - * Returns 0 on success, negative error code otherwise. - */ -static int __replace_page(struct vm_area_struct *vma, unsigned long addr, - struct page *old_page, struct page *new_page) -{ - struct folio *old_folio =3D page_folio(old_page); - struct folio *new_folio; - struct mm_struct *mm =3D vma->vm_mm; - DEFINE_FOLIO_VMA_WALK(pvmw, old_folio, vma, addr, 0); - int err; - struct mmu_notifier_range range; - pte_t pte; - - mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, addr, - addr + PAGE_SIZE); - - if (new_page) { - new_folio =3D page_folio(new_page); - err =3D mem_cgroup_charge(new_folio, vma->vm_mm, GFP_KERNEL); - if (err) - return err; - } - - /* For folio_free_swap() below */ - folio_lock(old_folio); - - mmu_notifier_invalidate_range_start(&range); - err =3D -EAGAIN; - if (!page_vma_mapped_walk(&pvmw)) - goto unlock; - VM_BUG_ON_PAGE(addr !=3D pvmw.address, old_page); - pte =3D ptep_get(pvmw.pte); - - /* - * Handle PFN swap PTES, such as device-exclusive ones, that actually - * map pages: simply trigger GUP again to fix it up. - */ - if (unlikely(!pte_present(pte))) { - page_vma_mapped_walk_done(&pvmw); - goto unlock; - } - - if (new_page) { - folio_get(new_folio); - folio_add_new_anon_rmap(new_folio, vma, addr, RMAP_EXCLUSIVE); - folio_add_lru_vma(new_folio, vma); - } else - /* no new page, just dec_mm_counter for old_page */ - dec_mm_counter(mm, MM_ANONPAGES); - - if (!folio_test_anon(old_folio)) { - dec_mm_counter(mm, mm_counter_file(old_folio)); - inc_mm_counter(mm, MM_ANONPAGES); - } - - flush_cache_page(vma, addr, pte_pfn(pte)); - ptep_clear_flush(vma, addr, pvmw.pte); - if (new_page) - set_pte_at(mm, addr, pvmw.pte, - mk_pte(new_page, vma->vm_page_prot)); - - folio_remove_rmap_pte(old_folio, old_page, vma); - if (!folio_mapped(old_folio)) - folio_free_swap(old_folio); - page_vma_mapped_walk_done(&pvmw); - folio_put(old_folio); - - err =3D 0; - unlock: - mmu_notifier_invalidate_range_end(&range); - folio_unlock(old_folio); - return err; -} - /** * is_swbp_insn - check if instruction is breakpoint instruction. * @insn: instruction to be checked. @@ -463,6 +379,105 @@ static int update_ref_ctr(struct uprobe *uprobe, stru= ct mm_struct *mm, return ret; } =20 +static bool orig_page_is_identical(struct vm_area_struct *vma, + unsigned long vaddr, struct page *page, bool *pmd_mappable) +{ + const pgoff_t index =3D vaddr_to_offset(vma, vaddr) >> PAGE_SHIFT; + struct page *orig_page =3D find_get_page(vma->vm_file->f_inode->i_mapping, + index); + struct folio *orig_folio; + bool identical; + + if (!orig_page) + return false; + orig_folio =3D page_folio(orig_page); + + *pmd_mappable =3D folio_test_pmd_mappable(orig_folio); + identical =3D folio_test_uptodate(orig_folio) && + pages_identical(page, orig_page); + folio_put(orig_folio); + return identical; +} + +static int __uprobe_write_opcode(struct vm_area_struct *vma, + struct folio_walk *fw, struct folio *folio, + unsigned long opcode_vaddr, uprobe_opcode_t opcode) +{ + const unsigned long vaddr =3D opcode_vaddr & PAGE_MASK; + const bool is_register =3D !!is_swbp_insn(&opcode); + bool pmd_mappable; + + /* We're done if we don't find an anonymous folio when unregistering. */ + if (!folio_test_anon(folio)) + return is_register ? -EFAULT : 0; + + /* For now, we'll only handle PTE-mapped folios. */ + if (fw->level !=3D FW_LEVEL_PTE) + return -EFAULT; + + /* + * See can_follow_write_pte(): we'd actually prefer a writable PTE here, + * but the VMA might not be writable. + */ + if (!pte_write(fw->pte)) { + if (!PageAnonExclusive(fw->page)) + return -EFAULT; + if (unlikely(userfaultfd_pte_wp(vma, fw->pte))) + return -EFAULT; + /* SOFTDIRTY is handled via pte_mkdirty() below. */ + } + + /* + * We'll temporarily unmap the page and flush the TLB, such that we can + * modify the page atomically. + */ + flush_cache_page(vma, vaddr, pte_pfn(fw->pte)); + fw->pte =3D ptep_clear_flush(vma, vaddr, fw->ptep); + + /* Verify that the page content is still as expected. */ + if (verify_opcode(fw->page, opcode_vaddr, &opcode) <=3D 0) { + set_pte_at(vma->vm_mm, vaddr, fw->ptep, fw->pte); + return -EAGAIN; + } + copy_to_page(fw->page, opcode_vaddr, &opcode, UPROBE_SWBP_INSN_SIZE); + + /* + * When unregistering, we may only zap a PTE if uffd is disabled and + * there are no unexpected folio references ... + */ + if (is_register || userfaultfd_missing(vma) || + (folio_ref_count(folio) !=3D folio_mapcount(folio) + + folio_test_swapcache(folio) * folio_nr_pages(folio))) + goto remap; + + /* + * ... and the mapped page is identical to the original page that + * would get faulted in on next access. + */ + if (!orig_page_is_identical(vma, vaddr, fw->page, &pmd_mappable)) + goto remap; + + dec_mm_counter(vma->vm_mm, MM_ANONPAGES); + folio_remove_rmap_pte(folio, fw->page, vma); + if (!folio_mapped(folio) && folio_test_swapcache(folio) && + folio_trylock(folio)) { + folio_free_swap(folio); + folio_unlock(folio); + } + folio_put(folio); + + return pmd_mappable; +remap: + /* + * Make sure that our copy_to_page() changes become visible before the + * set_pte_at() write. + */ + smp_wmb(); + /* We modified the page. Make sure to mark the PTE dirty. */ + set_pte_at(vma->vm_mm, vaddr, fw->ptep, pte_mkdirty(fw->pte)); + return 0; +} + /* * NOTE: * Expect the breakpoint instruction to be the smallest size instruction f= or @@ -475,116 +490,105 @@ static int update_ref_ctr(struct uprobe *uprobe, st= ruct mm_struct *mm, * uprobe_write_opcode - write the opcode at a given virtual address. * @auprobe: arch specific probepoint information. * @vma: the probed virtual memory area. - * @vaddr: the virtual address to store the opcode. - * @opcode: opcode to be written at @vaddr. + * @opcode_vaddr: the virtual address to store the opcode. + * @opcode: opcode to be written at @opcode_vaddr. * * Called with mm->mmap_lock held for read or write. * Return 0 (success) or a negative errno. */ int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct= *vma, - unsigned long vaddr, uprobe_opcode_t opcode) + const unsigned long opcode_vaddr, uprobe_opcode_t opcode) { + const unsigned long vaddr =3D opcode_vaddr & PAGE_MASK; struct mm_struct *mm =3D vma->vm_mm; struct uprobe *uprobe; - struct page *old_page, *new_page; int ret, is_register, ref_ctr_updated =3D 0; - bool orig_page_huge =3D false; unsigned int gup_flags =3D FOLL_FORCE; + struct mmu_notifier_range range; + struct folio_walk fw; + struct folio *folio; + struct page *page; =20 is_register =3D is_swbp_insn(&opcode); uprobe =3D container_of(auprobe, struct uprobe, arch); =20 -retry: + if (WARN_ON_ONCE(!is_cow_mapping(vma->vm_flags))) + return -EINVAL; + + /* + * When registering, we have to break COW to get an exclusive anonymous + * page that we can safely modify. Use FOLL_WRITE to trigger a write + * fault if required. When unregistering, we might be lucky and the + * anon page is already gone. So defer write faults until really + * required. Use FOLL_SPLIT_PMD, because __uprobe_write_opcode() + * cannot deal with PMDs yet. + */ if (is_register) - gup_flags |=3D FOLL_SPLIT_PMD; - /* Read the page with vaddr into memory */ - ret =3D get_user_pages_remote(mm, vaddr, 1, gup_flags, &old_page, NULL); + gup_flags |=3D FOLL_WRITE | FOLL_SPLIT_PMD; + +retry: + ret =3D get_user_pages_remote(mm, vaddr, 1, gup_flags, &page, NULL); if (ret !=3D 1) - return ret; + goto out; =20 - ret =3D verify_opcode(old_page, vaddr, &opcode); + ret =3D verify_opcode(page, opcode_vaddr, &opcode); + put_page(page); if (ret <=3D 0) - goto put_old; - - if (is_zero_page(old_page)) { - ret =3D -EINVAL; - goto put_old; - } - - if (WARN(!is_register && PageCompound(old_page), - "uprobe unregister should never work on compound page\n")) { - ret =3D -EINVAL; - goto put_old; - } + goto out; =20 /* We are going to replace instruction, update ref_ctr. */ if (!ref_ctr_updated && uprobe->ref_ctr_offset) { ret =3D update_ref_ctr(uprobe, mm, is_register ? 1 : -1); if (ret) - goto put_old; + goto out; =20 ref_ctr_updated =3D 1; } =20 - ret =3D 0; - if (!is_register && !PageAnon(old_page)) - goto put_old; - - ret =3D anon_vma_prepare(vma); - if (ret) - goto put_old; - - ret =3D -ENOMEM; - new_page =3D alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr); - if (!new_page) - goto put_old; - - __SetPageUptodate(new_page); - copy_highpage(new_page, old_page); - copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE); - if (!is_register) { - struct page *orig_page; - pgoff_t index; - - VM_BUG_ON_PAGE(!PageAnon(old_page), old_page); - - index =3D vaddr_to_offset(vma, vaddr & PAGE_MASK) >> PAGE_SHIFT; - orig_page =3D find_get_page(vma->vm_file->f_inode->i_mapping, - index); - - if (orig_page) { - if (PageUptodate(orig_page) && - pages_identical(new_page, orig_page)) { - /* let go new_page */ - put_page(new_page); - new_page =3D NULL; - - if (PageCompound(orig_page)) - orig_page_huge =3D true; - } - put_page(orig_page); - } + /* + * In the common case, we'll be able to zap the page when + * unregistering. So trigger MMU notifiers now, as we won't + * be able to do it under PTL. + */ + mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, + vaddr, vaddr + PAGE_SIZE); + mmu_notifier_invalidate_range_start(&range); + } + + /* Walk the page tables again, to perform the actual update. */ + folio =3D folio_walk_start(&fw, vma, vaddr, 0); + if (folio) { + ret =3D __uprobe_write_opcode(vma, &fw, folio, opcode_vaddr, + opcode); + folio_walk_end(&fw, vma); + } else { + ret =3D -EAGAIN; } =20 - ret =3D __replace_page(vma, vaddr & PAGE_MASK, old_page, new_page); - if (new_page) - put_page(new_page); -put_old: - put_page(old_page); + if (!is_register) + mmu_notifier_invalidate_range_end(&range); =20 - if (unlikely(ret =3D=3D -EAGAIN)) + switch (ret) { + case -EFAULT: + gup_flags |=3D FOLL_WRITE | FOLL_SPLIT_PMD; + fallthrough; + case -EAGAIN: goto retry; + default: + break; + } =20 +out: /* Revert back reference counter if instruction update failed. */ - if (ret && is_register && ref_ctr_updated) + if (ret < 0 && is_register && ref_ctr_updated) update_ref_ctr(uprobe, mm, -1); =20 /* try collapse pmd for compound page */ - if (!ret && orig_page_huge) + if (ret > 0) collapse_pte_mapped_thp(mm, vaddr, false); =20 - return ret; + return ret < 0 ? ret : 0; } =20 /** --=20 2.48.1