---
title: "<linux kernel> 3.2 pdflush change "
author: "sunzixun"
date: "2012-03-01T17:46:28Z"
tags: []
categories: ["操作系统"]
slug: "linux-kernel-3-2-pdflush-change"
draft: false
lastmod: "2012-03-13T11:01:02Z"
id: 1434591
url: "https://sunzixun.iteye.com/blog/1434591"
---

sync\_inode - write an inode and its pages to disk.

 * @inode: the inode to sync

 

 

The function is responsible to synchronize all dirty inodes belongings to a given superblock 

 

 

task = kthread\_create(bdi\_writeback\_thread, &bdi-&gt;wb,

     "flush-%s", dev\_name(bdi-&gt;dev));

 

root@szx3:/home/szx# ps -ef|grep flush

root       950     2  0 08:55 ?        00:00:00 \[flush-8:0]

bdi\_writeback\_thread

 

 

struct bdi\_writeback {

struct backing\_dev\_info \*bdi; /* our parent bdi \*/

unsigned int nr;

 

unsigned long last\_old\_flush; /* last old data flush \*/

unsigned long last\_active; /* last time bdi thread was active \*/

 

struct task\_struct \*task; /* writeback thread \*/

struct timer\_list wakeup\_timer; /* used for delayed bdi thread wakeup \*/

struct list\_head b\_dirty; /* dirty inodes \*/

struct list\_head b\_io; /* parked for writeback \*/

struct list\_head b\_more\_io; /* parked for more writeback \*/

spinlock\_t list\_lock; /* protects the b\_* lists \*/

};

 

 

struct backing\_dev\_info {

struct list\_head bdi\_list;

unsigned long ra\_pages; /* max readahead in PAGE\_CACHE\_SIZE units \*/

unsigned long state; /* Always use atomic bitops on this \*/

unsigned int capabilities; /* Device capabilities \*/

congested\_fn \*congested\_fn; /* Function pointer if device is md/dm \*/

void \*congested\_data; /* Pointer to aux data for congested func \*/

 

char \*name;

 

struct percpu\_counter bdi\_stat\[NR\_BDI\_STAT\_ITEMS];

 

unsigned long bw\_time\_stamp; /* last time write bw is updated \*/

unsigned long dirtied\_stamp;

unsigned long written\_stamp; /* pages written at bw\_time\_stamp \*/

unsigned long write\_bandwidth; /* the estimated write bandwidth \*/

unsigned long avg\_write\_bandwidth; /* further smoothed write bw \*/

 

/*

 * The base dirty throttle rate, re-calculated on every 200ms.

 * All the bdi tasks' dirty rate will be curbed under it.

 * @dirty\_ratelimit tracks the estimated @balanced\_dirty\_ratelimit

 * in small steps and is much more smooth/stable than the latter.

 \*/

unsigned long dirty\_ratelimit;

unsigned long balanced\_dirty\_ratelimit;

 

struct prop\_local\_percpu completions;

int dirty\_exceeded;

 

unsigned int min\_ratio;

unsigned int max\_ratio, max\_prop\_frac;

 

struct bdi\_writeback wb;  /* default writeback info for this bdi \*/

spinlock\_t wb\_lock;   /* protects work\_list \*/

 

struct list\_head work\_list;

 

struct device \*dev;

 

struct timer\_list laptop\_mode\_wb\_timer;

 

#ifdef CONFIG\_DEBUG\_FS

struct dentry \*debug\_dir;

struct dentry \*debug\_stats;

#endif

};

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/*

 * Passed into wb\_writeback(), essentially a subset of writeback\_control

 \*/

struct wb\_writeback\_work {

long nr\_pages;

struct super\_block \*sb;

unsigned long \*older\_than\_this;

enum writeback\_sync\_modes sync\_mode;

unsigned int tagged\_writepages:1;

unsigned int for\_kupdate:1;

unsigned int range\_cyclic:1;

unsigned int for\_background:1;

enum wb\_reason reason; /* why was writeback initiated? \*/

 

struct list\_head list; /* pending work list \*/

struct completion \*done; /* set if the caller waits \*/

};

 

 

long wb\_do\_writeback(struct bdi\_writeback \*wb, int force\_wait)

{

struct backing\_dev\_info \*bdi = wb-&gt;bdi;

struct wb\_writeback\_work \*work;

long wrote = 0;

 

set\_bit(BDI\_writeback\_running, &wb-&gt;bdi-&gt;state);

while ((work = get\_next\_work\_item(bdi)) != NULL) {

/*

 * Override sync mode, in case we must wait for completion

 * because this thread is exiting now.

 \*/

if (force\_wait)

work-&gt;sync\_mode = WB\_SYNC\_ALL;

 

trace\_writeback\_exec(bdi, work);

 

wrote += wb\_writeback(wb, work);

 

/*

 * Notify the caller of completion if this is a synchronous

 * work item, otherwise just free it.

 \*/

if (work-&gt;done)

complete(work-&gt;done);

else

kfree(work);

}

 

/*

 * Check for periodic writeback, kupdated() style

 \*/

wrote += wb\_check\_old\_data\_flush(wb);

wrote += wb\_check\_background\_flush(wb);

clear\_bit(BDI\_writeback\_running, &wb-&gt;bdi-&gt;state);

 

return wrote;

}

 

 

 the first time one of an inode's pages is dirtied, we mark the dirtying-time in the inode's address\_space So this periodic writeback code just walks the superblock inode list, writing back any inodes which are older than a specific point in time.

 

 

\_\_wait\_on\_bit

 

 

wait\_queue\_head\_t \*bit\_waitqueue(void \*word, int bit)

{

const int shift = BITS\_PER\_LONG == 32 ? 5 : 6;

const struct zone \*zone = page\_zone(virt\_to\_page(word));

unsigned long val = (unsigned long)word &lt;&lt; shift | bit;

 

return &zone-&gt;wait\_table\[hash\_long(val, zone-&gt;wait\_table\_bits)];

}

 

Zone -&gt; wait\_table

 

Zone -&gt;  wait\_table\_bits

Power-of-2 order of the size of the wait queue hash table array

 

The purpose of all there is to keep track of the people waiting for a page to become available and make them runnable again when possible . The trouble is that this consumes a lot of space ,especially when so few things wait on pages at a given time.

So instead of using per-page wait-queues  , we use a waitqueue hash table.

 

 

 

 

 

BSF - Bit Scan Forward (386+)

        Usage:  BSF     dest,src

        Modifies flags: ZF

        Scans source operand for first bit set.  Sets ZF if a bit is found set and loads the destination with an index to first set bit.  Clears ZF is no bits are found set.  BSF scans forward across bit pattern (0-n) while BSR scans in reverse (n-0).

 

int zone\_wait\_table\_init(struct zone \*zone, unsigned long zone\_size\_pages)

{

int i;

struct pglist\_data \*pgdat = zone-&gt;zone\_pgdat;

size\_t alloc\_size;

 

/*

 * The per-page waitqueue mechanism uses hashed waitqueues

 * per zone.

 \*/

zone-&gt;wait\_table\_hash\_nr\_entries =

 wait\_table\_hash\_nr\_entries(zone\_size\_pages);

zone-&gt;wait\_table\_bits =

wait\_table\_bits(zone-&gt;wait\_table\_hash\_nr\_entries);

//....

}

 

 

 

 

从 低位 开始搜索 

static inline unsigned long \_\_ffs(unsigned long word)

{

asm("bsf %1,%0"

: "=r" (word)

: "rm" (word));

return word;

}

 

 

 

 

 

 

 \*/

static void inode\_wait\_for\_writeback(struct inode \*inode,

     struct bdi\_writeback \*wb)

{

DEFINE\_WAIT\_BIT(wq, &inode-&gt;i\_state, \_\_I\_SYNC);

wait\_queue\_head\_t \*wqh;

 

wqh = bit\_waitqueue(&inode-&gt;i\_state, \_\_I\_SYNC);

while (inode-&gt;i\_state & I\_SYNC) {

spin\_unlock(&inode-&gt;i\_lock);

spin\_unlock(&wb-&gt;list\_lock);

\_\_wait\_on\_bit(wqh, &wq, inode\_wait, TASK\_UNINTERRUPTIBLE);

spin\_lock(&wb-&gt;list\_lock);

spin\_lock(&inode-&gt;i\_lock);

}

}

 

 

The kernel can start to synchronize data from various different places , but all paths save one end up in 

\[sysc\_sb\_inodes] The function is responsible to synchronize all dirty inodes belonging to a given superblock , 

\[writeback\_single\_inode ] is used for each inode . 

 

static int

writeback\_single\_inode(struct inode \*inode, struct bdi\_writeback \*wb,

       struct writeback\_control \*wbc)

{

struct address\_space \*mapping = inode-&gt;i\_mapping;

long nr\_to\_write = wbc-&gt;nr\_to\_write;

unsigned dirty;

int ret;

 

assert\_spin\_locked(&wb-&gt;list\_lock);

assert\_spin\_locked(&inode-&gt;i\_lock);

 

if (!atomic\_read(&inode-&gt;i\_count))

WARN\_ON(!(inode-&gt;i\_state & (I\_WILL\_FREE|I\_FREEING)));

else

WARN\_ON(inode-&gt;i\_state & I\_WILL\_FREE);

 

if (inode-&gt;i\_state & I\_SYNC) {

/*

 * If this inode is locked for writeback and we are not doing

 * writeback-for-data-integrity, move it to b\_more\_io so that

 * writeback can proceed with the other inodes on s\_io.

 *

 * We'll have another go at writing back this inode when we

 * completed a full scan of b\_io.

 \*/

if (wbc-&gt;sync\_mode != WB\_SYNC\_ALL) {

requeue\_io(inode, wb);

trace\_writeback\_single\_inode\_requeue(inode, wbc,

     nr\_to\_write);

return 0;

}

 

/*

 * It's a data-integrity sync.  We must wait.

 \*/

inode\_wait\_for\_writeback(inode, wb);

}

 

BUG\_ON(inode-&gt;i\_state & I\_SYNC);

 

/* Set I\_SYNC, reset I\_DIRTY\_PAGES \*/

inode-&gt;i\_state |= I\_SYNC;

inode-&gt;i\_state &= ~I\_DIRTY\_PAGES;

spin\_unlock(&inode-&gt;i\_lock);

spin\_unlock(&wb-&gt;list\_lock);

 

ret = do\_writepages(mapping, wbc);

 

/*

 * Make sure to wait on the data before writing out the metadata.

 * This is important for filesystems that modify metadata on data

 * I/O completion.

 \*/

if (wbc-&gt;sync\_mode == WB\_SYNC\_ALL) {

int err = filemap\_fdatawait(mapping);

if (ret == 0)

ret = err;

}

 

/*

 * Some filesystems may redirty the inode during the writeback

 * due to delalloc, clear dirty metadata flags right before

 * write\_inode()

 \*/

spin\_lock(&inode-&gt;i\_lock);

dirty = inode-&gt;i\_state & I\_DIRTY;

inode-&gt;i\_state &= ~(I\_DIRTY\_SYNC | I\_DIRTY\_DATASYNC);

spin\_unlock(&inode-&gt;i\_lock);

/* Don't write the inode if only I\_DIRTY\_PAGES was set \*/

if (dirty & (I\_DIRTY\_SYNC | I\_DIRTY\_DATASYNC)) {

int err = write\_inode(inode, wbc);

if (ret == 0)

ret = err;

}

 

spin\_lock(&wb-&gt;list\_lock);

spin\_lock(&inode-&gt;i\_lock);

inode-&gt;i\_state &= ~I\_SYNC;

if (!(inode-&gt;i\_state & I\_FREEING)) {

/*

 * Sync livelock prevention. Each inode is tagged and synced in

 * one shot. If still dirty, it will be redirty\_tail()'ed below.

 * Update the dirty time to prevent enqueue and sync it again.

 \*/

if ((inode-&gt;i\_state & I\_DIRTY) &&

    (wbc-&gt;sync\_mode == WB\_SYNC\_ALL || wbc-&gt;tagged\_writepages))

inode-&gt;dirtied\_when = jiffies;

 

if (mapping\_tagged(mapping, PAGECACHE\_TAG\_DIRTY)) {

/*

 * We didn't write back all the pages.  nfs\_writepages()

 * sometimes bales out without doing anything.

 \*/

inode-&gt;i\_state |= I\_DIRTY\_PAGES;

if (wbc-&gt;nr\_to\_write &lt;= 0) {

/*

 * slice used up: queue for next turn

 \*/

requeue\_io(inode, wb);

} else {

/*

 * Writeback blocked by something other than

 * congestion. Delay the inode for some time to

 * avoid spinning on the CPU (100% iowait)

 * retrying writeback of the dirty page/inode

 * that cannot be performed immediately.

 \*/

redirty\_tail(inode, wb);

}

} else if (inode-&gt;i\_state & I\_DIRTY) {

/*

 * Filesystems can dirty the inode during writeback

 * operations, such as delayed allocation during

 * submission or metadata updates after data IO

 * completion.

 \*/

redirty\_tail(inode, wb);

} else {

/*

 * The inode is clean.  At this point we either have

 * a reference to the inode or it's on it's way out.

 * No need to add it back to the LRU.

 \*/

list\_del\_init(&inode-&gt;i\_wb\_list);

}

}

inode\_sync\_complete(inode);

trace\_writeback\_single\_inode(inode, wbc, nr\_to\_write);

return ret;

}