From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52249449B28 for ; Thu, 20 Aug 2026 13:18:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787231920; cv=none; b=pmio/ygR3o+f4Qy7Q2S2Y/rWBPqCjRndm8qyGAgsH5du/n//SytprPIEIqHH0fBtWjZlpZi+WcTKy7L3K8fAZornr1gECMVjpr1wwaGlnegd3CQ+mSMeRox+zCYWD97wxgvBKhXF7t/z0Omi1KAbNIqM0MKE+V63t0aF0JR2xHQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787231920; c=relaxed/simple; bh=B2fucAtdhxsImTUAGZWjue4CaG9TwF8/ytvWhAOak1g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Nmb7VvB2tStp/V09JajEzb2ZEDtAeXN13KCfuhdzTzq3hZ1Zg3ox/D537kizt/bZc7D3dZDyUKMP9VViA52Edi334eM/QI/hUZDVGnTHCYHkyFdqpo9wdY+QCvD1qj2UrPqDSZnfDp3vgbZTAz7O8a7Y4rSJLAobMFUl5ZJgRuw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SnnW2LK6; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SnnW2LK6" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-496bb7cdf51so26531145e9.2 for ; Thu, 20 Aug 2026 06:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787231918; x=1787836718; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=d/dzYche3n13dnE4ECcGdXysT5dUdq4Uh980G2RdpPA=; b=SnnW2LK6X04KPLB43BPAdE50px/DsktrZGQv28Rbm/D9hpv56ie965uLNyVs32s31v 2QvCsSXjs1vFmKcPDwKn3f4klDreOjO14QLSJqr2o35Btyqpj/aM89kSRjbwGoCnL2AK xU6ZXAYpm9ZfJ7IwM8BxApq+4eFChJlncBQPY9X27EEjTObE5vev5yVuKq4c68MFKJp4 76g09XAp8gjMSSrrUbY1VDfXRI4rQx6rqLSBRNahJwRUaNulTl2C4qt+QQpHKVk77sDN kLC1b4dxVyNlhvolyFOmZHv6Fe6i8hGOK/wVjlWHdUkQHnHT+ML2EHXVeY3GrNANoIWR f5IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787231918; x=1787836718; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=d/dzYche3n13dnE4ECcGdXysT5dUdq4Uh980G2RdpPA=; b=ZmqlbSfxcxm4B8+tAGTEoPJUlEHn7YkQ+xc6aZQ7NAGHiRC49QGqUNcYE08VPF1dG/ ycDW1jUGauzTmkSz0zo8FXYmIsZAcinLFpjgy6cr2MR7qALRyjCVnWicuKGAwotSPDwz Y5UC7Vm/HczEfGXpkYWvVxygd4kEXWv4thqNn9txVfPL6b1fUgVvbTVoPk8XgrFym3gc bVc2hzkaJ+kg6F/9+qamSUXd9UBF61PRS8xiC2dABXzVnqwLXzEgMZtx0byX0Ojg28Hh JfjrHqnKoJ0jTp8VFoiPRMApHBFaUpB+LgrhQbMRuYv4L1pBkqMDEo9QQ7SCozQccGuv d2zw== X-Gm-Message-State: AOJu0YyLw2sUTFnAQaAy7dv+kPayb/XE8OqnxQFSFY/DIJtmKBjc6Zsf nCfT1kIzvMEVZ7AYnu2bA7u8zUvc98QpgZKQ9UqcY2T+nj6WVTLtc69W X-Gm-Gg: AR+sD125HlnXZvHXfo/sluZ2QzIIrdvBFM8pGKygL4pOj8BnQeeBWcNq4TYTTUniJ3Q Jt2YT24NJj3F6InJIRX5ySfyGRGnhIibs8j+0b4HV59UGoBOQ6yUyYlZ6T8wZgXOPPgqZWrDOiO UYn6CMmZTZwc6HcCAHjau/KAoIjpNps11fhgPlqai52hXqPHnHdv2EfovYflzA5zeofLadfdFzU qkoQ8tM8yH0e03e8GWdzGfYz15cUyIo9VrXbrSZne5AlfRbsYXWHXkZtQHA4gkkQmwen0+C9W7O gI3C3uvoMcNobdCCKF+lp64YrOpkmQfu9heJm362FJhI9MNdlFMYZ9u3ngXUQ3DVQgk1wBkgNd8 TzzWbndlOAbcHl+og24RgcAck0P671AiK7+PNSbISuEjNE21pqtjiwZz8juLfsDA4ytuhVjugIi 1fBwmbG+5jwFXFCtliPcEiftO8W3YWANPeQcbSKiSN8AuB+2SA71cJiUODC4iLLk80m9GTFvmr0 E2DWqEaLQ== X-Received: by 2002:a05:600c:6749:b0:495:6274:56c2 with SMTP id 5b1f17b1804b1-499aa1896f1mr232088665e9.2.1787231917285; Thu, 20 Aug 2026 06:18:37 -0700 (PDT) Received: from dell-desktop ([2a02:587:4b5e:1100:7303:647d:7cc4:8552]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-499aa0e473dsm136027275e9.15.2026.08.20.06.18.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2026 06:18:36 -0700 (PDT) From: Anastasios Papagiannis To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk, brauner@kernel.org, akpm@linux-foundation.org, david@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, kpsingh@kernel.org, matt@bobrowski.net, song@kernel.org, utilityemal77@gmail.com, tasos.papagiannnis@gmail.com Subject: [PATCH bpf-next v2 1/3] mm: Add copy_remote_mm_str() Date: Thu, 20 Aug 2026 16:17:59 +0300 Message-ID: <20260820131801.68759-2-tasos.papagiannnis@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260820131801.68759-1-tasos.papagiannnis@gmail.com> References: <20260820131801.68759-1-tasos.papagiannnis@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit copy_remote_vm_str() gets the target address space from a struct task_struct. This does not work for an address space that exists but is not yet associated with a task_struct, such as the mm held by struct linux_binprm during exec. Add copy_remote_mm_str(), which operates directly on a struct mm_struct. Signed-off-by: Anastasios Papagiannis --- include/linux/mm.h | 2 ++ mm/memory.c | 26 +++++++++++++++++++++++++- mm/nommu.c | 26 +++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 485df9c2dbdd..eede435bf4a3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3222,6 +3222,8 @@ extern int access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, int len, unsigned int gup_flags); #ifdef CONFIG_BPF_SYSCALL +extern int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags); extern int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, void *buf, int len, unsigned int gup_flags); #endif diff --git a/mm/memory.c b/mm/memory.c index 6b8280cfc1db..da0010c4475d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -7218,6 +7218,30 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, return buf - old_buf; } +/** + * copy_remote_mm_str - copy a string from a remote address space. + * @mm: the remote address space + * @addr: start address to read from + * @buf: destination buffer + * @len: number of bytes to copy + * @gup_flags: flags modifying lookup behaviour + * + * The caller must hold a reference on @mm. + * + * Return: number of bytes copied from @addr (source) to @buf (destination), + * not including the trailing NUL. If @len is zero, return 0 without accessing + * @buf. Otherwise, @buf is always NUL-terminated. On any error, return + * -EFAULT. + */ +int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + if (unlikely(len == 0)) + return 0; + + return __copy_remote_vm_str(mm, addr, buf, len, gup_flags); +} + /** * copy_remote_vm_str - copy a string from another process's address space. * @tsk: the task of the target address space @@ -7247,7 +7271,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, return -EFAULT; } - ret = __copy_remote_vm_str(mm, addr, buf, len, gup_flags); + ret = copy_remote_mm_str(mm, addr, buf, len, gup_flags); mmput(mm); diff --git a/mm/nommu.c b/mm/nommu.c index ed3934bc2de4..1fca0bb7f042 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1752,6 +1752,30 @@ static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, return ret; } +/** + * copy_remote_mm_str - copy a string from a remote address space. + * @mm: the remote address space + * @addr: start address to read from + * @buf: destination buffer + * @len: number of bytes to copy + * @gup_flags: flags modifying lookup behaviour (unused) + * + * The caller must hold a reference on @mm. + * + * Return: number of bytes copied from @addr (source) to @buf (destination), + * not including the trailing NUL. If @len is zero, return 0 without accessing + * @buf. Otherwise, @buf is always NUL-terminated. On any error, return + * -EFAULT. + */ +int copy_remote_mm_str(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + if (unlikely(len == 0)) + return 0; + + return __copy_remote_vm_str(mm, addr, buf, len); +} + /** * copy_remote_vm_str - copy a string from another process's address space. * @tsk: the task of the target address space @@ -1781,7 +1805,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, return -EFAULT; } - ret = __copy_remote_vm_str(mm, addr, buf, len); + ret = copy_remote_mm_str(mm, addr, buf, len, gup_flags); mmput(mm); -- 2.55.0