linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
To: linux-mm <linux-mm@kvack.org>
Subject: [RFC:PATCH 02/07] Core function for packing, unpacking, and freeing file tails
Date: Wed, 29 Aug 2007 16:53:36 -0400	[thread overview]
Message-ID: <20070829205336.28328.58040.sendpatchset@norville.austin.ibm.com> (raw)
In-Reply-To: <20070829205325.28328.67953.sendpatchset@norville.austin.ibm.com>

Core function for packing, unpacking, and freeing file tails

Signed-off-by: Dave Kleikamp <shaggy@linux.vnet.ibm.com>

---

 include/linux/vm_file_tail.h |   71 ++++++++++++++++++++
 mm/Makefile                  |    1 
 mm/file_tail.c               |  148 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 220 insertions(+)

diff -Nurp linux001/include/linux/vm_file_tail.h linux002/include/linux/vm_file_tail.h
--- linux001/include/linux/vm_file_tail.h	1969-12-31 18:00:00.000000000 -0600
+++ linux002/include/linux/vm_file_tail.h	2007-08-29 13:27:46.000000000 -0500
@@ -0,0 +1,71 @@
+#ifndef FILE_TAIL_H
+#define FILE_TAIL_H
+
+#include <linux/fs.h>
+#include <linux/pagemap.h>
+
+/*
+ * This file deals with storing tails of files in buffers smaller than a page.
+ *
+ * FIXME: The contents of the file could possibly go into linux/pagemap.h.
+ */
+
+#ifdef CONFIG_VM_FILE_TAILS
+
+static inline int vm_file_tail_packed(struct address_space *mapping)
+{
+	return (mapping->tail != NULL);
+}
+
+static inline unsigned long vm_file_tail_index(struct address_space *mapping)
+{
+	return (unsigned long) (i_size_read(mapping->host) >> PAGE_CACHE_SHIFT);
+}
+
+static inline int vm_file_tail_length(struct address_space *mapping)
+{
+	return (int) (i_size_read(mapping->host) & (PAGE_CACHE_SIZE - 1));
+}
+
+static inline void vm_file_tail_free(struct address_space *mapping)
+{
+	unsigned long flags;
+	void *tail;
+	if (mapping && mapping->tail) {
+		spin_lock_irqsave(&mapping->tail_lock, flags);
+		tail = mapping->tail;
+		mapping->tail = NULL;
+		spin_unlock_irqrestore(&mapping->tail_lock, flags);
+		kfree(tail);
+	}
+}
+
+/*
+ * vm_file_tail_pack() returns 1 on success, 0 otherwise
+ *
+ * The caller must hold a reference on the page
+ */
+int vm_file_tail_pack(struct page *);
+void vm_file_tail_unpack(struct address_space *);
+
+/*
+ * Unpack the tail if it's at the specified index
+ */
+static inline void vm_file_tail_unpack_index(struct address_space *mapping,
+					     unsigned long index)
+{
+	if (index == vm_file_tail_index(mapping) && mapping->tail)
+		vm_file_tail_unpack(mapping);
+}
+
+#else /* !CONFIG_VM_FILE_TAILS */
+
+#define vm_file_tail_packed(mapping) 0
+#define vm_file_tail_free(mapping) do {} while (0)
+#define vm_file_tail_pack(page) 0
+#define vm_file_tail_unpack(mapping) do {} while (0)
+#define vm_file_tail_unpack_index(mapping, index) do {} while (0)
+
+#endif /* CONFIG_VM_FILE_TAILS */
+
+#endif	/* FILE_TAIL_H */
diff -Nurp linux001/mm/Makefile linux002/mm/Makefile
--- linux001/mm/Makefile	2007-08-28 09:57:20.000000000 -0500
+++ linux002/mm/Makefile	2007-08-29 13:27:46.000000000 -0500
@@ -29,4 +29,5 @@ obj-$(CONFIG_FS_XIP) += filemap_xip.o
 obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_SMP) += allocpercpu.o
 obj-$(CONFIG_QUICKLIST) += quicklist.o
+obj-$(CONFIG_VM_FILE_TAILS) += file_tail.o
 
