binder.c 102 KB

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