ATA Trim

From Thomas-Krenn-Wiki
Jump to: navigation, search

Using the ATA Trim command, the operating system tells an SSD (solid-state drive) which data segments are no longer needed and should be marked as deleted. When the entire capacity of an SSD is used, ATA Trim will increase the performance and the life cycle of the SDD. If an ATA Trim cannot be performed (such as when using RAID controllers), over-provisioning provides similar advantages, however at the price of slightly reduced capacity (90%, for example).[1]

Prerequisites

In order for ATA Trim to work, the following components must be supported:

  • by the SSD itself
  • by the operating system (Windows 7, for example)[2] or Linux kernels as of version 2.6.33[3])
  • by the file system

ATA TRIM and RAID Controllers

As of mid-August 2010, we did not know of any RAID controllers that support ATA TRIM for SSDs in RAID volumes.[4][5] The firmware/RAID driver from Intel only supports the TRIM command for drives (in AHCI mode and in RAID mode) that are not part of a RAID volume as of version 9.6 of Rapid Storage Technology.[6] In the future, Intel also wants to support ATA Trim for RAID 0 volumes.[7]

ATA Trim and Linux Swap Devices

Swap devices under Linux support the discard function:[8][9][10]

  • the Linux kernel supports the discard function for trim for swap devices as of kernel version 2.6.29
  • Since some SSDs show performance losses when performing an online discard, batched discard has become optional since kernel version 2.6.36 (-d flag from swapon)
  • When swapon is executed for the first time, the entire swap area will continue to be trimmed by discard regardless of the -–d flag.

ATA Trim and Linux Device Mapper

As of Kernel 2.6.36, the following targets support discards:

  • dm-delay
  • dm-linear
  • dm-mpath
  • dm-stripe (raid0)

As of Kernel 2.6.38, the following has been added:

  • dm-mirror (raid1)

ATA Trim and Linux LVM

(this information will be provided later)

ATA Trim and Linux Software RAIDs

Status as of November 2012

  • ATA Trim with Linux Software RAID is supported since Linux Kernel 3.7.

Status as of August 2012

Status as of July 2011

  • The Linux software RAID (md) does not have built-in support for discards and therefore does not support TRIM. The use of Device Mapper’s dm-mirror for mirroring data is, however, available with discard support as of RHEL 6.1 roughly. Red Hat advises against using Linux software RAIDs at RAID Levels 1, 4, 5 or 6, because all blocks on the storage device will be written during the initialization of RAID management utilities, like mdadm, in order to ensure that the checksums are functioning properly. Because the SSD will be fully written by this, its life cycle and performance will be reduced.[11] Generous SSD Over-provisioning using hdparm would be a workaround for this.
  • Chris Caputo has written a script (raid1ext4trim.sh-1.4) that makes a manual ATA trim possible for Ext4 file systems on a Level 1 software RAID array.[12][13] However, the RAID slave devices must all be SSDs for this (e.g. /dev/sda) and may not be partitions (e.g. /dev/sda1). The script can be found at hdparm and is stored in the hdparm-9.37/wiper/contrib/ sub-directory of the source package.

Potential Future Developments

  • There was a discussion about potential discard support for Linux software RAID in June and July of 2011. Regarding this, see Linux Software RAID Roadmap.

raid1ext4trim.sh-1.4 Example

The following example demonstrates the application of raid1ext4trim.sh-1.4 to a RAID 1 array using version 1.0 metadata.

[root@localhost ~]# /root/raid1ext4trim.sh-1.4 /dev/md0 /mnt
raid1ext4trim.sh-1.4: TRIM utility for live RAID1 ext4 SATA SSDs, version 1.4, by Chris Caputo, based on Mark Lord's wiper.sh.

Reserve defaulting to 10 megabytes.
Creating temporary file (107219596 KB '/mnt/raid1ext4trim.sh-1.4_TMPFILE.12054') ... 
TRIMable constituents of md0: sda sdb
This will be a DRY-RUN only.  Use --commit to do it for real.
Simulating TRIM operations...
TRIM beginning on sda...
(dry-run) trimming 214439200 sectors from 3504 ranges
TRIM finished successfully on sda.
TRIM beginning on sdb...
(dry-run) trimming 214439200 sectors from 3504 ranges
TRIM finished successfully on sdb.
Removing temporary file '/mnt/raid1ext4trim.sh-1.4_TMPFILE.12054'...
Done.
[root@localhost ~]# /root/raid1ext4trim.sh-1.4 --commit /dev/md0 /mnt
raid1ext4trim.sh-1.4: TRIM utility for live RAID1 ext4 SATA SSDs, version 1.4, by Chris Caputo, based on Mark Lord's wiper.sh.

Reserve defaulting to 10 megabytes.
Creating temporary file (107219596 KB '/mnt/raid1ext4trim.sh-1.4_TMPFILE.12137') ... 
TRIMable constituents of md0: sda sdb
Beginning TRIM operations...
TRIM beginning on sda...

/dev/sda:
trimming 31080445 sectors from 512 ranges
succeeded

/dev/sda:
trimming 31375361 sectors from 512 ranges
succeeded

