From patchwork Wed Feb 19 01:44:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11390197 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 0BED21395 for ; Wed, 19 Feb 2020 01:44:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C057124654 for ; Wed, 19 Feb 2020 01:44:52 +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="grpW1aOc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C057124654 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 2D1FC6B000C; Tue, 18 Feb 2020 20:44:48 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 282936B000D; Tue, 18 Feb 2020 20:44:48 -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 1984E6B000E; Tue, 18 Feb 2020 20:44:48 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0118.hostedemail.com [216.40.44.118]) by kanga.kvack.org (Postfix) with ESMTP id 03A616B000C for ; Tue, 18 Feb 2020 20:44:47 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id AC2091EF1 for ; Wed, 19 Feb 2020 01:44:47 +0000 (UTC) X-FDA: 76505182614.04.rain21_63fb2e553aa36 X-Spam-Summary: 2,0,0,0e4957370cae1045,d41d8cd98f00b204,minchan.kim@gmail.com,:akpm@linux-foundation.org:linux-kernel@vger.kernel.org::linux-api@vger.kernel.org:oleksandr@redhat.com:surenb@google.com:timmurray@google.com:dancol@google.com:sspatil@google.com:sonnyrao@google.com:bgeffon@google.com:mhocko@suse.com:hannes@cmpxchg.org:shakeelb@google.com:joaodias@google.com:joel@joelfernandes.org:sj38.park@gmail.com:alexander.h.duyck@linux.intel.com:jannh@google.com:minchan@kernel.org:christian@brauner.io:ktkhai@virtuozzo.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:6743:7903:8603:9025:10004:11026:11473:11658:11914:12043:12291:12296:12297:12438:12517:12519:12555:12683:12895:12986: 13161:13 X-HE-Tag: rain21_63fb2e553aa36 X-Filterd-Recvd-Size: 7105 Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Wed, 19 Feb 2020 01:44:46 +0000 (UTC) Received: by mail-pj1-f65.google.com with SMTP id gv17so1825385pjb.1 for ; Tue, 18 Feb 2020 17:44:46 -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=S6I7FkbWENtipd9zPITUUz5qzwuADyMhSE4JPk/5aKo=; b=grpW1aOcWY7IHSfhWkaQekrHm6oGkQJgAbcYNwHWWr8RBl8yhAq7SWaWmfi9gFhATr 7WszFDcQhbjWb2XWYhUE6Iw0vro+RRY0MiqfowH/YpmjfpbbiLMQQMKCqURY3XS80SFW DEcTlHoQDPpBLtE5WA6aRVkzBDDyXmQt9wU2OrYaGJPFgNpU0vrRRHNr/Q9wPhIbCTHA lHxnnz0SMDaqyThdCgCiYimxOspulO3xIsDK06unf7EefYUcpQOqE+brGSsec/s2VmvY r0JB2w3NHhYOWizQgKT7ddX7eLIecrq+HG90y9k4bu0zm9NaKFWs04ufT7Nd8cwb0nUB giiQ== 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=S6I7FkbWENtipd9zPITUUz5qzwuADyMhSE4JPk/5aKo=; b=sU5MQrHcbUWCQ+hkdPHUzJtkjLuyzTPP/qyV8bjNU9++79sYj9l9FP81xfY6mS80jQ nde2YqkAwv4lMACMFkCu0U7OvkZAXGUR9axESlFgfZoH83b3GFH8/uXYoG4UVP6mgmG4 HZ9rrcQCtw/duf14hCImUI7nTkUCDsEE7cr/JMqP3Bp6lqPLMsleTu9ztUqhsn6r4VGn mNm/9doreAeZnclmLNPYAsj5P0jrj8Py+8lmvqIuIMKscP9642mF7H8O7CxZIrcaS09y jZ2/aT5rehAMtQFaABGt2uokxEDrBElWI7SITa+8cW+nXEhtfIYlVMiKH6kUy7xlYXLa Hm5Q== X-Gm-Message-State: APjAAAXeLmTtzpV/raOnt1MwJSR/uxhIf06fJKo8jOKv7OIcFWmU+15Y tlAACnw+bx70hvV0D7n0FQg= X-Google-Smtp-Source: APXvYqyWYkd0WFT38bEZICS0jffyP4aaTQq2M2uUcJYdPWz+zOIDDDnU29CSI021/GJpxVHswXHbuw== X-Received: by 2002:a17:902:462:: with SMTP id 89mr23938662ple.270.1582076686098; Tue, 18 Feb 2020 17:44:46 -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 t186sm221192pgd.26.2020.02.18.17.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Feb 2020 17:44:45 -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 , sj38.park@gmail.com, alexander.h.duyck@linux.intel.com, Jann Horn , Minchan Kim , Christian Brauner , Kirill Tkhai Subject: [PATCH v6 5/7] mm: support both pid and pidfd for process_madvise Date: Tue, 18 Feb 2020 17:44:31 -0800 Message-Id: <20200219014433.88424-6-minchan@kernel.org> X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog In-Reply-To: <20200219014433.88424-1-minchan@kernel.org> References: <20200219014433.88424-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 Reviewed-by: Suren Baghdasaryan --- 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 def1507c2030..f6d9b9e66243 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1182,11 +1182,10 @@ SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) return do_madvise(current, current->mm, 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; @@ -1197,20 +1196,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); @@ -1223,7 +1233,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; }