From patchwork Wed Apr 3 23:52:23 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 15889 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 0FEFD23E39 for ; Wed, 3 Apr 2013 23:52:49 +0000 (UTC) Received: from mail-ve0-f200.google.com (mail-ve0-f200.google.com [209.85.128.200]) by fiordland.canonical.com (Postfix) with ESMTP id A83CDA18B09 for ; Wed, 3 Apr 2013 23:52:48 +0000 (UTC) Received: by mail-ve0-f200.google.com with SMTP id m1sf3370451ves.7 for ; Wed, 03 Apr 2013 16:52:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:mime-version:x-beenthere:x-received:received-spf :x-received:x-forwarded-to:x-forwarded-for:delivered-to:x-received :received-spf:x-received:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references:x-gm-message-state:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe; bh=+InUNeL5X0z5X5DcwAEyxRPxc6Wqw5VsV/XV5ID1jFQ=; b=pDs3pXPq8vMAslAkqxGtZzFTT/nnr+eBwm+Ld2ABOMwhIZxEJYBP/gPwxoMO98MpqO MeI+WWi0HISXm3SsiU3Z94fxLMJXMJ8lvAiIVyc4ZHKsb/gbsXJMk40vmNZgef3Mi0sO 9DqJk6aBsu1HP+U4tv5BKRj9SasThPUXpU+GMpH7TTvo17ffhKwVMcdMrIoxfBLHBX+N FWcl0naC6uueuqsT/VGwiD9qs5Mz49R/utAUKrdOGLMlHsbZC7+t2iVH7Q+iYdemdsy+ hv7G1VVPBQpUIvnmR3m0yWamwtSeAdHIqP7rVn0jsqHGNwcvQe6qQN1nUKQl75KnTauu zsiQ== X-Received: by 10.224.157.1 with SMTP id z1mr2096693qaw.8.1365033168113; Wed, 03 Apr 2013 16:52:48 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.130.134 with SMTP id oe6ls1198204qeb.46.gmail; Wed, 03 Apr 2013 16:52:47 -0700 (PDT) X-Received: by 10.52.19.239 with SMTP id i15mr2585425vde.47.1365033167839; Wed, 03 Apr 2013 16:52:47 -0700 (PDT) Received: from mail-vc0-f172.google.com (mail-vc0-f172.google.com [209.85.220.172]) by mx.google.com with ESMTPS id do1si6584670vdb.147.2013.04.03.16.52.47 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 03 Apr 2013 16:52:47 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.172 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.172; Received: by mail-vc0-f172.google.com with SMTP id hr11so1919935vcb.31 for ; Wed, 03 Apr 2013 16:52:47 -0700 (PDT) X-Received: by 10.52.76.103 with SMTP id j7mr2603882vdw.90.1365033167680; Wed, 03 Apr 2013 16:52:47 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.59.4.204 with SMTP id cg12csp191420ved; Wed, 3 Apr 2013 16:52:47 -0700 (PDT) X-Received: by 10.66.139.169 with SMTP id qz9mr6173353pab.159.1365033166748; Wed, 03 Apr 2013 16:52:46 -0700 (PDT) Received: from mail-pa0-f49.google.com (mail-pa0-f49.google.com [209.85.220.49]) by mx.google.com with ESMTPS id au5si8475708pbd.109.2013.04.03.16.52.46 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 03 Apr 2013 16:52:46 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.49 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) client-ip=209.85.220.49; Received: by mail-pa0-f49.google.com with SMTP id kp14so1161445pab.22 for ; Wed, 03 Apr 2013 16:52:46 -0700 (PDT) X-Received: by 10.66.122.74 with SMTP id lq10mr6247276pab.78.1365033166310; Wed, 03 Apr 2013 16:52:46 -0700 (PDT) Received: from localhost.localdomain (c-24-21-54-107.hsd1.or.comcast.net. [24.21.54.107]) by mx.google.com with ESMTPS id ql7sm7752384pbb.2.2013.04.03.16.52.44 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 03 Apr 2013 16:52:45 -0700 (PDT) From: John Stultz To: linux-kernel@vger.kernel.org Cc: John Stultz , linux-mm@kvack.org, Michael Kerrisk , Arun Sharma , Mel Gorman , Hugh Dickins , Dave Hansen , Rik van Riel , Neil Brown , Mike Hommey , Taras Glek , KOSAKI Motohiro , KAMEZAWA Hiroyuki , Jason Evans , sanjay@google.com, Paul Turner , Johannes Weiner , Michel Lespinasse , Andrew Morton , Minchan Kim Subject: [RFC PATCH 4/4] vrange: Enable purging of file backed volatile ranges Date: Wed, 3 Apr 2013 16:52:23 -0700 Message-Id: <1365033144-15156-5-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1365033144-15156-1-git-send-email-john.stultz@linaro.org> References: <1365033144-15156-1-git-send-email-john.stultz@linaro.org> X-Gm-Message-State: ALoCoQmY+ju9PRyySYQbf8CEh5H1CcWTeSe1Y74Ya8JPrHl09j5A+ZsoV8IlS3sPl21U5QkOmWie X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.172 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Rework the victim range selection to also support file backed volatile ranges. Cc: linux-mm@kvack.org Cc: Michael Kerrisk Cc: Arun Sharma Cc: Mel Gorman Cc: Hugh Dickins Cc: Dave Hansen Cc: Rik van Riel Cc: Neil Brown Cc: Mike Hommey Cc: Taras Glek Cc: KOSAKI Motohiro Cc: KAMEZAWA Hiroyuki Cc: Jason Evans Cc: sanjay@google.com Cc: Paul Turner Cc: Johannes Weiner Cc: Michel Lespinasse Cc: Andrew Morton Cc: Minchan Kim Signed-off-by: John Stultz --- include/linux/vrange.h | 8 ++++ mm/vrange.c | 118 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 89 insertions(+), 37 deletions(-) diff --git a/include/linux/vrange.h b/include/linux/vrange.h index 91960eb..bada2bd 100644 --- a/include/linux/vrange.h +++ b/include/linux/vrange.h @@ -47,6 +47,14 @@ static inline struct mm_struct *vrange_get_owner_mm(struct vrange *vrange) return container_of(vrange->owner, struct mm_struct, vroot); } +static inline +struct address_space *vrange_get_owner_mapping(struct vrange *vrange) +{ + if (vrange_type(vrange) != VRANGE_FILE) + return NULL; + return container_of(vrange->owner, struct address_space, vroot); +} + void vrange_init(void); extern void mm_exit_vrange(struct mm_struct *mm); diff --git a/mm/vrange.c b/mm/vrange.c index 671909c..b652513 100644 --- a/mm/vrange.c +++ b/mm/vrange.c @@ -690,8 +690,9 @@ static unsigned int discard_vma_pages(struct zone *zone, struct mm_struct *mm, return ret; } -unsigned int discard_vrange(struct zone *zone, struct vrange *vrange, - int nr_to_discard) +static unsigned int discard_anon_vrange(struct zone *zone, + struct vrange *vrange, + int nr_to_discard) { struct mm_struct *mm; unsigned long start = vrange->node.start; @@ -732,52 +733,91 @@ out: return nr_discarded; } +static unsigned int discard_file_vrange(struct zone *zone, + struct vrange *vrange, + int nr_to_discard) +{ + struct address_space *mapping; + unsigned long start = vrange->node.start; + unsigned long end = vrange->node.last; + unsigned long count = ((end-start) >> PAGE_CACHE_SHIFT); + + mapping = vrange_get_owner_mapping(vrange); + + truncate_inode_pages_range(mapping, start, end); + vrange->purged = true; + + return count; +} + +unsigned int discard_vrange(struct zone *zone, struct vrange *vrange, + int nr_to_discard) +{ + if (vrange_type(vrange) == VRANGE_ANON) + return discard_anon_vrange(zone, vrange, nr_to_discard); + return discard_file_vrange(zone, vrange, nr_to_discard); +} + + +/* Take a vrange refcount and depending on the type + * the vrange->owner's mm refcount or inode refcount + */ +static int hold_victim_vrange(struct vrange *vrange) +{ + if (vrange_type(vrange) == VRANGE_ANON) { + struct mm_struct *mm = vrange_get_owner_mm(vrange); + + + if (atomic_read(&mm->mm_users) == 0) + return -1; + + + if (!atomic_inc_not_zero(&vrange->refcount)) + return -1; + /* + * we need to access mmap_sem further routine so + * need to get a refcount of mm. + * NOTE: We guarantee mm_count isn't zero in here because + * if we found vrange from LRU list, it means we are + * before exit_vrange or remove_vrange. + */ + atomic_inc(&mm->mm_count); + } else { + struct address_space *mapping; + mapping = vrange_get_owner_mapping(vrange); + + if (!atomic_inc_not_zero(&vrange->refcount)) + return -1; + __iget(mapping->host); + } + + return 0; +} + + + /* - * Get next victim vrange from LRU and hold a vrange refcount - * and vrange->mm's refcount. + * Get next victim vrange from LRU and hold needed refcounts. */ static struct vrange *get_victim_vrange(void) { - struct mm_struct *mm; struct vrange *vrange = NULL; struct list_head *cur, *tmp; spin_lock(&lru_lock); list_for_each_prev_safe(cur, tmp, &lru_vrange) { vrange = list_entry(cur, struct vrange, lru); - mm = vrange_get_owner_mm(vrange); - - if (!mm) { - vrange = NULL; - continue; - } - /* the process is exiting so pass it */ - if (atomic_read(&mm->mm_users) == 0) { + if (hold_victim_vrange(vrange)) { list_del_init(&vrange->lru); vrange = NULL; continue; } - /* vrange is freeing so continue to loop */ - if (!atomic_inc_not_zero(&vrange->refcount)) { - list_del_init(&vrange->lru); - vrange = NULL; - continue; - } - - /* - * we need to access mmap_sem further routine so - * need to get a refcount of mm. - * NOTE: We guarantee mm_count isn't zero in here because - * if we found vrange from LRU list, it means we are - * before mm_exit_vrange or remove_vrange. - */ - atomic_inc(&mm->mm_count); - /* Isolate vrange */ list_del_init(&vrange->lru); break; + } spin_unlock(&lru_lock); @@ -786,11 +826,18 @@ static struct vrange *get_victim_vrange(void) static void put_victim_range(struct vrange *vrange) { - struct mm_struct *mm = vrange_get_owner_mm(vrange); - put_vrange(vrange); - if (mm) + + if (vrange_type(vrange) == VRANGE_ANON) { + struct mm_struct *mm = vrange_get_owner_mm(vrange); + mmdrop(mm); + } else { + struct address_space *mapping; + + mapping = vrange_get_owner_mapping(vrange); + iput(mapping->host); + } } unsigned int discard_vrange_pages(struct zone *zone, int nr_to_discard) @@ -799,11 +846,8 @@ unsigned int discard_vrange_pages(struct zone *zone, int nr_to_discard) unsigned int nr_discarded = 0; start_vrange = vrange = get_victim_vrange(); - if (start_vrange) { - struct mm_struct *mm = vrange_get_owner_mm(vrange); - atomic_inc(&start_vrange->refcount); - atomic_inc(&mm->mm_count); - } + if (start_vrange) + hold_victim_vrange(start_vrange); while (vrange) { nr_discarded += discard_vrange(zone, vrange, nr_to_discard);