binder.c 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324
  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. /*
  18. * Locking overview
  19. *
  20. * There are 3 main spinlocks which must be acquired in the
  21. * order shown:
  22. *
  23. * 1) proc->outer_lock : protects binder_ref
  24. * binder_proc_lock() and binder_proc_unlock() are
  25. * used to acq/rel.
  26. * 2) node->lock : protects most fields of binder_node.
  27. * binder_node_lock() and binder_node_unlock() are
  28. * used to acq/rel
  29. * 3) proc->inner_lock : protects the thread and node lists
  30. * (proc->threads, proc->nodes) and all todo lists associated
  31. * with the binder_proc (proc->todo, thread->todo,
  32. * proc->delivered_death and node->async_todo), as well as
  33. * thread->transaction_stack
  34. * binder_inner_proc_lock() and binder_inner_proc_unlock()
  35. * are used to acq/rel
  36. *
  37. * Any lock under procA must never be nested under any lock at the same
  38. * level or below on procB.
  39. *
  40. * Functions that require a lock held on entry indicate which lock
  41. * in the suffix of the function name:
  42. *
  43. * foo_olocked() : requires node->outer_lock
  44. * foo_nlocked() : requires node->lock
  45. * foo_ilocked() : requires proc->inner_lock
  46. * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
  47. * foo_nilocked(): requires node->lock and proc->inner_lock
  48. * ...
  49. */
  50. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  51. #include <asm/cacheflush.h>
  52. #include <linux/fdtable.h>
  53. #include <linux/file.h>
  54. #include <linux/freezer.h>
  55. #include <linux/fs.h>
  56. #include <linux/list.h>
  57. #include <linux/miscdevice.h>
  58. #include <linux/module.h>
  59. #include <linux/mutex.h>
  60. #include <linux/nsproxy.h>
  61. #include <linux/poll.h>
  62. #include <linux/debugfs.h>
  63. #include <linux/rbtree.h>
  64. #include <linux/sched/signal.h>
  65. #include <linux/sched/mm.h>
  66. #include <linux/seq_file.h>
  67. #include <linux/uaccess.h>
  68. #include <linux/pid_namespace.h>
  69. #include <linux/security.h>
  70. #include <linux/spinlock.h>
  71. #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
  72. #define BINDER_IPC_32BIT 1
  73. #endif
  74. #include <uapi/linux/android/binder.h>
  75. #include "binder_alloc.h"
  76. #include "binder_trace.h"
  77. static HLIST_HEAD(binder_deferred_list);
  78. static DEFINE_MUTEX(binder_deferred_lock);
  79. static HLIST_HEAD(binder_devices);
  80. static HLIST_HEAD(binder_procs);
  81. static DEFINE_MUTEX(binder_procs_lock);
  82. static HLIST_HEAD(binder_dead_nodes);
  83. static DEFINE_SPINLOCK(binder_dead_nodes_lock);
  84. static struct dentry *binder_debugfs_dir_entry_root;
  85. static struct dentry *binder_debugfs_dir_entry_proc;
  86. static atomic_t binder_last_id;
  87. #define BINDER_DEBUG_ENTRY(name) \
  88. static int binder_##name##_open(struct inode *inode, struct file *file) \
  89. { \
  90. return single_open(file, binder_##name##_show, inode->i_private); \
  91. } \
  92. \
  93. static const struct file_operations binder_##name##_fops = { \
  94. .owner = THIS_MODULE, \
  95. .open = binder_##name##_open, \
  96. .read = seq_read, \
  97. .llseek = seq_lseek, \
  98. .release = single_release, \
  99. }
  100. static int binder_proc_show(struct seq_file *m, void *unused);
  101. BINDER_DEBUG_ENTRY(proc);
  102. /* This is only defined in include/asm-arm/sizes.h */
  103. #ifndef SZ_1K
  104. #define SZ_1K 0x400
  105. #endif
  106. #ifndef SZ_4M
  107. #define SZ_4M 0x400000
  108. #endif
  109. #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
  110. enum {
  111. BINDER_DEBUG_USER_ERROR = 1U << 0,
  112. BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
  113. BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
  114. BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
  115. BINDER_DEBUG_DEAD_BINDER = 1U << 4,
  116. BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
  117. BINDER_DEBUG_READ_WRITE = 1U << 6,
  118. BINDER_DEBUG_USER_REFS = 1U << 7,
  119. BINDER_DEBUG_THREADS = 1U << 8,
  120. BINDER_DEBUG_TRANSACTION = 1U << 9,
  121. BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
  122. BINDER_DEBUG_FREE_BUFFER = 1U << 11,
  123. BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
  124. BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
  125. BINDER_DEBUG_SPINLOCKS = 1U << 14,
  126. };
  127. static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
  128. BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
  129. module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
  130. static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
  131. module_param_named(devices, binder_devices_param, charp, 0444);
  132. static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
  133. static int binder_stop_on_user_error;
  134. static int binder_set_stop_on_user_error(const char *val,
  135. struct kernel_param *kp)
  136. {
  137. int ret;
  138. ret = param_set_int(val, kp);
  139. if (binder_stop_on_user_error < 2)
  140. wake_up(&binder_user_error_wait);
  141. return ret;
  142. }
  143. module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
  144. param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
  145. #define binder_debug(mask, x...) \
  146. do { \
  147. if (binder_debug_mask & mask) \
  148. pr_info(x); \
  149. } while (0)
  150. #define binder_user_error(x...) \
  151. do { \
  152. if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
  153. pr_info(x); \
  154. if (binder_stop_on_user_error) \
  155. binder_stop_on_user_error = 2; \
  156. } while (0)
  157. #define to_flat_binder_object(hdr) \
  158. container_of(hdr, struct flat_binder_object, hdr)
  159. #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
  160. #define to_binder_buffer_object(hdr) \
  161. container_of(hdr, struct binder_buffer_object, hdr)
  162. #define to_binder_fd_array_object(hdr) \
  163. container_of(hdr, struct binder_fd_array_object, hdr)
  164. enum binder_stat_types {
  165. BINDER_STAT_PROC,
  166. BINDER_STAT_THREAD,
  167. BINDER_STAT_NODE,
  168. BINDER_STAT_REF,
  169. BINDER_STAT_DEATH,
  170. BINDER_STAT_TRANSACTION,
  171. BINDER_STAT_TRANSACTION_COMPLETE,
  172. BINDER_STAT_COUNT
  173. };
  174. struct binder_stats {
  175. atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
  176. atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
  177. atomic_t obj_created[BINDER_STAT_COUNT];
  178. atomic_t obj_deleted[BINDER_STAT_COUNT];
  179. };
  180. static struct binder_stats binder_stats;
  181. static inline void binder_stats_deleted(enum binder_stat_types type)
  182. {
  183. atomic_inc(&binder_stats.obj_deleted[type]);
  184. }
  185. static inline void binder_stats_created(enum binder_stat_types type)
  186. {
  187. atomic_inc(&binder_stats.obj_created[type]);
  188. }
  189. struct binder_transaction_log_entry {
  190. int debug_id;
  191. int debug_id_done;
  192. int call_type;
  193. int from_proc;
  194. int from_thread;
  195. int target_handle;
  196. int to_proc;
  197. int to_thread;
  198. int to_node;
  199. int data_size;
  200. int offsets_size;
  201. int return_error_line;
  202. uint32_t return_error;
  203. uint32_t return_error_param;
  204. const char *context_name;
  205. };
  206. struct binder_transaction_log {
  207. atomic_t cur;
  208. bool full;
  209. struct binder_transaction_log_entry entry[32];
  210. };
  211. static struct binder_transaction_log binder_transaction_log;
  212. static struct binder_transaction_log binder_transaction_log_failed;
  213. static struct binder_transaction_log_entry *binder_transaction_log_add(
  214. struct binder_transaction_log *log)
  215. {
  216. struct binder_transaction_log_entry *e;
  217. unsigned int cur = atomic_inc_return(&log->cur);
  218. if (cur >= ARRAY_SIZE(log->entry))
  219. log->full = 1;
  220. e = &log->entry[cur % ARRAY_SIZE(log->entry)];
  221. WRITE_ONCE(e->debug_id_done, 0);
  222. /*
  223. * write-barrier to synchronize access to e->debug_id_done.
  224. * We make sure the initialized 0 value is seen before
  225. * memset() other fields are zeroed by memset.
  226. */
  227. smp_wmb();
  228. memset(e, 0, sizeof(*e));
  229. return e;
  230. }
  231. struct binder_context {
  232. struct binder_node *binder_context_mgr_node;
  233. struct mutex context_mgr_node_lock;
  234. kuid_t binder_context_mgr_uid;
  235. const char *name;
  236. };
  237. struct binder_device {
  238. struct hlist_node hlist;
  239. struct miscdevice miscdev;
  240. struct binder_context context;
  241. };
  242. /**
  243. * struct binder_work - work enqueued on a worklist
  244. * @entry: node enqueued on list
  245. * @type: type of work to be performed
  246. *
  247. * There are separate work lists for proc, thread, and node (async).
  248. */
  249. struct binder_work {
  250. struct list_head entry;
  251. enum {
  252. BINDER_WORK_TRANSACTION = 1,
  253. BINDER_WORK_TRANSACTION_COMPLETE,
  254. BINDER_WORK_RETURN_ERROR,
  255. BINDER_WORK_NODE,
  256. BINDER_WORK_DEAD_BINDER,
  257. BINDER_WORK_DEAD_BINDER_AND_CLEAR,
  258. BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
  259. } type;
  260. };
  261. struct binder_error {
  262. struct binder_work work;
  263. uint32_t cmd;
  264. };
  265. /**
  266. * struct binder_node - binder node bookkeeping
  267. * @debug_id: unique ID for debugging
  268. * (invariant after initialized)
  269. * @lock: lock for node fields
  270. * @work: worklist element for node work
  271. * (protected by @proc->inner_lock)
  272. * @rb_node: element for proc->nodes tree
  273. * (protected by @proc->inner_lock)
  274. * @dead_node: element for binder_dead_nodes list
  275. * (protected by binder_dead_nodes_lock)
  276. * @proc: binder_proc that owns this node
  277. * (invariant after initialized)
  278. * @refs: list of references on this node
  279. * (protected by @lock)
  280. * @internal_strong_refs: used to take strong references when
  281. * initiating a transaction
  282. * (protected by @proc->inner_lock if @proc
  283. * and by @lock)
  284. * @local_weak_refs: weak user refs from local process
  285. * (protected by @proc->inner_lock if @proc
  286. * and by @lock)
  287. * @local_strong_refs: strong user refs from local process
  288. * (protected by @proc->inner_lock if @proc
  289. * and by @lock)
  290. * @tmp_refs: temporary kernel refs
  291. * (protected by @proc->inner_lock while @proc
  292. * is valid, and by binder_dead_nodes_lock
  293. * if @proc is NULL. During inc/dec and node release
  294. * it is also protected by @lock to provide safety
  295. * as the node dies and @proc becomes NULL)
  296. * @ptr: userspace pointer for node
  297. * (invariant, no lock needed)
  298. * @cookie: userspace cookie for node
  299. * (invariant, no lock needed)
  300. * @has_strong_ref: userspace notified of strong ref
  301. * (protected by @proc->inner_lock if @proc
  302. * and by @lock)
  303. * @pending_strong_ref: userspace has acked notification of strong ref
  304. * (protected by @proc->inner_lock if @proc
  305. * and by @lock)
  306. * @has_weak_ref: userspace notified of weak ref
  307. * (protected by @proc->inner_lock if @proc
  308. * and by @lock)
  309. * @pending_weak_ref: userspace has acked notification of weak ref
  310. * (protected by @proc->inner_lock if @proc
  311. * and by @lock)
  312. * @has_async_transaction: async transaction to node in progress
  313. * (protected by @lock)
  314. * @accept_fds: file descriptor operations supported for node
  315. * (invariant after initialized)
  316. * @min_priority: minimum scheduling priority
  317. * (invariant after initialized)
  318. * @async_todo: list of async work items
  319. * (protected by @proc->inner_lock)
  320. *
  321. * Bookkeeping structure for binder nodes.
  322. */
  323. struct binder_node {
  324. int debug_id;
  325. spinlock_t lock;
  326. struct binder_work work;
  327. union {
  328. struct rb_node rb_node;
  329. struct hlist_node dead_node;
  330. };
  331. struct binder_proc *proc;
  332. struct hlist_head refs;
  333. int internal_strong_refs;
  334. int local_weak_refs;
  335. int local_strong_refs;
  336. int tmp_refs;
  337. binder_uintptr_t ptr;
  338. binder_uintptr_t cookie;
  339. struct {
  340. /*
  341. * bitfield elements protected by
  342. * proc inner_lock
  343. */
  344. u8 has_strong_ref:1;
  345. u8 pending_strong_ref:1;
  346. u8 has_weak_ref:1;
  347. u8 pending_weak_ref:1;
  348. };
  349. struct {
  350. /*
  351. * invariant after initialization
  352. */
  353. u8 accept_fds:1;
  354. u8 min_priority;
  355. };
  356. bool has_async_transaction;
  357. struct list_head async_todo;
  358. };
  359. struct binder_ref_death {
  360. /**
  361. * @work: worklist element for death notifications
  362. * (protected by inner_lock of the proc that
  363. * this ref belongs to)
  364. */
  365. struct binder_work work;
  366. binder_uintptr_t cookie;
  367. };
  368. /**
  369. * struct binder_ref_data - binder_ref counts and id
  370. * @debug_id: unique ID for the ref
  371. * @desc: unique userspace handle for ref
  372. * @strong: strong ref count (debugging only if not locked)
  373. * @weak: weak ref count (debugging only if not locked)
  374. *
  375. * Structure to hold ref count and ref id information. Since
  376. * the actual ref can only be accessed with a lock, this structure
  377. * is used to return information about the ref to callers of
  378. * ref inc/dec functions.
  379. */
  380. struct binder_ref_data {
  381. int debug_id;
  382. uint32_t desc;
  383. int strong;
  384. int weak;
  385. };
  386. /**
  387. * struct binder_ref - struct to track references on nodes
  388. * @data: binder_ref_data containing id, handle, and current refcounts
  389. * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
  390. * @rb_node_node: node for lookup by @node in proc's rb_tree
  391. * @node_entry: list entry for node->refs list in target node
  392. * (protected by @node->lock)
  393. * @proc: binder_proc containing ref
  394. * @node: binder_node of target node. When cleaning up a
  395. * ref for deletion in binder_cleanup_ref, a non-NULL
  396. * @node indicates the node must be freed
  397. * @death: pointer to death notification (ref_death) if requested
  398. * (protected by @node->lock)
  399. *
  400. * Structure to track references from procA to target node (on procB). This
  401. * structure is unsafe to access without holding @proc->outer_lock.
  402. */
  403. struct binder_ref {
  404. /* Lookups needed: */
  405. /* node + proc => ref (transaction) */
  406. /* desc + proc => ref (transaction, inc/dec ref) */
  407. /* node => refs + procs (proc exit) */
  408. struct binder_ref_data data;
  409. struct rb_node rb_node_desc;
  410. struct rb_node rb_node_node;
  411. struct hlist_node node_entry;
  412. struct binder_proc *proc;
  413. struct binder_node *node;
  414. struct binder_ref_death *death;
  415. };
  416. enum binder_deferred_state {
  417. BINDER_DEFERRED_PUT_FILES = 0x01,
  418. BINDER_DEFERRED_FLUSH = 0x02,
  419. BINDER_DEFERRED_RELEASE = 0x04,
  420. };
  421. /**
  422. * struct binder_proc - binder process bookkeeping
  423. * @proc_node: element for binder_procs list
  424. * @threads: rbtree of binder_threads in this proc
  425. * (protected by @inner_lock)
  426. * @nodes: rbtree of binder nodes associated with
  427. * this proc ordered by node->ptr
  428. * (protected by @inner_lock)
  429. * @refs_by_desc: rbtree of refs ordered by ref->desc
  430. * (protected by @outer_lock)
  431. * @refs_by_node: rbtree of refs ordered by ref->node
  432. * (protected by @outer_lock)
  433. * @pid PID of group_leader of process
  434. * (invariant after initialized)
  435. * @tsk task_struct for group_leader of process
  436. * (invariant after initialized)
  437. * @files files_struct for process
  438. * (invariant after initialized)
  439. * @deferred_work_node: element for binder_deferred_list
  440. * (protected by binder_deferred_lock)
  441. * @deferred_work: bitmap of deferred work to perform
  442. * (protected by binder_deferred_lock)
  443. * @is_dead: process is dead and awaiting free
  444. * when outstanding transactions are cleaned up
  445. * (protected by @inner_lock)
  446. * @todo: list of work for this process
  447. * (protected by @inner_lock)
  448. * @wait: wait queue head to wait for proc work
  449. * (invariant after initialized)
  450. * @stats: per-process binder statistics
  451. * (atomics, no lock needed)
  452. * @delivered_death: list of delivered death notification
  453. * (protected by @inner_lock)
  454. * @max_threads: cap on number of binder threads
  455. * (protected by @inner_lock)
  456. * @requested_threads: number of binder threads requested but not
  457. * yet started. In current implementation, can
  458. * only be 0 or 1.
  459. * (protected by @inner_lock)
  460. * @requested_threads_started: number binder threads started
  461. * (protected by @inner_lock)
  462. * @ready_threads: number of threads waiting for proc work
  463. * (protected by @inner_lock)
  464. * @tmp_ref: temporary reference to indicate proc is in use
  465. * (protected by @inner_lock)
  466. * @default_priority: default scheduler priority
  467. * (invariant after initialized)
  468. * @debugfs_entry: debugfs node
  469. * @alloc: binder allocator bookkeeping
  470. * @context: binder_context for this proc
  471. * (invariant after initialized)
  472. * @inner_lock: can nest under outer_lock and/or node lock
  473. * @outer_lock: no nesting under innor or node lock
  474. * Lock order: 1) outer, 2) node, 3) inner
  475. *
  476. * Bookkeeping structure for binder processes
  477. */
  478. struct binder_proc {
  479. struct hlist_node proc_node;
  480. struct rb_root threads;
  481. struct rb_root nodes;
  482. struct rb_root refs_by_desc;
  483. struct rb_root refs_by_node;
  484. int pid;
  485. struct task_struct *tsk;
  486. struct files_struct *files;
  487. struct hlist_node deferred_work_node;
  488. int deferred_work;
  489. bool is_dead;
  490. struct list_head todo;
  491. wait_queue_head_t wait;
  492. struct binder_stats stats;
  493. struct list_head delivered_death;
  494. int max_threads;
  495. int requested_threads;
  496. int requested_threads_started;
  497. int ready_threads;
  498. int tmp_ref;
  499. long default_priority;
  500. struct dentry *debugfs_entry;
  501. struct binder_alloc alloc;
  502. struct binder_context *context;
  503. spinlock_t inner_lock;
  504. spinlock_t outer_lock;
  505. };
  506. enum {
  507. BINDER_LOOPER_STATE_REGISTERED = 0x01,
  508. BINDER_LOOPER_STATE_ENTERED = 0x02,
  509. BINDER_LOOPER_STATE_EXITED = 0x04,
  510. BINDER_LOOPER_STATE_INVALID = 0x08,
  511. BINDER_LOOPER_STATE_WAITING = 0x10,
  512. };
  513. /**
  514. * struct binder_thread - binder thread bookkeeping
  515. * @proc: binder process for this thread
  516. * (invariant after initialization)
  517. * @rb_node: element for proc->threads rbtree
  518. * (protected by @proc->inner_lock)
  519. * @pid: PID for this thread
  520. * (invariant after initialization)
  521. * @looper: bitmap of looping state
  522. * (only accessed by this thread)
  523. * @looper_needs_return: looping thread needs to exit driver
  524. * (no lock needed)
  525. * @transaction_stack: stack of in-progress transactions for this thread
  526. * (protected by @proc->inner_lock)
  527. * @todo: list of work to do for this thread
  528. * (protected by @proc->inner_lock)
  529. * @return_error: transaction errors reported by this thread
  530. * (only accessed by this thread)
  531. * @reply_error: transaction errors reported by target thread
  532. * (protected by @proc->inner_lock)
  533. * @wait: wait queue for thread work
  534. * @stats: per-thread statistics
  535. * (atomics, no lock needed)
  536. * @tmp_ref: temporary reference to indicate thread is in use
  537. * (atomic since @proc->inner_lock cannot
  538. * always be acquired)
  539. * @is_dead: thread is dead and awaiting free
  540. * when outstanding transactions are cleaned up
  541. * (protected by @proc->inner_lock)
  542. *
  543. * Bookkeeping structure for binder threads.
  544. */
  545. struct binder_thread {
  546. struct binder_proc *proc;
  547. struct rb_node rb_node;
  548. int pid;
  549. int looper; /* only modified by this thread */
  550. bool looper_need_return; /* can be written by other thread */
  551. struct binder_transaction *transaction_stack;
  552. struct list_head todo;
  553. struct binder_error return_error;
  554. struct binder_error reply_error;
  555. wait_queue_head_t wait;
  556. struct binder_stats stats;
  557. atomic_t tmp_ref;
  558. bool is_dead;
  559. };
  560. struct binder_transaction {
  561. int debug_id;
  562. struct binder_work work;
  563. struct binder_thread *from;
  564. struct binder_transaction *from_parent;
  565. struct binder_proc *to_proc;
  566. struct binder_thread *to_thread;
  567. struct binder_transaction *to_parent;
  568. unsigned need_reply:1;
  569. /* unsigned is_dead:1; */ /* not used at the moment */
  570. struct binder_buffer *buffer;
  571. unsigned int code;
  572. unsigned int flags;
  573. long priority;
  574. long saved_priority;
  575. kuid_t sender_euid;
  576. /**
  577. * @lock: protects @from, @to_proc, and @to_thread
  578. *
  579. * @from, @to_proc, and @to_thread can be set to NULL
  580. * during thread teardown
  581. */
  582. spinlock_t lock;
  583. };
  584. /**
  585. * binder_proc_lock() - Acquire outer lock for given binder_proc
  586. * @proc: struct binder_proc to acquire
  587. *
  588. * Acquires proc->outer_lock. Used to protect binder_ref
  589. * structures associated with the given proc.
  590. */
  591. #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
  592. static void
  593. _binder_proc_lock(struct binder_proc *proc, int line)
  594. {
  595. binder_debug(BINDER_DEBUG_SPINLOCKS,
  596. "%s: line=%d\n", __func__, line);
  597. spin_lock(&proc->outer_lock);
  598. }
  599. /**
  600. * binder_proc_unlock() - Release spinlock for given binder_proc
  601. * @proc: struct binder_proc to acquire
  602. *
  603. * Release lock acquired via binder_proc_lock()
  604. */
  605. #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
  606. static void
  607. _binder_proc_unlock(struct binder_proc *proc, int line)
  608. {
  609. binder_debug(BINDER_DEBUG_SPINLOCKS,
  610. "%s: line=%d\n", __func__, line);
  611. spin_unlock(&proc->outer_lock);
  612. }
  613. /**
  614. * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
  615. * @proc: struct binder_proc to acquire
  616. *
  617. * Acquires proc->inner_lock. Used to protect todo lists
  618. */
  619. #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
  620. static void
  621. _binder_inner_proc_lock(struct binder_proc *proc, int line)
  622. {
  623. binder_debug(BINDER_DEBUG_SPINLOCKS,
  624. "%s: line=%d\n", __func__, line);
  625. spin_lock(&proc->inner_lock);
  626. }
  627. /**
  628. * binder_inner_proc_unlock() - Release inner lock for given binder_proc
  629. * @proc: struct binder_proc to acquire
  630. *
  631. * Release lock acquired via binder_inner_proc_lock()
  632. */
  633. #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
  634. static void
  635. _binder_inner_proc_unlock(struct binder_proc *proc, int line)
  636. {
  637. binder_debug(BINDER_DEBUG_SPINLOCKS,
  638. "%s: line=%d\n", __func__, line);
  639. spin_unlock(&proc->inner_lock);
  640. }
  641. /**
  642. * binder_node_lock() - Acquire spinlock for given binder_node
  643. * @node: struct binder_node to acquire
  644. *
  645. * Acquires node->lock. Used to protect binder_node fields
  646. */
  647. #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
  648. static void
  649. _binder_node_lock(struct binder_node *node, int line)
  650. {
  651. binder_debug(BINDER_DEBUG_SPINLOCKS,
  652. "%s: line=%d\n", __func__, line);
  653. spin_lock(&node->lock);
  654. }
  655. /**
  656. * binder_node_unlock() - Release spinlock for given binder_proc
  657. * @node: struct binder_node to acquire
  658. *
  659. * Release lock acquired via binder_node_lock()
  660. */
  661. #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
  662. static void
  663. _binder_node_unlock(struct binder_node *node, int line)
  664. {
  665. binder_debug(BINDER_DEBUG_SPINLOCKS,
  666. "%s: line=%d\n", __func__, line);
  667. spin_unlock(&node->lock);
  668. }
  669. /**
  670. * binder_node_inner_lock() - Acquire node and inner locks
  671. * @node: struct binder_node to acquire
  672. *
  673. * Acquires node->lock. If node->proc also acquires
  674. * proc->inner_lock. Used to protect binder_node fields
  675. */
  676. #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
  677. static void
  678. _binder_node_inner_lock(struct binder_node *node, int line)
  679. {
  680. binder_debug(BINDER_DEBUG_SPINLOCKS,
  681. "%s: line=%d\n", __func__, line);
  682. spin_lock(&node->lock);
  683. if (node->proc)
  684. binder_inner_proc_lock(node->proc);
  685. }
  686. /**
  687. * binder_node_unlock() - Release node and inner locks
  688. * @node: struct binder_node to acquire
  689. *
  690. * Release lock acquired via binder_node_lock()
  691. */
  692. #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
  693. static void
  694. _binder_node_inner_unlock(struct binder_node *node, int line)
  695. {
  696. struct binder_proc *proc = node->proc;
  697. binder_debug(BINDER_DEBUG_SPINLOCKS,
  698. "%s: line=%d\n", __func__, line);
  699. if (proc)
  700. binder_inner_proc_unlock(proc);
  701. spin_unlock(&node->lock);
  702. }
  703. static bool binder_worklist_empty_ilocked(struct list_head *list)
  704. {
  705. return list_empty(list);
  706. }
  707. /**
  708. * binder_worklist_empty() - Check if no items on the work list
  709. * @proc: binder_proc associated with list
  710. * @list: list to check
  711. *
  712. * Return: true if there are no items on list, else false
  713. */
  714. static bool binder_worklist_empty(struct binder_proc *proc,
  715. struct list_head *list)
  716. {
  717. bool ret;
  718. binder_inner_proc_lock(proc);
  719. ret = binder_worklist_empty_ilocked(list);
  720. binder_inner_proc_unlock(proc);
  721. return ret;
  722. }
  723. static void
  724. binder_enqueue_work_ilocked(struct binder_work *work,
  725. struct list_head *target_list)
  726. {
  727. BUG_ON(target_list == NULL);
  728. BUG_ON(work->entry.next && !list_empty(&work->entry));
  729. list_add_tail(&work->entry, target_list);
  730. }
  731. /**
  732. * binder_enqueue_work() - Add an item to the work list
  733. * @proc: binder_proc associated with list
  734. * @work: struct binder_work to add to list
  735. * @target_list: list to add work to
  736. *
  737. * Adds the work to the specified list. Asserts that work
  738. * is not already on a list.
  739. */
  740. static void
  741. binder_enqueue_work(struct binder_proc *proc,
  742. struct binder_work *work,
  743. struct list_head *target_list)
  744. {
  745. binder_inner_proc_lock(proc);
  746. binder_enqueue_work_ilocked(work, target_list);
  747. binder_inner_proc_unlock(proc);
  748. }
  749. static void
  750. binder_dequeue_work_ilocked(struct binder_work *work)
  751. {
  752. list_del_init(&work->entry);
  753. }
  754. /**
  755. * binder_dequeue_work() - Removes an item from the work list
  756. * @proc: binder_proc associated with list
  757. * @work: struct binder_work to remove from list
  758. *
  759. * Removes the specified work item from whatever list it is on.
  760. * Can safely be called if work is not on any list.
  761. */
  762. static void
  763. binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
  764. {
  765. binder_inner_proc_lock(proc);
  766. binder_dequeue_work_ilocked(work);
  767. binder_inner_proc_unlock(proc);
  768. }
  769. static struct binder_work *binder_dequeue_work_head_ilocked(
  770. struct list_head *list)
  771. {
  772. struct binder_work *w;
  773. w = list_first_entry_or_null(list, struct binder_work, entry);
  774. if (w)
  775. list_del_init(&w->entry);
  776. return w;
  777. }
  778. /**
  779. * binder_dequeue_work_head() - Dequeues the item at head of list
  780. * @proc: binder_proc associated with list
  781. * @list: list to dequeue head
  782. *
  783. * Removes the head of the list if there are items on the list
  784. *
  785. * Return: pointer dequeued binder_work, NULL if list was empty
  786. */
  787. static struct binder_work *binder_dequeue_work_head(
  788. struct binder_proc *proc,
  789. struct list_head *list)
  790. {
  791. struct binder_work *w;
  792. binder_inner_proc_lock(proc);
  793. w = binder_dequeue_work_head_ilocked(list);
  794. binder_inner_proc_unlock(proc);
  795. return w;
  796. }
  797. static void
  798. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
  799. static void binder_free_thread(struct binder_thread *thread);
  800. static void binder_free_proc(struct binder_proc *proc);
  801. static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
  802. static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
  803. {
  804. struct files_struct *files = proc->files;
  805. unsigned long rlim_cur;
  806. unsigned long irqs;
  807. if (files == NULL)
  808. return -ESRCH;
  809. if (!lock_task_sighand(proc->tsk, &irqs))
  810. return -EMFILE;
  811. rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
  812. unlock_task_sighand(proc->tsk, &irqs);
  813. return __alloc_fd(files, 0, rlim_cur, flags);
  814. }
  815. /*
  816. * copied from fd_install
  817. */
  818. static void task_fd_install(
  819. struct binder_proc *proc, unsigned int fd, struct file *file)
  820. {
  821. if (proc->files)
  822. __fd_install(proc->files, fd, file);
  823. }
  824. /*
  825. * copied from sys_close
  826. */
  827. static long task_close_fd(struct binder_proc *proc, unsigned int fd)
  828. {
  829. int retval;
  830. if (proc->files == NULL)
  831. return -ESRCH;
  832. retval = __close_fd(proc->files, fd);
  833. /* can't restart close syscall because file table entry was cleared */
  834. if (unlikely(retval == -ERESTARTSYS ||
  835. retval == -ERESTARTNOINTR ||
  836. retval == -ERESTARTNOHAND ||
  837. retval == -ERESTART_RESTARTBLOCK))
  838. retval = -EINTR;
  839. return retval;
  840. }
  841. static void binder_set_nice(long nice)
  842. {
  843. long min_nice;
  844. if (can_nice(current, nice)) {
  845. set_user_nice(current, nice);
  846. return;
  847. }
  848. min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
  849. binder_debug(BINDER_DEBUG_PRIORITY_CAP,
  850. "%d: nice value %ld not allowed use %ld instead\n",
  851. current->pid, nice, min_nice);
  852. set_user_nice(current, min_nice);
  853. if (min_nice <= MAX_NICE)
  854. return;
  855. binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
  856. }
  857. static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
  858. binder_uintptr_t ptr)
  859. {
  860. struct rb_node *n = proc->nodes.rb_node;
  861. struct binder_node *node;
  862. BUG_ON(!spin_is_locked(&proc->inner_lock));
  863. while (n) {
  864. node = rb_entry(n, struct binder_node, rb_node);
  865. if (ptr < node->ptr)
  866. n = n->rb_left;
  867. else if (ptr > node->ptr)
  868. n = n->rb_right;
  869. else {
  870. /*
  871. * take an implicit weak reference
  872. * to ensure node stays alive until
  873. * call to binder_put_node()
  874. */
  875. binder_inc_node_tmpref_ilocked(node);
  876. return node;
  877. }
  878. }
  879. return NULL;
  880. }
  881. static struct binder_node *binder_get_node(struct binder_proc *proc,
  882. binder_uintptr_t ptr)
  883. {
  884. struct binder_node *node;
  885. binder_inner_proc_lock(proc);
  886. node = binder_get_node_ilocked(proc, ptr);
  887. binder_inner_proc_unlock(proc);
  888. return node;
  889. }
  890. static struct binder_node *binder_init_node_ilocked(
  891. struct binder_proc *proc,
  892. struct binder_node *new_node,
  893. struct flat_binder_object *fp)
  894. {
  895. struct rb_node **p = &proc->nodes.rb_node;
  896. struct rb_node *parent = NULL;
  897. struct binder_node *node;
  898. binder_uintptr_t ptr = fp ? fp->binder : 0;
  899. binder_uintptr_t cookie = fp ? fp->cookie : 0;
  900. __u32 flags = fp ? fp->flags : 0;
  901. BUG_ON(!spin_is_locked(&proc->inner_lock));
  902. while (*p) {
  903. parent = *p;
  904. node = rb_entry(parent, struct binder_node, rb_node);
  905. if (ptr < node->ptr)
  906. p = &(*p)->rb_left;
  907. else if (ptr > node->ptr)
  908. p = &(*p)->rb_right;
  909. else {
  910. /*
  911. * A matching node is already in
  912. * the rb tree. Abandon the init
  913. * and return it.
  914. */
  915. binder_inc_node_tmpref_ilocked(node);
  916. return node;
  917. }
  918. }
  919. node = new_node;
  920. binder_stats_created(BINDER_STAT_NODE);
  921. node->tmp_refs++;
  922. rb_link_node(&node->rb_node, parent, p);
  923. rb_insert_color(&node->rb_node, &proc->nodes);
  924. node->debug_id = atomic_inc_return(&binder_last_id);
  925. node->proc = proc;
  926. node->ptr = ptr;
  927. node->cookie = cookie;
  928. node->work.type = BINDER_WORK_NODE;
  929. node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
  930. node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
  931. spin_lock_init(&node->lock);
  932. INIT_LIST_HEAD(&node->work.entry);
  933. INIT_LIST_HEAD(&node->async_todo);
  934. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  935. "%d:%d node %d u%016llx c%016llx created\n",
  936. proc->pid, current->pid, node->debug_id,
  937. (u64)node->ptr, (u64)node->cookie);
  938. return node;
  939. }
  940. static struct binder_node *binder_new_node(struct binder_proc *proc,
  941. struct flat_binder_object *fp)
  942. {
  943. struct binder_node *node;
  944. struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
  945. if (!new_node)
  946. return NULL;
  947. binder_inner_proc_lock(proc);
  948. node = binder_init_node_ilocked(proc, new_node, fp);
  949. binder_inner_proc_unlock(proc);
  950. if (node != new_node)
  951. /*
  952. * The node was already added by another thread
  953. */
  954. kfree(new_node);
  955. return node;
  956. }
  957. static void binder_free_node(struct binder_node *node)
  958. {
  959. kfree(node);
  960. binder_stats_deleted(BINDER_STAT_NODE);
  961. }
  962. static int binder_inc_node_nilocked(struct binder_node *node, int strong,
  963. int internal,
  964. struct list_head *target_list)
  965. {
  966. struct binder_proc *proc = node->proc;
  967. BUG_ON(!spin_is_locked(&node->lock));
  968. if (proc)
  969. BUG_ON(!spin_is_locked(&proc->inner_lock));
  970. if (strong) {
  971. if (internal) {
  972. if (target_list == NULL &&
  973. node->internal_strong_refs == 0 &&
  974. !(node->proc &&
  975. node == node->proc->context->binder_context_mgr_node &&
  976. node->has_strong_ref)) {
  977. pr_err("invalid inc strong node for %d\n",
  978. node->debug_id);
  979. return -EINVAL;
  980. }
  981. node->internal_strong_refs++;
  982. } else
  983. node->local_strong_refs++;
  984. if (!node->has_strong_ref && target_list) {
  985. binder_dequeue_work_ilocked(&node->work);
  986. binder_enqueue_work_ilocked(&node->work, target_list);
  987. }
  988. } else {
  989. if (!internal)
  990. node->local_weak_refs++;
  991. if (!node->has_weak_ref && list_empty(&node->work.entry)) {
  992. if (target_list == NULL) {
  993. pr_err("invalid inc weak node for %d\n",
  994. node->debug_id);
  995. return -EINVAL;
  996. }
  997. binder_enqueue_work_ilocked(&node->work, target_list);
  998. }
  999. }
  1000. return 0;
  1001. }
  1002. static int binder_inc_node(struct binder_node *node, int strong, int internal,
  1003. struct list_head *target_list)
  1004. {
  1005. int ret;
  1006. binder_node_inner_lock(node);
  1007. ret = binder_inc_node_nilocked(node, strong, internal, target_list);
  1008. binder_node_inner_unlock(node);
  1009. return ret;
  1010. }
  1011. static bool binder_dec_node_nilocked(struct binder_node *node,
  1012. int strong, int internal)
  1013. {
  1014. struct binder_proc *proc = node->proc;
  1015. BUG_ON(!spin_is_locked(&node->lock));
  1016. if (proc)
  1017. BUG_ON(!spin_is_locked(&proc->inner_lock));
  1018. if (strong) {
  1019. if (internal)
  1020. node->internal_strong_refs--;
  1021. else
  1022. node->local_strong_refs--;
  1023. if (node->local_strong_refs || node->internal_strong_refs)
  1024. return false;
  1025. } else {
  1026. if (!internal)
  1027. node->local_weak_refs--;
  1028. if (node->local_weak_refs || node->tmp_refs ||
  1029. !hlist_empty(&node->refs))
  1030. return false;
  1031. }
  1032. if (proc && (node->has_strong_ref || node->has_weak_ref)) {
  1033. if (list_empty(&node->work.entry)) {
  1034. binder_enqueue_work_ilocked(&node->work, &proc->todo);
  1035. wake_up_interruptible(&node->proc->wait);
  1036. }
  1037. } else {
  1038. if (hlist_empty(&node->refs) && !node->local_strong_refs &&
  1039. !node->local_weak_refs && !node->tmp_refs) {
  1040. if (proc) {
  1041. binder_dequeue_work_ilocked(&node->work);
  1042. rb_erase(&node->rb_node, &proc->nodes);
  1043. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1044. "refless node %d deleted\n",
  1045. node->debug_id);
  1046. } else {
  1047. BUG_ON(!list_empty(&node->work.entry));
  1048. spin_lock(&binder_dead_nodes_lock);
  1049. /*
  1050. * tmp_refs could have changed so
  1051. * check it again
  1052. */
  1053. if (node->tmp_refs) {
  1054. spin_unlock(&binder_dead_nodes_lock);
  1055. return false;
  1056. }
  1057. hlist_del(&node->dead_node);
  1058. spin_unlock(&binder_dead_nodes_lock);
  1059. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1060. "dead node %d deleted\n",
  1061. node->debug_id);
  1062. }
  1063. return true;
  1064. }
  1065. }
  1066. return false;
  1067. }
  1068. static void binder_dec_node(struct binder_node *node, int strong, int internal)
  1069. {
  1070. bool free_node;
  1071. binder_node_inner_lock(node);
  1072. free_node = binder_dec_node_nilocked(node, strong, internal);
  1073. binder_node_inner_unlock(node);
  1074. if (free_node)
  1075. binder_free_node(node);
  1076. }
  1077. static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
  1078. {
  1079. /*
  1080. * No call to binder_inc_node() is needed since we
  1081. * don't need to inform userspace of any changes to
  1082. * tmp_refs
  1083. */
  1084. node->tmp_refs++;
  1085. }
  1086. /**
  1087. * binder_inc_node_tmpref() - take a temporary reference on node
  1088. * @node: node to reference
  1089. *
  1090. * Take reference on node to prevent the node from being freed
  1091. * while referenced only by a local variable. The inner lock is
  1092. * needed to serialize with the node work on the queue (which
  1093. * isn't needed after the node is dead). If the node is dead
  1094. * (node->proc is NULL), use binder_dead_nodes_lock to protect
  1095. * node->tmp_refs against dead-node-only cases where the node
  1096. * lock cannot be acquired (eg traversing the dead node list to
  1097. * print nodes)
  1098. */
  1099. static void binder_inc_node_tmpref(struct binder_node *node)
  1100. {
  1101. binder_node_lock(node);
  1102. if (node->proc)
  1103. binder_inner_proc_lock(node->proc);
  1104. else
  1105. spin_lock(&binder_dead_nodes_lock);
  1106. binder_inc_node_tmpref_ilocked(node);
  1107. if (node->proc)
  1108. binder_inner_proc_unlock(node->proc);
  1109. else
  1110. spin_unlock(&binder_dead_nodes_lock);
  1111. binder_node_unlock(node);
  1112. }
  1113. /**
  1114. * binder_dec_node_tmpref() - remove a temporary reference on node
  1115. * @node: node to reference
  1116. *
  1117. * Release temporary reference on node taken via binder_inc_node_tmpref()
  1118. */
  1119. static void binder_dec_node_tmpref(struct binder_node *node)
  1120. {
  1121. bool free_node;
  1122. binder_node_inner_lock(node);
  1123. if (!node->proc)
  1124. spin_lock(&binder_dead_nodes_lock);
  1125. node->tmp_refs--;
  1126. BUG_ON(node->tmp_refs < 0);
  1127. if (!node->proc)
  1128. spin_unlock(&binder_dead_nodes_lock);
  1129. /*
  1130. * Call binder_dec_node() to check if all refcounts are 0
  1131. * and cleanup is needed. Calling with strong=0 and internal=1
  1132. * causes no actual reference to be released in binder_dec_node().
  1133. * If that changes, a change is needed here too.
  1134. */
  1135. free_node = binder_dec_node_nilocked(node, 0, 1);
  1136. binder_node_inner_unlock(node);
  1137. if (free_node)
  1138. binder_free_node(node);
  1139. }
  1140. static void binder_put_node(struct binder_node *node)
  1141. {
  1142. binder_dec_node_tmpref(node);
  1143. }
  1144. static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
  1145. u32 desc, bool need_strong_ref)
  1146. {
  1147. struct rb_node *n = proc->refs_by_desc.rb_node;
  1148. struct binder_ref *ref;
  1149. while (n) {
  1150. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  1151. if (desc < ref->data.desc) {
  1152. n = n->rb_left;
  1153. } else if (desc > ref->data.desc) {
  1154. n = n->rb_right;
  1155. } else if (need_strong_ref && !ref->data.strong) {
  1156. binder_user_error("tried to use weak ref as strong ref\n");
  1157. return NULL;
  1158. } else {
  1159. return ref;
  1160. }
  1161. }
  1162. return NULL;
  1163. }
  1164. /**
  1165. * binder_get_ref_for_node_olocked() - get the ref associated with given node
  1166. * @proc: binder_proc that owns the ref
  1167. * @node: binder_node of target
  1168. * @new_ref: newly allocated binder_ref to be initialized or %NULL
  1169. *
  1170. * Look up the ref for the given node and return it if it exists
  1171. *
  1172. * If it doesn't exist and the caller provides a newly allocated
  1173. * ref, initialize the fields of the newly allocated ref and insert
  1174. * into the given proc rb_trees and node refs list.
  1175. *
  1176. * Return: the ref for node. It is possible that another thread
  1177. * allocated/initialized the ref first in which case the
  1178. * returned ref would be different than the passed-in
  1179. * new_ref. new_ref must be kfree'd by the caller in
  1180. * this case.
  1181. */
  1182. static struct binder_ref *binder_get_ref_for_node_olocked(
  1183. struct binder_proc *proc,
  1184. struct binder_node *node,
  1185. struct binder_ref *new_ref)
  1186. {
  1187. struct binder_context *context = proc->context;
  1188. struct rb_node **p = &proc->refs_by_node.rb_node;
  1189. struct rb_node *parent = NULL;
  1190. struct binder_ref *ref;
  1191. struct rb_node *n;
  1192. while (*p) {
  1193. parent = *p;
  1194. ref = rb_entry(parent, struct binder_ref, rb_node_node);
  1195. if (node < ref->node)
  1196. p = &(*p)->rb_left;
  1197. else if (node > ref->node)
  1198. p = &(*p)->rb_right;
  1199. else
  1200. return ref;
  1201. }
  1202. if (!new_ref)
  1203. return NULL;
  1204. binder_stats_created(BINDER_STAT_REF);
  1205. new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
  1206. new_ref->proc = proc;
  1207. new_ref->node = node;
  1208. rb_link_node(&new_ref->rb_node_node, parent, p);
  1209. rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
  1210. new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
  1211. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  1212. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  1213. if (ref->data.desc > new_ref->data.desc)
  1214. break;
  1215. new_ref->data.desc = ref->data.desc + 1;
  1216. }
  1217. p = &proc->refs_by_desc.rb_node;
  1218. while (*p) {
  1219. parent = *p;
  1220. ref = rb_entry(parent, struct binder_ref, rb_node_desc);
  1221. if (new_ref->data.desc < ref->data.desc)
  1222. p = &(*p)->rb_left;
  1223. else if (new_ref->data.desc > ref->data.desc)
  1224. p = &(*p)->rb_right;
  1225. else
  1226. BUG();
  1227. }
  1228. rb_link_node(&new_ref->rb_node_desc, parent, p);
  1229. rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
  1230. binder_node_lock(node);
  1231. hlist_add_head(&new_ref->node_entry, &node->refs);
  1232. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1233. "%d new ref %d desc %d for node %d\n",
  1234. proc->pid, new_ref->data.debug_id, new_ref->data.desc,
  1235. node->debug_id);
  1236. binder_node_unlock(node);
  1237. return new_ref;
  1238. }
  1239. static void binder_cleanup_ref_olocked(struct binder_ref *ref)
  1240. {
  1241. bool delete_node = false;
  1242. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  1243. "%d delete ref %d desc %d for node %d\n",
  1244. ref->proc->pid, ref->data.debug_id, ref->data.desc,
  1245. ref->node->debug_id);
  1246. rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
  1247. rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
  1248. binder_node_inner_lock(ref->node);
  1249. if (ref->data.strong)
  1250. binder_dec_node_nilocked(ref->node, 1, 1);
  1251. hlist_del(&ref->node_entry);
  1252. delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
  1253. binder_node_inner_unlock(ref->node);
  1254. /*
  1255. * Clear ref->node unless we want the caller to free the node
  1256. */
  1257. if (!delete_node) {
  1258. /*
  1259. * The caller uses ref->node to determine
  1260. * whether the node needs to be freed. Clear
  1261. * it since the node is still alive.
  1262. */
  1263. ref->node = NULL;
  1264. }
  1265. if (ref->death) {
  1266. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1267. "%d delete ref %d desc %d has death notification\n",
  1268. ref->proc->pid, ref->data.debug_id,
  1269. ref->data.desc);
  1270. binder_dequeue_work(ref->proc, &ref->death->work);
  1271. binder_stats_deleted(BINDER_STAT_DEATH);
  1272. }
  1273. binder_stats_deleted(BINDER_STAT_REF);
  1274. }
  1275. /**
  1276. * binder_inc_ref_olocked() - increment the ref for given handle
  1277. * @ref: ref to be incremented
  1278. * @strong: if true, strong increment, else weak
  1279. * @target_list: list to queue node work on
  1280. *
  1281. * Increment the ref. @ref->proc->outer_lock must be held on entry
  1282. *
  1283. * Return: 0, if successful, else errno
  1284. */
  1285. static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
  1286. struct list_head *target_list)
  1287. {
  1288. int ret;
  1289. if (strong) {
  1290. if (ref->data.strong == 0) {
  1291. ret = binder_inc_node(ref->node, 1, 1, target_list);
  1292. if (ret)
  1293. return ret;
  1294. }
  1295. ref->data.strong++;
  1296. } else {
  1297. if (ref->data.weak == 0) {
  1298. ret = binder_inc_node(ref->node, 0, 1, target_list);
  1299. if (ret)
  1300. return ret;
  1301. }
  1302. ref->data.weak++;
  1303. }
  1304. return 0;
  1305. }
  1306. /**
  1307. * binder_dec_ref() - dec the ref for given handle
  1308. * @ref: ref to be decremented
  1309. * @strong: if true, strong decrement, else weak
  1310. *
  1311. * Decrement the ref.
  1312. *
  1313. * Return: true if ref is cleaned up and ready to be freed
  1314. */
  1315. static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
  1316. {
  1317. if (strong) {
  1318. if (ref->data.strong == 0) {
  1319. binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
  1320. ref->proc->pid, ref->data.debug_id,
  1321. ref->data.desc, ref->data.strong,
  1322. ref->data.weak);
  1323. return false;
  1324. }
  1325. ref->data.strong--;
  1326. if (ref->data.strong == 0)
  1327. binder_dec_node(ref->node, strong, 1);
  1328. } else {
  1329. if (ref->data.weak == 0) {
  1330. binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
  1331. ref->proc->pid, ref->data.debug_id,
  1332. ref->data.desc, ref->data.strong,
  1333. ref->data.weak);
  1334. return false;
  1335. }
  1336. ref->data.weak--;
  1337. }
  1338. if (ref->data.strong == 0 && ref->data.weak == 0) {
  1339. binder_cleanup_ref_olocked(ref);
  1340. return true;
  1341. }
  1342. return false;
  1343. }
  1344. /**
  1345. * binder_get_node_from_ref() - get the node from the given proc/desc
  1346. * @proc: proc containing the ref
  1347. * @desc: the handle associated with the ref
  1348. * @need_strong_ref: if true, only return node if ref is strong
  1349. * @rdata: the id/refcount data for the ref
  1350. *
  1351. * Given a proc and ref handle, return the associated binder_node
  1352. *
  1353. * Return: a binder_node or NULL if not found or not strong when strong required
  1354. */
  1355. static struct binder_node *binder_get_node_from_ref(
  1356. struct binder_proc *proc,
  1357. u32 desc, bool need_strong_ref,
  1358. struct binder_ref_data *rdata)
  1359. {
  1360. struct binder_node *node;
  1361. struct binder_ref *ref;
  1362. binder_proc_lock(proc);
  1363. ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
  1364. if (!ref)
  1365. goto err_no_ref;
  1366. node = ref->node;
  1367. /*
  1368. * Take an implicit reference on the node to ensure
  1369. * it stays alive until the call to binder_put_node()
  1370. */
  1371. binder_inc_node_tmpref(node);
  1372. if (rdata)
  1373. *rdata = ref->data;
  1374. binder_proc_unlock(proc);
  1375. return node;
  1376. err_no_ref:
  1377. binder_proc_unlock(proc);
  1378. return NULL;
  1379. }
  1380. /**
  1381. * binder_free_ref() - free the binder_ref
  1382. * @ref: ref to free
  1383. *
  1384. * Free the binder_ref. Free the binder_node indicated by ref->node
  1385. * (if non-NULL) and the binder_ref_death indicated by ref->death.
  1386. */
  1387. static void binder_free_ref(struct binder_ref *ref)
  1388. {
  1389. if (ref->node)
  1390. binder_free_node(ref->node);
  1391. kfree(ref->death);
  1392. kfree(ref);
  1393. }
  1394. /**
  1395. * binder_update_ref_for_handle() - inc/dec the ref for given handle
  1396. * @proc: proc containing the ref
  1397. * @desc: the handle associated with the ref
  1398. * @increment: true=inc reference, false=dec reference
  1399. * @strong: true=strong reference, false=weak reference
  1400. * @rdata: the id/refcount data for the ref
  1401. *
  1402. * Given a proc and ref handle, increment or decrement the ref
  1403. * according to "increment" arg.
  1404. *
  1405. * Return: 0 if successful, else errno
  1406. */
  1407. static int binder_update_ref_for_handle(struct binder_proc *proc,
  1408. uint32_t desc, bool increment, bool strong,
  1409. struct binder_ref_data *rdata)
  1410. {
  1411. int ret = 0;
  1412. struct binder_ref *ref;
  1413. bool delete_ref = false;
  1414. binder_proc_lock(proc);
  1415. ref = binder_get_ref_olocked(proc, desc, strong);
  1416. if (!ref) {
  1417. ret = -EINVAL;
  1418. goto err_no_ref;
  1419. }
  1420. if (increment)
  1421. ret = binder_inc_ref_olocked(ref, strong, NULL);
  1422. else
  1423. delete_ref = binder_dec_ref_olocked(ref, strong);
  1424. if (rdata)
  1425. *rdata = ref->data;
  1426. binder_proc_unlock(proc);
  1427. if (delete_ref)
  1428. binder_free_ref(ref);
  1429. return ret;
  1430. err_no_ref:
  1431. binder_proc_unlock(proc);
  1432. return ret;
  1433. }
  1434. /**
  1435. * binder_dec_ref_for_handle() - dec the ref for given handle
  1436. * @proc: proc containing the ref
  1437. * @desc: the handle associated with the ref
  1438. * @strong: true=strong reference, false=weak reference
  1439. * @rdata: the id/refcount data for the ref
  1440. *
  1441. * Just calls binder_update_ref_for_handle() to decrement the ref.
  1442. *
  1443. * Return: 0 if successful, else errno
  1444. */
  1445. static int binder_dec_ref_for_handle(struct binder_proc *proc,
  1446. uint32_t desc, bool strong, struct binder_ref_data *rdata)
  1447. {
  1448. return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
  1449. }
  1450. /**
  1451. * binder_inc_ref_for_node() - increment the ref for given proc/node
  1452. * @proc: proc containing the ref
  1453. * @node: target node
  1454. * @strong: true=strong reference, false=weak reference
  1455. * @target_list: worklist to use if node is incremented
  1456. * @rdata: the id/refcount data for the ref
  1457. *
  1458. * Given a proc and node, increment the ref. Create the ref if it
  1459. * doesn't already exist
  1460. *
  1461. * Return: 0 if successful, else errno
  1462. */
  1463. static int binder_inc_ref_for_node(struct binder_proc *proc,
  1464. struct binder_node *node,
  1465. bool strong,
  1466. struct list_head *target_list,
  1467. struct binder_ref_data *rdata)
  1468. {
  1469. struct binder_ref *ref;
  1470. struct binder_ref *new_ref = NULL;
  1471. int ret = 0;
  1472. binder_proc_lock(proc);
  1473. ref = binder_get_ref_for_node_olocked(proc, node, NULL);
  1474. if (!ref) {
  1475. binder_proc_unlock(proc);
  1476. new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  1477. if (!new_ref)
  1478. return -ENOMEM;
  1479. binder_proc_lock(proc);
  1480. ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
  1481. }
  1482. ret = binder_inc_ref_olocked(ref, strong, target_list);
  1483. *rdata = ref->data;
  1484. binder_proc_unlock(proc);
  1485. if (new_ref && ref != new_ref)
  1486. /*
  1487. * Another thread created the ref first so
  1488. * free the one we allocated
  1489. */
  1490. kfree(new_ref);
  1491. return ret;
  1492. }
  1493. static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
  1494. struct binder_transaction *t)
  1495. {
  1496. BUG_ON(!target_thread);
  1497. BUG_ON(!spin_is_locked(&target_thread->proc->inner_lock));
  1498. BUG_ON(target_thread->transaction_stack != t);
  1499. BUG_ON(target_thread->transaction_stack->from != target_thread);
  1500. target_thread->transaction_stack =
  1501. target_thread->transaction_stack->from_parent;
  1502. t->from = NULL;
  1503. }
  1504. /**
  1505. * binder_thread_dec_tmpref() - decrement thread->tmp_ref
  1506. * @thread: thread to decrement
  1507. *
  1508. * A thread needs to be kept alive while being used to create or
  1509. * handle a transaction. binder_get_txn_from() is used to safely
  1510. * extract t->from from a binder_transaction and keep the thread
  1511. * indicated by t->from from being freed. When done with that
  1512. * binder_thread, this function is called to decrement the
  1513. * tmp_ref and free if appropriate (thread has been released
  1514. * and no transaction being processed by the driver)
  1515. */
  1516. static void binder_thread_dec_tmpref(struct binder_thread *thread)
  1517. {
  1518. /*
  1519. * atomic is used to protect the counter value while
  1520. * it cannot reach zero or thread->is_dead is false
  1521. */
  1522. binder_inner_proc_lock(thread->proc);
  1523. atomic_dec(&thread->tmp_ref);
  1524. if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
  1525. binder_inner_proc_unlock(thread->proc);
  1526. binder_free_thread(thread);
  1527. return;
  1528. }
  1529. binder_inner_proc_unlock(thread->proc);
  1530. }
  1531. /**
  1532. * binder_proc_dec_tmpref() - decrement proc->tmp_ref
  1533. * @proc: proc to decrement
  1534. *
  1535. * A binder_proc needs to be kept alive while being used to create or
  1536. * handle a transaction. proc->tmp_ref is incremented when
  1537. * creating a new transaction or the binder_proc is currently in-use
  1538. * by threads that are being released. When done with the binder_proc,
  1539. * this function is called to decrement the counter and free the
  1540. * proc if appropriate (proc has been released, all threads have
  1541. * been released and not currenly in-use to process a transaction).
  1542. */
  1543. static void binder_proc_dec_tmpref(struct binder_proc *proc)
  1544. {
  1545. binder_inner_proc_lock(proc);
  1546. proc->tmp_ref--;
  1547. if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
  1548. !proc->tmp_ref) {
  1549. binder_inner_proc_unlock(proc);
  1550. binder_free_proc(proc);
  1551. return;
  1552. }
  1553. binder_inner_proc_unlock(proc);
  1554. }
  1555. /**
  1556. * binder_get_txn_from() - safely extract the "from" thread in transaction
  1557. * @t: binder transaction for t->from
  1558. *
  1559. * Atomically return the "from" thread and increment the tmp_ref
  1560. * count for the thread to ensure it stays alive until
  1561. * binder_thread_dec_tmpref() is called.
  1562. *
  1563. * Return: the value of t->from
  1564. */
  1565. static struct binder_thread *binder_get_txn_from(
  1566. struct binder_transaction *t)
  1567. {
  1568. struct binder_thread *from;
  1569. spin_lock(&t->lock);
  1570. from = t->from;
  1571. if (from)
  1572. atomic_inc(&from->tmp_ref);
  1573. spin_unlock(&t->lock);
  1574. return from;
  1575. }
  1576. /**
  1577. * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
  1578. * @t: binder transaction for t->from
  1579. *
  1580. * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
  1581. * to guarantee that the thread cannot be released while operating on it.
  1582. * The caller must call binder_inner_proc_unlock() to release the inner lock
  1583. * as well as call binder_dec_thread_txn() to release the reference.
  1584. *
  1585. * Return: the value of t->from
  1586. */
  1587. static struct binder_thread *binder_get_txn_from_and_acq_inner(
  1588. struct binder_transaction *t)
  1589. {
  1590. struct binder_thread *from;
  1591. from = binder_get_txn_from(t);
  1592. if (!from)
  1593. return NULL;
  1594. binder_inner_proc_lock(from->proc);
  1595. if (t->from) {
  1596. BUG_ON(from != t->from);
  1597. return from;
  1598. }
  1599. binder_inner_proc_unlock(from->proc);
  1600. binder_thread_dec_tmpref(from);
  1601. return NULL;
  1602. }
  1603. static void binder_free_transaction(struct binder_transaction *t)
  1604. {
  1605. if (t->buffer)
  1606. t->buffer->transaction = NULL;
  1607. kfree(t);
  1608. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  1609. }
  1610. static void binder_send_failed_reply(struct binder_transaction *t,
  1611. uint32_t error_code)
  1612. {
  1613. struct binder_thread *target_thread;
  1614. struct binder_transaction *next;
  1615. BUG_ON(t->flags & TF_ONE_WAY);
  1616. while (1) {
  1617. target_thread = binder_get_txn_from_and_acq_inner(t);
  1618. if (target_thread) {
  1619. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1620. "send failed reply for transaction %d to %d:%d\n",
  1621. t->debug_id,
  1622. target_thread->proc->pid,
  1623. target_thread->pid);
  1624. binder_pop_transaction_ilocked(target_thread, t);
  1625. if (target_thread->reply_error.cmd == BR_OK) {
  1626. target_thread->reply_error.cmd = error_code;
  1627. binder_enqueue_work_ilocked(
  1628. &target_thread->reply_error.work,
  1629. &target_thread->todo);
  1630. wake_up_interruptible(&target_thread->wait);
  1631. } else {
  1632. WARN(1, "Unexpected reply error: %u\n",
  1633. target_thread->reply_error.cmd);
  1634. }
  1635. binder_inner_proc_unlock(target_thread->proc);
  1636. binder_thread_dec_tmpref(target_thread);
  1637. binder_free_transaction(t);
  1638. return;
  1639. }
  1640. next = t->from_parent;
  1641. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  1642. "send failed reply for transaction %d, target dead\n",
  1643. t->debug_id);
  1644. binder_free_transaction(t);
  1645. if (next == NULL) {
  1646. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1647. "reply failed, no target thread at root\n");
  1648. return;
  1649. }
  1650. t = next;
  1651. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  1652. "reply failed, no target thread -- retry %d\n",
  1653. t->debug_id);
  1654. }
  1655. }
  1656. /**
  1657. * binder_validate_object() - checks for a valid metadata object in a buffer.
  1658. * @buffer: binder_buffer that we're parsing.
  1659. * @offset: offset in the buffer at which to validate an object.
  1660. *
  1661. * Return: If there's a valid metadata object at @offset in @buffer, the
  1662. * size of that object. Otherwise, it returns zero.
  1663. */
  1664. static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
  1665. {
  1666. /* Check if we can read a header first */
  1667. struct binder_object_header *hdr;
  1668. size_t object_size = 0;
  1669. if (offset > buffer->data_size - sizeof(*hdr) ||
  1670. buffer->data_size < sizeof(*hdr) ||
  1671. !IS_ALIGNED(offset, sizeof(u32)))
  1672. return 0;
  1673. /* Ok, now see if we can read a complete object. */
  1674. hdr = (struct binder_object_header *)(buffer->data + offset);
  1675. switch (hdr->type) {
  1676. case BINDER_TYPE_BINDER:
  1677. case BINDER_TYPE_WEAK_BINDER:
  1678. case BINDER_TYPE_HANDLE:
  1679. case BINDER_TYPE_WEAK_HANDLE:
  1680. object_size = sizeof(struct flat_binder_object);
  1681. break;
  1682. case BINDER_TYPE_FD:
  1683. object_size = sizeof(struct binder_fd_object);
  1684. break;
  1685. case BINDER_TYPE_PTR:
  1686. object_size = sizeof(struct binder_buffer_object);
  1687. break;
  1688. case BINDER_TYPE_FDA:
  1689. object_size = sizeof(struct binder_fd_array_object);
  1690. break;
  1691. default:
  1692. return 0;
  1693. }
  1694. if (offset <= buffer->data_size - object_size &&
  1695. buffer->data_size >= object_size)
  1696. return object_size;
  1697. else
  1698. return 0;
  1699. }
  1700. /**
  1701. * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
  1702. * @b: binder_buffer containing the object
  1703. * @index: index in offset array at which the binder_buffer_object is
  1704. * located
  1705. * @start: points to the start of the offset array
  1706. * @num_valid: the number of valid offsets in the offset array
  1707. *
  1708. * Return: If @index is within the valid range of the offset array
  1709. * described by @start and @num_valid, and if there's a valid
  1710. * binder_buffer_object at the offset found in index @index
  1711. * of the offset array, that object is returned. Otherwise,
  1712. * %NULL is returned.
  1713. * Note that the offset found in index @index itself is not
  1714. * verified; this function assumes that @num_valid elements
  1715. * from @start were previously verified to have valid offsets.
  1716. */
  1717. static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
  1718. binder_size_t index,
  1719. binder_size_t *start,
  1720. binder_size_t num_valid)
  1721. {
  1722. struct binder_buffer_object *buffer_obj;
  1723. binder_size_t *offp;
  1724. if (index >= num_valid)
  1725. return NULL;
  1726. offp = start + index;
  1727. buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
  1728. if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
  1729. return NULL;
  1730. return buffer_obj;
  1731. }
  1732. /**
  1733. * binder_validate_fixup() - validates pointer/fd fixups happen in order.
  1734. * @b: transaction buffer
  1735. * @objects_start start of objects buffer
  1736. * @buffer: binder_buffer_object in which to fix up
  1737. * @offset: start offset in @buffer to fix up
  1738. * @last_obj: last binder_buffer_object that we fixed up in
  1739. * @last_min_offset: minimum fixup offset in @last_obj
  1740. *
  1741. * Return: %true if a fixup in buffer @buffer at offset @offset is
  1742. * allowed.
  1743. *
  1744. * For safety reasons, we only allow fixups inside a buffer to happen
  1745. * at increasing offsets; additionally, we only allow fixup on the last
  1746. * buffer object that was verified, or one of its parents.
  1747. *
  1748. * Example of what is allowed:
  1749. *
  1750. * A
  1751. * B (parent = A, offset = 0)
  1752. * C (parent = A, offset = 16)
  1753. * D (parent = C, offset = 0)
  1754. * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
  1755. *
  1756. * Examples of what is not allowed:
  1757. *
  1758. * Decreasing offsets within the same parent:
  1759. * A
  1760. * C (parent = A, offset = 16)
  1761. * B (parent = A, offset = 0) // decreasing offset within A
  1762. *
  1763. * Referring to a parent that wasn't the last object or any of its parents:
  1764. * A
  1765. * B (parent = A, offset = 0)
  1766. * C (parent = A, offset = 0)
  1767. * C (parent = A, offset = 16)
  1768. * D (parent = B, offset = 0) // B is not A or any of A's parents
  1769. */
  1770. static bool binder_validate_fixup(struct binder_buffer *b,
  1771. binder_size_t *objects_start,
  1772. struct binder_buffer_object *buffer,
  1773. binder_size_t fixup_offset,
  1774. struct binder_buffer_object *last_obj,
  1775. binder_size_t last_min_offset)
  1776. {
  1777. if (!last_obj) {
  1778. /* Nothing to fix up in */
  1779. return false;
  1780. }
  1781. while (last_obj != buffer) {
  1782. /*
  1783. * Safe to retrieve the parent of last_obj, since it
  1784. * was already previously verified by the driver.
  1785. */
  1786. if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
  1787. return false;
  1788. last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
  1789. last_obj = (struct binder_buffer_object *)
  1790. (b->data + *(objects_start + last_obj->parent));
  1791. }
  1792. return (fixup_offset >= last_min_offset);
  1793. }
  1794. static void binder_transaction_buffer_release(struct binder_proc *proc,
  1795. struct binder_buffer *buffer,
  1796. binder_size_t *failed_at)
  1797. {
  1798. binder_size_t *offp, *off_start, *off_end;
  1799. int debug_id = buffer->debug_id;
  1800. binder_debug(BINDER_DEBUG_TRANSACTION,
  1801. "%d buffer release %d, size %zd-%zd, failed at %p\n",
  1802. proc->pid, buffer->debug_id,
  1803. buffer->data_size, buffer->offsets_size, failed_at);
  1804. if (buffer->target_node)
  1805. binder_dec_node(buffer->target_node, 1, 0);
  1806. off_start = (binder_size_t *)(buffer->data +
  1807. ALIGN(buffer->data_size, sizeof(void *)));
  1808. if (failed_at)
  1809. off_end = failed_at;
  1810. else
  1811. off_end = (void *)off_start + buffer->offsets_size;
  1812. for (offp = off_start; offp < off_end; offp++) {
  1813. struct binder_object_header *hdr;
  1814. size_t object_size = binder_validate_object(buffer, *offp);
  1815. if (object_size == 0) {
  1816. pr_err("transaction release %d bad object at offset %lld, size %zd\n",
  1817. debug_id, (u64)*offp, buffer->data_size);
  1818. continue;
  1819. }
  1820. hdr = (struct binder_object_header *)(buffer->data + *offp);
  1821. switch (hdr->type) {
  1822. case BINDER_TYPE_BINDER:
  1823. case BINDER_TYPE_WEAK_BINDER: {
  1824. struct flat_binder_object *fp;
  1825. struct binder_node *node;
  1826. fp = to_flat_binder_object(hdr);
  1827. node = binder_get_node(proc, fp->binder);
  1828. if (node == NULL) {
  1829. pr_err("transaction release %d bad node %016llx\n",
  1830. debug_id, (u64)fp->binder);
  1831. break;
  1832. }
  1833. binder_debug(BINDER_DEBUG_TRANSACTION,
  1834. " node %d u%016llx\n",
  1835. node->debug_id, (u64)node->ptr);
  1836. binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
  1837. 0);
  1838. binder_put_node(node);
  1839. } break;
  1840. case BINDER_TYPE_HANDLE:
  1841. case BINDER_TYPE_WEAK_HANDLE: {
  1842. struct flat_binder_object *fp;
  1843. struct binder_ref_data rdata;
  1844. int ret;
  1845. fp = to_flat_binder_object(hdr);
  1846. ret = binder_dec_ref_for_handle(proc, fp->handle,
  1847. hdr->type == BINDER_TYPE_HANDLE, &rdata);
  1848. if (ret) {
  1849. pr_err("transaction release %d bad handle %d, ret = %d\n",
  1850. debug_id, fp->handle, ret);
  1851. break;
  1852. }
  1853. binder_debug(BINDER_DEBUG_TRANSACTION,
  1854. " ref %d desc %d\n",
  1855. rdata.debug_id, rdata.desc);
  1856. } break;
  1857. case BINDER_TYPE_FD: {
  1858. struct binder_fd_object *fp = to_binder_fd_object(hdr);
  1859. binder_debug(BINDER_DEBUG_TRANSACTION,
  1860. " fd %d\n", fp->fd);
  1861. if (failed_at)
  1862. task_close_fd(proc, fp->fd);
  1863. } break;
  1864. case BINDER_TYPE_PTR:
  1865. /*
  1866. * Nothing to do here, this will get cleaned up when the
  1867. * transaction buffer gets freed
  1868. */
  1869. break;
  1870. case BINDER_TYPE_FDA: {
  1871. struct binder_fd_array_object *fda;
  1872. struct binder_buffer_object *parent;
  1873. uintptr_t parent_buffer;
  1874. u32 *fd_array;
  1875. size_t fd_index;
  1876. binder_size_t fd_buf_size;
  1877. fda = to_binder_fd_array_object(hdr);
  1878. parent = binder_validate_ptr(buffer, fda->parent,
  1879. off_start,
  1880. offp - off_start);
  1881. if (!parent) {
  1882. pr_err("transaction release %d bad parent offset",
  1883. debug_id);
  1884. continue;
  1885. }
  1886. /*
  1887. * Since the parent was already fixed up, convert it
  1888. * back to kernel address space to access it
  1889. */
  1890. parent_buffer = parent->buffer -
  1891. binder_alloc_get_user_buffer_offset(
  1892. &proc->alloc);
  1893. fd_buf_size = sizeof(u32) * fda->num_fds;
  1894. if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
  1895. pr_err("transaction release %d invalid number of fds (%lld)\n",
  1896. debug_id, (u64)fda->num_fds);
  1897. continue;
  1898. }
  1899. if (fd_buf_size > parent->length ||
  1900. fda->parent_offset > parent->length - fd_buf_size) {
  1901. /* No space for all file descriptors here. */
  1902. pr_err("transaction release %d not enough space for %lld fds in buffer\n",
  1903. debug_id, (u64)fda->num_fds);
  1904. continue;
  1905. }
  1906. fd_array = (u32 *)(parent_buffer + fda->parent_offset);
  1907. for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
  1908. task_close_fd(proc, fd_array[fd_index]);
  1909. } break;
  1910. default:
  1911. pr_err("transaction release %d bad object type %x\n",
  1912. debug_id, hdr->type);
  1913. break;
  1914. }
  1915. }
  1916. }
  1917. static int binder_translate_binder(struct flat_binder_object *fp,
  1918. struct binder_transaction *t,
  1919. struct binder_thread *thread)
  1920. {
  1921. struct binder_node *node;
  1922. struct binder_proc *proc = thread->proc;
  1923. struct binder_proc *target_proc = t->to_proc;
  1924. struct binder_ref_data rdata;
  1925. int ret = 0;
  1926. node = binder_get_node(proc, fp->binder);
  1927. if (!node) {
  1928. node = binder_new_node(proc, fp);
  1929. if (!node)
  1930. return -ENOMEM;
  1931. }
  1932. if (fp->cookie != node->cookie) {
  1933. binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
  1934. proc->pid, thread->pid, (u64)fp->binder,
  1935. node->debug_id, (u64)fp->cookie,
  1936. (u64)node->cookie);
  1937. ret = -EINVAL;
  1938. goto done;
  1939. }
  1940. if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
  1941. ret = -EPERM;
  1942. goto done;
  1943. }
  1944. ret = binder_inc_ref_for_node(target_proc, node,
  1945. fp->hdr.type == BINDER_TYPE_BINDER,
  1946. &thread->todo, &rdata);
  1947. if (ret)
  1948. goto done;
  1949. if (fp->hdr.type == BINDER_TYPE_BINDER)
  1950. fp->hdr.type = BINDER_TYPE_HANDLE;
  1951. else
  1952. fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
  1953. fp->binder = 0;
  1954. fp->handle = rdata.desc;
  1955. fp->cookie = 0;
  1956. trace_binder_transaction_node_to_ref(t, node, &rdata);
  1957. binder_debug(BINDER_DEBUG_TRANSACTION,
  1958. " node %d u%016llx -> ref %d desc %d\n",
  1959. node->debug_id, (u64)node->ptr,
  1960. rdata.debug_id, rdata.desc);
  1961. done:
  1962. binder_put_node(node);
  1963. return ret;
  1964. }
  1965. static int binder_translate_handle(struct flat_binder_object *fp,
  1966. struct binder_transaction *t,
  1967. struct binder_thread *thread)
  1968. {
  1969. struct binder_proc *proc = thread->proc;
  1970. struct binder_proc *target_proc = t->to_proc;
  1971. struct binder_node *node;
  1972. struct binder_ref_data src_rdata;
  1973. int ret = 0;
  1974. node = binder_get_node_from_ref(proc, fp->handle,
  1975. fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
  1976. if (!node) {
  1977. binder_user_error("%d:%d got transaction with invalid handle, %d\n",
  1978. proc->pid, thread->pid, fp->handle);
  1979. return -EINVAL;
  1980. }
  1981. if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
  1982. ret = -EPERM;
  1983. goto done;
  1984. }
  1985. binder_node_lock(node);
  1986. if (node->proc == target_proc) {
  1987. if (fp->hdr.type == BINDER_TYPE_HANDLE)
  1988. fp->hdr.type = BINDER_TYPE_BINDER;
  1989. else
  1990. fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
  1991. fp->binder = node->ptr;
  1992. fp->cookie = node->cookie;
  1993. if (node->proc)
  1994. binder_inner_proc_lock(node->proc);
  1995. binder_inc_node_nilocked(node,
  1996. fp->hdr.type == BINDER_TYPE_BINDER,
  1997. 0, NULL);
  1998. if (node->proc)
  1999. binder_inner_proc_unlock(node->proc);
  2000. trace_binder_transaction_ref_to_node(t, node, &src_rdata);
  2001. binder_debug(BINDER_DEBUG_TRANSACTION,
  2002. " ref %d desc %d -> node %d u%016llx\n",
  2003. src_rdata.debug_id, src_rdata.desc, node->debug_id,
  2004. (u64)node->ptr);
  2005. binder_node_unlock(node);
  2006. } else {
  2007. int ret;
  2008. struct binder_ref_data dest_rdata;
  2009. binder_node_unlock(node);
  2010. ret = binder_inc_ref_for_node(target_proc, node,
  2011. fp->hdr.type == BINDER_TYPE_HANDLE,
  2012. NULL, &dest_rdata);
  2013. if (ret)
  2014. goto done;
  2015. fp->binder = 0;
  2016. fp->handle = dest_rdata.desc;
  2017. fp->cookie = 0;
  2018. trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
  2019. &dest_rdata);
  2020. binder_debug(BINDER_DEBUG_TRANSACTION,
  2021. " ref %d desc %d -> ref %d desc %d (node %d)\n",
  2022. src_rdata.debug_id, src_rdata.desc,
  2023. dest_rdata.debug_id, dest_rdata.desc,
  2024. node->debug_id);
  2025. }
  2026. done:
  2027. binder_put_node(node);
  2028. return ret;
  2029. }
  2030. static int binder_translate_fd(int fd,
  2031. struct binder_transaction *t,
  2032. struct binder_thread *thread,
  2033. struct binder_transaction *in_reply_to)
  2034. {
  2035. struct binder_proc *proc = thread->proc;
  2036. struct binder_proc *target_proc = t->to_proc;
  2037. int target_fd;
  2038. struct file *file;
  2039. int ret;
  2040. bool target_allows_fd;
  2041. if (in_reply_to)
  2042. target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
  2043. else
  2044. target_allows_fd = t->buffer->target_node->accept_fds;
  2045. if (!target_allows_fd) {
  2046. binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
  2047. proc->pid, thread->pid,
  2048. in_reply_to ? "reply" : "transaction",
  2049. fd);
  2050. ret = -EPERM;
  2051. goto err_fd_not_accepted;
  2052. }
  2053. file = fget(fd);
  2054. if (!file) {
  2055. binder_user_error("%d:%d got transaction with invalid fd, %d\n",
  2056. proc->pid, thread->pid, fd);
  2057. ret = -EBADF;
  2058. goto err_fget;
  2059. }
  2060. ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
  2061. if (ret < 0) {
  2062. ret = -EPERM;
  2063. goto err_security;
  2064. }
  2065. target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
  2066. if (target_fd < 0) {
  2067. ret = -ENOMEM;
  2068. goto err_get_unused_fd;
  2069. }
  2070. task_fd_install(target_proc, target_fd, file);
  2071. trace_binder_transaction_fd(t, fd, target_fd);
  2072. binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
  2073. fd, target_fd);
  2074. return target_fd;
  2075. err_get_unused_fd:
  2076. err_security:
  2077. fput(file);
  2078. err_fget:
  2079. err_fd_not_accepted:
  2080. return ret;
  2081. }
  2082. static int binder_translate_fd_array(struct binder_fd_array_object *fda,
  2083. struct binder_buffer_object *parent,
  2084. struct binder_transaction *t,
  2085. struct binder_thread *thread,
  2086. struct binder_transaction *in_reply_to)
  2087. {
  2088. binder_size_t fdi, fd_buf_size, num_installed_fds;
  2089. int target_fd;
  2090. uintptr_t parent_buffer;
  2091. u32 *fd_array;
  2092. struct binder_proc *proc = thread->proc;
  2093. struct binder_proc *target_proc = t->to_proc;
  2094. fd_buf_size = sizeof(u32) * fda->num_fds;
  2095. if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
  2096. binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
  2097. proc->pid, thread->pid, (u64)fda->num_fds);
  2098. return -EINVAL;
  2099. }
  2100. if (fd_buf_size > parent->length ||
  2101. fda->parent_offset > parent->length - fd_buf_size) {
  2102. /* No space for all file descriptors here. */
  2103. binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
  2104. proc->pid, thread->pid, (u64)fda->num_fds);
  2105. return -EINVAL;
  2106. }
  2107. /*
  2108. * Since the parent was already fixed up, convert it
  2109. * back to the kernel address space to access it
  2110. */
  2111. parent_buffer = parent->buffer -
  2112. binder_alloc_get_user_buffer_offset(&target_proc->alloc);
  2113. fd_array = (u32 *)(parent_buffer + fda->parent_offset);
  2114. if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
  2115. binder_user_error("%d:%d parent offset not aligned correctly.\n",
  2116. proc->pid, thread->pid);
  2117. return -EINVAL;
  2118. }
  2119. for (fdi = 0; fdi < fda->num_fds; fdi++) {
  2120. target_fd = binder_translate_fd(fd_array[fdi], t, thread,
  2121. in_reply_to);
  2122. if (target_fd < 0)
  2123. goto err_translate_fd_failed;
  2124. fd_array[fdi] = target_fd;
  2125. }
  2126. return 0;
  2127. err_translate_fd_failed:
  2128. /*
  2129. * Failed to allocate fd or security error, free fds
  2130. * installed so far.
  2131. */
  2132. num_installed_fds = fdi;
  2133. for (fdi = 0; fdi < num_installed_fds; fdi++)
  2134. task_close_fd(target_proc, fd_array[fdi]);
  2135. return target_fd;
  2136. }
  2137. static int binder_fixup_parent(struct binder_transaction *t,
  2138. struct binder_thread *thread,
  2139. struct binder_buffer_object *bp,
  2140. binder_size_t *off_start,
  2141. binder_size_t num_valid,
  2142. struct binder_buffer_object *last_fixup_obj,
  2143. binder_size_t last_fixup_min_off)
  2144. {
  2145. struct binder_buffer_object *parent;
  2146. u8 *parent_buffer;
  2147. struct binder_buffer *b = t->buffer;
  2148. struct binder_proc *proc = thread->proc;
  2149. struct binder_proc *target_proc = t->to_proc;
  2150. if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
  2151. return 0;
  2152. parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
  2153. if (!parent) {
  2154. binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
  2155. proc->pid, thread->pid);
  2156. return -EINVAL;
  2157. }
  2158. if (!binder_validate_fixup(b, off_start,
  2159. parent, bp->parent_offset,
  2160. last_fixup_obj,
  2161. last_fixup_min_off)) {
  2162. binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
  2163. proc->pid, thread->pid);
  2164. return -EINVAL;
  2165. }
  2166. if (parent->length < sizeof(binder_uintptr_t) ||
  2167. bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
  2168. /* No space for a pointer here! */
  2169. binder_user_error("%d:%d got transaction with invalid parent offset\n",
  2170. proc->pid, thread->pid);
  2171. return -EINVAL;
  2172. }
  2173. parent_buffer = (u8 *)(parent->buffer -
  2174. binder_alloc_get_user_buffer_offset(
  2175. &target_proc->alloc));
  2176. *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
  2177. return 0;
  2178. }
  2179. static void binder_transaction(struct binder_proc *proc,
  2180. struct binder_thread *thread,
  2181. struct binder_transaction_data *tr, int reply,
  2182. binder_size_t extra_buffers_size)
  2183. {
  2184. int ret;
  2185. struct binder_transaction *t;
  2186. struct binder_work *tcomplete;
  2187. binder_size_t *offp, *off_end, *off_start;
  2188. binder_size_t off_min;
  2189. u8 *sg_bufp, *sg_buf_end;
  2190. struct binder_proc *target_proc = NULL;
  2191. struct binder_thread *target_thread = NULL;
  2192. struct binder_node *target_node = NULL;
  2193. struct list_head *target_list;
  2194. wait_queue_head_t *target_wait;
  2195. struct binder_transaction *in_reply_to = NULL;
  2196. struct binder_transaction_log_entry *e;
  2197. uint32_t return_error = 0;
  2198. uint32_t return_error_param = 0;
  2199. uint32_t return_error_line = 0;
  2200. struct binder_buffer_object *last_fixup_obj = NULL;
  2201. binder_size_t last_fixup_min_off = 0;
  2202. struct binder_context *context = proc->context;
  2203. int t_debug_id = atomic_inc_return(&binder_last_id);
  2204. e = binder_transaction_log_add(&binder_transaction_log);
  2205. e->debug_id = t_debug_id;
  2206. e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
  2207. e->from_proc = proc->pid;
  2208. e->from_thread = thread->pid;
  2209. e->target_handle = tr->target.handle;
  2210. e->data_size = tr->data_size;
  2211. e->offsets_size = tr->offsets_size;
  2212. e->context_name = proc->context->name;
  2213. if (reply) {
  2214. binder_inner_proc_lock(proc);
  2215. in_reply_to = thread->transaction_stack;
  2216. if (in_reply_to == NULL) {
  2217. binder_inner_proc_unlock(proc);
  2218. binder_user_error("%d:%d got reply transaction with no transaction stack\n",
  2219. proc->pid, thread->pid);
  2220. return_error = BR_FAILED_REPLY;
  2221. return_error_param = -EPROTO;
  2222. return_error_line = __LINE__;
  2223. goto err_empty_call_stack;
  2224. }
  2225. if (in_reply_to->to_thread != thread) {
  2226. spin_lock(&in_reply_to->lock);
  2227. binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
  2228. proc->pid, thread->pid, in_reply_to->debug_id,
  2229. in_reply_to->to_proc ?
  2230. in_reply_to->to_proc->pid : 0,
  2231. in_reply_to->to_thread ?
  2232. in_reply_to->to_thread->pid : 0);
  2233. spin_unlock(&in_reply_to->lock);
  2234. binder_inner_proc_unlock(proc);
  2235. return_error = BR_FAILED_REPLY;
  2236. return_error_param = -EPROTO;
  2237. return_error_line = __LINE__;
  2238. in_reply_to = NULL;
  2239. goto err_bad_call_stack;
  2240. }
  2241. thread->transaction_stack = in_reply_to->to_parent;
  2242. binder_inner_proc_unlock(proc);
  2243. binder_set_nice(in_reply_to->saved_priority);
  2244. target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
  2245. if (target_thread == NULL) {
  2246. return_error = BR_DEAD_REPLY;
  2247. return_error_line = __LINE__;
  2248. goto err_dead_binder;
  2249. }
  2250. if (target_thread->transaction_stack != in_reply_to) {
  2251. binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
  2252. proc->pid, thread->pid,
  2253. target_thread->transaction_stack ?
  2254. target_thread->transaction_stack->debug_id : 0,
  2255. in_reply_to->debug_id);
  2256. binder_inner_proc_unlock(target_thread->proc);
  2257. return_error = BR_FAILED_REPLY;
  2258. return_error_param = -EPROTO;
  2259. return_error_line = __LINE__;
  2260. in_reply_to = NULL;
  2261. target_thread = NULL;
  2262. goto err_dead_binder;
  2263. }
  2264. target_proc = target_thread->proc;
  2265. target_proc->tmp_ref++;
  2266. binder_inner_proc_unlock(target_thread->proc);
  2267. } else {
  2268. if (tr->target.handle) {
  2269. struct binder_ref *ref;
  2270. /*
  2271. * There must already be a strong ref
  2272. * on this node. If so, do a strong
  2273. * increment on the node to ensure it
  2274. * stays alive until the transaction is
  2275. * done.
  2276. */
  2277. binder_proc_lock(proc);
  2278. ref = binder_get_ref_olocked(proc, tr->target.handle,
  2279. true);
  2280. if (ref) {
  2281. binder_inc_node(ref->node, 1, 0, NULL);
  2282. target_node = ref->node;
  2283. }
  2284. binder_proc_unlock(proc);
  2285. if (target_node == NULL) {
  2286. binder_user_error("%d:%d got transaction to invalid handle\n",
  2287. proc->pid, thread->pid);
  2288. return_error = BR_FAILED_REPLY;
  2289. return_error_param = -EINVAL;
  2290. return_error_line = __LINE__;
  2291. goto err_invalid_target_handle;
  2292. }
  2293. } else {
  2294. mutex_lock(&context->context_mgr_node_lock);
  2295. target_node = context->binder_context_mgr_node;
  2296. if (target_node == NULL) {
  2297. return_error = BR_DEAD_REPLY;
  2298. mutex_unlock(&context->context_mgr_node_lock);
  2299. return_error_line = __LINE__;
  2300. goto err_no_context_mgr_node;
  2301. }
  2302. binder_inc_node(target_node, 1, 0, NULL);
  2303. mutex_unlock(&context->context_mgr_node_lock);
  2304. }
  2305. e->to_node = target_node->debug_id;
  2306. binder_node_lock(target_node);
  2307. target_proc = target_node->proc;
  2308. if (target_proc == NULL) {
  2309. binder_node_unlock(target_node);
  2310. return_error = BR_DEAD_REPLY;
  2311. return_error_line = __LINE__;
  2312. goto err_dead_binder;
  2313. }
  2314. binder_inner_proc_lock(target_proc);
  2315. target_proc->tmp_ref++;
  2316. binder_inner_proc_unlock(target_proc);
  2317. binder_node_unlock(target_node);
  2318. if (security_binder_transaction(proc->tsk,
  2319. target_proc->tsk) < 0) {
  2320. return_error = BR_FAILED_REPLY;
  2321. return_error_param = -EPERM;
  2322. return_error_line = __LINE__;
  2323. goto err_invalid_target_handle;
  2324. }
  2325. binder_inner_proc_lock(proc);
  2326. if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
  2327. struct binder_transaction *tmp;
  2328. tmp = thread->transaction_stack;
  2329. if (tmp->to_thread != thread) {
  2330. spin_lock(&tmp->lock);
  2331. binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
  2332. proc->pid, thread->pid, tmp->debug_id,
  2333. tmp->to_proc ? tmp->to_proc->pid : 0,
  2334. tmp->to_thread ?
  2335. tmp->to_thread->pid : 0);
  2336. spin_unlock(&tmp->lock);
  2337. binder_inner_proc_unlock(proc);
  2338. return_error = BR_FAILED_REPLY;
  2339. return_error_param = -EPROTO;
  2340. return_error_line = __LINE__;
  2341. goto err_bad_call_stack;
  2342. }
  2343. while (tmp) {
  2344. struct binder_thread *from;
  2345. spin_lock(&tmp->lock);
  2346. from = tmp->from;
  2347. if (from && from->proc == target_proc) {
  2348. atomic_inc(&from->tmp_ref);
  2349. target_thread = from;
  2350. spin_unlock(&tmp->lock);
  2351. break;
  2352. }
  2353. spin_unlock(&tmp->lock);
  2354. tmp = tmp->from_parent;
  2355. }
  2356. }
  2357. binder_inner_proc_unlock(proc);
  2358. }
  2359. if (target_thread) {
  2360. e->to_thread = target_thread->pid;
  2361. target_list = &target_thread->todo;
  2362. target_wait = &target_thread->wait;
  2363. } else {
  2364. target_list = &target_proc->todo;
  2365. target_wait = &target_proc->wait;
  2366. }
  2367. e->to_proc = target_proc->pid;
  2368. /* TODO: reuse incoming transaction for reply */
  2369. t = kzalloc(sizeof(*t), GFP_KERNEL);
  2370. if (t == NULL) {
  2371. return_error = BR_FAILED_REPLY;
  2372. return_error_param = -ENOMEM;
  2373. return_error_line = __LINE__;
  2374. goto err_alloc_t_failed;
  2375. }
  2376. binder_stats_created(BINDER_STAT_TRANSACTION);
  2377. spin_lock_init(&t->lock);
  2378. tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
  2379. if (tcomplete == NULL) {
  2380. return_error = BR_FAILED_REPLY;
  2381. return_error_param = -ENOMEM;
  2382. return_error_line = __LINE__;
  2383. goto err_alloc_tcomplete_failed;
  2384. }
  2385. binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
  2386. t->debug_id = t_debug_id;
  2387. if (reply)
  2388. binder_debug(BINDER_DEBUG_TRANSACTION,
  2389. "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
  2390. proc->pid, thread->pid, t->debug_id,
  2391. target_proc->pid, target_thread->pid,
  2392. (u64)tr->data.ptr.buffer,
  2393. (u64)tr->data.ptr.offsets,
  2394. (u64)tr->data_size, (u64)tr->offsets_size,
  2395. (u64)extra_buffers_size);
  2396. else
  2397. binder_debug(BINDER_DEBUG_TRANSACTION,
  2398. "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
  2399. proc->pid, thread->pid, t->debug_id,
  2400. target_proc->pid, target_node->debug_id,
  2401. (u64)tr->data.ptr.buffer,
  2402. (u64)tr->data.ptr.offsets,
  2403. (u64)tr->data_size, (u64)tr->offsets_size,
  2404. (u64)extra_buffers_size);
  2405. if (!reply && !(tr->flags & TF_ONE_WAY))
  2406. t->from = thread;
  2407. else
  2408. t->from = NULL;
  2409. t->sender_euid = task_euid(proc->tsk);
  2410. t->to_proc = target_proc;
  2411. t->to_thread = target_thread;
  2412. t->code = tr->code;
  2413. t->flags = tr->flags;
  2414. t->priority = task_nice(current);
  2415. trace_binder_transaction(reply, t, target_node);
  2416. t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
  2417. tr->offsets_size, extra_buffers_size,
  2418. !reply && (t->flags & TF_ONE_WAY));
  2419. if (IS_ERR(t->buffer)) {
  2420. /*
  2421. * -ESRCH indicates VMA cleared. The target is dying.
  2422. */
  2423. return_error_param = PTR_ERR(t->buffer);
  2424. return_error = return_error_param == -ESRCH ?
  2425. BR_DEAD_REPLY : BR_FAILED_REPLY;
  2426. return_error_line = __LINE__;
  2427. t->buffer = NULL;
  2428. goto err_binder_alloc_buf_failed;
  2429. }
  2430. t->buffer->allow_user_free = 0;
  2431. t->buffer->debug_id = t->debug_id;
  2432. t->buffer->transaction = t;
  2433. t->buffer->target_node = target_node;
  2434. trace_binder_transaction_alloc_buf(t->buffer);
  2435. off_start = (binder_size_t *)(t->buffer->data +
  2436. ALIGN(tr->data_size, sizeof(void *)));
  2437. offp = off_start;
  2438. if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
  2439. tr->data.ptr.buffer, tr->data_size)) {
  2440. binder_user_error("%d:%d got transaction with invalid data ptr\n",
  2441. proc->pid, thread->pid);
  2442. return_error = BR_FAILED_REPLY;
  2443. return_error_param = -EFAULT;
  2444. return_error_line = __LINE__;
  2445. goto err_copy_data_failed;
  2446. }
  2447. if (copy_from_user(offp, (const void __user *)(uintptr_t)
  2448. tr->data.ptr.offsets, tr->offsets_size)) {
  2449. binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
  2450. proc->pid, thread->pid);
  2451. return_error = BR_FAILED_REPLY;
  2452. return_error_param = -EFAULT;
  2453. return_error_line = __LINE__;
  2454. goto err_copy_data_failed;
  2455. }
  2456. if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
  2457. binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
  2458. proc->pid, thread->pid, (u64)tr->offsets_size);
  2459. return_error = BR_FAILED_REPLY;
  2460. return_error_param = -EINVAL;
  2461. return_error_line = __LINE__;
  2462. goto err_bad_offset;
  2463. }
  2464. if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
  2465. binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
  2466. proc->pid, thread->pid,
  2467. (u64)extra_buffers_size);
  2468. return_error = BR_FAILED_REPLY;
  2469. return_error_param = -EINVAL;
  2470. return_error_line = __LINE__;
  2471. goto err_bad_offset;
  2472. }
  2473. off_end = (void *)off_start + tr->offsets_size;
  2474. sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
  2475. sg_buf_end = sg_bufp + extra_buffers_size;
  2476. off_min = 0;
  2477. for (; offp < off_end; offp++) {
  2478. struct binder_object_header *hdr;
  2479. size_t object_size = binder_validate_object(t->buffer, *offp);
  2480. if (object_size == 0 || *offp < off_min) {
  2481. binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
  2482. proc->pid, thread->pid, (u64)*offp,
  2483. (u64)off_min,
  2484. (u64)t->buffer->data_size);
  2485. return_error = BR_FAILED_REPLY;
  2486. return_error_param = -EINVAL;
  2487. return_error_line = __LINE__;
  2488. goto err_bad_offset;
  2489. }
  2490. hdr = (struct binder_object_header *)(t->buffer->data + *offp);
  2491. off_min = *offp + object_size;
  2492. switch (hdr->type) {
  2493. case BINDER_TYPE_BINDER:
  2494. case BINDER_TYPE_WEAK_BINDER: {
  2495. struct flat_binder_object *fp;
  2496. fp = to_flat_binder_object(hdr);
  2497. ret = binder_translate_binder(fp, t, thread);
  2498. if (ret < 0) {
  2499. return_error = BR_FAILED_REPLY;
  2500. return_error_param = ret;
  2501. return_error_line = __LINE__;
  2502. goto err_translate_failed;
  2503. }
  2504. } break;
  2505. case BINDER_TYPE_HANDLE:
  2506. case BINDER_TYPE_WEAK_HANDLE: {
  2507. struct flat_binder_object *fp;
  2508. fp = to_flat_binder_object(hdr);
  2509. ret = binder_translate_handle(fp, t, thread);
  2510. if (ret < 0) {
  2511. return_error = BR_FAILED_REPLY;
  2512. return_error_param = ret;
  2513. return_error_line = __LINE__;
  2514. goto err_translate_failed;
  2515. }
  2516. } break;
  2517. case BINDER_TYPE_FD: {
  2518. struct binder_fd_object *fp = to_binder_fd_object(hdr);
  2519. int target_fd = binder_translate_fd(fp->fd, t, thread,
  2520. in_reply_to);
  2521. if (target_fd < 0) {
  2522. return_error = BR_FAILED_REPLY;
  2523. return_error_param = target_fd;
  2524. return_error_line = __LINE__;
  2525. goto err_translate_failed;
  2526. }
  2527. fp->pad_binder = 0;
  2528. fp->fd = target_fd;
  2529. } break;
  2530. case BINDER_TYPE_FDA: {
  2531. struct binder_fd_array_object *fda =
  2532. to_binder_fd_array_object(hdr);
  2533. struct binder_buffer_object *parent =
  2534. binder_validate_ptr(t->buffer, fda->parent,
  2535. off_start,
  2536. offp - off_start);
  2537. if (!parent) {
  2538. binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
  2539. proc->pid, thread->pid);
  2540. return_error = BR_FAILED_REPLY;
  2541. return_error_param = -EINVAL;
  2542. return_error_line = __LINE__;
  2543. goto err_bad_parent;
  2544. }
  2545. if (!binder_validate_fixup(t->buffer, off_start,
  2546. parent, fda->parent_offset,
  2547. last_fixup_obj,
  2548. last_fixup_min_off)) {
  2549. binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
  2550. proc->pid, thread->pid);
  2551. return_error = BR_FAILED_REPLY;
  2552. return_error_param = -EINVAL;
  2553. return_error_line = __LINE__;
  2554. goto err_bad_parent;
  2555. }
  2556. ret = binder_translate_fd_array(fda, parent, t, thread,
  2557. in_reply_to);
  2558. if (ret < 0) {
  2559. return_error = BR_FAILED_REPLY;
  2560. return_error_param = ret;
  2561. return_error_line = __LINE__;
  2562. goto err_translate_failed;
  2563. }
  2564. last_fixup_obj = parent;
  2565. last_fixup_min_off =
  2566. fda->parent_offset + sizeof(u32) * fda->num_fds;
  2567. } break;
  2568. case BINDER_TYPE_PTR: {
  2569. struct binder_buffer_object *bp =
  2570. to_binder_buffer_object(hdr);
  2571. size_t buf_left = sg_buf_end - sg_bufp;
  2572. if (bp->length > buf_left) {
  2573. binder_user_error("%d:%d got transaction with too large buffer\n",
  2574. proc->pid, thread->pid);
  2575. return_error = BR_FAILED_REPLY;
  2576. return_error_param = -EINVAL;
  2577. return_error_line = __LINE__;
  2578. goto err_bad_offset;
  2579. }
  2580. if (copy_from_user(sg_bufp,
  2581. (const void __user *)(uintptr_t)
  2582. bp->buffer, bp->length)) {
  2583. binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
  2584. proc->pid, thread->pid);
  2585. return_error_param = -EFAULT;
  2586. return_error = BR_FAILED_REPLY;
  2587. return_error_line = __LINE__;
  2588. goto err_copy_data_failed;
  2589. }
  2590. /* Fixup buffer pointer to target proc address space */
  2591. bp->buffer = (uintptr_t)sg_bufp +
  2592. binder_alloc_get_user_buffer_offset(
  2593. &target_proc->alloc);
  2594. sg_bufp += ALIGN(bp->length, sizeof(u64));
  2595. ret = binder_fixup_parent(t, thread, bp, off_start,
  2596. offp - off_start,
  2597. last_fixup_obj,
  2598. last_fixup_min_off);
  2599. if (ret < 0) {
  2600. return_error = BR_FAILED_REPLY;
  2601. return_error_param = ret;
  2602. return_error_line = __LINE__;
  2603. goto err_translate_failed;
  2604. }
  2605. last_fixup_obj = bp;
  2606. last_fixup_min_off = 0;
  2607. } break;
  2608. default:
  2609. binder_user_error("%d:%d got transaction with invalid object type, %x\n",
  2610. proc->pid, thread->pid, hdr->type);
  2611. return_error = BR_FAILED_REPLY;
  2612. return_error_param = -EINVAL;
  2613. return_error_line = __LINE__;
  2614. goto err_bad_object_type;
  2615. }
  2616. }
  2617. tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
  2618. binder_enqueue_work(proc, tcomplete, &thread->todo);
  2619. t->work.type = BINDER_WORK_TRANSACTION;
  2620. if (reply) {
  2621. binder_inner_proc_lock(target_proc);
  2622. if (target_thread->is_dead) {
  2623. binder_inner_proc_unlock(target_proc);
  2624. goto err_dead_proc_or_thread;
  2625. }
  2626. BUG_ON(t->buffer->async_transaction != 0);
  2627. binder_pop_transaction_ilocked(target_thread, in_reply_to);
  2628. binder_enqueue_work_ilocked(&t->work, target_list);
  2629. binder_inner_proc_unlock(target_proc);
  2630. binder_free_transaction(in_reply_to);
  2631. } else if (!(t->flags & TF_ONE_WAY)) {
  2632. BUG_ON(t->buffer->async_transaction != 0);
  2633. binder_inner_proc_lock(proc);
  2634. t->need_reply = 1;
  2635. t->from_parent = thread->transaction_stack;
  2636. thread->transaction_stack = t;
  2637. binder_inner_proc_unlock(proc);
  2638. binder_inner_proc_lock(target_proc);
  2639. if (target_proc->is_dead ||
  2640. (target_thread && target_thread->is_dead)) {
  2641. binder_inner_proc_unlock(target_proc);
  2642. binder_inner_proc_lock(proc);
  2643. binder_pop_transaction_ilocked(thread, t);
  2644. binder_inner_proc_unlock(proc);
  2645. goto err_dead_proc_or_thread;
  2646. }
  2647. binder_enqueue_work_ilocked(&t->work, target_list);
  2648. binder_inner_proc_unlock(target_proc);
  2649. } else {
  2650. BUG_ON(target_node == NULL);
  2651. BUG_ON(t->buffer->async_transaction != 1);
  2652. binder_node_lock(target_node);
  2653. if (target_node->has_async_transaction) {
  2654. target_list = &target_node->async_todo;
  2655. target_wait = NULL;
  2656. } else
  2657. target_node->has_async_transaction = 1;
  2658. /*
  2659. * Test/set of has_async_transaction
  2660. * must be atomic with enqueue on
  2661. * async_todo
  2662. */
  2663. binder_inner_proc_lock(target_proc);
  2664. if (target_proc->is_dead ||
  2665. (target_thread && target_thread->is_dead)) {
  2666. binder_inner_proc_unlock(target_proc);
  2667. binder_node_unlock(target_node);
  2668. goto err_dead_proc_or_thread;
  2669. }
  2670. binder_enqueue_work_ilocked(&t->work, target_list);
  2671. binder_inner_proc_unlock(target_proc);
  2672. binder_node_unlock(target_node);
  2673. }
  2674. if (target_wait) {
  2675. if (reply || !(tr->flags & TF_ONE_WAY))
  2676. wake_up_interruptible_sync(target_wait);
  2677. else
  2678. wake_up_interruptible(target_wait);
  2679. }
  2680. if (target_thread)
  2681. binder_thread_dec_tmpref(target_thread);
  2682. binder_proc_dec_tmpref(target_proc);
  2683. /*
  2684. * write barrier to synchronize with initialization
  2685. * of log entry
  2686. */
  2687. smp_wmb();
  2688. WRITE_ONCE(e->debug_id_done, t_debug_id);
  2689. return;
  2690. err_dead_proc_or_thread:
  2691. return_error = BR_DEAD_REPLY;
  2692. return_error_line = __LINE__;
  2693. err_translate_failed:
  2694. err_bad_object_type:
  2695. err_bad_offset:
  2696. err_bad_parent:
  2697. err_copy_data_failed:
  2698. trace_binder_transaction_failed_buffer_release(t->buffer);
  2699. binder_transaction_buffer_release(target_proc, t->buffer, offp);
  2700. target_node = NULL;
  2701. t->buffer->transaction = NULL;
  2702. binder_alloc_free_buf(&target_proc->alloc, t->buffer);
  2703. err_binder_alloc_buf_failed:
  2704. kfree(tcomplete);
  2705. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  2706. err_alloc_tcomplete_failed:
  2707. kfree(t);
  2708. binder_stats_deleted(BINDER_STAT_TRANSACTION);
  2709. err_alloc_t_failed:
  2710. err_bad_call_stack:
  2711. err_empty_call_stack:
  2712. err_dead_binder:
  2713. err_invalid_target_handle:
  2714. err_no_context_mgr_node:
  2715. if (target_thread)
  2716. binder_thread_dec_tmpref(target_thread);
  2717. if (target_proc)
  2718. binder_proc_dec_tmpref(target_proc);
  2719. if (target_node)
  2720. binder_dec_node(target_node, 1, 0);
  2721. binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
  2722. "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
  2723. proc->pid, thread->pid, return_error, return_error_param,
  2724. (u64)tr->data_size, (u64)tr->offsets_size,
  2725. return_error_line);
  2726. {
  2727. struct binder_transaction_log_entry *fe;
  2728. e->return_error = return_error;
  2729. e->return_error_param = return_error_param;
  2730. e->return_error_line = return_error_line;
  2731. fe = binder_transaction_log_add(&binder_transaction_log_failed);
  2732. *fe = *e;
  2733. /*
  2734. * write barrier to synchronize with initialization
  2735. * of log entry
  2736. */
  2737. smp_wmb();
  2738. WRITE_ONCE(e->debug_id_done, t_debug_id);
  2739. WRITE_ONCE(fe->debug_id_done, t_debug_id);
  2740. }
  2741. BUG_ON(thread->return_error.cmd != BR_OK);
  2742. if (in_reply_to) {
  2743. thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
  2744. binder_enqueue_work(thread->proc,
  2745. &thread->return_error.work,
  2746. &thread->todo);
  2747. binder_send_failed_reply(in_reply_to, return_error);
  2748. } else {
  2749. thread->return_error.cmd = return_error;
  2750. binder_enqueue_work(thread->proc,
  2751. &thread->return_error.work,
  2752. &thread->todo);
  2753. }
  2754. }
  2755. static int binder_thread_write(struct binder_proc *proc,
  2756. struct binder_thread *thread,
  2757. binder_uintptr_t binder_buffer, size_t size,
  2758. binder_size_t *consumed)
  2759. {
  2760. uint32_t cmd;
  2761. struct binder_context *context = proc->context;
  2762. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  2763. void __user *ptr = buffer + *consumed;
  2764. void __user *end = buffer + size;
  2765. while (ptr < end && thread->return_error.cmd == BR_OK) {
  2766. int ret;
  2767. if (get_user(cmd, (uint32_t __user *)ptr))
  2768. return -EFAULT;
  2769. ptr += sizeof(uint32_t);
  2770. trace_binder_command(cmd);
  2771. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
  2772. atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
  2773. atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
  2774. atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
  2775. }
  2776. switch (cmd) {
  2777. case BC_INCREFS:
  2778. case BC_ACQUIRE:
  2779. case BC_RELEASE:
  2780. case BC_DECREFS: {
  2781. uint32_t target;
  2782. const char *debug_string;
  2783. bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
  2784. bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
  2785. struct binder_ref_data rdata;
  2786. if (get_user(target, (uint32_t __user *)ptr))
  2787. return -EFAULT;
  2788. ptr += sizeof(uint32_t);
  2789. ret = -1;
  2790. if (increment && !target) {
  2791. struct binder_node *ctx_mgr_node;
  2792. mutex_lock(&context->context_mgr_node_lock);
  2793. ctx_mgr_node = context->binder_context_mgr_node;
  2794. if (ctx_mgr_node)
  2795. ret = binder_inc_ref_for_node(
  2796. proc, ctx_mgr_node,
  2797. strong, NULL, &rdata);
  2798. mutex_unlock(&context->context_mgr_node_lock);
  2799. }
  2800. if (ret)
  2801. ret = binder_update_ref_for_handle(
  2802. proc, target, increment, strong,
  2803. &rdata);
  2804. if (!ret && rdata.desc != target) {
  2805. binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
  2806. proc->pid, thread->pid,
  2807. target, rdata.desc);
  2808. }
  2809. switch (cmd) {
  2810. case BC_INCREFS:
  2811. debug_string = "IncRefs";
  2812. break;
  2813. case BC_ACQUIRE:
  2814. debug_string = "Acquire";
  2815. break;
  2816. case BC_RELEASE:
  2817. debug_string = "Release";
  2818. break;
  2819. case BC_DECREFS:
  2820. default:
  2821. debug_string = "DecRefs";
  2822. break;
  2823. }
  2824. if (ret) {
  2825. binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
  2826. proc->pid, thread->pid, debug_string,
  2827. strong, target, ret);
  2828. break;
  2829. }
  2830. binder_debug(BINDER_DEBUG_USER_REFS,
  2831. "%d:%d %s ref %d desc %d s %d w %d\n",
  2832. proc->pid, thread->pid, debug_string,
  2833. rdata.debug_id, rdata.desc, rdata.strong,
  2834. rdata.weak);
  2835. break;
  2836. }
  2837. case BC_INCREFS_DONE:
  2838. case BC_ACQUIRE_DONE: {
  2839. binder_uintptr_t node_ptr;
  2840. binder_uintptr_t cookie;
  2841. struct binder_node *node;
  2842. bool free_node;
  2843. if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
  2844. return -EFAULT;
  2845. ptr += sizeof(binder_uintptr_t);
  2846. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  2847. return -EFAULT;
  2848. ptr += sizeof(binder_uintptr_t);
  2849. node = binder_get_node(proc, node_ptr);
  2850. if (node == NULL) {
  2851. binder_user_error("%d:%d %s u%016llx no match\n",
  2852. proc->pid, thread->pid,
  2853. cmd == BC_INCREFS_DONE ?
  2854. "BC_INCREFS_DONE" :
  2855. "BC_ACQUIRE_DONE",
  2856. (u64)node_ptr);
  2857. break;
  2858. }
  2859. if (cookie != node->cookie) {
  2860. binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
  2861. proc->pid, thread->pid,
  2862. cmd == BC_INCREFS_DONE ?
  2863. "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  2864. (u64)node_ptr, node->debug_id,
  2865. (u64)cookie, (u64)node->cookie);
  2866. binder_put_node(node);
  2867. break;
  2868. }
  2869. binder_node_inner_lock(node);
  2870. if (cmd == BC_ACQUIRE_DONE) {
  2871. if (node->pending_strong_ref == 0) {
  2872. binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
  2873. proc->pid, thread->pid,
  2874. node->debug_id);
  2875. binder_node_inner_unlock(node);
  2876. binder_put_node(node);
  2877. break;
  2878. }
  2879. node->pending_strong_ref = 0;
  2880. } else {
  2881. if (node->pending_weak_ref == 0) {
  2882. binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
  2883. proc->pid, thread->pid,
  2884. node->debug_id);
  2885. binder_node_inner_unlock(node);
  2886. binder_put_node(node);
  2887. break;
  2888. }
  2889. node->pending_weak_ref = 0;
  2890. }
  2891. free_node = binder_dec_node_nilocked(node,
  2892. cmd == BC_ACQUIRE_DONE, 0);
  2893. WARN_ON(free_node);
  2894. binder_debug(BINDER_DEBUG_USER_REFS,
  2895. "%d:%d %s node %d ls %d lw %d tr %d\n",
  2896. proc->pid, thread->pid,
  2897. cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
  2898. node->debug_id, node->local_strong_refs,
  2899. node->local_weak_refs, node->tmp_refs);
  2900. binder_node_inner_unlock(node);
  2901. binder_put_node(node);
  2902. break;
  2903. }
  2904. case BC_ATTEMPT_ACQUIRE:
  2905. pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
  2906. return -EINVAL;
  2907. case BC_ACQUIRE_RESULT:
  2908. pr_err("BC_ACQUIRE_RESULT not supported\n");
  2909. return -EINVAL;
  2910. case BC_FREE_BUFFER: {
  2911. binder_uintptr_t data_ptr;
  2912. struct binder_buffer *buffer;
  2913. if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
  2914. return -EFAULT;
  2915. ptr += sizeof(binder_uintptr_t);
  2916. buffer = binder_alloc_prepare_to_free(&proc->alloc,
  2917. data_ptr);
  2918. if (buffer == NULL) {
  2919. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
  2920. proc->pid, thread->pid, (u64)data_ptr);
  2921. break;
  2922. }
  2923. if (!buffer->allow_user_free) {
  2924. binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
  2925. proc->pid, thread->pid, (u64)data_ptr);
  2926. break;
  2927. }
  2928. binder_debug(BINDER_DEBUG_FREE_BUFFER,
  2929. "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
  2930. proc->pid, thread->pid, (u64)data_ptr,
  2931. buffer->debug_id,
  2932. buffer->transaction ? "active" : "finished");
  2933. if (buffer->transaction) {
  2934. buffer->transaction->buffer = NULL;
  2935. buffer->transaction = NULL;
  2936. }
  2937. if (buffer->async_transaction && buffer->target_node) {
  2938. struct binder_node *buf_node;
  2939. struct binder_work *w;
  2940. buf_node = buffer->target_node;
  2941. binder_node_inner_lock(buf_node);
  2942. BUG_ON(!buf_node->has_async_transaction);
  2943. BUG_ON(buf_node->proc != proc);
  2944. w = binder_dequeue_work_head_ilocked(
  2945. &buf_node->async_todo);
  2946. if (!w)
  2947. buf_node->has_async_transaction = 0;
  2948. else
  2949. binder_enqueue_work_ilocked(
  2950. w, &thread->todo);
  2951. binder_node_inner_unlock(buf_node);
  2952. }
  2953. trace_binder_transaction_buffer_release(buffer);
  2954. binder_transaction_buffer_release(proc, buffer, NULL);
  2955. binder_alloc_free_buf(&proc->alloc, buffer);
  2956. break;
  2957. }
  2958. case BC_TRANSACTION_SG:
  2959. case BC_REPLY_SG: {
  2960. struct binder_transaction_data_sg tr;
  2961. if (copy_from_user(&tr, ptr, sizeof(tr)))
  2962. return -EFAULT;
  2963. ptr += sizeof(tr);
  2964. binder_transaction(proc, thread, &tr.transaction_data,
  2965. cmd == BC_REPLY_SG, tr.buffers_size);
  2966. break;
  2967. }
  2968. case BC_TRANSACTION:
  2969. case BC_REPLY: {
  2970. struct binder_transaction_data tr;
  2971. if (copy_from_user(&tr, ptr, sizeof(tr)))
  2972. return -EFAULT;
  2973. ptr += sizeof(tr);
  2974. binder_transaction(proc, thread, &tr,
  2975. cmd == BC_REPLY, 0);
  2976. break;
  2977. }
  2978. case BC_REGISTER_LOOPER:
  2979. binder_debug(BINDER_DEBUG_THREADS,
  2980. "%d:%d BC_REGISTER_LOOPER\n",
  2981. proc->pid, thread->pid);
  2982. binder_inner_proc_lock(proc);
  2983. if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
  2984. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  2985. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
  2986. proc->pid, thread->pid);
  2987. } else if (proc->requested_threads == 0) {
  2988. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  2989. binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
  2990. proc->pid, thread->pid);
  2991. } else {
  2992. proc->requested_threads--;
  2993. proc->requested_threads_started++;
  2994. }
  2995. thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
  2996. binder_inner_proc_unlock(proc);
  2997. break;
  2998. case BC_ENTER_LOOPER:
  2999. binder_debug(BINDER_DEBUG_THREADS,
  3000. "%d:%d BC_ENTER_LOOPER\n",
  3001. proc->pid, thread->pid);
  3002. if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
  3003. thread->looper |= BINDER_LOOPER_STATE_INVALID;
  3004. binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
  3005. proc->pid, thread->pid);
  3006. }
  3007. thread->looper |= BINDER_LOOPER_STATE_ENTERED;
  3008. break;
  3009. case BC_EXIT_LOOPER:
  3010. binder_debug(BINDER_DEBUG_THREADS,
  3011. "%d:%d BC_EXIT_LOOPER\n",
  3012. proc->pid, thread->pid);
  3013. thread->looper |= BINDER_LOOPER_STATE_EXITED;
  3014. break;
  3015. case BC_REQUEST_DEATH_NOTIFICATION:
  3016. case BC_CLEAR_DEATH_NOTIFICATION: {
  3017. uint32_t target;
  3018. binder_uintptr_t cookie;
  3019. struct binder_ref *ref;
  3020. struct binder_ref_death *death = NULL;
  3021. if (get_user(target, (uint32_t __user *)ptr))
  3022. return -EFAULT;
  3023. ptr += sizeof(uint32_t);
  3024. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  3025. return -EFAULT;
  3026. ptr += sizeof(binder_uintptr_t);
  3027. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  3028. /*
  3029. * Allocate memory for death notification
  3030. * before taking lock
  3031. */
  3032. death = kzalloc(sizeof(*death), GFP_KERNEL);
  3033. if (death == NULL) {
  3034. WARN_ON(thread->return_error.cmd !=
  3035. BR_OK);
  3036. thread->return_error.cmd = BR_ERROR;
  3037. binder_enqueue_work(
  3038. thread->proc,
  3039. &thread->return_error.work,
  3040. &thread->todo);
  3041. binder_debug(
  3042. BINDER_DEBUG_FAILED_TRANSACTION,
  3043. "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
  3044. proc->pid, thread->pid);
  3045. break;
  3046. }
  3047. }
  3048. binder_proc_lock(proc);
  3049. ref = binder_get_ref_olocked(proc, target, false);
  3050. if (ref == NULL) {
  3051. binder_user_error("%d:%d %s invalid ref %d\n",
  3052. proc->pid, thread->pid,
  3053. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  3054. "BC_REQUEST_DEATH_NOTIFICATION" :
  3055. "BC_CLEAR_DEATH_NOTIFICATION",
  3056. target);
  3057. binder_proc_unlock(proc);
  3058. kfree(death);
  3059. break;
  3060. }
  3061. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  3062. "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
  3063. proc->pid, thread->pid,
  3064. cmd == BC_REQUEST_DEATH_NOTIFICATION ?
  3065. "BC_REQUEST_DEATH_NOTIFICATION" :
  3066. "BC_CLEAR_DEATH_NOTIFICATION",
  3067. (u64)cookie, ref->data.debug_id,
  3068. ref->data.desc, ref->data.strong,
  3069. ref->data.weak, ref->node->debug_id);
  3070. binder_node_lock(ref->node);
  3071. if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
  3072. if (ref->death) {
  3073. binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
  3074. proc->pid, thread->pid);
  3075. binder_node_unlock(ref->node);
  3076. binder_proc_unlock(proc);
  3077. kfree(death);
  3078. break;
  3079. }
  3080. binder_stats_created(BINDER_STAT_DEATH);
  3081. INIT_LIST_HEAD(&death->work.entry);
  3082. death->cookie = cookie;
  3083. ref->death = death;
  3084. if (ref->node->proc == NULL) {
  3085. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  3086. if (thread->looper &
  3087. (BINDER_LOOPER_STATE_REGISTERED |
  3088. BINDER_LOOPER_STATE_ENTERED))
  3089. binder_enqueue_work(
  3090. proc,
  3091. &ref->death->work,
  3092. &thread->todo);
  3093. else {
  3094. binder_enqueue_work(
  3095. proc,
  3096. &ref->death->work,
  3097. &proc->todo);
  3098. wake_up_interruptible(
  3099. &proc->wait);
  3100. }
  3101. }
  3102. } else {
  3103. if (ref->death == NULL) {
  3104. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
  3105. proc->pid, thread->pid);
  3106. binder_node_unlock(ref->node);
  3107. binder_proc_unlock(proc);
  3108. break;
  3109. }
  3110. death = ref->death;
  3111. if (death->cookie != cookie) {
  3112. binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
  3113. proc->pid, thread->pid,
  3114. (u64)death->cookie,
  3115. (u64)cookie);
  3116. binder_node_unlock(ref->node);
  3117. binder_proc_unlock(proc);
  3118. break;
  3119. }
  3120. ref->death = NULL;
  3121. binder_inner_proc_lock(proc);
  3122. if (list_empty(&death->work.entry)) {
  3123. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  3124. if (thread->looper &
  3125. (BINDER_LOOPER_STATE_REGISTERED |
  3126. BINDER_LOOPER_STATE_ENTERED))
  3127. binder_enqueue_work_ilocked(
  3128. &death->work,
  3129. &thread->todo);
  3130. else {
  3131. binder_enqueue_work_ilocked(
  3132. &death->work,
  3133. &proc->todo);
  3134. wake_up_interruptible(
  3135. &proc->wait);
  3136. }
  3137. } else {
  3138. BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
  3139. death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
  3140. }
  3141. binder_inner_proc_unlock(proc);
  3142. }
  3143. binder_node_unlock(ref->node);
  3144. binder_proc_unlock(proc);
  3145. } break;
  3146. case BC_DEAD_BINDER_DONE: {
  3147. struct binder_work *w;
  3148. binder_uintptr_t cookie;
  3149. struct binder_ref_death *death = NULL;
  3150. if (get_user(cookie, (binder_uintptr_t __user *)ptr))
  3151. return -EFAULT;
  3152. ptr += sizeof(cookie);
  3153. binder_inner_proc_lock(proc);
  3154. list_for_each_entry(w, &proc->delivered_death,
  3155. entry) {
  3156. struct binder_ref_death *tmp_death =
  3157. container_of(w,
  3158. struct binder_ref_death,
  3159. work);
  3160. if (tmp_death->cookie == cookie) {
  3161. death = tmp_death;
  3162. break;
  3163. }
  3164. }
  3165. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  3166. "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
  3167. proc->pid, thread->pid, (u64)cookie,
  3168. death);
  3169. if (death == NULL) {
  3170. binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
  3171. proc->pid, thread->pid, (u64)cookie);
  3172. binder_inner_proc_unlock(proc);
  3173. break;
  3174. }
  3175. binder_dequeue_work_ilocked(&death->work);
  3176. if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
  3177. death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
  3178. if (thread->looper &
  3179. (BINDER_LOOPER_STATE_REGISTERED |
  3180. BINDER_LOOPER_STATE_ENTERED))
  3181. binder_enqueue_work_ilocked(
  3182. &death->work, &thread->todo);
  3183. else {
  3184. binder_enqueue_work_ilocked(
  3185. &death->work,
  3186. &proc->todo);
  3187. wake_up_interruptible(&proc->wait);
  3188. }
  3189. }
  3190. binder_inner_proc_unlock(proc);
  3191. } break;
  3192. default:
  3193. pr_err("%d:%d unknown command %d\n",
  3194. proc->pid, thread->pid, cmd);
  3195. return -EINVAL;
  3196. }
  3197. *consumed = ptr - buffer;
  3198. }
  3199. return 0;
  3200. }
  3201. static void binder_stat_br(struct binder_proc *proc,
  3202. struct binder_thread *thread, uint32_t cmd)
  3203. {
  3204. trace_binder_return(cmd);
  3205. if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
  3206. atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
  3207. atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
  3208. atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
  3209. }
  3210. }
  3211. static int binder_has_proc_work(struct binder_proc *proc,
  3212. struct binder_thread *thread)
  3213. {
  3214. return !binder_worklist_empty(proc, &proc->todo) ||
  3215. thread->looper_need_return;
  3216. }
  3217. static int binder_has_thread_work(struct binder_thread *thread)
  3218. {
  3219. return !binder_worklist_empty(thread->proc, &thread->todo) ||
  3220. thread->looper_need_return;
  3221. }
  3222. static int binder_put_node_cmd(struct binder_proc *proc,
  3223. struct binder_thread *thread,
  3224. void __user **ptrp,
  3225. binder_uintptr_t node_ptr,
  3226. binder_uintptr_t node_cookie,
  3227. int node_debug_id,
  3228. uint32_t cmd, const char *cmd_name)
  3229. {
  3230. void __user *ptr = *ptrp;
  3231. if (put_user(cmd, (uint32_t __user *)ptr))
  3232. return -EFAULT;
  3233. ptr += sizeof(uint32_t);
  3234. if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
  3235. return -EFAULT;
  3236. ptr += sizeof(binder_uintptr_t);
  3237. if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
  3238. return -EFAULT;
  3239. ptr += sizeof(binder_uintptr_t);
  3240. binder_stat_br(proc, thread, cmd);
  3241. binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
  3242. proc->pid, thread->pid, cmd_name, node_debug_id,
  3243. (u64)node_ptr, (u64)node_cookie);
  3244. *ptrp = ptr;
  3245. return 0;
  3246. }
  3247. static int binder_thread_read(struct binder_proc *proc,
  3248. struct binder_thread *thread,
  3249. binder_uintptr_t binder_buffer, size_t size,
  3250. binder_size_t *consumed, int non_block)
  3251. {
  3252. void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
  3253. void __user *ptr = buffer + *consumed;
  3254. void __user *end = buffer + size;
  3255. int ret = 0;
  3256. int wait_for_proc_work;
  3257. if (*consumed == 0) {
  3258. if (put_user(BR_NOOP, (uint32_t __user *)ptr))
  3259. return -EFAULT;
  3260. ptr += sizeof(uint32_t);
  3261. }
  3262. retry:
  3263. binder_inner_proc_lock(proc);
  3264. wait_for_proc_work = thread->transaction_stack == NULL &&
  3265. binder_worklist_empty_ilocked(&thread->todo);
  3266. if (wait_for_proc_work)
  3267. proc->ready_threads++;
  3268. binder_inner_proc_unlock(proc);
  3269. thread->looper |= BINDER_LOOPER_STATE_WAITING;
  3270. trace_binder_wait_for_work(wait_for_proc_work,
  3271. !!thread->transaction_stack,
  3272. !binder_worklist_empty(proc, &thread->todo));
  3273. if (wait_for_proc_work) {
  3274. if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  3275. BINDER_LOOPER_STATE_ENTERED))) {
  3276. binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
  3277. proc->pid, thread->pid, thread->looper);
  3278. wait_event_interruptible(binder_user_error_wait,
  3279. binder_stop_on_user_error < 2);
  3280. }
  3281. binder_set_nice(proc->default_priority);
  3282. if (non_block) {
  3283. if (!binder_has_proc_work(proc, thread))
  3284. ret = -EAGAIN;
  3285. } else
  3286. ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
  3287. } else {
  3288. if (non_block) {
  3289. if (!binder_has_thread_work(thread))
  3290. ret = -EAGAIN;
  3291. } else
  3292. ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
  3293. }
  3294. binder_inner_proc_lock(proc);
  3295. if (wait_for_proc_work)
  3296. proc->ready_threads--;
  3297. binder_inner_proc_unlock(proc);
  3298. thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
  3299. if (ret)
  3300. return ret;
  3301. while (1) {
  3302. uint32_t cmd;
  3303. struct binder_transaction_data tr;
  3304. struct binder_work *w = NULL;
  3305. struct list_head *list = NULL;
  3306. struct binder_transaction *t = NULL;
  3307. struct binder_thread *t_from;
  3308. binder_inner_proc_lock(proc);
  3309. if (!binder_worklist_empty_ilocked(&thread->todo))
  3310. list = &thread->todo;
  3311. else if (!binder_worklist_empty_ilocked(&proc->todo) &&
  3312. wait_for_proc_work)
  3313. list = &proc->todo;
  3314. else {
  3315. binder_inner_proc_unlock(proc);
  3316. /* no data added */
  3317. if (ptr - buffer == 4 && !thread->looper_need_return)
  3318. goto retry;
  3319. break;
  3320. }
  3321. if (end - ptr < sizeof(tr) + 4) {
  3322. binder_inner_proc_unlock(proc);
  3323. break;
  3324. }
  3325. w = binder_dequeue_work_head_ilocked(list);
  3326. switch (w->type) {
  3327. case BINDER_WORK_TRANSACTION: {
  3328. binder_inner_proc_unlock(proc);
  3329. t = container_of(w, struct binder_transaction, work);
  3330. } break;
  3331. case BINDER_WORK_RETURN_ERROR: {
  3332. struct binder_error *e = container_of(
  3333. w, struct binder_error, work);
  3334. WARN_ON(e->cmd == BR_OK);
  3335. binder_inner_proc_unlock(proc);
  3336. if (put_user(e->cmd, (uint32_t __user *)ptr))
  3337. return -EFAULT;
  3338. e->cmd = BR_OK;
  3339. ptr += sizeof(uint32_t);
  3340. binder_stat_br(proc, thread, e->cmd);
  3341. } break;
  3342. case BINDER_WORK_TRANSACTION_COMPLETE: {
  3343. binder_inner_proc_unlock(proc);
  3344. cmd = BR_TRANSACTION_COMPLETE;
  3345. if (put_user(cmd, (uint32_t __user *)ptr))
  3346. return -EFAULT;
  3347. ptr += sizeof(uint32_t);
  3348. binder_stat_br(proc, thread, cmd);
  3349. binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
  3350. "%d:%d BR_TRANSACTION_COMPLETE\n",
  3351. proc->pid, thread->pid);
  3352. kfree(w);
  3353. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  3354. } break;
  3355. case BINDER_WORK_NODE: {
  3356. struct binder_node *node = container_of(w, struct binder_node, work);
  3357. int strong, weak;
  3358. binder_uintptr_t node_ptr = node->ptr;
  3359. binder_uintptr_t node_cookie = node->cookie;
  3360. int node_debug_id = node->debug_id;
  3361. int has_weak_ref;
  3362. int has_strong_ref;
  3363. void __user *orig_ptr = ptr;
  3364. BUG_ON(proc != node->proc);
  3365. strong = node->internal_strong_refs ||
  3366. node->local_strong_refs;
  3367. weak = !hlist_empty(&node->refs) ||
  3368. node->local_weak_refs ||
  3369. node->tmp_refs || strong;
  3370. has_strong_ref = node->has_strong_ref;
  3371. has_weak_ref = node->has_weak_ref;
  3372. if (weak && !has_weak_ref) {
  3373. node->has_weak_ref = 1;
  3374. node->pending_weak_ref = 1;
  3375. node->local_weak_refs++;
  3376. }
  3377. if (strong && !has_strong_ref) {
  3378. node->has_strong_ref = 1;
  3379. node->pending_strong_ref = 1;
  3380. node->local_strong_refs++;
  3381. }
  3382. if (!strong && has_strong_ref)
  3383. node->has_strong_ref = 0;
  3384. if (!weak && has_weak_ref)
  3385. node->has_weak_ref = 0;
  3386. if (!weak && !strong) {
  3387. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  3388. "%d:%d node %d u%016llx c%016llx deleted\n",
  3389. proc->pid, thread->pid,
  3390. node_debug_id,
  3391. (u64)node_ptr,
  3392. (u64)node_cookie);
  3393. rb_erase(&node->rb_node, &proc->nodes);
  3394. binder_inner_proc_unlock(proc);
  3395. binder_node_lock(node);
  3396. /*
  3397. * Acquire the node lock before freeing the
  3398. * node to serialize with other threads that
  3399. * may have been holding the node lock while
  3400. * decrementing this node (avoids race where
  3401. * this thread frees while the other thread
  3402. * is unlocking the node after the final
  3403. * decrement)
  3404. */
  3405. binder_node_unlock(node);
  3406. binder_free_node(node);
  3407. } else
  3408. binder_inner_proc_unlock(proc);
  3409. if (weak && !has_weak_ref)
  3410. ret = binder_put_node_cmd(
  3411. proc, thread, &ptr, node_ptr,
  3412. node_cookie, node_debug_id,
  3413. BR_INCREFS, "BR_INCREFS");
  3414. if (!ret && strong && !has_strong_ref)
  3415. ret = binder_put_node_cmd(
  3416. proc, thread, &ptr, node_ptr,
  3417. node_cookie, node_debug_id,
  3418. BR_ACQUIRE, "BR_ACQUIRE");
  3419. if (!ret && !strong && has_strong_ref)
  3420. ret = binder_put_node_cmd(
  3421. proc, thread, &ptr, node_ptr,
  3422. node_cookie, node_debug_id,
  3423. BR_RELEASE, "BR_RELEASE");
  3424. if (!ret && !weak && has_weak_ref)
  3425. ret = binder_put_node_cmd(
  3426. proc, thread, &ptr, node_ptr,
  3427. node_cookie, node_debug_id,
  3428. BR_DECREFS, "BR_DECREFS");
  3429. if (orig_ptr == ptr)
  3430. binder_debug(BINDER_DEBUG_INTERNAL_REFS,
  3431. "%d:%d node %d u%016llx c%016llx state unchanged\n",
  3432. proc->pid, thread->pid,
  3433. node_debug_id,
  3434. (u64)node_ptr,
  3435. (u64)node_cookie);
  3436. if (ret)
  3437. return ret;
  3438. } break;
  3439. case BINDER_WORK_DEAD_BINDER:
  3440. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  3441. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  3442. struct binder_ref_death *death;
  3443. uint32_t cmd;
  3444. binder_uintptr_t cookie;
  3445. death = container_of(w, struct binder_ref_death, work);
  3446. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
  3447. cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
  3448. else
  3449. cmd = BR_DEAD_BINDER;
  3450. cookie = death->cookie;
  3451. binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
  3452. "%d:%d %s %016llx\n",
  3453. proc->pid, thread->pid,
  3454. cmd == BR_DEAD_BINDER ?
  3455. "BR_DEAD_BINDER" :
  3456. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  3457. (u64)cookie);
  3458. if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
  3459. binder_inner_proc_unlock(proc);
  3460. kfree(death);
  3461. binder_stats_deleted(BINDER_STAT_DEATH);
  3462. } else {
  3463. binder_enqueue_work_ilocked(
  3464. w, &proc->delivered_death);
  3465. binder_inner_proc_unlock(proc);
  3466. }
  3467. if (put_user(cmd, (uint32_t __user *)ptr))
  3468. return -EFAULT;
  3469. ptr += sizeof(uint32_t);
  3470. if (put_user(cookie,
  3471. (binder_uintptr_t __user *)ptr))
  3472. return -EFAULT;
  3473. ptr += sizeof(binder_uintptr_t);
  3474. binder_stat_br(proc, thread, cmd);
  3475. if (cmd == BR_DEAD_BINDER)
  3476. goto done; /* DEAD_BINDER notifications can cause transactions */
  3477. } break;
  3478. }
  3479. if (!t)
  3480. continue;
  3481. BUG_ON(t->buffer == NULL);
  3482. if (t->buffer->target_node) {
  3483. struct binder_node *target_node = t->buffer->target_node;
  3484. tr.target.ptr = target_node->ptr;
  3485. tr.cookie = target_node->cookie;
  3486. t->saved_priority = task_nice(current);
  3487. if (t->priority < target_node->min_priority &&
  3488. !(t->flags & TF_ONE_WAY))
  3489. binder_set_nice(t->priority);
  3490. else if (!(t->flags & TF_ONE_WAY) ||
  3491. t->saved_priority > target_node->min_priority)
  3492. binder_set_nice(target_node->min_priority);
  3493. cmd = BR_TRANSACTION;
  3494. } else {
  3495. tr.target.ptr = 0;
  3496. tr.cookie = 0;
  3497. cmd = BR_REPLY;
  3498. }
  3499. tr.code = t->code;
  3500. tr.flags = t->flags;
  3501. tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
  3502. t_from = binder_get_txn_from(t);
  3503. if (t_from) {
  3504. struct task_struct *sender = t_from->proc->tsk;
  3505. tr.sender_pid = task_tgid_nr_ns(sender,
  3506. task_active_pid_ns(current));
  3507. } else {
  3508. tr.sender_pid = 0;
  3509. }
  3510. tr.data_size = t->buffer->data_size;
  3511. tr.offsets_size = t->buffer->offsets_size;
  3512. tr.data.ptr.buffer = (binder_uintptr_t)
  3513. ((uintptr_t)t->buffer->data +
  3514. binder_alloc_get_user_buffer_offset(&proc->alloc));
  3515. tr.data.ptr.offsets = tr.data.ptr.buffer +
  3516. ALIGN(t->buffer->data_size,
  3517. sizeof(void *));
  3518. if (put_user(cmd, (uint32_t __user *)ptr)) {
  3519. if (t_from)
  3520. binder_thread_dec_tmpref(t_from);
  3521. return -EFAULT;
  3522. }
  3523. ptr += sizeof(uint32_t);
  3524. if (copy_to_user(ptr, &tr, sizeof(tr))) {
  3525. if (t_from)
  3526. binder_thread_dec_tmpref(t_from);
  3527. return -EFAULT;
  3528. }
  3529. ptr += sizeof(tr);
  3530. trace_binder_transaction_received(t);
  3531. binder_stat_br(proc, thread, cmd);
  3532. binder_debug(BINDER_DEBUG_TRANSACTION,
  3533. "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
  3534. proc->pid, thread->pid,
  3535. (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
  3536. "BR_REPLY",
  3537. t->debug_id, t_from ? t_from->proc->pid : 0,
  3538. t_from ? t_from->pid : 0, cmd,
  3539. t->buffer->data_size, t->buffer->offsets_size,
  3540. (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
  3541. if (t_from)
  3542. binder_thread_dec_tmpref(t_from);
  3543. t->buffer->allow_user_free = 1;
  3544. if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
  3545. binder_inner_proc_lock(thread->proc);
  3546. t->to_parent = thread->transaction_stack;
  3547. t->to_thread = thread;
  3548. thread->transaction_stack = t;
  3549. binder_inner_proc_unlock(thread->proc);
  3550. } else {
  3551. binder_free_transaction(t);
  3552. }
  3553. break;
  3554. }
  3555. done:
  3556. *consumed = ptr - buffer;
  3557. binder_inner_proc_lock(proc);
  3558. if (proc->requested_threads + proc->ready_threads == 0 &&
  3559. proc->requested_threads_started < proc->max_threads &&
  3560. (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
  3561. BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
  3562. /*spawn a new thread if we leave this out */) {
  3563. proc->requested_threads++;
  3564. binder_inner_proc_unlock(proc);
  3565. binder_debug(BINDER_DEBUG_THREADS,
  3566. "%d:%d BR_SPAWN_LOOPER\n",
  3567. proc->pid, thread->pid);
  3568. if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
  3569. return -EFAULT;
  3570. binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
  3571. } else
  3572. binder_inner_proc_unlock(proc);
  3573. return 0;
  3574. }
  3575. static void binder_release_work(struct binder_proc *proc,
  3576. struct list_head *list)
  3577. {
  3578. struct binder_work *w;
  3579. while (1) {
  3580. w = binder_dequeue_work_head(proc, list);
  3581. if (!w)
  3582. return;
  3583. switch (w->type) {
  3584. case BINDER_WORK_TRANSACTION: {
  3585. struct binder_transaction *t;
  3586. t = container_of(w, struct binder_transaction, work);
  3587. if (t->buffer->target_node &&
  3588. !(t->flags & TF_ONE_WAY)) {
  3589. binder_send_failed_reply(t, BR_DEAD_REPLY);
  3590. } else {
  3591. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3592. "undelivered transaction %d\n",
  3593. t->debug_id);
  3594. binder_free_transaction(t);
  3595. }
  3596. } break;
  3597. case BINDER_WORK_RETURN_ERROR: {
  3598. struct binder_error *e = container_of(
  3599. w, struct binder_error, work);
  3600. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3601. "undelivered TRANSACTION_ERROR: %u\n",
  3602. e->cmd);
  3603. } break;
  3604. case BINDER_WORK_TRANSACTION_COMPLETE: {
  3605. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3606. "undelivered TRANSACTION_COMPLETE\n");
  3607. kfree(w);
  3608. binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
  3609. } break;
  3610. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  3611. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
  3612. struct binder_ref_death *death;
  3613. death = container_of(w, struct binder_ref_death, work);
  3614. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3615. "undelivered death notification, %016llx\n",
  3616. (u64)death->cookie);
  3617. kfree(death);
  3618. binder_stats_deleted(BINDER_STAT_DEATH);
  3619. } break;
  3620. default:
  3621. pr_err("unexpected work type, %d, not freed\n",
  3622. w->type);
  3623. break;
  3624. }
  3625. }
  3626. }
  3627. static struct binder_thread *binder_get_thread_ilocked(
  3628. struct binder_proc *proc, struct binder_thread *new_thread)
  3629. {
  3630. struct binder_thread *thread = NULL;
  3631. struct rb_node *parent = NULL;
  3632. struct rb_node **p = &proc->threads.rb_node;
  3633. while (*p) {
  3634. parent = *p;
  3635. thread = rb_entry(parent, struct binder_thread, rb_node);
  3636. if (current->pid < thread->pid)
  3637. p = &(*p)->rb_left;
  3638. else if (current->pid > thread->pid)
  3639. p = &(*p)->rb_right;
  3640. else
  3641. return thread;
  3642. }
  3643. if (!new_thread)
  3644. return NULL;
  3645. thread = new_thread;
  3646. binder_stats_created(BINDER_STAT_THREAD);
  3647. thread->proc = proc;
  3648. thread->pid = current->pid;
  3649. atomic_set(&thread->tmp_ref, 0);
  3650. init_waitqueue_head(&thread->wait);
  3651. INIT_LIST_HEAD(&thread->todo);
  3652. rb_link_node(&thread->rb_node, parent, p);
  3653. rb_insert_color(&thread->rb_node, &proc->threads);
  3654. thread->looper_need_return = true;
  3655. thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
  3656. thread->return_error.cmd = BR_OK;
  3657. thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
  3658. thread->reply_error.cmd = BR_OK;
  3659. return thread;
  3660. }
  3661. static struct binder_thread *binder_get_thread(struct binder_proc *proc)
  3662. {
  3663. struct binder_thread *thread;
  3664. struct binder_thread *new_thread;
  3665. binder_inner_proc_lock(proc);
  3666. thread = binder_get_thread_ilocked(proc, NULL);
  3667. binder_inner_proc_unlock(proc);
  3668. if (!thread) {
  3669. new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
  3670. if (new_thread == NULL)
  3671. return NULL;
  3672. binder_inner_proc_lock(proc);
  3673. thread = binder_get_thread_ilocked(proc, new_thread);
  3674. binder_inner_proc_unlock(proc);
  3675. if (thread != new_thread)
  3676. kfree(new_thread);
  3677. }
  3678. return thread;
  3679. }
  3680. static void binder_free_proc(struct binder_proc *proc)
  3681. {
  3682. BUG_ON(!list_empty(&proc->todo));
  3683. BUG_ON(!list_empty(&proc->delivered_death));
  3684. binder_alloc_deferred_release(&proc->alloc);
  3685. put_task_struct(proc->tsk);
  3686. binder_stats_deleted(BINDER_STAT_PROC);
  3687. kfree(proc);
  3688. }
  3689. static void binder_free_thread(struct binder_thread *thread)
  3690. {
  3691. BUG_ON(!list_empty(&thread->todo));
  3692. binder_stats_deleted(BINDER_STAT_THREAD);
  3693. binder_proc_dec_tmpref(thread->proc);
  3694. kfree(thread);
  3695. }
  3696. static int binder_thread_release(struct binder_proc *proc,
  3697. struct binder_thread *thread)
  3698. {
  3699. struct binder_transaction *t;
  3700. struct binder_transaction *send_reply = NULL;
  3701. int active_transactions = 0;
  3702. struct binder_transaction *last_t = NULL;
  3703. binder_inner_proc_lock(thread->proc);
  3704. /*
  3705. * take a ref on the proc so it survives
  3706. * after we remove this thread from proc->threads.
  3707. * The corresponding dec is when we actually
  3708. * free the thread in binder_free_thread()
  3709. */
  3710. proc->tmp_ref++;
  3711. /*
  3712. * take a ref on this thread to ensure it
  3713. * survives while we are releasing it
  3714. */
  3715. atomic_inc(&thread->tmp_ref);
  3716. rb_erase(&thread->rb_node, &proc->threads);
  3717. t = thread->transaction_stack;
  3718. if (t) {
  3719. spin_lock(&t->lock);
  3720. if (t->to_thread == thread)
  3721. send_reply = t;
  3722. }
  3723. thread->is_dead = true;
  3724. while (t) {
  3725. last_t = t;
  3726. active_transactions++;
  3727. binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
  3728. "release %d:%d transaction %d %s, still active\n",
  3729. proc->pid, thread->pid,
  3730. t->debug_id,
  3731. (t->to_thread == thread) ? "in" : "out");
  3732. if (t->to_thread == thread) {
  3733. t->to_proc = NULL;
  3734. t->to_thread = NULL;
  3735. if (t->buffer) {
  3736. t->buffer->transaction = NULL;
  3737. t->buffer = NULL;
  3738. }
  3739. t = t->to_parent;
  3740. } else if (t->from == thread) {
  3741. t->from = NULL;
  3742. t = t->from_parent;
  3743. } else
  3744. BUG();
  3745. spin_unlock(&last_t->lock);
  3746. if (t)
  3747. spin_lock(&t->lock);
  3748. }
  3749. binder_inner_proc_unlock(thread->proc);
  3750. if (send_reply)
  3751. binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
  3752. binder_release_work(proc, &thread->todo);
  3753. binder_thread_dec_tmpref(thread);
  3754. return active_transactions;
  3755. }
  3756. static unsigned int binder_poll(struct file *filp,
  3757. struct poll_table_struct *wait)
  3758. {
  3759. struct binder_proc *proc = filp->private_data;
  3760. struct binder_thread *thread = NULL;
  3761. int wait_for_proc_work;
  3762. thread = binder_get_thread(proc);
  3763. binder_inner_proc_lock(thread->proc);
  3764. wait_for_proc_work = thread->transaction_stack == NULL &&
  3765. binder_worklist_empty_ilocked(&thread->todo);
  3766. binder_inner_proc_unlock(thread->proc);
  3767. if (wait_for_proc_work) {
  3768. if (binder_has_proc_work(proc, thread))
  3769. return POLLIN;
  3770. poll_wait(filp, &proc->wait, wait);
  3771. if (binder_has_proc_work(proc, thread))
  3772. return POLLIN;
  3773. } else {
  3774. if (binder_has_thread_work(thread))
  3775. return POLLIN;
  3776. poll_wait(filp, &thread->wait, wait);
  3777. if (binder_has_thread_work(thread))
  3778. return POLLIN;
  3779. }
  3780. return 0;
  3781. }
  3782. static int binder_ioctl_write_read(struct file *filp,
  3783. unsigned int cmd, unsigned long arg,
  3784. struct binder_thread *thread)
  3785. {
  3786. int ret = 0;
  3787. struct binder_proc *proc = filp->private_data;
  3788. unsigned int size = _IOC_SIZE(cmd);
  3789. void __user *ubuf = (void __user *)arg;
  3790. struct binder_write_read bwr;
  3791. if (size != sizeof(struct binder_write_read)) {
  3792. ret = -EINVAL;
  3793. goto out;
  3794. }
  3795. if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
  3796. ret = -EFAULT;
  3797. goto out;
  3798. }
  3799. binder_debug(BINDER_DEBUG_READ_WRITE,
  3800. "%d:%d write %lld at %016llx, read %lld at %016llx\n",
  3801. proc->pid, thread->pid,
  3802. (u64)bwr.write_size, (u64)bwr.write_buffer,
  3803. (u64)bwr.read_size, (u64)bwr.read_buffer);
  3804. if (bwr.write_size > 0) {
  3805. ret = binder_thread_write(proc, thread,
  3806. bwr.write_buffer,
  3807. bwr.write_size,
  3808. &bwr.write_consumed);
  3809. trace_binder_write_done(ret);
  3810. if (ret < 0) {
  3811. bwr.read_consumed = 0;
  3812. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  3813. ret = -EFAULT;
  3814. goto out;
  3815. }
  3816. }
  3817. if (bwr.read_size > 0) {
  3818. ret = binder_thread_read(proc, thread, bwr.read_buffer,
  3819. bwr.read_size,
  3820. &bwr.read_consumed,
  3821. filp->f_flags & O_NONBLOCK);
  3822. trace_binder_read_done(ret);
  3823. if (!binder_worklist_empty(proc, &proc->todo))
  3824. wake_up_interruptible(&proc->wait);
  3825. if (ret < 0) {
  3826. if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
  3827. ret = -EFAULT;
  3828. goto out;
  3829. }
  3830. }
  3831. binder_debug(BINDER_DEBUG_READ_WRITE,
  3832. "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
  3833. proc->pid, thread->pid,
  3834. (u64)bwr.write_consumed, (u64)bwr.write_size,
  3835. (u64)bwr.read_consumed, (u64)bwr.read_size);
  3836. if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
  3837. ret = -EFAULT;
  3838. goto out;
  3839. }
  3840. out:
  3841. return ret;
  3842. }
  3843. static int binder_ioctl_set_ctx_mgr(struct file *filp)
  3844. {
  3845. int ret = 0;
  3846. struct binder_proc *proc = filp->private_data;
  3847. struct binder_context *context = proc->context;
  3848. struct binder_node *new_node;
  3849. kuid_t curr_euid = current_euid();
  3850. mutex_lock(&context->context_mgr_node_lock);
  3851. if (context->binder_context_mgr_node) {
  3852. pr_err("BINDER_SET_CONTEXT_MGR already set\n");
  3853. ret = -EBUSY;
  3854. goto out;
  3855. }
  3856. ret = security_binder_set_context_mgr(proc->tsk);
  3857. if (ret < 0)
  3858. goto out;
  3859. if (uid_valid(context->binder_context_mgr_uid)) {
  3860. if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
  3861. pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
  3862. from_kuid(&init_user_ns, curr_euid),
  3863. from_kuid(&init_user_ns,
  3864. context->binder_context_mgr_uid));
  3865. ret = -EPERM;
  3866. goto out;
  3867. }
  3868. } else {
  3869. context->binder_context_mgr_uid = curr_euid;
  3870. }
  3871. new_node = binder_new_node(proc, NULL);
  3872. if (!new_node) {
  3873. ret = -ENOMEM;
  3874. goto out;
  3875. }
  3876. binder_node_lock(new_node);
  3877. new_node->local_weak_refs++;
  3878. new_node->local_strong_refs++;
  3879. new_node->has_strong_ref = 1;
  3880. new_node->has_weak_ref = 1;
  3881. context->binder_context_mgr_node = new_node;
  3882. binder_node_unlock(new_node);
  3883. binder_put_node(new_node);
  3884. out:
  3885. mutex_unlock(&context->context_mgr_node_lock);
  3886. return ret;
  3887. }
  3888. static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  3889. {
  3890. int ret;
  3891. struct binder_proc *proc = filp->private_data;
  3892. struct binder_thread *thread;
  3893. unsigned int size = _IOC_SIZE(cmd);
  3894. void __user *ubuf = (void __user *)arg;
  3895. /*pr_info("binder_ioctl: %d:%d %x %lx\n",
  3896. proc->pid, current->pid, cmd, arg);*/
  3897. binder_selftest_alloc(&proc->alloc);
  3898. trace_binder_ioctl(cmd, arg);
  3899. ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  3900. if (ret)
  3901. goto err_unlocked;
  3902. thread = binder_get_thread(proc);
  3903. if (thread == NULL) {
  3904. ret = -ENOMEM;
  3905. goto err;
  3906. }
  3907. switch (cmd) {
  3908. case BINDER_WRITE_READ:
  3909. ret = binder_ioctl_write_read(filp, cmd, arg, thread);
  3910. if (ret)
  3911. goto err;
  3912. break;
  3913. case BINDER_SET_MAX_THREADS: {
  3914. int max_threads;
  3915. if (copy_from_user(&max_threads, ubuf,
  3916. sizeof(max_threads))) {
  3917. ret = -EINVAL;
  3918. goto err;
  3919. }
  3920. binder_inner_proc_lock(proc);
  3921. proc->max_threads = max_threads;
  3922. binder_inner_proc_unlock(proc);
  3923. break;
  3924. }
  3925. case BINDER_SET_CONTEXT_MGR:
  3926. ret = binder_ioctl_set_ctx_mgr(filp);
  3927. if (ret)
  3928. goto err;
  3929. break;
  3930. case BINDER_THREAD_EXIT:
  3931. binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
  3932. proc->pid, thread->pid);
  3933. binder_thread_release(proc, thread);
  3934. thread = NULL;
  3935. break;
  3936. case BINDER_VERSION: {
  3937. struct binder_version __user *ver = ubuf;
  3938. if (size != sizeof(struct binder_version)) {
  3939. ret = -EINVAL;
  3940. goto err;
  3941. }
  3942. if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
  3943. &ver->protocol_version)) {
  3944. ret = -EINVAL;
  3945. goto err;
  3946. }
  3947. break;
  3948. }
  3949. default:
  3950. ret = -EINVAL;
  3951. goto err;
  3952. }
  3953. ret = 0;
  3954. err:
  3955. if (thread)
  3956. thread->looper_need_return = false;
  3957. wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
  3958. if (ret && ret != -ERESTARTSYS)
  3959. pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
  3960. err_unlocked:
  3961. trace_binder_ioctl_done(ret);
  3962. return ret;
  3963. }
  3964. static void binder_vma_open(struct vm_area_struct *vma)
  3965. {
  3966. struct binder_proc *proc = vma->vm_private_data;
  3967. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  3968. "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  3969. proc->pid, vma->vm_start, vma->vm_end,
  3970. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  3971. (unsigned long)pgprot_val(vma->vm_page_prot));
  3972. }
  3973. static void binder_vma_close(struct vm_area_struct *vma)
  3974. {
  3975. struct binder_proc *proc = vma->vm_private_data;
  3976. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  3977. "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
  3978. proc->pid, vma->vm_start, vma->vm_end,
  3979. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  3980. (unsigned long)pgprot_val(vma->vm_page_prot));
  3981. binder_alloc_vma_close(&proc->alloc);
  3982. binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
  3983. }
  3984. static int binder_vm_fault(struct vm_fault *vmf)
  3985. {
  3986. return VM_FAULT_SIGBUS;
  3987. }
  3988. static const struct vm_operations_struct binder_vm_ops = {
  3989. .open = binder_vma_open,
  3990. .close = binder_vma_close,
  3991. .fault = binder_vm_fault,
  3992. };
  3993. static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
  3994. {
  3995. int ret;
  3996. struct binder_proc *proc = filp->private_data;
  3997. const char *failure_string;
  3998. if (proc->tsk != current->group_leader)
  3999. return -EINVAL;
  4000. if ((vma->vm_end - vma->vm_start) > SZ_4M)
  4001. vma->vm_end = vma->vm_start + SZ_4M;
  4002. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4003. "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
  4004. __func__, proc->pid, vma->vm_start, vma->vm_end,
  4005. (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
  4006. (unsigned long)pgprot_val(vma->vm_page_prot));
  4007. if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
  4008. ret = -EPERM;
  4009. failure_string = "bad vm_flags";
  4010. goto err_bad_arg;
  4011. }
  4012. vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
  4013. vma->vm_ops = &binder_vm_ops;
  4014. vma->vm_private_data = proc;
  4015. ret = binder_alloc_mmap_handler(&proc->alloc, vma);
  4016. if (ret)
  4017. return ret;
  4018. proc->files = get_files_struct(current);
  4019. return 0;
  4020. err_bad_arg:
  4021. pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
  4022. proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
  4023. return ret;
  4024. }
  4025. static int binder_open(struct inode *nodp, struct file *filp)
  4026. {
  4027. struct binder_proc *proc;
  4028. struct binder_device *binder_dev;
  4029. binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
  4030. current->group_leader->pid, current->pid);
  4031. proc = kzalloc(sizeof(*proc), GFP_KERNEL);
  4032. if (proc == NULL)
  4033. return -ENOMEM;
  4034. spin_lock_init(&proc->inner_lock);
  4035. spin_lock_init(&proc->outer_lock);
  4036. get_task_struct(current->group_leader);
  4037. proc->tsk = current->group_leader;
  4038. INIT_LIST_HEAD(&proc->todo);
  4039. init_waitqueue_head(&proc->wait);
  4040. proc->default_priority = task_nice(current);
  4041. binder_dev = container_of(filp->private_data, struct binder_device,
  4042. miscdev);
  4043. proc->context = &binder_dev->context;
  4044. binder_alloc_init(&proc->alloc);
  4045. binder_stats_created(BINDER_STAT_PROC);
  4046. proc->pid = current->group_leader->pid;
  4047. INIT_LIST_HEAD(&proc->delivered_death);
  4048. filp->private_data = proc;
  4049. mutex_lock(&binder_procs_lock);
  4050. hlist_add_head(&proc->proc_node, &binder_procs);
  4051. mutex_unlock(&binder_procs_lock);
  4052. if (binder_debugfs_dir_entry_proc) {
  4053. char strbuf[11];
  4054. snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
  4055. /*
  4056. * proc debug entries are shared between contexts, so
  4057. * this will fail if the process tries to open the driver
  4058. * again with a different context. The priting code will
  4059. * anyway print all contexts that a given PID has, so this
  4060. * is not a problem.
  4061. */
  4062. proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
  4063. binder_debugfs_dir_entry_proc,
  4064. (void *)(unsigned long)proc->pid,
  4065. &binder_proc_fops);
  4066. }
  4067. return 0;
  4068. }
  4069. static int binder_flush(struct file *filp, fl_owner_t id)
  4070. {
  4071. struct binder_proc *proc = filp->private_data;
  4072. binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
  4073. return 0;
  4074. }
  4075. static void binder_deferred_flush(struct binder_proc *proc)
  4076. {
  4077. struct rb_node *n;
  4078. int wake_count = 0;
  4079. binder_inner_proc_lock(proc);
  4080. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
  4081. struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
  4082. thread->looper_need_return = true;
  4083. if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
  4084. wake_up_interruptible(&thread->wait);
  4085. wake_count++;
  4086. }
  4087. }
  4088. binder_inner_proc_unlock(proc);
  4089. wake_up_interruptible_all(&proc->wait);
  4090. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4091. "binder_flush: %d woke %d threads\n", proc->pid,
  4092. wake_count);
  4093. }
  4094. static int binder_release(struct inode *nodp, struct file *filp)
  4095. {
  4096. struct binder_proc *proc = filp->private_data;
  4097. debugfs_remove(proc->debugfs_entry);
  4098. binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
  4099. return 0;
  4100. }
  4101. static int binder_node_release(struct binder_node *node, int refs)
  4102. {
  4103. struct binder_ref *ref;
  4104. int death = 0;
  4105. struct binder_proc *proc = node->proc;
  4106. binder_release_work(proc, &node->async_todo);
  4107. binder_node_lock(node);
  4108. binder_inner_proc_lock(proc);
  4109. binder_dequeue_work_ilocked(&node->work);
  4110. /*
  4111. * The caller must have taken a temporary ref on the node,
  4112. */
  4113. BUG_ON(!node->tmp_refs);
  4114. if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
  4115. binder_inner_proc_unlock(proc);
  4116. binder_node_unlock(node);
  4117. binder_free_node(node);
  4118. return refs;
  4119. }
  4120. node->proc = NULL;
  4121. node->local_strong_refs = 0;
  4122. node->local_weak_refs = 0;
  4123. binder_inner_proc_unlock(proc);
  4124. spin_lock(&binder_dead_nodes_lock);
  4125. hlist_add_head(&node->dead_node, &binder_dead_nodes);
  4126. spin_unlock(&binder_dead_nodes_lock);
  4127. hlist_for_each_entry(ref, &node->refs, node_entry) {
  4128. refs++;
  4129. /*
  4130. * Need the node lock to synchronize
  4131. * with new notification requests and the
  4132. * inner lock to synchronize with queued
  4133. * death notifications.
  4134. */
  4135. binder_inner_proc_lock(ref->proc);
  4136. if (!ref->death) {
  4137. binder_inner_proc_unlock(ref->proc);
  4138. continue;
  4139. }
  4140. death++;
  4141. BUG_ON(!list_empty(&ref->death->work.entry));
  4142. ref->death->work.type = BINDER_WORK_DEAD_BINDER;
  4143. binder_enqueue_work_ilocked(&ref->death->work,
  4144. &ref->proc->todo);
  4145. wake_up_interruptible(&ref->proc->wait);
  4146. binder_inner_proc_unlock(ref->proc);
  4147. }
  4148. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  4149. "node %d now dead, refs %d, death %d\n",
  4150. node->debug_id, refs, death);
  4151. binder_node_unlock(node);
  4152. binder_put_node(node);
  4153. return refs;
  4154. }
  4155. static void binder_deferred_release(struct binder_proc *proc)
  4156. {
  4157. struct binder_context *context = proc->context;
  4158. struct rb_node *n;
  4159. int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
  4160. BUG_ON(proc->files);
  4161. mutex_lock(&binder_procs_lock);
  4162. hlist_del(&proc->proc_node);
  4163. mutex_unlock(&binder_procs_lock);
  4164. mutex_lock(&context->context_mgr_node_lock);
  4165. if (context->binder_context_mgr_node &&
  4166. context->binder_context_mgr_node->proc == proc) {
  4167. binder_debug(BINDER_DEBUG_DEAD_BINDER,
  4168. "%s: %d context_mgr_node gone\n",
  4169. __func__, proc->pid);
  4170. context->binder_context_mgr_node = NULL;
  4171. }
  4172. mutex_unlock(&context->context_mgr_node_lock);
  4173. binder_inner_proc_lock(proc);
  4174. /*
  4175. * Make sure proc stays alive after we
  4176. * remove all the threads
  4177. */
  4178. proc->tmp_ref++;
  4179. proc->is_dead = true;
  4180. threads = 0;
  4181. active_transactions = 0;
  4182. while ((n = rb_first(&proc->threads))) {
  4183. struct binder_thread *thread;
  4184. thread = rb_entry(n, struct binder_thread, rb_node);
  4185. binder_inner_proc_unlock(proc);
  4186. threads++;
  4187. active_transactions += binder_thread_release(proc, thread);
  4188. binder_inner_proc_lock(proc);
  4189. }
  4190. nodes = 0;
  4191. incoming_refs = 0;
  4192. while ((n = rb_first(&proc->nodes))) {
  4193. struct binder_node *node;
  4194. node = rb_entry(n, struct binder_node, rb_node);
  4195. nodes++;
  4196. /*
  4197. * take a temporary ref on the node before
  4198. * calling binder_node_release() which will either
  4199. * kfree() the node or call binder_put_node()
  4200. */
  4201. binder_inc_node_tmpref_ilocked(node);
  4202. rb_erase(&node->rb_node, &proc->nodes);
  4203. binder_inner_proc_unlock(proc);
  4204. incoming_refs = binder_node_release(node, incoming_refs);
  4205. binder_inner_proc_lock(proc);
  4206. }
  4207. binder_inner_proc_unlock(proc);
  4208. outgoing_refs = 0;
  4209. binder_proc_lock(proc);
  4210. while ((n = rb_first(&proc->refs_by_desc))) {
  4211. struct binder_ref *ref;
  4212. ref = rb_entry(n, struct binder_ref, rb_node_desc);
  4213. outgoing_refs++;
  4214. binder_cleanup_ref_olocked(ref);
  4215. binder_proc_unlock(proc);
  4216. binder_free_ref(ref);
  4217. binder_proc_lock(proc);
  4218. }
  4219. binder_proc_unlock(proc);
  4220. binder_release_work(proc, &proc->todo);
  4221. binder_release_work(proc, &proc->delivered_death);
  4222. binder_debug(BINDER_DEBUG_OPEN_CLOSE,
  4223. "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
  4224. __func__, proc->pid, threads, nodes, incoming_refs,
  4225. outgoing_refs, active_transactions);
  4226. binder_proc_dec_tmpref(proc);
  4227. }
  4228. static void binder_deferred_func(struct work_struct *work)
  4229. {
  4230. struct binder_proc *proc;
  4231. struct files_struct *files;
  4232. int defer;
  4233. do {
  4234. mutex_lock(&binder_deferred_lock);
  4235. if (!hlist_empty(&binder_deferred_list)) {
  4236. proc = hlist_entry(binder_deferred_list.first,
  4237. struct binder_proc, deferred_work_node);
  4238. hlist_del_init(&proc->deferred_work_node);
  4239. defer = proc->deferred_work;
  4240. proc->deferred_work = 0;
  4241. } else {
  4242. proc = NULL;
  4243. defer = 0;
  4244. }
  4245. mutex_unlock(&binder_deferred_lock);
  4246. files = NULL;
  4247. if (defer & BINDER_DEFERRED_PUT_FILES) {
  4248. files = proc->files;
  4249. if (files)
  4250. proc->files = NULL;
  4251. }
  4252. if (defer & BINDER_DEFERRED_FLUSH)
  4253. binder_deferred_flush(proc);
  4254. if (defer & BINDER_DEFERRED_RELEASE)
  4255. binder_deferred_release(proc); /* frees proc */
  4256. if (files)
  4257. put_files_struct(files);
  4258. } while (proc);
  4259. }
  4260. static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
  4261. static void
  4262. binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
  4263. {
  4264. mutex_lock(&binder_deferred_lock);
  4265. proc->deferred_work |= defer;
  4266. if (hlist_unhashed(&proc->deferred_work_node)) {
  4267. hlist_add_head(&proc->deferred_work_node,
  4268. &binder_deferred_list);
  4269. schedule_work(&binder_deferred_work);
  4270. }
  4271. mutex_unlock(&binder_deferred_lock);
  4272. }
  4273. static void print_binder_transaction_ilocked(struct seq_file *m,
  4274. struct binder_proc *proc,
  4275. const char *prefix,
  4276. struct binder_transaction *t)
  4277. {
  4278. struct binder_proc *to_proc;
  4279. struct binder_buffer *buffer = t->buffer;
  4280. WARN_ON(!spin_is_locked(&proc->inner_lock));
  4281. spin_lock(&t->lock);
  4282. to_proc = t->to_proc;
  4283. seq_printf(m,
  4284. "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
  4285. prefix, t->debug_id, t,
  4286. t->from ? t->from->proc->pid : 0,
  4287. t->from ? t->from->pid : 0,
  4288. to_proc ? to_proc->pid : 0,
  4289. t->to_thread ? t->to_thread->pid : 0,
  4290. t->code, t->flags, t->priority, t->need_reply);
  4291. spin_unlock(&t->lock);
  4292. if (proc != to_proc) {
  4293. /*
  4294. * Can only safely deref buffer if we are holding the
  4295. * correct proc inner lock for this node
  4296. */
  4297. seq_puts(m, "\n");
  4298. return;
  4299. }
  4300. if (buffer == NULL) {
  4301. seq_puts(m, " buffer free\n");
  4302. return;
  4303. }
  4304. if (buffer->target_node)
  4305. seq_printf(m, " node %d", buffer->target_node->debug_id);
  4306. seq_printf(m, " size %zd:%zd data %p\n",
  4307. buffer->data_size, buffer->offsets_size,
  4308. buffer->data);
  4309. }
  4310. static void print_binder_work_ilocked(struct seq_file *m,
  4311. struct binder_proc *proc,
  4312. const char *prefix,
  4313. const char *transaction_prefix,
  4314. struct binder_work *w)
  4315. {
  4316. struct binder_node *node;
  4317. struct binder_transaction *t;
  4318. switch (w->type) {
  4319. case BINDER_WORK_TRANSACTION:
  4320. t = container_of(w, struct binder_transaction, work);
  4321. print_binder_transaction_ilocked(
  4322. m, proc, transaction_prefix, t);
  4323. break;
  4324. case BINDER_WORK_RETURN_ERROR: {
  4325. struct binder_error *e = container_of(
  4326. w, struct binder_error, work);
  4327. seq_printf(m, "%stransaction error: %u\n",
  4328. prefix, e->cmd);
  4329. } break;
  4330. case BINDER_WORK_TRANSACTION_COMPLETE:
  4331. seq_printf(m, "%stransaction complete\n", prefix);
  4332. break;
  4333. case BINDER_WORK_NODE:
  4334. node = container_of(w, struct binder_node, work);
  4335. seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
  4336. prefix, node->debug_id,
  4337. (u64)node->ptr, (u64)node->cookie);
  4338. break;
  4339. case BINDER_WORK_DEAD_BINDER:
  4340. seq_printf(m, "%shas dead binder\n", prefix);
  4341. break;
  4342. case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
  4343. seq_printf(m, "%shas cleared dead binder\n", prefix);
  4344. break;
  4345. case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
  4346. seq_printf(m, "%shas cleared death notification\n", prefix);
  4347. break;
  4348. default:
  4349. seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
  4350. break;
  4351. }
  4352. }
  4353. static void print_binder_thread_ilocked(struct seq_file *m,
  4354. struct binder_thread *thread,
  4355. int print_always)
  4356. {
  4357. struct binder_transaction *t;
  4358. struct binder_work *w;
  4359. size_t start_pos = m->count;
  4360. size_t header_pos;
  4361. WARN_ON(!spin_is_locked(&thread->proc->inner_lock));
  4362. seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
  4363. thread->pid, thread->looper,
  4364. thread->looper_need_return,
  4365. atomic_read(&thread->tmp_ref));
  4366. header_pos = m->count;
  4367. t = thread->transaction_stack;
  4368. while (t) {
  4369. if (t->from == thread) {
  4370. print_binder_transaction_ilocked(m, thread->proc,
  4371. " outgoing transaction", t);
  4372. t = t->from_parent;
  4373. } else if (t->to_thread == thread) {
  4374. print_binder_transaction_ilocked(m, thread->proc,
  4375. " incoming transaction", t);
  4376. t = t->to_parent;
  4377. } else {
  4378. print_binder_transaction_ilocked(m, thread->proc,
  4379. " bad transaction", t);
  4380. t = NULL;
  4381. }
  4382. }
  4383. list_for_each_entry(w, &thread->todo, entry) {
  4384. print_binder_work_ilocked(m, thread->proc, " ",
  4385. " pending transaction", w);
  4386. }
  4387. if (!print_always && m->count == header_pos)
  4388. m->count = start_pos;
  4389. }
  4390. static void print_binder_node_nilocked(struct seq_file *m,
  4391. struct binder_node *node)
  4392. {
  4393. struct binder_ref *ref;
  4394. struct binder_work *w;
  4395. int count;
  4396. WARN_ON(!spin_is_locked(&node->lock));
  4397. if (node->proc)
  4398. WARN_ON(!spin_is_locked(&node->proc->inner_lock));
  4399. count = 0;
  4400. hlist_for_each_entry(ref, &node->refs, node_entry)
  4401. count++;
  4402. seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
  4403. node->debug_id, (u64)node->ptr, (u64)node->cookie,
  4404. node->has_strong_ref, node->has_weak_ref,
  4405. node->local_strong_refs, node->local_weak_refs,
  4406. node->internal_strong_refs, count, node->tmp_refs);
  4407. if (count) {
  4408. seq_puts(m, " proc");
  4409. hlist_for_each_entry(ref, &node->refs, node_entry)
  4410. seq_printf(m, " %d", ref->proc->pid);
  4411. }
  4412. seq_puts(m, "\n");
  4413. if (node->proc) {
  4414. list_for_each_entry(w, &node->async_todo, entry)
  4415. print_binder_work_ilocked(m, node->proc, " ",
  4416. " pending async transaction", w);
  4417. }
  4418. }
  4419. static void print_binder_ref_olocked(struct seq_file *m,
  4420. struct binder_ref *ref)
  4421. {
  4422. WARN_ON(!spin_is_locked(&ref->proc->outer_lock));
  4423. binder_node_lock(ref->node);
  4424. seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
  4425. ref->data.debug_id, ref->data.desc,
  4426. ref->node->proc ? "" : "dead ",
  4427. ref->node->debug_id, ref->data.strong,
  4428. ref->data.weak, ref->death);
  4429. binder_node_unlock(ref->node);
  4430. }
  4431. static void print_binder_proc(struct seq_file *m,
  4432. struct binder_proc *proc, int print_all)
  4433. {
  4434. struct binder_work *w;
  4435. struct rb_node *n;
  4436. size_t start_pos = m->count;
  4437. size_t header_pos;
  4438. struct binder_node *last_node = NULL;
  4439. seq_printf(m, "proc %d\n", proc->pid);
  4440. seq_printf(m, "context %s\n", proc->context->name);
  4441. header_pos = m->count;
  4442. binder_inner_proc_lock(proc);
  4443. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  4444. print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
  4445. rb_node), print_all);
  4446. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
  4447. struct binder_node *node = rb_entry(n, struct binder_node,
  4448. rb_node);
  4449. /*
  4450. * take a temporary reference on the node so it
  4451. * survives and isn't removed from the tree
  4452. * while we print it.
  4453. */
  4454. binder_inc_node_tmpref_ilocked(node);
  4455. /* Need to drop inner lock to take node lock */
  4456. binder_inner_proc_unlock(proc);
  4457. if (last_node)
  4458. binder_put_node(last_node);
  4459. binder_node_inner_lock(node);
  4460. print_binder_node_nilocked(m, node);
  4461. binder_node_inner_unlock(node);
  4462. last_node = node;
  4463. binder_inner_proc_lock(proc);
  4464. }
  4465. binder_inner_proc_unlock(proc);
  4466. if (last_node)
  4467. binder_put_node(last_node);
  4468. if (print_all) {
  4469. binder_proc_lock(proc);
  4470. for (n = rb_first(&proc->refs_by_desc);
  4471. n != NULL;
  4472. n = rb_next(n))
  4473. print_binder_ref_olocked(m, rb_entry(n,
  4474. struct binder_ref,
  4475. rb_node_desc));
  4476. binder_proc_unlock(proc);
  4477. }
  4478. binder_alloc_print_allocated(m, &proc->alloc);
  4479. binder_inner_proc_lock(proc);
  4480. list_for_each_entry(w, &proc->todo, entry)
  4481. print_binder_work_ilocked(m, proc, " ",
  4482. " pending transaction", w);
  4483. list_for_each_entry(w, &proc->delivered_death, entry) {
  4484. seq_puts(m, " has delivered dead binder\n");
  4485. break;
  4486. }
  4487. binder_inner_proc_unlock(proc);
  4488. if (!print_all && m->count == header_pos)
  4489. m->count = start_pos;
  4490. }
  4491. static const char * const binder_return_strings[] = {
  4492. "BR_ERROR",
  4493. "BR_OK",
  4494. "BR_TRANSACTION",
  4495. "BR_REPLY",
  4496. "BR_ACQUIRE_RESULT",
  4497. "BR_DEAD_REPLY",
  4498. "BR_TRANSACTION_COMPLETE",
  4499. "BR_INCREFS",
  4500. "BR_ACQUIRE",
  4501. "BR_RELEASE",
  4502. "BR_DECREFS",
  4503. "BR_ATTEMPT_ACQUIRE",
  4504. "BR_NOOP",
  4505. "BR_SPAWN_LOOPER",
  4506. "BR_FINISHED",
  4507. "BR_DEAD_BINDER",
  4508. "BR_CLEAR_DEATH_NOTIFICATION_DONE",
  4509. "BR_FAILED_REPLY"
  4510. };
  4511. static const char * const binder_command_strings[] = {
  4512. "BC_TRANSACTION",
  4513. "BC_REPLY",
  4514. "BC_ACQUIRE_RESULT",
  4515. "BC_FREE_BUFFER",
  4516. "BC_INCREFS",
  4517. "BC_ACQUIRE",
  4518. "BC_RELEASE",
  4519. "BC_DECREFS",
  4520. "BC_INCREFS_DONE",
  4521. "BC_ACQUIRE_DONE",
  4522. "BC_ATTEMPT_ACQUIRE",
  4523. "BC_REGISTER_LOOPER",
  4524. "BC_ENTER_LOOPER",
  4525. "BC_EXIT_LOOPER",
  4526. "BC_REQUEST_DEATH_NOTIFICATION",
  4527. "BC_CLEAR_DEATH_NOTIFICATION",
  4528. "BC_DEAD_BINDER_DONE",
  4529. "BC_TRANSACTION_SG",
  4530. "BC_REPLY_SG",
  4531. };
  4532. static const char * const binder_objstat_strings[] = {
  4533. "proc",
  4534. "thread",
  4535. "node",
  4536. "ref",
  4537. "death",
  4538. "transaction",
  4539. "transaction_complete"
  4540. };
  4541. static void print_binder_stats(struct seq_file *m, const char *prefix,
  4542. struct binder_stats *stats)
  4543. {
  4544. int i;
  4545. BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
  4546. ARRAY_SIZE(binder_command_strings));
  4547. for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
  4548. int temp = atomic_read(&stats->bc[i]);
  4549. if (temp)
  4550. seq_printf(m, "%s%s: %d\n", prefix,
  4551. binder_command_strings[i], temp);
  4552. }
  4553. BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
  4554. ARRAY_SIZE(binder_return_strings));
  4555. for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
  4556. int temp = atomic_read(&stats->br[i]);
  4557. if (temp)
  4558. seq_printf(m, "%s%s: %d\n", prefix,
  4559. binder_return_strings[i], temp);
  4560. }
  4561. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  4562. ARRAY_SIZE(binder_objstat_strings));
  4563. BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
  4564. ARRAY_SIZE(stats->obj_deleted));
  4565. for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
  4566. int created = atomic_read(&stats->obj_created[i]);
  4567. int deleted = atomic_read(&stats->obj_deleted[i]);
  4568. if (created || deleted)
  4569. seq_printf(m, "%s%s: active %d total %d\n",
  4570. prefix,
  4571. binder_objstat_strings[i],
  4572. created - deleted,
  4573. created);
  4574. }
  4575. }
  4576. static void print_binder_proc_stats(struct seq_file *m,
  4577. struct binder_proc *proc)
  4578. {
  4579. struct binder_work *w;
  4580. struct rb_node *n;
  4581. int count, strong, weak;
  4582. size_t free_async_space =
  4583. binder_alloc_get_free_async_space(&proc->alloc);
  4584. seq_printf(m, "proc %d\n", proc->pid);
  4585. seq_printf(m, "context %s\n", proc->context->name);
  4586. count = 0;
  4587. binder_inner_proc_lock(proc);
  4588. for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
  4589. count++;
  4590. seq_printf(m, " threads: %d\n", count);
  4591. seq_printf(m, " requested threads: %d+%d/%d\n"
  4592. " ready threads %d\n"
  4593. " free async space %zd\n", proc->requested_threads,
  4594. proc->requested_threads_started, proc->max_threads,
  4595. proc->ready_threads,
  4596. free_async_space);
  4597. count = 0;
  4598. for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
  4599. count++;
  4600. binder_inner_proc_unlock(proc);
  4601. seq_printf(m, " nodes: %d\n", count);
  4602. count = 0;
  4603. strong = 0;
  4604. weak = 0;
  4605. binder_proc_lock(proc);
  4606. for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
  4607. struct binder_ref *ref = rb_entry(n, struct binder_ref,
  4608. rb_node_desc);
  4609. count++;
  4610. strong += ref->data.strong;
  4611. weak += ref->data.weak;
  4612. }
  4613. binder_proc_unlock(proc);
  4614. seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
  4615. count = binder_alloc_get_allocated_count(&proc->alloc);
  4616. seq_printf(m, " buffers: %d\n", count);
  4617. count = 0;
  4618. binder_inner_proc_lock(proc);
  4619. list_for_each_entry(w, &proc->todo, entry) {
  4620. if (w->type == BINDER_WORK_TRANSACTION)
  4621. count++;
  4622. }
  4623. binder_inner_proc_unlock(proc);
  4624. seq_printf(m, " pending transactions: %d\n", count);
  4625. print_binder_stats(m, " ", &proc->stats);
  4626. }
  4627. static int binder_state_show(struct seq_file *m, void *unused)
  4628. {
  4629. struct binder_proc *proc;
  4630. struct binder_node *node;
  4631. struct binder_node *last_node = NULL;
  4632. seq_puts(m, "binder state:\n");
  4633. spin_lock(&binder_dead_nodes_lock);
  4634. if (!hlist_empty(&binder_dead_nodes))
  4635. seq_puts(m, "dead nodes:\n");
  4636. hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
  4637. /*
  4638. * take a temporary reference on the node so it
  4639. * survives and isn't removed from the list
  4640. * while we print it.
  4641. */
  4642. node->tmp_refs++;
  4643. spin_unlock(&binder_dead_nodes_lock);
  4644. if (last_node)
  4645. binder_put_node(last_node);
  4646. binder_node_lock(node);
  4647. print_binder_node_nilocked(m, node);
  4648. binder_node_unlock(node);
  4649. last_node = node;
  4650. spin_lock(&binder_dead_nodes_lock);
  4651. }
  4652. spin_unlock(&binder_dead_nodes_lock);
  4653. if (last_node)
  4654. binder_put_node(last_node);
  4655. mutex_lock(&binder_procs_lock);
  4656. hlist_for_each_entry(proc, &binder_procs, proc_node)
  4657. print_binder_proc(m, proc, 1);
  4658. mutex_unlock(&binder_procs_lock);
  4659. return 0;
  4660. }
  4661. static int binder_stats_show(struct seq_file *m, void *unused)
  4662. {
  4663. struct binder_proc *proc;
  4664. seq_puts(m, "binder stats:\n");
  4665. print_binder_stats(m, "", &binder_stats);
  4666. mutex_lock(&binder_procs_lock);
  4667. hlist_for_each_entry(proc, &binder_procs, proc_node)
  4668. print_binder_proc_stats(m, proc);
  4669. mutex_unlock(&binder_procs_lock);
  4670. return 0;
  4671. }
  4672. static int binder_transactions_show(struct seq_file *m, void *unused)
  4673. {
  4674. struct binder_proc *proc;
  4675. seq_puts(m, "binder transactions:\n");
  4676. mutex_lock(&binder_procs_lock);
  4677. hlist_for_each_entry(proc, &binder_procs, proc_node)
  4678. print_binder_proc(m, proc, 0);
  4679. mutex_unlock(&binder_procs_lock);
  4680. return 0;
  4681. }
  4682. static int binder_proc_show(struct seq_file *m, void *unused)
  4683. {
  4684. struct binder_proc *itr;
  4685. int pid = (unsigned long)m->private;
  4686. mutex_lock(&binder_procs_lock);
  4687. hlist_for_each_entry(itr, &binder_procs, proc_node) {
  4688. if (itr->pid == pid) {
  4689. seq_puts(m, "binder proc state:\n");
  4690. print_binder_proc(m, itr, 1);
  4691. }
  4692. }
  4693. mutex_unlock(&binder_procs_lock);
  4694. return 0;
  4695. }
  4696. static void print_binder_transaction_log_entry(struct seq_file *m,
  4697. struct binder_transaction_log_entry *e)
  4698. {
  4699. int debug_id = READ_ONCE(e->debug_id_done);
  4700. /*
  4701. * read barrier to guarantee debug_id_done read before
  4702. * we print the log values
  4703. */
  4704. smp_rmb();
  4705. seq_printf(m,
  4706. "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
  4707. e->debug_id, (e->call_type == 2) ? "reply" :
  4708. ((e->call_type == 1) ? "async" : "call "), e->from_proc,
  4709. e->from_thread, e->to_proc, e->to_thread, e->context_name,
  4710. e->to_node, e->target_handle, e->data_size, e->offsets_size,
  4711. e->return_error, e->return_error_param,
  4712. e->return_error_line);
  4713. /*
  4714. * read-barrier to guarantee read of debug_id_done after
  4715. * done printing the fields of the entry
  4716. */
  4717. smp_rmb();
  4718. seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
  4719. "\n" : " (incomplete)\n");
  4720. }
  4721. static int binder_transaction_log_show(struct seq_file *m, void *unused)
  4722. {
  4723. struct binder_transaction_log *log = m->private;
  4724. unsigned int log_cur = atomic_read(&log->cur);
  4725. unsigned int count;
  4726. unsigned int cur;
  4727. int i;
  4728. count = log_cur + 1;
  4729. cur = count < ARRAY_SIZE(log->entry) && !log->full ?
  4730. 0 : count % ARRAY_SIZE(log->entry);
  4731. if (count > ARRAY_SIZE(log->entry) || log->full)
  4732. count = ARRAY_SIZE(log->entry);
  4733. for (i = 0; i < count; i++) {
  4734. unsigned int index = cur++ % ARRAY_SIZE(log->entry);
  4735. print_binder_transaction_log_entry(m, &log->entry[index]);
  4736. }
  4737. return 0;
  4738. }
  4739. static const struct file_operations binder_fops = {
  4740. .owner = THIS_MODULE,
  4741. .poll = binder_poll,
  4742. .unlocked_ioctl = binder_ioctl,
  4743. .compat_ioctl = binder_ioctl,
  4744. .mmap = binder_mmap,
  4745. .open = binder_open,
  4746. .flush = binder_flush,
  4747. .release = binder_release,
  4748. };
  4749. BINDER_DEBUG_ENTRY(state);
  4750. BINDER_DEBUG_ENTRY(stats);
  4751. BINDER_DEBUG_ENTRY(transactions);
  4752. BINDER_DEBUG_ENTRY(transaction_log);
  4753. static int __init init_binder_device(const char *name)
  4754. {
  4755. int ret;
  4756. struct binder_device *binder_device;
  4757. binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
  4758. if (!binder_device)
  4759. return -ENOMEM;
  4760. binder_device->miscdev.fops = &binder_fops;
  4761. binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
  4762. binder_device->miscdev.name = name;
  4763. binder_device->context.binder_context_mgr_uid = INVALID_UID;
  4764. binder_device->context.name = name;
  4765. mutex_init(&binder_device->context.context_mgr_node_lock);
  4766. ret = misc_register(&binder_device->miscdev);
  4767. if (ret < 0) {
  4768. kfree(binder_device);
  4769. return ret;
  4770. }
  4771. hlist_add_head(&binder_device->hlist, &binder_devices);
  4772. return ret;
  4773. }
  4774. static int __init binder_init(void)
  4775. {
  4776. int ret;
  4777. char *device_name, *device_names, *device_tmp;
  4778. struct binder_device *device;
  4779. struct hlist_node *tmp;
  4780. binder_alloc_shrinker_init();
  4781. atomic_set(&binder_transaction_log.cur, ~0U);
  4782. atomic_set(&binder_transaction_log_failed.cur, ~0U);
  4783. binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
  4784. if (binder_debugfs_dir_entry_root)
  4785. binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
  4786. binder_debugfs_dir_entry_root);
  4787. if (binder_debugfs_dir_entry_root) {
  4788. debugfs_create_file("state",
  4789. S_IRUGO,
  4790. binder_debugfs_dir_entry_root,
  4791. NULL,
  4792. &binder_state_fops);
  4793. debugfs_create_file("stats",
  4794. S_IRUGO,
  4795. binder_debugfs_dir_entry_root,
  4796. NULL,
  4797. &binder_stats_fops);
  4798. debugfs_create_file("transactions",
  4799. S_IRUGO,
  4800. binder_debugfs_dir_entry_root,
  4801. NULL,
  4802. &binder_transactions_fops);
  4803. debugfs_create_file("transaction_log",
  4804. S_IRUGO,
  4805. binder_debugfs_dir_entry_root,
  4806. &binder_transaction_log,
  4807. &binder_transaction_log_fops);
  4808. debugfs_create_file("failed_transaction_log",
  4809. S_IRUGO,
  4810. binder_debugfs_dir_entry_root,
  4811. &binder_transaction_log_failed,
  4812. &binder_transaction_log_fops);
  4813. }
  4814. /*
  4815. * Copy the module_parameter string, because we don't want to
  4816. * tokenize it in-place.
  4817. */
  4818. device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
  4819. if (!device_names) {
  4820. ret = -ENOMEM;
  4821. goto err_alloc_device_names_failed;
  4822. }
  4823. strcpy(device_names, binder_devices_param);
  4824. device_tmp = device_names;
  4825. while ((device_name = strsep(&device_tmp, ","))) {
  4826. ret = init_binder_device(device_name);
  4827. if (ret)
  4828. goto err_init_binder_device_failed;
  4829. }
  4830. return ret;
  4831. err_init_binder_device_failed:
  4832. hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
  4833. misc_deregister(&device->miscdev);
  4834. hlist_del(&device->hlist);
  4835. kfree(device);
  4836. }
  4837. kfree(device_names);
  4838. err_alloc_device_names_failed:
  4839. debugfs_remove_recursive(binder_debugfs_dir_entry_root);
  4840. return ret;
  4841. }
  4842. device_initcall(binder_init);
  4843. #define CREATE_TRACE_POINTS
  4844. #include "binder_trace.h"
  4845. MODULE_LICENSE("GPL v2");