From patchwork Wed Feb 12 23:39:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11379357 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 DD8DB921 for ; Wed, 12 Feb 2020 23:39:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9C52A20578 for ; Wed, 12 Feb 2020 23:39:55 +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="JS+hQMpj" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9C52A20578 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 2B3D16B04CB; Wed, 12 Feb 2020 18:39:54 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 217B16B04CD; Wed, 12 Feb 2020 18:39:54 -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 08F856B04CE; Wed, 12 Feb 2020 18:39:53 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0182.hostedemail.com [216.40.44.182]) by kanga.kvack.org (Postfix) with ESMTP id D9AE56B04CB for ; Wed, 12 Feb 2020 18:39:53 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 935831EF3 for ; Wed, 12 Feb 2020 23:39:53 +0000 (UTC) X-FDA: 76483095066.13.woman78_73687d396ae0d X-Spam-Summary: 2,0,0,9d055a0564571928,d41d8cd98f00b204,minchan.kim@gmail.com,,RULES_HIT:2:41:69:355:379:541:800:960:966:968:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1605:1730:1747:1777:1792:2196:2199:2376:2393:2559:2562:2693:3138:3139:3140:3141:3142:3865:3866:3868:3870:3871:3872:3874:4049:4120:4321:4385:4605:5007:6261:6653:6742:7875:7903:9592:10004:11026:11473:11658:11914:12043:12291:12296:12297:12438:12517:12519:12555:12679:12683:12895:13894:14096:14394:21080:21324:21433:21444:21451:21611:21627:21796:21987:21990:30036:30054:30070,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:23,LUA_SUMMARY:none X-HE-Tag: woman78_73687d396ae0d X-Filterd-Recvd-Size: 9795 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Feb 2020 23:39:52 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id b9so1895447pgk.12 for ; Wed, 12 Feb 2020 15:39:52 -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=CaESisSBxNPE2doLDk535d6lHmOCff9MNldO17oRtwM=; b=JS+hQMpjCiSL/bIcob819JXcAFntb6DAajrrH2XpwlMNN6RJhPqFesgZv9GtSgnfoB aUKzPlRpzQU80yQftWr2vGL/FZdbBys3oDgZXPHnsoqgaJ2VEWPw8op4HsMyuBddKH/q B8vHpU/IxHuFiOqwXizS1lpENgPDFdB7hfFPhSwCXTMSKwXI8o5cYcO/SFSu+rufyWep //bioNwQy39ikfsAvjCe+X9yT2lNBl8bpy22qfMdminq2e0AQlUqvnV6Mn/S81lmkwGG ayz7J/S53s8PWQyY7fJ7vkmG7TDqoWt+keVJFHkOS4fyvDuMZO6vvM6vrbHIh9Nk5AJF 1nxw== 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=CaESisSBxNPE2doLDk535d6lHmOCff9MNldO17oRtwM=; b=DHHnU/sqz8/ZwwHvxPSKtRiXh77/rti0J2tSaEBGaAgpZjuaMJgMwMrQ5J3yTwByXD uVUZaX2udBGOhDiKfeDaiV1WPWJzy3vIbI8oYrV6YLiqGALdrgKFl2fz0KSj10PtJmVY I/9lJx2MKpLIGSzXjUPIDjWOA2ECloRop3/z+ecg2mPgbdDeH+imGszjqL8TX7DOwdvy ax6kLwTgDXVjiu2uhZapl15f5y4cRUqEZBl4XjPcElgje4UhvJi8op+eHAd4RiKLOWpJ JparHrIoxgT3qN9CoksTuXdGChW4fm6n2cRhqfQV0jPnCQ7OfWqvlKOzC2994yFNdirK KqSA== X-Gm-Message-State: APjAAAXyJZjntsgc5FKiiKwZaoM57oI/45jCUs4Vy6Z6RnsMBrDRAF0/ sRi/GtrD8NH7e9CAAwZMKKrfEBNT X-Google-Smtp-Source: APXvYqyUCxZWmHJ5S2Cx4UhfOoURZcoQU6tgdwR9R+D7NvOkBNmrWtkBNpJggLo/YZLejL+o9stwDA== X-Received: by 2002:aa7:85d9:: with SMTP id z25mr11119876pfn.223.1581550792005; Wed, 12 Feb 2020 15:39:52 -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.39.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 15:39:50 -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 Subject: [PATCH v4 1/8] mm: pass task to do_madvise Date: Wed, 12 Feb 2020 15:39:39 -0800 Message-Id: <20200212233946.246210-2-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: In upcoming patches, do_madvise will be called from external process context so it shouldn't asssume "current" is always hinted process's task_struct. Thus, let's get the mm_struct from vma->vm_mm, not current because vma is always hinted process's one. And let's pass *current* as new task argument of do_madvise so it shouldn't change existing behavior. Signed-off-by: Minchan Kim --- fs/io_uring.c | 2 +- include/linux/mm.h | 3 ++- mm/madvise.c | 37 ++++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 63beda9bafc5..6307206b970f 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -2736,7 +2736,7 @@ static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, if (force_nonblock) return -EAGAIN; - ret = do_madvise(ma->addr, ma->len, ma->advice); + ret = do_madvise(current, ma->addr, ma->len, ma->advice); if (ret < 0) req_set_fail_links(req); io_cqring_add_event(req, ret); diff --git a/include/linux/mm.h b/include/linux/mm.h index 52269e56c514..8cb41131ec96 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2323,7 +2323,8 @@ extern int __do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf, bool downgrade); extern int do_munmap(struct mm_struct *, unsigned long, size_t, struct list_head *uf); -extern int do_madvise(unsigned long start, size_t len_in, int behavior); +extern int do_madvise(struct task_struct *task, unsigned long start, + size_t len_in, int behavior); static inline unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, diff --git a/mm/madvise.c b/mm/madvise.c index 43b47d3fae02..ab4011ba2d9e 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -256,6 +256,7 @@ static long madvise_willneed(struct vm_area_struct *vma, { struct file *file = vma->vm_file; loff_t offset; + struct mm_struct *mm = vma->vm_mm; *prev = vma; #ifdef CONFIG_SWAP @@ -288,12 +289,12 @@ static long madvise_willneed(struct vm_area_struct *vma, */ *prev = NULL; /* tell sys_madvise we drop mmap_sem */ get_file(file); - up_read(¤t->mm->mmap_sem); + up_read(&mm->mmap_sem); offset = (loff_t)(start - vma->vm_start) + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED); fput(file); - down_read(¤t->mm->mmap_sem); + down_read(&mm->mmap_sem); return 0; } @@ -674,9 +675,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, } out: if (nr_swap) { - if (current->mm == mm) - sync_mm_rss(mm); - + sync_mm_rss(mm); add_mm_counter(mm, MM_SWAPENTS, nr_swap); } arch_leave_lazy_mmu_mode(); @@ -756,6 +755,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, unsigned long start, unsigned long end, int behavior) { + struct mm_struct *mm = vma->vm_mm; *prev = vma; if (!can_madv_lru_vma(vma)) return -EINVAL; @@ -763,8 +763,8 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, if (!userfaultfd_remove(vma, start, end)) { *prev = NULL; /* mmap_sem has been dropped, prev is stale */ - down_read(¤t->mm->mmap_sem); - vma = find_vma(current->mm, start); + down_read(&mm->mmap_sem); + vma = find_vma(mm, start); if (!vma) return -ENOMEM; if (start < vma->vm_start) { @@ -818,6 +818,7 @@ static long madvise_remove(struct vm_area_struct *vma, loff_t offset; int error; struct file *f; + struct mm_struct *mm = vma->vm_mm; *prev = NULL; /* tell sys_madvise we drop mmap_sem */ @@ -845,13 +846,13 @@ static long madvise_remove(struct vm_area_struct *vma, get_file(f); if (userfaultfd_remove(vma, start, end)) { /* mmap_sem was not released by userfaultfd_remove() */ - up_read(¤t->mm->mmap_sem); + up_read(&mm->mmap_sem); } error = vfs_fallocate(f, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, end - start); fput(f); - down_read(¤t->mm->mmap_sem); + down_read(&mm->mmap_sem); return error; } @@ -1044,7 +1045,8 @@ madvise_behavior_valid(int behavior) * -EBADF - map exists, but area maps something that isn't a file. * -EAGAIN - a kernel resource was temporarily unavailable. */ -int do_madvise(unsigned long start, size_t len_in, int behavior) +int do_madvise(struct task_struct *task, unsigned long start, + size_t len_in, int behavior) { unsigned long end, tmp; struct vm_area_struct *vma, *prev; @@ -1053,6 +1055,7 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) int write; size_t len; struct blk_plug plug; + struct mm_struct *mm = task->mm; start = untagged_addr(start); @@ -1082,10 +1085,10 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) write = madvise_need_mmap_write(behavior); if (write) { - if (down_write_killable(¤t->mm->mmap_sem)) + if (down_write_killable(&mm->mmap_sem)) return -EINTR; } else { - down_read(¤t->mm->mmap_sem); + down_read(&mm->mmap_sem); } /* @@ -1093,7 +1096,7 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) * ranges, just ignore them, but return -ENOMEM at the end. * - different from the way of handling in mlock etc. */ - vma = find_vma_prev(current->mm, start, &prev); + vma = find_vma_prev(mm, start, &prev); if (vma && start > vma->vm_start) prev = vma; @@ -1130,19 +1133,19 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) if (prev) vma = prev->vm_next; else /* madvise_remove dropped mmap_sem */ - vma = find_vma(current->mm, start); + vma = find_vma(mm, start); } out: blk_finish_plug(&plug); if (write) - up_write(¤t->mm->mmap_sem); + up_write(&mm->mmap_sem); else - up_read(¤t->mm->mmap_sem); + up_read(&mm->mmap_sem); return error; } SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior) { - return do_madvise(start, len_in, behavior); + return do_madvise(current, start, len_in, behavior); } From patchwork Wed Feb 12 23:39:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11379359 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 B6B3C139A for ; Wed, 12 Feb 2020 23:39:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5CA012168B for ; Wed, 12 Feb 2020 23:39:58 +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="MqwnV4ex" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5CA012168B 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 28CF36B04CD; Wed, 12 Feb 2020 18:39:57 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 216DA6B04CF; Wed, 12 Feb 2020 18:39:57 -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 01B2D6B04D0; Wed, 12 Feb 2020 18:39:56 -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 D3C6F6B04CD for ; Wed, 12 Feb 2020 18:39:56 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 88BF7180AD80F for ; Wed, 12 Feb 2020 23:39:56 +0000 (UTC) X-FDA: 76483095192.18.wave24_73c45b03fde06 X-Spam-Summary: 2,0,0,5686e936ede16eeb,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:alexander.h.duyck@linux.intel.com:minchan@kernel.org,RULES_HIT:41:327:355:379:541:800:960:966:967:973:988:989:1042:1260:1311:1314:1345:1437:1515:1605:1730:1747:1777:1792:1801:2194:2196:2198:2199:2200:2201:2393:2525:2553:2559:2565:2570:2682:2685:2693:2703:2859:2892:2895:2896:2901:2911:2924:2925:2926:2933:2937:2939:2942:2945:2947:2951:2954:3022:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4031:4250:4321:4385:4425:4605:5007:6119:6261:7875:7903:7974:8603:8660:8957:9025:10004:11658:13141:13142:13148:13149:13161:13184:13229:13230,0,RBL:209.85.210.196 :@gmail. X-HE-Tag: wave24_73c45b03fde06 X-Filterd-Recvd-Size: 22631 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Feb 2020 23:39:55 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id k29so2000040pfp.13 for ; Wed, 12 Feb 2020 15:39:55 -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=6F6ZICo7z1kWqzeRepX+XfoBqt3QjP8xsR0NoiX3gAA=; b=MqwnV4ex5XfWJ7aIYpi0bbpR6YwpNyjTuavwvVBplqNGUQd4TFvPZJl2Fxalha9U0s kpv6Mlsz2v+563GLv2IVajI8cJWKwR6yazl4zK7uGIS6vz2zJ8snv4YDOBnL73764KzF Kfn4OGwJZd/rYIIwyiEceDt1wOjpvdja2CnYGFuf+wc6utoujoEaq1B14s3RGUi/Oo/f 855VmkKFEYlFQziTnZfFpqYXY+vnrD5cF8cw+iQAzIRFABNcbPli1JG4YZVd1RXDTc3W VFS9bF2WszrOh8dDGlqU+jdSPYn0wb29avDnMHlOqPfM1zWJJvgiNTW9y/wur2mDXwQk OoYw== 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=6F6ZICo7z1kWqzeRepX+XfoBqt3QjP8xsR0NoiX3gAA=; b=cqNHMwm4BBZHpxTHP2nVr9yEXoEZHNlikK3eR0J+ARW0RPwj4RpOcZeGqhRJ5fP1St 3P6wGFMx0WM9epxCCkVxFBAtzHswtZOAH96+rQXaYUe36EHmBCx0IBoGJ3IU92kRG3tB uke1GLw2lPmBeU+O7SltAWiK48duSfu0aWWjYKuVyKovS0R6sip6l792cW9HW1ET6UNM wBmV4VdV7qVzNnoVin3WYAw0q5q7CpdWcDPva/SBp9HccLsJLhimwNxSVhhvHcQVT3s0 LZ5dCAAtpb9AfOeSYWR/x1v+jQUx/NX+m82FubCdpzqBD2pPXPnSPZ2K33lzToY6CdAp JUDg== X-Gm-Message-State: APjAAAXQqZhTIEwV/seCi082riH1CUCvnvKJV/2znhunzmwyEtXN9ynf K5gctW2pc/XINNDR9ZFKG1yUztXr X-Google-Smtp-Source: APXvYqy2LFVNnzEYzlxL33b6Cq8mjbCYX8VHQZa0vtNlb8+2IMf8UJoLtyTZl66Uit7Fj2SDm/EiQg== X-Received: by 2002:a63:2a02:: with SMTP id q2mr14564349pgq.198.1581550794209; Wed, 12 Feb 2020 15:39:54 -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.39.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 15:39:53 -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 Subject: [PATCH v4 2/8] mm: introduce external memory hinting API Date: Wed, 12 Feb 2020 15:39:40 -0800 Message-Id: <20200212233946.246210-3-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 usecase that System Management Software(SMS) want to give a memory hint like MADV_[COLD|PAGEEOUT] to other processes and in the case of Android, it is the ActivityManagerService. It's similar in spirit to madvise(MADV_WONTNEED), but the information required to make the reclaim decision is not known to the app. Instead, it is known to the centralized userspace daemon(ActivityManagerService), and that daemon must be able to initiate reclaim on its own without any app involvement. To solve the issue, this patch introduces a new syscall process_madvise(2). It uses pidfd of an external process to give the hint. int process_madvise(int pidfd, void *addr, size_t length, int advise, unsigned long flag); Since it could affect other process's address range, only privileged process(CAP_SYS_PTRACE) or something else(e.g., being the same UID) gives it the right to ptrace the process could use it successfully. The flag argument is reserved for future use if we need to extend the API. I think supporting all hints madvise has/will supported/support to process_madvise is rather risky. Because we are not sure all hints make sense from external process and implementation for the hint may rely on the caller being in the current context so it could be error-prone. Thus, I just limited hints as MADV_[COLD|PAGEOUT] in this patch. If someone want to add other hints, we could hear hear the usecase and review it for each hint. It's safer for maintenance rather than introducing a buggy syscall but hard to fix it later. Q.1 - Why does any external entity have better knowledge? Quote from Sandeep "For Android, every application (including the special SystemServer) are forked from Zygote. The reason of course is to share as many libraries and classes between the two as possible to benefit from the preloading during boot. After applications start, (almost) all of the APIs end up calling into this SystemServer process over IPC (binder) and back to the application. In a fully running system, the SystemServer monitors every single process periodically to calculate their PSS / RSS and also decides which process is "important" to the user for interactivity. So, because of how these processes start _and_ the fact that the SystemServer is looping to monitor each process, it does tend to *know* which address range of the application is not used / useful. Besides, we can never rely on applications to clean things up themselves. We've had the "hey app1, the system is low on memory, please trim your memory usage down" notifications for a long time[1]. They rely on applications honoring the broadcasts and very few do. So, if we want to avoid the inevitable killing of the application and restarting it, some way to be able to tell the OS about unimportant memory in these applications will be useful. - ssp Q.2 - How to guarantee the race(i.e., object validation) between when giving a hint from an external process and get the hint from the target process? process_madvise operates on the target process's address space as it exists at the instant that process_madvise is called. If the space target process can run between the time the process_madvise process inspects the target process address space and the time that process_madvise is actually called, process_madvise may operate on memory regions that the calling process does not expect. It's the responsibility of the process calling process_madvise to close this race condition. For example, the calling process can suspend the target process with ptrace, SIGSTOP, or the freezer cgroup so that it doesn't have an opportunity to change its own address space before process_madvise is called. Another option is to operate on memory regions that the caller knows a priori will be unchanged in the target process. Yet another option is to accept the race for certain process_madvise calls after reasoning that mistargeting will do no harm. The suggested API itself does not provide synchronization. It also apply other APIs like move_pages, process_vm_write. The race isn't really a problem though. Why is it so wrong to require that callers do their own synchronization in some manner? Nobody objects to write(2) merely because it's possible for two processes to open the same file and clobber each other's writes --- instead, we tell people to use flock or something. Think about mmap. It never guarantees newly allocated address space is still valid when the user tries to access it because other threads could unmap the memory right before. That's where we need synchronization by using other API or design from userside. It shouldn't be part of API itself. If someone needs more fine-grained synchronization rather than process level, there were two ideas suggested - cookie[2] and anon-fd[3]. Both are applicable via using last reserved argument of the API but I don't think it's necessary right now since we have already ways to prevent the race so don't want to add additional complexity with more fine-grained optimization model. To make the API extend, it reserved an unsigned long as last argument so we could support it in future if someone really needs it. Q.3 - Why doesn't ptrace work? Injecting an madvise in the target process using ptrace would not work for us because such injected madvise would have to be executed by the target process, which means that process would have to be runnable and that creates the risk of the abovementioned race and hinting a wrong VMA. Furthermore, we want to act the hint in caller's context, not calle because calle is usually limited in cpuset/cgroups or even freezed state so they can't act by themselves quick enough, which causes more thrashing/kill. It doesn't work if the target process are ptraced(e.g., strace, debugger, minidump) because a process can have at most one ptracer. [1] https://developer.android.com/topic/performance/memory" [2] process_getinfo for getting the cookie which is updated whenever vma of process address layout are changed - Daniel Colascione - https://lore.kernel.org/lkml/20190520035254.57579-1-minchan@kernel.org/T/#m7694416fd179b2066a2c62b5b139b14e3894e224 [3] anonymous fd which is used for the object(i.e., address range) validation - Michal Hocko - https://lore.kernel.org/lkml/20200120112722.GY18451@dhcp22.suse.cz/ Signed-off-by: Minchan Kim --- arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 2 + arch/ia64/kernel/syscalls/syscall.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + include/linux/syscalls.h | 2 + include/uapi/asm-generic/unistd.h | 4 +- kernel/sys_ni.c | 1 + mm/madvise.c | 64 +++++++++++++++++++++ 21 files changed, 88 insertions(+), 2 deletions(-) diff --git a/arch/alpha/kernel/syscalls/syscall.tbl b/arch/alpha/kernel/syscalls/syscall.tbl index 36d42da7466a..c82952e6fb80 100644 --- a/arch/alpha/kernel/syscalls/syscall.tbl +++ b/arch/alpha/kernel/syscalls/syscall.tbl @@ -477,3 +477,4 @@ # 545 reserved for clone3 547 common openat2 sys_openat2 548 common pidfd_getfd sys_pidfd_getfd +549 common process_madvise sys_process_madvise diff --git a/arch/arm/tools/syscall.tbl b/arch/arm/tools/syscall.tbl index 4d1cf74a2caa..54c2719fec46 100644 --- a/arch/arm/tools/syscall.tbl +++ b/arch/arm/tools/syscall.tbl @@ -451,3 +451,4 @@ 435 common clone3 sys_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/arch/arm64/include/asm/unistd.h b/arch/arm64/include/asm/unistd.h index 1dd22da1c3a9..75f04a1023be 100644 --- a/arch/arm64/include/asm/unistd.h +++ b/arch/arm64/include/asm/unistd.h @@ -38,7 +38,7 @@ #define __ARM_NR_compat_set_tls (__ARM_NR_COMPAT_BASE + 5) #define __ARM_NR_COMPAT_END (__ARM_NR_COMPAT_BASE + 0x800) -#define __NR_compat_syscalls 439 +#define __NR_compat_syscalls 440 #endif #define __ARCH_WANT_SYS_CLONE diff --git a/arch/arm64/include/asm/unistd32.h b/arch/arm64/include/asm/unistd32.h index c1c61635f89c..2a27be7a1f91 100644 --- a/arch/arm64/include/asm/unistd32.h +++ b/arch/arm64/include/asm/unistd32.h @@ -883,6 +883,8 @@ __SYSCALL(__NR_clone3, sys_clone3) __SYSCALL(__NR_openat2, sys_openat2) #define __NR_pidfd_getfd 438 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd) +#define __NR_process_madvise 439 +__SYSCALL(__NR_process_madvise, process_madvise) /* * Please add new compat syscalls above this comment and update diff --git a/arch/ia64/kernel/syscalls/syscall.tbl b/arch/ia64/kernel/syscalls/syscall.tbl index 042911e670b8..9524af1c318c 100644 --- a/arch/ia64/kernel/syscalls/syscall.tbl +++ b/arch/ia64/kernel/syscalls/syscall.tbl @@ -358,3 +358,4 @@ # 435 reserved for clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/arch/m68k/kernel/syscalls/syscall.tbl b/arch/m68k/kernel/syscalls/syscall.tbl index f4f49fcb76d0..8197050c097c 100644 --- a/arch/m68k/kernel/syscalls/syscall.tbl +++ b/arch/m68k/kernel/syscalls/syscall.tbl @@ -437,3 +437,4 @@ 435 common clone3 __sys_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/arch/microblaze/kernel/syscalls/syscall.tbl b/arch/microblaze/kernel/syscalls/syscall.tbl index 4c67b11f9c9e..c5b6c8afe445 100644 --- a/arch/microblaze/kernel/syscalls/syscall.tbl +++ b/arch/microblaze/kernel/syscalls/syscall.tbl @@ -443,3 +443,4 @@ 435 common clone3 sys_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/arch/mips/kernel/syscalls/syscall_n32.tbl b/arch/mips/kernel/syscalls/syscall_n32.tbl index 1f9e8ad636cc..8ec8c558aa9c 100644 --- a/arch/mips/kernel/syscalls/syscall_n32.tbl +++ b/arch/mips/kernel/syscalls/syscall_n32.tbl @@ -376,3 +376,4 @@ 435 n32 clone3 __sys_clone3 437 n32 openat2 sys_openat2 438 n32 pidfd_getfd sys_pidfd_getfd +439 n32 process_madvise sys_process_madvise diff --git a/arch/mips/kernel/syscalls/syscall_n64.tbl b/arch/mips/kernel/syscalls/syscall_n64.tbl index c0b9d802dbf6..0078f891bb92 100644 --- a/arch/mips/kernel/syscalls/syscall_n64.tbl +++ b/arch/mips/kernel/syscalls/syscall_n64.tbl @@ -352,3 +352,4 @@ 435 n64 clone3 __sys_clone3 437 n64 openat2 sys_openat2 438 n64 pidfd_getfd sys_pidfd_getfd +439 n64 process_madvise sys_process_madvise diff --git a/arch/parisc/kernel/syscalls/syscall.tbl b/arch/parisc/kernel/syscalls/syscall.tbl index 52a15f5cd130..09c3b5dc6855 100644 --- a/arch/parisc/kernel/syscalls/syscall.tbl +++ b/arch/parisc/kernel/syscalls/syscall.tbl @@ -435,3 +435,4 @@ 435 common clone3 sys_clone3_wrapper 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/arch/powerpc/kernel/syscalls/syscall.tbl b/arch/powerpc/kernel/syscalls/syscall.tbl index 35b61bfc1b1a..97eac48c2937 100644 --- a/arch/powerpc/kernel/syscalls/syscall.tbl +++ b/arch/powerpc/kernel/syscalls/syscall.tbl @@ -519,3 +519,4 @@ 435 nospu clone3 ppc_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/arch/s390/kernel/syscalls/syscall.tbl b/arch/s390/kernel/syscalls/syscall.tbl index bd7bd3581a0f..8dc8bfd958ea 100644 --- a/arch/s390/kernel/syscalls/syscall.tbl +++ b/arch/s390/kernel/syscalls/syscall.tbl @@ -440,3 +440,4 @@ 435 common clone3 sys_clone3 sys_clone3 437 common openat2 sys_openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise sys_process_madvise diff --git a/arch/sh/kernel/syscalls/syscall.tbl b/arch/sh/kernel/syscalls/syscall.tbl index c7a30fcd135f..e69d98040777 100644 --- a/arch/sh/kernel/syscalls/syscall.tbl +++ b/arch/sh/kernel/syscalls/syscall.tbl @@ -440,3 +440,4 @@ # 435 reserved for clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/arch/sparc/kernel/syscalls/syscall.tbl b/arch/sparc/kernel/syscalls/syscall.tbl index f13615ecdecc..6f6e66dd51f9 100644 --- a/arch/sparc/kernel/syscalls/syscall.tbl +++ b/arch/sparc/kernel/syscalls/syscall.tbl @@ -483,3 +483,4 @@ # 435 reserved for clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscalls/syscall_32.tbl index c17cb77eb150..1b2184549e27 100644 --- a/arch/x86/entry/syscalls/syscall_32.tbl +++ b/arch/x86/entry/syscalls/syscall_32.tbl @@ -442,3 +442,4 @@ 435 i386 clone3 sys_clone3 __ia32_sys_clone3 437 i386 openat2 sys_openat2 __ia32_sys_openat2 438 i386 pidfd_getfd sys_pidfd_getfd __ia32_sys_pidfd_getfd +439 i386 process_madvise sys_process_madvise __ia32_sys_process_madvise diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl index 44d510bc9b78..82d60eb1e00d 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -359,6 +359,7 @@ 435 common clone3 __x64_sys_clone3/ptregs 437 common openat2 __x64_sys_openat2 438 common pidfd_getfd __x64_sys_pidfd_getfd +439 common process_madvise __x64_sys_process_madvise # # x32-specific system call numbers start at 512 to avoid cache impact diff --git a/arch/xtensa/kernel/syscalls/syscall.tbl b/arch/xtensa/kernel/syscalls/syscall.tbl index 85a9ab1bc04d..165cae047770 100644 --- a/arch/xtensa/kernel/syscalls/syscall.tbl +++ b/arch/xtensa/kernel/syscalls/syscall.tbl @@ -408,3 +408,4 @@ 435 common clone3 sys_clone3 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd +439 common process_madvise sys_process_madvise diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 1815065d52f3..e4cd2c2f8bb4 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -876,6 +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, + 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, unsigned long flags); diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h index 3a3201e4618e..85d8c9376a63 100644 --- a/include/uapi/asm-generic/unistd.h +++ b/include/uapi/asm-generic/unistd.h @@ -855,9 +855,11 @@ __SYSCALL(__NR_clone3, sys_clone3) __SYSCALL(__NR_openat2, sys_openat2) #define __NR_pidfd_getfd 438 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd) +#define __NR_pidfd_getfd 439 +__SYSCALL(__NR_process_madvise, sys_process_madvise) #undef __NR_syscalls -#define __NR_syscalls 439 +#define __NR_syscalls 440 /* * 32 bit systems traditionally used different diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index 3b69a560a7ac..6c7332776e8e 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -280,6 +280,7 @@ COND_SYSCALL(mlockall); COND_SYSCALL(munlockall); COND_SYSCALL(mincore); COND_SYSCALL(madvise); +COND_SYSCALL(process_madvise); COND_SYSCALL(remap_file_pages); COND_SYSCALL(mbind); COND_SYSCALL_COMPAT(mbind); diff --git a/mm/madvise.c b/mm/madvise.c index ab4011ba2d9e..8611f1d39289 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -984,6 +985,18 @@ madvise_behavior_valid(int behavior) } } +static bool +process_madvise_behavior_valid(int behavior) +{ + switch (behavior) { + case MADV_COLD: + case MADV_PAGEOUT: + return true; + default: + return false; + } +} + /* * The madvise(2) system call. * @@ -1031,6 +1044,11 @@ madvise_behavior_valid(int behavior) * MADV_DONTDUMP - the application wants to prevent pages in the given range * from being included in its core dump. * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump. + * MADV_COLD - the application uses the memory less so the kernel can + * deactivate the memory to evict them quickly when the memory + * pressure happen. + * MADV_PAGEOUT - the application uses the memroy very rarely so kernel can + * page out the memory instantly. * * return values: * zero - success @@ -1149,3 +1167,49 @@ 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, + 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; + + if (flags != 0) + return -EINVAL; + + if (!process_madvise_behavior_valid(behavior)) + return -EINVAL; + + f = fdget(pidfd); + if (!f.file) + return -EBADF; + + pid = pidfd_pid(f.file); + if (IS_ERR(pid)) { + ret = PTR_ERR(pid); + goto fdput; + } + + task = get_pid_task(pid, PIDTYPE_PID); + if (!task) { + ret = -ESRCH; + goto fdput; + } + + mm = mm_access(task, PTRACE_MODE_ATTACH_FSCREDS); + if (IS_ERR_OR_NULL(mm)) { + ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH; + goto release_task; + } + + ret = do_madvise(task, start, len_in, behavior); + mmput(mm); +release_task: + put_task_struct(task); +fdput: + fdput(f); + return ret; +} From patchwork Wed Feb 12 23:39:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11379361 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 2CB3C139A for ; Wed, 12 Feb 2020 23:40:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E42032168B for ; Wed, 12 Feb 2020 23:40:00 +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="qEAmDJpj" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E42032168B 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 B8F9E6B04CF; Wed, 12 Feb 2020 18:39:59 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B18326B04D1; Wed, 12 Feb 2020 18:39:59 -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 A05DC6B04D2; Wed, 12 Feb 2020 18:39:59 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0166.hostedemail.com [216.40.44.166]) by kanga.kvack.org (Postfix) with ESMTP id 82FED6B04CF for ; Wed, 12 Feb 2020 18:39:59 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3AA4E180AD80F for ; Wed, 12 Feb 2020 23:39:59 +0000 (UTC) X-FDA: 76483095318.24.leg68_743daa725482f X-Spam-Summary: 2,0,0,d9fa351297a1c719,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:alexander.h.duyck@linux.intel.com:minchan@kernel.org:oleg@redhat.com,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1359:1437:1515:1534:1540:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3352:3865:3868:3872:4321:5007:6261:6653:6742:7901:7903:10004:11658:11914:12296:12297:12517:12519:12555:12895:13069:13311:13357:13894:14096:14181:14384:14394:14721:21080:21444:21451:21627:21990:30054:30069,0,RBL:209.85.215.196:@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: X-HE-Tag: leg68_743daa725482f X-Filterd-Recvd-Size: 4384 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Feb 2020 23:39:58 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id l24so2042835pgk.2 for ; Wed, 12 Feb 2020 15:39:58 -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=5FaTihNB0iO6aVJlFQnliVUDjcHxSy/v5NwxGrH8kEI=; b=qEAmDJpjHq1/SDf6Xx5QnzwOEfkQzfaAAuSuJ0sR01YF0yYlVoCLxLOOwV+QGxC1A6 At4V57OeHDRLTUEM5vf09xAy2F5SvJ+57jVcDXKUcwcDgz1ol2m6zScXLJG7OoSu3BWF AsNMxbhzRVlrb5IdNChiSE0Z5R7G10ipo2dnSqTIuSy26l/Yc2Wca1lHimE+eQw7dL3a EFrmpjflcH51ALHlyIJ+xp3f9nuMS3HRz/Nsr/RFsucD8oolwMZuMwHYxV+TxNob5BwB ZmiX8UPIf3vovj+TMAhkkQCfjW1LXhJX+AyqoPTtIQCxX3ctHrDxmuSA3VhkddJQeDAj Gz1Q== 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=5FaTihNB0iO6aVJlFQnliVUDjcHxSy/v5NwxGrH8kEI=; b=DBbSt5OVUd66t0rQd7rD4xzoM4Vj/3Zn5RGktxvmrH0qMmOBLS70YMRXl2b23hPIuy pUR2AL0rS6RdQIfK/KfBVTJKCQqQKCH9pOYkIPSvrwebYrOeQWm7xTIYSdHg1iU0LJ0a GG/h8QXXTmhdRHX0oBm7Z940NbA8xYmPXyAFVV3nrwnk4r0WVTOyQjpRLN/Zemrcrs1u 9JTRDmsEYmc0gOKBmPWZFX3EPWsrwLs/gcDjyTOKTOG0HBqBCJknzi+lJK/Vj5gOUaMy fN/BsUoXoVt0ULJ6lTM73tNm/oxsIVP5YN1slgmhqh+XTRs/Jms4gze8hNzUZ0uMbIsN p7iQ== X-Gm-Message-State: APjAAAWVgf1aPt4SUrLVzMMwRq7lQhfgU6O9dNABl7CCtOPmoZSF845W QDqVTRRcIg5KvZxI92xu31Y= X-Google-Smtp-Source: APXvYqwUgswxPMoz82nKSTevATYCfitBRedBrE6cghsT9OKRaDZWTkOVO5apq4PKzc/j/CtkDUG47w== X-Received: by 2002:a63:3c08:: with SMTP id j8mr15003128pga.223.1581550797747; Wed, 12 Feb 2020 15:39:57 -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.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 15:39:56 -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 , Oleg Nesterov Subject: [PATCH v4 3/8] mm: validate mm in do_madvise Date: Wed, 12 Feb 2020 15:39:41 -0800 Message-Id: <20200212233946.246210-4-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: Oleg pointed out mm could be nulllified right after mm_access succeeds. This patch validates it before the using. Cc: Oleg Nesterov Signed-off-by: Minchan Kim --- mm/madvise.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/madvise.c b/mm/madvise.c index 8611f1d39289..bb04c7897eb9 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1073,7 +1073,11 @@ int do_madvise(struct task_struct *task, unsigned long start, int write; size_t len; struct blk_plug plug; - struct mm_struct *mm = task->mm; + struct mm_struct *mm = READ_ONCE(task->mm); + + /* task can exit and nullify its ->mm right after mm_access() */ + if (!mm) + return -ESRCH; start = untagged_addr(start); From patchwork Wed Feb 12 23:39:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11379363 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 9D49E139A for ; Wed, 12 Feb 2020 23:40:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5E7E72168B for ; Wed, 12 Feb 2020 23:40: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="ugnk4mbC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E7E72168B 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 21C686B04D1; Wed, 12 Feb 2020 18:40:02 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0B81B6B04D3; Wed, 12 Feb 2020 18:40:02 -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 E99736B04D4; Wed, 12 Feb 2020 18:40:01 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0202.hostedemail.com [216.40.44.202]) by kanga.kvack.org (Postfix) with ESMTP id CE3E16B04D1 for ; Wed, 12 Feb 2020 18:40:01 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 7159C824805A for ; Wed, 12 Feb 2020 23:40:01 +0000 (UTC) X-FDA: 76483095402.15.verse35_748aeccd7d43b X-Spam-Summary: 2,0,0,b9fbfe83be2e5426,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:alexander.h.duyck@linux.intel.com:minchan@kernel.org,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1544:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:3138:3139:3140:3141:3142:3355:3867:4118:4250:4385:5007:6119:6261:6642:6653:6742:7903:10004:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12895:13868:13894:14096:14181:14394:14721:21080:21444:21451:21627:21990:30054:30070,0,RBL:209.85.214.195:@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,MS BL:0,DNS X-HE-Tag: verse35_748aeccd7d43b X-Filterd-Recvd-Size: 7842 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by imf20.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Feb 2020 23:40:00 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id p11so1544573plq.10 for ; Wed, 12 Feb 2020 15:40:00 -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=tK/qyDb2GX3eOsQv+8LykkGI1WZNv+AWZfoVVRiylFw=; b=ugnk4mbCpyLdbSnvSLG+4LE+l7uQjSspFTFdoXumnT9VtWq0fApfN4PwLFAP90qoof my+fCb7um1UNKkVcQdS2Fy2CxJIvpngIkgvSrCwcCeP+rS9OAZOpRZjv+YLq/FZj+YU6 p3hj4WL+PtezT3ZPgcOlwn9B3TR3+L4h0YclrXkhWtBMF46MqTSO7Nwuji88u0C9Ys9I Victfq0Q7S8GZ3z/WzMRtXU0sMlhPBiNmTzTa96CahdBEIyJw/QeiUCPOtn1bxydOGgY TBqqxdcWMBLx95yG//Tr7q3kXPK7/xDqjdZ7j+UaU3pTyktPWgY6/MvqDY5EfO60bb3s Bx9w== 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=tK/qyDb2GX3eOsQv+8LykkGI1WZNv+AWZfoVVRiylFw=; b=RBKOE4oMaeOjeUDiLDJyIIDD1HlRNvQ0OWPONm2oWtZa6cSzCJd9i/T/GJSJgDGQXw xyQZ91hZAQonI3ae5jQflUafmVELhiksZuUys/mUxhqRQrmE3RCyavT4kso4rTx8EETa EUtJe4FCdNM+fLXGvQuJCBEzF+rI4XqW1FtAEl2eLgJ2BD29uZ+VRKMX5N6TUf/dL2z0 9y6tcmyb9J215f1X4LDlq3OCxybhydemlvZ0AAXVmxk3WJIn22OxRVSr3KmEG8EeifE6 C9DiDQIio3WUveOdN7D0t/7s+Klo7YXFmMczrvJRS7bCV8ZlgJ8szeABan4zUrDOTAKF b7ww== X-Gm-Message-State: APjAAAWnJX/uvyiYuBQlFDRJw4aRDdGXWEqzi4WpKWsGiUhBIfa8baLO 99RktrdWvbmBUfE4LQ85+a0= X-Google-Smtp-Source: APXvYqzEB9/J7b1QjW8h1fUyP6Qmptb0s33s1keuBeCMZm1l7u56kP5ukjXfiNoaeQnUfZEkSg508w== X-Received: by 2002:a17:902:8bc1:: with SMTP id r1mr10844147plo.279.1581550799359; Wed, 12 Feb 2020 15:39:59 -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.39.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 15:39:58 -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 Subject: [PATCH v4 4/8] mm: check fatal signal pending of target process Date: Wed, 12 Feb 2020 15:39:42 -0800 Message-Id: <20200212233946.246210-5-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: Bail out to prevent unnecessary CPU overhead if target process ha pending fatal signal during MADV_COLD| MADV_PAGEOUT operation. Signed-off-by: Minchan Kim --- mm/madvise.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index bb04c7897eb9..276b9d81c1dd 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -36,6 +36,7 @@ struct madvise_walk_private { struct mmu_gather *tlb; bool pageout; + struct task_struct *task; }; /* @@ -316,6 +317,9 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, if (fatal_signal_pending(current)) return -EINTR; + if (fatal_signal_pending(private->task)) + return -EINTR; + #ifdef CONFIG_TRANSPARENT_HUGEPAGE if (pmd_trans_huge(*pmd)) { pmd_t orig_pmd; @@ -471,12 +475,14 @@ static const struct mm_walk_ops cold_walk_ops = { }; static void madvise_cold_page_range(struct mmu_gather *tlb, + struct task_struct *task, struct vm_area_struct *vma, unsigned long addr, unsigned long end) { struct madvise_walk_private walk_private = { .pageout = false, .tlb = tlb, + .task = task, }; tlb_start_vma(tlb, vma); @@ -484,7 +490,8 @@ static void madvise_cold_page_range(struct mmu_gather *tlb, tlb_end_vma(tlb, vma); } -static long madvise_cold(struct vm_area_struct *vma, +static long madvise_cold(struct task_struct *task, + struct vm_area_struct *vma, struct vm_area_struct **prev, unsigned long start_addr, unsigned long end_addr) { @@ -497,19 +504,21 @@ static long madvise_cold(struct vm_area_struct *vma, lru_add_drain(); tlb_gather_mmu(&tlb, mm, start_addr, end_addr); - madvise_cold_page_range(&tlb, vma, start_addr, end_addr); + madvise_cold_page_range(&tlb, task, vma, start_addr, end_addr); tlb_finish_mmu(&tlb, start_addr, end_addr); return 0; } static void madvise_pageout_page_range(struct mmu_gather *tlb, + struct task_struct *task, struct vm_area_struct *vma, unsigned long addr, unsigned long end) { struct madvise_walk_private walk_private = { .pageout = true, .tlb = tlb, + .task = task, }; tlb_start_vma(tlb, vma); @@ -533,7 +542,8 @@ static inline bool can_do_pageout(struct vm_area_struct *vma) inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0; } -static long madvise_pageout(struct vm_area_struct *vma, +static long madvise_pageout(struct task_struct *task, + struct vm_area_struct *vma, struct vm_area_struct **prev, unsigned long start_addr, unsigned long end_addr) { @@ -549,7 +559,7 @@ static long madvise_pageout(struct vm_area_struct *vma, lru_add_drain(); tlb_gather_mmu(&tlb, mm, start_addr, end_addr); - madvise_pageout_page_range(&tlb, vma, start_addr, end_addr); + madvise_pageout_page_range(&tlb, task, vma, start_addr, end_addr); tlb_finish_mmu(&tlb, start_addr, end_addr); return 0; @@ -927,7 +937,8 @@ static int madvise_inject_error(int behavior, #endif static long -madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev, +madvise_vma(struct task_struct *task, struct vm_area_struct *vma, + struct vm_area_struct **prev, unsigned long start, unsigned long end, int behavior) { switch (behavior) { @@ -936,9 +947,9 @@ madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev, case MADV_WILLNEED: return madvise_willneed(vma, prev, start, end); case MADV_COLD: - return madvise_cold(vma, prev, start, end); + return madvise_cold(task, vma, prev, start, end); case MADV_PAGEOUT: - return madvise_pageout(vma, prev, start, end); + return madvise_pageout(task, vma, prev, start, end); case MADV_FREE: case MADV_DONTNEED: return madvise_dontneed_free(vma, prev, start, end, behavior); @@ -1143,7 +1154,7 @@ int do_madvise(struct task_struct *task, unsigned long start, tmp = end; /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */ - error = madvise_vma(vma, &prev, start, tmp, behavior); + error = madvise_vma(task, vma, &prev, start, tmp, behavior); if (error) goto out; start = tmp; From patchwork Wed Feb 12 23:39:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11379365 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 1327A921 for ; Wed, 12 Feb 2020 23:40:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CA5EE21734 for ; Wed, 12 Feb 2020 23:40:05 +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="FZrxKndd" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CA5EE21734 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 8F5726B04D3; Wed, 12 Feb 2020 18:40:03 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8A85D6B04D5; Wed, 12 Feb 2020 18:40:03 -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 6D1276B04D6; Wed, 12 Feb 2020 18:40:03 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0145.hostedemail.com [216.40.44.145]) by kanga.kvack.org (Postfix) with ESMTP id 504586B04D3 for ; Wed, 12 Feb 2020 18:40:03 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 01752824805A for ; Wed, 12 Feb 2020 23:40:03 +0000 (UTC) X-FDA: 76483095486.27.dirt46_74c7db148872a X-Spam-Summary: 2,0,0,6d7a5714a18556af,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:alexander.h.duyck@linux.intel.com:minchan@kernel.org,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1359:1437:1515:1534:1541:1711:1730:1747:1777:1792:2393:2559:2562:2693:3138:3139:3140:3141:3142:3352:3865:3871:4250:4321:5007:6119:6261:6653:6742:7576:7903:10004:10226:11026:11658:11914:12043:12296:12297:12517:12519:12555:12679:12895:13069:13311:13357:13894:14096:14181:14384:14394:14721:21080:21444:21451:21627:21990:30054:30070,0,RBL:209.85.215.195:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SP X-HE-Tag: dirt46_74c7db148872a X-Filterd-Recvd-Size: 4618 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Feb 2020 23:40:02 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id w21so2023587pgl.9 for ; Wed, 12 Feb 2020 15:40:02 -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=ZMr8LhEVC9oO5IOdT1EAti6B5+7g8NKkMYB8pLjiUts=; b=FZrxKnddlcDw5zyfExhzpFB7yNfAtSGotiVUgDx7kQeEtRk96KD2mjuJAwspjEWZEN 8KQGuw5rgbMTjblF/1hERi1coEcedKQRj0A3ZDpTh/rLPLJ01OSf3UbmgfW6utovZI3H qKbek4ZUVtEesWnc8Jz1oh3eReFVrokHuT8LrXYSlGbQs0tKXLnC7cKdOT9I8H90mNP/ 6LeqDCmJSqRfYdC0SBilvY0LlpVtSqLG7Oof/zDRge1nTZaZh3bSa2QLWuT7PpN2E/V8 MufG1jDdEtKBDxSNXo3mv7bob5OGe9IF3J0ofzL96vWBtLLs9Lykq6WoQHJCnrmHn7sj g+/A== 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=ZMr8LhEVC9oO5IOdT1EAti6B5+7g8NKkMYB8pLjiUts=; b=aznDoG/dN3SjTwwtkhnauDaTP+3fbaVzcmn8EV1MidEYkP9/lNoCGA4Wj+QthoIHAw VuwoqYjLZascnfz0kXKuxSJQP7g+biif6kZNW+ZL1ouyn1cQvUxdf+4ovQBxwU5SUev0 vPseiECpOC6IU5liUnjx7s8E80NuvWNiwg9AphDkg+SSb7rKZ7lzmW7iw54FtyQyj/fw mh+TthQYTOrrPL196S/GzuYqw7XIk9DKLA+9aapm595FH16RcyAU1KwqbhoQUZqRJgX9 euUB9AAkWGknZD0x8v/ddKACe24bELcAnORPTk9DX+l0Xbb8cY4zJGzeLenRpWmce8YM 6rCg== X-Gm-Message-State: APjAAAWumqOBgniOncYfHBnZ9xXrXSoFV+acFUBz8mtWNYQfY4rGrX78 GJaVuFupTJNm/Wo/sTq6RXo= X-Google-Smtp-Source: APXvYqyTYiXBm1zUHh7AFeoGz7OUaVklPnIUFSpjd2o7FpUwNPngIaBgYKUhERpn7MoMtdc1mZ47HA== X-Received: by 2002:a65:645a:: with SMTP id s26mr10828696pgv.135.1581550801457; Wed, 12 Feb 2020 15:40:01 -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.39.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 15:40:00 -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 Subject: [PATCH v4 5/8] mm/madvise: employ mmget_still_valid for write lock Date: Wed, 12 Feb 2020 15:39:43 -0800 Message-Id: <20200212233946.246210-6-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: From: Oleksandr Natalenko Do the very same trick as we already do since 04f5866e41fb. KSM hints will require locking mmap_sem for write since they modify vm_flags, so for remote KSM hinting this additional check is needed. Signed-off-by: Oleksandr Natalenko Signed-off-by: Minchan Kim --- mm/madvise.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/madvise.c b/mm/madvise.c index 276b9d81c1dd..71f0ba199ae8 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1120,6 +1120,8 @@ int do_madvise(struct task_struct *task, unsigned long start, if (write) { if (down_write_killable(&mm->mmap_sem)) return -EINTR; + if (current->mm != mm && !mmget_still_valid(mm)) + goto skip_mm; } else { down_read(&mm->mmap_sem); } @@ -1170,6 +1172,7 @@ int do_madvise(struct task_struct *task, unsigned long start, } out: blk_finish_plug(&plug); +skip_mm: if (write) up_write(&mm->mmap_sem); else From patchwork Wed Feb 12 23:39:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11379367 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 71BA9921 for ; Wed, 12 Feb 2020 23:40:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 347922168B for ; Wed, 12 Feb 2020 23:40:08 +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="Vzb3l8tk" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 347922168B 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 9E20E6B04D5; Wed, 12 Feb 2020 18:40:05 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 96A4A6B04D7; Wed, 12 Feb 2020 18:40:05 -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 7482A6B04D8; Wed, 12 Feb 2020 18:40:05 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0125.hostedemail.com [216.40.44.125]) by kanga.kvack.org (Postfix) with ESMTP id 57BE06B04D5 for ; Wed, 12 Feb 2020 18:40:05 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id F17DE440B for ; Wed, 12 Feb 2020 23:40:04 +0000 (UTC) X-FDA: 76483095570.20.face34_7507bb09aca1e X-Spam-Summary: 30,2,0,38a38a48a73182af,d41d8cd98f00b204,minchan.kim@gmail.com,,RULES_HIT:41:355:379:541:800:960:967:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1542:1711:1730:1747:1777:1792:2393:2525:2559:2563:2682:2685:2693:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4321:4823:5007:6119:6120:6261:6653:6742:7576:7901:7903:8557:9025:10004:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12679:12895:12986:13869:13894:14181:14394:14721:21080:21222:21444:21451:21524:21611:21627:21740:21772:21889:21990:30005:30034:30054,0,RBL:209.85.214.196:@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:25,LUA_SUMMARY:none X-HE-Tag: face34_7507bb09aca1e X-Filterd-Recvd-Size: 5785 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf32.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Feb 2020 23:40:04 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id y8so1539564pll.13 for ; Wed, 12 Feb 2020 15:40:04 -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=AXU0e7YyFS/VUMYPbADn6DLwQaySKSAu7t+ONiQoTpg=; b=Vzb3l8tkR4YbxU66SZ9n9nBxwBBBt3Hb7W8USVfds/HSLFN5fliwziScWU/0DUU5u7 P1akVLwV7GzSWcQ6oucwCOZhQaJljjR5vveLA/FERFd3Dr6X8i0OyADhtbvtXCrOUIXC G3mHqB80cTp79sAh0e+KI56k+GeAp2Ruixvd52hxGSas6+WO67LnF/e9VmYldCqDAv1D kNTDeNcgTqr4P+NkFB052cowzeBEWuR0GcccOrR0IoK5Osec1hs4sQZLnhrnUhu+mZUi C+coPGSDtJaW3TYnkOzl6mmlVLkonabbog7rKg6Hw5C6Lk9UAC6RBUVyKZG3GHBFnpWn Gn4w== 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=AXU0e7YyFS/VUMYPbADn6DLwQaySKSAu7t+ONiQoTpg=; b=EHDV6TkARTc0yURzg1IVKoKbZ4rayu2S8oD+/CphDSzRU+wu4QmhwvFEi6SdiPQOXd 3PEC6SzlrInDVpCj2CAtfNX4jFMAgU+zeTJ6WSJk89a2umUD4vV10uaDye+o1YZ/zckk zDeU/vAREGnU5so4wBEGqB+iHsPT8EIY9aegS6kJa+pF0uXVfvZRq9t2e5dT9CjqCyFB /PXfDS4uqXx6QPab3i3bmIqG9/A3Zzmd+RmlDCm4KAXn+9s0SdXq1q8up/+pDdQRS6SR QZhK5J2950zS6YirgxGYGYCypsY/lPGhGn+22M0OQKby/DDt56QFJgqcnk0Q2yV0JPGe FmuA== X-Gm-Message-State: APjAAAUlYOpCRcGurIl1G8NXZJanr4KSOdME+3t/K1AEUOPzShg9xr8l UzoUGacR/XrykbjHvKsQDuo= X-Google-Smtp-Source: APXvYqwD/pMWtWK6l2LA9FEXSKZnFfQWBoMzwuldcspK6qD6assD8dbx8pK1RFbqi/vyXacz06EMUw== X-Received: by 2002:a17:902:8341:: with SMTP id z1mr24836376pln.178.1581550803112; Wed, 12 Feb 2020 15:40:03 -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.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 15:40:02 -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 Subject: [PATCH v4 6/8] mm/madvise: allow KSM hints for remote API Date: Wed, 12 Feb 2020 15:39:44 -0800 Message-Id: <20200212233946.246210-7-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: From: Oleksandr Natalenko It all began with the fact that KSM works only on memory that is marked by madvise(). And the only way to get around that is to either: * use LD_PRELOAD; or * patch the kernel with something like UKSM or PKSM. (i skip ptrace can of worms here intentionally) To overcome this restriction, lets employ a new remote madvise API. This can be used by some small userspace helper daemon that will do auto-KSM job for us. I think of two major consumers of remote KSM hints: * hosts, that run containers, especially similar ones and especially in a trusted environment, sharing the same runtime like Node.js; * heavy applications, that can be run in multiple instances, not limited to opensource ones like Firefox, but also those that cannot be modified since they are binary-only and, maybe, statically linked. Speaking of statistics, more numbers can be found in the very first submission, that is related to this one [1]. For my current setup with two Firefox instances I get 100 to 200 MiB saved for the second instance depending on the amount of tabs. 1 FF instance with 15 tabs: $ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc 410 2 FF instances, second one has 12 tabs (all the tabs are different): $ echo "$(cat /sys/kernel/mm/ksm/pages_sharing) * 4 / 1024" | bc 592 At the very moment I do not have specific numbers for containerised workload, but those should be comparable in case the containers share similar/same runtime. [1] https://lore.kernel.org/patchwork/patch/1012142/ Signed-off-by: Oleksandr Natalenko Signed-off-by: Minchan Kim --- mm/madvise.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/madvise.c b/mm/madvise.c index 71f0ba199ae8..b1237466657e 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1002,6 +1002,10 @@ process_madvise_behavior_valid(int behavior) switch (behavior) { case MADV_COLD: case MADV_PAGEOUT: +#ifdef CONFIG_KSM + case MADV_MERGEABLE: + case MADV_UNMERGEABLE: +#endif return true; default: return false; From patchwork Wed Feb 12 23:39:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11379369 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 B271E921 for ; Wed, 12 Feb 2020 23:40:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7E3682168B for ; Wed, 12 Feb 2020 23:40:10 +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="f9JXNeGA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7E3682168B 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 B3E6B6B04D7; Wed, 12 Feb 2020 18:40:07 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id AA42D6B04D9; Wed, 12 Feb 2020 18:40:07 -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 91DBD6B04DA; Wed, 12 Feb 2020 18:40:07 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0096.hostedemail.com [216.40.44.96]) by kanga.kvack.org (Postfix) with ESMTP id 7496E6B04D7 for ; Wed, 12 Feb 2020 18:40:07 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 149EA180AD80F for ; Wed, 12 Feb 2020 23:40:07 +0000 (UTC) X-FDA: 76483095654.19.sense68_755dd84c4d721 X-Spam-Summary: 2,0,0,8214bbd96d630d64,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:alexander.h.duyck@linux.intel.com:minchan@kernel.org:christian@brauner.io,RULES_HIT:41:69:355:379:541:800:960:973:982:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1542:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3353:3865:3867:4321:4605:5007:6261:6653:6742:7875:7903:9592:10004:11026:11658:11914:12296:12297:12517:12519:12555:12895:13894:14096:14181:14394:14721:21080:21324:21444:21451:21627:21990:30054:30062,0,RBL:209.85.216.66:@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,M SBL:0,DN X-HE-Tag: sense68_755dd84c4d721 X-Filterd-Recvd-Size: 5589 Received: from mail-pj1-f66.google.com (mail-pj1-f66.google.com [209.85.216.66]) by imf32.hostedemail.com (Postfix) with ESMTP for ; Wed, 12 Feb 2020 23:40:06 +0000 (UTC) Received: by mail-pj1-f66.google.com with SMTP id d5so1531631pjz.5 for ; Wed, 12 Feb 2020 15:40:06 -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=WYzOGpKVdAHMy+iPLX1CcTtO6CKLdGBLsdfZj/dlK3Y=; b=f9JXNeGAff+DANp8AqHpgqhG0pAWgaP0mE4TLt1CG3ZDpPOXz2JYJGRXmrWlobmcnF Mrn5rstIOGszz+lI+JIIh+klaU6x0ERcKL0TYzj3lkFXWflMZX+fm2G6xnm14WCb9R1k RFZ05uPkwQjJQrRPEpYl/Vs5739i9oE7+XvsTNP9+z4WXqjrTc4cxnCfTNZDoEQ3ZIIT i9zUyW6xeMUWB2J2LhMKpFjmHPwhiRHTjbuZ3Peh1psbX1k9N3+WNeb1yNfuanPaaUNg awpJNeuvthozvWL97pCmIOtTM+W4LvhEoSBsu+kqIX6bRIzmE8GOwPGjOlnA9H0q+9iv IWyg== 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=WYzOGpKVdAHMy+iPLX1CcTtO6CKLdGBLsdfZj/dlK3Y=; b=etdIVnQDknyVU4JCALBr+HeZW1OA2qlcAiGDl/2TTVcVCCwPrn3OyMJ8Z4oWZb9/9L S0oYPVUwQRhc7dIfVnXDih6zMu6eHPmPVE1n41TLXqu9eLE3mrGhb9a7gL79hrdxTxot ZwzUmBlpu5xBxj6bPGx7C541uHFAhY+6WbETcDZU1ImT+q27Snt6zsCIa88M73ERMDcw BfepGYkMmm7H7afhM2OwPj0IFVswSjK2KL60SSsJf3V38HvEqCB7tdagFCbGedYTBDTE cNVZsShX23g+xF0BmqvrNOJtRzEL+GNa/qCgd25lwI69zA56UQ9RidUbQ6reEZ8pBCeK zqVQ== X-Gm-Message-State: APjAAAXKXYRECwuCKRTXjWj9earFi3HvcUfbSEmQSF6SjGmLXT070iXi 5qCsReKG9kx8+ympTghgk/M= X-Google-Smtp-Source: APXvYqxzdMSgjet/sJg2AzFVv1VfQOV4vJS4AINZURSRk4raXqtjTGX7apkA6Ratnn2Z2ah6CRpUnw== X-Received: by 2002:a17:902:302:: with SMTP id 2mr26208596pld.58.1581550805243; Wed, 12 Feb 2020 15:40:05 -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.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 15:40:04 -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 Subject: [PATCH v4 7/8] pid: export pidfd_get_pid Date: Wed, 12 Feb 2020 15:39:45 -0800 Message-Id: <20200212233946.246210-8-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: process_madvise syscall needs pidfd_get_pid function to translate pidfd to pid so this patch exports the function. Cc: Christian Brauner Signed-off-by: Minchan Kim Reviewed-by: Alexander Duyck --- include/linux/pid.h | 1 + kernel/exit.c | 17 ----------------- kernel/pid.c | 17 +++++++++++++++++ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/include/linux/pid.h b/include/linux/pid.h index 998ae7d24450..023d9c3a8edc 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -75,6 +75,7 @@ extern const struct file_operations pidfd_fops; struct file; extern struct pid *pidfd_pid(const struct file *file); +extern struct pid *pidfd_get_pid(unsigned int fd); static inline struct pid *get_pid(struct pid *pid) { diff --git a/kernel/exit.c b/kernel/exit.c index 0b81b26a872a..43375f9d8bbc 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1470,23 +1470,6 @@ static long do_wait(struct wait_opts *wo) return retval; } -static struct pid *pidfd_get_pid(unsigned int fd) -{ - struct fd f; - struct pid *pid; - - f = fdget(fd); - if (!f.file) - return ERR_PTR(-EBADF); - - pid = pidfd_pid(f.file); - if (!IS_ERR(pid)) - get_pid(pid); - - fdput(f); - return pid; -} - static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, int options, struct rusage *ru) { diff --git a/kernel/pid.c b/kernel/pid.c index 0f4ecb57214c..360ba480a2a9 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -496,6 +496,23 @@ struct pid *find_ge_pid(int nr, struct pid_namespace *ns) return idr_get_next(&ns->idr, &nr); } +struct pid *pidfd_get_pid(unsigned int fd) +{ + struct fd f; + struct pid *pid; + + f = fdget(fd); + if (!f.file) + return ERR_PTR(-EBADF); + + pid = pidfd_pid(f.file); + if (!IS_ERR(pid)) + get_pid(pid); + + fdput(f); + return pid; +} + /** * pidfd_create() - Create a new pid file descriptor. * 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; }