From patchwork Sat May 16 01:20:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11553219 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 D0453912 for ; Sat, 16 May 2020 01:21:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 940E620756 for ; Sat, 16 May 2020 01:21:03 +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="fwB8tR/B" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 940E620756 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 B38CE8E0003; Fri, 15 May 2020 21:21:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id AEA168E0001; Fri, 15 May 2020 21:21:02 -0400 (EDT) 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 9D84C8E0003; Fri, 15 May 2020 21:21:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 850CB8E0001 for ; Fri, 15 May 2020 21:21:02 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 37F148248047 for ; Sat, 16 May 2020 01:21:02 +0000 (UTC) X-FDA: 76820828364.28.month92_9018ac87c316 X-Spam-Summary: 2,0,0,11a380c199c80859,d41d8cd98f00b204,minchan.kim@gmail.com,,RULES_HIT:2:41:69:355:379:541:800:960:966:967:973:982:988:989:1260:1311:1314:1345:1437:1515:1535:1605:1606:1730:1747:1777:1792:2196:2199:2393:2525:2553:2559:2564:2682:2685:2859:2892:2901:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4119:4250:4321:4385:4605:5007:6261:6653:6742:6743:7903:8603:8660:8784:9025:9121:9592:10004:11026:11233:11473:11658:11914:12043:12296:12297:12438:12517:12519:12555:12683:12895:12986:13148:13230:13894:14096:14394:21080:21212:21324:21444:21451:21627:21749:21789:21795:21811:21990:30003:30034:30051:30054:30070:30075:30080:30090,0,RBL:209.85.216.67:@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:23,LUA_SUMMARY:none X-HE-Tag: month92_9018ac87c316 X-Filterd-Recvd-Size: 8997 Received: from mail-pj1-f67.google.com (mail-pj1-f67.google.com [209.85.216.67]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Sat, 16 May 2020 01:21:01 +0000 (UTC) Received: by mail-pj1-f67.google.com with SMTP id t40so1776247pjb.3 for ; Fri, 15 May 2020 18:21:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=eD8nSEX9FeEfFYFcx1sY+GdGn8YPwFSgWFRNwl0px7U=; b=fwB8tR/Bt20pITmsd0QARLZNQnS8xckhvPsIJziPVT/Sn230jQib78WGwDeCpaW10Y KCgSDnjQvCRfC9RAI1OSumIjP9IDX5v11MmVPBvIUwmCKG7G9nvdV54UVIDrK6+oWfpL YlXxdYC8PgRRzD5a5iFtCxiDyXmMeta57/Bjqg64gcE3IP65HSbaslPzkWSwZi8n5fcx MG59wSAr7Wf0c0UVSildzKqkcTxViOIw8hi8joSpxdHAFmnpTGb462qrE2xl1PFGJ9do jBReA+yk0BAG5z7WXcCtGsIy43FMq+/LSOq//rLHmdkSmkRk8nZ65KkUmdq5XlsWTW58 nrYQ== 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 :mime-version:content-transfer-encoding; bh=eD8nSEX9FeEfFYFcx1sY+GdGn8YPwFSgWFRNwl0px7U=; b=QGmRkrN4Cnpfj+qcSZkQIzAdhZPnvDP+PYJ2nttALDP8879Tfv2hKdS/vS2J9O+Kqj loWjyftNQdbjGm2qVTaXzVq1kiBBxjz3HZ+zhSrhCK/2ybKCsPeLihaDt0VJeVB+ZRhS sFMFEEB4Pk1FUP5xaqtyh9/mc9vxXEhSGB9JpnUOC3w2S+H7If+ntAQYJ5mC2RELOSH2 HLH7d6IMyoSftruJcYyL0KYqh2q8OlFYfzkjeRIA1EA7s28JLk+Xm3Lw2DT76OjV+0Cc U6Cmm/O4f39l+qSlBE+neWfYoYqXxCOZbEaJgC/5BcAH1+NzU5m49rG2XVz2ofBiyy0s GqVw== X-Gm-Message-State: AOAM532YIC7mSaJ6S9KJrC99mlX8bNJ516F6oNLC8DmdP1BhRA8nE0bl CdvYEiv9ST5+ffTctolTvPc= X-Google-Smtp-Source: ABdhPJxaQ4eukrKWAZXgD0uhfLUXE8sPXxhov8YiL7z2w5J5Emj5+A0V94NHNrxEQhsUsDYmR1lKGQ== X-Received: by 2002:a17:90b:1004:: with SMTP id gm4mr6722768pjb.35.1589592060414; Fri, 15 May 2020 18:21:00 -0700 (PDT) Received: from bbox-1.mtv.corp.google.com ([2620:15c:211:1:3e01:2939:5992:52da]) by smtp.gmail.com with ESMTPSA id ep10sm104530pjb.21.2020.05.15.18.20.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 May 2020 18:20:59 -0700 (PDT) From: Minchan Kim To: Andrew Morton Cc: LKML , Christian Brauner , 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 , Jann Horn , alexander.h.duyck@linux.intel.com, SeongJae Park , David Rientjes , Arjun Roy , Kirill Tkhai , Minchan Kim Subject: [PATCH] mm: use only pidfd for process_madvise syscall Date: Fri, 15 May 2020 18:20:55 -0700 Message-Id: <20200516012055.126205-1-minchan@kernel.org> X-Mailer: git-send-email 2.26.2.761.g0e0b3e54be-goog 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: Based on discussion[1], people didn't feel we need to support both pid and pidfd for every new coming API[2] so this patch keeps only pidfd. This patch also changes flags's type with "unsigned int". So finally, the API is as follows, ssize_t process_madvise(int pidfd, const struct iovec *iovec, unsigned long vlen, int advice, unsigned int flags); DESCRIPTION The process_madvise() system call is used to give advice or directions to the kernel about the address ranges from external process as well as local process. It provides the advice to address ranges of process described by iovec and vlen. The goal of such advice is to improve system or application performance. The pidfd selects the process referred to by the PID file descriptor specified in pidfd. (See pidofd_open(2) for further information) The pointer iovec points to an array of iovec structures, defined in as: struct iovec { void *iov_base; /* starting address */ size_t iov_len; /* number of bytes to be advised */ }; The iovec describes address ranges beginning at address(iov_base) and with size length of bytes(iov_len). The vlen represents the number of elements in iovec. The advice is indicated in the advice argument, which is one of the following at this moment if the target process specified by idtype and id is external. MADV_COLD MADV_PAGEOUT MADV_MERGEABLE MADV_UNMERGEABLE Permission to provide a hint to external process is governed by a ptrace access mode PTRACE_MODE_ATTACH_FSCREDS check; see ptrace(2). The process_madvise supports every advice madvise(2) has if target process is in same thread group with calling process so user could use process_madvise(2) to extend existing madvise(2) to support vector address ranges. RETURN VALUE On success, process_madvise() returns the number of bytes advised. This return value may be less than the total number of requested bytes, if an error occurred. The caller should check return value to determine whether a partial advice occurred. [1] https://lore.kernel.org/linux-mm/20200509124817.xmrvsrq3mla6b76k@wittgenstein/ [2] https://lore.kernel.org/linux-mm/9d849087-3359-c4ab-fbec-859e8186c509@virtuozzo.com/ Signed-off-by: Minchan Kim Reviewed-by: Suren Baghdasaryan Reviewed-by: Suren Baghdasaryan Signed-off-by: Minchan Kim Reviewed-by: Suren Baghdasaryan Signed-off-by: Minchan Kim Signed-off-by: Christian Brauner Signed-off-by: Minchan Kim --- mm/madvise.c | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index d3fbbe52d230..35c9b220146a 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1229,8 +1229,8 @@ static int process_madvise_vec(struct task_struct *target_task, return ret; } -static ssize_t do_process_madvise(int which, pid_t upid, struct iov_iter *iter, - int behavior, unsigned long flags) +static ssize_t do_process_madvise(int pidfd, struct iov_iter *iter, + int behavior, unsigned int flags) { ssize_t ret; struct pid *pid; @@ -1241,26 +1241,12 @@ static ssize_t do_process_madvise(int which, pid_t upid, struct iov_iter *iter, if (flags != 0) return -EINVAL; - 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_get_pid(upid); - if (IS_ERR(pid)) - return PTR_ERR(pid); - break; - default: + if (pidfd < 0) return -EINVAL; - } + + pid = pidfd_get_pid(pidfd); + if (IS_ERR(pid)) + return PTR_ERR(pid); task = get_pid_task(pid, PIDTYPE_PID); if (!task) { @@ -1292,9 +1278,8 @@ static ssize_t do_process_madvise(int which, pid_t upid, struct iov_iter *iter, return ret; } -SYSCALL_DEFINE6(process_madvise, int, which, pid_t, upid, - const struct iovec __user *, vec, unsigned long, vlen, - int, behavior, unsigned long, flags) +SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, + unsigned long, vlen, int, behavior, unsigned int, flags) { ssize_t ret; struct iovec iovstack[UIO_FASTIOV]; @@ -1303,19 +1288,18 @@ SYSCALL_DEFINE6(process_madvise, int, which, pid_t, upid, ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter); if (ret >= 0) { - ret = do_process_madvise(which, upid, &iter, behavior, flags); + ret = do_process_madvise(pidfd, &iter, behavior, flags); kfree(iov); } return ret; } #ifdef CONFIG_COMPAT -COMPAT_SYSCALL_DEFINE6(process_madvise, compat_int_t, which, - compat_pid_t, upid, +COMPAT_SYSCALL_DEFINE5(process_madvise, compat_int_t, pidfd, const struct compat_iovec __user *, vec, compat_ulong_t, vlen, compat_int_t, behavior, - compat_ulong_t, flags) + compat_int_t, flags) { ssize_t ret; @@ -1326,7 +1310,7 @@ COMPAT_SYSCALL_DEFINE6(process_madvise, compat_int_t, which, ret = compat_import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter); if (ret >= 0) { - ret = do_process_madvise(which, upid, &iter, behavior, flags); + ret = do_process_madvise(pidfd, &iter, behavior, flags); kfree(iov); } return ret;