From patchwork Tue Jan 18 21:19:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Aziz X-Patchwork-Id: 12716906 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 93C06C433F5 for ; Tue, 18 Jan 2022 21:20:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D5C606B0078; Tue, 18 Jan 2022 16:20:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D0B2D6B0073; Tue, 18 Jan 2022 16:20:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC0D96B007B; Tue, 18 Jan 2022 16:20:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id 80A9C6B0075 for ; Tue, 18 Jan 2022 16:20:40 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 4197E92779 for ; Tue, 18 Jan 2022 21:20:40 +0000 (UTC) X-FDA: 79044677040.14.DC20F02 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf25.hostedemail.com (Postfix) with ESMTP id ADB4CA0004 for ; Tue, 18 Jan 2022 21:20:39 +0000 (UTC) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20IKEZlG013780; Tue, 18 Jan 2022 21:20:15 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 : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=2GI/FyC9Fd+f3mWeDhzM6dyhQ/6gPBjtaUMfsTe/i04=; b=SjhpDC/uP72XkvfuesEeUHwYqG75tJd0s0jqlf2WSPaBXp/dwbmoUjeEyYH0Iti5yzuh h+kTh9Udeme5+t0AIM1jNtmwkx8lZbcaE+fc3YmJjmyKOLRPHvRLeFhKhwajJJieUzn2 CRHurC4EUqMsZz0sPkgVt/ZEan7inDjqiHFOtyMCJdWO0ergOsfneUQ2PMzon/M9jS0L uqb/XyqDsTYWduZOA4VHLDS7GOA7+p/w1rUSXelXx0DGV31vG0hqjn5Ekj88hi11PfJo LaBS/eQQ00xgfaBpFIgOGpWfKu9Ynf2NpTItWlI0GkzBmvppwhnyIi+quBlpf74Tx/KQ rQ== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc52u7f9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:15 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20IKubiD030214; Tue, 18 Jan 2022 21:20:14 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2173.outbound.protection.outlook.com [104.47.59.173]) by aserp3020.oracle.com with ESMTP id 3dkp34uybd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VedLcVOom2iUF3p6n68PetB3WQsYLzNywmuGK6OtngKaOdvryy9it3U5RAK2cYPKlfbG/oITk/Zxau0/d5aA43vB4IEmHwq+AW8rqVw54SchzbzCMougzxpHElOem0LMjFj9ce5khlWrJXuvj2BaTeYeykatE9IE7TsHLvNjTFo1HFi/NJYYI/yx2TeNqlTRuACa/wmt+PRbrL4+bTTeo/qPGFr6pe2eNKEFDpW/EtE+fcbuEQ93dseVF1x9K9TmnIWIsJLbvx2fY7P9hC6RELT2M21LmIvXA/QXjJltsF0Bhi1frL0cFiBpltpJw/RxyopI3O7jB4+/RL9PHua87w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2GI/FyC9Fd+f3mWeDhzM6dyhQ/6gPBjtaUMfsTe/i04=; b=iVoTedZML3X5aIlIqJwoIs9vZvTlfKk2Vl2V3uOhjgXeVOvPQpEbFIvSTbDT6B4wFdef8ZTtqA95gCILUXFmGBc6lYq3y/UmLPMWglynoBBb+aCjua7wwkNYVSY6/6B5Lo0W1H320f4sYn7pyX+OVLS5Ajx9OoXHhp5TYYwTiFaEdcW/etLZVEMyFFrTQmfsPpA4B7jMjwT8vdWZMrK9qgn8/XbWR56SgUZbbhxXv5mgHndGe5G4aZtE5NmaS7o/2Ob5Ah2F/lVBLrCiZIji/NADYQ2Qb3tvtONtKIZwc/SWZwswWo+S21FyvOzm7Qp305/4hlqiVKfWoSSHfrnZCw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2GI/FyC9Fd+f3mWeDhzM6dyhQ/6gPBjtaUMfsTe/i04=; b=ZjTCbkQfniRHDIp0JhrZIZ9IAZX9rzBijXGCOk+1nlUTIHIoScGWdb8Pj1qsdKouEwCmNVY4JDV/1YLzXzKVsCYA11COwagrffDPNLN4t0lpq1JHo440HDh4JdWv4GV4f/cy7uetAeb1IWqPZO+jN/RSE1JpQrXsHfNT8GP4bGo= Received: from BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) by CH2PR10MB4374.namprd10.prod.outlook.com (2603:10b6:610:af::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.10; Tue, 18 Jan 2022 21:20:12 +0000 Received: from BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2]) by BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2%6]) with mapi id 15.20.4888.014; Tue, 18 Jan 2022 21:20:12 +0000 From: Khalid Aziz To: akpm@linux-foundation.org, willy@infradead.org, longpeng2@huawei.com, arnd@arndb.de, dave.hansen@linux.intel.com, david@redhat.com, rppt@kernel.org, surenb@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Khalid Aziz Subject: [RFC PATCH 1/6] mm: Add new system calls mshare, mshare_unlink Date: Tue, 18 Jan 2022 14:19:13 -0700 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-ClientProxiedBy: BYAPR07CA0001.namprd07.prod.outlook.com (2603:10b6:a02:bc::14) To BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d9be30c6-2d4f-4fb2-ee63-08d9dac84fde X-MS-TrafficTypeDiagnostic: CH2PR10MB4374:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ahT1t9IOV1iCjem24XL77SmmnDFnR0r6oEScTYcQ7xww7BQdOZDiHKBT9OWlzaScnSNREnLhIXaEVP5Pohmg2u5GYrZR6CLxcRtwi8F7/C6ynljL/7/X40P8uqqcbl4wlKAytrZoy4tlShYoNBztkQ0itlWndddO7o6tUDLM6F+n7/2sk8472INmTj0LFECBWE56e0CHFx64rDeFixHSrUb8h73pe65A/1kfNzmFpAcv6bWcidUfQBdmUdMvI3Mdm84AakZAD7Jrac6ldR6qDau6Q4CJlfndgaezBrdrqk6xC9xLHExOsrUoMKKuQA9BluObWvacpCgkPkF3fR1FZ1DptxGe7MUs3R5u+gViSTO13h8cZPTmYEYut88jK6RJcEI00aXlZ0tOeMmt80FGdBP8Isgo19G0W4RZYmyBS2VM0tT5lhR1OazDX1N+iGHQtYGFhx4CMEwPt/3DY1HXxEtEJ+ZHWq0lL9a/soLCRXAVNSnYp7SfOKCWf+4TTjyHIdWyE+aLzYDtW+YF4y5/K8xSB96anINQP249sV25shqf2KQocLGKh5FPG7x9wlOf6NRU3pB95X0T5+nYMScTpuNP6YFJA61dqN4cv563fBhN/Uhvbk8j5cYYr5HF0lKuqJGeCMCimN0GhTbCel5zastixdRdiwQ8C7k/Hpjph2DUiTqaVveMgol2k5+dzzBZm1o96Ej9eLFiEdI+KLvW1g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN8PR10MB3220.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(44832011)(86362001)(8676002)(2616005)(508600001)(52116002)(4326008)(66476007)(6512007)(5660300002)(7416002)(38100700002)(83380400001)(2906002)(6486002)(6506007)(8936002)(186003)(107886003)(921005)(36756003)(316002)(66946007)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: eOooo4CcR0e5iYC5EmV2iI0khYIERKIKg/W5h5FH0yqgDe8gIUvqJHP5hRfGBJ3qFyIfKHqF85cj/x6V3nE+jyk5/sTBY/7Uygr+SVvMg2bcWecboHG1lUUPhzozdyBTNPtrojfGz8dTuJ7stoz9GTL4splbFcEZjQGjjP0bqRVKY7mJUU7oeZ6yaeIbhYcwvmCWN989OF7fO7GANQcLIseYBLYVG0znFKqR4xPRoONrwvzbmNWYopa2Khlhjf9y6OknAzALnEmyT5bujXks9HI4D6XyxD5D5eXZKimGCAQUhVcMCnqwP060oKNExlho/5MVbzuLALlS1KjbXMz639XqFKHH0eLQBb2kwbKiAliSIe3b3HbQ4tBkIJAP6CLmRVPySx9hrLYlJ7INjVBttunJsPI0zW3NblQsTAWqzVJpXztvNq5oCAdh1xFRcjO6MNt2eW/LNzGBH70FDHDuRnL247QAJaPAGjNbq+eYfZY27841rblFobOdsn8n2iRHkn8U3b7jkdsCu8Z85UmofEL2iwC9YOF7VHTIDCLLVLVRwDO1D7I+v0KX3yQ9dtEkDKvGr4MAyEYO9R48JDGgUt6YkNcXjEYwiXWFHYbE/cC3kJ3JqYh3qO8TfNQyb63lh9oihPUXY2G3mfvTULb1Y6WhNdhq26hWHPEblNmIc0qN0Shi1eLj1Vm/E1gx2OIftmUxCgvt3KhE50+2lYNgHSsIhwALwKeVzmuxz0X+bdB4gRXxpXAV9JDkjnq7kMGUVtJ8c6ARzppRdFu914Q9n59CGt1ZuK0wEKW+tQYxQ4qL8sMDxXmhLgSFLpfPtWEY4I/WnR1hR3R4A97HB/pRqODZ9ogg00uvn59wjH23bQffF3k4lJfT+6V7B2YT7ASUdTeCZd0GvPOtCAuhA6CDR/vbUdjQ5ty4zuXt9yh48fdDSq2QEiOxZ4jK5v9fcjCfnecPo5k23IvX0Pm5NGKyJJkc5GIN4Pi3DnnFhWiiK1F6cdVpt+oZ7JNSj75KzmlJ7PdRUqwHjW0z5AmDzkn5PC3ItoEYh3f7y2i51IjEbUn3D4HHcSzAKbFtQsSyYaLl8hiC5b0fH4hrS9eVG+46uBI5CrupDVfoJQu49co6wYTNoiUWl0tVPVRFALRGV6uqZYBPXbeIA/6+1n6m87y5gFF1VZqlDbgAHSDRyXj76qViR9rvyrbNgP0JSbNO8E2s84tjpxy3Fb91iwAzuluhrTZhbonOBahI9GrVfDgjEaKNBJ6qoCoTAba7iXIWO6Mzt/pjdbMobd0Hi/8GY/vLSfKspNE2blCNZisPfmN5i5pOeEEBIkD/LmEoZcwbioVf35wasGMVUtES8HUm2RvaZZJ3UFgNHlA99J8qX4uk12LPin0mTehbA4IQebQnA50WjrWndcQkEW2a9SMP4YLZAns6+LPBCH0MEpbBlFKE+Z9JeVvZU0tmDyEYYTFFMSL62QIKiwIp8SYYY3LZBjUkOkACMJj7hBs8drJyOuknQ2FW4ewzTpWBIm/C7uSGbXjJwnW8+GlJkjwQu1dY9HmehQnNiiW3No6RTybscnrgIiUOBBX1kXbyEdi2sw/o9V5CsG0ys7GH1xhS0JTRomz2RP0y2gy/zJZlw1q1OLJu1FoN193mTvz+244na23gY2kSvpIO3txwS69I/D6uOpviuQ== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d9be30c6-2d4f-4fb2-ee63-08d9dac84fde X-MS-Exchange-CrossTenant-AuthSource: BN8PR10MB3220.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2022 21:20:12.0147 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 64HI8jpr00PcTa1sByLpjYhij8abPzUBSopdLOdS17yY66+5C6SCEQDX1HLzra/7qjezRj5RARENDrcwUFBOZg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR10MB4374 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10231 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 bulkscore=0 mlxlogscore=999 adultscore=0 suspectscore=0 malwarescore=0 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201180121 X-Proofpoint-GUID: 9JI_DYADL2dZieeENYlWOc3YNv6E9ewe X-Proofpoint-ORIG-GUID: 9JI_DYADL2dZieeENYlWOc3YNv6E9ewe X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: ADB4CA0004 X-Stat-Signature: k16cdmoej31tpjo5mmwmadrg7nxx8dsy Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b="SjhpDC/u"; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=ZjTCbkQf; spf=none (imf25.hostedemail.com: domain of khalid.aziz@oracle.com has no SPF policy when checking 205.220.165.32) smtp.mailfrom=khalid.aziz@oracle.com; dmarc=pass (policy=none) header.from=oracle.com X-HE-Tag: 1642540839-301273 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: Add two new system calls to support PTE sharing across processes through explicit declarations of shared address space.There is almost no implementation in this patch and it only wires up the system calls for x86_64 only. mshare() returns a file descriptor which does not support any operations yet. Signed-off-by: Khalid Aziz Signed-off-by: Matthew Wilcox (Oracle) --- arch/x86/entry/syscalls/syscall_64.tbl | 2 + include/uapi/asm-generic/unistd.h | 7 ++- mm/Makefile | 2 +- mm/mshare.c | 60 ++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 mm/mshare.c diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl index fe8f8dd157b4..bb403deca1ff 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -371,6 +371,8 @@ 447 common memfd_secret sys_memfd_secret 448 common process_mrelease sys_process_mrelease 449 common futex_waitv sys_futex_waitv +450 common mshare sys_mshare +451 common mshare_unlink sys_mshare_unlink # # Due to a historical design error, certain syscalls are numbered differently diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h index 4557a8b6086f..27349ad579ff 100644 --- a/include/uapi/asm-generic/unistd.h +++ b/include/uapi/asm-generic/unistd.h @@ -883,8 +883,13 @@ __SYSCALL(__NR_process_mrelease, sys_process_mrelease) #define __NR_futex_waitv 449 __SYSCALL(__NR_futex_waitv, sys_futex_waitv) +#define __NR_mshare 450 +__SYSCALL(__NR_mshare, sys_mshare) +#define __NR_mshare_unlink 451 +__SYSCALL(__NR_mshare_unlink, sys_mshare_unlink) + #undef __NR_syscalls -#define __NR_syscalls 450 +#define __NR_syscalls 452 /* * 32 bit systems traditionally used different diff --git a/mm/Makefile b/mm/Makefile index d6c0042e3aa0..fca44c0d5e74 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -35,7 +35,7 @@ CFLAGS_init-mm.o += $(call cc-disable-warning, override-init) CFLAGS_init-mm.o += $(call cc-disable-warning, initializer-overrides) mmu-y := nommu.o -mmu-$(CONFIG_MMU) := highmem.o memory.o mincore.o \ +mmu-$(CONFIG_MMU) := highmem.o memory.o mincore.o mshare.o \ mlock.o mmap.o mmu_gather.o mprotect.o mremap.o \ msync.o page_vma_mapped.o pagewalk.o \ pgtable-generic.o rmap.o vmalloc.o diff --git a/mm/mshare.c b/mm/mshare.c new file mode 100644 index 000000000000..c723f8369f06 --- /dev/null +++ b/mm/mshare.c @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * mm/mshare.c + * + * Page table sharing code + * + * + * Copyright (C) 2021 Oracle Corp. All rights reserved. + * Authors: Matthew Wilcox + * Khalid Aziz + */ + +#include +#include +#include + +static const struct file_operations mshare_fops = { +}; + +/* + * mshare syscall. Returns a file descriptor + */ +SYSCALL_DEFINE5(mshare, const char *, name, unsigned long, addr, + unsigned long, len, int, oflag, mode_t, mode) +{ + int fd; + + /* + * Address range being shared must be aligned to pgdir + * boundary and its size must be a multiple of pgdir size + */ + if ((addr | len) & (PGDIR_SIZE - 1)) + return -EINVAL; + + /* + * Allocate a file descriptor to return + * + * TODO: This code ignores the object name completely. Add + * support for that + */ + fd = anon_inode_getfd("mshare", &mshare_fops, NULL, O_RDWR); + + return fd; +} + +/* + * mshare_unlink syscall. Close and remove the named mshare'd object + */ +SYSCALL_DEFINE1(mshare_unlink, const char *, name) +{ + int fd; + + /* + * Delete the named object + * + * TODO: Mark mshare'd range for deletion + * + */ + return 0; +} From patchwork Tue Jan 18 21:19:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Aziz X-Patchwork-Id: 12716910 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 C141DC433EF for ; Tue, 18 Jan 2022 21:20:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA00D6B0075; Tue, 18 Jan 2022 16:20:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9AC516B007E; Tue, 18 Jan 2022 16:20:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 62D446B0080; Tue, 18 Jan 2022 16:20:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0195.hostedemail.com [216.40.44.195]) by kanga.kvack.org (Postfix) with ESMTP id 380B26B0075 for ; Tue, 18 Jan 2022 16:20:42 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id E7C06181C49D9 for ; Tue, 18 Jan 2022 21:20:41 +0000 (UTC) X-FDA: 79044677082.11.0704F19 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf06.hostedemail.com (Postfix) with ESMTP id 3E1E6180006 for ; Tue, 18 Jan 2022 21:20:41 +0000 (UTC) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20IKEdRW023808; Tue, 18 Jan 2022 21:20:20 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 : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=ElbwZLavMHhHKFKqKAZsHLd8K6H2gl0uN3mepsqZ6WA=; b=KpIzta3OaU/S1/oH48Bz3xMwsHw8niDgaByMS0ITY8bdUpfjpjtMgJFx9xOx+4GuF2bG lYeT6zRbgtix+eJ+hKENx3MP+gJL0XAokgKgN+alw+KSYLBAB0BpTCcgUQ7FtAEeK9Gp YiKqiIy63ilCyvyL3T5S36SUbqoRUhKsDrW71fjM7RVx0J/EM9n1+1e1gehFe/zT+FSu z6lEbEmIISmkK67JTkGnQxdDjb5Hr/xrUU4MMMHBYf7HX2zCcou1VYRiYWZLnwnrmzlv 1c6XFOsIAkRg6nPCSCXi6UYMjQrjycEumE16ur4Rhphf7cEMUvYTR1mZOCmHMHAJr1Ej RQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc51b3kk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:20 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20ILK6de062381; Tue, 18 Jan 2022 21:20:18 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2109.outbound.protection.outlook.com [104.47.70.109]) by userp3020.oracle.com with ESMTP id 3dkqqp4r6q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RtdE5p7UczmwE9K9jucstp6iYR4ri+G3BJkHAV+fXWoCTCJ8pYLpbIco0P90QqBXAjz7q5iSGpERvI7ry19QG70jIg3fdfQWXu9hWN2icKaaIvNZwVaFsIEFY6uLjTINopuFO/bbPzSVBsQxWLbc+nSYm4CNIEcxg8ufvgZaSnZYaDFK2jyA8KVwhRjn5jUrYTEfmkGgsmth1GfOwZEYclO3QJQ6GAnJD3ZykTC5fUKQktUUInjZHiHt0xqxtom+ZW9Giisdc/fDXqew/vDK4x8wIi8vDWA5AiuMfebYaayaWH6RGV0mI/HbZO7gCCsnQzz8r5CdrZdPwjb0rxQnQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ElbwZLavMHhHKFKqKAZsHLd8K6H2gl0uN3mepsqZ6WA=; b=cgm8/az71+/QqJ/fX6+z4ZuAVIxJWczVzmZNxWOBQzB/SK82cy+5bx9UyR6usVVk4nF3E2V+FRgh/uZx6VQN2tp0eBQW+C2WpLwkKAkrtXfdFpuZ7AgTiE5YlXr5WjuSJLf0YmxUh6TJAH5So49mGK12MLhdaMiGnhrPz1Stv6eeZHuxaaLqu4fsPC+ZfqUm9o+nSIzXG11MSxytLAVjIdK3mPkKT1deEkwLL3G6diO8zp3tW7xBD9VWZBPtwLoMYHaJjIl8J3ReC6uPFPKu9rp3rNk5xn/3L4WszC2yH6I9uLa+5emTeBG9mBQn0XbhYOTM1PLx9qt75P0yAcFssA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ElbwZLavMHhHKFKqKAZsHLd8K6H2gl0uN3mepsqZ6WA=; b=R7a9lxYLtl0uJ9Y9/+HTxZvBhBRNzndZAsQFBA0k3LyURYj2ijHamlUD9d8zUaMLKrIG/HcvpVRE7jUzYnw5VHlfYKMWgwPgSn7A2Sdqsx6EcwqYyLAfIvcqKdNrf05H4ZdHiQ2APWLBou/jLcHjL+wjylAQJxaFHY1fBztHJcQ= Received: from BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) by MWHPR10MB1584.namprd10.prod.outlook.com (2603:10b6:300:29::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.12; Tue, 18 Jan 2022 21:20:15 +0000 Received: from BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2]) by BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2%6]) with mapi id 15.20.4888.014; Tue, 18 Jan 2022 21:20:15 +0000 From: Khalid Aziz To: akpm@linux-foundation.org, willy@infradead.org, longpeng2@huawei.com, arnd@arndb.de, dave.hansen@linux.intel.com, david@redhat.com, rppt@kernel.org, surenb@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Khalid Aziz Subject: [RFC PATCH 2/6] mm: Add msharefs filesystem Date: Tue, 18 Jan 2022 14:19:14 -0700 Message-Id: <32784ee26d895bae2484e15fef205d5590720c4b.1642526745.git.khalid.aziz@oracle.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-ClientProxiedBy: BYAPR07CA0001.namprd07.prod.outlook.com (2603:10b6:a02:bc::14) To BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2ed5232a-cbc3-4965-31ed-08d9dac851d1 X-MS-TrafficTypeDiagnostic: MWHPR10MB1584:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:419; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jUk6M0zDVUUZ1VXfo+hmHmSgAeaEqGobihZHIjQC5YflK6UEUFJk/Zt59FBxj9f70bkB3IoQn1A7aOMH12QgrXDrYoc/BCORHMer/kn+RDPjy6WfXXYrQwr9b3hJWQJeZ1/uYhwSmZRCWv/Zj1YFyXH9j+eS6Qpg9QWsm2bBpuLim5kFKiWqzYR0hJ5QIdZ3CQ9DALXpjVf36Ac1srhIaLqw7u+SvPHXTbcpXQBDYmUotuUrhFoV34VqCfgKnrCmbP9QfDu5sGQscFxWS8W4LKmIfYpyLt4oc7Tf9paiaWiqZG6QGSMfaJRHbk0WY2Uw35BLWTT+dkzFUgpt1gnYL8zGcdwaf9brNs/AO6BUHBBmFiF6f+rsrCJBaWvIxRhgwFmmhSv4u+dPJsA+gSxcowY6t7u5e4RKuKXs76HLQYsk6idriVQEVnf3jcja51rTZxFHc4po4QtFhBvMwX+mCHrfQgxnHZM+iHLLBNtEuYLlLZ0fWOV8vlX20e3CE6c8Z8cBRaOtzjT4fduG2vynJOJb1kIJlTCYwTBAMMetUPYpuhLfv+mrB5JoaogWQkm0i6rX1+vtiEb6Wqp7ikz6JUSnkY0t8Y0+eA7bzAu1ulLtjnzQFgOcsWKdqQFFgV3T6fPzPVqFs0MSew2PEkvkFHI9oE2w5JqlbXNcusS1oh1WbOQl/xb6u9KtgJcZpyqnA6IcXYJr58EwWDfrN7RbiQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN8PR10MB3220.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(6486002)(921005)(38100700002)(316002)(508600001)(52116002)(6506007)(66476007)(5660300002)(4326008)(86362001)(44832011)(107886003)(66946007)(66556008)(36756003)(8676002)(8936002)(2616005)(6512007)(2906002)(186003)(83380400001)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0IwZUv9V8oQeAZZDFICHEsRo2jkewO9bzgr4MqxGo8Vg70/51uFr+IkPvyL3L0LgUfm5hZEU9E9YcCbKcm0YU+eDqZ0SfpvDCfCW2Zj7LZmDpsQlElAn1XKmeiY/18y5ml8uwSUsAtJoRSp48wdOCJUgXKU8nnvxkQjsvdsMx4D0uZLuA9rUwO2BodSVgVbxYg5Eqwv0LN/mJ6G+x1yrxV53st5X4RSO7cqkgCzNtN9Tvnt0r+hgl/f+Tuf4oBSJEovnMeDULTkRVTNCvnl90KHb8RbeBahem2GZ8Kl7gDa3+iwrN2cBNFSJObaK3dNVNUoQF5gCWOU+IMtH5WDHVssyDbv33ybP4/HQfo0pBfMj0XFfDBmZ8TOCEweR4iHTHCxg9PIXjilDFsMvJmS0Pxk31cLtrj3e/9q9uC3Fr9L5FQKmjPy+1BtcnJ/MC3tBsPXdnM5KlmxAXVhLG5OjkztUMeB+1KgNv7RJCN5RQ3lxASVhh3upyr2bMMi8MdwoDPn5WN8evq5ioG6E2OyrEZ3oY5QsUly5vJ7RvbfCIETcn6cYYAQk8SbZ/osAlH04P7oZMri4qvqbMLdAOu34EugEDFbtyeH8VCLNX19aSGzfaBb1AYO9BQAUzMmY9X+8E2KRMyKjnGrSdoON0YEIPgqjcduBCdAyNOiLdfc3G8H9TlcA69YlpD5JTpw0pWXd9CjvOIJL5XmPS3Dwz0H3oRNvz19bw/Y3cRhwallPObhoRxju7qNufpoJxHc9e3JUvccKggv3bODnIbZBEdMU1kr4QbMXdWoycSN1DAOzQkXrgb3UpHZJjgwxkIFBX3wXf4sRXb5+yClvulQ1XhJWlT3i24d9XDRsGhG8DXHATP/0pfwqOLaYKzgaPn7fMI156xUsi67Snqy326X/cxtSXncaFfs8YEFoOxNtsf5yC3lZkO4bx0jx77HXSudMpxMDj/x2Wd1r7umMybUrVXBPcfb3FIZCdl+8edCS1OawSm1bbnpVFj1X+2Lv0C8MS7UPu8IiFzpc1VygvvTazHWeCsEEoQS80t3w5dk2xStVOMgw68G7x9+DMK3FjoX137890vprVYFOmGdJK72g/JLqEepR5epqujS8uVuoHtRDEF6gtL9R7dycxRj6zqW5MHG4OBupdv9ghl9f7sv6TJhvFoBvuk0/VIRGysIQXAQbHhfRwKEZmDI2RDyvOAier7Z+Ga54rNhG3wYlmgBRDF7CrM68LA+NirYBHiXJgL9ZUI0JtVtK8qM4yzkfmjV9H15Gs0zvqm7GXCH1l2isOYfMU66OPPTPx7ZqgPK4IX6FCv21vfLl9vPlcGpnA39YYhdnfmPQ98TpQ0Z8Y4/wL4SQORylgRQRpbTx9e/pX9psMD9oHhf1idMfkV6MfA+w3FBxOrBnVTE+oVxx9UZ5T7Gb64NO+cciU49XNdulmBsf+UtH0lkl/o3YZxUJamzOY4DXwioGGPSQ20YxZGRJhKHlnbNAC24yu3nV+GZd2dcoV02T2pXdgeg2SMkJSCawkvI3+km/GuGftmodhd7sonDIzdpWDQ0mWzLRnCa2ydbKYXX3TmnvEj9j/pv8BFm432sM/aT499b5P2f7eZLgRZ9X2X6oAm4OIZTTWD1lRPTbd4z4w0LZ0Sv0bkNe861r+p+pQ1e11aQTZIt62ewMopMJFw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2ed5232a-cbc3-4965-31ed-08d9dac851d1 X-MS-Exchange-CrossTenant-AuthSource: BN8PR10MB3220.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2022 21:20:15.3426 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: JkdSOMdn2HwjgxWPun1Izwwd/6Od9d9++KfeFAFS0XNOB6hnkqyzkFgo4QmUc41jKflAYEUih5zQ7x72e3W9pQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1584 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10231 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxlogscore=999 malwarescore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201180122 X-Proofpoint-GUID: 85KVOkd9Bk14rHi_-dnOzfjW2kI1mkV- X-Proofpoint-ORIG-GUID: 85KVOkd9Bk14rHi_-dnOzfjW2kI1mkV- X-Rspamd-Queue-Id: 3E1E6180006 X-Stat-Signature: 1ewxagwp4kqpkggmzci7fsjsjgahede4 Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=KpIzta3O; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=R7a9lxYL; dmarc=pass (policy=none) header.from=oracle.com; spf=none (imf06.hostedemail.com: domain of khalid.aziz@oracle.com has no SPF policy when checking 205.220.165.32) smtp.mailfrom=khalid.aziz@oracle.com X-Rspamd-Server: rspam02 X-HE-Tag: 1642540841-491740 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: Add a ram-based filesystem that contains the files created for each shared address range. This patch adds just the filesystem and creation of files. Page table entries for these shared ranges created by mshare syscall are still not shared. Signed-off-by: Khalid Aziz --- Documentation/filesystems/msharefs.rst | 19 +++ include/uapi/linux/magic.h | 1 + mm/mshare.c | 191 +++++++++++++++++++++++-- 3 files changed, 197 insertions(+), 14 deletions(-) create mode 100644 Documentation/filesystems/msharefs.rst diff --git a/Documentation/filesystems/msharefs.rst b/Documentation/filesystems/msharefs.rst new file mode 100644 index 000000000000..fd161f67045d --- /dev/null +++ b/Documentation/filesystems/msharefs.rst @@ -0,0 +1,19 @@ +.. SPDX-License-Identifier: GPL-2.0 + +===================================================== +msharefs - a filesystem to support shared page tables +===================================================== + +msharefs is a ram-based filesystem that allows multiple processes to +share page table entries for shared pages. + +msharefs is typically mounted like this:: + + mount -t msharefs none /sys/fs/mshare + +When a process calls mshare syscall with a name for the shared address +range, a file with the same name is created under msharefs with that +name. This file can be opened by another process, if permissions +allow, to query the addresses shared under this range. These files are +removed by mshare_unlink syscall and can not be deleted directly. +Hence these files are created as immutable files. diff --git a/include/uapi/linux/magic.h b/include/uapi/linux/magic.h index 35687dcb1a42..26a12e33a3c1 100644 --- a/include/uapi/linux/magic.h +++ b/include/uapi/linux/magic.h @@ -98,5 +98,6 @@ #define Z3FOLD_MAGIC 0x33 #define PPC_CMM_MAGIC 0xc7571590 #define SECRETMEM_MAGIC 0x5345434d /* "SECM" */ +#define MSHARE_MAGIC 0x4d534852 /* "MSHR" */ #endif /* __LINUX_MAGIC_H__ */ diff --git a/mm/mshare.c b/mm/mshare.c index c723f8369f06..e48d0f615f9f 100644 --- a/mm/mshare.c +++ b/mm/mshare.c @@ -10,20 +10,117 @@ * Khalid Aziz */ -#include #include +#include #include +#include +#include +#include +#include +#include -static const struct file_operations mshare_fops = { +static struct super_block *msharefs_sb; + +static const struct file_operations msharefs_file_operations = { + .open = simple_open, + .llseek = no_llseek, }; +static int +msharefs_d_hash(const struct dentry *dentry, struct qstr *qstr) +{ + unsigned long hash = init_name_hash(dentry); + const unsigned char *s = qstr->name; + unsigned int len = qstr->len; + + while (len--) + hash = partial_name_hash(*s++, hash); + qstr->hash = end_name_hash(hash); + return 0; +} + +static struct dentry +*msharefs_alloc_dentry(struct dentry *parent, const char *name) +{ + struct dentry *d; + struct qstr q; + int err; + + q.name = name; + q.len = strlen(name); + + err = msharefs_d_hash(parent, &q); + if (err) + return ERR_PTR(err); + + d = d_alloc(parent, &q); + if (d) + return d; + + return ERR_PTR(-ENOMEM); +} + +static struct inode +*msharefs_get_inode(struct super_block *sb, int mode) +{ + struct inode *inode = new_inode(sb); + + if (inode) { + inode->i_ino = get_next_ino(); + inode->i_mode = mode; + + /* + * msharefs are not meant to be manipulated from userspace. + * Reading from the file is the only allowed operation + */ + inode->i_flags = S_IMMUTABLE; + + inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); + inode->i_fop = &msharefs_file_operations; + inode->i_size = 0; + inode->i_uid = current_fsuid(); + inode->i_gid = current_fsgid(); + } + + return inode; +} + +static int +mshare_file_create(const char *name, unsigned long flags) +{ + struct inode *inode; + struct dentry *root, *dentry; + int err = 0; + + root = msharefs_sb->s_root; + + inode = msharefs_get_inode(msharefs_sb, S_IFREG | 0400); + if (IS_ERR(inode)) + return PTR_ERR(inode); + + dentry = msharefs_alloc_dentry(root, name); + if (IS_ERR(dentry)) { + err = PTR_ERR(dentry); + goto fail_inode; + } + + d_add(dentry, inode); + + return err; + +fail_inode: + iput(inode); + return err; +} + /* - * mshare syscall. Returns a file descriptor + * mshare syscall */ -SYSCALL_DEFINE5(mshare, const char *, name, unsigned long, addr, +SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, unsigned long, len, int, oflag, mode_t, mode) { - int fd; + char mshare_name[NAME_MAX]; + int err; /* * Address range being shared must be aligned to pgdir @@ -32,15 +129,14 @@ SYSCALL_DEFINE5(mshare, const char *, name, unsigned long, addr, if ((addr | len) & (PGDIR_SIZE - 1)) return -EINVAL; - /* - * Allocate a file descriptor to return - * - * TODO: This code ignores the object name completely. Add - * support for that - */ - fd = anon_inode_getfd("mshare", &mshare_fops, NULL, O_RDWR); + err = copy_from_user(mshare_name, name, NAME_MAX); + if (err) + goto err_out; - return fd; + err = mshare_file_create(mshare_name, oflag); + +err_out: + return err; } /* @@ -48,7 +144,8 @@ SYSCALL_DEFINE5(mshare, const char *, name, unsigned long, addr, */ SYSCALL_DEFINE1(mshare_unlink, const char *, name) { - int fd; + char mshare_name[NAME_MAX]; + int err; /* * Delete the named object @@ -56,5 +153,71 @@ SYSCALL_DEFINE1(mshare_unlink, const char *, name) * TODO: Mark mshare'd range for deletion * */ + err = copy_from_user(mshare_name, name, NAME_MAX); + if (err) + goto err_out; + return 0; + +err_out: + return err; +} + +static const struct dentry_operations msharefs_d_ops = { + .d_hash = msharefs_d_hash, +}; + +static int +msharefs_fill_super(struct super_block *sb, struct fs_context *fc) +{ + static const struct tree_descr empty_descr = {""}; + int err; + + sb->s_d_op = &msharefs_d_ops; + err = simple_fill_super(sb, MSHARE_MAGIC, &empty_descr); + if (err) + return err; + + msharefs_sb = sb; + return 0; +} + +static int +msharefs_get_tree(struct fs_context *fc) +{ + return get_tree_single(fc, msharefs_fill_super); +} + +static const struct fs_context_operations msharefs_context_ops = { + .get_tree = msharefs_get_tree, +}; + +static int +mshare_init_fs_context(struct fs_context *fc) +{ + fc->ops = &msharefs_context_ops; return 0; } + +static struct file_system_type mshare_fs = { + .name = "msharefs", + .init_fs_context = mshare_init_fs_context, + .kill_sb = kill_litter_super, +}; + +static int +mshare_init(void) +{ + int ret = 0; + + ret = sysfs_create_mount_point(fs_kobj, "mshare"); + if (ret) + return ret; + + ret = register_filesystem(&mshare_fs); + if (ret) + sysfs_remove_mount_point(fs_kobj, "mshare"); + + return ret; +} + +fs_initcall(mshare_init); From patchwork Tue Jan 18 21:19:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Aziz X-Patchwork-Id: 12716909 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 E3498C433FE for ; Tue, 18 Jan 2022 21:20:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 73F706B007B; Tue, 18 Jan 2022 16:20:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C8876B0075; Tue, 18 Jan 2022 16:20:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36EE36B007D; Tue, 18 Jan 2022 16:20:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0140.hostedemail.com [216.40.44.140]) by kanga.kvack.org (Postfix) with ESMTP id 141886B0075 for ; Tue, 18 Jan 2022 16:20:42 -0500 (EST) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id C7A08181CBC11 for ; Tue, 18 Jan 2022 21:20:41 +0000 (UTC) X-FDA: 79044677082.31.7020FE3 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf31.hostedemail.com (Postfix) with ESMTP id 72D1E20006 for ; Tue, 18 Jan 2022 21:20:40 +0000 (UTC) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20IKEaet013797; Tue, 18 Jan 2022 21:20:23 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 : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=3qKJw62UCoeOA2yhT2v7u2xg868SrCEtlCLACAoo4o0=; b=ho6RtR/RQGA5LKvhOpOY4C5ADc/ncKDzBFWL3RtFDDH14a91ix3VcOGnrJovI6B+rK3L QzjOQc+SerAlExoBxaF2cDWjpMlVxTack7toQqaDVhUcYd4xpAr0pdF8TBxCUWzL5i6w WmZxL49LFTrZaZSIqv64VL3VzL+GTLuUStHSZRTeLwjynQtY+sNoiBWvX31pnrYPSZun IFTLHP0eWU4ByMABj3hr82OMeczifilS6IrrLr7f7xa1i/1y9REfiQh3DBD4SpIw0H9+ Q62AUANesEnKONwjhWT3rp4NRbq64UfsEnHodSF8K/dUjJ7loFW4ADe4ldf5PdLM6ItA Ag== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc52u7fn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:23 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20ILK4mF062125; Tue, 18 Jan 2022 21:20:21 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2102.outbound.protection.outlook.com [104.47.70.102]) by userp3020.oracle.com with ESMTP id 3dkqqp4r97-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bqxzdShYsNIRhmuKJ/X6/+5aj3rSHIHLV19gyIs+6vdS2RneHHe+lgs2Rj4Yiw1ql+TdQp992ik3OhbPLJ/oFfux88SF+HAo0Py0roMo79XCeoUPNreFRfRNi6jrvk1rIT3SeSsDG4jMx7BriV/s93Z4BVLJsUXG4oBZ6UbKaoRucJw6moZNxakn/fAnpMdOkmCpt4QG/eQAt5bxn2/oDll4ald3K8+yfsVNLpIPWxOhSXDefclmIHtE77jcLSwXVw+8aG/1kjrrIvMD7SdrdFCvOdMIKjq4I9GYe84fWLRLoxd5aSZvDxyMTbjYK7nhf3zhpMB//ZePlsdI7t39xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3qKJw62UCoeOA2yhT2v7u2xg868SrCEtlCLACAoo4o0=; b=OKQZ0qHXZ0EPIwA8Crzl7kajYyFWDI/pcBdjDGYY1dN6mu+HLjJQrfotsK7ZA9ZInmrTc68FuP10LaKG5CzegbNZ8nSlk7lB9AbWETM0ABrWz/1/qJHwPYUf/yUBMTwnpdpC1mUBnhNi9syn823/Of3SGfrzfxrE9bZrnEFmTyWiia6vY8pqlci5YyZpf9HT+57ukLdhY7M5VITpT85fJSXhzPa3WgdITYdXwUjdTAWaIg8u7EUILvyp9Ejs4fLw1BPd0/DhyrgYKlmD4B/arFkWwS5RIU9741pJB6eary2qKDzWLHOISVBpnlW6JuQ53+GPzQ73+Kgk06OOKS144g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3qKJw62UCoeOA2yhT2v7u2xg868SrCEtlCLACAoo4o0=; b=vQVItWWhRxzAlFJ3k6dvmH5+/zlapLAcikGYNYVUsEmZ7/WDiSJvjmHZ2lmfCQsrqi7qJfPuAK5Y3qt68G2v7aCw9bYov6WzX2KeyZP2EEbM0Q0Vj8B3gmn6KXAbW9ft6MEKdA+75fy8J23cv7TgKcjMVRLEmK7vQ/UNkjfhDeI= Received: from BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) by MWHPR10MB1584.namprd10.prod.outlook.com (2603:10b6:300:29::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.12; Tue, 18 Jan 2022 21:20:18 +0000 Received: from BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2]) by BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2%6]) with mapi id 15.20.4888.014; Tue, 18 Jan 2022 21:20:18 +0000 From: Khalid Aziz To: akpm@linux-foundation.org, willy@infradead.org, longpeng2@huawei.com, arnd@arndb.de, dave.hansen@linux.intel.com, david@redhat.com, rppt@kernel.org, surenb@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Khalid Aziz Subject: [RFC PATCH 3/6] mm: Add read for msharefs Date: Tue, 18 Jan 2022 14:19:15 -0700 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-ClientProxiedBy: BYAPR07CA0001.namprd07.prod.outlook.com (2603:10b6:a02:bc::14) To BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0dd4aee3-7dd0-41cc-259c-08d9dac853d4 X-MS-TrafficTypeDiagnostic: MWHPR10MB1584:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:635; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HfzErbsfvk8wOxXm/VHlrZeaO9DlM8z2Du6YscL+WrR/zV8PeFQJaB4hpU0js+kUPvuuoVa4EqGxKlw4n6s4EkQyahtFwh8W5h6dyGBoTPy6ttPkeCz49JxfTw8/ulCcXtHwd5+oNcaPd5Cuvea4mvI7Gbp2sGCw+iulw7G1C2QX4DNBBbDoTwIQe4RfFpNzv5JeuqORAW/pbB96tLNGcmcoH4KQqnS2TxO+5Sr8/q5cJ37VpIuoveTvasUrIa1mojmG5+GhzorWu9/M8wA6B55twN/9VQz5NUSY0heDBU24ThTOcZ6If7hzVbIhKoTqo5CqivATDMFP03MAcZWUsQh/LYAht868O/OqGNYfn7vK+QRmSMHwbLhdkDMGBLC1Q2uk9ZGxdwpmgoH0y6yZdhwalYzn/Uso+/JQ74RqRIY8AvkgYZKGBER7CbnH///s3ChMKlcwR6XnXbSw6WqynHJ99AJM97dekzMQ+W04MeoruVxTTHAHp9cgu73MoKA1RqtH5yTqX6IhbrOV+GLWvBn2q7lzXl8RY03r33W3CUWdwe8iF0ftvNnLt2tsNfkVcaWJm0AJtkLmrg82reNn3Qo5kMQ6O5KNw1KhOscf4UWiNE6d0IGXCCvfoBxu9TbEgVpBBrdaE6kwtej4zkF/G8t8U+Aehs4JUIgyhlJXgmZ2cpxRVMRAX6Vb5QgoP6fpdf4JgQsekyfizWOsjDKzrw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN8PR10MB3220.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(6486002)(921005)(38100700002)(316002)(508600001)(52116002)(6506007)(66476007)(5660300002)(4326008)(86362001)(44832011)(107886003)(6666004)(66946007)(66556008)(36756003)(8676002)(8936002)(2616005)(6512007)(2906002)(186003)(83380400001)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: yos5/sDkxM2zmnEkDZYVmXyFqltYRN/rDJyoiIO6X4csV11H+jXj0FcaiDQczN4Vq8Cn02QFYqH8WdOCHnIw9YutnEktN4Ju7t4UiXM5DrndHxjT0Vhi34trgRUTN4zkZ3LuxeEr3zvNQXYcdwpBQKYeOV+YRi0mVt9xLuWBP+YZryP5rwt4Ofln8IbAPJG3AWy7VUnZIrXyR3znoOCh8nUXzpYZAaCu/8vv97+ETJ8IVxZokX8rBsPkTi0Av8/5XC1zn/pO2gSK9zBfVg6XopjGiBHDCYsmQSSPlHxcmQ/xAUUa61badcJU7YEnv23fhTMvbrQM2S+SJdXOzgpLpCOur3tX5B3GvIz1H3lhv4ufH56vskp7+y5PAKTHjfxHvW/H+v7XM+GhVpgP2u6PEutcEkdJPn+sRhxOXlpYwn50JHsBpzI6jGQE6nu8wffq4U/MbekdLSDdhyOOikeXKrlHG5pElb5buI7IhIbsea/5WrvVfRU3ig+KstoC3fdCmxGe2e2N1lbwHBiNBzcCjKNcUO0wq3AwNhg6DZ5yJ+ToiCSdgOANOLgOTbKZVSB5P9AEmTOCI8ikAMZRwtyCxi/UgxMQLqi1W4NB/FOIWFyQs+Imu9Mw/Z9NkPnDbYzhJoVulslvqtALUz4tTJUT4RvxmyNTYhNeEw1ytX99GeBi/yNns5eSmnSSSHt8TCmz91mA8Xzu8sCDDXG7OUlsyI4OHjBSyC82/G7RDO3uzHTTEY9BNwZ9m0i/lPLfi3hm/CEHKTuHpGIh1ht2HQ0Zy2AfXyBH6Sk4wdBzjGrWYuY21R+uNc89+lWzBnXjVLWp/dm7BARUDIsOVYcRK8u6qkjYp39JTxRvnuE4TSAvinFocXKBwYrhnK926FVsx0M4PepL/sMGqcFb8HWjK/HemI22kQJ39pKTflw97DMuPA9LhxUCwNvx99cMC8+2NDg/IDH3b2U+Ziw3ki3zdIO9l0bq4uPoyp1EIBVfnVXuhwI2xom29+GJuPsp/PIUSp3O2t2PgFRIWscToYbABVJG/QbetGXzKpW8raONV4Uy7nAz8IncuYr0yA1OLb7sDP5KB7g+73mRQpyJZt1cfBijjXLPwgIH4r2GfXQ7IDVC4OiPovDzdV7kTpD5SdRMqJwRc5jsozOLkX0DqE3i4sVE1HmrTudEMfvm96JA5AfL+fczG8jU2qBNVCJIyq5vy5feSZATBE9w5UAuEn9/yIV+BS903t4BG/dAiFqJwu1Kg1+VCY9A4KZ9T2Pw1E0NssKyRW9scrnXi+hIs6slWLeVBNdgfQNgzUt26arCcQ9ygUNSoSzsBJl1acr+61cxeogKZhygukJObbftTltPYL7MfdoaOVPtlfQJc/m6Au5EG5tidk03+fKa0XpVjNdV9RpMJAwG+0E2Fp4XJ9M/B3jwpPTXH5yevHOl8v4Ew2UoGCclWZVudwuK1aBi2q5M7yDM8E0QGdDkgDwOFTJVAe5sWctYlQBrXfJJPZsrzkkImZZmrGlE7otpvyMCo/IXaGBlcrayN0KuZg483Ub3KT4AprzsJpYwFBq1b3tc0IY7VGG16ijO/jZ57XpvM/NT9mxNqhmkUfGpCDipemQgnsiYDbP2pkm0V8r3vesfA6S3Y79yT9HcEWA1Jbdr58FIMc+7Q/9GHKUKr4TuHhsgv+y1wA== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0dd4aee3-7dd0-41cc-259c-08d9dac853d4 X-MS-Exchange-CrossTenant-AuthSource: BN8PR10MB3220.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2022 21:20:18.6706 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zkdS25XRC9WMch4aTHA6sJkKcolgJC0SZj3rgMECpAAtPvaEmzNlcKV8F8uf2ijuWwjVjQFGPU5HAHx2/J4gWg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1584 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10231 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxlogscore=999 malwarescore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201180122 X-Proofpoint-GUID: teFXwto_L5cMzRY8bpAjmfE7HuJPoMZ4 X-Proofpoint-ORIG-GUID: teFXwto_L5cMzRY8bpAjmfE7HuJPoMZ4 Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b="ho6RtR/R"; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=vQVItWWh; dmarc=pass (policy=none) header.from=oracle.com; spf=none (imf31.hostedemail.com: domain of khalid.aziz@oracle.com has no SPF policy when checking 205.220.165.32) smtp.mailfrom=khalid.aziz@oracle.com X-Stat-Signature: w7zn6u9ihfk6gfazni3bsuprjep9jjxw X-Rspamd-Queue-Id: 72D1E20006 X-Rspamd-Server: rspam12 X-HE-Tag: 1642540840-8293 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: Allocate a new mm to store shared page table. Add a read operation to file created by mshare syscall to return the starting address and size of region shared by the corresponding range. Signed-off-by: Khalid Aziz --- mm/mshare.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/mm/mshare.c b/mm/mshare.c index e48d0f615f9f..d998b23c25ab 100644 --- a/mm/mshare.c +++ b/mm/mshare.c @@ -16,14 +16,37 @@ #include #include #include +#include #include #include +struct mshare_data { + struct mm_struct *mm; + refcount_t refcnt; +}; + static struct super_block *msharefs_sb; +static ssize_t +mshare_read(struct kiocb *iocb, struct iov_iter *iov) +{ + struct mshare_data *info = iocb->ki_filp->private_data; + struct mm_struct *mm = info->mm; + size_t ret; + unsigned long tmp[2]; + + tmp[0] = mm->mmap_base; + tmp[1] = mm->task_size - mm->mmap_base; + ret = copy_to_iter(&tmp, sizeof(tmp), iov); + if (!ret) + return -EFAULT; + return ret; +} + static const struct file_operations msharefs_file_operations = { - .open = simple_open, - .llseek = no_llseek, + .open = simple_open, + .read_iter = mshare_read, + .llseek = no_llseek, }; static int @@ -77,7 +100,12 @@ static struct inode inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); inode->i_fop = &msharefs_file_operations; - inode->i_size = 0; + + /* + * A read from this file will return two unsigned long + */ + inode->i_size = 2 * sizeof(unsigned long); + inode->i_uid = current_fsuid(); inode->i_gid = current_fsgid(); } @@ -86,7 +114,8 @@ static struct inode } static int -mshare_file_create(const char *name, unsigned long flags) +mshare_file_create(const char *name, unsigned long flags, + struct mshare_data *info) { struct inode *inode; struct dentry *root, *dentry; @@ -98,6 +127,8 @@ mshare_file_create(const char *name, unsigned long flags) if (IS_ERR(inode)) return PTR_ERR(inode); + inode->i_private = info; + dentry = msharefs_alloc_dentry(root, name); if (IS_ERR(dentry)) { err = PTR_ERR(dentry); @@ -120,6 +151,8 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, unsigned long, len, int, oflag, mode_t, mode) { char mshare_name[NAME_MAX]; + struct mshare_data *info; + struct mm_struct *mm; int err; /* @@ -133,8 +166,31 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, if (err) goto err_out; - err = mshare_file_create(mshare_name, oflag); + mm = mm_alloc(); + if (!mm) + return -ENOMEM; + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) { + err = -ENOMEM; + goto err_relmm; + } + mm->mmap_base = addr; + mm->task_size = addr + len; + if (!mm->task_size) + mm->task_size--; + info->mm = mm; + refcount_set(&info->refcnt, 1); + + err = mshare_file_create(mshare_name, oflag, info); + if (err) + goto err_relinfo; + + return 0; +err_relinfo: + kfree(info); +err_relmm: + mmput(mm); err_out: return err; } From patchwork Tue Jan 18 21:19:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Aziz X-Patchwork-Id: 12716907 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 5E5D1C43217 for ; Tue, 18 Jan 2022 21:20:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2068A6B0073; Tue, 18 Jan 2022 16:20:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F06696B007D; Tue, 18 Jan 2022 16:20:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BAB666B0075; Tue, 18 Jan 2022 16:20:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0249.hostedemail.com [216.40.44.249]) by kanga.kvack.org (Postfix) with ESMTP id 7FDCD6B0074 for ; Tue, 18 Jan 2022 16:20:40 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 35B418249980 for ; Tue, 18 Jan 2022 21:20:40 +0000 (UTC) X-FDA: 79044677040.17.0E3D0D6 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf15.hostedemail.com (Postfix) with ESMTP id A34E9A0003 for ; Tue, 18 Jan 2022 21:20:39 +0000 (UTC) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20IKEEwH021250; Tue, 18 Jan 2022 21:20:26 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 : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=KVZEqbez+HEjEszY7yPbZ6Pivn8loewptq4mxjOMA5c=; b=f9taApvtqrULiTxat6+6us5QEIZgzkUre2iOct43IUCnXImMZc20HUp2Cu2Z10nX2vj4 pU2oRNQxaJJQ5EEYiR74N4Lue550rKbTC4ZrahDKYyW+HfCtb1iNyOd+Qf084cGpEPbX xdinPIbMa1QXT7pEShgsoeFGGCGo6SeizhP5o6R6oKknCzuspgVLID5AU2hWEw1kx9p5 59OP/AgHYMOdTueY28oZHJIjF8jnNTfEE7HNm0qH/IpTGb/NUGoMgYSxR2z/m2mgrTSw x052xW9fHw99owOF/lzHLlARSmOblfIh3UIaAl+ygWA9eCnC9BYD1iBt4TyO+h+TOKab Rg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc4q39j9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:26 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20ILK4K8062049; Tue, 18 Jan 2022 21:20:25 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by userp3020.oracle.com with ESMTP id 3dkqqp4rby-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k33n3C8Bj5Z1SgFo/yxIIAD0BPnvzIchISkHJZs2wT6X9b7yG6Bcm9u3Xb1aRQuBUj2TC4xsiJvU8DWWtr46Mi3TJ3Ru9/wBkJbAFmhlVrKsXtcjNrZhRMaxZYotULVRTJGtFuEwGrZ76UAGCF/HApX5SSjPWL1QsCps6nDonbJhUJ8Gav+I3gzIlgetPOQ8gSkhL5eBguWRh99aEc/TYZGhOkfa5O1VwnEQbAdPuV8/Y5ZLqfn9apI+/EVOI0xQ4ec6EM400ftDtN72ih6MnvjW3L7lAAV80zR/1S4zwkJJEhTNCAEwxWRqNp0bQmzop4n8Jcy8OHNQqF8Bv55mIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=KVZEqbez+HEjEszY7yPbZ6Pivn8loewptq4mxjOMA5c=; b=jR1EE1ym3B11s4zcic8Y9iBaE2HnLV1ZW42VW0MfdBF/0qxN2gwvC9NaH7cOrxjnHvCSy5FxWCjuEWEaXtlwFgPIb/kBtfefKZfAy6v2PlawncnjoG3aBzLqqYsPXxtZf0kEPNDG0nnL6IwPR9VWPxFQCLWazgiAJ9MJg/q8vAUBIPdsIvESHxk9hozdAezHRDJAFZo9jsExHevmo0o9RyNoilYnYHSS6OorLgBoLi+p5vzz9XcqeemSbJic+wrg14b6O7M3IdykX6hITIG5Uj7/iht3GJ2/6v3mhPS3qzhBvpMHSQ56gi5mn0PfaAREutE/ilUKC2LnfgblZpL50Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KVZEqbez+HEjEszY7yPbZ6Pivn8loewptq4mxjOMA5c=; b=oD8Z0scvz9TfQo8RuY36hXMcK9geAqu/OT70vx5/uZNDDSw94QTHmHutofMgjgH2J/yiZJwd5iP9WbLD+Vd1G2YECLFzFf5x5Q6p0bLVS/HsHRnAOVpeqo3X8slgYFZHvucFYojJSAr6oYR1SjxhShBLXmPCGlDRLxTD1lBJ0fg= Received: from BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) by MWHPR10MB1584.namprd10.prod.outlook.com (2603:10b6:300:29::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.12; Tue, 18 Jan 2022 21:20:22 +0000 Received: from BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2]) by BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2%6]) with mapi id 15.20.4888.014; Tue, 18 Jan 2022 21:20:22 +0000 From: Khalid Aziz To: akpm@linux-foundation.org, willy@infradead.org, longpeng2@huawei.com, arnd@arndb.de, dave.hansen@linux.intel.com, david@redhat.com, rppt@kernel.org, surenb@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Khalid Aziz Subject: [RFC PATCH 4/6] mm: implement mshare_unlink syscall Date: Tue, 18 Jan 2022 14:19:16 -0700 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-ClientProxiedBy: BYAPR07CA0001.namprd07.prod.outlook.com (2603:10b6:a02:bc::14) To BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6d2fd1fa-2da4-436e-cb7e-08d9dac855c8 X-MS-TrafficTypeDiagnostic: MWHPR10MB1584:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:283; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wJxuz+azbtEdDRu9v10f6Qmmj0BKpVzgKDZ+yoichqwAwZP1JnYSdVP9yRjtojnKJSgzT30tbTphKUQJPFaRLOfIfOCjrVzvT8VLlM7KJu8/go3wS5o9pVR3wSziM+3Sx+Yi34SHO9ieS3Wiuh4xXojsHgcP368kdjXS2YjXovhKKdZqx5hjOjd9annjOPQSRvyyx86jfS6VgAFeb7Fk12dB/DGd+1E1RJoSTd7Z2kjeaBvoJz52i/i8N1MbdUHisKICc36YDBCEqrMqCxbXjMPvD+WvcjIGJwzIbpbldYxvX66WLs59mcDns7BB0VTVZ5kdHpBJMxFZocHRHdytfAXb/ECzMiUGMeY9N3KoJUC7Z5lZQpm6iE88v7MzbeMfvIwm6oSB1rdqOT/f4+sEVrO/oaGGvcM/htInrmCTeCOmbEExrz91AoCiR5dON2OdHpMbWZuT6BE7EBd3dN8FrnRFWepOPoRUmtlDbdezbz0/TjjA4VCt1OhaP3E8a8qQ1yxCllRPB94tpbEvr0JjBp7awW63xdXqB1fRuv9ETq3Q7m9ZIR65ICAE892qYcN9/nzF4RFGYXkAf1uPGt7LyrkCF4BJEGd5VT5u5wPmaN+TwmQevmzu2Yj32LWhvgoJo2ydTEOtq/fnZNMHVFtMw1T1rxfByuW7gChj4hfCtiLBaIgRPiTa/FgP4RPZxArGlKh0MBg0+1jge19du4GXgQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN8PR10MB3220.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(6486002)(921005)(38100700002)(316002)(508600001)(52116002)(6506007)(66476007)(5660300002)(4326008)(86362001)(44832011)(107886003)(6666004)(66946007)(66556008)(36756003)(8676002)(8936002)(2616005)(6512007)(2906002)(186003)(83380400001)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: nE9cTBCndAt2hooJjK0lP6+5jV12DwPPnLkISIYB31y5jYUSD8h4g033LKspLrdYbx6+wVLgNqOQAWFH2M3pGTqS+L4ohhIjWgUll/HsXRvGUPHM/i8gPeHP9Tv+s9x7tv6eAK58dxg1rky1EpwNdxYQNnPfl0xjXcT+IviJmFqidWxcoCAej6GmVlDoQswluumowDkFb/AXKVnW5UWgOwpyaNCTfiIeX2bata8ufGeMuULPMXlnk9MC3LozC9ZrM098DRThNxCTK17vfIJO/UPwO8n7JFQOkD6k7cvY8IJbFbVkO29HJSF3u8W4rPMlWQ7z9KTxrAkuwoY/d0qsY/mZoqZsUXsm9yN0BUdEwDSeiX5BNWuHexF2IlashVURp2cwaszmA6xxLAWDbMHvNKozXq/BOQtUmDwhiEtq6B1WoxO6k3w5w/Lzhg3ToiTpg781Wi0eUJ3dt4Jyf6V/PY9aAOivLgZTwdVUfGRum99xKjxTso2y66zm/PhmN+V1JtQ5c6hj8d69Z/aM37ZfSXEHv2Jba5HYu/Iv4F9oLuf04O8jePi3J8ur6gYQh7hc4rci7PD2Qm7NCLlSllTE/5cIaaREzVC8I9IYToYiHJ+H3IUeMcmJZ20pl/P8FTISs70lvXlghhs5CJ2iHaBZiGDlx7jlCgTcPrTlEBRxe8c77NRQFJ3D06vj/SiyGHcAO5Tt7Zm8cqXlOUqsvKpBWchAw/zPVZokjoS39OSALUsWNv2Do3xP6RF4TwvQGN/tUW3y9ahNXYSvIhSoTNRi6G8NXImBdaEcXJHi/qsEUGDsgum6shMOg9PRtWn/Ya9SxAXwQctQRx1AcOrtyJu61uillAk5xEvw+GEqUumKe6KzvzT5S2VFdbxatsrJ/J6ZuO4Q2dRLYtNU9DuEQ89q0HzxjbXAaNYJcATGJ/C3WTApWn0X2qjUXBrL6kLcDWM9h7EID6SVr57Mql1OChbJqy44KKnCLT8BBm+D3kykmVsUXLmVy6v4i3966bx2JCt2oBHFmrEGFxrG0KunZKG2B+bxO54Z1zYC496/KIz+rrpUHY0hzQ3A11A3KD7qWNasrOuILh2H9Aw/ZRhouEkypQhn65ezyaAJG0BRNiTMLsUGcrwLuS4YLEvmITqenaQ66CEkrvVmZ/7O3DfTWdZeBKG/ayBRxJ4dTAsjNMUQOiBzetYfps6uQd8mtniRlBWJjgeyTHyvPNxn0PkzUCRhQOseewslvci4bAx8HtPqsuGqYmAS1nHgUymtXfUtZ9Ivmc+ME7ZJCtb8fh4c/+tmuNrJSDZpoWAPmNsyAOfWsYvOdaIqwRQupegtQD1ooZKPt6VQrvDgVMDCQg3muqUyR9dYMtRi49Pq3hd3JD2HumBMRP1K3LwmyXKp5r3CGxqNw1hytPEwNCxHoGq6eHl/ucjWRFTH013cZaubh8D6fERXZnIgc84IE0cqZoqltyz+8oJMGom8phh1H9/w50rpgx2wJpA+DHFtNHa6KiBjuAV5FkWYAdoj9xUVD0wQotDe52J0X1R09WYxi8OgNIYG4xgPZOTWFoPtbtqion/NX/CCPV7rAeRV9abT7frWJi5OBqJtcui1Iid3ZQv2/41mMgls9FLEn5iAceUQf3PamGXkkIMSuCa+BSR86YXgWh9saOhr3+I+kUfi5T5dpl5rbQ== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6d2fd1fa-2da4-436e-cb7e-08d9dac855c8 X-MS-Exchange-CrossTenant-AuthSource: BN8PR10MB3220.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2022 21:20:21.9672 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 0euU8GEyNbElicuqH1JmFIbVUHJvmDJMD95EPIXeQ4Od1CBFJPigyui4Jm0KM5b0CgamY7gE3AaeHdk/IknDHw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1584 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10231 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxlogscore=871 malwarescore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201180122 X-Proofpoint-GUID: LnaIVF5X3D6sODwJy0W1tHcw0R9Kor0H X-Proofpoint-ORIG-GUID: LnaIVF5X3D6sODwJy0W1tHcw0R9Kor0H X-Rspamd-Queue-Id: A34E9A0003 X-Stat-Signature: xcpbc9umpgzc3akz8iwp7a5649gaii58 Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=f9taApvt; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=oD8Z0scv; dmarc=pass (policy=none) header.from=oracle.com; spf=none (imf15.hostedemail.com: domain of khalid.aziz@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=khalid.aziz@oracle.com X-Rspamd-Server: rspam03 X-HE-Tag: 1642540839-370571 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: Add code to allow mshare syscall to be made for an exisitng mshare'd region. Complete the implementation for mshare_unlink syscall. Make reading mshare resource name from userspace safer. Fix code to allow msharefs to be unmounted cleanly. Signed-off-by: Khalid Aziz --- mm/mshare.c | 144 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 113 insertions(+), 31 deletions(-) diff --git a/mm/mshare.c b/mm/mshare.c index d998b23c25ab..8273136363cc 100644 --- a/mm/mshare.c +++ b/mm/mshare.c @@ -22,11 +22,24 @@ struct mshare_data { struct mm_struct *mm; + mode_t mode; refcount_t refcnt; }; static struct super_block *msharefs_sb; +static void +msharefs_evict_inode(struct inode *inode) +{ + clear_inode(inode); +} + +static const struct super_operations msharefs_ops = { + .statfs = simple_statfs, + .drop_inode = generic_delete_inode, + .evict_inode = msharefs_evict_inode, +}; + static ssize_t mshare_read(struct kiocb *iocb, struct iov_iter *iov) { @@ -114,7 +127,7 @@ static struct inode } static int -mshare_file_create(const char *name, unsigned long flags, +mshare_file_create(struct filename *fname, int flags, struct mshare_data *info) { struct inode *inode; @@ -123,13 +136,16 @@ mshare_file_create(const char *name, unsigned long flags, root = msharefs_sb->s_root; + /* + * This is a read only file. + */ inode = msharefs_get_inode(msharefs_sb, S_IFREG | 0400); if (IS_ERR(inode)) return PTR_ERR(inode); inode->i_private = info; - dentry = msharefs_alloc_dentry(root, name); + dentry = msharefs_alloc_dentry(root, fname->name); if (IS_ERR(dentry)) { err = PTR_ERR(dentry); goto fail_inode; @@ -137,6 +153,7 @@ mshare_file_create(const char *name, unsigned long flags, d_add(dentry, inode); + dput(dentry); return err; fail_inode: @@ -150,10 +167,13 @@ mshare_file_create(const char *name, unsigned long flags, SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, unsigned long, len, int, oflag, mode_t, mode) { - char mshare_name[NAME_MAX]; struct mshare_data *info; struct mm_struct *mm; - int err; + struct filename *fname = getname(name); + struct dentry *dentry; + struct inode *inode; + struct qstr namestr; + int err = PTR_ERR(fname); /* * Address range being shared must be aligned to pgdir @@ -162,29 +182,56 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, if ((addr | len) & (PGDIR_SIZE - 1)) return -EINVAL; - err = copy_from_user(mshare_name, name, NAME_MAX); + if (IS_ERR(fname)) + goto err_out; + + /* + * Does this mshare entry exist already? If it does, calling + * mshare with O_EXCL|O_CREAT is an error + */ + namestr.name = fname->name; + namestr.len = strlen(fname->name); + err = msharefs_d_hash(msharefs_sb->s_root, &namestr); if (err) goto err_out; + dentry = d_lookup(msharefs_sb->s_root, &namestr); + if (dentry && (oflag & (O_EXCL|O_CREAT))) { + err = -EEXIST; + dput(dentry); + goto err_out; + } - mm = mm_alloc(); - if (!mm) - return -ENOMEM; - info = kzalloc(sizeof(*info), GFP_KERNEL); - if (!info) { - err = -ENOMEM; - goto err_relmm; + if (dentry) { + inode = d_inode(dentry); + if (inode == NULL) { + err = -EINVAL; + goto err_out; + } + info = inode->i_private; + refcount_inc(&info->refcnt); + dput(dentry); + } else { + mm = mm_alloc(); + if (!mm) + return -ENOMEM; + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) { + err = -ENOMEM; + goto err_relmm; + } + mm->mmap_base = addr; + mm->task_size = addr + len; + if (!mm->task_size) + mm->task_size--; + info->mm = mm; + info->mode = mode; + refcount_set(&info->refcnt, 1); + err = mshare_file_create(fname, oflag, info); + if (err) + goto err_relinfo; } - mm->mmap_base = addr; - mm->task_size = addr + len; - if (!mm->task_size) - mm->task_size--; - info->mm = mm; - refcount_set(&info->refcnt, 1); - - err = mshare_file_create(mshare_name, oflag, info); - if (err) - goto err_relinfo; + putname(fname); return 0; err_relinfo: @@ -192,6 +239,7 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, err_relmm: mmput(mm); err_out: + putname(fname); return err; } @@ -200,21 +248,54 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, */ SYSCALL_DEFINE1(mshare_unlink, const char *, name) { - char mshare_name[NAME_MAX]; - int err; + struct filename *fname = getname(name); + int err = PTR_ERR(fname); + struct dentry *dentry; + struct inode *inode; + struct mshare_data *info; + struct qstr namestr; - /* - * Delete the named object - * - * TODO: Mark mshare'd range for deletion - * - */ - err = copy_from_user(mshare_name, name, NAME_MAX); + if (IS_ERR(fname)) + goto err_out; + + namestr.name = fname->name; + namestr.len = strlen(fname->name); + err = msharefs_d_hash(msharefs_sb->s_root, &namestr); if (err) goto err_out; + dentry = d_lookup(msharefs_sb->s_root, &namestr); + if (dentry == NULL) { + err = -EINVAL; + goto err_out; + } + + inode = d_inode(dentry); + if (inode == NULL) { + err = -EINVAL; + goto err_dput; + } + info = inode->i_private; + + /* + * Is this the last reference? + */ + if (refcount_dec_and_test(&info->refcnt)) { + simple_unlink(d_inode(msharefs_sb->s_root), dentry); + d_drop(dentry); + d_delete(dentry); + mmput(info->mm); + kfree(info); + } else { + dput(dentry); + } + + putname(fname); return 0; +err_dput: + dput(dentry); err_out: + putname(fname); return err; } @@ -228,6 +309,7 @@ msharefs_fill_super(struct super_block *sb, struct fs_context *fc) static const struct tree_descr empty_descr = {""}; int err; + sb->s_op = &msharefs_ops; sb->s_d_op = &msharefs_d_ops; err = simple_fill_super(sb, MSHARE_MAGIC, &empty_descr); if (err) From patchwork Tue Jan 18 21:19:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Aziz X-Patchwork-Id: 12716908 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 70EA2C433F5 for ; Tue, 18 Jan 2022 21:20:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54C7F6B0074; Tue, 18 Jan 2022 16:20:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1BDA16B007B; Tue, 18 Jan 2022 16:20:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6E486B0074; Tue, 18 Jan 2022 16:20:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0245.hostedemail.com [216.40.44.245]) by kanga.kvack.org (Postfix) with ESMTP id 8D0E96B0078 for ; Tue, 18 Jan 2022 16:20:40 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 43B29181C49D9 for ; Tue, 18 Jan 2022 21:20:40 +0000 (UTC) X-FDA: 79044677040.11.00DC79D Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf26.hostedemail.com (Postfix) with ESMTP id C3D26140002 for ; Tue, 18 Jan 2022 21:20:39 +0000 (UTC) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20IKEDj5021239; Tue, 18 Jan 2022 21:20:29 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 : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=Yrq2zSkIAuRB6yAhS9WJ7UnsVTZZ/949oV+gt52hZ6Q=; b=l450seAnpXgSCNfscMSSuo89ojpLVkZjaStBeylW9ATha05TOdWavWowBZLcbd9aiYGu 94elNMkTQktdmAsx2z/kKkPw65pRFqggwpu5Q7RH9pJyZGWAsvH3Cupan0LHcpLoACeB fz5HOPv8jSMfd30bt1RfXkeVyD69/a26eKDZiULkGgv0FMEME2WjBxKBjniIrN8VUzfp DQPQnZQOLIXKlSwUDUx++DlILO9NA0LKUP8G7H0pihx4CtgR5biH8fUOppkm4hzglE74 MawJVb0Tm8yim+2hEo6gNTKBHaimkboM25RKzz9RKfT7+wgJYPNfcwEM3bowt2X+aUQE 6A== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc4q39je-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:29 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20ILKNHI147767; Tue, 18 Jan 2022 21:20:28 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam08lp2041.outbound.protection.outlook.com [104.47.74.41]) by aserp3020.oracle.com with ESMTP id 3dkp34uype-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:28 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jzW5GDZ4GxgnVWBIfnzcRO801YYHZy0+I31QLWpSN74JMWVuJyStJ8kEwAB9meRit2lU3BBU30u4V8QpR7bgfWUBllhrQ90b3JCEunbnYgFlwL45bb/1ZgQgAVD2R3iGPFc0Ms+XUhSA5TdNskJ6bCNfQzHPv23kbA2vulzyYTfCQ70YHIErNmGbraxgBzJ0I9WgfhCpu28xJ5/5XhOp04i2n4ZrxFjI3KvyC2VUXKEu89fnYpG2tH0h1qV3dGfMZeufqj5lpa9c8653/Yfq5RezYhVadvPVtwm0ESeoeO/rLnNzZ4jd1srXziB5Iqbhpeijpi79P9xNIx00hZUuPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Yrq2zSkIAuRB6yAhS9WJ7UnsVTZZ/949oV+gt52hZ6Q=; b=dtrbCav/pXFjti3C8ICHCPffCvnHtf3eR+vK3eUbq2lREo4tGV/aMDjVALCnkR50ta5yYqjVCBKQB/o7gIF45+PX/Y+AlS4yPTBoW0cHxAqXv/NMrrCfE60DxAq0od6nDI580gB4WoPNrhqBdytBm3JM5cuDVPScNT1dmMFyvHi2eeemxtZPFhJoOCEYt15QsxNVg5GFYNpcux+TYEkKHEtxkgCw4lr5KsfbcOKukH+oaJXg0BtYCyqmYcGJzHlWpgcQNmrT56nsogqpUZaU8PQi8RSHHlXmbukHLW3hf11n/+R3nzZ/4VrqG/bhTf0DIFFkwYz7yjU7biLorNCFrg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Yrq2zSkIAuRB6yAhS9WJ7UnsVTZZ/949oV+gt52hZ6Q=; b=iH38Z2xmZHrB0Ms0daPRYcrLgvs4FHt5PTzpmWWAjRxxws//tiou2HiIgdYKvPkGNhFNSv8myk43Pji5kTcg0qPFWyn3zquiwn4/kN0I7rIkAJ7WWS4rlfuPnVJq12HWwIW0ObmzS3eKM5HEbvCXJuUjzOzQe7zOHlhHQKg5Skc= Received: from BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) by BYAPR10MB3029.namprd10.prod.outlook.com (2603:10b6:a03:8d::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.9; Tue, 18 Jan 2022 21:20:25 +0000 Received: from BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2]) by BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2%6]) with mapi id 15.20.4888.014; Tue, 18 Jan 2022 21:20:25 +0000 From: Khalid Aziz To: akpm@linux-foundation.org, willy@infradead.org, longpeng2@huawei.com, arnd@arndb.de, dave.hansen@linux.intel.com, david@redhat.com, rppt@kernel.org, surenb@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Khalid Aziz Subject: [RFC PATCH 5/6] mm: Add locking to msharefs syscalls Date: Tue, 18 Jan 2022 14:19:17 -0700 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-ClientProxiedBy: BYAPR07CA0001.namprd07.prod.outlook.com (2603:10b6:a02:bc::14) To BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2c25db69-5afa-44d0-85b9-08d9dac857b1 X-MS-TrafficTypeDiagnostic: BYAPR10MB3029:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:446; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JJPX24zegSP4oeoof77n9az6BwlFna1yV6B45AD8u3nPKDrik84yrW3ZMHp1VmObUBzJDOg1h1Km5bseVsd0m/8ZvWocoJpE9PWbVj0VUqfgfDUrCPo1PrkAE1ZVXyseF+4kNQQlwCZ3Hsa/ao9qvLvj01HttpqWmJpvI5JomcV2LJJdzSdBcjJa0lL69dyEnT6+8xiIqO8hNXNrW7Fs7ZVddX4d+u6pnCmpx5Wa965JFteAzr0e8WRkSoYw0qi5vikraA1N0bzN6Sdx7vc4A2njo57LMaI0A8/6kmIJPns5p5lmuBP/V2JR0DozVUAXX+k/ovbX50KcPW1GD9HOYTLiZNwVoanhQsl2ht28U0qhiY4ANiN/Dkx5vk21G0wKOdoWmgEJ4uXlI8fo0l51q2pYuslW5A9kdJRgWH7LlRLBc/yg1VlwUpiMDHnFGGNQrnnCDOxQZJpv58IFe73Nc1+pxbf73RresJJRLaWKS+HXR4LC47rO3/O8C2NN31sW+dUUBh2dCiGsyOsqvfZLG2FkbLApJEXu1YVDFDI9CbFlqdKavkHyV99rjZWCCDhwWq2cPYZGrXD1J3uF8NF3z7Yw4adpDrgKdtqqI0V9W31dWXahAicI1gp3YgE1czKdsWjAG5wqVBi7D00KRiiR5JuwGYZ6zZMHVAqFcHp8nXkJ4Y+KAwfbJk1uwI5ZESwU X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN8PR10MB3220.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(36756003)(38100700002)(316002)(921005)(6512007)(186003)(6506007)(44832011)(83380400001)(6486002)(8936002)(6666004)(2906002)(66946007)(2616005)(52116002)(4326008)(7416002)(86362001)(66476007)(8676002)(508600001)(66556008)(107886003)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ybbSL+jdCKpStF1oEf8c57rjlsws07Y1/aVu8j5tKhkgyW4vY0WKj2myBynEOcZv4QVlVxlN5uwg+Y63M6QYhE78eooxc//6zqVf7piHQMOrCvU6LuIX6n+EmPrYjMic7vVgxqsNaN7tY3/WZgaFdMqXSa9hRLMbVWl0+2bqjvk0F0IjAGfoPK9gV6awi2U+WJhj8yhy8/RkELChL9Z1NsIGdgkPEbKQ1Kr0/zwgx+CybkKf+b2b0qYKMfqchaITv8vKfnMPdLBkvTQWAGGM5JSBqgnhRAG3FKVyumoJMR4EL0cAdR10/aH+9M+RZOjs2PUXWysbOLy4MjIAmpMY9o50okXhVqAYOh6iNfy8kLWNFo8mdJ4mQ8bXImqI6KdO529arMH2mGo7PP8lCE8qbw3cKRRCcU+aU55GnPmznAJt4A/vBxfBRqMPcH2kfvHMd+WasZAvy9Px+Vb2HrSyqeH2loTfsiAi+2WdQrXTDKo3ggtKR7Tkvaz4JovVjdYc9Z6R6vqg1DSRy47aOUoxyAsGoFtlYzxu7uQm1Y95SMy57QV7/Rvc/uyg8IrQo2N+Q0gJ7QzMG2TnP9YnSXT3Q+ca6QUFFwDunF0tA8l+bV8K05qjhvG6fs5+41ItRWDaPWLIFFau32hDzbMNS2pfnFhARZHZJFBQH0nzMk95ma9Vy9296vRidMRQgeO/EXuWq99Sctm2l4qskvh8Cw/hThimxNmW/7uKtdoS9QBJeordrY7DuTOZEY+JvTW5K7CW8uMZiZaewXN4+Kvj+N7Vop43KyZdb7FxmRxNyKGUiuTuvSoUtZpmsiMRGFK5ZbmV6fyZd8PutqlBBI4tk/PUUYl5jsHjQd2cbBThFQaPWGJKH0vDxBpKcxne+YT5bNF9XGegUaBM8OqOzWM72EzJWJg+gXkgQp1yGmxNP9+PLK03ypeJ6p6NqlmrHL6jUESFbOXHYHm8tuQKR+ryLhGMkebwL2ROXlwrU1kL0tqmZPmIkVk5h7CJNbXe/loSkpcogneJo0gYA1YxBAGtWFWFFf6RSjoC070A5e57nJmCGkv2lZP+PkToIgj9iHU4iCfP9F5wtim3/g2W5Be97K9NnI9iTHftS6DCZSEJU4B9Cocj2Dr5HWLfCkg73UqhZdFkU9Z5nteuwRRLYK/nFxcQQF/2yF2cc1b+IbthlQcm9xip3Qh+atyiOhigJPerc3p8e7LSycbySEUHdak/kwgbyevFfVt/I6B5KrAZKi8MrjlxgntD/JeRm9QvaiX/1nV3R8SlnhiQR2O3D4a1+JWaXieLZqKBwDgFDFeoLFlNVT2Fk/+l9pSYvfCKGi6UChIWKb/y4b4HVTL7r/vIelDf/kPrnsDLSwLSl02ZNxRtQLRSQGwrOsYj54FzN3n3ew+7C1NWAjgBl5f8uqVuY7M1XfqXmEiRwMP5bNIJ9wC4ma9nTDvV1AfH9u4sdupqz+ElRGFYe/ny8tX/w6QzyMxo0S5pAZKaFHQqCHHt2mX7O6VD7pJt7AUTzZ2f0AGRWzLNXvHwkFZm5mccCzaau/BmYOxOOejhwKe+gEhAcf3nzzEvuRbcpWjQrFi5QHQSUfGvlSVyFUoya8A3MxKjD4eKjzP1WgIsVM4KB79N5RraWRVv/gRUJYsJ0RTeqSiUoVpj X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2c25db69-5afa-44d0-85b9-08d9dac857b1 X-MS-Exchange-CrossTenant-AuthSource: BN8PR10MB3220.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2022 21:20:25.1545 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hwLLi3JJFGj1eMrlWevz9CA9SPFwoakBTi4PRMSquLO0uoH+6D01s3zU9kRfQPIa1op49gSmB6qXudiJAJUKTA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR10MB3029 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10231 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 bulkscore=0 mlxlogscore=999 adultscore=0 suspectscore=0 malwarescore=0 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201180122 X-Proofpoint-GUID: LIR0LEQ-rbqWTGbL3JHOMUe1UMH6uQ6s X-Proofpoint-ORIG-GUID: LIR0LEQ-rbqWTGbL3JHOMUe1UMH6uQ6s X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: C3D26140002 X-Stat-Signature: a5dzabobxwfsnybo64qjopwx318oy146 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=l450seAn; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=iH38Z2xm; spf=none (imf26.hostedemail.com: domain of khalid.aziz@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=khalid.aziz@oracle.com; dmarc=pass (policy=none) header.from=oracle.com X-HE-Tag: 1642540839-313725 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: Lock the root inode for msharefs when creating a new file or deleting an existing one to avoid races. mshare syscalls are low frequency operations, so locking the root inode is reasonable. Signed-off-by: Khalid Aziz --- mm/mshare.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mm/mshare.c b/mm/mshare.c index 8273136363cc..adfd5a280e5b 100644 --- a/mm/mshare.c +++ b/mm/mshare.c @@ -194,11 +194,12 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, err = msharefs_d_hash(msharefs_sb->s_root, &namestr); if (err) goto err_out; + inode_lock(d_inode(msharefs_sb->s_root)); dentry = d_lookup(msharefs_sb->s_root, &namestr); if (dentry && (oflag & (O_EXCL|O_CREAT))) { err = -EEXIST; dput(dentry); - goto err_out; + goto err_unlock_inode; } if (dentry) { @@ -231,6 +232,7 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, goto err_relinfo; } + inode_unlock(d_inode(msharefs_sb->s_root)); putname(fname); return 0; @@ -238,6 +240,8 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, kfree(info); err_relmm: mmput(mm); +err_unlock_inode: + inode_unlock(d_inode(msharefs_sb->s_root)); err_out: putname(fname); return err; @@ -263,10 +267,11 @@ SYSCALL_DEFINE1(mshare_unlink, const char *, name) err = msharefs_d_hash(msharefs_sb->s_root, &namestr); if (err) goto err_out; + inode_lock(d_inode(msharefs_sb->s_root)); dentry = d_lookup(msharefs_sb->s_root, &namestr); if (dentry == NULL) { err = -EINVAL; - goto err_out; + goto err_unlock_inode; } inode = d_inode(dentry); @@ -289,11 +294,14 @@ SYSCALL_DEFINE1(mshare_unlink, const char *, name) dput(dentry); } + inode_unlock(d_inode(msharefs_sb->s_root)); putname(fname); return 0; err_dput: dput(dentry); +err_unlock_inode: + inode_unlock(d_inode(msharefs_sb->s_root)); err_out: putname(fname); return err; From patchwork Tue Jan 18 21:19:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Aziz X-Patchwork-Id: 12716911 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 E1A96C433F5 for ; Tue, 18 Jan 2022 21:20:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40B7F6B007D; Tue, 18 Jan 2022 16:20:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 370146B007E; Tue, 18 Jan 2022 16:20:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0FCF66B0080; Tue, 18 Jan 2022 16:20:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0023.hostedemail.com [216.40.44.23]) by kanga.kvack.org (Postfix) with ESMTP id E4A4E6B007D for ; Tue, 18 Jan 2022 16:20:50 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id A10D795293 for ; Tue, 18 Jan 2022 21:20:50 +0000 (UTC) X-FDA: 79044677460.17.2A4DC5D Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf24.hostedemail.com (Postfix) with ESMTP id 04AF8180002 for ; Tue, 18 Jan 2022 21:20:49 +0000 (UTC) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20IKEeXe007879; Tue, 18 Jan 2022 21:20:38 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 : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=IKtghRJml7dROoVgt7NTaYemW9dQC20dDMGSxYcdVG0=; b=gzvSOhI7/eiuwu0acx5SQPd+tetoQ7xsAz/kd5atUdwps0xhY3smts53nyHv3Dxk4Ro4 2rfRNf+UH9yD6577woPRN9ddIPzb4NG4NszbUcWz+nIfLmGBab7/RHihsjFpv2IqFQwX agPFS3JWIHHcSnW5NolvPgnfzaN4F6h8KqZBZIsX2YSvMSlVih5Bfdnt7nceUcQeGXFl 1ZoELs0CE/HKXYBiDFGHVciubJpCWtURdLMPb1kSFnsGnkTjIrTKTkI9SHTxleeDnujS e9Arn+kY2S+xxxfg5xzxy3fv22Yv0pZWYV4Y+5vfO7q8cqmMqbqujmrhKm1uRZPPOxOj BQ== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc4vk8pt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:37 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20ILKY4k129513; Tue, 18 Jan 2022 21:20:36 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam08lp2049.outbound.protection.outlook.com [104.47.74.49]) by userp3030.oracle.com with ESMTP id 3dkkcy0wu5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Jan 2022 21:20:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JLakHh0oPnPVXhzSxTTwUpLnspL/gbLLW1COPJGgJ/Mb7vfQF4TT50di/WCnrgoxFGNoTwQYkTEGDhwJ1j/0fq5+NPoCsDHtCvLLlh/ht1TaMYEwTO+L7FUbKuDQ4EhMwfrq+Q7kG59SGb4VX3rkI/WRupVso+Fo1nCGLgHl4gxaFr1dVAIkP/9BLtS273olKxzlT9+MZ3xVcxLGwhkCN0roAHDHWt8NxNEp2FcyIRhBjdqI/20pqAjzs5wsFXwY30z3fo7TRkxhuv7YAjf5wL/+tNEOj4Akro8H0FzFhGzEAZ+g8v03/OdUGUjneXK4LgpQWw8ZTMa9oF3I2R/3Mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=IKtghRJml7dROoVgt7NTaYemW9dQC20dDMGSxYcdVG0=; b=lF2lvHo+r39wEGu8KZZNH9QwVbz291orFlTxd46xWlcoj4XzAvR3QSMwsTinbo40eA/p4HETTBtp3YKA9HKGumiI4uW479HxcRHCnbB83BOmleQnZMgBYoxYlWg2Rb74LG9Cuh40Ravr2h8u1JS1LzJDGgotZJMPMFkld3BH4L2ymCbNvSkAmYR5QqtYXKouOrvbrb54NFC5GZGdzspJKJ3IUvC+qlN8IZYVU1/phcffs/034+hkyoL5bE9MSZt3lJbBjnqY49abbPZNCIWT5vPMH8k9fK6ZIce6FQCRd2tMBo+LbQEmNZbcLXI8o69Q92iAbo5loUQBlBwzeZ7rqA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IKtghRJml7dROoVgt7NTaYemW9dQC20dDMGSxYcdVG0=; b=wzeYXstJlKz3WVq1Fw52XUt03V4TalBFdKUbjWUZMGfAk+MffNNfOoYMLHMbDqbn+qenMT3k3vhgVHPAch2CJ4WHgJS6PJKhXP5bpV4Q2XHKX9amuEkM25WaK9iirTxEP9ufbauvlTpNR2vswqXJ6iaj5YaRZjakwtPQ7+znzDc= Received: from BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) by BYAPR10MB3029.namprd10.prod.outlook.com (2603:10b6:a03:8d::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.9; Tue, 18 Jan 2022 21:20:28 +0000 Received: from BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2]) by BN8PR10MB3220.namprd10.prod.outlook.com ([fe80::104f:849b:6a11:c5c2%6]) with mapi id 15.20.4888.014; Tue, 18 Jan 2022 21:20:28 +0000 From: Khalid Aziz To: akpm@linux-foundation.org, willy@infradead.org, longpeng2@huawei.com, arnd@arndb.de, dave.hansen@linux.intel.com, david@redhat.com, rppt@kernel.org, surenb@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Khalid Aziz Subject: [RFC PATCH 6/6] mm: Add basic page table sharing using mshare Date: Tue, 18 Jan 2022 14:19:18 -0700 Message-Id: <5c1e369b7620c20d51729acd745f8180c4e2a416.1642526745.git.khalid.aziz@oracle.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-ClientProxiedBy: BYAPR07CA0001.namprd07.prod.outlook.com (2603:10b6:a02:bc::14) To BN8PR10MB3220.namprd10.prod.outlook.com (2603:10b6:408:c8::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0d915b6e-49e5-4a16-04a7-08d9dac8599f X-MS-TrafficTypeDiagnostic: BYAPR10MB3029:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2803; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tw5tBx/KdZvh8gjhcAdxAqQyjR1STU4ut7NMn84IdYUvagqeKJ1Od1itpPbaGKsqJieCdHVrN4Z2RqERngh3eVktxXNIcqcRRwrBMENfhw2dVxbSIywQIcY6zQy1IH1/bofxgR0Cxs0F1dDp/g/qCfBQ39qWwvnHwXuB0KdiP3g0HHNctSqMMkyjD1xbJPIof1VnQOeVSLnbKDfv61Chud+jCBjM7EmPaSp7O3yz57QNJtLYvdifNnmMqp56sIVqvKIaLZNU/HVuCuIkJLp2AJu12cN8vIGhejVakmbZVkoqQFWtKP8pHUmyGngo+joZIxpT1pQYVEzyjwb8cEujndtRD3tMX5MyrONByainzJKyv5plULMikmgLPDesvdOCr/JqpSiWMrXO+WykkNa+5/KVTy981JKn11DbJ7IRqp0sXrVyCgFYNO5mAwrDAwyYNL2c2RmCU81Gh45CXLiGv9KgAhm1ez8FNPVJw7A31ovVudM2AKzuns1kt6bNYHF5OPaRjHO/jvWH3cR6hZMkxkouKmC9h8h5g3P1Vxo+14HhG8HKsiSW90Ps5ED19t0c3dOZ4bkz1OpvEoPh++x5Z41uOzwpj2tD81QFAi4oBszcKGQ1TJCpgq1SCdTU36/kknOnsYRZU9c/cJ0H3ypT8JCxxq4EuFisMkVd/hRswzAWL0zGPwYd1SC3NsGnuKkori0sMJOphUqnHkqyeaOhmQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN8PR10MB3220.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(36756003)(38100700002)(316002)(921005)(6512007)(186003)(6506007)(44832011)(83380400001)(6486002)(8936002)(6666004)(2906002)(66946007)(2616005)(52116002)(4326008)(7416002)(86362001)(30864003)(66476007)(8676002)(508600001)(66556008)(107886003)(5660300002)(21314003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2uHQTlcvOrpcXkOvXkFvb5c/cUNY84QsLW1VgcXkJ/VAuFCldEBa//fz03NX9oMpzpJSkxQASRzzpv2HBfo+ZOj3/AoHMdUSKsxzHDs4nDxpF54s6acXyxWb23bcdO448ZNqvk9x1Aw/VFp/mUrKYK4p9WysP2BmKxupUrHxTJmWU5sfJMbpYmeXRZPCvVFeobVUk7ZTzAK8vOSm4DveYG3s/U6WW7upcH5h9vLQJJ2wprgs3WxOUhqYu66tQPmvLsYNjQ/pg4LF/pCN7mSNpFMGgPbR6IAT5BXShvdemzyAc7ASdyUCQm1YLYwf4ynvinReye8mu2xZlYQJwdytfTL89+KdScSSaYmzsJjT8RzsbA4mzusg/L4Mm+KWgzv7HlsneXKoFahJ0bsssNvIcmxNEb3HS5xnC2mIZiO9Jpz9uu7H8z+E0S4MyqWJUWFfHwxGSIai6EVoWsovO5K0eH+otVzD4b8/k78wH/Ez+c65ccB6d6llJN83bO0vDfiXxr5Szi4KFxqqbe/m1gls3cZ3f2FRHTIKEfafcku0NLZtzlg1Kux72IiC7Cbxg+9RhPHrRb3WSZ1VkhQgGIhRSWK8lxNZ27eV+tG2AX0Eugvje8WX6Lrer5WLoQ7Vm7MHpXX5/n7BXFZ0rsb5bO9rs/xnd80B7HB3NvVYLIIubtVkv8lw7h06bSw0xKijRpqKl2NdLeZ4P5x5mcWbRQqKXdSW+sptNQvj5E/VSB1OtNcgMlrergzsgiGzDbp3GL8XrDn/hfkbyBskY/ld6aXYz7HnGHZSYpdVDWvfPxJQAnoQK5rMJardQD77eBFdLvgdoqCQVkdLlGjdK71OTtoL7ymGdf/5l68OgiTBaXneI8uRzAoTvBdl+Dm1z9SqaFnzdfK/ZX1Fw8gZ0K3y+gzqnI0Mg1wLkxWJO96Nc7bKsmRgR8MU1543GL3leqlZ035VpbjPWcn+Jd62xtj2k4SGG48Sy7czNLVAEuI71Cqwy4ah4cV2lkBnHoO3HeI4Cf1Wvumoxck8QPEEMaNAx5Z1us/LFk4KxU5GrjTCB9P+uNo8Spx/8C5owpmQi0CkEFBivKVNmQscUqoDfWhA3OgOqe3lZsYTTY29spJERu52GslGGPKe/ocz6Juw5tv3QBM6PdJINBlgygUtVybnEEa5ADqOTIncN9OK9qJMT/AmSZnysHQtCFPYNu8wCXOPriiSe8xMLurWtH05gPrO5Iz/MKclGmMryHceXPgxyK2kd6F+t5WzJDFSlBUeMqZWH3hSakPPZQmfBuQVVcFY1QxAgEOlrs6zJtrBjR5e2izIbfJt8fkvwoLLhuUBTLUa4SgA7UR3CHW+d2oxbNra8ZvD9tAKL40Gq2ckL/Hei2SwItF7XJdUIM861JQINTnvIA8OehTNSH2TTsIFMQi7w7VtvXsMfTOd5HXnKI+FpxP4Q7/OzFQnImgFcBKX9gq/AGbV+n82bd9v92RmvHBQYYne8pfpDoBKdxUwIrdfnBPiQ+Fck+MRcqpI6vAVLsy1h8VTnsjUmSFnxN0CGPfT++U+spKY+R1ZabgwRDFREyBBqWlhTphSP38HAZPZcBJ/qSh0N/k1+vXGaV+Mxv6f1lORAc4zigkJkkFIdW0N8NXhHPp6J/ndl/qcQFS7aenSUJaZ X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0d915b6e-49e5-4a16-04a7-08d9dac8599f X-MS-Exchange-CrossTenant-AuthSource: BN8PR10MB3220.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2022 21:20:28.6074 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zkO2if1IIbFZWWkZcdSwT5/1euAyqa6YaFJeBcxEZ7B01KTm5Qk3yppWnFbS6/X4zo3HXmDMK8i9GkfeJ+muMA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR10MB3029 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10231 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 bulkscore=0 malwarescore=0 adultscore=0 phishscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201180122 X-Proofpoint-GUID: 4y36GxPkK4cdn-3CnahSESEsrIt9STXV X-Proofpoint-ORIG-GUID: 4y36GxPkK4cdn-3CnahSESEsrIt9STXV X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 04AF8180002 X-Stat-Signature: awy8qhcmrcxtbjru5dfjp4qq8kusdx5s Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=gzvSOhI7; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=wzeYXstJ; spf=none (imf24.hostedemail.com: domain of khalid.aziz@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=khalid.aziz@oracle.com; dmarc=pass (policy=none) header.from=oracle.com X-HE-Tag: 1642540849-696467 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: This patch adds basic page table sharing across tasks by making mshare syscall. It does this by creating a new mm_struct which hosts the shared vmas and page tables. This mm_struct is maintained as long as there is at least one task using the mshare'd range. It is cleaned up by the last mshare_unlink syscall. NOTE: WORK IN PRGRESS. This is only a working prototype and has bugs and many missing pieces. mm/mshare.c documents these bugs and issues that should be addressed in a more complete implementation. Signed-off-by: Khalid Aziz Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/mm.h | 8 + include/trace/events/mmflags.h | 3 +- mm/internal.h | 7 + mm/memory.c | 35 ++++- mm/mshare.c | 265 +++++++++++++++++++++++++++++++-- 5 files changed, 299 insertions(+), 19 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index a7e4a9e7d807..63128f6c83cd 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -308,11 +308,13 @@ extern unsigned int kobjsize(const void *objp); #define VM_HIGH_ARCH_BIT_2 34 /* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_BIT_3 35 /* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_BIT_4 36 /* bit only usable on 64-bit architectures */ +#define VM_HIGH_ARCH_BIT_5 37 /* bit only usable on 64-bit architectures */ #define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0) #define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1) #define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2) #define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3) #define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4) +#define VM_HIGH_ARCH_5 BIT(VM_HIGH_ARCH_BIT_5) #endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */ #ifdef CONFIG_ARCH_HAS_PKEYS @@ -354,6 +356,12 @@ extern unsigned int kobjsize(const void *objp); # define VM_MTE_ALLOWED VM_NONE #endif +#ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS +#define VM_SHARED_PT VM_HIGH_ARCH_5 +#else +#define VM_SHARED_PT 0 +#endif + #ifndef VM_GROWSUP # define VM_GROWSUP VM_NONE #endif diff --git a/include/trace/events/mmflags.h b/include/trace/events/mmflags.h index 116ed4d5d0f8..002dbf2711c5 100644 --- a/include/trace/events/mmflags.h +++ b/include/trace/events/mmflags.h @@ -184,7 +184,8 @@ IF_HAVE_VM_SOFTDIRTY(VM_SOFTDIRTY, "softdirty" ) \ {VM_MIXEDMAP, "mixedmap" }, \ {VM_HUGEPAGE, "hugepage" }, \ {VM_NOHUGEPAGE, "nohugepage" }, \ - {VM_MERGEABLE, "mergeable" } \ + {VM_MERGEABLE, "mergeable" }, \ + {VM_SHARED_PT, "sharedpt" } \ #define show_vma_flags(flags) \ (flags) ? __print_flags(flags, "|", \ diff --git a/mm/internal.h b/mm/internal.h index 3b79a5c9427a..9bfc4dde7d70 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -713,4 +713,11 @@ void vunmap_range_noflush(unsigned long start, unsigned long end); int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, unsigned long addr, int page_nid, int *flags); +extern vm_fault_t find_shared_vma(struct vm_area_struct **vma, + unsigned long *addrp); +static inline bool vma_is_shared(const struct vm_area_struct *vma) +{ + return vma->vm_flags & VM_SHARED_PT; +} + #endif /* __MM_INTERNAL_H */ diff --git a/mm/memory.c b/mm/memory.c index 8f1de811a1dc..b506bbbfae60 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -418,16 +418,25 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, } else { /* * Optimization: gather nearby vmas into one call down + * as long as they all belong to the same mm (that + * may not be the case if a vma is part of mshare'd + * range */ while (next && next->vm_start <= vma->vm_end + PMD_SIZE - && !is_vm_hugetlb_page(next)) { + && !is_vm_hugetlb_page(next) + && vma->vm_mm == tlb->mm) { vma = next; next = vma->vm_next; unlink_anon_vmas(vma); unlink_file_vma(vma); } - free_pgd_range(tlb, addr, vma->vm_end, - floor, next ? next->vm_start : ceiling); + /* + * Free pgd only if pgd is not allocated for an + * mshare'd range + */ + if (vma->vm_mm == tlb->mm) + free_pgd_range(tlb, addr, vma->vm_end, + floor, next ? next->vm_start : ceiling); } vma = next; } @@ -1528,6 +1537,13 @@ void unmap_page_range(struct mmu_gather *tlb, pgd_t *pgd; unsigned long next; + /* + * If this is an mshare'd page, do not unmap it since it might + * still be in use. + */ + if (vma->vm_mm != tlb->mm) + return; + BUG_ON(addr >= end); tlb_start_vma(tlb, vma); pgd = pgd_offset(vma->vm_mm, addr); @@ -4757,6 +4773,7 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, unsigned int flags, struct pt_regs *regs) { vm_fault_t ret; + bool shared = false; __set_current_state(TASK_RUNNING); @@ -4766,6 +4783,15 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, /* do counter updates before entering really critical section. */ check_sync_rss_stat(current); + if (unlikely(vma_is_shared(vma))) { + ret = find_shared_vma(&vma, &address); + if (ret) + return ret; + if (!vma) + return VM_FAULT_SIGSEGV; + shared = true; + } + if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, flags & FAULT_FLAG_INSTRUCTION, flags & FAULT_FLAG_REMOTE)) @@ -4783,6 +4809,9 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, else ret = __handle_mm_fault(vma, address, flags); + if (shared) + mmap_read_unlock(vma->vm_mm); + if (flags & FAULT_FLAG_USER) { mem_cgroup_exit_user_fault(); /* diff --git a/mm/mshare.c b/mm/mshare.c index adfd5a280e5b..ffdc72963b6b 100644 --- a/mm/mshare.c +++ b/mm/mshare.c @@ -6,8 +6,37 @@ * * * Copyright (C) 2021 Oracle Corp. All rights reserved. - * Authors: Matthew Wilcox - * Khalid Aziz + * Authors: Matthew Wilcox + * Khalid Aziz + * + * Current issues/questions: + * - mshare_unlink should unmap all shared VMAs for the calling task + * - If a task that had called mshare dies, make sure its shared VMAs + * are cleaned up properly and refcount to shared region is + * updated correctly. + * - Should mshare_unlink be allowed only for the tasks that called + * mshare() originally so the two calls are matched up? If yes, + * should there still be a way to clean up stale shared regions? + * - Allow already mapped in VMAs to be mshare'd by the task creating + * mshare region. This will potentially require splitting VMAs + * - What happens when a task tries to attach to an existing mshare + * region and it already has VMAs mapped into that address range? + * Possibilities - (1) mshare() fails, or (2) unmap current VMAs + * and create new ones with new mapping. If (2), what happens + * if exisiting VMA is larger than mshare region - split the + * VMA and leave partial original mapping intact, or unmap all + * overlapping VMAs + * - Can the tasks using mshare region mmap/mremap things into sections + * of the range? If yes, that will require additional work. Which + * mmaps should be supported - anonymous, files?? + * - How does this work with hugepages? + * - How does this work with OOM killer? + * - BUG: PTEs no longer work once the task that created mshare + * range dies. + * - mmu_notifier uses vma->vm_mm. That very likely breaks with + * this code + * - Should consumer processes be allowed to only map the entire + * mshare'd region or should they be allowed to map subset of it? */ #include @@ -17,17 +46,50 @@ #include #include #include +#include #include #include +#include "internal.h" struct mshare_data { - struct mm_struct *mm; + struct mm_struct *mm, *host_mm; mode_t mode; refcount_t refcnt; }; static struct super_block *msharefs_sb; +/* Returns holding the host mm's lock for read. Caller must release. */ +vm_fault_t +find_shared_vma(struct vm_area_struct **vmap, unsigned long *addrp) +{ + struct vm_area_struct *vma, *guest = *vmap; + struct mshare_data *info = guest->vm_private_data; + struct mm_struct *host_mm = info->mm; + unsigned long host_addr; + pgd_t *pgd, *guest_pgd; + + host_addr = *addrp - guest->vm_start + host_mm->mmap_base; + pgd = pgd_offset(host_mm, host_addr); + guest_pgd = pgd_offset(current->mm, *addrp); + if (!pgd_same(*guest_pgd, *pgd)) { + set_pgd(guest_pgd, *pgd); + return VM_FAULT_NOPAGE; + } + + *addrp = host_addr; + mmap_read_lock(host_mm); + vma = find_vma(host_mm, host_addr); + + /* XXX: expand stack? */ + if (vma && vma->vm_start > host_addr) + vma = NULL; + + *vmap = vma; + vma->vm_mm = host_mm; + return 0; +} + static void msharefs_evict_inode(struct inode *inode) { @@ -168,13 +230,23 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, unsigned long, len, int, oflag, mode_t, mode) { struct mshare_data *info; - struct mm_struct *mm; struct filename *fname = getname(name); struct dentry *dentry; struct inode *inode; struct qstr namestr; + struct vm_area_struct *vma, *next, *new_vma; + struct mm_struct *new_mm; + unsigned long end; int err = PTR_ERR(fname); + + /* + * Is msharefs mounted? TODO: If not mounted, return error + * or automount? + */ + if (msharefs_sb == NULL) + return -ENOENT; + /* * Address range being shared must be aligned to pgdir * boundary and its size must be a multiple of pgdir size @@ -185,6 +257,8 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, if (IS_ERR(fname)) goto err_out; + end = addr + len; + /* * Does this mshare entry exist already? If it does, calling * mshare with O_EXCL|O_CREAT is an error @@ -197,49 +271,183 @@ SYSCALL_DEFINE5(mshare, const char __user *, name, unsigned long, addr, inode_lock(d_inode(msharefs_sb->s_root)); dentry = d_lookup(msharefs_sb->s_root, &namestr); if (dentry && (oflag & (O_EXCL|O_CREAT))) { + inode = d_inode(dentry); err = -EEXIST; dput(dentry); goto err_unlock_inode; } if (dentry) { + unsigned long mapaddr, prot = PROT_NONE; + + /* + * TODO: Address the following comment + * + * For now, we do not allow mshare mapping an existing mshare + * region if any overlapping mappings exist in calling + * process already + */ + mmap_write_lock(current->mm); + vma = find_vma_intersection(current->mm, addr, end); + if (vma) { + mmap_write_unlock(current->mm); + err = -EINVAL; + goto err_out; + } + inode = d_inode(dentry); if (inode == NULL) { + mmap_write_unlock(current->mm); err = -EINVAL; goto err_out; } info = inode->i_private; - refcount_inc(&info->refcnt); dput(dentry); + + /* + * Map in the address range as anonymous mappings + */ + mmap_write_unlock(current->mm); + oflag &= (O_RDONLY | O_WRONLY | O_RDWR); + if (oflag & O_RDONLY) + prot |= PROT_READ; + else if (oflag & O_WRONLY) + prot |= PROT_WRITE; + else if (oflag & O_RDWR) + prot |= (PROT_READ | PROT_WRITE); + mapaddr = vm_mmap(NULL, addr, len, prot, + MAP_FIXED | MAP_SHARED | MAP_ANONYMOUS, 0); + if (IS_ERR((void *)mapaddr)) { + err = -EINVAL; + goto err_out; + } + + refcount_inc(&info->refcnt); + + /* + * Now that we have mmap'd the mshare'd range, update vma + * flags and vm_mm pointer for this mshare'd range. + */ + mmap_write_lock(current->mm); + vma = find_vma(current->mm, addr); + if (vma && vma->vm_start < addr) { + mmap_write_unlock(current->mm); + err = -EINVAL; + goto err_out; + } + + while (vma && vma->vm_start < (addr + len)) { + vma->vm_private_data = info; + vma->vm_mm = info->mm; + vma->vm_flags |= VM_SHARED_PT; + next = vma->vm_next; + vma = next; + } } else { - mm = mm_alloc(); - if (!mm) + unsigned long myaddr; + struct mm_struct *old_mm; + + old_mm = current->mm; + new_mm = mm_alloc(); + if (!new_mm) return -ENOMEM; info = kzalloc(sizeof(*info), GFP_KERNEL); if (!info) { err = -ENOMEM; goto err_relmm; } - mm->mmap_base = addr; - mm->task_size = addr + len; - if (!mm->task_size) - mm->task_size--; - info->mm = mm; + new_mm->mmap_base = addr; + new_mm->task_size = addr + len; + if (!new_mm->task_size) + new_mm->task_size--; + info->mm = new_mm; + info->host_mm = old_mm; info->mode = mode; refcount_set(&info->refcnt, 1); + + /* + * VMAs for this address range may or may not exist. + * If VMAs exist, they should be marked as shared at + * this point and page table info should be copied + * over to newly created mm_struct. TODO: If VMAs do not + * exist, create them and mark them as shared. + */ + mmap_write_lock(old_mm); + vma = find_vma_intersection(old_mm, addr, end); + if (!vma) { + err = -EINVAL; + goto unlock; + } + /* + * TODO: If the currently allocated VMA goes beyond the + * mshare'd range, this VMA needs to be split. + * + * Double check that source VMAs do not extend outside + * the range + */ + vma = find_vma(old_mm, addr + len); + if (vma && vma->vm_start < (addr + len)) { + err = -EINVAL; + goto unlock; + } + + vma = find_vma(old_mm, addr); + if (vma && vma->vm_start < addr) { + err = -EINVAL; + goto unlock; + } + + mmap_write_lock(new_mm); + while (vma && vma->vm_start < (addr + len)) { + /* + * Copy this vma over to host mm + */ + new_vma = vm_area_dup(vma); + if (!new_vma) { + err = -ENOMEM; + goto unlock; + } + vma->vm_mm = new_mm; + err = insert_vm_struct(new_mm, new_vma); + if (err) + goto unlock; + + vma->vm_private_data = info; + vma->vm_mm = new_mm; + vma->vm_flags |= VM_SHARED_PT; + vma = vma->vm_next; + } + mmap_write_unlock(new_mm); + + err = mshare_file_create(fname, oflag, info); if (err) - goto err_relinfo; + goto unlock; + + /* + * Copy over current PTEs + */ + myaddr = addr; + while (myaddr < new_mm->task_size) { + *pgd_offset(new_mm, myaddr) = *pgd_offset(old_mm, myaddr); + myaddr += PGDIR_SIZE; + } + /* + * TODO: Free the corresponding page table in calling + * process + */ } + mmap_write_unlock(current->mm); inode_unlock(d_inode(msharefs_sb->s_root)); putname(fname); return 0; -err_relinfo: +unlock: + mmap_write_unlock(current->mm); kfree(info); err_relmm: - mmput(mm); + mmput(new_mm); err_unlock_inode: inode_unlock(d_inode(msharefs_sb->s_root)); err_out: @@ -259,6 +467,9 @@ SYSCALL_DEFINE1(mshare_unlink, const char *, name) struct mshare_data *info; struct qstr namestr; + if (msharefs_sb == NULL) + return -ENOENT; + if (IS_ERR(fname)) goto err_out; @@ -283,14 +494,38 @@ SYSCALL_DEFINE1(mshare_unlink, const char *, name) /* * Is this the last reference? + * TODO: permission checks are needed before proceeding */ if (refcount_dec_and_test(&info->refcnt)) { simple_unlink(d_inode(msharefs_sb->s_root), dentry); d_drop(dentry); d_delete(dentry); + /* + * TODO: Release all physical pages allocated for this + * mshare range and release associated page table. If + * the final unlink happens from the process that created + * mshare'd range, do we return page tables and pages to + * that process so the creating process can continue using + * the address range it had chosen to mshare at some + * point? + * + * TODO: unmap shared vmas from every task that is using + * this mshare'd range. + */ mmput(info->mm); kfree(info); } else { + /* + * TODO: If mshare'd range is still mapped in the process, + * it should be unmapped. Following is minimal code and + * might need fix up + */ + unsigned long tmp; + + tmp = info->mm->task_size - info->mm->mmap_base; + if (info->host_mm != current->mm) + vm_munmap(info->mm->mmap_base, tmp); + dput(dentry); }