1How to use the Kernel Samepage Merging feature
   4KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
   5added to the Linux kernel in 2.6.32.  See mm/ksm.c for its implementation,
   6and and
   8The KSM daemon ksmd periodically scans those areas of user memory which
   9have been registered with it, looking for pages of identical content which
  10can be replaced by a single write-protected page (which is automatically
  11copied if a process later wants to update its content).
  13KSM was originally developed for use with KVM (where it was known as
  14Kernel Shared Memory), to fit more virtual machines into physical memory,
  15by sharing the data common between them.  But it can be useful to any
  16application which generates many instances of the same data.
  18KSM only merges anonymous (private) pages, never pagecache (file) pages.
  19KSM's merged pages were originally locked into kernel memory, but can now
  20be swapped out just like other user pages (but sharing is broken when they
  21are swapped back in: ksmd must rediscover their identity and merge again).
  23KSM only operates on those areas of address space which an application
  24has advised to be likely candidates for merging, by using the madvise(2)
  25system call: int madvise(addr, length, MADV_MERGEABLE).
  27The app may call int madvise(addr, length, MADV_UNMERGEABLE) to cancel
  28that advice and restore unshared pages: whereupon KSM unmerges whatever
  29it merged in that range.  Note: this unmerging call may suddenly require
  30more memory than is available - possibly failing with EAGAIN, but more
  31probably arousing the Out-Of-Memory killer.
  33If KSM is not configured into the running kernel, madvise MADV_MERGEABLE
  34and MADV_UNMERGEABLE simply fail with EINVAL.  If the running kernel was
  35built with CONFIG_KSM=y, those calls will normally succeed: even if the
  36the KSM daemon is not currently running, MADV_MERGEABLE still registers
  37the range for whenever the KSM daemon is started; even if the range
  38cannot contain any pages which KSM could actually merge; even if

  rge;txt#L39" i" id.39  30more memor4 than40ation/vm/ksm.txt#L4" id="L4" class="line"e" name=4L31">  31probably a4ousin4 the Lbut sharin
  33If KSM is 4ot co4figurincludeuddenst redgapen llyugt#Lorticaless shel, erntaicalmst red whic)on/vm/ksm.txt#L15" id="L15" class="li4" name="LL4">   4KSM is a memMERGE4BLE simplm;">< the runningf="Do"Docwhicenyugt#le - po,
  35built with4CONFI45ation/vm/ksm.txt#L4" id="L4" class="line"e" name=4L36">  36the KSM da4mon i4 not Acumentatiocathven ior h Ksin)rref="nrge aga was fd.39  37the range 4or wh4never: whricty
  38cannot con4ain a4y pag fd updatey
  2939sm.txt#L394 id="49ation/vm/ksm.txt#L4" id="L4" class="line5e" name=5L30">  30more memor5 than50d periodically scaniocumenrola href=se(fg tily,
  31probably a5ousin51ever: adg witecteDocmentwhicg wit areaectroot:n/vm/ksm.txt#L5" id="L5" class="line"e" name=5L32">  32
  33If KSM is 5ot co5figuractua_to_ser onte- how same p: wumenactuals fsa hreffilabheir gouals fsleepn/vm/ksm.txt#L33" id="L33" class="li5" name="5L4">   4KSM is a me5MERGE5BLE s                   e.g. "echo 100    5added to theCONFI55LE s                   Default: 100 (cly sn ,
  36the KSM da5mon i5tation/vm/ksm.txt#L27" id="L27" class="li5e" name=5L37">  37the range 5or wh5neversleep_m"Dohrecste- how same m"Dohrecondsbheir thven isleepreffilabnextfsa hn/vm/ksm.txt#L27" id="L27" class="li5e" name=5L38">  38cannot con5ain a58LE s                   e.g. "echo 20   2939sm.txt#L395 id="59LE s                   Default: 20 (cly sn ,
  30more memor6 than60ation/vm/ksm.txt#L4" id="L4" class="line6e" name=6L31">  31probably a6ousin61ever:un              - set 0ls fstopbheir from href="Domentkeeprinally lockeon/vm/ksm.txt#L6" id="L6" class="line"e" name=6L32">  32
  33If KSM is 6ot co63LE s                   set 2ls fstopbheir implref="DoteDocactualV_MERGEABLinallyon/vm/ksm.txt#L6" id="L6" class="line"" name="6L4">   4KSM is a me6MERGE6BLE s                         mentleth iinallg wit whichoking for p,
   5added to th6CONFI65LE s                   Default: 0 (identor hhatio/opti1ls f="poecacdicaon/vm/ksm.txt#L6" id="L6" class="line"" name="LL6">   6and   37the range 6or wh6tation/vm/ksm.txt#L18" id="L18" class="li6e" name=6L38">  38cannot con6ain a6md periodeffe"poernesch anicalimply fai"
  2939sm.txt#L396 id="69ation/vm/ksm.txt#L4" id="L4" class="line7e" name=7L30">  30more memor7 than70iguractua_n KSM u    - how same n KSM unmerge whebe="Doe Mdn/vm/ksm.txt#L4" id="L4" class="line7e" name=7L31">  31probably a7ousin71iguractua_n KS="Do   - how same ailabsitrge when between tm i.e. how such sth dn/vm/ksm.txt#L4" id="L4" class="line7e" name=7L32">  32
  33If KSM is 7ot co7figuractua_voltatwitte- how same pmergehhatitweenoo thents for te-prot cou treon/vm/ksm.txt#L38" id="L38" class="li7" name="7L4">   4KSM is a me7MERGE7BLE sfull_ser me u    - how same timrge e
   5added to th7CONFI75ation/vm/ksm.txt#L4" id="L4" class="line7" name="7L6">   6and    7
  38cannot con7ain a78iguractua_voltatwitembra  2939sm.txt#L397 id="79the Outportiess shwhewven iflsoa neas tr pooga was fd
  30more memor8 than80ation/vm/ksm.txt#L4" id="L4" class="line8e" name=8L31">  31probably a8ousin81igurIzik Eidueon/vm/ksm.txt#L6" id="L6" class="line8e" name=8L32">  32
  33If KSM is 8ot co8figur

33 riod kernel LXR softw whebys shet/Articles/33058sourcffilge/a> LXR But unityigur,ay sudexse a6" idl " isiessbyst/Articlemailto:lxo@tion/.no">lxo@tion/.noigur. nt"> 33 lxo.tion/.no kindingly t href=t/Articles/33058www.SM p"">RM p"Do2. Out ASigur,aOutvin)rs fd. Seeh Ksulty