From patchwork Tue Sep 1 00:06:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Minchan Kim X-Patchwork-Id: 11747129 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 7D016109B for ; Tue, 1 Sep 2020 00:07:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3DE4F2100A for ; Tue, 1 Sep 2020 00:07:53 +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="loncq7c6" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3DE4F2100A 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 544216B0037; Mon, 31 Aug 2020 20:07:52 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4F5F46B0055; Mon, 31 Aug 2020 20:07:52 -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 3BD5D6B005A; Mon, 31 Aug 2020 20:07:52 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0047.hostedemail.com [216.40.44.47]) by kanga.kvack.org (Postfix) with ESMTP id EDC596B0037 for ; Mon, 31 Aug 2020 20:07:51 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id B15A0180AD80F for ; Tue, 1 Sep 2020 00:07:51 +0000 (UTC) X-FDA: 77212554342.29.game33_4b0dcf927093 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin29.hostedemail.com (Postfix) with ESMTP id 84B8B18086CB5 for ; Tue, 1 Sep 2020 00:07:51 +0000 (UTC) X-Spam-Summary: 1,0,0,06fe45c09f1594ca,d41d8cd98f00b204,minchan.kim@gmail.com,,RULES_HIT:1:2:41:69:355:379:541:800:960:966:967:968:973:982:988:989:1260:1263:1311:1314:1345:1359:1431:1437:1515:1605:1730:1747:1777:1792:2194:2196:2198:2199:2200:2201:2376:2393:2525:2559:2566:2682:2685:2693:2859:2892:2901:2902:2911:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4051:4250:4321:4385:4425:4605:5007:6261:6653:6742:6743:7514:7875:7901:7903:8599:8660:8957:9025:10004:10913:11026:11473:11658:11914:12043:12296:12297:12438:12517:12519:12555:12679:12683:12783:12895:12986:13053:13141:13148:13191:13192:13229:13230:13846:13894:14096:14394:21080:21094:21323:21324:21433:21444:21451:21611:21627:21796:21939:21987:21990:30003:30036:30054:30064:30070:30075,0,RBL:209.85.214.196:@gmail.com:.lbl8.mailshell.net-62.50.0.100 66.100.201.100;04yfoowzo1ktgki83wpmmwy9s7w87ocb1f91d1aww37z51cgtj83pdah 994i7wk. X-HE-Tag: game33_4b0dcf927093 X-Filterd-Recvd-Size: 12569 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf24.hostedemail.com (Postfix) with ESMTP for ; Tue, 1 Sep 2020 00:07:50 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id k13so4021471plk.13 for ; Mon, 31 Aug 2020 17:07:50 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=9v0toS4YEWMYzK02MEFlXArA+LdQ8YNff+2tLjRKFE8=; b=loncq7c6CT+AEoKDTBJeM5bSVJwnYUmCav1ptE1n063QgFNE+8rPq3hINxu9nCjGo7 0ji4wtAxZlGbmP0dRWQjrccM9maOKSJiJ++HBj0v3y3HjWsUML4N5hI6xIoV6RbsMWpY YZytYZlrEJx4zOhYOnsfRWQQd34a1vQCND3tyNficVkuj3htbAespGz6J6rVgY/KCxsa /bBIGlpkIxHTJspvAwAg8Ybq/aaY+kNfYSItb+WAo1okZTSRNY3eN3e2NWF7LjqxKxKq qD2exgxLj9A5HWX3STiTlvb0jAN9qeLAyIDW3OJYQWVDxBmPRVJthnOzPFzygTj2BnSv GCsw== 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=9v0toS4YEWMYzK02MEFlXArA+LdQ8YNff+2tLjRKFE8=; b=rB91cW2fuxHfktr246iVCRizUq5gaRwzRvXBU93j7+bEiAmxV8Dy00uIXKTE6uPITZ iStUOXVDXZ57eJFjI7P61EX5YcVeDi4ShrmkZsnmVvkL0Dqd3UlWPhMoT+os8KqRa0P/ Azr8mHby4i/bg7dXu9iKEy8BqrDWODVfbMt1/PyW2XoYk9lWLb0OM6pq0K3TTOT0dcIz oKoeE1/OgUKQiauU+WcKIQXcCm55YNWotOaavwv4XJ0vgBvu1U6yLa1gTB8pPRCY6RJM 7yQvLChBYQn/MG4UX6WM2oa1JSsFXdqxwBIIHRX6uzLWKYur0mMi5+m83KC1L/QJWeDb PtZA== X-Gm-Message-State: AOAM530gLS+dKwa2VsTLz8rtuLJOhpjiUrZou1kse53k5fuGj0+frBKp UTQcFXXE4gAggTM2Wp0qJIU= X-Google-Smtp-Source: ABdhPJx1GFPW+c6yEJLwrJXLnT6GZ2kh1PjSdZHUxZHP4IaAqCPy2tDZk7LgzqeJ2r3EK5Jrn6rNhw== X-Received: by 2002:a17:90b:4a10:: with SMTP id kk16mr1603213pjb.203.1598918869690; Mon, 31 Aug 2020 17:07:49 -0700 (PDT) Received: from bbox-1.mtv.corp.google.com ([2620:15c:211:1:7220:84ff:fe09:5e58]) by smtp.gmail.com with ESMTPSA id k5sm769777pjq.5.2020.08.31.17.07.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 17:07:48 -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 , Sandeep Patil , Sonny Rao , Brian Geffon , Michal Hocko , Johannes Weiner , Shakeel Butt , John Dias , Joel Fernandes , Jann Horn , alexander.h.duyck@linux.intel.com, sj38.park@gmail.com, David Rientjes , Arjun Roy , Linus Torvalds , Minchan Kim , Vlastimil Babka , Jens Axboe , Daniel Colascione , Christian Brauner , Kirill Tkhai , SeongJae Park , linux-man@vger.kernel.org Subject: [PATCH v9 1/3] mm/madvise: pass mm to do_madvise Date: Mon, 31 Aug 2020 17:06:31 -0700 Message-Id: <20200901000633.1920247-2-minchan@kernel.org> X-Mailer: git-send-email 2.28.0.402.g5ffc5be6b7-goog In-Reply-To: <20200901000633.1920247-1-minchan@kernel.org> References: <20200901000633.1920247-1-minchan@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 84B8B18086CB5 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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: Patch series "introduce memory hinting API for external process", v9. Now, we have MADV_PAGEOUT and MADV_COLD as madvise hinting API. With that, application could give hints to kernel what memory range are preferred to be reclaimed. However, in some platform(e.g., Android), the information required to make the hinting decision is not known to the app. Instead, it is known to a centralized userspace daemon(e.g., ActivityManagerService), and that daemon must be able to initiate reclaim on its own without any app involvement. To solve the concern, this patch introduces new syscall - process_madvise(2). Bascially, it's same with madvise(2) syscall but it has some differences. 1. It needs pidfd of target process to provide the hint 2. It supports only MADV_{COLD|PAGEOUT|MERGEABLE|UNMEREABLE} at this moment. Other hints in madvise will be opened when there are explicit requests from community to prevent unexpected bugs we couldn't support. 3. Only privileged processes can do something for other process's address space. For more detail of the new API, please see "mm: introduce external memory hinting API" description in this patchset. This patch (of 4): In upcoming patches, do_madvise will be called from external process context so we shouldn't asssume "current" is always hinted process's task_struct. Furthermore, we must not access mm_struct via task->mm, but obtain it via access_mm() once (in the following patch) and only use that pointer [1], so pass it to do_madvise() as well. Note the vma->vm_mm pointers are safe, so we can use them further down the call stack. And let's pass current->mm as arguments of do_madvise so it shouldn't change existing behavior but prepare next patch to make review easy. [vbabka@suse.cz: changelog tweak] [minchan@kernel.org: use current->mm for io_uring] Link: http://lkml.kernel.org/r/20200423145215.72666-1-minchan@kernel.org [akpm@linux-foundation.org: fix it for upstream changes] [akpm@linux-foundation.org: whoops] [rdunlap@infradead.org: add missing includes] Link: http://lkml.kernel.org/r/20200622192900.22757-1-minchan@kernel.org Link: http://lkml.kernel.org/r/20200302193630.68771-2-minchan@kernel.org Link: http://lkml.kernel.org/r/20200622192900.22757-2-minchan@kernel.org Signed-off-by: Minchan Kim Reviewed-by: Suren Baghdasaryan Reviewed-by: Vlastimil Babka Acked-by: David Rientjes Cc: Jens Axboe Cc: Jann Horn Cc: Tim Murray Cc: Daniel Colascione Cc: Sandeep Patil Cc: Sonny Rao Cc: Brian Geffon Cc: Michal Hocko Cc: Johannes Weiner Cc: Shakeel Butt Cc: John Dias Cc: Joel Fernandes Cc: Alexander Duyck Cc: SeongJae Park Cc: Christian Brauner Cc: Kirill Tkhai Cc: Oleksandr Natalenko Cc: SeongJae Park Cc: Christian Brauner Cc: --- fs/io_uring.c | 2 +- include/linux/mm.h | 2 +- mm/madvise.c | 32 ++++++++++++++++++-------------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 94b9b5cf5971..cfb8003be09c 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -3823,7 +3823,7 @@ static int io_madvise(struct io_kiocb *req, bool force_nonblock) if (force_nonblock) return -EAGAIN; - ret = do_madvise(ma->addr, ma->len, ma->advice); + ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); if (ret < 0) req_set_fail_links(req); io_req_complete(req, ret); diff --git a/include/linux/mm.h b/include/linux/mm.h index 517751310dd2..a12354e63e49 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2570,7 +2570,7 @@ 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 mm_struct *mm, unsigned long start, size_t len_in, int behavior); #ifdef CONFIG_MMU extern int __mm_populate(unsigned long addr, unsigned long len, diff --git a/mm/madvise.c b/mm/madvise.c index 4d87b31a1576..3bf5c8261f1c 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -255,6 +255,7 @@ static long madvise_willneed(struct vm_area_struct *vma, struct vm_area_struct **prev, unsigned long start, unsigned long end) { + struct mm_struct *mm = vma->vm_mm; struct file *file = vma->vm_file; loff_t offset; @@ -291,10 +292,10 @@ static long madvise_willneed(struct vm_area_struct *vma, get_file(file); offset = (loff_t)(start - vma->vm_start) + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); - mmap_read_unlock(current->mm); + mmap_read_unlock(mm); vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED); fput(file); - mmap_read_lock(current->mm); + mmap_read_lock(mm); return 0; } @@ -763,6 +764,8 @@ 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; @@ -770,8 +773,8 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, if (!userfaultfd_remove(vma, start, end)) { *prev = NULL; /* mmap_lock has been dropped, prev is stale */ - mmap_read_lock(current->mm); - vma = find_vma(current->mm, start); + mmap_read_lock(mm); + vma = find_vma(mm, start); if (!vma) return -ENOMEM; if (start < vma->vm_start) { @@ -825,6 +828,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_lock */ @@ -852,13 +856,13 @@ static long madvise_remove(struct vm_area_struct *vma, get_file(f); if (userfaultfd_remove(vma, start, end)) { /* mmap_lock was not released by userfaultfd_remove() */ - mmap_read_unlock(current->mm); + mmap_read_unlock(mm); } error = vfs_fallocate(f, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, end - start); fput(f); - mmap_read_lock(current->mm); + mmap_read_lock(mm); return error; } @@ -1046,7 +1050,7 @@ 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 mm_struct *mm, unsigned long start, size_t len_in, int behavior) { unsigned long end, tmp; struct vm_area_struct *vma, *prev; @@ -1084,10 +1088,10 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) write = madvise_need_mmap_write(behavior); if (write) { - if (mmap_write_lock_killable(current->mm)) + if (mmap_write_lock_killable(mm)) return -EINTR; } else { - mmap_read_lock(current->mm); + mmap_read_lock(mm); } /* @@ -1095,7 +1099,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; @@ -1132,19 +1136,19 @@ int do_madvise(unsigned long start, size_t len_in, int behavior) if (prev) vma = prev->vm_next; else /* madvise_remove dropped mmap_lock */ - vma = find_vma(current->mm, start); + vma = find_vma(mm, start); } out: blk_finish_plug(&plug); if (write) - mmap_write_unlock(current->mm); + mmap_write_unlock(mm); else - mmap_read_unlock(current->mm); + mmap_read_unlock(mm); 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->mm, start, len_in, behavior); }