From: Alexander Duyck Date: Wed, 31 Dec 2014 10:56:49 -0800 Subject: [PATCH] fib_trie: Push tnode flushing down to inflate/halve This change pushes the tnode freeing down into the inflate and halve functions. It makes more sense here as we have a better grasp of what is going on and when a given cluster of nodes is ready to be freed. I believe this may address a bug in the freeing logic as well. For some reason if the freelist got to a certain size we would call synchronize_rcu(). I'm assuming that what they meant to do is call synchronize_rcu() after they had handed off that much memory via call_rcu(). As such that is what I have updated the behavior to be. Signed-off-by: Alexander Duyck Signed-off-by: David S. Miller --- --- a/net/ipv4/fib_trie.c +++ b/net/ipv4/fib_trie.c @@ -147,8 +147,6 @@ struct trie { }; static void resize(struct trie *t, struct tnode *tn); -/* tnodes to free after resize(); protected by RTNL */ -static struct callback_head *tnode_free_head; static size_t tnode_free_size; /* @@ -307,32 +305,6 @@ static struct tnode *tnode_alloc(size_t return vzalloc(size); } -static void tnode_free_safe(struct tnode *tn) -{ - BUG_ON(IS_LEAF(tn)); - tn->rcu.next = tnode_free_head; - tnode_free_head = &tn->rcu; -} - -static void tnode_free_flush(void) -{ - struct callback_head *head; - - while ((head = tnode_free_head)) { - struct tnode *tn = container_of(head, struct tnode, rcu); - - tnode_free_head = head->next; - tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]); - - node_free(tn); - } - - if (tnode_free_size >= PAGE_SIZE * sync_pages) { - tnode_free_size = 0; - synchronize_rcu(); - } -} - static struct tnode *leaf_new(t_key key) { struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL); @@ -433,17 +405,33 @@ static void put_child_root(struct tnode rcu_assign_pointer(t->trie, n); } -static void tnode_clean_free(struct tnode *tn) +static inline void tnode_free_init(struct tnode *tn) { - struct tnode *tofree; - unsigned long i; + tn->rcu.next = NULL; +} + +static inline void tnode_free_append(struct tnode *tn, struct tnode *n) +{ + n->rcu.next = tn->rcu.next; + tn->rcu.next = &n->rcu; +} - for (i = 0; i < tnode_child_length(tn); i++) { - tofree = tnode_get_child(tn, i); - if (tofree) - node_free(tofree); +static void tnode_free(struct tnode *tn) +{ + struct callback_head *head = &tn->rcu; + + while (head) { + head = head->next; + tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]); + node_free(tn); + + tn = container_of(head, struct tnode, rcu); + } + + if (tnode_free_size >= PAGE_SIZE * sync_pages) { + tnode_free_size = 0; + synchronize_rcu(); } - node_free(tn); } static int inflate(struct trie *t, struct tnode *oldtnode) @@ -476,20 +464,23 @@ static int inflate(struct trie *t, struc inode->bits - 1); if (!left) goto nomem; + tnode_free_append(tn, left); right = tnode_new(inode->key | m, inode->pos, inode->bits - 1); - if (!right) { - node_free(left); + if (!right) goto nomem; - } + tnode_free_append(tn, right); put_child(tn, 2*i, left); put_child(tn, 2*i+1, right); } } + /* prepare oldtnode to be freed */ + tnode_free_init(oldtnode); + for (i = 0; i < olen; i++) { struct tnode *inode = tnode_get_child(oldtnode, i); struct tnode *left, *right; @@ -505,12 +496,13 @@ static int inflate(struct trie *t, struc continue; } + /* drop the node in the old tnode free list */ + tnode_free_append(oldtnode, inode); + /* An internal node with two children */ if (inode->bits == 1) { put_child(tn, 2*i, rtnl_dereference(inode->child[0])); put_child(tn, 2*i+1, rtnl_dereference(inode->child[1])); - - tnode_free_safe(inode); continue; } @@ -556,17 +548,19 @@ static int inflate(struct trie *t, struc put_child(tn, 2 * i, left); put_child(tn, 2 * i + 1, right); - tnode_free_safe(inode); - + /* resize child nodes */ resize(t, left); resize(t, right); } put_child_root(tp, t, tn->key, tn); - tnode_free_safe(oldtnode); + + /* we completed without error, prepare to free old node */ + tnode_free(oldtnode); return 0; nomem: - tnode_clean_free(tn); + /* all pointers should be clean so we are done */ + tnode_free(tn); return -ENOMEM; } @@ -599,17 +593,20 @@ static int halve(struct trie *t, struct struct tnode *newn; newn = tnode_new(left->key, oldtnode->pos, 1); - if (!newn) { - tnode_clean_free(tn); + tnode_free(tn); return -ENOMEM; } + tnode_free_append(tn, newn); put_child(tn, i/2, newn); } } + /* prepare oldtnode to be freed */ + tnode_free_init(oldtnode); + for (i = 0; i < olen; i += 2) { struct tnode *newBinNode; @@ -636,11 +633,14 @@ static int halve(struct trie *t, struct put_child(tn, i / 2, newBinNode); + /* resize child node */ resize(t, newBinNode); } put_child_root(tp, t, tn->key, tn); - tnode_free_safe(oldtnode); + + /* all pointers should be clean so we are done */ + tnode_free(oldtnode); return 0; } @@ -798,7 +798,8 @@ no_children: node_set_parent(n, tp); /* drop dead node */ - tnode_free_safe(tn); + tnode_free_init(tn); + tnode_free(tn); } } @@ -884,16 +885,12 @@ static void trie_rebalance(struct trie * while ((tp = node_parent(tn)) != NULL) { resize(t, tn); - - tnode_free_flush(); tn = tp; } /* Handle last (top) tnode */ if (IS_TNODE(tn)) resize(t, tn); - - tnode_free_flush(); } /* only used from updater-side */ '>65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
/*
 * Moschip MCS814x clock routines
 *
 * Copyright (C) 2012, Florian Fainelli <florian@openwrt.org>
 *
 * Licensed under GPLv2
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/export.h>
#include <linux/spinlock.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/clkdev.h>
#include <linux/clk.h>

#include <mach/mcs814x.h>

#include "common.h"

#define KHZ	1000
#define MHZ	(KHZ * KHZ)

struct clk_ops {
	unsigned long (*get_rate)(struct clk *clk);
	int (*set_rate)(struct clk *clk, unsigned long rate);
	struct clk *(*get_parent)(struct clk *clk);
	int (*enable)(struct clk *clk, int enable);
};

struct clk {
	struct clk *parent;     	/* parent clk */
	unsigned long rate;     	/* clock rate in Hz */
	unsigned long divider;		/* clock divider */
	u32 usecount;			/* reference count */
	struct clk_ops *ops;		/* clock operation */
	u32 enable_reg;			/* clock enable register */
	u32 enable_mask;		/* clock enable mask */
};

