binder.c 119 KB

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