Trim (computing)

A Trim command (commonly typeset as TRIM) allows an operating system to inform a solid-state drive (SSD) which blocks of data are no longer considered in use and can be wiped internally.[1]

Trim was introduced soon after SSDs were introduced. Because low-level operation of SSDs differs significantly from hard drives, the typical way in which operating systems handle operations like deletes and formats resulted in unanticipated progressive performance degradation of write operations on SSDs.[2] Trimming enables the SSD to handle garbage collection overhead, which would otherwise significantly slow down future write operations to the involved blocks, in advance.[3]

Although tools to "reset" some drives to a fresh state were already available before the introduction of trimming, they also delete all data on the drive, which makes them impractical to use for ongoing optimization.[4] By 2014 many SSDs had internal idle/background garbage collection mechanisms that work independently of trimming; although this successfully maintains their performance even under operating systems that do not support Trim, it has the associated drawbacks of increased write amplification and wear of the flash cells.[5]

Because of the way that many file systems handle delete operations, by flagging data blocks as "not in use",[6][7] storage media (SSDs, but also traditional hard drives) generally do not know which sectors/pages are truly in use and which can be considered free space. Contrary to, for example an overwrite operation, a delete will not involve a physical write to the sectors that contain the data. Since a common SSD has no knowledge of the file system structures, including the list of unused blocks/sectors, the storage medium remains unaware that the blocks have become available. While this often enables undelete tools to recover files from electromechanical hard disks,[7][8] despite the files being reported as "deleted" by the operating system, it also means that when the operating system later performs a write operation to one of the sectors, which it considers free space, it effectively becomes an overwrite operation from the point of view of the storage medium. For magnetic disks this is no different from writing an empty sector, but because of how some SSDs function at the lowest level, an overwrite produces significant overhead compared to writing data into an empty page, potentially crippling write performance.[7][9]

SSDs store data in flash memory cells that are grouped into pages typically of 4 to 16 kB, grouped together into blocks of typically 128 to 512 pages, e.g., 512 kB per block with 4 kB pages in 128-page blocks.[6][10]NAND flash memory cells can only be directly written to when they are empty. If they may contain data, the contents must be erased before a write operation. An SSD write operation can be done on a single page but, due to hardware limitations, erase commands always affect entire blocks;[10] consequently writing data to empty pages on an SSD is very fast, but slows down considerably once previously written pages need to be overwritten. Since an erase of the cells in the page is needed before it can be written again, but only entire blocks can be erased, an overwrite will initiate a read-erase-modify-write cycle:[6][11] the contents of the entire block are stored in cache, then the entire block is erased from the SSD, then the overwritten page is written to the cached block, and only then can the entire updated block be written to the flash medium. This phenomenon is known as write amplification.[12][13]

The Trim command is designed to enable the operating system to notify the SSD which pages no longer contain valid data due to erases either by the user or operating system itself. During a delete operation, the OS will mark the sectors as free for new data and send a Trim command to the SSD to mark them as not containing valid data. After that the SSD knows not to preserve the contents of the block when writing a page, resulting in less write amplification with fewer writes to the flash, higher write speed, and increased drive life.

Different SSDs implement the Trim command somewhat differently, so performance can vary.[3][8]

Trim irreversibly deletes from the SSD the data Trim affects. Recovery of data deleted by Trim is not possible. This is unlike a magnetic drive from which deleted data may often be recovered.[14]

Trimming is only effective on operating systems which support it. The table below identifies each notable operating system and the first version supporting the command. Additionally, older solid-state drives designed before the addition of the Trim command to the ATA standard will need firmware updates, otherwise the new command will be ignored. However, not every drive can be upgraded to support Trimming.

Support was added at the block device layer in 8.1. Filesystem support was added in FreeBSD 8.3 and FreeBSD 9, beginning with UFS.[17]ZFS trimming support was added in FreeBSD 9.2.[18][19] FreeBSD 10 supports trimming on software RAID configurations.[20]

Initial support for discard operations was added for FTL NAND flash devices in 2.6.28. Support for the ATA Trim command was added in 2.6.33.[23]

Not all filesystems make use of Trim. Among the filesystems that can issue Trim requests automatically are Ext4,[24]Btrfs,[25]FAT, GFS2,[26]JFS,[27] and XFS.[27] However, this is disabled by default due to performance concerns,[28] but can be enabled by setting the "discard" mount option. Ext3, NILFS2 and OCFS2 offer ioctls to perform offline trimming. The Trim specification calls for supporting a list of trim ranges, but as of kernel 3.0 trim is only invoked with a single range that is slower.[29]

Although the AHCI block device driver gained the ability to display whether a device supports the Trim operation in 10.6.6 (10J3210),[31] the functionality itself remained inaccessible until 10.6.8, when the Trim operation was exposed via the IOStorageFamily and filesystem (HFS+) support was added.[citation needed] Until 10.10.4, Mac OS X natively enabled Trim only for Apple-branded SSDs; third-party utilities are available to enable it for other brands. Old third party TRIM drivers stopped working as of the Yosemite update.[32] Updated drivers now exist that work with OS X Yosemite.[33][34] In Mac OS X update 10.10.4, Apple added a command line utility, trimforce, that can be used to enable TRIM on third-party SSDs.[35]

Windows 7 only supports trim for ordinary (SATA) drives and does not support this command for Storport PCI-Express SSDs that are a different type of device, even if the device itself would accept the command.[38] It is confirmed that with native Microsoft drivers the Trim command works in AHCI and legacy IDE / ATA Mode.[39] Windows 8 and later Windows operating systems support trim for PCI Express SSDs based on NVMe, and the unmap command which is a full analog of the trim command from Serial ATA for devices that use the SCSI driver stack.

The RST (Rapid Storage Technology option ROM) and drivers are only allowing trim to pass to the controller onto the drive in Intel 7 series chipsets using driver versions 11.2.0.0 or later. For Intel chipsets such as the 6 series and the newest X79, a modified (option ROM) must be used on these chipsets to allow trim to be received at the drive. The exception to the X79 chipset is if the motherboard manufacturer has added a ROM switch; this entails both the RST and RST-E ROMs being inside the BIOS/UEFI. This allows the RST ROM to be used, allowing trim to function, as trim does not work on the RST-E ROM.[43] The official response from Intel is,[44] best performance can be achieved by using a driver with same version as the ROM; for example if the BIOS/UEFI has an 11.0.0.0m option ROM, then use an 11.x version driver.

As of January 2012[update], support for the Trim command has not been implemented in most hardware based RAID technologies. Software RAID implementations often do include support for TRIM. For example, TRIM has been supported for Mac OS X RAID volumes since 2011, using the SoftRAID application, including TRIM and RAID support for all non-Apple SSD devices. (While Mac OS 10.10 disabled support for TRIM on third-party devices as part of its extensions security enhancements, as of July 1, 2015, the Mac OS 10.10.4 update officially allows users to enable TRIM for third party SSD devices using the command line. However, Apple's RAID still does not support TRIM, so the third-party SoftRAID application is necessary to enable TRIM with RAID in Mac OS X.) Another case where it has been implemented is in post-January-2011 releases of the Linux kernel's dmraid, which implements BIOS-assisted "fake hardware RAID" support, and now passes through any Trim requests from the filesystem that sits on the RAID array.[45] Not to be confused with dmraid, Linux's general-purpose software RAID system, mdraid, has experimental support for batch-based (rather than live, upon file deletion), Trim on RAID 1 arrays when systems are configured to periodically run the mdtrim utility on filesystems (even those like ext3 without native Trim support).[46] For a short time in March 2010, users were led to believe that the Intel Rapid Storage Technology (RST) 9.6 drivers supported Trim in RAID volumes, but Intel later clarified that Trim was supported for the BIOS settings of AHCI mode and RAID mode, but not if the drive was part of a RAID volume.[47]

As of November 2011, Intel has indicated in the release notes for RST 11.5 Alpha that they intend to add support for Trim on RAID 0 volumes in the next version of RST.[48] Red Hat has also recommended against using software RAID levels 1, 4, 5, and 6 on SSDs, because during initialization, most RAID management utilities (e.g. Linux's mdadm) write to all blocks on the devices to ensure that checksums (or drive-to-drive verifies, in the case of RAID 1) operate properly, causing the SSD to believe that all blocks other than in the spare area are in use, significantly degrading performance.[49]

As of August 2012, Intel confirms that 7-series chipsets with Rapid Storage Technology (RST) 11.2 drivers will support TRIM for RAID 0 in Microsoft Windows 7.[50] While Intel did not confirm support for 6-series chipsets, TRIM on RAID 0 volumes has been shown to work on Z68 and P67 chipsets by hardware enthusiasts with a modified RAID option ROM.[51] It is speculated that the lack of official support for 6-series chipsets is due to validation costs[52] or an attempt to encourage consumers to upgrade,[53] rather than for technical reasons.

Where the filesystem does not automatically support Trim, some utilities can send trimming commands manually. Usually they determine which blocks are free and then pass this list as a series of trimming commands to the drive. These utilities are available from various manufacturers (Intel,[54] G.Skill[55]) or as general utilities (hdparm since v9.17[56][57]).

A drawback of the original ATA TRIM command is that it was defined as a non-queueable command and therefore could not easily be mixed with a normal workload of queued read and write operations. SATA 3.1 introduced a queued TRIM command to remedy this.[61]

There are different types of Trim defined by SATA Words 69 and 169 returned from an ATA IDENTIFY DEVICE command:

Non-deterministic Trim: each read command to the Logical block address (LBA) after a Trim may return different data.

Deterministic Trim (DRAT): all read commands to the LBA after a Trim shall return the same data, or become determinate.

Deterministic Read Zero after Trim (RZAT): all read commands to the LBA after a Trim shall return zero.

There is additional information in SATA Word 105 that describes the Maximum number of 512-byte blocks per DATA SET MANAGEMENT command that a drive can support. Typically this defaults to 8 (or 4 kB) but many drives reduce this to one to meet the Microsoft Windows Hardware Requirements for Trim, Command completion time shall not exceed 20 ms or 8 ms × (number of LBA range entries), whichever is greater, and shall always be less than 600 ms. http://msdn.microsoft.com/en-us/library/windows/hardware/jj134356.aspx An individual LBA range is called an LBA Range Entry and is represented by eight bytes. The LBA is expressed by the LBA Range Entry's first six bytes and the Range Length is a zero based number (e.g., 0=0 and 1=1)represented by the remaining two bytes. If the two byte range length is zero, then the LBA Range Entry shall be discarded as padding.(need reference to T13 ATA/ATAPI Command Set - 3 (ACS-3)) This means that for each 512 byte block of Trim ranges that a device supports, the maximum is 64 ranges of 32 MB, or 2 GB. If a device supports SATA Word 105 at 8 then it should be able to trim 16 GB in a single Trim (DATA SET MANAGEMENT) command.

The MultiMediaCard and SD ERASE (CMD38) command provides similar functionality to the ATA TRIM command, although it requires that erased blocks be overwritten with either zeroes or ones. eMMC 4.5 further defines a "discard" sub-operation that more closely matches ATA TRIM in that the contents of discarded blocks can be considered indeterminate (i.e., "don't care").

When encryption is in use, using the Trim command reveals information about which blocks are in use and which are not.[63]

The original version of the Trim command has been defined as a non-queued command by the T13 subcommittee, and consequently incurs massive execution penalty if used carelessly, e.g., if sent after each filesystem delete command. The non-queued nature of the command requires the driver to first wait for all outstanding commands to be finished, issue the trim command, then resume normal commands. Trim can take a lot of time to complete depending on the firmware in the SSD and may even trigger a garbage collection cycle.[citation needed] This penalty can be minimized in solutions that periodically do a batched trim, rather than trimming upon every file deletion, by scheduling such batch jobs for times when system utilization is minimal. This Trim shortcoming has been overcome in Serial ATA revision 3.1 with the introduction of the Queued Trim Command.[64][65]

Queued Trim commands have been linked to serious data corruption in several devices, most notably Micron's M500,[66] Crucial's M500,[66] and Samsung 8** series.[67] The data corruption has been confirmed for the Linux operating system on these devices as of July 1, 2015[68]

These devices are blacklisted in Linux's libata-core.c to force Linux to send these drives non-queued Trim commands instead of queued Trim commands: