# [solved] swapon is SLOW in new kernels

## jmattax

I recently decided that I should build a new kernel since the one I was running was rather old. I got and configured gentoo-sources-3.2.1-r2 and booted them simply enough. However, my swap file now takes forever to load. Using the new kernel I get

```
uname -a

Linux gatita 3.2.1-gentoo-r2 #2 SMP Sat Mar 17 14:46:21 MDT 2012 x86_64 AMD Phenom(tm) II X4 925 Processor AuthenticAMD GNU/Linux

```

and after turning the swap off after booting

```
time swapon -v /var/swapfile 

swapon on /var/swapfile

swapon: /var/swapfile: found swap signature: version 1, page-size 4, same byte order

swapon: /var/swapfile: pagesize=4096, swapsize=6442450944, devsize=6442450944 

real    4m34.691s

user    0m0.001s

sys     0m1.395s 

```

where as with my old kernel

```
uname -a

Linux gatita 2.6.30-gentoo-r5 #9 SMP Sun May 9 12:04:59 MDT 2010 x86_64 AMD Phenom(tm) II X4 925 Processor AuthenticAMD GNU/Linux

```

and after turning the swap off so I can time it coming back on

```
time swapon -v /var/swapfile 

swapon on /var/swapfile

swapon: /var/swapfile: found swap signature: version 1, page-size 4, same byte order

swapon: /var/swapfile: pagesize=4096, swapsize=6442450944, devsize=6442450944

```

I can't figure out what the problem is here, but obviously I don't want to run the new kernel till I solve the problem since it means taking about 5 minutes to boot.Last edited by jmattax on Fri Mar 23, 2012 1:03 am; edited 1 time in total

----------

## Hu

Does it help if you use a swap partition?  What type of filesystem is /var?  Is /var on a spinning disk or solid state disk?

----------

## jmattax

I'm not sure on the swap partition I'll have to install a new physical disk to test that, but I'll be able to do that tomorrow or later tonight.

/var is on an ext3 formatted solid state disk that has been running well for me with the old kernel for about a year.

----------

## EatMeerkats

Weird, on a 3.2.11-gentoo kernel + ext4 on SSD, I get:

```
% time sudo swapon /swapfile

sudo swapon /swapfile  0.00s user 0.11s system 99% cpu 0.117 total
```

----------

## jmattax

EatMeerkats: What package is your kernel from? I show 3.2.1 as the most recent stable kernel version for gentoo-sources.

I'm still working on testing with a partition, I have a spare hard drive installed, and the BIOS and Windows see it, but Gentoo doesn't so I'm going down a rabbit trail trying to get it recognized.

----------

## EatMeerkats

Oh, I just unmaked the latest version in portage.

----------

## jmattax

I just mounted a swap partition in the new kernel it went right quick. It is about a third the size but otherwise the same.

```
time swapon -v /dev/sdb5

swapon on /dev/sdb5

swapon: /dev/sdb5: found swap signature: version 1, page-size 4, same byte order

swapon: /dev/sdb5: pagesize=4096, swapsize=2006933504, devsize=2006936064

real    0m0.005s

user    0m0.001s

sys     0m0.002s

```

----------

## Hu

 *jmattax wrote:*   

> I just mounted a swap partition in the new kernel it went right quick. It is about a third the size but otherwise the same.

 To be sure I understand, by "new kernel" in the post quoted here, you mean the 3.2.1-gentoo-r2 kernel that performs poorly with swap files performs well with swap partitions?

----------

## jmattax

Hu: that is correct, in the 3.2.1-gentoo-r2 time to swapon an about 6GB swap file is about 4:30 (with the time command no knowing where most of that is spent), while swapon for a 2GB partition is well  under a second.

```

gatita gentoo-sources # time swapon -v /dev/sdb5

swapon on /dev/sdb5

swapon: /dev/sdb5: found swap signature: version 1, page-size 4, same byte order

swapon: /dev/sdb5: pagesize=4096, swapsize=2006933504, devsize=2006936064

real    0m0.005s

user    0m0.001s

sys     0m0.002s

gatita gentoo-sources # time swapon -v /var/swapfile 

swapon on /var/swapfile

swapon: /var/swapfile: found swap signature: version 1, page-size 4, same byte order

swapon: /var/swapfile: pagesize=4096, swapsize=6442450944, devsize=6442450944

real    4m35.733s

user    0m0.001s

sys     0m1.808s

gatita gentoo-sources # uname -a

Linux gatita 3.2.1-gentoo-r2 #2 SMP Sat Mar 17 14:46:21 MDT 2012 x86_64 AMD Phenom(tm) II X4 925 Processor AuthenticAMD GNU/Linux

```

The strange thing here is that the time command doesn't seem to have any idea where the time is being spent turning the swap on for the swap file the user+sys time is under 2 seconds.

----------

## jmattax

I now have a little more information, using gentoo-sources-3.2.11 has the same symptoms as 3.2.1-r2. Also, I tested mounting a loopback device to see if that was the performance problem, but it wasn't.

----------

## Hu

If this is reproducible in vanilla kernels and is a regression relative to earlier 3.x kernels, I think your next step is to contact LKML to report it the kernel developers.

----------

## jmattax

The memory management sublist of the LKML solved it for me. It required a patch to the kernel it involved changing the order of the statements in a conditional. Since they said that someone there didn't like it I'm going to post the text of it here.

--- a/mm/swapfile.c~swap-dont-do-discard-if-no-discard-option-added

+++ a/mm/swapfile.c

@@ -2103,7 +2103,7 @@ SYSCALL_DEFINE2(swapon, const char __use

 			p->flags |= SWP_SOLIDSTATE;

 			p->cluster_next = 1 + (random32() % p->highest_bit);

 		}

-		if (discard_swap(p) == 0 && (swap_flags & SWAP_FLAG_DISCARD))

+		if ((swap_flags & SWAP_FLAG_DISCARD) && discard_swap(p) == 0)

 			p->flags |= SWP_DISCARDABLE;

 	}

----------

