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); }