From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 9937F43E07D for ; Tue, 11 Aug 2026 11:22:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786447367; cv=none; b=OR/UNmuaJdZPy0g518yra1etxPNC5fmiEDJd+YI0wXEJl+JZUYYdnIg9Yd1RENpPOJc1OA1FIRrhPkt+zbjV6pjifCzQG0iz7qBFcMMKr25ZxgDrxEh5UZB2m+yyWacN30ocLXmtWK7LB5s1mbJOuEuMro542Q4IUlRqvNgdQUk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786447367; c=relaxed/simple; bh=wLw+wL5lOK5ZHxFbzv7hNMRisyZ2gWWpu7JX/Ww1mB4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kumVCrs0V0Lw82yEbhwVl6Qo5WqaZuu7H0o3+aaPZ+HhC9VMNrAKrS1fFOmIOUgd0K4PPMFSSuBeAmuK1Glws7+g/YMxkMPe9mzg6vHUusoxZHDxv7T9gteR6bW6EUcsaBv6r3dwAltSQw9lu5D0KOEVpN69m8ULKYeyhLLmC1o= 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=ry0r6Tj7; arc=none smtp.client-ip=209.85.128.44 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="ry0r6Tj7" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-49558ce01afso5929035e9.1 for ; Tue, 11 Aug 2026 04:22:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786447359; x=1787052159; 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=Oj+eoknvOaxVg+TzcZeSMNSYsDXOGxOvzczJwlu1YLQ=; b=ry0r6Tj7hJUIrbXgcsVrZuj0QeERRGDMZji/eoNIQk3MAVPFE6fYgkmZMsJYQ2rmqS TO2qtcHjVNoCbkvGoH62rzcv88mvaqFMAeG760x83pZNyl9nS49SHwr/D0U4JNPpnFyp fGn94lLFtmrzb1boiR9uy3HxIg01AAoUDOPME4XjBptYBrPQFYSdqUsoqeIURe8oCHFh CEcrC904z1R+8dvjsjaPkyjbP0GaV01GYbPozF/BDFvuX22ANwO7ndXGyMp9AS3fNwD2 q3pwtNqZUSBgZ9t7S22O9jbfF4lsdP8Twde/CVblT9Ua45AmSiwNwxv1O9Lrw/qOPWbk JPmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786447359; x=1787052159; 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=Oj+eoknvOaxVg+TzcZeSMNSYsDXOGxOvzczJwlu1YLQ=; b=qx6h3Vs+abEVOs7d6Izzfw4Ty6+9XAry6ZQ8BAMKjNo7AfoBIXZ5Koe+/ldE7Gfth9 MZHQDR1iRGHiga9TuvaUX63dqNxeYrkvqhjgtvXWXAya+vc1kMWe3alU6T2DIHyfPQQU cNM41scVwGpdvbMITdha46yqtJvTJl9ZsTy8624i13JPo4nu3mGonHB89bhNhmuJidFk u1Wu1sf88vjowhvhKPXSSCRVwmMKcLFYXVZFa+GUmnxmRVSaGWOufMpMqwGwk9IoJpv6 TJK1gX6exSPTC7wOOYy8zaddoQzwkr8u4OGpfJxq89Mv9lSocRnEVtmFPYyr6Y5XXo+N zXpg== X-Gm-Message-State: AOJu0YxBR5N3T+g7WV8TlaaA64L+80pPoUHdXyat7t8Y8ZjggXkNmJm/ DwItMlUFjYgAPJ1eYKE3UChEp4prKMhajABw3S49S7ctv07JHjUcylcv X-Gm-Gg: AR+sD10ioRMTjBc3lp/zasAITd3RYxSjZ50g9C8KDNlY/Qb6e7JigQnFC1mnREoOdXh ExUyDH/YdMAjU/EW5R+IhiERII4AjELHdqgiQ9hvCWTkLturxdxGYZ0USiGvUDaPoX981lm85Zm 3uC/x3sidtv0mMEsYKQd+vud3Mx1vVFrN7ZW9/pvJNOhkCxCLbRUTVJ/f8dPtISlc3Zs4RMtAs3 yk/DALvBTi7u9hvuMAhkQ9wiG5Gs0UKOxLEqKXoPg+szRm3qTjA14d4PYdxiQ9dVUispur07pr9 uQjiRAiulDBym9cpTsPOyLCIVTQxrrgi0TGsFf51tkg5yRZMIAxzOiWuVpsGIzSoDauHLGaZ2GB kDrIFgt+OkCMI38gGaJHO8AD1xt8aFAwR5Pz5ILM3MiQW3QOYgISSGUf2N61S+V5eXiKChU0mZa m9gzq4/7PwQWSuHiksgUfxoBj1C+1Vwe7i6ok90D+n1fwuDWsEiWK0Sprqg92uuhGzIfPcbdJeN pqeOIYQDE2EXNY= X-Received: by 2002:a05:600c:4695:b0:499:726a:117c with SMTP id 5b1f17b1804b1-49978494f70mr32371385e9.19.1786447359204; Tue, 11 Aug 2026 04:22:39 -0700 (PDT) Received: from dell-desktop ([2a02:587:4b5e:1100:7303:647d:7cc4:8552]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4997889c2bcsm47547665e9.3.2026.08.11.04.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2026 04:22:38 -0700 (PDT) From: Anastasios Papagiannis To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.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, tasos.papagiannnis@gmail.com Subject: [PATCH bpf-next 1/5] mm: Add copy_remote_mm_str() Date: Tue, 11 Aug 2026 14:21:50 +0300 Message-ID: <20260811112154.94053-2-tasos.papagiannnis@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260811112154.94053-1-tasos.papagiannnis@gmail.com> References: <20260811112154.94053-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. This patch adds 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 | 25 ++++++++++++++++++++++++- mm/nommu.c | 25 ++++++++++++++++++++++++- 3 files changed, 50 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..03cbd3b02bef 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -7218,6 +7218,29 @@ 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. Always guarantees a NUL-terminated buffer. + * 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 +7270,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..8fbe6306d074 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1752,6 +1752,29 @@ 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. Always guarantees a NUL-terminated buffer. + * 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 +1804,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