From patchwork Fri Jan 24 23:54:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Yznaga X-Patchwork-Id: 13950037 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 98E5FC0218B for ; Fri, 24 Jan 2025 23:56:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 720E72800BD; Fri, 24 Jan 2025 18:56:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6A0962800B0; Fri, 24 Jan 2025 18:56:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 47E202800BD; Fri, 24 Jan 2025 18:56:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 296052800B0 for ; Fri, 24 Jan 2025 18:56:15 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D63C8A1679 for ; Fri, 24 Jan 2025 23:56:14 +0000 (UTC) X-FDA: 83044006668.21.D4D1D26 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf25.hostedemail.com (Postfix) with ESMTP id B566BA0010 for ; Fri, 24 Jan 2025 23:56:12 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="a2Fn/my0"; spf=pass (imf25.hostedemail.com: domain of anthony.yznaga@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=anthony.yznaga@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737762973; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fcIGcV/98l8ZSjQDmXrF3affeH5gAuXvROyoFcfZEuw=; b=uZ5anrQ+xneXc2ajUWZ8FoydgdOAcMXxi7h69wytk8Ao/+I1ITvCXOjlsqerzrR/wnEwUg yjYTHWT7FjhhPu/a48exIQqWepFxGshOOhAnu5WClBBv4nvJALkxAuM7CwsOx236sh5o9W USgr3A77IMA3w1Ey8fx/WEob7EkD7WM= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="a2Fn/my0"; spf=pass (imf25.hostedemail.com: domain of anthony.yznaga@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=anthony.yznaga@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737762973; a=rsa-sha256; cv=none; b=kQdjrloNcCoqER2BTMxbtMkfKuXJkH6i/dt23EZ5qs9ojsOgsKUOzyHIfCYhBULkt/Tio+ 8OUcTpDjNfS+8RkQAVHT80Y3uMAzGumz3CeQSYkhQPlV+Goyr0S9fQJVuPwdAxq0+Xgh0F JbNDZ9pe81BWXlLixsSjZiZ6VV15eGw= Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 50OIi39S019133; Fri, 24 Jan 2025 23:56:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=fcIGc V/98l8ZSjQDmXrF3affeH5gAuXvROyoFcfZEuw=; b=a2Fn/my0YFitA1HTu3rMs vVjepHWSc4mtb1D4lZwU2+HUiFmyAfRG+b2J9B7sTQIhkhHv+5so55NFpuw+9QB6 5IlnBjtBB1nScTizxp8+HvWpPPlV8Xk7iu5bC2OppEWguINRcXS8x0rTEEduQ8zY 6DphIs0lyq7VA2SY/CjUxmOmQR2yda66f9hy9vTq5C4r9eoCY48aAV/BQhk/YKEf gXDR570rmgzu1e/LVmfqRVSIMF9lquv00jDKnJAiVDdsXsi3N1RZMtOdsZjGiBDt gYZ8Dcz/6PLRmaL0lP7CNShlYBppS11nC8cJec/kCQ23yo/t+0IRNU8t1fN4NuzE w== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 4485qaw408-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 24 Jan 2025 23:56:00 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 50OMD09b036431; Fri, 24 Jan 2025 23:55:59 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44917u4ama-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 24 Jan 2025 23:55:59 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 50ONsxQE018051; Fri, 24 Jan 2025 23:55:58 GMT Received: from localhost.us.oracle.com (dhcp-10-65-130-174.vpn.oracle.com [10.65.130.174]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44917u49ww-17; Fri, 24 Jan 2025 23:55:58 +0000 From: Anthony Yznaga To: akpm@linux-foundation.org, willy@infradead.org, markhemm@googlemail.com, viro@zeniv.linux.org.uk, david@redhat.com, khalid@kernel.org Cc: anthony.yznaga@oracle.com, jthoughton@google.com, corbet@lwn.net, dave.hansen@intel.com, kirill@shutemov.name, luto@kernel.org, brauner@kernel.org, arnd@arndb.de, ebiederm@xmission.com, catalin.marinas@arm.com, mingo@redhat.com, peterz@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.com, vbabka@suse.cz, jannh@google.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, tglx@linutronix.de, cgroups@vger.kernel.org, x86@kernel.org, linux-doc@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mhiramat@kernel.org, rostedt@goodmis.org, vasily.averin@linux.dev, xhao@linux.alibaba.com, pcc@google.com, neilb@suse.de, maz@kernel.org Subject: [PATCH 16/20] mshare: add MSHAREFS_CREATE_MAPPING Date: Fri, 24 Jan 2025 15:54:50 -0800 Message-ID: <20250124235454.84587-17-anthony.yznaga@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250124235454.84587-1-anthony.yznaga@oracle.com> References: <20250124235454.84587-1-anthony.yznaga@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-01-24_10,2025-01-23_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2411120000 definitions=main-2501240162 X-Proofpoint-GUID: 5QNgZKQltQ9yn3rK-ZEnWzG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RqHc2Guw8SzoHsWWescvisAVll5O0TKkdAXgjLlTVd7ruu6oMGON/mAKBKCzUwO2UftmUKSfvnui75dAmCvn8u3sMTfJ YtvMnue2 af+l5UXKTePVN3oBOMiUy2iLh+XMTNqqp2d1N55Kx9wwcSQGFsDcr3YH6DWeaNVE/ygPV6S0qf9Y//1ZvKDAul/zHqe8M4sYqXZZA6b+XZcytXhW/BJS87ImCHEcYKnNT6xMsLrpferq/ABmRVJudqF2B0QN7PlR5i2Brp9F4QtLXV5u11cyO+wiQYGibrdRLtTH9/aONQeFFqmoPyh+BGSM4tHT+E3ZZLZ1XygIQx2lzXRHUwW9YgdcL5LVsthuTTHU9pvE8+GN73UrAoERCleDKkw== 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: Add an ioctl for mapping objects within an mshare region. The arguments are the same as mmap(). Only shared anonymous memory mapped with MAP_FIXED is supported initially. Signed-off-by: Anthony Yznaga --- include/uapi/linux/msharefs.h | 9 +++++ mm/mshare.c | 65 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/include/uapi/linux/msharefs.h b/include/uapi/linux/msharefs.h index c7b509c7e093..fea0afdf000d 100644 --- a/include/uapi/linux/msharefs.h +++ b/include/uapi/linux/msharefs.h @@ -20,10 +20,19 @@ */ #define MSHAREFS_GET_SIZE _IOR('x', 0, struct mshare_info) #define MSHAREFS_SET_SIZE _IOW('x', 1, struct mshare_info) +#define MSHAREFS_CREATE_MAPPING _IOW('x', 2, struct mshare_create) struct mshare_info { __u64 start; __u64 size; }; +struct mshare_create { + __u64 addr; + __u64 size; + __u64 offset; + __u32 prot; + __u32 flags; + __u32 fd; +}; #endif diff --git a/mm/mshare.c b/mm/mshare.c index 9ada1544aeb1..d70f10210b46 100644 --- a/mm/mshare.c +++ b/mm/mshare.c @@ -194,12 +194,60 @@ msharefs_set_size(struct mm_struct *host_mm, struct mshare_data *m_data, return 0; } +static long +msharefs_create_mapping(struct mm_struct *host_mm, struct mshare_data *m_data, + struct mshare_create *mcreate) +{ + unsigned long mshare_start, mshare_end; + unsigned long mapped_addr; + unsigned long populate = 0; + unsigned long addr = mcreate->addr; + unsigned long size = mcreate->size; + unsigned int fd = mcreate->fd; + int prot = mcreate->prot; + int flags = mcreate->flags; + vm_flags_t vm_flags; + int err = -EINVAL; + + mshare_start = m_data->minfo.start; + mshare_end = mshare_start + m_data->minfo.size; + + if ((addr < mshare_start) || (addr >= mshare_end) || + (addr + size > mshare_end)) + goto out; + + /* + * Only anonymous shared memory at fixed addresses is allowed for now. + */ + if ((flags & (MAP_SHARED | MAP_FIXED)) != (MAP_SHARED | MAP_FIXED)) + goto out; + if (fd != -1) + goto out; + + if (mmap_write_lock_killable(host_mm)) { + err = -EINTR; + goto out; + } + + err = 0; + mapped_addr = __do_mmap(NULL, addr, size, prot, flags, vm_flags, + 0, &populate, NULL, host_mm); + + if (IS_ERR_VALUE(mapped_addr)) + err = (long)mapped_addr; + + mmap_write_unlock(host_mm); +out: + return err; +} + static long msharefs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct mshare_data *m_data = filp->private_data; struct mm_struct *host_mm = m_data->mm; struct mshare_info minfo; + struct mshare_create mcreate; switch (cmd) { case MSHAREFS_GET_SIZE: @@ -228,6 +276,23 @@ msharefs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) return msharefs_set_size(host_mm, m_data, &minfo); + case MSHAREFS_CREATE_MAPPING: + if (copy_from_user(&mcreate, (struct mshare_create __user *)arg, + sizeof(mcreate))) + return -EFAULT; + + /* + * validate mshare region + */ + spin_lock(&m_data->m_lock); + if (m_data->minfo.size == 0) { + spin_unlock(&m_data->m_lock); + return -EINVAL; + } + spin_unlock(&m_data->m_lock); + + return msharefs_create_mapping(host_mm, m_data, &mcreate); + default: return -ENOTTY; }