iaa-crypto.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =========================================
  3. IAA Compression Accelerator Crypto Driver
  4. =========================================
  5. Tom Zanussi <tom.zanussi@linux.intel.com>
  6. The IAA crypto driver supports compression/decompression compatible
  7. with the DEFLATE compression standard described in RFC 1951, which is
  8. the compression/decompression algorithm exported by this module.
  9. The IAA hardware spec can be found here:
  10. https://cdrdv2.intel.com/v1/dl/getContent/721858
  11. The iaa_crypto driver is designed to work as a layer underneath
  12. higher-level compression devices such as zswap.
  13. Users can select IAA compress/decompress acceleration by specifying
  14. one of the supported IAA compression algorithms in whatever facility
  15. allows compression algorithms to be selected.
  16. For example, a zswap device can select the IAA 'fixed' mode
  17. represented by selecting the 'deflate-iaa' crypto compression
  18. algorithm::
  19. # echo deflate-iaa > /sys/module/zswap/parameters/compressor
  20. This will tell zswap to use the IAA 'fixed' compression mode for all
  21. compresses and decompresses.
  22. Currently, there is only one compression modes available, 'fixed'
  23. mode.
  24. The 'fixed' compression mode implements the compression scheme
  25. specified by RFC 1951 and is given the crypto algorithm name
  26. 'deflate-iaa'. (Because the IAA hardware has a 4k history-window
  27. limitation, only buffers <= 4k, or that have been compressed using a
  28. <= 4k history window, are technically compliant with the deflate spec,
  29. which allows for a window of up to 32k. Because of this limitation,
  30. the IAA fixed mode deflate algorithm is given its own algorithm name
  31. rather than simply 'deflate').
  32. Config options and other setup
  33. ==============================
  34. The IAA crypto driver is available via menuconfig using the following
  35. path::
  36. Cryptographic API -> Hardware crypto devices -> Support for Intel(R) IAA Compression Accelerator
  37. In the configuration file the option called CONFIG_CRYPTO_DEV_IAA_CRYPTO.
  38. The IAA crypto driver also supports statistics, which are available
  39. via menuconfig using the following path::
  40. Cryptographic API -> Hardware crypto devices -> Support for Intel(R) IAA Compression -> Enable Intel(R) IAA Compression Accelerator Statistics
  41. In the configuration file the option called CONFIG_CRYPTO_DEV_IAA_CRYPTO_STATS.
  42. The following config options should also be enabled::
  43. CONFIG_IRQ_REMAP=y
  44. CONFIG_INTEL_IOMMU=y
  45. CONFIG_INTEL_IOMMU_SVM=y
  46. CONFIG_PCI_ATS=y
  47. CONFIG_PCI_PRI=y
  48. CONFIG_PCI_PASID=y
  49. CONFIG_INTEL_IDXD=m
  50. CONFIG_INTEL_IDXD_SVM=y
  51. IAA is one of the first Intel accelerator IPs that can work in
  52. conjunction with the Intel IOMMU. There are multiple modes that exist
  53. for testing. Based on IOMMU configuration, there are 3 modes::
  54. - Scalable
  55. - Legacy
  56. - No IOMMU
  57. Scalable mode
  58. -------------
  59. Scalable mode supports Shared Virtual Memory (SVM or SVA). It is
  60. entered when using the kernel boot commandline::
  61. intel_iommu=on,sm_on
  62. with VT-d turned on in BIOS.
  63. With scalable mode, both shared and dedicated workqueues are available
  64. for use.
  65. For scalable mode, the following BIOS settings should be enabled::
  66. Socket Configuration > IIO Configuration > Intel VT for Directed I/O (VT-d) > Intel VT for Directed I/O
  67. Socket Configuration > IIO Configuration > PCIe ENQCMD > ENQCMDS
  68. Legacy mode
  69. -----------
  70. Legacy mode is entered when using the kernel boot commandline::
  71. intel_iommu=off
  72. or VT-d is not turned on in BIOS.
  73. If you have booted into Linux and not sure if VT-d is on, do a "dmesg
  74. | grep -i dmar". If you don't see a number of DMAR devices enumerated,
  75. most likely VT-d is not on.
  76. With legacy mode, only dedicated workqueues are available for use.
  77. No IOMMU mode
  78. -------------
  79. No IOMMU mode is entered when using the kernel boot commandline::
  80. iommu=off.
  81. With no IOMMU mode, only dedicated workqueues are available for use.
  82. Usage
  83. =====
  84. accel-config
  85. ------------
  86. When loaded, the iaa_crypto driver automatically creates a default
  87. configuration and enables it, and assigns default driver attributes.
  88. If a different configuration or set of driver attributes is required,
  89. the user must first disable the IAA devices and workqueues, reset the
  90. configuration, and then re-register the deflate-iaa algorithm with the
  91. crypto subsystem by removing and reinserting the iaa_crypto module.
  92. The :ref:`iaa_disable_script` in the 'Use Cases'
  93. section below can be used to disable the default configuration.
  94. See :ref:`iaa_default_config` below for details of the default
  95. configuration.
  96. More likely than not, however, and because of the complexity and
  97. configurability of the accelerator devices, the user will want to
  98. configure the device and manually enable the desired devices and
  99. workqueues.
  100. The userspace tool to help doing that is called accel-config. Using
  101. accel-config to configure device or loading a previously saved config
  102. is highly recommended. The device can be controlled via sysfs
  103. directly but comes with the warning that you should do this ONLY if
  104. you know exactly what you are doing. The following sections will not
  105. cover the sysfs interface but assumes you will be using accel-config.
  106. The :ref:`iaa_sysfs_config` section in the appendix below can be
  107. consulted for the sysfs interface details if interested.
  108. The accel-config tool along with instructions for building it can be
  109. found here:
  110. https://github.com/intel/idxd-config/#readme
  111. Typical usage
  112. -------------
  113. In order for the iaa_crypto module to actually do any
  114. compression/decompression work on behalf of a facility, one or more
  115. IAA workqueues need to be bound to the iaa_crypto driver.
  116. For instance, here's an example of configuring an IAA workqueue and
  117. binding it to the iaa_crypto driver (note that device names are
  118. specified as 'iax' rather than 'iaa' - this is because upstream still
  119. has the old 'iax' device naming in place) ::
  120. # configure wq1.0
  121. accel-config config-wq --group-id=0 --mode=dedicated --type=kernel --priority=10 --name="iaa_crypto" --driver-name="crypto" iax1/wq1.0
  122. accel-config config-engine iax1/engine1.0 --group-id=0
  123. # enable IAA device iax1
  124. accel-config enable-device iax1
  125. # enable wq1.0 on IAX device iax1
  126. accel-config enable-wq iax1/wq1.0
  127. Whenever a new workqueue is bound to or unbound from the iaa_crypto
  128. driver, the available workqueues are 'rebalanced' such that work
  129. submitted from a particular CPU is given to the most appropriate
  130. workqueue available. Current best practice is to configure and bind
  131. at least one workqueue for each IAA device, but as long as there is at
  132. least one workqueue configured and bound to any IAA device in the
  133. system, the iaa_crypto driver will work, albeit most likely not as
  134. efficiently.
  135. The IAA crypto algorigthms is operational and compression and
  136. decompression operations are fully enabled following the successful
  137. binding of the first IAA workqueue to the iaa_crypto driver.
  138. Similarly, the IAA crypto algorithm is not operational and compression
  139. and decompression operations are disabled following the unbinding of
  140. the last IAA worqueue to the iaa_crypto driver.
  141. As a result, the IAA crypto algorithms and thus the IAA hardware are
  142. only available when one or more workques are bound to the iaa_crypto
  143. driver.
  144. When there are no IAA workqueues bound to the driver, the IAA crypto
  145. algorithms can be unregistered by removing the module.
  146. Driver attributes
  147. -----------------
  148. There are a couple user-configurable driver attributes that can be
  149. used to configure various modes of operation. They're listed below,
  150. along with their default values. To set any of these attributes, echo
  151. the appropriate values to the attribute file located under
  152. /sys/bus/dsa/drivers/crypto/
  153. The attribute settings at the time the IAA algorithms are registered
  154. are captured in each algorithm's crypto_ctx and used for all compresses
  155. and decompresses when using that algorithm.
  156. The available attributes are:
  157. - verify_compress
  158. Toggle compression verification. If set, each compress will be
  159. internally decompressed and the contents verified, returning error
  160. codes if unsuccessful. This can be toggled with 0/1::
  161. echo 0 > /sys/bus/dsa/drivers/crypto/verify_compress
  162. The default setting is '1' - verify all compresses.
  163. - sync_mode
  164. Select mode to be used to wait for completion of each compresses
  165. and decompress operation.
  166. The crypto async interface support implemented by iaa_crypto
  167. provides an implementation that satisfies the interface but does
  168. so in a synchronous manner - it fills and submits the IDXD
  169. descriptor and then loops around waiting for it to complete before
  170. returning. This isn't a problem at the moment, since all existing
  171. callers (e.g. zswap) wrap any asynchronous callees in a
  172. synchronous wrapper anyway.
  173. The iaa_crypto driver does however provide true asynchronous
  174. support for callers that can make use of it. In this mode, it
  175. fills and submits the IDXD descriptor, then returns immediately
  176. with -EINPROGRESS. The caller can then either poll for completion
  177. itself, which requires specific code in the caller which currently
  178. nothing in the upstream kernel implements, or go to sleep and wait
  179. for an interrupt signaling completion. This latter mode is
  180. supported by current users in the kernel such as zswap via
  181. synchronous wrappers. Although it is supported this mode is
  182. significantly slower than the synchronous mode that does the
  183. polling in the iaa_crypto driver previously mentioned.
  184. This mode can be enabled by writing 'async_irq' to the sync_mode
  185. iaa_crypto driver attribute::
  186. echo async_irq > /sys/bus/dsa/drivers/crypto/sync_mode
  187. Async mode without interrupts (caller must poll) can be enabled by
  188. writing 'async' to it (please see Caveat)::
  189. echo async > /sys/bus/dsa/drivers/crypto/sync_mode
  190. The mode that does the polling in the iaa_crypto driver can be
  191. enabled by writing 'sync' to it::
  192. echo sync > /sys/bus/dsa/drivers/crypto/sync_mode
  193. The default mode is 'sync'.
  194. Caveat: since the only mechanism that iaa_crypto currently implements
  195. for async polling without interrupts is via the 'sync' mode as
  196. described earlier, writing 'async' to
  197. '/sys/bus/dsa/drivers/crypto/sync_mode' will internally enable the
  198. 'sync' mode. This is to ensure correct iaa_crypto behavior until true
  199. async polling without interrupts is enabled in iaa_crypto.
  200. .. _iaa_default_config:
  201. IAA Default Configuration
  202. -------------------------
  203. When the iaa_crypto driver is loaded, each IAA device has a single
  204. work queue configured for it, with the following attributes::
  205. mode "dedicated"
  206. threshold 0
  207. size Total WQ Size from WQCAP
  208. priority 10
  209. type IDXD_WQT_KERNEL
  210. group 0
  211. name "iaa_crypto"
  212. driver_name "crypto"
  213. The devices and workqueues are also enabled and therefore the driver
  214. is ready to be used without any additional configuration.
  215. The default driver attributes in effect when the driver is loaded are::
  216. sync_mode "sync"
  217. verify_compress 1
  218. In order to change either the device/work queue or driver attributes,
  219. the enabled devices and workqueues must first be disabled. In order
  220. to have the new configuration applied to the deflate-iaa crypto
  221. algorithm, it needs to be re-registered by removing and reinserting
  222. the iaa_crypto module. The :ref:`iaa_disable_script` in the 'Use
  223. Cases' section below can be used to disable the default configuration.
  224. Statistics
  225. ==========
  226. If the optional debugfs statistics support is enabled, the IAA crypto
  227. driver will generate statistics which can be accessed in debugfs at::
  228. # ls -al /sys/kernel/debug/iaa-crypto/
  229. total 0
  230. drwxr-xr-x 2 root root 0 Mar 3 07:55 .
  231. drwx------ 53 root root 0 Mar 3 07:55 ..
  232. -rw-r--r-- 1 root root 0 Mar 3 07:55 global_stats
  233. -rw-r--r-- 1 root root 0 Mar 3 07:55 stats_reset
  234. -rw-r--r-- 1 root root 0 Mar 3 07:55 wq_stats
  235. The global_stats file shows a set of global statistics collected since
  236. the driver has been loaded or reset::
  237. # cat global_stats
  238. global stats:
  239. total_comp_calls: 4300
  240. total_decomp_calls: 4164
  241. total_sw_decomp_calls: 0
  242. total_comp_bytes_out: 5993989
  243. total_decomp_bytes_in: 5993989
  244. total_completion_einval_errors: 0
  245. total_completion_timeout_errors: 0
  246. total_completion_comp_buf_overflow_errors: 136
  247. The wq_stats file shows per-wq stats, a set for each iaa device and wq
  248. in addition to some global stats::
  249. # cat wq_stats
  250. iaa device:
  251. id: 1
  252. n_wqs: 1
  253. comp_calls: 0
  254. comp_bytes: 0
  255. decomp_calls: 0
  256. decomp_bytes: 0
  257. wqs:
  258. name: iaa_crypto
  259. comp_calls: 0
  260. comp_bytes: 0
  261. decomp_calls: 0
  262. decomp_bytes: 0
  263. iaa device:
  264. id: 3
  265. n_wqs: 1
  266. comp_calls: 0
  267. comp_bytes: 0
  268. decomp_calls: 0
  269. decomp_bytes: 0
  270. wqs:
  271. name: iaa_crypto
  272. comp_calls: 0
  273. comp_bytes: 0
  274. decomp_calls: 0
  275. decomp_bytes: 0
  276. iaa device:
  277. id: 5
  278. n_wqs: 1
  279. comp_calls: 1360
  280. comp_bytes: 1999776
  281. decomp_calls: 0
  282. decomp_bytes: 0
  283. wqs:
  284. name: iaa_crypto
  285. comp_calls: 1360
  286. comp_bytes: 1999776
  287. decomp_calls: 0
  288. decomp_bytes: 0
  289. iaa device:
  290. id: 7
  291. n_wqs: 1
  292. comp_calls: 2940
  293. comp_bytes: 3994213
  294. decomp_calls: 4164
  295. decomp_bytes: 5993989
  296. wqs:
  297. name: iaa_crypto
  298. comp_calls: 2940
  299. comp_bytes: 3994213
  300. decomp_calls: 4164
  301. decomp_bytes: 5993989
  302. ...
  303. Writing to 'stats_reset' resets all the stats, including the
  304. per-device and per-wq stats::
  305. # echo 1 > stats_reset
  306. # cat wq_stats
  307. global stats:
  308. total_comp_calls: 0
  309. total_decomp_calls: 0
  310. total_comp_bytes_out: 0
  311. total_decomp_bytes_in: 0
  312. total_completion_einval_errors: 0
  313. total_completion_timeout_errors: 0
  314. total_completion_comp_buf_overflow_errors: 0
  315. ...
  316. Use cases
  317. =========
  318. Simple zswap test
  319. -----------------
  320. For this example, the kernel should be configured according to the
  321. dedicated mode options described above, and zswap should be enabled as
  322. well::
  323. CONFIG_ZSWAP=y
  324. This is a simple test that uses iaa_compress as the compressor for a
  325. swap (zswap) device. It sets up the zswap device and then uses the
  326. memory_memadvise program listed below to forcibly swap out and in a
  327. specified number of pages, demonstrating both compress and decompress.
  328. The zswap test expects the work queues for each IAA device on the
  329. system to be configured properly as a kernel workqueue with a
  330. workqueue driver_name of "crypto".
  331. The first step is to make sure the iaa_crypto module is loaded::
  332. modprobe iaa_crypto
  333. If the IAA devices and workqueues haven't previously been disabled and
  334. reconfigured, then the default configuration should be in place and no
  335. further IAA configuration is necessary. See :ref:`iaa_default_config`
  336. below for details of the default configuration.
  337. If the default configuration is in place, you should see the iaa
  338. devices and wq0s enabled::
  339. # cat /sys/bus/dsa/devices/iax1/state
  340. enabled
  341. # cat /sys/bus/dsa/devices/iax1/wq1.0/state
  342. enabled
  343. To demonstrate that the following steps work as expected, these
  344. commands can be used to enable debug output::
  345. # echo -n 'module iaa_crypto +p' > /sys/kernel/debug/dynamic_debug/control
  346. # echo -n 'module idxd +p' > /sys/kernel/debug/dynamic_debug/control
  347. Use the following commands to enable zswap::
  348. # echo 0 > /sys/module/zswap/parameters/enabled
  349. # echo 50 > /sys/module/zswap/parameters/max_pool_percent
  350. # echo deflate-iaa > /sys/module/zswap/parameters/compressor
  351. # echo zsmalloc > /sys/module/zswap/parameters/zpool
  352. # echo 1 > /sys/module/zswap/parameters/enabled
  353. # echo 100 > /proc/sys/vm/swappiness
  354. # echo never > /sys/kernel/mm/transparent_hugepage/enabled
  355. # echo 1 > /proc/sys/vm/overcommit_memory
  356. Now you can now run the zswap workload you want to measure. For
  357. example, using the memory_memadvise code below, the following command
  358. will swap in and out 100 pages::
  359. ./memory_madvise 100
  360. Allocating 100 pages to swap in/out
  361. Swapping out 100 pages
  362. Swapping in 100 pages
  363. Swapped out and in 100 pages
  364. You should see something like the following in the dmesg output::
  365. [ 404.202972] idxd 0000:e7:02.0: iaa_comp_acompress: dma_map_sg, src_addr 223925c000, nr_sgs 1, req->src 00000000ee7cb5e6, req->slen 4096, sg_dma_len(sg) 4096
  366. [ 404.202973] idxd 0000:e7:02.0: iaa_comp_acompress: dma_map_sg, dst_addr 21dadf8000, nr_sgs 1, req->dst 000000008d6acea8, req->dlen 4096, sg_dma_len(sg) 8192
  367. [ 404.202975] idxd 0000:e7:02.0: iaa_compress: desc->src1_addr 223925c000, desc->src1_size 4096, desc->dst_addr 21dadf8000, desc->max_dst_size 4096, desc->src2_addr 2203543000, desc->src2_size 1568
  368. [ 404.202981] idxd 0000:e7:02.0: iaa_compress_verify: (verify) desc->src1_addr 21dadf8000, desc->src1_size 228, desc->dst_addr 223925c000, desc->max_dst_size 4096, desc->src2_addr 0, desc->src2_size 0
  369. ...
  370. Now that basic functionality has been demonstrated, the defaults can
  371. be erased and replaced with a different configuration. To do that,
  372. first disable zswap::
  373. # echo lzo > /sys/module/zswap/parameters/compressor
  374. # swapoff -a
  375. # echo 0 > /sys/module/zswap/parameters/accept_threshold_percent
  376. # echo 0 > /sys/module/zswap/parameters/max_pool_percent
  377. # echo 0 > /sys/module/zswap/parameters/enabled
  378. # echo 0 > /sys/module/zswap/parameters/enabled
  379. Then run the :ref:`iaa_disable_script` in the 'Use Cases' section
  380. below to disable the default configuration.
  381. Finally turn swap back on::
  382. # swapon -a
  383. Following all that the IAA device(s) can now be re-configured and
  384. enabled as desired for further testing. Below is one example.
  385. The zswap test expects the work queues for each IAA device on the
  386. system to be configured properly as a kernel workqueue with a
  387. workqueue driver_name of "crypto".
  388. The below script automatically does that::
  389. #!/bin/bash
  390. echo "IAA devices:"
  391. lspci -d:0cfe
  392. echo "# IAA devices:"
  393. lspci -d:0cfe | wc -l
  394. #
  395. # count iaa instances
  396. #
  397. iaa_dev_id="0cfe"
  398. num_iaa=$(lspci -d:${iaa_dev_id} | wc -l)
  399. echo "Found ${num_iaa} IAA instances"
  400. #
  401. # disable iaa wqs and devices
  402. #
  403. echo "Disable IAA"
  404. for ((i = 1; i < ${num_iaa} * 2; i += 2)); do
  405. echo disable wq iax${i}/wq${i}.0
  406. accel-config disable-wq iax${i}/wq${i}.0
  407. echo disable iaa iax${i}
  408. accel-config disable-device iax${i}
  409. done
  410. echo "End Disable IAA"
  411. echo "Reload iaa_crypto module"
  412. rmmod iaa_crypto
  413. modprobe iaa_crypto
  414. echo "End Reload iaa_crypto module"
  415. #
  416. # configure iaa wqs and devices
  417. #
  418. echo "Configure IAA"
  419. for ((i = 1; i < ${num_iaa} * 2; i += 2)); do
  420. accel-config config-wq --group-id=0 --mode=dedicated --wq-size=128 --priority=10 --type=kernel --name="iaa_crypto" --driver-name="crypto" iax${i}/wq${i}.0
  421. accel-config config-engine iax${i}/engine${i}.0 --group-id=0
  422. done
  423. echo "End Configure IAA"
  424. #
  425. # enable iaa wqs and devices
  426. #
  427. echo "Enable IAA"
  428. for ((i = 1; i < ${num_iaa} * 2; i += 2)); do
  429. echo enable iaa iax${i}
  430. accel-config enable-device iax${i}
  431. echo enable wq iax${i}/wq${i}.0
  432. accel-config enable-wq iax${i}/wq${i}.0
  433. done
  434. echo "End Enable IAA"
  435. When the workqueues are bound to the iaa_crypto driver, you should
  436. see something similar to the following in dmesg output if you've
  437. enabled debug output (echo -n 'module iaa_crypto +p' >
  438. /sys/kernel/debug/dynamic_debug/control)::
  439. [ 60.752344] idxd 0000:f6:02.0: add_iaa_wq: added wq 000000004068d14d to iaa 00000000c9585ba2, n_wq 1
  440. [ 60.752346] iaa_crypto: rebalance_wq_table: nr_nodes=2, nr_cpus 160, nr_iaa 8, cpus_per_iaa 20
  441. [ 60.752347] iaa_crypto: rebalance_wq_table: iaa=0
  442. [ 60.752349] idxd 0000:6a:02.0: request_iaa_wq: getting wq from iaa_device 0000000042d7bc52 (0)
  443. [ 60.752350] idxd 0000:6a:02.0: request_iaa_wq: returning unused wq 00000000c8bb4452 (0) from iaa device 0000000042d7bc52 (0)
  444. [ 60.752352] iaa_crypto: rebalance_wq_table: assigned wq for cpu=0, node=0 = wq 00000000c8bb4452
  445. [ 60.752354] iaa_crypto: rebalance_wq_table: iaa=0
  446. [ 60.752355] idxd 0000:6a:02.0: request_iaa_wq: getting wq from iaa_device 0000000042d7bc52 (0)
  447. [ 60.752356] idxd 0000:6a:02.0: request_iaa_wq: returning unused wq 00000000c8bb4452 (0) from iaa device 0000000042d7bc52 (0)
  448. [ 60.752358] iaa_crypto: rebalance_wq_table: assigned wq for cpu=1, node=0 = wq 00000000c8bb4452
  449. [ 60.752359] iaa_crypto: rebalance_wq_table: iaa=0
  450. [ 60.752360] idxd 0000:6a:02.0: request_iaa_wq: getting wq from iaa_device 0000000042d7bc52 (0)
  451. [ 60.752361] idxd 0000:6a:02.0: request_iaa_wq: returning unused wq 00000000c8bb4452 (0) from iaa device 0000000042d7bc52 (0)
  452. [ 60.752362] iaa_crypto: rebalance_wq_table: assigned wq for cpu=2, node=0 = wq 00000000c8bb4452
  453. [ 60.752364] iaa_crypto: rebalance_wq_table: iaa=0
  454. .
  455. .
  456. .
  457. Once the workqueues and devices have been enabled, the IAA crypto
  458. algorithms are enabled and available. When the IAA crypto algorithms
  459. have been successfully enabled, you should see the following dmesg
  460. output::
  461. [ 64.893759] iaa_crypto: iaa_crypto_enable: iaa_crypto now ENABLED
  462. Now run the following zswap-specific setup commands to have zswap use
  463. the 'fixed' compression mode::
  464. echo 0 > /sys/module/zswap/parameters/enabled
  465. echo 50 > /sys/module/zswap/parameters/max_pool_percent
  466. echo deflate-iaa > /sys/module/zswap/parameters/compressor
  467. echo zsmalloc > /sys/module/zswap/parameters/zpool
  468. echo 1 > /sys/module/zswap/parameters/enabled
  469. echo 100 > /proc/sys/vm/swappiness
  470. echo never > /sys/kernel/mm/transparent_hugepage/enabled
  471. echo 1 > /proc/sys/vm/overcommit_memory
  472. Finally, you can now run the zswap workload you want to measure. For
  473. example, using the code below, the following command will swap in and
  474. out 100 pages::
  475. ./memory_madvise 100
  476. Allocating 100 pages to swap in/out
  477. Swapping out 100 pages
  478. Swapping in 100 pages
  479. Swapped out and in 100 pages
  480. You should see something like the following in the dmesg output if
  481. you've enabled debug output (echo -n 'module iaa_crypto +p' >
  482. /sys/kernel/debug/dynamic_debug/control)::
  483. [ 404.202972] idxd 0000:e7:02.0: iaa_comp_acompress: dma_map_sg, src_addr 223925c000, nr_sgs 1, req->src 00000000ee7cb5e6, req->slen 4096, sg_dma_len(sg) 4096
  484. [ 404.202973] idxd 0000:e7:02.0: iaa_comp_acompress: dma_map_sg, dst_addr 21dadf8000, nr_sgs 1, req->dst 000000008d6acea8, req->dlen 4096, sg_dma_len(sg) 8192
  485. [ 404.202975] idxd 0000:e7:02.0: iaa_compress: desc->src1_addr 223925c000, desc->src1_size 4096, desc->dst_addr 21dadf8000, desc->max_dst_size 4096, desc->src2_addr 2203543000, desc->src2_size 1568
  486. [ 404.202981] idxd 0000:e7:02.0: iaa_compress_verify: (verify) desc->src1_addr 21dadf8000, desc->src1_size 228, desc->dst_addr 223925c000, desc->max_dst_size 4096, desc->src2_addr 0, desc->src2_size 0
  487. [ 409.203227] idxd 0000:e7:02.0: iaa_comp_adecompress: dma_map_sg, src_addr 21ddd8b100, nr_sgs 1, req->src 0000000084adab64, req->slen 228, sg_dma_len(sg) 228
  488. [ 409.203235] idxd 0000:e7:02.0: iaa_comp_adecompress: dma_map_sg, dst_addr 21ee3dc000, nr_sgs 1, req->dst 000000004e2990d0, req->dlen 4096, sg_dma_len(sg) 4096
  489. [ 409.203239] idxd 0000:e7:02.0: iaa_decompress: desc->src1_addr 21ddd8b100, desc->src1_size 228, desc->dst_addr 21ee3dc000, desc->max_dst_size 4096, desc->src2_addr 0, desc->src2_size 0
  490. [ 409.203254] idxd 0000:e7:02.0: iaa_comp_adecompress: dma_map_sg, src_addr 21ddd8b100, nr_sgs 1, req->src 0000000084adab64, req->slen 228, sg_dma_len(sg) 228
  491. [ 409.203256] idxd 0000:e7:02.0: iaa_comp_adecompress: dma_map_sg, dst_addr 21f1551000, nr_sgs 1, req->dst 000000004e2990d0, req->dlen 4096, sg_dma_len(sg) 4096
  492. [ 409.203257] idxd 0000:e7:02.0: iaa_decompress: desc->src1_addr 21ddd8b100, desc->src1_size 228, desc->dst_addr 21f1551000, desc->max_dst_size 4096, desc->src2_addr 0, desc->src2_size 0
  493. In order to unregister the IAA crypto algorithms, and register new
  494. ones using different parameters, any users of the current algorithm
  495. should be stopped and the IAA workqueues and devices disabled.
  496. In the case of zswap, remove the IAA crypto algorithm as the
  497. compressor and turn off swap (to remove all references to
  498. iaa_crypto)::
  499. echo lzo > /sys/module/zswap/parameters/compressor
  500. swapoff -a
  501. echo 0 > /sys/module/zswap/parameters/accept_threshold_percent
  502. echo 0 > /sys/module/zswap/parameters/max_pool_percent
  503. echo 0 > /sys/module/zswap/parameters/enabled
  504. Once zswap is disabled and no longer using iaa_crypto, the IAA wqs and
  505. devices can be disabled.
  506. .. _iaa_disable_script:
  507. IAA disable script
  508. ------------------
  509. The below script automatically does that::
  510. #!/bin/bash
  511. echo "IAA devices:"
  512. lspci -d:0cfe
  513. echo "# IAA devices:"
  514. lspci -d:0cfe | wc -l
  515. #
  516. # count iaa instances
  517. #
  518. iaa_dev_id="0cfe"
  519. num_iaa=$(lspci -d:${iaa_dev_id} | wc -l)
  520. echo "Found ${num_iaa} IAA instances"
  521. #
  522. # disable iaa wqs and devices
  523. #
  524. echo "Disable IAA"
  525. for ((i = 1; i < ${num_iaa} * 2; i += 2)); do
  526. echo disable wq iax${i}/wq${i}.0
  527. accel-config disable-wq iax${i}/wq${i}.0
  528. echo disable iaa iax${i}
  529. accel-config disable-device iax${i}
  530. done
  531. echo "End Disable IAA"
  532. Finally, at this point the iaa_crypto module can be removed, which
  533. will unregister the current IAA crypto algorithms::
  534. rmmod iaa_crypto
  535. memory_madvise.c (gcc -o memory_memadvise memory_madvise.c)::
  536. #include <stdio.h>
  537. #include <stdlib.h>
  538. #include <string.h>
  539. #include <unistd.h>
  540. #include <sys/mman.h>
  541. #include <linux/mman.h>
  542. #ifndef MADV_PAGEOUT
  543. #define MADV_PAGEOUT 21 /* force pages out immediately */
  544. #endif
  545. #define PG_SZ 4096
  546. int main(int argc, char **argv)
  547. {
  548. int i, nr_pages = 1;
  549. int64_t *dump_ptr;
  550. char *addr, *a;
  551. int loop = 1;
  552. if (argc > 1)
  553. nr_pages = atoi(argv[1]);
  554. printf("Allocating %d pages to swap in/out\n", nr_pages);
  555. /* allocate pages */
  556. addr = mmap(NULL, nr_pages * PG_SZ, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  557. *addr = 1;
  558. /* initialize data in page to all '*' chars */
  559. memset(addr, '*', nr_pages * PG_SZ);
  560. printf("Swapping out %d pages\n", nr_pages);
  561. /* Tell kernel to swap it out */
  562. madvise(addr, nr_pages * PG_SZ, MADV_PAGEOUT);
  563. while (loop > 0) {
  564. /* Wait for swap out to finish */
  565. sleep(5);
  566. a = addr;
  567. printf("Swapping in %d pages\n", nr_pages);
  568. /* Access the page ... this will swap it back in again */
  569. for (i = 0; i < nr_pages; i++) {
  570. if (a[0] != '*') {
  571. printf("Bad data from decompress!!!!!\n");
  572. dump_ptr = (int64_t *)a;
  573. for (int j = 0; j < 100; j++) {
  574. printf(" page %d data: %#llx\n", i, *dump_ptr);
  575. dump_ptr++;
  576. }
  577. }
  578. a += PG_SZ;
  579. }
  580. loop --;
  581. }
  582. printf("Swapped out and in %d pages\n", nr_pages);
  583. Appendix
  584. ========
  585. .. _iaa_sysfs_config:
  586. IAA sysfs config interface
  587. --------------------------
  588. Below is a description of the IAA sysfs interface, which as mentioned
  589. in the main document, should only be used if you know exactly what you
  590. are doing. Even then, there's no compelling reason to use it directly
  591. since accel-config can do everything the sysfs interface can and in
  592. fact accel-config is based on it under the covers.
  593. The 'IAA config path' is /sys/bus/dsa/devices and contains
  594. subdirectories representing each IAA device, workqueue, engine, and
  595. group. Note that in the sysfs interface, the IAA devices are actually
  596. named using iax e.g. iax1, iax3, etc. (Note that IAA devices are the
  597. odd-numbered devices; the even-numbered devices are DSA devices and
  598. can be ignored for IAA).
  599. The 'IAA device bind path' is /sys/bus/dsa/drivers/idxd/bind and is
  600. the file that is written to enable an IAA device.
  601. The 'IAA workqueue bind path' is /sys/bus/dsa/drivers/crypto/bind and
  602. is the file that is written to enable an IAA workqueue.
  603. Similarly /sys/bus/dsa/drivers/idxd/unbind and
  604. /sys/bus/dsa/drivers/crypto/unbind are used to disable IAA devices and
  605. workqueues.
  606. The basic sequence of commands needed to set up the IAA devices and
  607. workqueues is:
  608. For each device::
  609. 1) Disable any workqueues enabled on the device. For example to
  610. disable workques 0 and 1 on IAA device 3::
  611. # echo wq3.0 > /sys/bus/dsa/drivers/crypto/unbind
  612. # echo wq3.1 > /sys/bus/dsa/drivers/crypto/unbind
  613. 2) Disable the device. For example to disable IAA device 3::
  614. # echo iax3 > /sys/bus/dsa/drivers/idxd/unbind
  615. 3) configure the desired workqueues. For example, to configure
  616. workqueue 3 on IAA device 3::
  617. # echo dedicated > /sys/bus/dsa/devices/iax3/wq3.3/mode
  618. # echo 128 > /sys/bus/dsa/devices/iax3/wq3.3/size
  619. # echo 0 > /sys/bus/dsa/devices/iax3/wq3.3/group_id
  620. # echo 10 > /sys/bus/dsa/devices/iax3/wq3.3/priority
  621. # echo "kernel" > /sys/bus/dsa/devices/iax3/wq3.3/type
  622. # echo "iaa_crypto" > /sys/bus/dsa/devices/iax3/wq3.3/name
  623. # echo "crypto" > /sys/bus/dsa/devices/iax3/wq3.3/driver_name
  624. 4) Enable the device. For example to enable IAA device 3::
  625. # echo iax3 > /sys/bus/dsa/drivers/idxd/bind
  626. 5) Enable the desired workqueues on the device. For example to
  627. enable workques 0 and 1 on IAA device 3::
  628. # echo wq3.0 > /sys/bus/dsa/drivers/crypto/bind
  629. # echo wq3.1 > /sys/bus/dsa/drivers/crypto/bind