binder.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677
  1. /* binder.c
  2. *
  3. * Android IPC Subsystem
  4. *
  5. * Copyright (C) 2007-2008 Google, Inc.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <asm/cacheflush.h>
  19. #include <linux/fdtable.h>
  20. #include <linux/file.h>
  21. #include <linux/freezer.h>
  22. #include <linux/fs.h>
  23. #include <linux/list.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/nsproxy.h>
  29. #include <linux/poll.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/sched.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/slab.h>
  37. #include <linux/pid_namespace.h>
  38. #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
  39. #define BINDER_IPC_32BIT 1
  40. #endif
  41. #include <uapi/linux/android/binder.h>
  42. #include "binder_trace.h"
  43. static DEFINE_MUTEX(binder_main_lock);
  44. static DEFINE_MUTEX(binder_deferred_lock);
  45. static DEFINE_MUTEX(binder_mmap_lock);
  46. static HLIST_HEAD(binder_procs);
  47. static HLIST_HEAD(binder_deferred_list);
  48. static HLIST_HEAD(binder_dead_nodes);
  49. static struct dentry *binder_debugfs_dir_entry_root;
  50. static struct dentry *binder_debugfs_dir_entry_proc;
  51. static struct binder_node *binder_context_mgr_node;
  52. static kuid_t binder_context_mgr_uid = INVALID_UID;
  53. static int binder_last_id;
  54. static struct workqueue_struct *binder_deferred_workqueue;
  55. #define BINDER_DEBUG_ENTRY(name) \
  56. static int binder_##name##_open(struct inode *inode, struct file *file) \
  57. { \
  58. return single_open(file, binder_##name##_show, inode->i_private); \
  59. } \
  60. \
  61. static const struct file_operations binder_##name##_fops = { \
  62. .owner = THIS_MODULE, \
  63. .open = binder_##name##_open, \
  64. .read = seq_read, \
  65. .llseek = seq_lseek, \
  66. .release = single_release, \
  67. }
  68. static int binder_proc_show(struct seq_file *m, void *unused);
  69. BINDER_DEBUG_ENTRY(proc);
  70. /* This is only defined in include/asm-arm/sizes.h */
  71. #ifndef SZ_1K
  72. #define SZ_1K 0x400
  73. #endif
  74. #ifndef SZ_4M
  75. #define SZ_4M 0x400000
  76. #endif
  77. #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
  78. #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
  79. enum {
  80. BINDER_DEBUG_USER_ERROR = 1U << 0,
  81. BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
  82. BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
  83. BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
  84. BINDER_DEBUG_DEAD_BINDER = 1U << 4,
  85. BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
  86. BINDER_DEBUG_READ_WRITE = 1U << 6,
  87. BINDER_DEBUG_USER_REFS = 1U << 7,
  88. BINDER_DEBUG_THREADS = 1U << 8,
  89. BINDER_DEBUG_TRANSACTION = 1U << 9,
  90. BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
  91. BINDER_DEBUG_FREE_BUFFER = 1U << 11,
  92. BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
  93. BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
  94. BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
  95. BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
  96. };
  97. static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
  98. BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
  99. module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
  100. static bool binder_debug_no_lock;
  101. module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
  102. static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
  103. static int binder_stop_on_user_error;
  104. static int binder_set_stop_on_user_error(const char *val,
  105. struct kernel_param *kp)
  106. {
  107. int ret;
  108. ret = param_set_int(val, kp);
  109. if (binder_stop_on_user_error < 2)
  110. wake_up(&binder_user_error_wait);
  111. return ret;
  112. }
  113. module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
  114. param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
  115. #define binder_debug(mask, x...) \
  116. do { \
  117. if (binder_debug_mask & mask) \
  118. pr_info(x); \
  119. } while (0)
  120. #define binder_user_error(x...) \
  121. do { \
  122. if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
  123. pr_info(x); \
  124. if (binder_stop_on_user_error) \
  125. binder_stop_on_user_error = 2; \
  126. } while (0)
  127. enum binder_stat_types {
  128. BINDER_STAT_PROC,
  129. BINDER_STAT_THREAD,
  130. BINDER_STAT_NODE,
  131. BINDER_STAT_REF,
  132. BINDER_STAT_DEATH,
  133. BINDER_STAT_TRANSACTION,
  134. BINDER_STAT_TRANSACTION_COMPLETE,
  135. BINDER_STAT_COUNT
  136. };
  137. struct binder_stats {
  138. int br[_IOC_NR(BR_FAILED_REPLY) + 1];
  139. int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
  140. int obj_created[BINDER_STAT_COUNT];
  141. int obj_deleted[BINDER_STAT_COUNT];
  142. };
  143. static struct binder_stats binder_stats;
  144. static inline void binder_stats_deleted(enum binder_stat_types type)
  145. {
  146. binder_stats.obj_deleted[type]++;
  147. }
  148. static inline void binder_stats_created(enum binder_stat_types type)
  149. {
  150. binder_stats.obj_created[type]++;
  151. }
  152. struct binder_transaction_log_entry {
  153. int debug_id;
  154. int call_type;
  155. int from_proc;
  156. int from_thread;
  157. int target_handle;
  158. int to_proc;
  159. int to_thread;
  160. int to_node;
  161. int data_size;
  162. int offsets_size;
  163. };
  164. struct binder_transaction_log {
  165. int next;
  166. int full;
  167. struct binder_transaction_log_entry entry[32];
  168. };
  169. static struct binder_transaction_log binder_transaction_log;
  170. static struct binder_transaction_log binder_transaction_log_failed;
  171. static struct binder_transaction_log_entry *binder_transaction_log_add(
  172. struct binder_transaction_log *log)
  173. {
  174. struct binder_transaction_log_entry *e;
  175. e = &log->entry[log->next];
  176. memset(e, 0, sizeof(*e));
  177. log->next++;
  178. if (log->next == ARRAY_SIZE(log->entry)) {
  179. log->next = 0;
  180. log->full = 1;
  181. }
  182. return e;
  183. }
  184. struct binder_work {
  185. struct list_head entry;
  186. enum {
  187. BINDER_WORK_TRANSACTION = 1,
  188. BINDER_WORK_TRANSACTION_COMPLETE,
  189. BINDER_WORK_NODE,
  190. BINDER_WORK_DEAD_BINDER,
  191. BINDER_WORK_DEAD_BINDER_AND_CLEAR,
  192. BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
  193. } type;
  194. };
  195. struct binder_node {
  196. int debug_id;
  197. struct binder_work work;
  198. union {
  199. struct rb_node rb_node;
  200. struct hlist_node dead_node;
  201. };
  202. struct binder_proc *proc;
  203. struct hlist_head refs;
  204. int internal_strong_refs;
  205. int local_weak_refs;
  206. int local_strong_refs;
  207. binder_uintptr_t ptr;
  208. binder_uintptr_t cookie;
  209. unsigned has_strong_ref:1;
  210. unsigned pending_strong_ref:1;
  211. unsigned has_weak_ref:1;
  212. unsigned pending_weak_ref:1;
  213. unsigned has_async_transaction:1;
  214. unsigned accept_fds:1;
  215. unsigned min_priority:8;
  216. struct list_head async_todo;
  217. };
  218. struct binder_ref_death {
  219. struct binder_work work;
  220. binder_uintptr_t cookie;
  221. };
  222. struct binder_ref {
  223. /* Lookups needed: */
  224. /* node + proc => ref (transaction) */
  225. /* desc + proc => ref (transaction, inc/dec ref) */
  226. /* node => refs + procs (proc exit) */
  227. int debug_id;
  228. struct rb_node rb_node_desc;
  229. struct rb_node rb_node_node;
  230. struct hlist_node node_entry;
  231. struct binder_proc *proc;
  232. struct binder_node *node;
  233. uint32_t desc;
  234. int strong;
  235. int weak;
  236. struct binder_ref_death *death;
  237. };
  238. struct binder_buffer {
  239. struct list_head entry; /* free and allocated entries by address */
  240. struct rb_node rb_node; /* free entry by size or allocated entry */
  241. /* by address */
  242. unsigned free:1;
  243. unsigned allow_user_free:1;
  244. unsigned async_transaction:1;
  245. unsigned debug_id:29;
  246. struct binder_transaction *transaction;
  247. struct binder_node *target_node;
  248. size_t data_size;
  249. size_t offsets_size;
  250. uint8_t data[0];
  251. };
  252. enum binder_deferred_state {
  253. BINDER_DEFERRED_PUT_FILES = 0x01,
  254. BINDER_DEFERRED_FLUSH = 0x02,
  255. BINDER_DEFERRED_RELEASE = 0x04,
  256. };
  257. struct binder_proc {
  258. struct hlist_node proc_node;
  259. struct rb_root threads;
  260. struct rb_root nodes;
  261. struct rb_root refs_by_desc;
  262. struct rb_root refs_by_node;
  263. int pid;
  264. struct vm_area_struct *vma;
  265. struct mm_struct *vma_vm_mm;
  266. struct task_struct *tsk;
  267. struct files_struct *files;
  268. struct hlist_node deferred_work_node;
  269. int deferred_work;
  270. void *buffer;
  271. ptrdiff_t user_buffer_offset;
  272. struct list_head buffers;
  273. struct rb_root free_buffers;
  274. struct rb_root allocated_buffers;
  275. size_t free_async_space;
  276. struct page **pages;
  277. size_t buffer_size;
  278. uint32_t buffer_free;
  279. struct list_head todo;
  280. wait_queue_head_t wait;
  281. struct binder_stats stats;
  282. struct list_head delivered_death;
  283. int max_threads;
  284. int requested_threads;
  285. int requested_threads_started;
  286. int ready_threads;
  287. long default_priority;
  288. struct dentry *debugfs_entry;
  289. };
  290. enum {
  291. BINDER_LOOPER_STATE_REGISTERED = 0x01,
  292. BINDER_LOOPER_STATE_ENTERED = 0x02,
  293. BINDER_LOOPER_STATE_EXITED = 0x04,
  294. BINDER_LOOPER_STATE_INVALID = 0x08,
  295. BINDER_LOOPER_STATE_WAITING = 0x10,
  296. BINDER_LOOPER_STATE_NEED_RETURN = 0x20
  297. };
  298. struct binder_thread {
  299. struct binder_proc *proc;
  300. struct rb_node rb_node;
  301. int pid;
  302. int looper;
  303. struct binder_transaction *transaction_stack;
  304. struct list_head todo;
  305. uint32_t return_error; /* Write failed, return error code in read buf */
  306. uint32_t return_error2; /* Write failed, return error code in read */
  307. /* buffer. Used when sending a reply to a dead process that */
  308. /* we are also waiting on */
  309. wait_queue_head_t wait;
  310. struct binder_stats stats;
  311. };
  312. struct binder_transaction {
  313. int debug_id;
  314. struct binder_work work;
  315. struct binder_thread *from;
  316. struct binder_transaction *from_parent;
  317. struct binder_proc *to_proc;
  318. struct binder_thread *to_thread;
  319. struct binder_transaction *to_parent;
  320. unsigned need_reply:1;
  321. /* unsigned is_dead:1; */ /* not used at the moment */
  322. struct binder_buffer *buffer;
  323. unsigned int code;
  324. unsigned int flags;
  325. long priority;
  326. long saved_priority;
  327. kuid_t sender_euid;
  328. };
  329. static void
  330. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
  331. static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
  332. {
  333. struct files_struct *files = proc->files;
  334. unsigned long rlim_cur;
  335. unsigned long irqs;
  336. if (files == NULL)
  337. return -ESRCH;
  338. if (!lock_task_sighand(proc->tsk, &irqs))
  339. return -EMFILE;
  340. rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
  341. unlock_task_sighand(proc->tsk, &irqs);
  342. return __alloc_fd(files, 0, rlim_cur, flags);
  343. }
  344. /*
  345. * copied from fd_install
  346. */
  347. static void task_fd_install(
  348. struct binder_proc *proc, unsigned int fd, struct file *file)
  349. {
  350. if (proc->files)
  351. __fd_install(proc->files, fd, file);
  352. }
  353. /*
  354. * copied from sys_close
  355. */
  356. static long task_close_fd(struct binder_proc *proc, unsigned int fd)
  357. {
  358. int retval;
  359. if (proc->files == NULL)
  360. return -ESRCH;
  361. retval = __close_fd(proc->files, fd);
  362. /* can't restart close syscall because file table entry was cleared */
  363. if (unlikely(retval == -ERESTARTSYS ||
  364. retval == -ERESTARTNOINTR ||
  365. retval == -ERESTARTNOHAND ||
  366. retval == -ERESTART_RESTARTBLOCK))
  367. retval = -EINTR;
  368. return retval;
  369. }
  370. static inline void binder_lock(const char *tag)
  371. {
  372. trace_binder_lock(tag);
  373. mutex_lock(&binder_main_lock);
  374. trace_binder_locked(tag);
  375. }
  376. static inline void binder_unlock(const char *tag)
  377. {
  378. trace_binder_unlock(tag);
  379. mutex_unlock(&binder_main_lock);
  380. }
  381. static void binder_set_nice(long nice)
  382. {
  383. long min_nice;
  384. if (can_nice(current, nice)) {
  385. set_user_nice(current, nice);
  386. return;
  387. }
  388. min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
  389. binder_debug(BINDER_DEBUG_PRIORITY_CAP,
  390. "%d: nice value %ld not allowed use %ld instead\n",
  391. current->pid, nice, min_nice);
  392. set_user_nice(current, min_nice);
  393. if (min_nice <= MAX_NICE)
  394. return;
  395. binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
  396. }
  397. static size_t binder_buffer_size(struct binder_proc *proc,
  398. struct binder_buffer *buffer)
  399. {
  400. if (list_is_last(&buffer->entry, &proc->buffers))
  401. return proc->buffer + proc->buffer_size - (void *)buffer->data;
  402. return (size_t)list_entry(buffer->entry.next,
  403. struct binder_buffer, entry) - (size_t)buffer->data;
  404. }
  405. static void binder_insert_free_buffer(struct binder_proc *proc,
  406. struct binder_buffer *new_buffer)
  407. {
  408. struct rb_node **p = &proc->free_buffers.rb_node;
  409. struct rb_node *parent = NULL;
  410. struct binder_buffer *buffer;
  411. size_t buffer_size;
  412. size_t new_buffer_size;
  413. BUG_ON(!new_buffer->free);
  414. new_buffer_size = binder_buffer_size(proc, new_buffer);
  415. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  416. "%d: add free buffer, size %zd, at %p\n",
  417. proc->pid, new_buffer_size, new_buffer);
  418. while (*p) {
  419. parent = *p;
  420. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  421. BUG_ON(!buffer->free);
  422. buffer_size = binder_buffer_size(proc, buffer);
  423. if (new_buffer_size < buffer_size)
  424. p = &parent->rb_left;
  425. else
  426. p = &parent->rb_right;
  427. }
  428. rb_link_node(&new_buffer->rb_node, parent, p);
  429. rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
  430. }
  431. static void binder_insert_allocated_buffer(struct binder_proc *proc,
  432. struct binder_buffer *new_buffer)
  433. {
  434. struct rb_node **p = &proc->allocated_buffers.rb_node;
  435. struct rb_node *parent = NULL;
  436. struct binder_buffer *buffer;
  437. BUG_ON(new_buffer->free);
  438. while (*p) {
  439. parent = *p;
  440. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  441. BUG_ON(buffer->free);
  442. if (new_buffer < buffer)
  443. p = &parent->rb_left;
  444. else if (new_buffer > buffer)
  445. p = &parent->rb_right;
  446. else
  447. BUG();
  448. }
  449. rb_link_node(&new_buffer->rb_node, parent, p);
  450. rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
  451. }
  452. static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
  453. uintptr_t user_ptr)
  454. {
  455. struct rb_node *n = proc->allocated_buffers.rb_node;
  456. struct binder_buffer *buffer;
  457. struct binder_buffer *kern_ptr;
  458. kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
  459. - offsetof(struct binder_buffer, data));
  460. while (n) {
  461. buffer = rb_entry(n, struct binder_buffer, rb_node);
  462. BUG_ON(buffer->free);
  463. if (kern_ptr < buffer)
  464. n = n->rb_left;
  465. else if (kern_ptr > buffer)
  466. n = n->rb_right;
  467. else
  468. return buffer;
  469. }
  470. return NULL;
  471. }
  472. static int binder_update_page_range(struct binder_proc *proc, int allocate,
  473. void *start, void *end,
  474. struct vm_area_struct *vma)
  475. {
  476. void *page_addr;
  477. unsigned long user_page_addr;
  478. struct vm_struct tmp_area;
  479. struct page **page;
  480. struct mm_struct *mm;
  481. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  482. "%d: %s pages %p-%p\n", proc->pid,
  483. allocate ? "allocate" : "free", start, end);
  484. if (end <= start)
  485. return 0;
  486. trace_binder_update_page_range(proc, allocate, start, end);
  487. if (vma)
  488. mm = NULL;
  489. else
  490. mm = get_task_mm(proc->tsk);
  491. if (mm) {
  492. down_write(&mm->mmap_sem);
  493. vma = proc->vma;
  494. if (vma && mm != proc->vma_vm_mm) {
  495. pr_err("%d: vma mm and task mm mismatch\n",
  496. proc->pid);
  497. vma = NULL;
  498. }
  499. }
  500. if (allocate == 0)
  501. goto free_range;
  502. if (vma == NULL) {
  503. pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
  504. proc->pid);
  505. goto err_no_vma;
  506. }
  507. for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
  508. int ret;
  509. page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
  510. BUG_ON(*page);
  511. *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
  512. if (*page == NULL) {
  513. pr_err("%d: binder_alloc_buf failed for page at %p\n",
  514. proc->pid, page_addr);
  515. goto err_alloc_page_failed;
  516. }
  517. tmp_area.addr = page_addr;
  518. tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
  519. ret = map_vm_area(&tmp_area, PAGE_KERNEL, page);
  520. if (ret) {
  521. pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
  522. proc->pid, page_addr);
  523. goto err_map_kernel_failed;
  524. }
  525. user_page_addr =
  526. (uintptr_t)page_addr + proc->user_buffer_offset;
  527. ret = vm_insert_page(vma, user_page_addr, page[0]);
  528. if (ret) {
  529. pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
  530. proc->pid, user_page_addr);
  531. goto err_vm_insert_page_failed;
  532. }
  533. /* vm_insert_page does not seem to increment the refcount */
  534. }
  535. if (mm) {
  536. up_write(&mm->mmap_sem);
  537. mmput(mm);
  538. }
  539. return 0;
  540. free_range:
  541. for (page_addr = end - PAGE_SIZE; page_addr >= start;
  542. page_addr -= PAGE_SIZE) {
  543. page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
  544. if (vma)
  545. zap_page_range(vma, (uintptr_t)page_addr +
  546. proc->user_buffer_offset, PAGE_SIZE, NULL);
  547. err_vm_insert_page_failed:
  548. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  549. err_map_kernel_failed:
  550. __free_page(*page);
  551. *page = NULL;
  552. err_alloc_page_failed:
  553. ;
  554. }
  555. err_no_vma:
  556. if (mm) {
  557. up_write(&mm->mmap_sem);
  558. mmput(mm);
  559. }
  560. return -ENOMEM;
  561. }
  562. static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
  563. size_t data_size,
  564. size_t offsets_size, int is_async)
  565. {
  566. struct rb_node *n = proc->free_buffers.rb_node;
  567. struct binder_buffer *buffer;
  568. size_t buffer_size;
  569. struct rb_node *best_fit = NULL;
  570. void *has_page_addr;
  571. void *end_page_addr;
  572. size_t size;
  573. if (proc->vma == NULL) {
  574. pr_err("%d: binder_alloc_buf, no vma\n",
  575. proc->pid);
  576. return NULL;
  577. }
  578. size = ALIGN(data_size, sizeof(void *)) +
  579. ALIGN(offsets_size, sizeof(void *));
  580. if (size < data_size || size < offsets_size) {
  581. binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
  582. proc->pid, data_size, offsets_size);
  583. return NULL;
  584. }
  585. if (is_async &&
  586. proc->free_async_space < size + sizeof(struct binder_buffer)) {
  587. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  588. "%d: binder_alloc_buf size %zd failed, no async space left\n",
  589. proc->pid, size);
  590. return NULL;
  591. }
  592. while (n) {
  593. buffer = rb_entry(n, struct binder_buffer, rb_node);
  594. BUG_ON(!buffer->free);
  595. buffer_size = binder_buffer_size(proc, buffer);
  596. if (size < buffer_size) {
  597. best_fit = n;
  598. n = n->rb_left;
  599. } else if (size > buffer_size)
  600. n = n->rb_right;
  601. else {
  602. best_fit = n;
  603. break;
  604. }
  605. }
  606. if (best_fit == NULL) {
  607. pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
  608. proc->pid, size);
  609. return NULL;
  610. }
  611. if (n == NULL) {
  612. buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
  613. buffer_size = binder_buffer_size(proc, buffer);
  614. }
  615. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  616. "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
  617. proc->pid, size, buffer, buffer_size);
  618. has_page_addr =
  619. (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
  620. if (n == NULL) {
  621. if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
  622. buffer_size = size; /* no room for other buffers */
  623. else
  624. buffer_size = size + sizeof(struct binder_buffer);
  625. }
  626. end_page_addr =
  627. (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
  628. if (end_page_addr > has_page_addr)
  629. end_page_addr = has_page_addr;
  630. if (binder_update_page_range(proc, 1,
  631. (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
  632. return NULL;
  633. rb_erase(best_fit, &proc->free_buffers);
  634. buffer->free = 0;
  635. binder_insert_allocated_buffer(proc, buffer);
  636. if (buffer_size != size) {
  637. struct binder_buffer *new_buffer = (void *)buffer->data + size;
  638. list_add(&new_buffer->entry, &buffer->entry);
  639. new_buffer->free = 1;
  640. binder_insert_free_buffer(proc, new_buffer);
  641. }
  642. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  643. "%d: binder_alloc_buf size %zd got %p\n",
  644. proc->pid, size, buffer);
  645. buffer->data_size = data_size;
  646. buffer->offsets_size = offsets_size;
  647. buffer->async_transaction = is_async;
  648. if (is_async) {
  649. proc->free_async_space -= size + sizeof(struct binder_buffer);
  650. binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  651. "%d: binder_alloc_buf size %zd async free %zd\n",
  652. proc->pid, size, proc->free_async_space);
  653. }
  654. return buffer;
  655. }
  656. static void *buffer_start_page(struct binder_buffer *buffer)
  657. {
  658. return (void *)((uintptr_t)buffer & PAGE_MASK);
  659. }
  660. static void *buffer_end_page(struct binder_buffer *buffer)
  661. {
  662. return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
  663. }
  664. static void binder_delete_free_buffer(struct binder_proc *proc,
  665. struct binder_buffer *buffer)
  666. {
  667. struct binder_buffer *prev, *next = NULL;
  668. int free_page_end = 1;
  669. int free_page_start = 1;
  670. BUG_ON(proc->buffers.next == &buffer->entry);
  671. prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
  672. BUG_ON(!prev->free);
  673. if (buffer_end_page(prev) == buffer_start_page(buffer)) {
  674. free_page_start = 0;
  675. if (buffer_end_page(prev) == buffer_end_page(buffer))
  676. free_page_end = 0;
  677. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  678. "%d: merge free, buffer %p share page with %p\n",
  679. proc->pid, buffer, prev);
  680. }
  681. if (!list_is_last(&buffer->entry, &proc->buffers)) {
  682. next = list_entry(buffer->entry.next,
  683. struct binder_buffer, entry);
  684. if (buffer_start_page(next) == buffer_end_page(buffer)) {
  685. free_page_end = 0;
  686. if (buffer_start_page(next) ==
  687. buffer_start_page(buffer))
  688. free_page_start = 0;
  689. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  690. "%d: merge free, buffer %p share page with %p\n",
  691. proc->pid, buffer, prev);
  692. }
  693. }
  694. list_del(&buffer->entry);
  695. if (free_page_start || free_page_end) {
  696. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  697. "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
  698. proc->pid, buffer, free_page_start ? "" : " end",
  699. free_page_end ? "" : " start", prev, next);
  700. binder_update_page_range(proc, 0, free_page_start ?
  701. buffer_start_page(buffer) : buffer_end_page(buffer),
  702. (free_page_end ? buffer_end_page(buffer) :
  703. buffer_start_page(buffer)) + PAGE_SIZE, NULL);
  704. }
  705. }
  706. static void binder_free_buf(struct binder_proc *proc,
  707. struct binder_buffer *buffer)
  708. {
  709. size_t size, buffer_size;
  710. buffer_size = binder_buffer_size(proc, buffer);
  711. size = ALIGN(buffer->data_size, sizeof(void *)) +
  712. ALIGN(buffer->offsets_size, sizeof(void *));
  713. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  714. "%d: binder_free_buf %p size %zd buffer_size %zd\n",
  715. proc->pid, buffer, size, buffer_size);
  716. BUG_ON(buffer->free);
  717. BUG_ON(size > buffer_size);
  718. BUG_ON(buffer->transaction != NULL);
  719. BUG_ON((void *)buffer < proc->buffer);
  720. BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
  721. if (buffer->async_transaction) {
  722. proc->free_async_space += size + sizeof(struct binder_buffer);
  723. binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  724. "%d: binder_free_buf size %zd async free %zd\n",
  725. proc->pid, size, proc->free_async_space);
  726. }
  727. binder_update_page_range(proc, 0,
  728. (void *)PAGE_ALIGN((uintptr_t)buffer->data),
  729. (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
  730. NULL);
  731. rb_erase(&buffer->rb_node, &proc->allocated_buffers);
  732. buffer->free = 1;
  733. if (!list_is_last(&buffer->entry, &proc->buffers)) {
  734. struct binder_buffer *next = list_entry(buffer->entry.next,
  735. struct binder_buffer, entry);
  736. if (next->free) {
  737. rb_erase(&next->rb_node, &proc->free_buffers);
  738. binder_delete_free_buffer(proc, next);
  739. }
  740. }
  741. if (proc->buffers.next != &buffer->entry) {
  742. struct binder_buffer *prev = list_entry(buffer->entry.prev,
  743. struct binder_buffer, entry);
  744. if (prev->free) {
  745. binder_delete_free_buffer(proc, buffer);
  746. rb_erase(&prev->rb_node, &proc->free_buffers);
  747. buffer = prev;
  748. }
  749. }
  750. binder_insert_free_buffer(proc, buffer);
  751. }
  752. static struct binder_node *binder_get_node(struct binder_proc *proc,
  753. binder_uintptr_t ptr)
  754. {
  755. struct rb_node *n = proc->nodes.rb_node;
  756. struct binder_node *node;
  757. while (n) {
  758. node = rb_entry(n, struct binder_node, rb_node);
  759. if (ptr < node->ptr)
  760. n = n->rb_left;
  761. else if (ptr > node->ptr)
  762. n = n->rb_right;
  763. else
  764. return node;
  765. }
  766. return NULL;
  767. }
  768. static struct binder_node *binder_new_node(struct binder_proc *proc,
  769. binder_uintptr_t ptr,
  770. binder_uintptr_t cookie)
  771. {
  772. struct rb_node **p = &proc->nodes.rb_node;
  773. struct rb_node *parent = NULL;
  774. struct binder_node *node;
  775. while (*p) {
  776. parent = *p;
  777. node = rb_entry(parent, struct binder_node, rb_node);
  778. if (ptr < node->ptr)
  779. p = &(*p)->rb_left;
  780. else if (ptr > node->ptr)
  781. p = &(*p)->rb_right;
  782. else
  783. return NULL;
  784. }
  785. node = kzalloc(sizeof(*node), GFP_KERNEL);
  786. if (node == NULL)
  787. return NULL;
  788. binder_stats_created(BINDER_STAT_NODE);
  789. rb_link_node(&node->rb_node, parent, p);
  790. rb_insert_color(&node->rb_node, &proc->nodes);
  791. node->debug_id = ++binder_last_id;
  792. node->proc = proc;
  793. node->ptr = ptr;
  794. node->cookie = cookie;
  795. node->work.type = BINDER_WORK_NODE;
  796. INIT_LIST_HEAD(&node->work.entry);
  797. INIT_LIST_HEAD(&node->async_todo);
  798. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  799. "%d:%d node %d u%016llx c%016llx created\n",
  800. proc->pid, current->pid, node->debug_id,
  801. (u64)node->ptr, (u64)node->cookie);
  802. return node;
  803. }
  804. static int binder_inc_node(struct binder_node *node, int strong, int internal,
  805. struct list_head *target_list)
  806. {
  807. if (strong) {
  808. if (internal) {
  809. if (target_list == NULL &&
  810. node->internal_strong_refs == 0 &&
  811. !(node == binder_context_mgr_node &&
  812. node->has_strong_ref)) {
  813. pr_err("invalid inc strong node for %d\n",
  814. node->debug_id);
  815. return -EINVAL;
  816. }
  817. node->internal_strong_refs++;
  818. } else
  819. node->local_strong_refs++;
  820. if (!node->has_strong_ref && target_list) {
  821. list_del_init(&node->work.entry);
  822. list_add_tail(&node->work.entry, target_list);
  823. }
  824. } else {
  825. if (!internal)
  826. node->local_weak_refs++;
  827. if (!node->has_weak_ref && list_empty(&node->work.entry)) {
  828. if (target_list == NULL) {
  829. pr_err("invalid inc weak node for %d\n",
  830. node->debug_id);
  831. return -EINVAL;
  832. }
  833. list_add_tail(&node->work.entry, target_list);
  834. }
  835. }
  836. return 0;
  837. }
  838. static int binder_dec_node(struct binder_node *node, int strong, int internal)
  839. {
  840. if (strong) {
  841. if (internal)
  842. node->internal_strong_refs--;
  843. else
  844. node->local_strong_refs--;
  845. if (node->local_strong_refs || node->internal_strong_refs)
  846. return 0;
  847. } else {
  848. if (!internal)
  849. node->local_weak_refs--;
  850. if (node->local_weak_refs || !hlist_empty(&node->refs))
  851. return 0;
  852. }
  853. if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
  854. if (list_empty(&node->work.entry)) {
  855. list_add_tail(&node->work.entry, &node->proc->todo);
  856. wake_up_interruptible(&node->proc->wait);
  857. }
  858. } else {
  859. if (hlist_empty(&node->refs) && !node->local_strong_refs &&
  860. !node->local_weak_refs) {
  861. list_del_init(&node->work.entry);
  862. if (node->proc) {
  863. rb_erase(&node->rb_node, &node->proc->nodes);
  864. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  865. "refless node %d deleted\n",
  866. node->debug_id);
  867. } else {
  868. hlist_del(&node->dead_node);
  869. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  870. "dead node %d deleted\n",
  871. node->debug_id);
  872. }
  873. kfree(node);
  874. binder_stats_deleted(BINDER_STAT_NODE);
  875. }
  876. }
  877. return 0;
  878. }
  879. static struct binder_ref *binder_get_ref(struct binder_proc *proc,
  880. uint32_t desc)
  881. {
  882. struct rb_node *n = proc->refs_by_desc.rb_node;
  883. struct binder_ref *ref;
  884. while (n) {
  885. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  886. if (desc < ref->desc)
  887. n = n->rb_left;
  888. else if (desc > ref->desc)
  889. n = n->rb_right;
  890. else
  891. return ref;
  892. }
  893. return NULL;
  894. }
  895. static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
  896. struct binder_node *node)
  897. {
  898. struct rb_node *n;
  899. struct rb_node **p = &proc->refs_by_node.rb_node;
  900. struct rb_node *parent = NULL;
  901. struct binder_ref *ref, *new_ref;
  902. while (*p) {
  903. parent = *p;
  904. ref = rb_entry(parent, struct binder_ref, rb_node_node);
  905. if (node < ref->node)
  906. p = &(*p)->rb_left;
  907. else if (node > ref->node)
  908. p = &(*p)->rb_right;
  909. else
  910. return ref;
  911. }
  912. new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  913. if (new_ref == NULL)
  914. return NULL;
  915. binder_stats_created(BINDER_STAT_REF);
  916. new_ref->debug_id = ++binder_last_id;
  917. new_ref->proc = proc;
  918. new_ref->node = node;
  919. rb_link_node(&new_ref->rb_node_node, parent, p);
  920. rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
  921. new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
  922. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  923. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  924. if (ref->desc > new_ref->desc)
  925. break;
  926. new_ref->desc = ref->desc + 1;
  927. }
  928. p = &proc->refs_by_desc.rb_node;
  929. while (*p) {
  930. parent = *p;
  931. ref = rb_entry(parent, struct binder_ref, rb_node_desc);
  932. if (new_ref->desc < ref->desc)
  933. p = &(*p)->rb_left;
  934. else if (new_ref->desc > ref->desc)
  935. p = &(*p)->rb_right;
  936. else
  937. BUG();
  938. }
  939. rb_link_node(&new_ref->rb_node_desc, parent, p);
  940. rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
  941. if (node) {
  942. hlist_add_head(&new_ref->node_entry, &node->refs);
  943. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  944. "%d new ref %d desc %d for node %d\n",
  945. proc->pid, new_ref->debug_id, new_ref->desc,
  946. node->debug_id);
  947. } else {
  948. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  949. "%d new ref %d desc %d for dead node\n",
  950. proc->pid, new_ref->debug_id, new_ref->desc);
  951. }
  952. return new_ref;
  953. }
  954. static void binder_delete_ref(struct binder_ref *ref)
  955. {
  956. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  957. "%d delete ref %d desc %d for node %d\n",
  958. ref->proc->pid, ref->debug_id, ref->desc,
  959. ref->node->debug_id);
  960. rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
  961. rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
  962. if (ref->strong)
  963. binder_dec_node(ref->node, 1, 1);
  964. hlist_del(&ref->node_entry);
  965. binder_dec_node(ref->node, 0, 1);
  966. if (ref->death) {
  967. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  968. "%d delete ref %d desc %d has death notification\n",
  969. ref->proc->pid, ref->debug_id, ref->desc);
  970. list_del(&ref->death->work.entry);
  971. kfree(ref->death);
  972. binder_stats_deleted(BINDER_STAT_DEATH);
  973. }
  974. kfree(ref);
  975. binder_stats_deleted(BINDER_STAT_REF);
  976. }
  977. static int binder_inc_ref(struct binder_ref *ref, int strong,
  978. struct list_head *target_list)
  979. {
  980. int ret;
  981. if (strong) {
  982. if (ref->strong == 0) {
  983. ret = binder_inc_node(ref->node, 1, 1, target_list);
  984. if (ret)
  985. return ret;
  986. }
  987. ref->strong++;
  988. } else {
  989. if (ref->weak == 0) {
  990. ret = binder_inc_node(ref->node, 0, 1, target_list);
  991. if (ret)
  992. return ret;
  993. }
  994. ref->weak++;
  995. }
  996. return 0;
  997. }
  998. static int binder_dec_ref(struct binder_ref *ref, int strong)
  999. {
  1000. if (strong) {
  1001. if (ref->strong == 0) {
  1002. binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
  1003. ref->proc->pid, ref->debug_id,
  1004. ref->desc, ref->strong, ref->weak);
  1005. return -EINVAL;
  1006. }
  1007. ref->strong--;
  1008. if (ref->strong == 0) {
  1009. int ret;
  1010. ret = binder_dec_node(ref->node, strong, 1);
  1011. if (ret)
  1012. return ret;
  1013. }
  1014. } else {
  1015. if (ref->weak == 0) {
  1016. binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
  1017. ref->proc->pid, ref->debug_id,
  1018. ref->desc, ref->strong, ref->weak);
  1019. return -EINVAL;
  1020. }
  1021. ref->weak--;
  1022. }
  1023. if (ref->strong == 0 && ref->weak == 0)
  1024. binder_delete_ref(ref);
  1025. return 0;
  1026. }
  1027. static void binder_pop_transaction(struct binder_thread *target_thread,
  1028. struct binder_transaction *t)
  1029. {
  1030. if (target_thread) {
  1031. BUG_ON(target_thread->transaction_stack != t);
  1032. BUG_ON(target_thread->transaction_stack->from != target_thread);
  1033. target_thread->transaction_stack =
  1034. target_thread->transaction_stack->from_parent;
  1035. t->from = NULL;
  1036. }
  1037. t->need_reply = 0;
  1038. if (t->buffer)
  1039. t->buffer->transaction = NULL;
  1040. kfree(t);
  1041. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  1042. }
  1043. static void binder_send_failed_reply(struct binder_transaction *t,
  1044. uint32_t error_code)
  1045. {
  1046. struct binder_thread *target_thread;
  1047. struct binder_transaction *next;
  1048. BUG_ON(t->flags & TF_ONE_WAY);
  1049. while (1) {
  1050. target_thread = t->from;
  1051. if (target_thread) {
  1052. if (target_thread->return_error != BR_OK &&
  1053. target_thread->return_error2 == BR_OK) {
  1054. target_thread->return_error2 =
  1055. target_thread->return_error;
  1056. target_thread->return_error = BR_OK;
  1057. }
  1058. if (target_thread->return_error == BR_OK) {
  1059. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1060. "send failed reply for transaction %d to %d:%d\n",
  1061. t->debug_id,
  1062. target_thread->proc->pid,
  1063. target_thread->pid);
  1064. binder_pop_transaction(target_thread, t);
  1065. target_thread->return_error = error_code;
  1066. wake_up_interruptible(&target_thread->wait);
  1067. } else {
  1068. pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
  1069. target_thread->proc->pid,
  1070. target_thread->pid,
  1071. target_thread->return_error);
  1072. }
  1073. return;
  1074. }
  1075. next = t->from_parent;
  1076. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1077. "send failed reply for transaction %d, target dead\n",
  1078. t->debug_id);
  1079. binder_pop_transaction(target_thread, t);
  1080. if (next == NULL) {
  1081. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1082. "reply failed, no target thread at root\n");
  1083. return;
  1084. }
  1085. t = next;
  1086. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1087. "reply failed, no target thread -- retry %d\n",
  1088. t->debug_id);
  1089. }
  1090. }
  1091. static void binder_transaction_buffer_release(struct binder_proc *proc,
  1092. struct binder_buffer *buffer,
  1093. binder_size_t *failed_at)
  1094. {
  1095. binder_size_t *offp, *off_end;
  1096. int debug_id = buffer->debug_id;
  1097. binder_debug(BINDER_DEBUG_TRANSACTION,
  1098. "%d buffer release %d, size %zd-%zd, failed at %p\n",
  1099. proc->pid, buffer->debug_id,
  1100. buffer->data_size, buffer->offsets_size, failed_at);
  1101. if (buffer->target_node)
  1102. binder_dec_node(buffer->target_node, 1, 0);
  1103. offp = (binder_size_t *)(buffer->data +
  1104. ALIGN(buffer->data_size, sizeof(void *)));
  1105. if (failed_at)
  1106. off_end = failed_at;
  1107. else
  1108. off_end = (void *)offp + buffer->offsets_size;
  1109. for (; offp < off_end; offp++) {
  1110. struct flat_binder_object *fp;
  1111. if (*offp > buffer->data_size - sizeof(*fp) ||
  1112. buffer->data_size < sizeof(*fp) ||
  1113. !IS_ALIGNED(*offp, sizeof(u32))) {
  1114. pr_err("transaction release %d bad offset %lld, size %zd\n",
  1115. debug_id, (u64)*offp, buffer->data_size);
  1116. continue;
  1117. }
  1118. fp = (struct flat_binder_object *)(buffer->data + *offp);
  1119. switch (fp->type) {
  1120. case BINDER_TYPE_BINDER:
  1121. case BINDER_TYPE_WEAK_BINDER: {
  1122. struct binder_node *node = binder_get_node(proc, fp->binder);
  1123. if (node == NULL) {
  1124. pr_err("transaction release %d bad node %016llx\n",
  1125. debug_id, (u64)fp->binder);
  1126. break;
  1127. }
  1128. binder_debug(BINDER_DEBUG_TRANSACTION,
  1129. " node %d u%016llx\n",
  1130. node->debug_id, (u64)node->ptr);
  1131. binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
  1132. } break;
  1133. case BINDER_TYPE_HANDLE:
  1134. case BINDER_TYPE_WEAK_HANDLE: {
  1135. struct binder_ref *ref = binder_get_ref(proc, fp->handle);
  1136. if (ref == NULL) {
  1137. pr_err("transaction release %d bad handle %d\n",
  1138. debug_id, fp->handle);
  1139. break;
  1140. }
  1141. binder_debug(BINDER_DEBUG_TRANSACTION,
  1142. " ref %d desc %d (node %d)\n",
  1143. ref->debug_id, ref->desc, ref->node->debug_id);
  1144. binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
  1145. } break;
  1146. case BINDER_TYPE_FD:
  1147. binder_debug(BINDER_DEBUG_TRANSACTION,
  1148. " fd %d\n", fp->handle);
  1149. if (failed_at)
  1150. task_close_fd(proc, fp->handle);
  1151. break;
  1152. default:
  1153. pr_err("transaction release %d bad object type %x\n",
  1154. debug_id, fp->type);
  1155. break;
  1156. }
  1157. }
  1158. }
  1159. static void binder_transaction(struct binder_proc *proc,
  1160. struct binder_thread *thread,
  1161. struct binder_transaction_data *tr, int reply)
  1162. {
  1163. struct binder_transaction *t;
  1164. struct binder_work *tcomplete;
  1165. binder_size_t *offp, *off_end;
  1166. struct binder_proc *target_proc;
  1167. struct binder_thread *target_thread = NULL;
  1168. struct binder_node *target_node = NULL;
  1169. struct list_head *target_list;
  1170. wait_queue_head_t *target_wait;
  1171. struct binder_transaction *in_reply_to = NULL;
  1172. struct binder_transaction_log_entry *e;
  1173. uint32_t return_error;
  1174. e = binder_transaction_log_add(&binder_transaction_log);
  1175. e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
  1176. e->from_proc = proc->pid;
  1177. e->from_thread = thread->pid;
  1178. e->target_handle = tr->target.handle;
  1179. e->data_size = tr->data_size;
  1180. e->offsets_size = tr->offsets_size;
  1181. if (reply) {
  1182. in_reply_to = thread->transaction_stack;
  1183. if (in_reply_to == NULL) {
  1184. binder_user_error("%d:%d got reply transaction with no transaction stack\n",
  1185. proc->pid, thread->pid);
  1186. return_error = BR_FAILED_REPLY;
  1187. goto err_empty_call_stack;
  1188. }
  1189. binder_set_nice(in_reply_to->saved_priority);
  1190. if (in_reply_to->to_thread != thread) {
  1191. binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
  1192. proc->pid, thread->pid, in_reply_to->debug_id,
  1193. in_reply_to->to_proc ?
  1194. in_reply_to->to_proc->pid : 0,
  1195. in_reply_to->to_thread ?
  1196. in_reply_to->to_thread->pid : 0);
  1197. return_error = BR_FAILED_REPLY;
  1198. in_reply_to = NULL;
  1199. goto err_bad_call_stack;
  1200. }
  1201. thread->transaction_stack = in_reply_to->to_parent;
  1202. target_thread = in_reply_to->from;
  1203. if (target_thread == NULL) {
  1204. return_error = BR_DEAD_REPLY;
  1205. goto err_dead_binder;
  1206. }
  1207. if (target_thread->transaction_stack != in_reply_to) {
  1208. binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
  1209. proc->pid, thread->pid,
  1210. target_thread->transaction_stack ?
  1211. target_thread->transaction_stack->debug_id : 0,
  1212. in_reply_to->debug_id);
  1213. return_error = BR_FAILED_REPLY;
  1214. in_reply_to = NULL;
  1215. target_thread = NULL;
  1216. goto err_dead_binder;
  1217. }
  1218. target_proc = target_thread->proc;
  1219. } else {
  1220. if (tr->target.handle) {
  1221. struct binder_ref *ref;
  1222. ref = binder_get_ref(proc, tr->target.handle);
  1223. if (ref == NULL) {
  1224. binder_user_error("%d:%d got transaction to invalid handle\n",
  1225. proc->pid, thread->pid);
  1226. return_error = BR_FAILED_REPLY;
  1227. goto err_invalid_target_handle;
  1228. }
  1229. target_node = ref->node;
  1230. } else {
  1231. target_node = binder_context_mgr_node;
  1232. if (target_node == NULL) {
  1233. return_error = BR_DEAD_REPLY;
  1234. goto err_no_context_mgr_node;
  1235. }
  1236. }
  1237. e->to_node = target_node->debug_id;
  1238. target_proc = target_node->proc;
  1239. if (target_proc == NULL) {
  1240. return_error = BR_DEAD_REPLY;
  1241. goto err_dead_binder;
  1242. }
  1243. if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
  1244. struct binder_transaction *tmp;
  1245. tmp = thread->transaction_stack;
  1246. if (tmp->to_thread != thread) {
  1247. binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
  1248. proc->pid, thread->pid, tmp->debug_id,
  1249. tmp->to_proc ? tmp->to_proc->pid : 0,
  1250. tmp->to_thread ?
  1251. tmp->to_thread->pid : 0);
  1252. return_error = BR_FAILED_REPLY;
  1253. goto err_bad_call_stack;
  1254. }
  1255. while (tmp) {
  1256. if (tmp->from && tmp->from->proc == target_proc)
  1257. target_thread = tmp->from;
  1258. tmp = tmp->from_parent;
  1259. }
  1260. }
  1261. }
  1262. if (target_thread) {
  1263. e->to_thread = target_thread->pid;
  1264. target_list = &target_thread->todo;
  1265. target_wait = &target_thread->wait;
  1266. } else {
  1267. target_list = &target_proc->todo;
  1268. target_wait = &target_proc->wait;
  1269. }
  1270. e->to_proc = target_proc->pid;
  1271. /* TODO: reuse incoming transaction for reply */
  1272. t = kzalloc(sizeof(*t), GFP_KERNEL);
  1273. if (t == NULL) {
  1274. return_error = BR_FAILED_REPLY;
  1275. goto err_alloc_t_failed;
  1276. }
  1277. binder_stats_created(BINDER_STAT_TRANSACTION);
  1278. tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
  1279. if (tcomplete == NULL) {
  1280. return_error = BR_FAILED_REPLY;
  1281. goto err_alloc_tcomplete_failed;
  1282. }
  1283. binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
  1284. t->debug_id = ++binder_last_id;
  1285. e->debug_id = t->debug_id;
  1286. if (reply)
  1287. binder_debug(BINDER_DEBUG_TRANSACTION,
  1288. "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
  1289. proc->pid, thread->pid, t->debug_id,
  1290. target_proc->pid, target_thread->pid,
  1291. (u64)tr->data.ptr.buffer,
  1292. (u64)tr->data.ptr.offsets,
  1293. (u64)tr->data_size, (u64)tr->offsets_size);
  1294. else
  1295. binder_debug(BINDER_DEBUG_TRANSACTION,
  1296. "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
  1297. proc->pid, thread->pid, t->debug_id,
  1298. target_proc->pid, target_node->debug_id,
  1299. (u64)tr->data.ptr.buffer,
  1300. (u64)tr->data.ptr.offsets,
  1301. (u64)tr->data_size, (u64)tr->offsets_size);
  1302. if (!reply && !(tr->flags & TF_ONE_WAY))
  1303. t->from = thread;
  1304. else
  1305. t->from = NULL;
  1306. t->sender_euid = task_euid(proc->tsk);
  1307. t->to_proc = target_proc;
  1308. t->to_thread = target_thread;
  1309. t->code = tr->code;
  1310. t->flags = tr->flags;
  1311. t->priority = task_nice(current);
  1312. trace_binder_transaction(reply, t, target_node);
  1313. t->buffer = binder_alloc_buf(target_proc, tr->data_size,
  1314. tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
  1315. if (t->buffer == NULL) {
  1316. return_error = BR_FAILED_REPLY;
  1317. goto err_binder_alloc_buf_failed;
  1318. }
  1319. t->buffer->allow_user_free = 0;
  1320. t->buffer->debug_id = t->debug_id;
  1321. t->buffer->transaction = t;
  1322. t->buffer->target_node = target_node;
  1323. trace_binder_transaction_alloc_buf(t->buffer);
  1324. if (target_node)
  1325. binder_inc_node(target_node, 1, 0, NULL);
  1326. offp = (binder_size_t *)(t->buffer->data +
  1327. ALIGN(tr->data_size, sizeof(void *)));
  1328. if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
  1329. tr->data.ptr.buffer, tr->data_size)) {
  1330. binder_user_error("%d:%d got transaction with invalid data ptr\n",
  1331. proc->pid, thread->pid);
  1332. return_error = BR_FAILED_REPLY;
  1333. goto err_copy_data_failed;
  1334. }
  1335. if (copy_from_user(offp, (const void __user *)(uintptr_t)
  1336. tr->data.ptr.offsets, tr->offsets_size)) {
  1337. binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
  1338. proc->pid, thread->pid);
  1339. return_error = BR_FAILED_REPLY;
  1340. goto err_copy_data_failed;
  1341. }
  1342. if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
  1343. binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
  1344. proc->pid, thread->pid, (u64)tr->offsets_size);
  1345. return_error = BR_FAILED_REPLY;
  1346. goto err_bad_offset;
  1347. }
  1348. off_end = (void *)offp + tr->offsets_size;
  1349. for (; offp < off_end; offp++) {
  1350. struct flat_binder_object *fp;
  1351. if (*offp > t->buffer->data_size - sizeof(*fp) ||
  1352. t->buffer->data_size < sizeof(*fp) ||
  1353. !IS_ALIGNED(*offp, sizeof(u32))) {
  1354. binder_user_error("%d:%d got transaction with invalid offset, %lld\n",
  1355. proc->pid, thread->pid, (u64)*offp);
  1356. return_error = BR_FAILED_REPLY;
  1357. goto err_bad_offset;
  1358. }
  1359. fp = (struct flat_binder_object *)(t->buffer->data + *offp);
  1360. switch (fp->type) {
  1361. case BINDER_TYPE_BINDER:
  1362. case BINDER_TYPE_WEAK_BINDER: {
  1363. struct binder_ref *ref;
  1364. struct binder_node *node = binder_get_node(proc, fp->binder);
  1365. if (node == NULL) {
  1366. node = binder_new_node(proc, fp->binder, fp->cookie);
  1367. if (node == NULL) {
  1368. return_error = BR_FAILED_REPLY;
  1369. goto err_binder_new_node_failed;
  1370. }
  1371. node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
  1372. node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
  1373. }
  1374. if (fp->cookie != node->cookie) {
  1375. binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
  1376. proc->pid, thread->pid,
  1377. (u64)fp->binder, node->debug_id,
  1378. (u64)fp->cookie, (u64)node->cookie);
  1379. return_error = BR_FAILED_REPLY;
  1380. goto err_binder_get_ref_for_node_failed;
  1381. }
  1382. ref = binder_get_ref_for_node(target_proc, node);
  1383. if (ref == NULL) {
  1384. return_error = BR_FAILED_REPLY;
  1385. goto err_binder_get_ref_for_node_failed;
  1386. }
  1387. if (fp->type == BINDER_TYPE_BINDER)
  1388. fp->type = BINDER_TYPE_HANDLE;
  1389. else
  1390. fp->type = BINDER_TYPE_WEAK_HANDLE;
  1391. fp->handle = ref->desc;
  1392. binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
  1393. &thread->todo);
  1394. trace_binder_transaction_node_to_ref(t, node, ref);
  1395. binder_debug(BINDER_DEBUG_TRANSACTION,
  1396. " node %d u%016llx -> ref %d desc %d\n",
  1397. node->debug_id, (u64)node->ptr,
  1398. ref->debug_id, ref->desc);
  1399. } break;
  1400. case BINDER_TYPE_HANDLE:
  1401. case BINDER_TYPE_WEAK_HANDLE: {
  1402. struct binder_ref *ref = binder_get_ref(proc, fp->handle);
  1403. if (ref == NULL) {
  1404. binder_user_error("%d:%d got transaction with invalid handle, %d\n",
  1405. proc->pid,
  1406. thread->pid, fp->handle);
  1407. return_error = BR_FAILED_REPLY;
  1408. goto err_binder_get_ref_failed;
  1409. }
  1410. if (ref->node->proc == target_proc) {
  1411. if (fp->type == BINDER_TYPE_HANDLE)
  1412. fp->type = BINDER_TYPE_BINDER;
  1413. else
  1414. fp->type = BINDER_TYPE_WEAK_BINDER;
  1415. fp->binder = ref->node->ptr;
  1416. fp->cookie = ref->node->cookie;
  1417. binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
  1418. trace_binder_transaction_ref_to_node(t, ref);
  1419. binder_debug(BINDER_DEBUG_TRANSACTION,
  1420. " ref %d desc %d -> node %d u%016llx\n",
  1421. ref->debug_id, ref->desc, ref->node->debug_id,
  1422. (u64)ref->node->ptr);
  1423. } else {
  1424. struct binder_ref *new_ref;
  1425. new_ref = binder_get_ref_for_node(target_proc, ref->node);
  1426. if (new_ref == NULL) {
  1427. return_error = BR_FAILED_REPLY;
  1428. goto err_binder_get_ref_for_node_failed;
  1429. }
  1430. fp->handle = new_ref->desc;
  1431. binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
  1432. trace_binder_transaction_ref_to_ref(t, ref,
  1433. new_ref);
  1434. binder_debug(BINDER_DEBUG_TRANSACTION,
  1435. " ref %d desc %d -> ref %d desc %d (node %d)\n",
  1436. ref->debug_id, ref->desc, new_ref->debug_id,
  1437. new_ref->desc, ref->node->debug_id);
  1438. }
  1439. } break;
  1440. case BINDER_TYPE_FD: {
  1441. int target_fd;
  1442. struct file *file;
  1443. if (reply) {
  1444. if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
  1445. binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
  1446. proc->pid, thread->pid, fp->handle);
  1447. return_error = BR_FAILED_REPLY;
  1448. goto err_fd_not_allowed;
  1449. }
  1450. } else if (!target_node->accept_fds) {
  1451. binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
  1452. proc->pid, thread->pid, fp->handle);
  1453. return_error = BR_FAILED_REPLY;
  1454. goto err_fd_not_allowed;
  1455. }
  1456. file = fget(fp->handle);
  1457. if (file == NULL) {
  1458. binder_user_error("%d:%d got transaction with invalid fd, %d\n",
  1459. proc->pid, thread->pid, fp->handle);
  1460. return_error = BR_FAILED_REPLY;
  1461. goto err_fget_failed;
  1462. }
  1463. target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
  1464. if (target_fd < 0) {
  1465. fput(file);
  1466. return_error = BR_FAILED_REPLY;
  1467. goto err_get_unused_fd_failed;
  1468. }
  1469. task_fd_install(target_proc, target_fd, file);
  1470. trace_binder_transaction_fd(t, fp->handle, target_fd);
  1471. binder_debug(BINDER_DEBUG_TRANSACTION,
  1472. " fd %d -> %d\n", fp->handle, target_fd);
  1473. /* TODO: fput? */
  1474. fp->handle = target_fd;
  1475. } break;
  1476. default:
  1477. binder_user_error("%d:%d got transaction with invalid object type, %x\n",
  1478. proc->pid, thread->pid, fp->type);
  1479. return_error = BR_FAILED_REPLY;
  1480. goto err_bad_object_type;
  1481. }
  1482. }
  1483. if (reply) {
  1484. BUG_ON(t->buffer->async_transaction != 0);
  1485. binder_pop_transaction(target_thread, in_reply_to);
  1486. } else if (!(t->flags & TF_ONE_WAY)) {
  1487. BUG_ON(t->buffer->async_transaction != 0);
  1488. t->need_reply = 1;
  1489. t->from_parent = thread->transaction_stack;
  1490. thread->transaction_stack = t;
  1491. } else {
  1492. BUG_ON(target_node == NULL);
  1493. BUG_ON(t->buffer->async_transaction != 1);
  1494. if (target_node->has_async_transaction) {
  1495. target_list = &target_node->async_todo;
  1496. target_wait = NULL;
  1497. } else
  1498. target_node->has_async_transaction = 1;
  1499. }
  1500. t->work.type = BINDER_WORK_TRANSACTION;
  1501. list_add_tail(&t->work.entry, target_list);
  1502. tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
  1503. list_add_tail(&tcomplete->entry, &thread->todo);
  1504. if (target_wait)
  1505. wake_up_interruptible(target_wait);
  1506. return;
  1507. err_get_unused_fd_failed:
  1508. err_fget_failed:
  1509. err_fd_not_allowed:
  1510. err_binder_get_ref_for_node_failed:
  1511. err_binder_get_ref_failed:
  1512. err_binder_new_node_failed:
  1513. err_bad_object_type:
  1514. err_bad_offset:
  1515. err_copy_data_failed:
  1516. trace_binder_transaction_failed_buffer_release(t->buffer);
  1517. binder_transaction_buffer_release(target_proc, t->buffer, offp);
  1518. t->buffer->transaction = NULL;
  1519. binder_free_buf(target_proc, t->buffer);
  1520. err_binder_alloc_buf_failed:
  1521. kfree(tcomplete);
  1522. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  1523. err_alloc_tcomplete_failed:
  1524. kfree(t);
  1525. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  1526. err_alloc_t_failed:
  1527. err_bad_call_stack:
  1528. err_empty_call_stack:
  1529. err_dead_binder:
  1530. err_invalid_target_handle:
  1531. err_no_context_mgr_node:
  1532. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1533. "%d:%d transaction failed %d, size %lld-%lld\n",
  1534. proc->pid, thread->pid, return_error,
  1535. (u64)tr->data_size, (u64)tr->offsets_size);
  1536. {
  1537. struct binder_transaction_log_entry *fe;
  1538. fe = binder_transaction_log_add(&binder_transaction_log_failed);
  1539. *fe = *e;
  1540. }
  1541. BUG_ON(thread->return_error != BR_OK);
  1542. if (in_reply_to) {
  1543. thread->return_error = BR_TRANSACTION_COMPLETE;
  1544. binder_send_failed_reply(in_reply_to, return_error);
  1545. } else
  1546. thread->return_error = return_error;
  1547. }
  1548. static int binder_thread_write(struct binder_proc *proc,
  1549. struct binder_thread *thread,
  1550. binder_uintptr_t binder_buffer, size_t size,
  1551. binder_size_t *consumed)
  1552. {
  1553. uint32_t cmd;
  1554. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  1555. void __user *ptr = buffer + *consumed;
  1556. void __user *end = buffer + size;
  1557. while (ptr < end && thread->return_error == BR_OK) {
  1558. if (get_user(cmd, (uint32_t __user *)ptr))
  1559. return -EFAULT;
  1560. ptr += sizeof(uint32_t);
  1561. trace_binder_command(cmd);
  1562. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
  1563. binder_stats.bc[_IOC_NR(cmd)]++;
  1564. proc->stats.bc[_IOC_NR(cmd)]++;
  1565. thread->stats.bc[_IOC_NR(cmd)]++;
  1566. }
  1567. switch (cmd) {
  1568. case BC_INCREFS:
  1569. case BC_ACQUIRE:
  1570. case BC_RELEASE:
  1571. case BC_DECREFS: {
  1572. uint32_t target;
  1573. struct binder_ref *ref;
  1574. const char *debug_string;
  1575. if (get_user(target, (uint32_t __user *)ptr))
  1576. return -EFAULT;
  1577. ptr += sizeof(uint32_t);
  1578. if (target == 0 && binder_context_mgr_node &&
  1579. (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
  1580. ref = binder_get_ref_for_node(proc,
  1581. binder_context_mgr_node);
  1582. if (ref->desc != target) {
  1583. binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
  1584. proc->pid, thread->pid,
  1585. ref->desc);
  1586. }
  1587. } else
  1588. ref = binder_get_ref(proc, target);
  1589. if (ref == NULL) {
  1590. binder_user_error("%d:%d refcount change on invalid ref %d\n",
  1591. proc->pid, thread->pid, target);
  1592. break;
  1593. }
  1594. switch (cmd) {
  1595. case BC_INCREFS:
  1596. debug_string = "IncRefs";
  1597. binder_inc_ref(ref, 0, NULL);
  1598. break;
  1599. case BC_ACQUIRE:
  1600. debug_string = "Acquire";
  1601. binder_inc_ref(ref, 1, NULL);
  1602. break;
  1603. case BC_RELEASE:
  1604. debug_string = "Release";
  1605. binder_dec_ref(ref, 1);
  1606. break;
  1607. case BC_DECREFS:
  1608. default:
  1609. debug_string = "DecRefs";
  1610. binder_dec_ref(ref, 0);
  1611. break;
  1612. }
  1613. binder_debug(BINDER_DEBUG_USER_REFS,
  1614. "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
  1615. proc->pid, thread->pid, debug_string, ref->debug_id,
  1616. ref->desc, ref->strong, ref->weak, ref->node->debug_id);
  1617. break;
  1618. }
  1619. case BC_INCREFS_DONE:
  1620. case BC_ACQUIRE_DONE: {
  1621. binder_uintptr_t node_ptr;
  1622. binder_uintptr_t cookie;
  1623. struct binder_node *node;
  1624. if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
  1625. return -EFAULT;
  1626. ptr += sizeof(binder_uintptr_t);
  1627. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1628. return -EFAULT;
  1629. ptr += sizeof(binder_uintptr_t);
  1630. node = binder_get_node(proc, node_ptr);
  1631. if (node == NULL) {
  1632. binder_user_error("%d:%d %s u%016llx no match\n",
  1633. proc->pid, thread->pid,
  1634. cmd == BC_INCREFS_DONE ?
  1635. "BC_INCREFS_DONE" :
  1636. "BC_ACQUIRE_DONE",
  1637. (u64)node_ptr);
  1638. break;
  1639. }
  1640. if (cookie != node->cookie) {
  1641. binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
  1642. proc->pid, thread->pid,
  1643. cmd == BC_INCREFS_DONE ?
  1644. "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  1645. (u64)node_ptr, node->debug_id,
  1646. (u64)cookie, (u64)node->cookie);
  1647. break;
  1648. }
  1649. if (cmd == BC_ACQUIRE_DONE) {
  1650. if (node->pending_strong_ref == 0) {
  1651. binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
  1652. proc->pid, thread->pid,
  1653. node->debug_id);
  1654. break;
  1655. }
  1656. node->pending_strong_ref = 0;
  1657. } else {
  1658. if (node->pending_weak_ref == 0) {
  1659. binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
  1660. proc->pid, thread->pid,
  1661. node->debug_id);
  1662. break;
  1663. }
  1664. node->pending_weak_ref = 0;
  1665. }
  1666. binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
  1667. binder_debug(BINDER_DEBUG_USER_REFS,
  1668. "%d:%d %s node %d ls %d lw %d\n",
  1669. proc->pid, thread->pid,
  1670. cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  1671. node->debug_id, node->local_strong_refs, node->local_weak_refs);
  1672. break;
  1673. }
  1674. case BC_ATTEMPT_ACQUIRE:
  1675. pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
  1676. return -EINVAL;
  1677. case BC_ACQUIRE_RESULT:
  1678. pr_err("BC_ACQUIRE_RESULT not supported\n");
  1679. return -EINVAL;
  1680. case BC_FREE_BUFFER: {
  1681. binder_uintptr_t data_ptr;
  1682. struct binder_buffer *buffer;
  1683. if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
  1684. return -EFAULT;
  1685. ptr += sizeof(binder_uintptr_t);
  1686. buffer = binder_buffer_lookup(proc, data_ptr);
  1687. if (buffer == NULL) {
  1688. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
  1689. proc->pid, thread->pid, (u64)data_ptr);
  1690. break;
  1691. }
  1692. if (!buffer->allow_user_free) {
  1693. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
  1694. proc->pid, thread->pid, (u64)data_ptr);
  1695. break;
  1696. }
  1697. binder_debug(BINDER_DEBUG_FREE_BUFFER,
  1698. "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
  1699. proc->pid, thread->pid, (u64)data_ptr,
  1700. buffer->debug_id,
  1701. buffer->transaction ? "active" : "finished");
  1702. if (buffer->transaction) {
  1703. buffer->transaction->buffer = NULL;
  1704. buffer->transaction = NULL;
  1705. }
  1706. if (buffer->async_transaction && buffer->target_node) {
  1707. BUG_ON(!buffer->target_node->has_async_transaction);
  1708. if (list_empty(&buffer->target_node->async_todo))
  1709. buffer->target_node->has_async_transaction = 0;
  1710. else
  1711. list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
  1712. }
  1713. trace_binder_transaction_buffer_release(buffer);
  1714. binder_transaction_buffer_release(proc, buffer, NULL);
  1715. binder_free_buf(proc, buffer);
  1716. break;
  1717. }
  1718. case BC_TRANSACTION:
  1719. case BC_REPLY: {
  1720. struct binder_transaction_data tr;
  1721. if (copy_from_user(&tr, ptr, sizeof(tr)))
  1722. return -EFAULT;
  1723. ptr += sizeof(tr);
  1724. binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
  1725. break;
  1726. }
  1727. case BC_REGISTER_LOOPER:
  1728. binder_debug(BINDER_DEBUG_THREADS,
  1729. "%d:%d BC_REGISTER_LOOPER\n",
  1730. proc->pid, thread->pid);
  1731. if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
  1732. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1733. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
  1734. proc->pid, thread->pid);
  1735. } else if (proc->requested_threads == 0) {
  1736. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1737. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
  1738. proc->pid, thread->pid);
  1739. } else {
  1740. proc->requested_threads--;
  1741. proc->requested_threads_started++;
  1742. }
  1743. thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
  1744. break;
  1745. case BC_ENTER_LOOPER:
  1746. binder_debug(BINDER_DEBUG_THREADS,
  1747. "%d:%d BC_ENTER_LOOPER\n",
  1748. proc->pid, thread->pid);
  1749. if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
  1750. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  1751. binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
  1752. proc->pid, thread->pid);
  1753. }
  1754. thread->looper |= BINDER_LOOPER_STATE_ENTERED;
  1755. break;
  1756. case BC_EXIT_LOOPER:
  1757. binder_debug(BINDER_DEBUG_THREADS,
  1758. "%d:%d BC_EXIT_LOOPER\n",
  1759. proc->pid, thread->pid);
  1760. thread->looper |= BINDER_LOOPER_STATE_EXITED;
  1761. break;
  1762. case BC_REQUEST_DEATH_NOTIFICATION:
  1763. case BC_CLEAR_DEATH_NOTIFICATION: {
  1764. uint32_t target;
  1765. binder_uintptr_t cookie;
  1766. struct binder_ref *ref;
  1767. struct binder_ref_death *death;
  1768. if (get_user(target, (uint32_t __user *)ptr))
  1769. return -EFAULT;
  1770. ptr += sizeof(uint32_t);
  1771. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1772. return -EFAULT;
  1773. ptr += sizeof(binder_uintptr_t);
  1774. ref = binder_get_ref(proc, target);
  1775. if (ref == NULL) {
  1776. binder_user_error("%d:%d %s invalid ref %d\n",
  1777. proc->pid, thread->pid,
  1778. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  1779. "BC_REQUEST_DEATH_NOTIFICATION" :
  1780. "BC_CLEAR_DEATH_NOTIFICATION",
  1781. target);
  1782. break;
  1783. }
  1784. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  1785. "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
  1786. proc->pid, thread->pid,
  1787. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  1788. "BC_REQUEST_DEATH_NOTIFICATION" :
  1789. "BC_CLEAR_DEATH_NOTIFICATION",
  1790. (u64)cookie, ref->debug_id, ref->desc,
  1791. ref->strong, ref->weak, ref->node->debug_id);
  1792. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  1793. if (ref->death) {
  1794. binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
  1795. proc->pid, thread->pid);
  1796. break;
  1797. }
  1798. death = kzalloc(sizeof(*death), GFP_KERNEL);
  1799. if (death == NULL) {
  1800. thread->return_error = BR_ERROR;
  1801. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1802. "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
  1803. proc->pid, thread->pid);
  1804. break;
  1805. }
  1806. binder_stats_created(BINDER_STAT_DEATH);
  1807. INIT_LIST_HEAD(&death->work.entry);
  1808. death->cookie = cookie;
  1809. ref->death = death;
  1810. if (ref->node->proc == NULL) {
  1811. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  1812. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1813. list_add_tail(&ref->death->work.entry, &thread->todo);
  1814. } else {
  1815. list_add_tail(&ref->death->work.entry, &proc->todo);
  1816. wake_up_interruptible(&proc->wait);
  1817. }
  1818. }
  1819. } else {
  1820. if (ref->death == NULL) {
  1821. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
  1822. proc->pid, thread->pid);
  1823. break;
  1824. }
  1825. death = ref->death;
  1826. if (death->cookie != cookie) {
  1827. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
  1828. proc->pid, thread->pid,
  1829. (u64)death->cookie,
  1830. (u64)cookie);
  1831. break;
  1832. }
  1833. ref->death = NULL;
  1834. if (list_empty(&death->work.entry)) {
  1835. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  1836. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1837. list_add_tail(&death->work.entry, &thread->todo);
  1838. } else {
  1839. list_add_tail(&death->work.entry, &proc->todo);
  1840. wake_up_interruptible(&proc->wait);
  1841. }
  1842. } else {
  1843. BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
  1844. death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
  1845. }
  1846. }
  1847. } break;
  1848. case BC_DEAD_BINDER_DONE: {
  1849. struct binder_work *w;
  1850. binder_uintptr_t cookie;
  1851. struct binder_ref_death *death = NULL;
  1852. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  1853. return -EFAULT;
  1854. ptr += sizeof(void *);
  1855. list_for_each_entry(w, &proc->delivered_death, entry) {
  1856. struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
  1857. if (tmp_death->cookie == cookie) {
  1858. death = tmp_death;
  1859. break;
  1860. }
  1861. }
  1862. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1863. "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
  1864. proc->pid, thread->pid, (u64)cookie,
  1865. death);
  1866. if (death == NULL) {
  1867. binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
  1868. proc->pid, thread->pid, (u64)cookie);
  1869. break;
  1870. }
  1871. list_del_init(&death->work.entry);
  1872. if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
  1873. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  1874. if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
  1875. list_add_tail(&death->work.entry, &thread->todo);
  1876. } else {
  1877. list_add_tail(&death->work.entry, &proc->todo);
  1878. wake_up_interruptible(&proc->wait);
  1879. }
  1880. }
  1881. } break;
  1882. default:
  1883. pr_err("%d:%d unknown command %d\n",
  1884. proc->pid, thread->pid, cmd);
  1885. return -EINVAL;
  1886. }
  1887. *consumed = ptr - buffer;
  1888. }
  1889. return 0;
  1890. }
  1891. static void binder_stat_br(struct binder_proc *proc,
  1892. struct binder_thread *thread, uint32_t cmd)
  1893. {
  1894. trace_binder_return(cmd);
  1895. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
  1896. binder_stats.br[_IOC_NR(cmd)]++;
  1897. proc->stats.br[_IOC_NR(cmd)]++;
  1898. thread->stats.br[_IOC_NR(cmd)]++;
  1899. }
  1900. }
  1901. static int binder_has_proc_work(struct binder_proc *proc,
  1902. struct binder_thread *thread)
  1903. {
  1904. return !list_empty(&proc->todo) ||
  1905. (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
  1906. }
  1907. static int binder_has_thread_work(struct binder_thread *thread)
  1908. {
  1909. return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
  1910. (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
  1911. }
  1912. static int binder_thread_read(struct binder_proc *proc,
  1913. struct binder_thread *thread,
  1914. binder_uintptr_t binder_buffer, size_t size,
  1915. binder_size_t *consumed, int non_block)
  1916. {
  1917. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  1918. void __user *ptr = buffer + *consumed;
  1919. void __user *end = buffer + size;
  1920. int ret = 0;
  1921. int wait_for_proc_work;
  1922. if (*consumed == 0) {
  1923. if (put_user(BR_NOOP, (uint32_t __user *)ptr))
  1924. return -EFAULT;
  1925. ptr += sizeof(uint32_t);
  1926. }
  1927. retry:
  1928. wait_for_proc_work = thread->transaction_stack == NULL &&
  1929. list_empty(&thread->todo);
  1930. if (thread->return_error != BR_OK && ptr < end) {
  1931. if (thread->return_error2 != BR_OK) {
  1932. if (put_user(thread->return_error2, (uint32_t __user *)ptr))
  1933. return -EFAULT;
  1934. ptr += sizeof(uint32_t);
  1935. binder_stat_br(proc, thread, thread->return_error2);
  1936. if (ptr == end)
  1937. goto done;
  1938. thread->return_error2 = BR_OK;
  1939. }
  1940. if (put_user(thread->return_error, (uint32_t __user *)ptr))
  1941. return -EFAULT;
  1942. ptr += sizeof(uint32_t);
  1943. binder_stat_br(proc, thread, thread->return_error);
  1944. thread->return_error = BR_OK;
  1945. goto done;
  1946. }
  1947. thread->looper |= BINDER_LOOPER_STATE_WAITING;
  1948. if (wait_for_proc_work)
  1949. proc->ready_threads++;
  1950. binder_unlock(__func__);
  1951. trace_binder_wait_for_work(wait_for_proc_work,
  1952. !!thread->transaction_stack,
  1953. !list_empty(&thread->todo));
  1954. if (wait_for_proc_work) {
  1955. if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  1956. BINDER_LOOPER_STATE_ENTERED))) {
  1957. binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
  1958. proc->pid, thread->pid, thread->looper);
  1959. wait_event_interruptible(binder_user_error_wait,
  1960. binder_stop_on_user_error < 2);
  1961. }
  1962. binder_set_nice(proc->default_priority);
  1963. if (non_block) {
  1964. if (!binder_has_proc_work(proc, thread))
  1965. ret = -EAGAIN;
  1966. } else
  1967. ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
  1968. } else {
  1969. if (non_block) {
  1970. if (!binder_has_thread_work(thread))
  1971. ret = -EAGAIN;
  1972. } else
  1973. ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
  1974. }
  1975. binder_lock(__func__);
  1976. if (wait_for_proc_work)
  1977. proc->ready_threads--;
  1978. thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
  1979. if (ret)
  1980. return ret;
  1981. while (1) {
  1982. uint32_t cmd;
  1983. struct binder_transaction_data tr;
  1984. struct binder_work *w;
  1985. struct binder_transaction *t = NULL;
  1986. if (!list_empty(&thread->todo)) {
  1987. w = list_first_entry(&thread->todo, struct binder_work,
  1988. entry);
  1989. } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
  1990. w = list_first_entry(&proc->todo, struct binder_work,
  1991. entry);
  1992. } else {
  1993. /* no data added */
  1994. if (ptr - buffer == 4 &&
  1995. !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
  1996. goto retry;
  1997. break;
  1998. }
  1999. if (end - ptr < sizeof(tr) + 4)
  2000. break;
  2001. switch (w->type) {
  2002. case BINDER_WORK_TRANSACTION: {
  2003. t = container_of(w, struct binder_transaction, work);
  2004. } break;
  2005. case BINDER_WORK_TRANSACTION_COMPLETE: {
  2006. cmd = BR_TRANSACTION_COMPLETE;
  2007. if (put_user(cmd, (uint32_t __user *)ptr))
  2008. return -EFAULT;
  2009. ptr += sizeof(uint32_t);
  2010. binder_stat_br(proc, thread, cmd);
  2011. binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
  2012. "%d:%d BR_TRANSACTION_COMPLETE\n",
  2013. proc->pid, thread->pid);
  2014. list_del(&w->entry);
  2015. kfree(w);
  2016. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2017. } break;
  2018. case BINDER_WORK_NODE: {
  2019. struct binder_node *node = container_of(w, struct binder_node, work);
  2020. uint32_t cmd = BR_NOOP;
  2021. const char *cmd_name;
  2022. int strong = node->internal_strong_refs || node->local_strong_refs;
  2023. int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
  2024. if (weak && !node->has_weak_ref) {
  2025. cmd = BR_INCREFS;
  2026. cmd_name = "BR_INCREFS";
  2027. node->has_weak_ref = 1;
  2028. node->pending_weak_ref = 1;
  2029. node->local_weak_refs++;
  2030. } else if (strong && !node->has_strong_ref) {
  2031. cmd = BR_ACQUIRE;
  2032. cmd_name = "BR_ACQUIRE";
  2033. node->has_strong_ref = 1;
  2034. node->pending_strong_ref = 1;
  2035. node->local_strong_refs++;
  2036. } else if (!strong && node->has_strong_ref) {
  2037. cmd = BR_RELEASE;
  2038. cmd_name = "BR_RELEASE";
  2039. node->has_strong_ref = 0;
  2040. } else if (!weak && node->has_weak_ref) {
  2041. cmd = BR_DECREFS;
  2042. cmd_name = "BR_DECREFS";
  2043. node->has_weak_ref = 0;
  2044. }
  2045. if (cmd != BR_NOOP) {
  2046. if (put_user(cmd, (uint32_t __user *)ptr))
  2047. return -EFAULT;
  2048. ptr += sizeof(uint32_t);
  2049. if (put_user(node->ptr,
  2050. (binder_uintptr_t __user *)ptr))
  2051. return -EFAULT;
  2052. ptr += sizeof(binder_uintptr_t);
  2053. if (put_user(node->cookie,
  2054. (binder_uintptr_t __user *)ptr))
  2055. return -EFAULT;
  2056. ptr += sizeof(binder_uintptr_t);
  2057. binder_stat_br(proc, thread, cmd);
  2058. binder_debug(BINDER_DEBUG_USER_REFS,
  2059. "%d:%d %s %d u%016llx c%016llx\n",
  2060. proc->pid, thread->pid, cmd_name,
  2061. node->debug_id,
  2062. (u64)node->ptr, (u64)node->cookie);
  2063. } else {
  2064. list_del_init(&w->entry);
  2065. if (!weak && !strong) {
  2066. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  2067. "%d:%d node %d u%016llx c%016llx deleted\n",
  2068. proc->pid, thread->pid,
  2069. node->debug_id,
  2070. (u64)node->ptr,
  2071. (u64)node->cookie);
  2072. rb_erase(&node->rb_node, &proc->nodes);
  2073. kfree(node);
  2074. binder_stats_deleted(BINDER_STAT_NODE);
  2075. } else {
  2076. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  2077. "%d:%d node %d u%016llx c%016llx state unchanged\n",
  2078. proc->pid, thread->pid,
  2079. node->debug_id,
  2080. (u64)node->ptr,
  2081. (u64)node->cookie);
  2082. }
  2083. }
  2084. } break;
  2085. case BINDER_WORK_DEAD_BINDER:
  2086. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2087. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  2088. struct binder_ref_death *death;
  2089. uint32_t cmd;
  2090. death = container_of(w, struct binder_ref_death, work);
  2091. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
  2092. cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
  2093. else
  2094. cmd = BR_DEAD_BINDER;
  2095. if (put_user(cmd, (uint32_t __user *)ptr))
  2096. return -EFAULT;
  2097. ptr += sizeof(uint32_t);
  2098. if (put_user(death->cookie,
  2099. (binder_uintptr_t __user *)ptr))
  2100. return -EFAULT;
  2101. ptr += sizeof(binder_uintptr_t);
  2102. binder_stat_br(proc, thread, cmd);
  2103. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  2104. "%d:%d %s %016llx\n",
  2105. proc->pid, thread->pid,
  2106. cmd == BR_DEAD_BINDER ?
  2107. "BR_DEAD_BINDER" :
  2108. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  2109. (u64)death->cookie);
  2110. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
  2111. list_del(&w->entry);
  2112. kfree(death);
  2113. binder_stats_deleted(BINDER_STAT_DEATH);
  2114. } else
  2115. list_move(&w->entry, &proc->delivered_death);
  2116. if (cmd == BR_DEAD_BINDER)
  2117. goto done; /* DEAD_BINDER notifications can cause transactions */
  2118. } break;
  2119. }
  2120. if (!t)
  2121. continue;
  2122. BUG_ON(t->buffer == NULL);
  2123. if (t->buffer->target_node) {
  2124. struct binder_node *target_node = t->buffer->target_node;
  2125. tr.target.ptr = target_node->ptr;
  2126. tr.cookie = target_node->cookie;
  2127. t->saved_priority = task_nice(current);
  2128. if (t->priority < target_node->min_priority &&
  2129. !(t->flags & TF_ONE_WAY))
  2130. binder_set_nice(t->priority);
  2131. else if (!(t->flags & TF_ONE_WAY) ||
  2132. t->saved_priority > target_node->min_priority)
  2133. binder_set_nice(target_node->min_priority);
  2134. cmd = BR_TRANSACTION;
  2135. } else {
  2136. tr.target.ptr = 0;
  2137. tr.cookie = 0;
  2138. cmd = BR_REPLY;
  2139. }
  2140. tr.code = t->code;
  2141. tr.flags = t->flags;
  2142. tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
  2143. if (t->from) {
  2144. struct task_struct *sender = t->from->proc->tsk;
  2145. tr.sender_pid = task_tgid_nr_ns(sender,
  2146. task_active_pid_ns(current));
  2147. } else {
  2148. tr.sender_pid = 0;
  2149. }
  2150. tr.data_size = t->buffer->data_size;
  2151. tr.offsets_size = t->buffer->offsets_size;
  2152. tr.data.ptr.buffer = (binder_uintptr_t)(
  2153. (uintptr_t)t->buffer->data +
  2154. proc->user_buffer_offset);
  2155. tr.data.ptr.offsets = tr.data.ptr.buffer +
  2156. ALIGN(t->buffer->data_size,
  2157. sizeof(void *));
  2158. if (put_user(cmd, (uint32_t __user *)ptr))
  2159. return -EFAULT;
  2160. ptr += sizeof(uint32_t);
  2161. if (copy_to_user(ptr, &tr, sizeof(tr)))
  2162. return -EFAULT;
  2163. ptr += sizeof(tr);
  2164. trace_binder_transaction_received(t);
  2165. binder_stat_br(proc, thread, cmd);
  2166. binder_debug(BINDER_DEBUG_TRANSACTION,
  2167. "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
  2168. proc->pid, thread->pid,
  2169. (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
  2170. "BR_REPLY",
  2171. t->debug_id, t->from ? t->from->proc->pid : 0,
  2172. t->from ? t->from->pid : 0, cmd,
  2173. t->buffer->data_size, t->buffer->offsets_size,
  2174. (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
  2175. list_del(&t->work.entry);
  2176. t->buffer->allow_user_free = 1;
  2177. if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
  2178. t->to_parent = thread->transaction_stack;
  2179. t->to_thread = thread;
  2180. thread->transaction_stack = t;
  2181. } else {
  2182. t->buffer->transaction = NULL;
  2183. kfree(t);
  2184. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2185. }
  2186. break;
  2187. }
  2188. done:
  2189. *consumed = ptr - buffer;
  2190. if (proc->requested_threads + proc->ready_threads == 0 &&
  2191. proc->requested_threads_started < proc->max_threads &&
  2192. (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  2193. BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
  2194. /*spawn a new thread if we leave this out */) {
  2195. proc->requested_threads++;
  2196. binder_debug(BINDER_DEBUG_THREADS,
  2197. "%d:%d BR_SPAWN_LOOPER\n",
  2198. proc->pid, thread->pid);
  2199. if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
  2200. return -EFAULT;
  2201. binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
  2202. }
  2203. return 0;
  2204. }
  2205. static void binder_release_work(struct list_head *list)
  2206. {
  2207. struct binder_work *w;
  2208. while (!list_empty(list)) {
  2209. w = list_first_entry(list, struct binder_work, entry);
  2210. list_del_init(&w->entry);
  2211. switch (w->type) {
  2212. case BINDER_WORK_TRANSACTION: {
  2213. struct binder_transaction *t;
  2214. t = container_of(w, struct binder_transaction, work);
  2215. if (t->buffer->target_node &&
  2216. !(t->flags & TF_ONE_WAY)) {
  2217. binder_send_failed_reply(t, BR_DEAD_REPLY);
  2218. } else {
  2219. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2220. "undelivered transaction %d\n",
  2221. t->debug_id);
  2222. t->buffer->transaction = NULL;
  2223. kfree(t);
  2224. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2225. }
  2226. } break;
  2227. case BINDER_WORK_TRANSACTION_COMPLETE: {
  2228. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2229. "undelivered TRANSACTION_COMPLETE\n");
  2230. kfree(w);
  2231. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2232. } break;
  2233. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2234. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  2235. struct binder_ref_death *death;
  2236. death = container_of(w, struct binder_ref_death, work);
  2237. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2238. "undelivered death notification, %016llx\n",
  2239. (u64)death->cookie);
  2240. kfree(death);
  2241. binder_stats_deleted(BINDER_STAT_DEATH);
  2242. } break;
  2243. default:
  2244. pr_err("unexpected work type, %d, not freed\n",
  2245. w->type);
  2246. break;
  2247. }
  2248. }
  2249. }
  2250. static struct binder_thread *binder_get_thread(struct binder_proc *proc)
  2251. {
  2252. struct binder_thread *thread = NULL;
  2253. struct rb_node *parent = NULL;
  2254. struct rb_node **p = &proc->threads.rb_node;
  2255. while (*p) {
  2256. parent = *p;
  2257. thread = rb_entry(parent, struct binder_thread, rb_node);
  2258. if (current->pid < thread->pid)
  2259. p = &(*p)->rb_left;
  2260. else if (current->pid > thread->pid)
  2261. p = &(*p)->rb_right;
  2262. else
  2263. break;
  2264. }
  2265. if (*p == NULL) {
  2266. thread = kzalloc(sizeof(*thread), GFP_KERNEL);
  2267. if (thread == NULL)
  2268. return NULL;
  2269. binder_stats_created(BINDER_STAT_THREAD);
  2270. thread->proc = proc;
  2271. thread->pid = current->pid;
  2272. init_waitqueue_head(&thread->wait);
  2273. INIT_LIST_HEAD(&thread->todo);
  2274. rb_link_node(&thread->rb_node, parent, p);
  2275. rb_insert_color(&thread->rb_node, &proc->threads);
  2276. thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
  2277. thread->return_error = BR_OK;
  2278. thread->return_error2 = BR_OK;
  2279. }
  2280. return thread;
  2281. }
  2282. static int binder_free_thread(struct binder_proc *proc,
  2283. struct binder_thread *thread)
  2284. {
  2285. struct binder_transaction *t;
  2286. struct binder_transaction *send_reply = NULL;
  2287. int active_transactions = 0;
  2288. rb_erase(&thread->rb_node, &proc->threads);
  2289. t = thread->transaction_stack;
  2290. if (t && t->to_thread == thread)
  2291. send_reply = t;
  2292. while (t) {
  2293. active_transactions++;
  2294. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  2295. "release %d:%d transaction %d %s, still active\n",
  2296. proc->pid, thread->pid,
  2297. t->debug_id,
  2298. (t->to_thread == thread) ? "in" : "out");
  2299. if (t->to_thread == thread) {
  2300. t->to_proc = NULL;
  2301. t->to_thread = NULL;
  2302. if (t->buffer) {
  2303. t->buffer->transaction = NULL;
  2304. t->buffer = NULL;
  2305. }
  2306. t = t->to_parent;
  2307. } else if (t->from == thread) {
  2308. t->from = NULL;
  2309. t = t->from_parent;
  2310. } else
  2311. BUG();
  2312. }
  2313. if (send_reply)
  2314. binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
  2315. binder_release_work(&thread->todo);
  2316. kfree(thread);
  2317. binder_stats_deleted(BINDER_STAT_THREAD);
  2318. return active_transactions;
  2319. }
  2320. static unsigned int binder_poll(struct file *filp,
  2321. struct poll_table_struct *wait)
  2322. {
  2323. struct binder_proc *proc = filp->private_data;
  2324. struct binder_thread *thread = NULL;
  2325. int wait_for_proc_work;
  2326. binder_lock(__func__);
  2327. thread = binder_get_thread(proc);
  2328. wait_for_proc_work = thread->transaction_stack == NULL &&
  2329. list_empty(&thread->todo) && thread->return_error == BR_OK;
  2330. binder_unlock(__func__);
  2331. if (wait_for_proc_work) {
  2332. if (binder_has_proc_work(proc, thread))
  2333. return POLLIN;
  2334. poll_wait(filp, &proc->wait, wait);
  2335. if (binder_has_proc_work(proc, thread))
  2336. return POLLIN;
  2337. } else {
  2338. if (binder_has_thread_work(thread))
  2339. return POLLIN;
  2340. poll_wait(filp, &thread->wait, wait);
  2341. if (binder_has_thread_work(thread))
  2342. return POLLIN;
  2343. }
  2344. return 0;
  2345. }
  2346. static int binder_ioctl_write_read(struct file *filp,
  2347. unsigned int cmd, unsigned long arg,
  2348. struct binder_thread *thread)
  2349. {
  2350. int ret = 0;
  2351. struct binder_proc *proc = filp->private_data;
  2352. unsigned int size = _IOC_SIZE(cmd);
  2353. void __user *ubuf = (void __user *)arg;
  2354. struct binder_write_read bwr;
  2355. if (size != sizeof(struct binder_write_read)) {
  2356. ret = -EINVAL;
  2357. goto out;
  2358. }
  2359. if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
  2360. ret = -EFAULT;
  2361. goto out;
  2362. }
  2363. binder_debug(BINDER_DEBUG_READ_WRITE,
  2364. "%d:%d write %lld at %016llx, read %lld at %016llx\n",
  2365. proc->pid, thread->pid,
  2366. (u64)bwr.write_size, (u64)bwr.write_buffer,
  2367. (u64)bwr.read_size, (u64)bwr.read_buffer);
  2368. if (bwr.write_size > 0) {
  2369. ret = binder_thread_write(proc, thread,
  2370. bwr.write_buffer,
  2371. bwr.write_size,
  2372. &bwr.write_consumed);
  2373. trace_binder_write_done(ret);
  2374. if (ret < 0) {
  2375. bwr.read_consumed = 0;
  2376. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  2377. ret = -EFAULT;
  2378. goto out;
  2379. }
  2380. }
  2381. if (bwr.read_size > 0) {
  2382. ret = binder_thread_read(proc, thread, bwr.read_buffer,
  2383. bwr.read_size,
  2384. &bwr.read_consumed,
  2385. filp->f_flags & O_NONBLOCK);
  2386. trace_binder_read_done(ret);
  2387. if (!list_empty(&proc->todo))
  2388. wake_up_interruptible(&proc->wait);
  2389. if (ret < 0) {
  2390. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  2391. ret = -EFAULT;
  2392. goto out;
  2393. }
  2394. }
  2395. binder_debug(BINDER_DEBUG_READ_WRITE,
  2396. "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
  2397. proc->pid, thread->pid,
  2398. (u64)bwr.write_consumed, (u64)bwr.write_size,
  2399. (u64)bwr.read_consumed, (u64)bwr.read_size);
  2400. if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
  2401. ret = -EFAULT;
  2402. goto out;
  2403. }
  2404. out:
  2405. return ret;
  2406. }
  2407. static int binder_ioctl_set_ctx_mgr(struct file *filp)
  2408. {
  2409. int ret = 0;
  2410. struct binder_proc *proc = filp->private_data;
  2411. kuid_t curr_euid = current_euid();
  2412. if (binder_context_mgr_node != NULL) {
  2413. pr_err("BINDER_SET_CONTEXT_MGR already set\n");
  2414. ret = -EBUSY;
  2415. goto out;
  2416. }
  2417. if (uid_valid(binder_context_mgr_uid)) {
  2418. if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
  2419. pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
  2420. from_kuid(&init_user_ns, curr_euid),
  2421. from_kuid(&init_user_ns,
  2422. binder_context_mgr_uid));
  2423. ret = -EPERM;
  2424. goto out;
  2425. }
  2426. } else {
  2427. binder_context_mgr_uid = curr_euid;
  2428. }
  2429. binder_context_mgr_node = binder_new_node(proc, 0, 0);
  2430. if (binder_context_mgr_node == NULL) {
  2431. ret = -ENOMEM;
  2432. goto out;
  2433. }
  2434. binder_context_mgr_node->local_weak_refs++;
  2435. binder_context_mgr_node->local_strong_refs++;
  2436. binder_context_mgr_node->has_strong_ref = 1;
  2437. binder_context_mgr_node->has_weak_ref = 1;
  2438. out:
  2439. return ret;
  2440. }
  2441. static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  2442. {
  2443. int ret;
  2444. struct binder_proc *proc = filp->private_data;
  2445. struct binder_thread *thread;
  2446. unsigned int size = _IOC_SIZE(cmd);
  2447. void __user *ubuf = (void __user *)arg;
  2448. /*pr_info("binder_ioctl: %d:%d %x %lx\n",
  2449. proc->pid, current->pid, cmd, arg);*/
  2450. trace_binder_ioctl(cmd, arg);
  2451. ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  2452. if (ret)
  2453. goto err_unlocked;
  2454. binder_lock(__func__);
  2455. thread = binder_get_thread(proc);
  2456. if (thread == NULL) {
  2457. ret = -ENOMEM;
  2458. goto err;
  2459. }
  2460. switch (cmd) {
  2461. case BINDER_WRITE_READ:
  2462. ret = binder_ioctl_write_read(filp, cmd, arg, thread);
  2463. if (ret)
  2464. goto err;
  2465. break;
  2466. case BINDER_SET_MAX_THREADS:
  2467. if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
  2468. ret = -EINVAL;
  2469. goto err;
  2470. }
  2471. break;
  2472. case BINDER_SET_CONTEXT_MGR:
  2473. ret = binder_ioctl_set_ctx_mgr(filp);
  2474. if (ret)
  2475. goto err;
  2476. break;
  2477. case BINDER_THREAD_EXIT:
  2478. binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
  2479. proc->pid, thread->pid);
  2480. binder_free_thread(proc, thread);
  2481. thread = NULL;
  2482. break;
  2483. case BINDER_VERSION: {
  2484. struct binder_version __user *ver = ubuf;
  2485. if (size != sizeof(struct binder_version)) {
  2486. ret = -EINVAL;
  2487. goto err;
  2488. }
  2489. if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
  2490. &ver->protocol_version)) {
  2491. ret = -EINVAL;
  2492. goto err;
  2493. }
  2494. break;
  2495. }
  2496. default:
  2497. ret = -EINVAL;
  2498. goto err;
  2499. }
  2500. ret = 0;
  2501. err:
  2502. if (thread)
  2503. thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
  2504. binder_unlock(__func__);
  2505. wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  2506. if (ret && ret != -ERESTARTSYS)
  2507. pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
  2508. err_unlocked:
  2509. trace_binder_ioctl_done(ret);
  2510. return ret;
  2511. }
  2512. static void binder_vma_open(struct vm_area_struct *vma)
  2513. {
  2514. struct binder_proc *proc = vma->vm_private_data;
  2515. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2516. "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2517. proc->pid, vma->vm_start, vma->vm_end,
  2518. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2519. (unsigned long)pgprot_val(vma->vm_page_prot));
  2520. }
  2521. static void binder_vma_close(struct vm_area_struct *vma)
  2522. {
  2523. struct binder_proc *proc = vma->vm_private_data;
  2524. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2525. "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2526. proc->pid, vma->vm_start, vma->vm_end,
  2527. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2528. (unsigned long)pgprot_val(vma->vm_page_prot));
  2529. proc->vma = NULL;
  2530. proc->vma_vm_mm = NULL;
  2531. binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
  2532. }
  2533. static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2534. {
  2535. return VM_FAULT_SIGBUS;
  2536. }
  2537. static struct vm_operations_struct binder_vm_ops = {
  2538. .open = binder_vma_open,
  2539. .close = binder_vma_close,
  2540. .fault = binder_vm_fault,
  2541. };
  2542. static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
  2543. {
  2544. int ret;
  2545. struct vm_struct *area;
  2546. struct binder_proc *proc = filp->private_data;
  2547. const char *failure_string;
  2548. struct binder_buffer *buffer;
  2549. if (proc->tsk != current)
  2550. return -EINVAL;
  2551. if ((vma->vm_end - vma->vm_start) > SZ_4M)
  2552. vma->vm_end = vma->vm_start + SZ_4M;
  2553. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2554. "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
  2555. proc->pid, vma->vm_start, vma->vm_end,
  2556. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  2557. (unsigned long)pgprot_val(vma->vm_page_prot));
  2558. if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
  2559. ret = -EPERM;
  2560. failure_string = "bad vm_flags";
  2561. goto err_bad_arg;
  2562. }
  2563. vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
  2564. mutex_lock(&binder_mmap_lock);
  2565. if (proc->buffer) {
  2566. ret = -EBUSY;
  2567. failure_string = "already mapped";
  2568. goto err_already_mapped;
  2569. }
  2570. area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
  2571. if (area == NULL) {
  2572. ret = -ENOMEM;
  2573. failure_string = "get_vm_area";
  2574. goto err_get_vm_area_failed;
  2575. }
  2576. proc->buffer = area->addr;
  2577. proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
  2578. mutex_unlock(&binder_mmap_lock);
  2579. #ifdef CONFIG_CPU_CACHE_VIPT
  2580. if (cache_is_vipt_aliasing()) {
  2581. while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
  2582. pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
  2583. vma->vm_start += PAGE_SIZE;
  2584. }
  2585. }
  2586. #endif
  2587. proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
  2588. if (proc->pages == NULL) {
  2589. ret = -ENOMEM;
  2590. failure_string = "alloc page array";
  2591. goto err_alloc_pages_failed;
  2592. }
  2593. proc->buffer_size = vma->vm_end - vma->vm_start;
  2594. vma->vm_ops = &binder_vm_ops;
  2595. vma->vm_private_data = proc;
  2596. if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
  2597. ret = -ENOMEM;
  2598. failure_string = "alloc small buf";
  2599. goto err_alloc_small_buf_failed;
  2600. }
  2601. buffer = proc->buffer;
  2602. INIT_LIST_HEAD(&proc->buffers);
  2603. list_add(&buffer->entry, &proc->buffers);
  2604. buffer->free = 1;
  2605. binder_insert_free_buffer(proc, buffer);
  2606. proc->free_async_space = proc->buffer_size / 2;
  2607. barrier();
  2608. proc->files = get_files_struct(current);
  2609. proc->vma = vma;
  2610. proc->vma_vm_mm = vma->vm_mm;
  2611. /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
  2612. proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
  2613. return 0;
  2614. err_alloc_small_buf_failed:
  2615. kfree(proc->pages);
  2616. proc->pages = NULL;
  2617. err_alloc_pages_failed:
  2618. mutex_lock(&binder_mmap_lock);
  2619. vfree(proc->buffer);
  2620. proc->buffer = NULL;
  2621. err_get_vm_area_failed:
  2622. err_already_mapped:
  2623. mutex_unlock(&binder_mmap_lock);
  2624. err_bad_arg:
  2625. pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
  2626. proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
  2627. return ret;
  2628. }
  2629. static int binder_open(struct inode *nodp, struct file *filp)
  2630. {
  2631. struct binder_proc *proc;
  2632. binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
  2633. current->group_leader->pid, current->pid);
  2634. proc = kzalloc(sizeof(*proc), GFP_KERNEL);
  2635. if (proc == NULL)
  2636. return -ENOMEM;
  2637. get_task_struct(current);
  2638. proc->tsk = current;
  2639. INIT_LIST_HEAD(&proc->todo);
  2640. init_waitqueue_head(&proc->wait);
  2641. proc->default_priority = task_nice(current);
  2642. binder_lock(__func__);
  2643. binder_stats_created(BINDER_STAT_PROC);
  2644. hlist_add_head(&proc->proc_node, &binder_procs);
  2645. proc->pid = current->group_leader->pid;
  2646. INIT_LIST_HEAD(&proc->delivered_death);
  2647. filp->private_data = proc;
  2648. binder_unlock(__func__);
  2649. if (binder_debugfs_dir_entry_proc) {
  2650. char strbuf[11];
  2651. snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
  2652. proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
  2653. binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
  2654. }
  2655. return 0;
  2656. }
  2657. static int binder_flush(struct file *filp, fl_owner_t id)
  2658. {
  2659. struct binder_proc *proc = filp->private_data;
  2660. binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
  2661. return 0;
  2662. }
  2663. static void binder_deferred_flush(struct binder_proc *proc)
  2664. {
  2665. struct rb_node *n;
  2666. int wake_count = 0;
  2667. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
  2668. struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
  2669. thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
  2670. if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
  2671. wake_up_interruptible(&thread->wait);
  2672. wake_count++;
  2673. }
  2674. }
  2675. wake_up_interruptible_all(&proc->wait);
  2676. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2677. "binder_flush: %d woke %d threads\n", proc->pid,
  2678. wake_count);
  2679. }
  2680. static int binder_release(struct inode *nodp, struct file *filp)
  2681. {
  2682. struct binder_proc *proc = filp->private_data;
  2683. debugfs_remove(proc->debugfs_entry);
  2684. binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
  2685. return 0;
  2686. }
  2687. static int binder_node_release(struct binder_node *node, int refs)
  2688. {
  2689. struct binder_ref *ref;
  2690. int death = 0;
  2691. list_del_init(&node->work.entry);
  2692. binder_release_work(&node->async_todo);
  2693. if (hlist_empty(&node->refs)) {
  2694. kfree(node);
  2695. binder_stats_deleted(BINDER_STAT_NODE);
  2696. return refs;
  2697. }
  2698. node->proc = NULL;
  2699. node->local_strong_refs = 0;
  2700. node->local_weak_refs = 0;
  2701. hlist_add_head(&node->dead_node, &binder_dead_nodes);
  2702. hlist_for_each_entry(ref, &node->refs, node_entry) {
  2703. refs++;
  2704. if (!ref->death)
  2705. continue;
  2706. death++;
  2707. if (list_empty(&ref->death->work.entry)) {
  2708. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  2709. list_add_tail(&ref->death->work.entry,
  2710. &ref->proc->todo);
  2711. wake_up_interruptible(&ref->proc->wait);
  2712. } else
  2713. BUG();
  2714. }
  2715. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  2716. "node %d now dead, refs %d, death %d\n",
  2717. node->debug_id, refs, death);
  2718. return refs;
  2719. }
  2720. static void binder_deferred_release(struct binder_proc *proc)
  2721. {
  2722. struct binder_transaction *t;
  2723. struct rb_node *n;
  2724. int threads, nodes, incoming_refs, outgoing_refs, buffers,
  2725. active_transactions, page_count;
  2726. BUG_ON(proc->vma);
  2727. BUG_ON(proc->files);
  2728. hlist_del(&proc->proc_node);
  2729. if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
  2730. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  2731. "%s: %d context_mgr_node gone\n",
  2732. __func__, proc->pid);
  2733. binder_context_mgr_node = NULL;
  2734. }
  2735. threads = 0;
  2736. active_transactions = 0;
  2737. while ((n = rb_first(&proc->threads))) {
  2738. struct binder_thread *thread;
  2739. thread = rb_entry(n, struct binder_thread, rb_node);
  2740. threads++;
  2741. active_transactions += binder_free_thread(proc, thread);
  2742. }
  2743. nodes = 0;
  2744. incoming_refs = 0;
  2745. while ((n = rb_first(&proc->nodes))) {
  2746. struct binder_node *node;
  2747. node = rb_entry(n, struct binder_node, rb_node);
  2748. nodes++;
  2749. rb_erase(&node->rb_node, &proc->nodes);
  2750. incoming_refs = binder_node_release(node, incoming_refs);
  2751. }
  2752. outgoing_refs = 0;
  2753. while ((n = rb_first(&proc->refs_by_desc))) {
  2754. struct binder_ref *ref;
  2755. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  2756. outgoing_refs++;
  2757. binder_delete_ref(ref);
  2758. }
  2759. binder_release_work(&proc->todo);
  2760. binder_release_work(&proc->delivered_death);
  2761. buffers = 0;
  2762. while ((n = rb_first(&proc->allocated_buffers))) {
  2763. struct binder_buffer *buffer;
  2764. buffer = rb_entry(n, struct binder_buffer, rb_node);
  2765. t = buffer->transaction;
  2766. if (t) {
  2767. t->buffer = NULL;
  2768. buffer->transaction = NULL;
  2769. pr_err("release proc %d, transaction %d, not freed\n",
  2770. proc->pid, t->debug_id);
  2771. /*BUG();*/
  2772. }
  2773. binder_free_buf(proc, buffer);
  2774. buffers++;
  2775. }
  2776. binder_stats_deleted(BINDER_STAT_PROC);
  2777. page_count = 0;
  2778. if (proc->pages) {
  2779. int i;
  2780. for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
  2781. void *page_addr;
  2782. if (!proc->pages[i])
  2783. continue;
  2784. page_addr = proc->buffer + i * PAGE_SIZE;
  2785. binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
  2786. "%s: %d: page %d at %p not freed\n",
  2787. __func__, proc->pid, i, page_addr);
  2788. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  2789. __free_page(proc->pages[i]);
  2790. page_count++;
  2791. }
  2792. kfree(proc->pages);
  2793. vfree(proc->buffer);
  2794. }
  2795. put_task_struct(proc->tsk);
  2796. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  2797. "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
  2798. __func__, proc->pid, threads, nodes, incoming_refs,
  2799. outgoing_refs, active_transactions, buffers, page_count);
  2800. kfree(proc);
  2801. }
  2802. static void binder_deferred_func(struct work_struct *work)
  2803. {
  2804. struct binder_proc *proc;
  2805. struct files_struct *files;
  2806. int defer;
  2807. do {
  2808. binder_lock(__func__);
  2809. mutex_lock(&binder_deferred_lock);
  2810. if (!hlist_empty(&binder_deferred_list)) {
  2811. proc = hlist_entry(binder_deferred_list.first,
  2812. struct binder_proc, deferred_work_node);
  2813. hlist_del_init(&proc->deferred_work_node);
  2814. defer = proc->deferred_work;
  2815. proc->deferred_work = 0;
  2816. } else {
  2817. proc = NULL;
  2818. defer = 0;
  2819. }
  2820. mutex_unlock(&binder_deferred_lock);
  2821. files = NULL;
  2822. if (defer & BINDER_DEFERRED_PUT_FILES) {
  2823. files = proc->files;
  2824. if (files)
  2825. proc->files = NULL;
  2826. }
  2827. if (defer & BINDER_DEFERRED_FLUSH)
  2828. binder_deferred_flush(proc);
  2829. if (defer & BINDER_DEFERRED_RELEASE)
  2830. binder_deferred_release(proc); /* frees proc */
  2831. binder_unlock(__func__);
  2832. if (files)
  2833. put_files_struct(files);
  2834. } while (proc);
  2835. }
  2836. static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
  2837. static void
  2838. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
  2839. {
  2840. mutex_lock(&binder_deferred_lock);
  2841. proc->deferred_work |= defer;
  2842. if (hlist_unhashed(&proc->deferred_work_node)) {
  2843. hlist_add_head(&proc->deferred_work_node,
  2844. &binder_deferred_list);
  2845. queue_work(binder_deferred_workqueue, &binder_deferred_work);
  2846. }
  2847. mutex_unlock(&binder_deferred_lock);
  2848. }
  2849. static void print_binder_transaction(struct seq_file *m, const char *prefix,
  2850. struct binder_transaction *t)
  2851. {
  2852. seq_printf(m,
  2853. "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
  2854. prefix, t->debug_id, t,
  2855. t->from ? t->from->proc->pid : 0,
  2856. t->from ? t->from->pid : 0,
  2857. t->to_proc ? t->to_proc->pid : 0,
  2858. t->to_thread ? t->to_thread->pid : 0,
  2859. t->code, t->flags, t->priority, t->need_reply);
  2860. if (t->buffer == NULL) {
  2861. seq_puts(m, " buffer free\n");
  2862. return;
  2863. }
  2864. if (t->buffer->target_node)
  2865. seq_printf(m, " node %d",
  2866. t->buffer->target_node->debug_id);
  2867. seq_printf(m, " size %zd:%zd data %p\n",
  2868. t->buffer->data_size, t->buffer->offsets_size,
  2869. t->buffer->data);
  2870. }
  2871. static void print_binder_buffer(struct seq_file *m, const char *prefix,
  2872. struct binder_buffer *buffer)
  2873. {
  2874. seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
  2875. prefix, buffer->debug_id, buffer->data,
  2876. buffer->data_size, buffer->offsets_size,
  2877. buffer->transaction ? "active" : "delivered");
  2878. }
  2879. static void print_binder_work(struct seq_file *m, const char *prefix,
  2880. const char *transaction_prefix,
  2881. struct binder_work *w)
  2882. {
  2883. struct binder_node *node;
  2884. struct binder_transaction *t;
  2885. switch (w->type) {
  2886. case BINDER_WORK_TRANSACTION:
  2887. t = container_of(w, struct binder_transaction, work);
  2888. print_binder_transaction(m, transaction_prefix, t);
  2889. break;
  2890. case BINDER_WORK_TRANSACTION_COMPLETE:
  2891. seq_printf(m, "%stransaction complete\n", prefix);
  2892. break;
  2893. case BINDER_WORK_NODE:
  2894. node = container_of(w, struct binder_node, work);
  2895. seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
  2896. prefix, node->debug_id,
  2897. (u64)node->ptr, (u64)node->cookie);
  2898. break;
  2899. case BINDER_WORK_DEAD_BINDER:
  2900. seq_printf(m, "%shas dead binder\n", prefix);
  2901. break;
  2902. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  2903. seq_printf(m, "%shas cleared dead binder\n", prefix);
  2904. break;
  2905. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
  2906. seq_printf(m, "%shas cleared death notification\n", prefix);
  2907. break;
  2908. default:
  2909. seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
  2910. break;
  2911. }
  2912. }
  2913. static void print_binder_thread(struct seq_file *m,
  2914. struct binder_thread *thread,
  2915. int print_always)
  2916. {
  2917. struct binder_transaction *t;
  2918. struct binder_work *w;
  2919. size_t start_pos = m->count;
  2920. size_t header_pos;
  2921. seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
  2922. header_pos = m->count;
  2923. t = thread->transaction_stack;
  2924. while (t) {
  2925. if (t->from == thread) {
  2926. print_binder_transaction(m,
  2927. " outgoing transaction", t);
  2928. t = t->from_parent;
  2929. } else if (t->to_thread == thread) {
  2930. print_binder_transaction(m,
  2931. " incoming transaction", t);
  2932. t = t->to_parent;
  2933. } else {
  2934. print_binder_transaction(m, " bad transaction", t);
  2935. t = NULL;
  2936. }
  2937. }
  2938. list_for_each_entry(w, &thread->todo, entry) {
  2939. print_binder_work(m, " ", " pending transaction", w);
  2940. }
  2941. if (!print_always && m->count == header_pos)
  2942. m->count = start_pos;
  2943. }
  2944. static void print_binder_node(struct seq_file *m, struct binder_node *node)
  2945. {
  2946. struct binder_ref *ref;
  2947. struct binder_work *w;
  2948. int count;
  2949. count = 0;
  2950. hlist_for_each_entry(ref, &node->refs, node_entry)
  2951. count++;
  2952. seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
  2953. node->debug_id, (u64)node->ptr, (u64)node->cookie,
  2954. node->has_strong_ref, node->has_weak_ref,
  2955. node->local_strong_refs, node->local_weak_refs,
  2956. node->internal_strong_refs, count);
  2957. if (count) {
  2958. seq_puts(m, " proc");
  2959. hlist_for_each_entry(ref, &node->refs, node_entry)
  2960. seq_printf(m, " %d", ref->proc->pid);
  2961. }
  2962. seq_puts(m, "\n");
  2963. list_for_each_entry(w, &node->async_todo, entry)
  2964. print_binder_work(m, " ",
  2965. " pending async transaction", w);
  2966. }
  2967. static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
  2968. {
  2969. seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
  2970. ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
  2971. ref->node->debug_id, ref->strong, ref->weak, ref->death);
  2972. }
  2973. static void print_binder_proc(struct seq_file *m,
  2974. struct binder_proc *proc, int print_all)
  2975. {
  2976. struct binder_work *w;
  2977. struct rb_node *n;
  2978. size_t start_pos = m->count;
  2979. size_t header_pos;
  2980. seq_printf(m, "proc %d\n", proc->pid);
  2981. header_pos = m->count;
  2982. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  2983. print_binder_thread(m, rb_entry(n, struct binder_thread,
  2984. rb_node), print_all);
  2985. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
  2986. struct binder_node *node = rb_entry(n, struct binder_node,
  2987. rb_node);
  2988. if (print_all || node->has_async_transaction)
  2989. print_binder_node(m, node);
  2990. }
  2991. if (print_all) {
  2992. for (n = rb_first(&proc->refs_by_desc);
  2993. n != NULL;
  2994. n = rb_next(n))
  2995. print_binder_ref(m, rb_entry(n, struct binder_ref,
  2996. rb_node_desc));
  2997. }
  2998. for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
  2999. print_binder_buffer(m, " buffer",
  3000. rb_entry(n, struct binder_buffer, rb_node));
  3001. list_for_each_entry(w, &proc->todo, entry)
  3002. print_binder_work(m, " ", " pending transaction", w);
  3003. list_for_each_entry(w, &proc->delivered_death, entry) {
  3004. seq_puts(m, " has delivered dead binder\n");
  3005. break;
  3006. }
  3007. if (!print_all && m->count == header_pos)
  3008. m->count = start_pos;
  3009. }
  3010. static const char * const binder_return_strings[] = {
  3011. "BR_ERROR",
  3012. "BR_OK",
  3013. "BR_TRANSACTION",
  3014. "BR_REPLY",
  3015. "BR_ACQUIRE_RESULT",
  3016. "BR_DEAD_REPLY",
  3017. "BR_TRANSACTION_COMPLETE",
  3018. "BR_INCREFS",
  3019. "BR_ACQUIRE",
  3020. "BR_RELEASE",
  3021. "BR_DECREFS",
  3022. "BR_ATTEMPT_ACQUIRE",
  3023. "BR_NOOP",
  3024. "BR_SPAWN_LOOPER",
  3025. "BR_FINISHED",
  3026. "BR_DEAD_BINDER",
  3027. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  3028. "BR_FAILED_REPLY"
  3029. };
  3030. static const char * const binder_command_strings[] = {
  3031. "BC_TRANSACTION",
  3032. "BC_REPLY",
  3033. "BC_ACQUIRE_RESULT",
  3034. "BC_FREE_BUFFER",
  3035. "BC_INCREFS",
  3036. "BC_ACQUIRE",
  3037. "BC_RELEASE",
  3038. "BC_DECREFS",
  3039. "BC_INCREFS_DONE",
  3040. "BC_ACQUIRE_DONE",
  3041. "BC_ATTEMPT_ACQUIRE",
  3042. "BC_REGISTER_LOOPER",
  3043. "BC_ENTER_LOOPER",
  3044. "BC_EXIT_LOOPER",
  3045. "BC_REQUEST_DEATH_NOTIFICATION",
  3046. "BC_CLEAR_DEATH_NOTIFICATION",
  3047. "BC_DEAD_BINDER_DONE"
  3048. };
  3049. static const char * const binder_objstat_strings[] = {
  3050. "proc",
  3051. "thread",
  3052. "node",
  3053. "ref",
  3054. "death",
  3055. "transaction",
  3056. "transaction_complete"
  3057. };
  3058. static void print_binder_stats(struct seq_file *m, const char *prefix,
  3059. struct binder_stats *stats)
  3060. {
  3061. int i;
  3062. BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
  3063. ARRAY_SIZE(binder_command_strings));
  3064. for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
  3065. if (stats->bc[i])
  3066. seq_printf(m, "%s%s: %d\n", prefix,
  3067. binder_command_strings[i], stats->bc[i]);
  3068. }
  3069. BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
  3070. ARRAY_SIZE(binder_return_strings));
  3071. for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
  3072. if (stats->br[i])
  3073. seq_printf(m, "%s%s: %d\n", prefix,
  3074. binder_return_strings[i], stats->br[i]);
  3075. }
  3076. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  3077. ARRAY_SIZE(binder_objstat_strings));
  3078. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  3079. ARRAY_SIZE(stats->obj_deleted));
  3080. for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
  3081. if (stats->obj_created[i] || stats->obj_deleted[i])
  3082. seq_printf(m, "%s%s: active %d total %d\n", prefix,
  3083. binder_objstat_strings[i],
  3084. stats->obj_created[i] - stats->obj_deleted[i],
  3085. stats->obj_created[i]);
  3086. }
  3087. }
  3088. static void print_binder_proc_stats(struct seq_file *m,
  3089. struct binder_proc *proc)
  3090. {
  3091. struct binder_work *w;
  3092. struct rb_node *n;
  3093. int count, strong, weak;
  3094. seq_printf(m, "proc %d\n", proc->pid);
  3095. count = 0;
  3096. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  3097. count++;
  3098. seq_printf(m, " threads: %d\n", count);
  3099. seq_printf(m, " requested threads: %d+%d/%d\n"
  3100. " ready threads %d\n"
  3101. " free async space %zd\n", proc->requested_threads,
  3102. proc->requested_threads_started, proc->max_threads,
  3103. proc->ready_threads, proc->free_async_space);
  3104. count = 0;
  3105. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
  3106. count++;
  3107. seq_printf(m, " nodes: %d\n", count);
  3108. count = 0;
  3109. strong = 0;
  3110. weak = 0;
  3111. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  3112. struct binder_ref *ref = rb_entry(n, struct binder_ref,
  3113. rb_node_desc);
  3114. count++;
  3115. strong += ref->strong;
  3116. weak += ref->weak;
  3117. }
  3118. seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
  3119. count = 0;
  3120. for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
  3121. count++;
  3122. seq_printf(m, " buffers: %d\n", count);
  3123. count = 0;
  3124. list_for_each_entry(w, &proc->todo, entry) {
  3125. switch (w->type) {
  3126. case BINDER_WORK_TRANSACTION:
  3127. count++;
  3128. break;
  3129. default:
  3130. break;
  3131. }
  3132. }
  3133. seq_printf(m, " pending transactions: %d\n", count);
  3134. print_binder_stats(m, " ", &proc->stats);
  3135. }
  3136. static int binder_state_show(struct seq_file *m, void *unused)
  3137. {
  3138. struct binder_proc *proc;
  3139. struct binder_node *node;
  3140. int do_lock = !binder_debug_no_lock;
  3141. if (do_lock)
  3142. binder_lock(__func__);
  3143. seq_puts(m, "binder state:\n");
  3144. if (!hlist_empty(&binder_dead_nodes))
  3145. seq_puts(m, "dead nodes:\n");
  3146. hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
  3147. print_binder_node(m, node);
  3148. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3149. print_binder_proc(m, proc, 1);
  3150. if (do_lock)
  3151. binder_unlock(__func__);
  3152. return 0;
  3153. }
  3154. static int binder_stats_show(struct seq_file *m, void *unused)
  3155. {
  3156. struct binder_proc *proc;
  3157. int do_lock = !binder_debug_no_lock;
  3158. if (do_lock)
  3159. binder_lock(__func__);
  3160. seq_puts(m, "binder stats:\n");
  3161. print_binder_stats(m, "", &binder_stats);
  3162. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3163. print_binder_proc_stats(m, proc);
  3164. if (do_lock)
  3165. binder_unlock(__func__);
  3166. return 0;
  3167. }
  3168. static int binder_transactions_show(struct seq_file *m, void *unused)
  3169. {
  3170. struct binder_proc *proc;
  3171. int do_lock = !binder_debug_no_lock;
  3172. if (do_lock)
  3173. binder_lock(__func__);
  3174. seq_puts(m, "binder transactions:\n");
  3175. hlist_for_each_entry(proc, &binder_procs, proc_node)
  3176. print_binder_proc(m, proc, 0);
  3177. if (do_lock)
  3178. binder_unlock(__func__);
  3179. return 0;
  3180. }
  3181. static int binder_proc_show(struct seq_file *m, void *unused)
  3182. {
  3183. struct binder_proc *proc = m->private;
  3184. int do_lock = !binder_debug_no_lock;
  3185. if (do_lock)
  3186. binder_lock(__func__);
  3187. seq_puts(m, "binder proc state:\n");
  3188. print_binder_proc(m, proc, 1);
  3189. if (do_lock)
  3190. binder_unlock(__func__);
  3191. return 0;
  3192. }
  3193. static void print_binder_transaction_log_entry(struct seq_file *m,
  3194. struct binder_transaction_log_entry *e)
  3195. {
  3196. seq_printf(m,
  3197. "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
  3198. e->debug_id, (e->call_type == 2) ? "reply" :
  3199. ((e->call_type == 1) ? "async" : "call "), e->from_proc,
  3200. e->from_thread, e->to_proc, e->to_thread, e->to_node,
  3201. e->target_handle, e->data_size, e->offsets_size);
  3202. }
  3203. static int binder_transaction_log_show(struct seq_file *m, void *unused)
  3204. {
  3205. struct binder_transaction_log *log = m->private;
  3206. int i;
  3207. if (log->full) {
  3208. for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
  3209. print_binder_transaction_log_entry(m, &log->entry[i]);
  3210. }
  3211. for (i = 0; i < log->next; i++)
  3212. print_binder_transaction_log_entry(m, &log->entry[i]);
  3213. return 0;
  3214. }
  3215. static const struct file_operations binder_fops = {
  3216. .owner = THIS_MODULE,
  3217. .poll = binder_poll,
  3218. .unlocked_ioctl = binder_ioctl,
  3219. .compat_ioctl = binder_ioctl,
  3220. .mmap = binder_mmap,
  3221. .open = binder_open,
  3222. .flush = binder_flush,
  3223. .release = binder_release,
  3224. };
  3225. static struct miscdevice binder_miscdev = {
  3226. .minor = MISC_DYNAMIC_MINOR,
  3227. .name = "binder",
  3228. .fops = &binder_fops
  3229. };
  3230. BINDER_DEBUG_ENTRY(state);
  3231. BINDER_DEBUG_ENTRY(stats);
  3232. BINDER_DEBUG_ENTRY(transactions);
  3233. BINDER_DEBUG_ENTRY(transaction_log);
  3234. static int __init binder_init(void)
  3235. {
  3236. int ret;
  3237. binder_deferred_workqueue = create_singlethread_workqueue("binder");
  3238. if (!binder_deferred_workqueue)
  3239. return -ENOMEM;
  3240. binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
  3241. if (binder_debugfs_dir_entry_root)
  3242. binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
  3243. binder_debugfs_dir_entry_root);
  3244. ret = misc_register(&binder_miscdev);
  3245. if (binder_debugfs_dir_entry_root) {
  3246. debugfs_create_file("state",
  3247. S_IRUGO,
  3248. binder_debugfs_dir_entry_root,
  3249. NULL,
  3250. &binder_state_fops);
  3251. debugfs_create_file("stats",
  3252. S_IRUGO,
  3253. binder_debugfs_dir_entry_root,
  3254. NULL,
  3255. &binder_stats_fops);
  3256. debugfs_create_file("transactions",
  3257. S_IRUGO,
  3258. binder_debugfs_dir_entry_root,
  3259. NULL,
  3260. &binder_transactions_fops);
  3261. debugfs_create_file("transaction_log",
  3262. S_IRUGO,
  3263. binder_debugfs_dir_entry_root,
  3264. &binder_transaction_log,
  3265. &binder_transaction_log_fops);
  3266. debugfs_create_file("failed_transaction_log",
  3267. S_IRUGO,
  3268. binder_debugfs_dir_entry_root,
  3269. &binder_transaction_log_failed,
  3270. &binder_transaction_log_fops);
  3271. }
  3272. return ret;
  3273. }
  3274. device_initcall(binder_init);
  3275. #define CREATE_TRACE_POINTS
  3276. #include "binder_trace.h"
  3277. MODULE_LICENSE("GPL v2");