Bug 187081

Summary: swapoff runs too early during shutdown
Product: Base System Reporter: Radim Kolar <hsn>
Component: confAssignee: Enji Cooper <ngie>
Status: Closed FIXED    
Severity: Affects Some People CC: arcade, c.kworr, emaste, freebsd-2024, jilles, koobs, ngie, pi, wosch
Priority: --- Keywords: easy, needs-qa, patch
Version: UnspecifiedFlags: jilles: mfc-stable11+
jilles: mfc-stable10+
jilles: mfc-stable9-
Hardware: Any   
OS: Any   
Bug Depends on: 224975    
Bug Blocks:    
Attachments:
Description Flags
fix "swaplate" to deal only with "late" swap devices none

Description Radim Kolar 2014-02-26 11:00:00 UTC
swapoff runs too early during system shutdown. it runs before other memory intensive daemons are stopped. For example trafficserver or varnish.

swapoff fails with out of memory error - system cant run without swap at that time. shutdown seems to continue normally even with swap still enabled.
Comment 1 c.kworr 2015-10-09 15:17:24 UTC
Created attachment 161855 [details]
fix "swaplate" to deal only with "late" swap devices

Hi. I like to join this heated conversation. :)

It seems to me you are bitten with /etc/rc.d/swaplate which should remove swap partitions that are not directly available. Yet due to bug in the swapon/swapoff logic it rather removes all swap partitions. I'm bored with this too as on my machine swap is used constantly and when rebooting for some weird reason I need to wait while the system gets all my sleepy daemons from swap back to the memory instead of just stopping them.

The key problem is "-a" and "-L" options to swapon/swapoff as they give only two choices:

1. Single "-a" selects only not "late" "auto" devices (the ones without "noauto" and "late") from /etc/fstab.
2. Both options ("-aL") select whole range of "auto" devices (only skipping "noauto" ones) from /etc/fstab.

This two choices doesn't give us a possibility to turn off only "late" devices, that can really be dependent on some daemons with higher priorities so they MAY fail further down "shutdown" sequence and as such they SHOULD be turned off early during "shutdown".

The fix I propose actually breaks compatibility but I don't think this would be an issue as:

 * there's not that much consumers of mentioned options in the scripts;
 * the fix brings clarity and usability.

In this fix:

 * single "-a" still select only not "late" "auto" devices;
 * option "-L" inverts "-a" meaning and selects only "late" "auto" devices;
 * documentation and starting script are fixed.
Comment 2 Enji Cooper freebsd_committer freebsd_triage 2015-10-19 19:22:07 UTC
(In reply to c.kworr from comment #1)

Interesting bug/patch.

I'll look at it.
Comment 3 Volodymyr Kostyrko 2016-02-16 14:54:59 UTC
Looks like this ticket stalled?
Comment 4 Kubilay Kocak freebsd_committer freebsd_triage 2016-08-12 16:19:11 UTC
Re-open (unsure of progress)

Ngie, have you had a chance to look at this patch?
Comment 5 Jilles Tjoelker freebsd_committer freebsd_triage 2016-10-02 21:19:02 UTC
I think swapon -aL can remain unchanged and only swapoff -aL needs the proposed change.

I created a Phabricator review at https://reviews.freebsd.org/D8126 .
Comment 6 Jilles Tjoelker freebsd_committer freebsd_triage 2016-10-02 21:19:37 UTC
*** Bug 211466 has been marked as a duplicate of this bug. ***
Comment 7 commit-hook freebsd_committer freebsd_triage 2016-10-21 21:56:34 UTC
A commit references this bug:

Author: jilles
Date: Fri Oct 21 21:55:50 UTC 2016
New revision: 307755
URL: https://svnweb.freebsd.org/changeset/base/307755

Log:
  swapoff: Remove only late devices with -aL.

  Currently, '/etc/rc.d/swaplate stop' removes all swap devices. This can be
  very slow and may not even be possible if there is a lot of swap space in
  use. However, removing swap devices is only needed for late swap devices
  that may depend on daemons that subsequent shutdown steps stop. Normal swap
  devices such as hard disk partitions will remain available throughout the
  shutdown process and need not be removed.

  In swapoff, interpret -aL to remove late swap devices only, and use this in
  etc/rc.d/swaplate. The meaning of -aL in swapon remains unchanged (add all
  swap devices, both normal and late).

  PR:		187081
  Reviewed by:	wblock (man page only), ngie
  MFC after:	1 week
  Differential Revision:	https://reviews.freebsd.org/D8126

Changes:
  head/etc/rc.d/swaplate
  head/sbin/swapon/swapon.8
  head/sbin/swapon/swapon.c
Comment 8 c.kworr 2016-10-21 22:12:19 UTC
(In reply to Jilles Tjoelker from comment #5)
Nice to see this one rolling. Thank you.

When I created patch I was trying to make options consistent between swapon and swapoff. Hope noone would brag about this. :)
Comment 9 commit-hook freebsd_committer freebsd_triage 2016-11-19 20:03:40 UTC
A commit references this bug:

Author: jilles
Date: Sat Nov 19 20:02:49 UTC 2016
New revision: 308865
URL: https://svnweb.freebsd.org/changeset/base/308865

Log:
  MFC r307755: swapoff: Remove only late devices with -aL.

  Currently, '/etc/rc.d/swaplate stop' removes all swap devices. This can be
  very slow and may not even be possible if there is a lot of swap space in
  use. However, removing swap devices is only needed for late swap devices
  that may depend on daemons that subsequent shutdown steps stop. Normal swap
  devices such as hard disk partitions will remain available throughout the
  shutdown process and need not be removed.

  In swapoff, interpret -aL to remove late swap devices only, and use this in
  etc/rc.d/swaplate. The meaning of -aL in swapon remains unchanged (add all
  swap devices, both normal and late).

  PR:		187081

Changes:
_U  stable/11/
  stable/11/etc/rc.d/swaplate
  stable/11/sbin/swapon/swapon.8
  stable/11/sbin/swapon/swapon.c
Comment 10 commit-hook freebsd_committer freebsd_triage 2016-11-20 13:45:02 UTC
A commit references this bug:

Author: jilles
Date: Sun Nov 20 13:44:27 UTC 2016
New revision: 308886
URL: https://svnweb.freebsd.org/changeset/base/308886

Log:
  MFC r307755: swapoff: Remove only late devices with -aL.

  Currently, '/etc/rc.d/swaplate stop' removes all swap devices. This can be
  very slow and may not even be possible if there is a lot of swap space in
  use. However, removing swap devices is only needed for late swap devices
  that may depend on daemons that subsequent shutdown steps stop. Normal swap
  devices such as hard disk partitions will remain available throughout the
  shutdown process and need not be removed.

  In swapoff, interpret -aL to remove late swap devices only, and use this in
  etc/rc.d/swaplate. The meaning of -aL in swapon remains unchanged (add all
  swap devices, both normal and late).

  PR:		187081

Changes:
_U  stable/10/
  stable/10/etc/rc.d/swaplate
  stable/10/sbin/swapon/swapon.8
  stable/10/sbin/swapon/swapon.c