/dev/sda:
trimming 31358977 sectors from 512 ranges
succeeded

/dev/sda:
trimming 31440896 sectors from 512 ranges
succeeded

/dev/sda:
trimming 31440896 sectors from 512 ranges
succeeded

/dev/sda:
trimming 31309822 sectors from 512 ranges
succeeded

/dev/sda:
trimming 26432803 sectors from 432 ranges
succeeded
TRIM finished successfully on sda.
TRIM beginning on sdb...

/dev/sdb:
trimming 31080445 sectors from 512 ranges
succeeded
trimming 31375361 sectors from 512 ranges
succeeded
trimming 31358977 sectors from 512 ranges
succeeded
trimming 31440896 sectors from 512 ranges
succeeded
trimming 31440896 sectors from 512 ranges
succeeded
trimming 31309822 sectors from 512 ranges
succeeded
trimming 26432803 sectors from 432 ranges
succeeded
TRIM finished successfully on sdb.
Removing temporary file '/mnt/raid1ext4trim.sh-1.4_TMPFILE.12137'...
Done.
[root@localhost ~]#

When applied to a RAID 1 array using version 1.2 metadata, the script does not work, because the script assumes that the data will be at the start of the underlying device.

[root@localhost ~]# /root/raid1ext4trim.sh-1.4 --verbose /dev/md0 /mnt
raid1ext4trim.sh-1.4: TRIM utility for live RAID1 ext4 SATA SSDs, version 1.4, by Chris Caputo, based on Mark Lord's wiper.sh.

Reserve defaulting to 10 megabytes.
Creating temporary file (105760448 KB '/mnt/raid1ext4trim.sh-1.4_TMPFILE.11462') ... 
Direct I/O of last extent of tmpfile on sda doesn't match that of md0.  Excluding.
Direct I/O of last extent of tmpfile on sdb doesn't match that of md0.  Excluding.
No constituent of md0 array has a matching checksum.  Aborting.
Removing temporary file '/mnt/raid1ext4trim.sh-1.4_TMPFILE.11462'...
Aborted.
[root@localhost ~]#

Online or Batched

ATA Trim can either immediately delete the corresponding data on the SSD at the time of deletion (called an Online Discard[14][15] under Linux) or periodically release blocks that are no longer needed with the manual help of a utility (called a Batched Discard under Linux with FITRIM ioctl), as shown in the following table.

Operating System / File System Automatic ATA TRIM
(data deleted immediately)
Manual ATA TRIM
(data deleted by a utility)
Windows Vista
Windows XP
Windows Server 2008
Windows Server 2003
- yes, using manufacturer’s utilities, such as
die Intel Solid State Drive Toolbox
Windows 7
Windows Server 2008 R2
Yes[16][17] yes, using manufacturer’s utilities, such as
die Intel Solid State Drive Toolbox
Linux / Ext4 Yes, online discard as of Kernel 2.6.33
(Ext4 requires the Discard setting)
Yes, using wiper.sh[18] (use hdparm)[19] or
Batched Discard as of Kernel 2.6.37 using fstrim[20][21][22] (included with util-linux 2.19 and later)[23]
Linux / Btrfs Yes, Online Discard as of Linux Kernel 2.6.32
(The Libata sub-system supports Discard as of Kernel 2.6.33)
(Btrfs requires the Discard setting)
Yes, batched discard as of Kernel 2.6.39

Example: ATA Trim under Ubuntu 10.10

The SSD itself must support ATA Trim so that you can use this feature, as also shown in this example (the * indicates that the option is available).

root@ubuntu-10-10:~# hdparm -I /dev/sda | grep -i trim
	   *	Data Set Management TRIM supported
	   *	Deterministic read ZEROs after TRIM
root@ubuntu-10-10:~# 

Default Configuration without ATA Trim

For this test, we will write the capital letters from A to Z to a file. When reading the sector using hdparm, the associated ASCII codes[24] will be displayed as hex.

Comment: with some SSDs (such as the Intel 320 Series SSDs) sync must be called after the echo command, so that hdparm --fibmap will return the LBA boundaries.

root@ubuntu-10-10:~# echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ" > /root/testfile1.txt
root@ubuntu-10-10:~# hdparm --fibmap /root/testfile1.txt 

/root/testfile1.txt:
 filesystem blocksize 4096, begins at LBA 61052928; assuming 512 byte sectors.
 byte_offset  begin_LBA    end_LBA    sectors
           0   66989472   66989479          8
root@ubuntu-10-10:~# hdparm --read-sector 66989472 /dev/sda

/dev/sda:
reading sector 66989472: succeeded
4241 4443 4645 4847 4a49 4c4b 4e4d 504f
5251 5453 5655 5857 5a59 000a 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
[...]
root@ubuntu-10-10:~# rm /root/testfile1.txt
root@ubuntu-10-10:~# sync
root@ubuntu-10-10:~# hdparm --read-sector 66989472 /dev/sda

/dev/sda:
reading sector 66989472: succeeded
4241 4443 4645 4847 4a49 4c4b 4e4d 504f
5251 5453 5655 5857 5a59 000a 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
[...]
root@ubuntu-10-10:~# 

