From patchwork Tue Sep 3 23:22:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Yznaga X-Patchwork-Id: 13789504 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 A2D4FCD37B1 for ; Tue, 3 Sep 2024 23:24:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EADAF8D01F8; Tue, 3 Sep 2024 19:24:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E35A68D01E4; Tue, 3 Sep 2024 19:24:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD8AD8D01F8; Tue, 3 Sep 2024 19:24:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AD14F8D01E4 for ; Tue, 3 Sep 2024 19:24:05 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 654EAC06DA for ; Tue, 3 Sep 2024 23:24:05 +0000 (UTC) X-FDA: 82525007250.28.41E357F Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf01.hostedemail.com (Postfix) with ESMTP id 6241240004 for ; Tue, 3 Sep 2024 23:24:03 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=EejzamMD; spf=pass (imf01.hostedemail.com: domain of anthony.yznaga@oracle.com designates 205.220.177.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=1725405717; 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=VK2/rT5IIEmxprXEAU2yy+HHJnHBBZfWU/+zXLoCJkA=; b=EG/6shM9hQS+joPs9i08KDEFgSBUbIsNhWklX3Rq7LUmSrpRlx4npOfwCQu8836bM+UQFO KNvelB4mU7XEL3dVFphx+i3GIQaShuzYp3olDeMjSwvmBsxO4fqRLpLyW1zwiVCqiIktec fMPtGRXV9KLTEWvOWBmYBRrT2Qyk88o= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=EejzamMD; spf=pass (imf01.hostedemail.com: domain of anthony.yznaga@oracle.com designates 205.220.177.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=1725405717; a=rsa-sha256; cv=none; b=KeNMSS/VXvO07gaQTo24i3J0gxfks1FpT2NYUPfSHOyBQvm39fg373IYUWQkGFMdJTBix7 SYoFTe9DNT7y8t9UyavuPU4DM0/BxHTKBn+3IpbqxeKgfvpiN0RXLlnuGmOSz6iRRMkASR UdBHbnKNVvbcvSmTW/ANgzvUN+XImLU= Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 483LBTND022739; Tue, 3 Sep 2024 23:23:51 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=V K2/rT5IIEmxprXEAU2yy+HHJnHBBZfWU/+zXLoCJkA=; b=EejzamMDBK7bPK5bb 0qt0mQjn5qkF+bv+ii/+f+UA/hfcsO4FDUSATiJLfN5sAhb7njBr6XKEg26G43N/ Un0lt+S0CoRg1U2RIXQEp2EBtenc6w8MFGkXGalrrGGgX+F6rBhwPpmnzH1Z5DYr V8mNpL9sRagOGCX1auCuiIUgINwjowoouwBVLdEAAKjCXCelimkJHpMK/Z5SQDTQ TyJViwri2XDZLuPG2Fdm15FlIXv2tB/d/mu3VV4Gd2UIeLec5OK9m409R1dId+Nt iN6SRiC/qf/BhY5+gFW4u/jdhJJw6ekLNzFbxSu0DGBJSoD86IlNKtM/wexRBsJl r0XFQ== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41dk84jug1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Sep 2024 23:23:50 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 483KsE5R001736; Tue, 3 Sep 2024 23:23:49 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 41bsmfmnn0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 Sep 2024 23:23:49 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 483NMkfQ040456; Tue, 3 Sep 2024 23:23:48 GMT Received: from localhost.us.oracle.com (dhcp-10-159-133-114.vpn.oracle.com [10.159.133.114]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 41bsmfmmwr-11; Tue, 03 Sep 2024 23:23:48 +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, andreyknvl@gmail.com, dave.hansen@intel.com, luto@kernel.org, brauner@kernel.org, arnd@arndb.de, ebiederm@xmission.com, catalin.marinas@arm.com, 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: [RFC PATCH v3 10/10] mshare: add MSHAREFS_CREATE_MAPPING Date: Tue, 3 Sep 2024 16:22:41 -0700 Message-ID: <20240903232241.43995-11-anthony.yznaga@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903232241.43995-1-anthony.yznaga@oracle.com> References: <20240903232241.43995-1-anthony.yznaga@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-03_11,2024-09-03_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxscore=0 mlxlogscore=792 suspectscore=0 phishscore=0 bulkscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2409030187 X-Proofpoint-GUID: o4IuCMD6qVU1RpEdvToueyKdV1ZsTS0a X-Proofpoint-ORIG-GUID: o4IuCMD6qVU1RpEdvToueyKdV1ZsTS0a X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 6241240004 X-Stat-Signature: 3hwewmdihxs8whqxggp5yebwwrf9pjbg X-Rspam-User: X-HE-Tag: 1725405843-734418 X-HE-Meta: U2FsdGVkX1/9h1ABie9oM/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ffG/45P/PLNp+h7B3T3ygjAFNDpky9JgTraFMV6+Z4hwwx+9pfod4CGAkAoVSDR+K8J9BRI+ZhL2h4QDDjcu7wp4r5oPiTmjL9CQLcFly7uDsLy2vfLvczrqJ6/iprATQCUjqujKuILE2AYwNz6izBeIPv54osbdcuJagv/XJd1SZAoo1s5EGPHGmdYZWulg8iMYto= 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() although only shared anonymous memory with some restrictions is supported initially. Signed-off-by: Anthony Yznaga --- include/uapi/linux/msharefs.h | 9 +++++ mm/mshare.c | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 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 8f47c8d6e6a4..7b89bf7f5ffc 100644 --- a/mm/mshare.c +++ b/mm/mshare.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -154,12 +155,65 @@ msharefs_set_size(struct mm_struct *mm, struct mshare_data *m_data, return 0; } +static long +msharefs_create_mapping(struct mm_struct *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; + + /* + * XXX Keep things simple initially and only allow the mapping of + * anonymous shared memory at fixed addresses without unmapping. + */ + if ((flags & (MAP_SHARED | MAP_FIXED)) != (MAP_SHARED | MAP_FIXED)) + goto out; + + if (fd != -1) + goto out; + + flags |= MAP_FIXED_NOREPLACE; + vm_flags = VM_NOHUGEPAGE; + + if (mmap_write_lock_killable(mm)) { + err = -EINTR; + goto out; + } + + err = 0; + mapped_addr = __do_mmap(NULL, addr, size, prot, flags, vm_flags, + 0, &populate, NULL, mm); + + if (IS_ERR_VALUE(mapped_addr)) + err = (long)mapped_addr; + + mmap_write_unlock(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 *mm = m_data->mm; struct mshare_info minfo; + struct mshare_create mcreate; switch (cmd) { case MSHAREFS_GET_SIZE: @@ -188,6 +242,23 @@ msharefs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) return msharefs_set_size(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.start == 0) { + spin_unlock(&m_data->m_lock); + return -EINVAL; + } + spin_unlock(&m_data->m_lock); + + return msharefs_create_mapping(mm, m_data, &mcreate); + default: return -ENOTTY; }