diff -Nurp linux001/mm/file_tail.c linux002/mm/file_tail.c
--- linux001/mm/file_tail.c	1969-12-31 18:00:00.000000000 -0600
+++ linux002/mm/file_tail.c	2007-08-29 13:27:46.000000000 -0500
@@ -0,0 +1,148 @@
+/*
+ *	linux/mm/file_tail.c
+ *
+ * Copyright (C) International Business Machines  Corp., 2006-2007
+ * Author: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
+ */
+
+/*
+ * VM File Tails are used to compactly store the data at the end of the
+ * file in a small SLAB-allocated buffer when the base page size is large.
+ */
+
+#include <linux/buffer_head.h>
+#include <linux/fs.h>
+#include <linux/hardirq.h>
+#include <linux/vm_file_tail.h>
+
+/*
+ * Unpack tail into page cache.
+ *
+ * The tail is never modfied, and can be safely discarded on error
+ */
+void vm_file_tail_unpack(struct address_space *mapping)
+{
+	unsigned int flags;
+	gfp_t gfp_mask;
+	pgoff_t index;
+	void *kaddr;
+	int length;
+	struct page *page;
+	void *tail;
+
+	if (!mapping->tail)
+		return;
+
+	/* Allocate page */
+
+	if (in_atomic())
+		gfp_mask = GFP_NOWAIT;
+	else
+		gfp_mask = mapping_gfp_mask(mapping);
+
+	page = __page_cache_alloc(gfp_mask);
+
+	/* Copy data from tail to new page */
+	if (page) {
+		spin_lock_irqsave(&mapping->tail_lock, flags);
+		index = vm_file_tail_index(mapping);
+		length = vm_file_tail_length(mapping);
+		tail = mapping->tail;
+		mapping->tail = NULL;
+		spin_unlock_irqrestore(&mapping->tail_lock, flags);
+
+		if (!tail) {	/* someone else freed the tail */
+			page_cache_release(page);
+			return;
+		}
+
+		kaddr = kmap_atomic(page, KM_USER0);
+		memcpy(kaddr, tail, length);
+		memset(kaddr + length, 0, PAGE_CACHE_SIZE - length);
+		kunmap_atomic(kaddr, KM_USER0);
+
+		kfree(tail);
+
+		add_to_page_cache_lru(page, mapping, index, gfp_mask);
+		unlock_page(page);
+		page_cache_release(page);
+	} else
+		/* Free the tail */
+		vm_file_tail_free(mapping);
+}
+
+/* * Determine if the page is eligible to be packed, and if so, pack it
+ *
+ * Non-fatal if this fails.  The page will remain in the page cache.
+ */
+int vm_file_tail_pack(struct page *page)
+{
+	unsigned long flags;
+	pgoff_t index;
+	void *kaddr;
+	int length;
+	struct address_space *mapping;
+	void *tail;
+
+	if (TestSetPageLocked(page))
+		return 0;
+
+	mapping = page->mapping;
+
+	if (!mapping ||
+	    mapping->tail ||
+	    PageDirty(page) ||
+	    !PageUptodate(page) ||
+	    PageWriteback(page) ||
+	    (page_count(page) > 2) ||
+	    mapping_mapped(mapping) ||
+	    PageSwapCache(page)) {
+		unlock_page(page);
+		return 0;
+	}
+
+	index = vm_file_tail_index(mapping);
+	length = vm_file_tail_length(mapping);
+
+	if ((index != page->index) ||
+	    (length > PAGE_CACHE_SIZE / 2)) {
+		unlock_page(page);
+		return 0;
+	}
+
+	if (PagePrivate(page) && !try_to_release_page(page, 0)) {
+		unlock_page(page);
+		return 0;
+	}
+
+	tail = kmalloc(length, GFP_NOWAIT);
+	if (!tail) {
+		unlock_page(page);
+		return 0;
+	}
+
+	kaddr = kmap_atomic(page, KM_USER0);
+	memcpy(tail, kaddr, length);
+	kunmap_atomic(kaddr, KM_USER0);
+
+	spin_lock_irqsave(&mapping->tail_lock, flags);
+
+	/* Check again under spinlock */
+	if (mapping->tail || (index != vm_file_tail_index(mapping)) ||
+	   (length != vm_file_tail_length(mapping))) {
+		/* File size must have changed */
+		spin_unlock_irqrestore(&mapping->tail_lock, flags);
+		unlock_page(page);
+		return 0;
+	}
+
+	mapping->tail = tail;
+
+	spin_unlock_irqrestore(&mapping->tail_lock, flags);
+
+	remove_from_page_cache(page);
+	page_cache_release(page);	/* pagecache ref */
+	unlock_page(page);
+
+	return 1;
+}

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  parent reply	other threads:[~2007-08-29 20:53 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-29 20:53 [RFC:PATCH 00/07] VM File Tails Dave Kleikamp
2007-08-29 20:53 ` [RFC:PATCH 01/07] Add tail to address space Dave Kleikamp
2007-08-29 20:53 ` Dave Kleikamp [this message]
2007-08-29 20:53 ` [RFC:PATCH 03/07] Release tail when inode is freed Dave Kleikamp
2007-08-29 20:53 ` [RFC:PATCH 04/07] Unpack or remove file tail when inode is resized Dave Kleikamp
2007-08-29 20:53 ` [RFC:PATCH 05/07] find_get_page() and find_lock_page() need to unpack the tail Dave Kleikamp
2007-08-29 20:54 ` [RFC:PATCH 06/07] For readahead, leave data in tail Dave Kleikamp
2007-08-29 20:54 ` [RFC:PATCH 07/07] shrink_active_list: pack file tails rather than move to inactive list Dave Kleikamp
2007-08-29 21:31 ` [RFC:PATCH 00/07] VM File Tails Jörn Engel
2007-08-29 21:45   ` Dave Kleikamp
2007-08-29 23:38     ` Jörn Engel
2007-08-30  2:15       ` Dave Kleikamp
2007-08-30 10:11         ` Jörn Engel
2007-08-31 21:00 ` Luiz Fernando N. Capitulino
2007-08-31 21:47   ` Dave Kleikamp
2007-09-03 21:09     ` Luiz Fernando N. Capitulino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070829205336.28328.58040.sendpatchset@norville.austin.ibm.com \
    --to=shaggy@linux.vnet.ibm.com \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox