From patchwork Wed Feb 12 23:39:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11379371 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 24590139A for ; Wed, 12 Feb 2020 23:40:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D84812168B for ; Wed, 12 Feb 2020 23:40:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bWREWAVI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D84812168B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 15A7D6B04D9; Wed, 12 Feb 2020 18:40:09 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0B9146B04DB; Wed, 12 Feb 2020 18:40:08 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFC156B04DC; Wed, 12 Feb 2020 18:40:08 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0152.hostedemail.com [216.40.44.152]) by kanga.kvack.org (Postfix) with ESMTP id C3DC76B04D9 for ; Wed, 12 Feb 2020 18:40:08 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 7B2D4181AC217 for ; Wed, 12 Feb 2020 23:40:08 +0000 (UTC) X-FDA: 76483095696.04.gun79_7593f34a6d343 X-Spam-Summary: 2,0,0,327047ff6ebe65fb,d41d8cd98f00b204,minchan.kim@gmail.com,,RULES_HIT:41:69:355:379:541:800:960:967:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1543:1711:1730:1747:1777:1792:2393:2525:2559:2563:2682:2685:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3355:3865:3866:3867:3868:3871:3872:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4118:4321:4605:5007:6261:6653:6742:7903:8603:9025:10004:11026:11473:11658:11914:12043:12291:12296:12297:12438:12517:12519:12555:12683:12895:12986:13161:13229:13894:14096:14181:14394:14721:21080:21324:21444:21451:21627:21749:21789:21795:21987:21990:30051:30054:30062,0,RBL:209.85.215.193:@gmail.com:.lbl8.mailshell.net-62.50.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:32,LUA_SUMMARY:none X-HE-Tag: gun79_7593f34a6d343 X-Filterd-Recvd-Size: 7062 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf34.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Feb 2020 23:40:07 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id b35so1889093pgm.13 for ; Wed, 12 Feb 2020 15:40:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pWH2/XfpqHoXeEJcQbZVzZEZcw7B8tmo1ECvMd0jYwE=; b=bWREWAVI4HhSS5OM8Zqnl6gUsWQ9sN96jWhHNUjWcDBFA+Q6mJrx/4g19dOVfs9Ye4 tPbmB8ynizGVZ8zI3npJCb7EInnYDMvogtNBHXlD8ulDggfqinu1JOMndy90RTXYX3OL bf0AX7gTt96+smPkoh8nTE8pu2WxkB2BtMtZEX/lg/bKppFe2pHIvft8NcjttFkYInCd xiTQixj7Dt1p7nbqf0yhVY5Y5tUCHY4PBFB41gEeUz8KgQoE4djWOzIJI6dVjnEm2bPS bD6zpMXu/madz4nXCNpu3PFIk2a/gkgO2xiGKTYT2Rl2GE3gkdPpZFzocuWl2sVHoHBA U2tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=pWH2/XfpqHoXeEJcQbZVzZEZcw7B8tmo1ECvMd0jYwE=; b=C/ozNW0jzdgyct1y80X5EKnmzf4KTBJbk9gpVYRjXxZP2ZPUzdmAc2x2dFt0gb3ysY Rmi3hZCMUXEg5zb5cW4EWqf576jsedr5nwybPi4gJbu1/eULF2NRo4lxIW4jBMhz0pFA ylXUkihVj9gM+N38TYIjyVbRK67ULWcC7udHG+e7pESlr+4kwkrW0PixFOhC+LBrX8QP gGXLAa+Vmj8pipNIJwi3ppA+7I8WVpeTnLmrqEAk7O1IHeZfntXHa3uyeKpwtCbytdlE +cVgWXGTwR1vIWBW+3xxH8lrbvNbojE7JFrzIYGpsXV7tHZR5obujm3yzEWddk5KF9AH lskw== X-Gm-Message-State: APjAAAVGPfg57mtofX5gcK+TJSCQHXFhIx1meW0gRDpdKKolBf9QGRNC Hu5JlZVstcHGtE975a248iJkpoTx X-Google-Smtp-Source: APXvYqyY0iR439WjIBFBQq68OmnI91robzRKkjf22DYwp60HBsJfY4DO0T5AXsqGpHdDMPCO6BXMzg== X-Received: by 2002:a62:18c9:: with SMTP id 192mr10746365pfy.117.1581550806888; Wed, 12 Feb 2020 15:40:06 -0800 (PST) Received: from bbox-1.mtv.corp.google.com ([2620:15c:211:1:3e01:2939:5992:52da]) by smtp.gmail.com with ESMTPSA id k5sm296664pju.29.2020.02.12.15.40.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 15:40:06 -0800 (PST) From: Minchan Kim To: Andrew Morton Cc: LKML , linux-mm , linux-api@vger.kernel.org, oleksandr@redhat.com, Suren Baghdasaryan , Tim Murray , Daniel Colascione , Sandeep Patil , Sonny Rao , Brian Geffon , Michal Hocko , Johannes Weiner , Shakeel Butt , John Dias , Joel Fernandes , Alexander Duyck , Minchan Kim , Christian Brauner , Kirill Tkhai Subject: [PATCH v4 8/8] mm: support both pid and pidfd for process_madvise Date: Wed, 12 Feb 2020 15:39:46 -0800 Message-Id: <20200212233946.246210-9-minchan@kernel.org> X-Mailer: git-send-email 2.25.0.225.g125e21ebc7-goog In-Reply-To: <20200212233946.246210-1-minchan@kernel.org> References: <20200212233946.246210-1-minchan@kernel.org> MIME-Version: 1.0 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: There is a demand[1] to support pid as well pidfd for process_madvise to reduce unnecessary syscall to get pidfd if the user has control of the target process(ie, they could guarantee the process is not gone or pid is not reused. Or, it might be okay to give a hint to wrong process). This patch aims for supporting both options like waitid(2). So, the syscall is currently, int process_madvise(int which, pid_t pid, void *addr, size_t length, int advise, unsigned long flag); @which is actually idtype_t for userspace libray and currently, it supports P_PID and P_PIDFD. [1] https://lore.kernel.org/linux-mm/9d849087-3359-c4ab-fbec-859e8186c509@virtuozzo.com/ Cc: Christian Brauner Suggested-by: Kirill Tkhai Signed-off-by: Minchan Kim --- include/linux/syscalls.h | 3 ++- mm/madvise.c | 34 ++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index e4cd2c2f8bb4..f5ada20e2943 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -876,7 +876,8 @@ asmlinkage long sys_munlockall(void); asmlinkage long sys_mincore(unsigned long start, size_t len, unsigned char __user * vec); asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior); -asmlinkage long sys_process_madvise(int pidfd, unsigned long start, + +asmlinkage long sys_process_madvise(int which, pid_t pid, unsigned long start, size_t len, int behavior, unsigned long flags); asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size, unsigned long prot, unsigned long pgoff, diff --git a/mm/madvise.c b/mm/madvise.c index b1237466657e..32833d0ba574 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1190,11 +1190,10 @@ SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) return do_madvise(current, start, len_in, behavior); } -SYSCALL_DEFINE5(process_madvise, int, pidfd, unsigned long, start, +SYSCALL_DEFINE6(process_madvise, int, which, pid_t, upid, unsigned long, start, size_t, len_in, int, behavior, unsigned long, flags) { int ret; - struct fd f; struct pid *pid; struct task_struct *task; struct mm_struct *mm; @@ -1205,20 +1204,31 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, unsigned long, start, if (!process_madvise_behavior_valid(behavior)) return -EINVAL; - f = fdget(pidfd); - if (!f.file) - return -EBADF; + switch (which) { + case P_PID: + if (upid <= 0) + return -EINVAL; + + pid = find_get_pid(upid); + if (!pid) + return -ESRCH; + break; + case P_PIDFD: + if (upid < 0) + return -EINVAL; - pid = pidfd_pid(f.file); - if (IS_ERR(pid)) { - ret = PTR_ERR(pid); - goto fdput; + pid = pidfd_get_pid(upid); + if (IS_ERR(pid)) + return PTR_ERR(pid); + break; + default: + return -EINVAL; } task = get_pid_task(pid, PIDTYPE_PID); if (!task) { ret = -ESRCH; - goto fdput; + goto put_pid; } mm = mm_access(task, PTRACE_MODE_ATTACH_FSCREDS); @@ -1231,7 +1241,7 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, unsigned long, start, mmput(mm); release_task: put_task_struct(task); -fdput: - fdput(f); +put_pid: + put_pid(pid); return ret; }