static unsigned long clk_divide_parent(struct clk *clk)
{
	if (clk->parent && clk->divider)
		return clk_get_rate(clk->parent) / clk->divider;
	else
		return 0;
}

static int clk_local_onoff_enable(struct clk *clk, int enable)
{
	u32 tmp;

	/* no enable_reg means the clock is always enabled */
	if (!clk->enable_reg)
		return 0;

	tmp = readl_relaxed(mcs814x_sysdbg_base + clk->enable_reg);
	if (!enable)
		tmp &= ~clk->enable_mask;
	else
		tmp |= clk->enable_mask;

	writel_relaxed(tmp, mcs814x_sysdbg_base + clk->enable_reg);

	return 0;
}

static struct clk_ops default_clk_ops = {
	.get_rate	= clk_divide_parent,
	.enable		= clk_local_onoff_enable,
};

static DEFINE_SPINLOCK(clocks_lock);

static const unsigned long cpu_freq_table[] = {
	175000,
	300000,
	125000,
	137500,
	212500,
	250000,
	162500,
	187500,
	162500,
	150000,
	225000,
	237500,
	200000,
	262500,
	275000,
	287500
};

static struct clk clk_cpu;

/* System clock is fixed at 50Mhz */
static struct clk clk_sys = {
	.rate	= 50 * MHZ,
};

static struct clk clk_sdram;

static struct clk clk_timer0 = {
	.parent	= &clk_sdram,
	.divider = 2,
	.ops	= &default_clk_ops,
};

static struct clk clk_timer1_2 = {
	.parent	= &clk_sys,
};

/* Watchdog clock is system clock / 128 */
static struct clk clk_wdt = {
	.parent	= &clk_sys,
	.divider = 128,
	.ops	= &default_clk_ops,
};

static struct clk clk_emac = {
	.ops		= &default_clk_ops,
	.enable_reg	= SYSDBG_SYSCTL,
	.enable_mask	= SYSCTL_EMAC,
};

