From patchwork Fri Jun 8 18:56:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Baron X-Patchwork-Id: 10455029 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CF667601D4 for ; Fri, 8 Jun 2018 18:58:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C01F6293FE for ; Fri, 8 Jun 2018 18:58:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B492D29409; Fri, 8 Jun 2018 18:58:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EFF09293FE for ; Fri, 8 Jun 2018 18:58:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EFEF6B0003; Fri, 8 Jun 2018 14:58:19 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 09E806B0005; Fri, 8 Jun 2018 14:58:19 -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 ED0456B0006; Fri, 8 Jun 2018 14:58:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm0-f72.google.com (mail-wm0-f72.google.com [74.125.82.72]) by kanga.kvack.org (Postfix) with ESMTP id 915486B0003 for ; Fri, 8 Jun 2018 14:58:18 -0400 (EDT) Received: by mail-wm0-f72.google.com with SMTP id x22-v6so1378187wmc.7 for ; Fri, 08 Jun 2018 11:58:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id; bh=itzodWythzEyEoJMwkew01V3D7MPmGPBRCvvntVprY4=; b=LjIFrOPWrYn9j0tQzbPC0VbfgKvz1un6cD5FZ815xXZLY6GiGLyXBRJJlKA49pThuR hQ6ijQTWol0SOeJU/YiLXrTNDxyBIU6Q5gOprbRx9EE4p4sgzCd7xAd01jitD5dyveht E3dfXMMvnLllksXEIGxkAtzG1iV8QMTsI+GYV0YpJ/NQT1Nx3BnEa/5v8NTy9p5xDflY 1j6dcg++nIgJTLg+Rx+ZKqW8MQ0XxEAF1hxtn359wbAeyON2wgHmldqRVqo+sxYzKNzv qoEhALWPNFcE5l8/jszbjQEBEY6cDVlYOdLqIr6xHKSuK7hizfQIqtAbMbPjDblT/Q7s YY8Q== X-Gm-Message-State: APt69E1MvzshGlSGGGP0Ob8jdtDH6qKAD175O7gj0uNINRAlII51qXVq OHN5cbV/Gx83fNG4sUHIbY883DyoEOQaIX0nFUgmF4oiWuorobpINEIodKcXLyPFtvKhvQpXTnz wNS3BmQyPR61Y5lhOL0a5maLJSGDShQk2T+9pDIP0mqnm3IFuDOP2AOBIKQgIvX3CBg== X-Received: by 2002:a50:d6d7:: with SMTP id l23-v6mr8420784edj.259.1528484297931; Fri, 08 Jun 2018 11:58:17 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ6s8gdeF6eDYmbluG4PRUjx2flbPq6qPzA99LeYOyPxhibvYznz9LhQmgNhn9Cd7jqfetM X-Received: by 2002:a50:d6d7:: with SMTP id l23-v6mr8420732edj.259.1528484296994; Fri, 08 Jun 2018 11:58:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528484296; cv=none; d=google.com; s=arc-20160816; b=KVLt5E/RbA1Dm14Yrrl3j3we89elcizeWhXuQGOdV1rFxpEnMtzd/Kb+r7nhneY33j 5/XPCMBhEOA7V07xRaEZtfWvfzyvJTD0GNwzhsmQKx6UkqR4D8O2C9ZZg1iIjp2wogHG FwYedqXKx8jB61bg4xmyjqMgslywn3a2TkQ44cA/2aO6qy19fWqSYI0IhgGwW0JZBYiJ 0AVbZo3GLmunH188dGqz/sWTheJLaRzASZXknFpb9exUl44vVCfSlkhw7DizoKqAYGDS cVF5qgt/A9grxiOm+lF7DUCGeY4/WnTfBrujcEGOEo+Q6otVZRjxHTt8bTmjtCt3gFyo F3Fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=itzodWythzEyEoJMwkew01V3D7MPmGPBRCvvntVprY4=; b=ln7cR5K4n5WMN1UEP4RQmF9t1nGGtVRbs7fZQssda/wYMPV47NgL8+LcY96fCrIYdc EqE8o2NQpJrkJGuDI390+blUIKEqzsdHY9Uxxh6HVTtU1qSkw4WJ/hnpSMMBXyE8a1le 0G4KswYkdTIVTBzqhobBbQQptZb7KcJi0fv4FMlKARVDAPdmGFOvhKZuLVAa97NBgH7j YZyo7qkRUdvOTYKaP30xyPNeeZeXEmnf74alC6em4FBXE/jk76ww31GXoXDZYSrz26ZM VzizAF38dTDEU0Wp/zQ+c4IdM8Eo7U/Br3gf/mLcLJnqlFmTP/6/hRAFky2Yg/pplSmo oxeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@akamai.com header.s=jan2016.eng header.b=XGIZh1PT; spf=pass (google.com: domain of jbaron@akamai.com designates 2620:100:9001:583::1 as permitted sender) smtp.mailfrom=jbaron@akamai.com; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=akamai.com Received: from mx0a-00190b01.pphosted.com (mx0a-00190b01.pphosted.com. [2620:100:9001:583::1]) by mx.google.com with ESMTPS id 19-v6si620153edz.385.2018.06.08.11.58.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Jun 2018 11:58:16 -0700 (PDT) Received-SPF: pass (google.com: domain of jbaron@akamai.com designates 2620:100:9001:583::1 as permitted sender) client-ip=2620:100:9001:583::1; Authentication-Results: mx.google.com; dkim=pass header.i=@akamai.com header.s=jan2016.eng header.b=XGIZh1PT; spf=pass (google.com: domain of jbaron@akamai.com designates 2620:100:9001:583::1 as permitted sender) smtp.mailfrom=jbaron@akamai.com; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=akamai.com Received: from pps.filterd (m0050093.ppops.net [127.0.0.1]) by m0050093.ppops.net-00190b01. (8.16.0.22/8.16.0.22) with SMTP id w58IvPOs020197; Fri, 8 Jun 2018 19:58:14 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=akamai.com; h=from : to : cc : subject : date : message-id; s=jan2016.eng; bh=itzodWythzEyEoJMwkew01V3D7MPmGPBRCvvntVprY4=; b=XGIZh1PTqd7Io/Gq+Xl+9BbztE8ai64KXIVMsU59NtO5IRDhBGT24rpci6yaECaPtzxn fDuWLW0yd56DpiKQQqfRFYHazyoO2wcP+CP+9pBOzPERkoCfTO80NOLvUqJtCfwDVPRl IE8ljMBu37nRIvzJTzizByveKnFs3V+zxukliBX4JtFWckElmUOBXu2+sQQD7KAKZi43 GMtMNzwaxrN8Itzu685G0oYWPI6YcTvV4bUfusoWuUXaVKTirGOQFalbPZDQDORjSi3f 6AXybAOnJxw1cTH22EGlSERlC4fWOYurdqyZTQiuM0J5P3TXM+b0BqIX2EV0kbBu1tWF Mw== Received: from prod-mail-ppoint3 (a96-6-114-86.deploy.static.akamaitechnologies.com [96.6.114.86] (may be forged)) by m0050093.ppops.net-00190b01. with ESMTP id 2jf85mmp5n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 08 Jun 2018 19:58:14 +0100 Received: from pps.filterd (prod-mail-ppoint3.akamai.com [127.0.0.1]) by prod-mail-ppoint3.akamai.com (8.16.0.21/8.16.0.21) with SMTP id w58IuAV0031234; Fri, 8 Jun 2018 14:58:13 -0400 Received: from prod-mail-relay14.akamai.com ([172.27.17.39]) by prod-mail-ppoint3.akamai.com with ESMTP id 2jbpjwvbuw-1; Fri, 08 Jun 2018 14:58:13 -0400 Received: from bos-lpjec.kendall.corp.akamai.com (bos-lpjec.kendall.corp.akamai.com [172.28.13.175]) by prod-mail-relay14.akamai.com (Postfix) with ESMTP id C542982D01; Fri, 8 Jun 2018 18:58:12 +0000 (GMT) From: Jason Baron To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Michal Hocko , Vlastimil Babka , Joonsoo Kim , Mel Gorman , "Kirill A. Shutemov" Subject: [PATCH] mm/madvise: allow MADV_DONTNEED to free memory that is MLOCK_ONFAULT Date: Fri, 8 Jun 2018 14:56:52 -0400 Message-Id: <1528484212-7199-1-git-send-email-jbaron@akamai.com> X-Mailer: git-send-email 1.9.1 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-06-08_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=2 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1805220000 definitions=main-1806080207 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-06-08_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1805220000 definitions=main-1806080207 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: X-Virus-Scanned: ClamAV using ClamSMTP In order to free memory that is marked MLOCK_ONFAULT, the memory region needs to be first unlocked, before calling MADV_DONTNEED. And if the region is to be reused as MLOCK_ONFAULT, we require another call to mlock2() with the MLOCK_ONFAULT flag. Let's simplify freeing memory that is set MLOCK_ONFAULT, by allowing MADV_DONTNEED to work directly for memory that is set MLOCK_ONFAULT. The locked memory limits, tracked by mm->locked_vm do not need to be adjusted in this case, since they were charged to the entire region when MLOCK_ONFAULT was initially set. Further, I don't think allowing MADV_FREE for MLOCK_ONFAULT regions makes sense, since the point of MLOCK_ONFAULT is for userspace to know when pages are locked in memory and thus to know when page faults will occur. Signed-off-by: Jason Baron Cc: Andrew Morton Cc: Michal Hocko Cc: Vlastimil Babka Cc: Joonsoo Kim Cc: Mel Gorman Cc: Kirill A. Shutemov --- mm/internal.h | 18 ++++++++++++++++++ mm/madvise.c | 4 ++-- mm/oom_kill.c | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 9e3654d..16c0041 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -15,6 +15,7 @@ #include #include #include +#include /* * The set of flags that only affect watermark checking and reclaim @@ -45,9 +46,26 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma, static inline bool can_madv_dontneed_vma(struct vm_area_struct *vma) { + return !(((vma->vm_flags & (VM_LOCKED|VM_LOCKONFAULT)) == VM_LOCKED) || + (vma->vm_flags & (VM_HUGETLB|VM_PFNMAP))); +} + +static inline bool can_madv_free_vma(struct vm_area_struct *vma) +{ return !(vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP)); } +static inline bool can_madv_dontneed_or_free_vma(struct vm_area_struct *vma, + int behavior) +{ + if (behavior == MADV_DONTNEED) + return can_madv_dontneed_vma(vma); + else if (behavior == MADV_FREE) + return can_madv_free_vma(vma); + else + return 0; +} + void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long addr, unsigned long end, diff --git a/mm/madvise.c b/mm/madvise.c index 4d3c922..61ff306 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -517,7 +517,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, int behavior) { *prev = vma; - if (!can_madv_dontneed_vma(vma)) + if (!can_madv_dontneed_or_free_vma(vma, behavior)) return -EINVAL; if (!userfaultfd_remove(vma, start, end)) { @@ -539,7 +539,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, */ return -ENOMEM; } - if (!can_madv_dontneed_vma(vma)) + if (!can_madv_dontneed_or_free_vma(vma, behavior)) return -EINVAL; if (end > vma->vm_end) { /* diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 8ba6cb8..9817d15 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -492,7 +492,7 @@ void __oom_reap_task_mm(struct mm_struct *mm) set_bit(MMF_UNSTABLE, &mm->flags); for (vma = mm->mmap ; vma; vma = vma->vm_next) { - if (!can_madv_dontneed_vma(vma)) + if (!can_madv_free_vma(vma)) continue; /*