binder.c 162 KB

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