binder.c 102 KB

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