static struct clk clk_ephy = {
	.ops		= &default_clk_ops,
	.enable_reg	= SYSDBG_PLL_CTL,
	.enable_mask	= ~SYSCTL_EPHY,	/* active low */
};

static struct clk clk_cipher = {
	.ops		= &default_clk_ops,
	.enable_reg	= SYSDBG_SYSCTL,
	.enable_mask	= SYSCTL_CIPHER,
};

#define CLK(_dev, _con, _clk)	\
{ .dev_id = (_dev), .con_id = (_con), .clk = (_clk) },

static struct clk_lookup mcs814x_chip_clks[] = {
	CLK("cpu", NULL, &clk_cpu)
	CLK("sys", NULL, &clk_sys)
	CLK("sdram", NULL, &clk_sdram)
	/* 32-bits timer0 */
	CLK("timer0", NULL, &clk_timer0)
	/* 16-bits timer1 */
	CLK("timer1", NULL, &clk_timer1_2)
	/* 64-bits timer2, same as timer 1 */
	CLK("timer2", NULL, &clk_timer1_2)
	CLK(NULL, "wdt", &clk_wdt)
	CLK(NULL, "emac", &clk_emac)
	CLK(NULL, "ephy", &clk_ephy)
	CLK(NULL, "cipher", &clk_cipher)
};

static void local_clk_disable(struct clk *clk)
{
	WARN_ON(!clk->usecount);

	if (clk->usecount > 0) {
		clk->usecount--;

		if ((clk->usecount == 0) && (clk->ops->enable))
			clk->ops->enable(clk, 0);

		if (clk->parent)
			local_clk_disable(clk->parent);
	}
}

static int local_clk_enable(struct clk *clk)
{
	int ret = 0;

	if (clk->parent)
		ret = local_clk_enable(clk->parent);

	if (ret)
		return ret;

	if ((clk->usecount == 0) && (clk->ops->enable))
		ret = clk->ops->enable(clk, 1);

	if (!ret)
		clk->usecount++;
	else if (clk->parent && clk->parent->ops->enable)
		local_clk_disable(clk->parent);

	return ret;
}

int clk_enable(struct clk *clk)
{
	int ret;
	unsigned long flags;

	spin_lock_irqsave(&clocks_lock, flags);
	ret = local_clk_enable(clk);
	spin_unlock_irqrestore(&clocks_lock, flags);

	return ret;
}
EXPORT_SYMBOL(clk_enable);

void clk_disable(struct clk *clk)
{
	unsigned long flags;

	spin_lock_irqsave(&clocks_lock, flags);
	local_clk_disable(clk);
	spin_unlock_irqrestore(&clocks_lock, flags);
}
EXPORT_SYMBOL(clk_disable);

unsigned long clk_get_rate(struct clk *clk)
{
	if (unlikely(IS_ERR_OR_NULL(clk)))
		return 0;

	if (clk->rate)
		return clk->rate;

	if (clk->ops && clk->ops->get_rate)
		return clk->ops->get_rate(clk);

	return clk_get_rate(clk->parent);
}
EXPORT_SYMBOL(clk_get_rate);

struct clk *clk_get_parent(struct clk *clk)
{
	unsigned long flags;

	if (unlikely(IS_ERR_OR_NULL(clk)))
		return NULL;

	if (!clk->ops || !clk->ops->get_parent)
		return clk->parent;

	spin_lock_irqsave(&clocks_lock, flags);
	clk->parent = clk->ops->get_parent(clk);
	spin_unlock_irqrestore(&clocks_lock, flags);

	return clk->parent;
}
EXPORT_SYMBOL(clk_get_parent);

void __init mcs814x_clk_init(void)
{
	u32 bs1;
	u8 cpu_freq;

	clkdev_add_table(mcs814x_chip_clks, ARRAY_SIZE(mcs814x_chip_clks));

	/* read the bootstrap registers to know the exact clocking scheme */
	bs1 = readl_relaxed(mcs814x_sysdbg_base + SYSDBG_BS1);
	cpu_freq = (bs1 >> CPU_FREQ_SHIFT) & CPU_FREQ_MASK;

	pr_info("CPU frequency: %lu (kHz)\n", cpu_freq_table[cpu_freq]);
	clk_cpu.rate = cpu_freq * KHZ;

	/* read SDRAM frequency */
	if (bs1 & SDRAM_FREQ_BIT)
		clk_sdram.rate = 100 * MHZ;
	else
		clk_sdram.rate = 133 * MHZ;

	pr_info("SDRAM frequency: %lu (MHz)\n", clk_sdram.rate / MHZ);
}