Configuring ATA Trimming and Online Discard

The ext4 file system will send a discard/TRIM command to the underlying storage device, when the discard mount option has been activated. Of course, the setting must be manually added to the /etc/fstab file, since discard is deactivated in the default configuration.[25]

discard: Controls whether ext4 should issue discard/TRIM commands to the underlying block device when blocks are freed. This is useful for SSD devices and sparse/thinly-provisioned LUNs, but it is off by default until sufficient testing has been done.

The discard setting can be easily added to /etc/fstab as a setting.

[...]
# / was on /dev/sda3 during installation
UUID=1b7be627-fcb5-43b4-a9fb-72c112041c53 /               ext4    errors=remount-ro,discard 0       1
[...]

The setting will be activated for the / partition once the system has been re-booted.

root@ubuntu-10-10:~# mount
/dev/sda3 on / type ext4 (rw,errors=remount-ro,discard,commit=600)
[...]
root@ubuntu-10-10:~# 

When deleting files, the associated data will now be removed from the SSD in fact.

root@ubuntu-10-10:~# echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ" > /root/testfile2.txt
root@ubuntu-10-10:~# sync
root@ubuntu-10-10:~# hdparm --fibmap /root/testfile2.txt 

/root/testfile2.txt:
 filesystem blocksize 4096, begins at LBA 61052928; assuming 512 byte sectors.
 byte_offset  begin_LBA    end_LBA    sectors
           0   73902760   73902767          8
root@ubuntu-10-10:~# hdparm --read-sector 73902760 /dev/sda

/dev/sda:
reading sector 73902760: succeeded
4241 4443 4645 4847 4a49 4c4b 4e4d 504f
5251 5453 5655 5857 5a59 000a 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
[...]
root@ubuntu-10-10:~# rm /root/testfile2.txt 
root@ubuntu-10-10:~# sync
root@ubuntu-10-10:~# hdparm --read-sector 73902760 /dev/sda

/dev/sda:
reading sector 73902760: succeeded
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
[...]
root@ubuntu-10-10:~#

Additional Information regarding ATA Trim under Linux

References

  1. Re: Software RAID and TRIM (Linux Raid Mailing List) As long as your SSDs have some over-provisioning (or you only partition something like 90% of the drive), and it's got good garbage collection, then TRIM will have minimal effect.
  2. http://blogs.msdn.com/b/e7/archive/2009/05/05/support-and-q-a-for-solid-state-drives-and.aspx
  3. http://www.heise.de/open/artikel/ATA-Trim-DRBD-Block-Layer-Virtualisierung-und-Tracing-936993.html
  4. http://www.heise.de/ct/hotline/Kein-Trim-Befehl-im-SSD-RAID-991529.html
  5. SSD TRIM and Adaptec RAID Controllers (Adaptec Support Knowledgebase ASK Answer ID 16999)
  6. Intel® Rapid Storage Technology: RAID features by chip set & controller hub (intel.com)
  7. Intel plans RAID Driver Support for ATA Trim (heise.de, 22.11.2011)
  8. swap: discard while swapping only if SWAP_FLAG_DISCARD
  9. discard change to swapon(2) and swapon(8) (Hugh Dickins, util-linux-ng mailing list, 24.10.2010)
  10. Re: discard change to swapon(2) and swapon(8) (Hugh Dickins, util-linux-ng mailing list, 09.11..2010)
  11. Chapter 22. Solid-State Disk Deployment Guidelines Red Hat Enterprise Linux 6, Storage Administration Guide (docs.redhat.com)
  12. script for TRIM on RAID1 full-disk arrays with live ext4 file systems (Linux RAID Mailing List)
  13. hdparm: Detail: 3007001 - TRIM on RAID1 full-disk arrays with live ext4 file systems (SourceForge.net)
  14. Kernel real time discard support
  15. Patchwork (2/6) libata: Report supported TRIM payload size The bigger problem for now is that the block layer does not export a way to pass in a vectorized list of ranges to discard.
  16. http://en.wikipedia.org/wiki/TRIM#Operating_system_and_SSD_support
  17. http://www.heise.de/newsticker/meldung/SSD-Hersteller-optimieren-Firmware-fuer-Windows-7-2-Update-842820.html
  18. http://sourceforge.net/projects/hdparm/files/
  19. Re: (PATCH 2/6) libata: Report supported TRIM payload size (linux-scsi mailing list, Greg Freemyer, 19.08.2010) The fundamental problem with hdparm is that because it bypasses most of the kernel i/o stack, it is not compatible with DM/LVM, but I think its a great prototype of what the kernel could do eventually.
  20. http://lwn.net/Articles/383933/
  21. http://www.heise.de/open/artikel/Kernel-Log-Was-2-6-37-bringt-2-Dateisysteme-1144739.html
  22. http://fstrim.sourceforge.net/
  23. fstrim: add new command
  24. http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
  25. http://www.kernel.org/doc/Documentation/filesystems/ext4.txt

Related articles

Optimize SSD Performance
SSD Over-provisioning using hdparm
SSD Secure Erase