From patchwork Fri Jun 1 23:38:46 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 9083 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 2AAF623E37 for ; Fri, 1 Jun 2012 23:39:02 +0000 (UTC) Received: from mail-yw0-f52.google.com (mail-yw0-f52.google.com [209.85.213.52]) by fiordland.canonical.com (Postfix) with ESMTP id BC1EDA180A3 for ; Fri, 1 Jun 2012 23:39:01 +0000 (UTC) Received: by yhpp61 with SMTP id p61so2401007yhp.11 for ; Fri, 01 Jun 2012 16:39:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf:from:to:cc :subject:date:message-id:x-mailer:in-reply-to:references :x-content-scanned:x-cbid:x-gm-message-state; bh=wEOoCl9fmrbzTuPA7p71xXuY4gytpgEFmk96ONmc/vc=; b=DxuCwOWZN6V5ufgNrjHwbV4UCQHf5rmXkVU7sNwCiwGb7Dhg4tGQmgsljh+kJHMZ81 G3k7xIzpzkMyGJj538SYRW6p2e1fK7oorFhD27uC3nmICmrUOyfY5zv4MKr5u3P/CHYw /nf4hvoXD/xUASkMIoQOW7FkmB4aD08at2Jde7CsFQqjWIkMAZx9oWiPRiCzWFa/DENs BcpT2eCQfBs2piRRBH51mnQeK8NPU1VMDPtHh+0haryzRbu6++fPZS/5xMP/rrWqYbFK KQUt2ZryCpx2oBjk/br4+daDXA3J/sSueHSv1D+Ny5GeQjTHo+QWlehRaH8rnQrZ4e0G +lbw== Received: by 10.50.193.196 with SMTP id hq4mr3098537igc.57.1338593940700; Fri, 01 Jun 2012 16:39:00 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.24.148 with SMTP id v20csp276ibb; Fri, 1 Jun 2012 16:39:00 -0700 (PDT) Received: by 10.68.228.130 with SMTP id si2mr1597054pbc.59.1338593939598; Fri, 01 Jun 2012 16:38:59 -0700 (PDT) Received: from e3.ny.us.ibm.com (e3.ny.us.ibm.com. [32.97.182.143]) by mx.google.com with ESMTPS id to9si6566668pbc.188.2012.06.01.16.38.58 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 01 Jun 2012 16:38:59 -0700 (PDT) Received-SPF: pass (google.com: domain of jstultz@us.ibm.com designates 32.97.182.143 as permitted sender) client-ip=32.97.182.143; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jstultz@us.ibm.com designates 32.97.182.143 as permitted sender) smtp.mail=jstultz@us.ibm.com Received: from /spool/local by e3.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 1 Jun 2012 19:38:58 -0400 Received: from d01dlp02.pok.ibm.com (9.56.224.85) by e3.ny.us.ibm.com (192.168.1.103) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 1 Jun 2012 19:38:56 -0400 Received: from d01relay02.pok.ibm.com (d01relay02.pok.ibm.com [9.56.227.234]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id 6FC746E804A; Fri, 1 Jun 2012 19:38:56 -0400 (EDT) Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by d01relay02.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q51Ncucv152058; Fri, 1 Jun 2012 19:38:56 -0400 Received: from d01av04.pok.ibm.com (loopback [127.0.0.1]) by d01av04.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q51Ncsca011871; Fri, 1 Jun 2012 19:38:56 -0400 Received: from kernel.beaverton.ibm.com (kernel.beaverton.ibm.com [9.47.67.96]) by d01av04.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q51NcsNY011851; Fri, 1 Jun 2012 19:38:54 -0400 Received: by kernel.beaverton.ibm.com (Postfix, from userid 1056) id 530D0C0624; Fri, 1 Jun 2012 16:38:53 -0700 (PDT) From: John Stultz To: LKML Cc: John Stultz , Andrew Morton , Android Kernel Team , Robert Love , Mel Gorman , Hugh Dickins , Dave Hansen , Rik van Riel , Dmitry Adamushko , Dave Chinner , Neil Brown , Andrea Righi , "Aneesh Kumar K.V" , Taras Glek , Mike Hommey , Jan Kara , KOSAKI Motohiro Subject: [PATCH 3/3] [RFC] tmpfs: Add FALLOC_FL_MARK_VOLATILE/UNMARK_VOLATILE handlers Date: Fri, 1 Jun 2012 16:38:46 -0700 Message-Id: <1338593926-17344-4-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.7.3.2.146.gca209 In-Reply-To: <1338593926-17344-1-git-send-email-john.stultz@linaro.org> References: <1338593926-17344-1-git-send-email-john.stultz@linaro.org> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12060123-8974-0000-0000-000009ACDE7B X-Gm-Message-State: ALoCoQlxzqUsDnf/EQxighyhv1obeKX59Gy5ZlHsXmqMHniEVoGMIxZDhEr1WktqEgECaQLdlQRW This patch enables FALLOC_FL_MARK_VOLATILE/UNMARK_VOLATILE functionality for tmpfs making use of the volatile range management code. Conceptually, FALLOC_FL_MARK_VOLATILE is like a delayed FALLOC_FL_PUNCH_HOLE. This allows applications that have data caches that can be re-created to tell the kernel that some memory contains data that is useful in the future, but can be recreated if needed, so if the kernel needs, it can zap the memory without having to swap it out. In use, applications use FALLOC_FL_MARK_VOLATILE to mark page ranges as volatile when they are not in use. Then later if they wants to reuse the data, they use FALLOC_FL_UNMARK_VOLATILE, which will return an error if the data has been purged. This is very much influenced by the Android Ashmem interface by Robert Love so credits to him and the Android developers. In many cases the code & logic come directly from the ashmem patch. The intent of this patch is to allow for ashmem-like behavior, but embeds the idea a little deeper into the VM code. This is a reworked version of the fadvise volatile idea submitted earlier to the list. Thanks to Dave Chinner for suggesting to rework the idea in this fashion. Also thanks to Dmitry Adamushko for continued review and bug reporting, and Dave Hansen for help with the original design and mentoring me in the VM code. v3: * Fix off by one issue when truncating page ranges * Use Dave Hansesn's suggestion to use shmem_writepage to trigger range purging instead of using a shrinker. CC: Andrew Morton CC: Android Kernel Team CC: Robert Love CC: Mel Gorman CC: Hugh Dickins CC: Dave Hansen CC: Rik van Riel CC: Dmitry Adamushko CC: Dave Chinner CC: Neil Brown CC: Andrea Righi CC: Aneesh Kumar K.V CC: Taras Glek CC: Mike Hommey CC: Jan Kara CC: KOSAKI Motohiro Signed-off-by: John Stultz --- fs/open.c | 3 +- include/linux/falloc.h | 7 ++-- mm/shmem.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/fs/open.c b/fs/open.c index d543012..448ed5a 100644 --- a/fs/open.c +++ b/fs/open.c @@ -223,7 +223,8 @@ int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len) return -EINVAL; /* Return error if mode is not supported */ - if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) + if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | + FALLOC_FL_MARK_VOLATILE | FALLOC_FL_UNMARK_VOLATILE)) return -EOPNOTSUPP; /* Punch hole must have keep size set */ diff --git a/include/linux/falloc.h b/include/linux/falloc.h index 73e0b62..3e47ad5 100644 --- a/include/linux/falloc.h +++ b/include/linux/falloc.h @@ -1,9 +1,10 @@ #ifndef _FALLOC_H_ #define _FALLOC_H_ -#define FALLOC_FL_KEEP_SIZE 0x01 /* default is extend size */ -#define FALLOC_FL_PUNCH_HOLE 0x02 /* de-allocates range */ - +#define FALLOC_FL_KEEP_SIZE 0x01 /* default is extend size */ +#define FALLOC_FL_PUNCH_HOLE 0x02 /* de-allocates range */ +#define FALLOC_FL_MARK_VOLATILE 0x04 /* mark range volatile */ +#define FALLOC_FL_UNMARK_VOLATILE 0x08 /* mark range non-volatile */ #ifdef __KERNEL__ /* diff --git a/mm/shmem.c b/mm/shmem.c index d576b84..0cc33a4 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -64,6 +64,7 @@ static struct vfsmount *shm_mnt; #include #include #include +#include #include #include @@ -624,11 +625,79 @@ static int shmem_setattr(struct dentry *dentry, struct iattr *attr) return error; } +static DEFINE_VOLATILE_FS_HEAD(shmem_volatile_head); + +static int shmem_mark_volatile(struct inode *inode, loff_t offset, loff_t len) +{ + loff_t lstart, lend; + int ret; + + lstart = offset >> PAGE_CACHE_SHIFT; + lend = (offset+len) >> PAGE_CACHE_SHIFT; + + volatile_range_lock(&shmem_volatile_head); + ret = volatile_range_add(&shmem_volatile_head, &inode->i_data, + lstart, lend); + if (ret > 0) { /* immdiately purge */ + shmem_truncate_range(inode, lstart<> PAGE_CACHE_SHIFT; + lend = (offset+len) >> PAGE_CACHE_SHIFT; + + volatile_range_lock(&shmem_volatile_head); + ret = volatile_range_remove(&shmem_volatile_head, + &inode->i_data, + lstart, lend); + volatile_range_unlock(&shmem_volatile_head); + + return ret; +} + +static void shmem_clear_volatile(struct inode *inode) +{ + volatile_range_lock(&shmem_volatile_head); + volatile_range_clear(&shmem_volatile_head, &inode->i_data); + volatile_range_unlock(&shmem_volatile_head); +} + + + +static int shmem_purge_one_volatile_range(void) +{ + struct address_space *mapping; + loff_t start, end; + int ret; + + volatile_range_lock(&shmem_volatile_head); + ret = volatile_ranges_pluck_lru(&shmem_volatile_head, &mapping, + &start, &end); + if (ret) + shmem_truncate_range(mapping->host, start<i_mapping->a_ops == &shmem_aops) { shmem_unacct_size(info->flags, inode->i_size); inode->i_size = 0; @@ -776,6 +845,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) struct inode *inode; swp_entry_t swap; pgoff_t index; + int purged; BUG_ON(!PageLocked(page)); mapping = page->mapping; @@ -830,6 +900,12 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) SetPageUptodate(page); } + + /* If we have volatile pages, try purging them first */ + purged = shmem_purge_one_volatile_range(); + if (purged) + goto redirty; + swap = get_swap_page(); if (!swap.val) goto redirty; @@ -1789,6 +1865,14 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, /* No need to unmap again: hole-punching leaves COWed pages */ error = 0; goto out; + } else if (mode & FALLOC_FL_MARK_VOLATILE) { + /* Mark pages volatile, sort of delayed hole punching */ + error = shmem_mark_volatile(inode, offset, len); + goto out; + } else if (mode & FALLOC_FL_UNMARK_VOLATILE) { + /* Mark pages non-volatile, return error if pages were purged */ + error = shmem_unmark_volatile(inode, offset, len); + goto out; } /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */