lockdep.c 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703
  1. /*
  2. * kernel/lockdep.c
  3. *
  4. * Runtime locking correctness validator
  5. *
  6. * Started by Ingo Molnar:
  7. *
  8. * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  9. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
  10. *
  11. * this code maps all the lock dependencies as they occur in a live kernel
  12. * and will warn about the following classes of locking bugs:
  13. *
  14. * - lock inversion scenarios
  15. * - circular lock dependencies
  16. * - hardirq/softirq safe/unsafe locking bugs
  17. *
  18. * Bugs are reported even if the current locking scenario does not cause
  19. * any deadlock at this point.
  20. *
  21. * I.e. if anytime in the past two locks were taken in a different order,
  22. * even if it happened for another task, even if those were different
  23. * locks (but of the same class as this lock), this code will detect it.
  24. *
  25. * Thanks to Arjan van de Ven for coming up with the initial idea of
  26. * mapping lock dependencies runtime.
  27. */
  28. #define DISABLE_BRANCH_PROFILING
  29. #include <linux/mutex.h>
  30. #include <linux/sched.h>
  31. #include <linux/sched/clock.h>
  32. #include <linux/sched/task.h>
  33. #include <linux/sched/mm.h>
  34. #include <linux/delay.h>
  35. #include <linux/module.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/kallsyms.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/stacktrace.h>
  42. #include <linux/debug_locks.h>
  43. #include <linux/irqflags.h>
  44. #include <linux/utsname.h>
  45. #include <linux/hash.h>
  46. #include <linux/ftrace.h>
  47. #include <linux/stringify.h>
  48. #include <linux/bitops.h>
  49. #include <linux/gfp.h>
  50. #include <linux/random.h>
  51. #include <linux/jhash.h>
  52. #include <asm/sections.h>
  53. #include "lockdep_internals.h"
  54. #define CREATE_TRACE_POINTS
  55. #include <trace/events/lock.h>
  56. #ifdef CONFIG_LOCKDEP_CROSSRELEASE
  57. #include <linux/slab.h>
  58. #endif
  59. #ifdef CONFIG_PROVE_LOCKING
  60. int prove_locking = 1;
  61. module_param(prove_locking, int, 0644);
  62. #else
  63. #define prove_locking 0
  64. #endif
  65. #ifdef CONFIG_LOCK_STAT
  66. int lock_stat = 1;
  67. module_param(lock_stat, int, 0644);
  68. #else
  69. #define lock_stat 0
  70. #endif
  71. #ifdef MTK_LOCK_DEBUG_NEW_DEPENDENCY
  72. //#define MTK_LOCK_DEBUG_NEW_DEPENDENCY_AEE
  73. /*
  74. * This is a debug function to catch the lock dependency at runtime.
  75. * This help to catch the first time of the lock dependency appeared
  76. * and show the backtrace of the held locks.
  77. *
  78. * 1. use "cat /proc/lockdep_chains" to see the full lock name and
  79. * which lock dependency you want to catch.
  80. * 2. According to the order of lock dependency to fill full lock names
  81. * into PREV_LOCK_NAME and NEXT_LOCK_NAME.
  82. * 3. According to the depth of lock dependency to adjust LOCK_DEPTH.
  83. * This help to limit the lock dependency scope and increase the
  84. * possibility to catch the lock dependency which you want.
  85. *
  86. * e.g.
  87. * cat /proc/lockdep_chains
  88. *
  89. * irq_context: 0
  90. * [ffffff800a1b1860] &(&lockA)->rlock
  91. * [ffffff800a1d5b98] &(&lockB)->rlock
  92. * [ffffff800b179b40] &(&lockC)->rlock
  93. *
  94. * You have to set as following to catch this dependency.
  95. * set PREV_LOCK_NAME as "&(&lockB)->rlock"
  96. * set NEXT_LOCK_NAME as "&(&lockC)->rlock"
  97. * set LOCK_DEPTH as 2 (because there are 2 locks held before
  98. * the final &(&lockC)->rlock)
  99. *
  100. * You can set LOCK_DEPTH as 0 to skip this condition.
  101. * Without this condition, you might get more than one dependency.
  102. *
  103. * e.g.
  104. * irq_context: 0
  105. * [ffffff800a1d5b98] &(&lockB)->rlock
  106. * [ffffff800b179b40] &(&lockC)->rlock
  107. *
  108. * irq_context: 0
  109. * [ffffff800a1d3b90] &(&lockE)->rlock
  110. * [ffffff800a1d4b08] &(&lockA)->rlock
  111. * [ffffff800a1d5b98] &(&lockB)->rlock
  112. * [ffffff800b179b40] &(&lockC)->rlock
  113. */
  114. #define PREV_LOCK_NAME "&(&lockB)->rlock"
  115. #define NEXT_LOCK_NAME "&(&lockC)->rlock"
  116. #define LOCK_DEPTH 0
  117. #endif
  118. #define MAX_LOCK_NAME 128
  119. #ifdef CONFIG_PROVE_LOCKING
  120. static noinline int trace_circular_bug(
  121. struct lock_list *this,
  122. struct lock_list *target,
  123. struct held_lock *check_src,
  124. struct held_lock *check_tgt,
  125. struct stack_trace *trace);
  126. static bool is_log_lock_held(struct task_struct *curr)
  127. {
  128. int i, depth = curr->lockdep_depth;
  129. struct held_lock *hlock = curr->held_locks;
  130. struct lockdep_map *instance;
  131. if (!depth)
  132. return false;
  133. for (i = 0; i < depth; i++) {
  134. instance = (hlock + i)->instance;
  135. /* used by pr_xxx */
  136. if (!strcmp(instance->name, "logbuf_lock"))
  137. return true;
  138. /* used by console */
  139. if (!strcmp(instance->name, "&(&port->lock)->rlock"))
  140. return true;
  141. if (!strcmp(instance->name, "&port_lock_key"))
  142. return true;
  143. }
  144. return false;
  145. }
  146. static void lock_dbg(const char *msg, ...)
  147. {
  148. char buf[256];
  149. va_list args;
  150. va_start(args, msg);
  151. vsnprintf(buf, sizeof(buf), msg, args);
  152. va_end(args);
  153. trace_lock_dbg(buf);
  154. }
  155. #endif
  156. #ifdef MTK_LOCK_MONITOR
  157. static char buf_lock[256];
  158. static unsigned int lock_mon_enable = 1;
  159. static unsigned int lock_mon_1st_th_ms = 2000; /* show name */
  160. static unsigned int lock_mon_2nd_th_ms = 5000; /* show backtrace */
  161. static unsigned int lock_mon_3rd_th_ms = 15000; /* trigger Kernel API dump */
  162. static unsigned int lock_mon_period_ms = 2500; /* check held locks */
  163. static unsigned int lock_mon_period_cnt = 4; /* show more information */
  164. static unsigned int lock_mon_door;
  165. #define MAX_WARN_LOCKS 100
  166. static unsigned int warn_locks;
  167. static const char * const held_lock_white_list[] = {
  168. "&tty->ldisc_sem",
  169. "&ldata->atomic_read_lock",
  170. "&f->f_pos_lock",
  171. "&p->lock",
  172. "&of->mutex",
  173. "&epfile->mutex",
  174. "&session->notif_wait_lock",
  175. "(&item_q->work)",
  176. "\"%s\"\"cmdq_flushq\"",
  177. "\"events\"" /* aee workqueue */
  178. };
  179. #define lock_mon_enabled() lock_mon_enable
  180. static void
  181. __add_held_lock(struct lockdep_map *lock, unsigned int subclass,
  182. int trylock, int read, int check, int hardirqs_off,
  183. struct lockdep_map *nest_lock, unsigned long ip,
  184. int references, int pin_count);
  185. static void
  186. __remove_held_lock(struct lockdep_map *lock, int nested, unsigned long ip);
  187. #else
  188. #define lock_mon_enabled() 0
  189. static void
  190. __add_held_lock(struct lockdep_map *lock, unsigned int subclass,
  191. int trylock, int read, int check, int hardirqs_off,
  192. struct lockdep_map *nest_lock, unsigned long ip,
  193. int references, int pin_count)
  194. {
  195. }
  196. static void
  197. __remove_held_lock(struct lockdep_map *lock, int nested, unsigned long ip)
  198. {
  199. }
  200. #endif
  201. static unsigned long long sec_high(unsigned long long nsec)
  202. {
  203. do_div(nsec, 1000000000);
  204. return nsec;
  205. }
  206. static unsigned long sec_low(unsigned long long nsec)
  207. {
  208. /* exclude part of nsec */
  209. return do_div(nsec, 1000000000)/1000;
  210. }
  211. #ifndef TO_KERNEL_LOG
  212. #define TO_KERNEL_LOG 0x1
  213. #define TO_FTRACE 0x2
  214. #define TO_DEFERRED 0x4
  215. #define TO_SRAM 0x8
  216. #define TO_BOTH (TO_KERNEL_LOG | TO_FTRACE)
  217. #define TO_BOTH_SAVE (TO_DEFERRED | TO_FTRACE)
  218. #endif
  219. #define add_suffix(out) (out == TO_SRAM) ? "\n" : ""
  220. #define MAX_WARN_MSG 120
  221. static unsigned int warn_msgs;
  222. static void lock_mon_msg(char *buf, int out)
  223. {
  224. if (out & TO_FTRACE)
  225. trace_lock_monitor(buf);
  226. /* check warn_msgs to avoid printing too much log */
  227. if (out & TO_KERNEL_LOG && warn_msgs++ < MAX_WARN_MSG) {
  228. pr_info("%s\n", buf);
  229. if (warn_msgs == MAX_WARN_MSG)
  230. pr_info(" See SYS_FTRACE for more log\n");
  231. }
  232. #ifdef CONFIG_MTK_RAM_CONSOLE
  233. if (out & TO_SRAM)
  234. aee_sram_fiq_log(buf);
  235. #endif
  236. }
  237. static void lockdep_aee(void);
  238. /*
  239. * lockdep_lock: protects the lockdep graph, the hashes and the
  240. * class/list/hash allocators.
  241. *
  242. * This is one of the rare exceptions where it's justified
  243. * to use a raw spinlock - we really dont want the spinlock
  244. * code to recurse back into the lockdep code...
  245. */
  246. static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  247. static int graph_lock(void)
  248. {
  249. arch_spin_lock(&lockdep_lock);
  250. /*
  251. * Make sure that if another CPU detected a bug while
  252. * walking the graph we dont change it (while the other
  253. * CPU is busy printing out stuff with the graph lock
  254. * dropped already)
  255. */
  256. if (!debug_locks) {
  257. arch_spin_unlock(&lockdep_lock);
  258. return 0;
  259. }
  260. /* prevent any recursions within lockdep from causing deadlocks */
  261. current->lockdep_recursion++;
  262. return 1;
  263. }
  264. static inline int graph_unlock(void)
  265. {
  266. if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
  267. /*
  268. * The lockdep graph lock isn't locked while we expect it to
  269. * be, we're confused now, bye!
  270. */
  271. return DEBUG_LOCKS_WARN_ON(1);
  272. }
  273. current->lockdep_recursion--;
  274. arch_spin_unlock(&lockdep_lock);
  275. return 0;
  276. }
  277. /*
  278. * Turn lock debugging off and return with 0 if it was off already,
  279. * and also release the graph lock:
  280. */
  281. static inline int debug_locks_off_graph_unlock(void)
  282. {
  283. int ret = debug_locks_off();
  284. arch_spin_unlock(&lockdep_lock);
  285. return ret;
  286. }
  287. unsigned long nr_list_entries;
  288. static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
  289. /*
  290. * All data structures here are protected by the global debug_lock.
  291. *
  292. * Mutex key structs only get allocated, once during bootup, and never
  293. * get freed - this significantly simplifies the debugging code.
  294. */
  295. unsigned long nr_lock_classes;
  296. static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
  297. static inline struct lock_class *hlock_class(struct held_lock *hlock)
  298. {
  299. if (!hlock->class_idx) {
  300. /*
  301. * Someone passed in garbage, we give up.
  302. */
  303. DEBUG_LOCKS_WARN_ON(1);
  304. return NULL;
  305. }
  306. return lock_classes + hlock->class_idx - 1;
  307. }
  308. #ifdef CONFIG_LOCK_STAT
  309. static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
  310. static inline u64 lockstat_clock(void)
  311. {
  312. return local_clock();
  313. }
  314. static int lock_point(unsigned long points[], unsigned long ip)
  315. {
  316. int i;
  317. for (i = 0; i < LOCKSTAT_POINTS; i++) {
  318. if (points[i] == 0) {
  319. points[i] = ip;
  320. break;
  321. }
  322. if (points[i] == ip)
  323. break;
  324. }
  325. return i;
  326. }
  327. static void lock_time_inc(struct lock_time *lt, u64 time)
  328. {
  329. if (time > lt->max)
  330. lt->max = time;
  331. if (time < lt->min || !lt->nr)
  332. lt->min = time;
  333. lt->total += time;
  334. lt->nr++;
  335. }
  336. static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
  337. {
  338. if (!src->nr)
  339. return;
  340. if (src->max > dst->max)
  341. dst->max = src->max;
  342. if (src->min < dst->min || !dst->nr)
  343. dst->min = src->min;
  344. dst->total += src->total;
  345. dst->nr += src->nr;
  346. }
  347. struct lock_class_stats lock_stats(struct lock_class *class)
  348. {
  349. struct lock_class_stats stats;
  350. int cpu, i;
  351. memset(&stats, 0, sizeof(struct lock_class_stats));
  352. for_each_possible_cpu(cpu) {
  353. struct lock_class_stats *pcs =
  354. &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
  355. for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
  356. stats.contention_point[i] += pcs->contention_point[i];
  357. for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
  358. stats.contending_point[i] += pcs->contending_point[i];
  359. lock_time_add(&pcs->read_waittime, &stats.read_waittime);
  360. lock_time_add(&pcs->write_waittime, &stats.write_waittime);
  361. lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
  362. lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
  363. for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
  364. stats.bounces[i] += pcs->bounces[i];
  365. }
  366. return stats;
  367. }
  368. void clear_lock_stats(struct lock_class *class)
  369. {
  370. int cpu;
  371. for_each_possible_cpu(cpu) {
  372. struct lock_class_stats *cpu_stats =
  373. &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
  374. memset(cpu_stats, 0, sizeof(struct lock_class_stats));
  375. }
  376. memset(class->contention_point, 0, sizeof(class->contention_point));
  377. memset(class->contending_point, 0, sizeof(class->contending_point));
  378. }
  379. static struct lock_class_stats *get_lock_stats(struct lock_class *class)
  380. {
  381. return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
  382. }
  383. static void put_lock_stats(struct lock_class_stats *stats)
  384. {
  385. put_cpu_var(cpu_lock_stats);
  386. }
  387. static void lock_release_holdtime(struct held_lock *hlock)
  388. {
  389. struct lock_class_stats *stats;
  390. u64 holdtime;
  391. if (!lock_stat)
  392. return;
  393. holdtime = lockstat_clock() - hlock->holdtime_stamp;
  394. stats = get_lock_stats(hlock_class(hlock));
  395. if (hlock->read)
  396. lock_time_inc(&stats->read_holdtime, holdtime);
  397. else
  398. lock_time_inc(&stats->write_holdtime, holdtime);
  399. put_lock_stats(stats);
  400. }
  401. #else
  402. static inline void lock_release_holdtime(struct held_lock *hlock)
  403. {
  404. }
  405. #endif
  406. /*
  407. * We keep a global list of all lock classes. The list only grows,
  408. * never shrinks. The list is only accessed with the lockdep
  409. * spinlock lock held.
  410. */
  411. LIST_HEAD(all_lock_classes);
  412. /*
  413. * The lockdep classes are in a hash-table as well, for fast lookup:
  414. */
  415. #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
  416. #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
  417. #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
  418. #define classhashentry(key) (classhash_table + __classhashfn((key)))
  419. static struct hlist_head classhash_table[CLASSHASH_SIZE];
  420. /*
  421. * We put the lock dependency chains into a hash-table as well, to cache
  422. * their existence:
  423. */
  424. #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
  425. #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
  426. #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
  427. #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
  428. static struct hlist_head chainhash_table[CHAINHASH_SIZE];
  429. /*
  430. * The hash key of the lock dependency chains is a hash itself too:
  431. * it's a hash of all locks taken up to that lock, including that lock.
  432. * It's a 64-bit hash, because it's important for the keys to be
  433. * unique.
  434. */
  435. static inline u64 iterate_chain_key(u64 key, u32 idx)
  436. {
  437. u32 k0 = key, k1 = key >> 32;
  438. __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
  439. return k0 | (u64)k1 << 32;
  440. }
  441. void lockdep_off(void)
  442. {
  443. current->lockdep_recursion++;
  444. }
  445. EXPORT_SYMBOL(lockdep_off);
  446. void lockdep_on(void)
  447. {
  448. current->lockdep_recursion--;
  449. }
  450. EXPORT_SYMBOL(lockdep_on);
  451. /*
  452. * Debugging switches:
  453. */
  454. #define VERBOSE 0
  455. #define VERY_VERBOSE 0
  456. #if VERBOSE
  457. # define HARDIRQ_VERBOSE 1
  458. # define SOFTIRQ_VERBOSE 1
  459. #else
  460. # define HARDIRQ_VERBOSE 0
  461. # define SOFTIRQ_VERBOSE 0
  462. #endif
  463. #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
  464. /*
  465. * Quick filtering for interesting events:
  466. */
  467. static int class_filter(struct lock_class *class)
  468. {
  469. #if 0
  470. /* Example */
  471. if (class->name_version == 1 &&
  472. !strcmp(class->name, "lockname"))
  473. return 1;
  474. if (class->name_version == 1 &&
  475. !strcmp(class->name, "&struct->lockfield"))
  476. return 1;
  477. #endif
  478. /* Filter everything else. 1 would be to allow everything else */
  479. return 0;
  480. }
  481. #endif
  482. static int verbose(struct lock_class *class)
  483. {
  484. #if VERBOSE
  485. return class_filter(class);
  486. #endif
  487. return 0;
  488. }
  489. /*
  490. * Stack-trace: tightly packed array of stack backtrace
  491. * addresses. Protected by the graph_lock.
  492. */
  493. unsigned long nr_stack_trace_entries;
  494. static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
  495. static void print_lockdep_off(const char *bug_msg)
  496. {
  497. printk(KERN_DEBUG "%s\n", bug_msg);
  498. printk(KERN_DEBUG "turning off the locking correctness validator.\n");
  499. #ifdef CONFIG_LOCK_STAT
  500. printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
  501. #endif
  502. }
  503. static int save_trace(struct stack_trace *trace)
  504. {
  505. trace->nr_entries = 0;
  506. trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
  507. trace->entries = stack_trace + nr_stack_trace_entries;
  508. trace->skip = 3;
  509. save_stack_trace(trace);
  510. /*
  511. * Some daft arches put -1 at the end to indicate its a full trace.
  512. *
  513. * <rant> this is buggy anyway, since it takes a whole extra entry so a
  514. * complete trace that maxes out the entries provided will be reported
  515. * as incomplete, friggin useless </rant>
  516. */
  517. if (trace->nr_entries != 0 &&
  518. trace->entries[trace->nr_entries-1] == ULONG_MAX)
  519. trace->nr_entries--;
  520. trace->max_entries = trace->nr_entries;
  521. nr_stack_trace_entries += trace->nr_entries;
  522. if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
  523. if (!debug_locks_off_graph_unlock())
  524. return 0;
  525. print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
  526. dump_stack();
  527. lockdep_aee();
  528. return 0;
  529. }
  530. return 1;
  531. }
  532. unsigned int nr_hardirq_chains;
  533. unsigned int nr_softirq_chains;
  534. unsigned int nr_process_chains;
  535. unsigned int max_lockdep_depth;
  536. #ifdef CONFIG_DEBUG_LOCKDEP
  537. /*
  538. * Various lockdep statistics:
  539. */
  540. DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
  541. #endif
  542. /*
  543. * Locking printouts:
  544. */
  545. #define __USAGE(__STATE) \
  546. [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
  547. [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
  548. [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
  549. [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
  550. static const char *usage_str[] =
  551. {
  552. #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
  553. #include "lockdep_states.h"
  554. #undef LOCKDEP_STATE
  555. [LOCK_USED] = "INITIAL USE",
  556. };
  557. const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
  558. {
  559. return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
  560. }
  561. static inline unsigned long lock_flag(enum lock_usage_bit bit)
  562. {
  563. return 1UL << bit;
  564. }
  565. #ifdef MTK_LOCK_DEBUG_NEW_DEPENDENCY
  566. static int check_lock_name(struct lock_class *class, const char *lock_name)
  567. {
  568. char str[KSYM_NAME_LEN];
  569. const char *name;
  570. char full_name[128], tmp[16];
  571. name = class->name;
  572. if (!name) {
  573. name = __get_key_name(class->key, str);
  574. snprintf(full_name, sizeof(full_name), "%s", name);
  575. } else {
  576. snprintf(full_name, sizeof(full_name), "%s", name);
  577. if (class->name_version > 1) {
  578. snprintf(tmp, sizeof(tmp), "#%d", class->name_version);
  579. strlcat(full_name, tmp, sizeof(full_name));
  580. }
  581. if (class->subclass) {
  582. snprintf(tmp, sizeof(tmp), "/%d", class->subclass);
  583. strlcat(full_name, tmp, sizeof(full_name));
  584. }
  585. }
  586. if (!strcmp(full_name, lock_name))
  587. return 1;
  588. return 0;
  589. }
  590. #endif
  591. static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
  592. {
  593. char c = '.';
  594. if (class->usage_mask & lock_flag(bit + 2))
  595. c = '+';
  596. if (class->usage_mask & lock_flag(bit)) {
  597. c = '-';
  598. if (class->usage_mask & lock_flag(bit + 2))
  599. c = '?';
  600. }
  601. return c;
  602. }
  603. void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
  604. {
  605. int i = 0;
  606. #define LOCKDEP_STATE(__STATE) \
  607. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
  608. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
  609. #include "lockdep_states.h"
  610. #undef LOCKDEP_STATE
  611. usage[i] = '\0';
  612. }
  613. static void __print_lock_name(struct lock_class *class)
  614. {
  615. char str[KSYM_NAME_LEN];
  616. const char *name;
  617. name = class->name;
  618. if (!name) {
  619. name = __get_key_name(class->key, str);
  620. printk(KERN_CONT "%s", name);
  621. } else {
  622. printk(KERN_CONT "%s", name);
  623. if (class->name_version > 1)
  624. printk(KERN_CONT "#%d", class->name_version);
  625. if (class->subclass)
  626. printk(KERN_CONT "/%d", class->subclass);
  627. }
  628. }
  629. static void print_lock_name(struct lock_class *class)
  630. {
  631. char usage[LOCK_USAGE_CHARS];
  632. get_usage_chars(class, usage);
  633. printk(KERN_CONT " (");
  634. __print_lock_name(class);
  635. printk(KERN_CONT "){%s}-{%hd:%hd}", usage,
  636. class->wait_type_outer ?: class->wait_type_inner,
  637. class->wait_type_inner);
  638. }
  639. static void print_lockdep_cache(struct lockdep_map *lock)
  640. {
  641. const char *name;
  642. char str[KSYM_NAME_LEN];
  643. name = lock->name;
  644. if (!name)
  645. name = __get_key_name(lock->key->subkeys, str);
  646. printk(KERN_CONT "%s", name);
  647. }
  648. static void print_lock(struct held_lock *hlock)
  649. {
  650. /*
  651. * We can be called locklessly through debug_show_all_locks() so be
  652. * extra careful, the hlock might have been released and cleared.
  653. */
  654. unsigned int class_idx = hlock->class_idx;
  655. /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
  656. barrier();
  657. if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
  658. printk(KERN_CONT "<RELEASED>\n");
  659. return;
  660. }
  661. print_lock_name(lock_classes + class_idx - 1);
  662. pr_cont(", at: [<%p>] %pS from [%lld.%06lu]\n",
  663. (void *)hlock->acquire_ip, (void *)hlock->acquire_ip,
  664. sec_high(hlock->timestamp), sec_low(hlock->timestamp));
  665. }
  666. /* MTK_LOCK_DEBUG_HELD_LOCK */
  667. void held_lock_save_trace(struct stack_trace *trace, unsigned long *entries)
  668. {
  669. #if !defined(CONFIG_KASAN) && !defined(CONFIG_UBSAN)
  670. trace->nr_entries = 0;
  671. trace->max_entries = HELD_LOCK_STACK_TRACE_DEPTH;
  672. trace->entries = entries;
  673. trace->skip = 2;
  674. save_stack_trace(trace);
  675. if (trace->nr_entries != 0 &&
  676. trace->entries[trace->nr_entries - 1] == ULONG_MAX)
  677. trace->nr_entries--;
  678. trace->nr_entries--;
  679. #endif
  680. }
  681. void held_lock_show_trace(struct stack_trace *trace, int output)
  682. {
  683. #if !defined(CONFIG_KASAN) && !defined(CONFIG_UBSAN)
  684. int i;
  685. char buf[256];
  686. if (!trace || !trace->entries)
  687. return;
  688. for (i = 0; i < trace->nr_entries; i++) {
  689. snprintf(buf, sizeof(buf), "%*c%pS%s", 6, ' ',
  690. (void *)trace->entries[i], add_suffix(output));
  691. lock_mon_msg(buf, output);
  692. }
  693. #endif
  694. }
  695. static void lockdep_print_held_locks(struct task_struct *curr)
  696. {
  697. int i, depth = curr->lockdep_depth;
  698. struct held_lock *hlock;
  699. if (!depth) {
  700. printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
  701. return;
  702. }
  703. pr_info("%d lock%s held by %s/%d/[%ld] on CPU#%d:\n",
  704. depth, depth > 1 ? "s" : "", curr->comm,
  705. curr->pid, curr->state, task_cpu(curr));
  706. hlock = curr->held_locks;
  707. for (i = 0; i < depth; i++) {
  708. printk(" #%d: ", i);
  709. print_lock(hlock + i);
  710. /* MTK_LOCK_DEBUG_HELD_LOCK */
  711. held_lock_show_trace(&(hlock + i)->trace, TO_KERNEL_LOG);
  712. }
  713. }
  714. static void print_kernel_ident(void)
  715. {
  716. printk("%s %.*s %s\n", init_utsname()->release,
  717. (int)strcspn(init_utsname()->version, " "),
  718. init_utsname()->version,
  719. print_tainted());
  720. }
  721. static int very_verbose(struct lock_class *class)
  722. {
  723. #if VERY_VERBOSE
  724. return class_filter(class);
  725. #endif
  726. return 0;
  727. }
  728. /*
  729. * Is this the address of a static object:
  730. */
  731. #ifdef __KERNEL__
  732. static int static_obj(void *obj)
  733. {
  734. unsigned long start = (unsigned long) &_stext,
  735. end = (unsigned long) &_end,
  736. addr = (unsigned long) obj;
  737. /*
  738. * static variable?
  739. */
  740. if ((addr >= start) && (addr < end))
  741. return 1;
  742. if (arch_is_kernel_data(addr))
  743. return 1;
  744. /*
  745. * in-kernel percpu var?
  746. */
  747. if (is_kernel_percpu_address(addr))
  748. return 1;
  749. /*
  750. * module static or percpu var?
  751. */
  752. return is_module_address(addr) || is_module_percpu_address(addr);
  753. }
  754. #endif
  755. /*
  756. * To make lock name printouts unique, we calculate a unique
  757. * class->name_version generation counter:
  758. */
  759. static int count_matching_names(struct lock_class *new_class)
  760. {
  761. struct lock_class *class;
  762. int count = 0;
  763. if (!new_class->name)
  764. return 0;
  765. list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
  766. if (new_class->key - new_class->subclass == class->key)
  767. return class->name_version;
  768. if (class->name && !strcmp(class->name, new_class->name))
  769. count = max(count, class->name_version);
  770. }
  771. return count + 1;
  772. }
  773. /*
  774. * Register a lock's class in the hash-table, if the class is not present
  775. * yet. Otherwise we look it up. We cache the result in the lock object
  776. * itself, so actual lookup of the hash should be once per lock object.
  777. */
  778. static inline struct lock_class *
  779. look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  780. {
  781. struct lockdep_subclass_key *key;
  782. struct hlist_head *hash_head;
  783. struct lock_class *class;
  784. bool is_static = false;
  785. if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
  786. debug_locks_off();
  787. printk(KERN_ERR
  788. "BUG: looking up invalid subclass: %u\n", subclass);
  789. printk(KERN_ERR
  790. "turning off the locking correctness validator.\n");
  791. dump_stack();
  792. lockdep_aee();
  793. return NULL;
  794. }
  795. /*
  796. * Static locks do not have their class-keys yet - for them the key
  797. * is the lock object itself. If the lock is in the per cpu area,
  798. * the canonical address of the lock (per cpu offset removed) is
  799. * used.
  800. */
  801. if (unlikely(!lock->key)) {
  802. unsigned long can_addr, addr = (unsigned long)lock;
  803. if (__is_kernel_percpu_address(addr, &can_addr))
  804. lock->key = (void *)can_addr;
  805. else if (__is_module_percpu_address(addr, &can_addr))
  806. lock->key = (void *)can_addr;
  807. else if (static_obj(lock))
  808. lock->key = (void *)lock;
  809. else
  810. return ERR_PTR(-EINVAL);
  811. is_static = true;
  812. }
  813. /*
  814. * NOTE: the class-key must be unique. For dynamic locks, a static
  815. * lock_class_key variable is passed in through the mutex_init()
  816. * (or spin_lock_init()) call - which acts as the key. For static
  817. * locks we use the lock object itself as the key.
  818. */
  819. BUILD_BUG_ON(sizeof(struct lock_class_key) >
  820. sizeof(struct lockdep_map));
  821. key = lock->key->subkeys + subclass;
  822. hash_head = classhashentry(key);
  823. /*
  824. * We do an RCU walk of the hash, see lockdep_free_key_range().
  825. */
  826. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  827. return NULL;
  828. hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
  829. if (class->key == key) {
  830. /*
  831. * Huh! same key, different name? Did someone trample
  832. * on some memory? We're most confused.
  833. */
  834. WARN_ON_ONCE(class->name != lock->name);
  835. return class;
  836. }
  837. }
  838. return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL);
  839. }
  840. #ifdef CONFIG_LOCKDEP_CROSSRELEASE
  841. static void cross_init(struct lockdep_map *lock, int cross);
  842. static int cross_lock(struct lockdep_map *lock);
  843. static int lock_acquire_crosslock(struct held_lock *hlock);
  844. static int lock_release_crosslock(struct lockdep_map *lock);
  845. #else
  846. static inline void cross_init(struct lockdep_map *lock, int cross) {}
  847. static inline int cross_lock(struct lockdep_map *lock) { return 0; }
  848. static inline int lock_acquire_crosslock(struct held_lock *hlock) { return 2; }
  849. static inline int lock_release_crosslock(struct lockdep_map *lock) { return 2; }
  850. #endif
  851. /*
  852. * Register a lock's class in the hash-table, if the class is not present
  853. * yet. Otherwise we look it up. We cache the result in the lock object
  854. * itself, so actual lookup of the hash should be once per lock object.
  855. */
  856. static struct lock_class *
  857. register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
  858. {
  859. struct lockdep_subclass_key *key;
  860. struct hlist_head *hash_head;
  861. struct lock_class *class;
  862. DEBUG_LOCKS_WARN_ON(!irqs_disabled());
  863. class = look_up_lock_class(lock, subclass);
  864. if (likely(!IS_ERR_OR_NULL(class)))
  865. goto out_set_class_cache;
  866. /*
  867. * Debug-check: all keys must be persistent!
  868. */
  869. if (IS_ERR(class)) {
  870. debug_locks_off();
  871. printk("INFO: trying to register non-static key.\n");
  872. printk("the code is fine but needs lockdep annotation.\n");
  873. printk("turning off the locking correctness validator.\n");
  874. dump_stack();
  875. lockdep_aee();
  876. return NULL;
  877. }
  878. key = lock->key->subkeys + subclass;
  879. hash_head = classhashentry(key);
  880. if (!graph_lock()) {
  881. return NULL;
  882. }
  883. /*
  884. * We have to do the hash-walk again, to avoid races
  885. * with another CPU:
  886. */
  887. hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
  888. if (class->key == key)
  889. goto out_unlock_set;
  890. }
  891. /*
  892. * Allocate a new key from the static array, and add it to
  893. * the hash:
  894. */
  895. if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
  896. if (!debug_locks_off_graph_unlock()) {
  897. return NULL;
  898. }
  899. print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
  900. dump_stack();
  901. return NULL;
  902. }
  903. class = lock_classes + nr_lock_classes++;
  904. debug_atomic_inc(nr_unused_locks);
  905. class->key = key;
  906. class->name = lock->name;
  907. class->subclass = subclass;
  908. INIT_LIST_HEAD(&class->lock_entry);
  909. INIT_LIST_HEAD(&class->locks_before);
  910. INIT_LIST_HEAD(&class->locks_after);
  911. class->name_version = count_matching_names(class);
  912. class->wait_type_inner = lock->wait_type_inner;
  913. class->wait_type_outer = lock->wait_type_outer;
  914. /*
  915. * We use RCU's safe list-add method to make
  916. * parallel walking of the hash-list safe:
  917. */
  918. hlist_add_head_rcu(&class->hash_entry, hash_head);
  919. /*
  920. * Add it to the global list of classes:
  921. */
  922. list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
  923. if (verbose(class)) {
  924. graph_unlock();
  925. printk("\nnew class %p: %s", class->key, class->name);
  926. if (class->name_version > 1)
  927. printk(KERN_CONT "#%d", class->name_version);
  928. printk(KERN_CONT "\n");
  929. dump_stack();
  930. if (!graph_lock()) {
  931. return NULL;
  932. }
  933. }
  934. out_unlock_set:
  935. graph_unlock();
  936. out_set_class_cache:
  937. if (!subclass || force)
  938. lock->class_cache[0] = class;
  939. else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  940. lock->class_cache[subclass] = class;
  941. /*
  942. * Hash collision, did we smoke some? We found a class with a matching
  943. * hash but the subclass -- which is hashed in -- didn't match.
  944. */
  945. if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
  946. return NULL;
  947. return class;
  948. }
  949. #ifdef CONFIG_PROVE_LOCKING
  950. /*
  951. * Allocate a lockdep entry. (assumes the graph_lock held, returns
  952. * with NULL on failure)
  953. */
  954. static struct lock_list *alloc_list_entry(void)
  955. {
  956. if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
  957. if (!debug_locks_off_graph_unlock())
  958. return NULL;
  959. print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
  960. dump_stack();
  961. lockdep_aee();
  962. return NULL;
  963. }
  964. return list_entries + nr_list_entries++;
  965. }
  966. /*
  967. * Add a new dependency to the head of the list:
  968. */
  969. static int add_lock_to_list(struct lock_class *this, struct list_head *head,
  970. unsigned long ip, int distance,
  971. struct stack_trace *trace)
  972. {
  973. struct lock_list *entry;
  974. /*
  975. * Lock not present yet - get a new dependency struct and
  976. * add it to the list:
  977. */
  978. entry = alloc_list_entry();
  979. if (!entry)
  980. return 0;
  981. entry->class = this;
  982. entry->distance = distance;
  983. entry->trace = *trace;
  984. /*
  985. * Both allocation and removal are done under the graph lock; but
  986. * iteration is under RCU-sched; see look_up_lock_class() and
  987. * lockdep_free_key_range().
  988. */
  989. list_add_tail_rcu(&entry->entry, head);
  990. return 1;
  991. }
  992. /*
  993. * For good efficiency of modular, we use power of 2
  994. */
  995. #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
  996. #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
  997. /*
  998. * The circular_queue and helpers is used to implement the
  999. * breadth-first search(BFS)algorithem, by which we can build
  1000. * the shortest path from the next lock to be acquired to the
  1001. * previous held lock if there is a circular between them.
  1002. */
  1003. struct circular_queue {
  1004. unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
  1005. unsigned int front, rear;
  1006. };
  1007. static struct circular_queue lock_cq;
  1008. unsigned int max_bfs_queue_depth;
  1009. static unsigned int lockdep_dependency_gen_id;
  1010. static inline void __cq_init(struct circular_queue *cq)
  1011. {
  1012. cq->front = cq->rear = 0;
  1013. lockdep_dependency_gen_id++;
  1014. }
  1015. static inline int __cq_empty(struct circular_queue *cq)
  1016. {
  1017. return (cq->front == cq->rear);
  1018. }
  1019. static inline int __cq_full(struct circular_queue *cq)
  1020. {
  1021. return ((cq->rear + 1) & CQ_MASK) == cq->front;
  1022. }
  1023. static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
  1024. {
  1025. if (__cq_full(cq))
  1026. return -1;
  1027. cq->element[cq->rear] = elem;
  1028. cq->rear = (cq->rear + 1) & CQ_MASK;
  1029. return 0;
  1030. }
  1031. static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
  1032. {
  1033. if (__cq_empty(cq))
  1034. return -1;
  1035. *elem = cq->element[cq->front];
  1036. cq->front = (cq->front + 1) & CQ_MASK;
  1037. return 0;
  1038. }
  1039. static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
  1040. {
  1041. return (cq->rear - cq->front) & CQ_MASK;
  1042. }
  1043. static inline void mark_lock_accessed(struct lock_list *lock,
  1044. struct lock_list *parent)
  1045. {
  1046. unsigned long nr;
  1047. nr = lock - list_entries;
  1048. WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
  1049. lock->parent = parent;
  1050. lock->class->dep_gen_id = lockdep_dependency_gen_id;
  1051. }
  1052. static inline unsigned long lock_accessed(struct lock_list *lock)
  1053. {
  1054. unsigned long nr;
  1055. nr = lock - list_entries;
  1056. WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
  1057. return lock->class->dep_gen_id == lockdep_dependency_gen_id;
  1058. }
  1059. static inline struct lock_list *get_lock_parent(struct lock_list *child)
  1060. {
  1061. return child->parent;
  1062. }
  1063. static inline int get_lock_depth(struct lock_list *child)
  1064. {
  1065. int depth = 0;
  1066. struct lock_list *parent;
  1067. while ((parent = get_lock_parent(child))) {
  1068. child = parent;
  1069. depth++;
  1070. }
  1071. return depth;
  1072. }
  1073. static int __bfs(struct lock_list *source_entry,
  1074. void *data,
  1075. int (*match)(struct lock_list *entry, void *data),
  1076. struct lock_list **target_entry,
  1077. int forward)
  1078. {
  1079. struct lock_list *entry;
  1080. struct list_head *head;
  1081. struct circular_queue *cq = &lock_cq;
  1082. int ret = 1;
  1083. if (match(source_entry, data)) {
  1084. *target_entry = source_entry;
  1085. ret = 0;
  1086. goto exit;
  1087. }
  1088. if (forward)
  1089. head = &source_entry->class->locks_after;
  1090. else
  1091. head = &source_entry->class->locks_before;
  1092. if (list_empty(head))
  1093. goto exit;
  1094. __cq_init(cq);
  1095. __cq_enqueue(cq, (unsigned long)source_entry);
  1096. while (!__cq_empty(cq)) {
  1097. struct lock_list *lock;
  1098. __cq_dequeue(cq, (unsigned long *)&lock);
  1099. if (!lock->class) {
  1100. ret = -2;
  1101. goto exit;
  1102. }
  1103. if (forward)
  1104. head = &lock->class->locks_after;
  1105. else
  1106. head = &lock->class->locks_before;
  1107. DEBUG_LOCKS_WARN_ON(!irqs_disabled());
  1108. list_for_each_entry_rcu(entry, head, entry) {
  1109. if (!lock_accessed(entry)) {
  1110. unsigned int cq_depth;
  1111. mark_lock_accessed(entry, lock);
  1112. if (match(entry, data)) {
  1113. *target_entry = entry;
  1114. ret = 0;
  1115. goto exit;
  1116. }
  1117. if (__cq_enqueue(cq, (unsigned long)entry)) {
  1118. ret = -1;
  1119. goto exit;
  1120. }
  1121. cq_depth = __cq_get_elem_count(cq);
  1122. if (max_bfs_queue_depth < cq_depth)
  1123. max_bfs_queue_depth = cq_depth;
  1124. }
  1125. }
  1126. }
  1127. exit:
  1128. return ret;
  1129. }
  1130. static inline int __bfs_forwards(struct lock_list *src_entry,
  1131. void *data,
  1132. int (*match)(struct lock_list *entry, void *data),
  1133. struct lock_list **target_entry)
  1134. {
  1135. return __bfs(src_entry, data, match, target_entry, 1);
  1136. }
  1137. static inline int __bfs_backwards(struct lock_list *src_entry,
  1138. void *data,
  1139. int (*match)(struct lock_list *entry, void *data),
  1140. struct lock_list **target_entry)
  1141. {
  1142. return __bfs(src_entry, data, match, target_entry, 0);
  1143. }
  1144. /*
  1145. * Recursive, forwards-direction lock-dependency checking, used for
  1146. * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
  1147. * checking.
  1148. */
  1149. /*
  1150. * Print a dependency chain entry (this is only done when a deadlock
  1151. * has been detected):
  1152. */
  1153. static noinline int
  1154. print_circular_bug_entry(struct lock_list *target, int depth)
  1155. {
  1156. if (debug_locks_silent)
  1157. return 0;
  1158. printk("\n-> #%u", depth);
  1159. print_lock_name(target->class);
  1160. printk(KERN_CONT ":\n");
  1161. print_stack_trace(&target->trace, 6);
  1162. return 0;
  1163. }
  1164. static void
  1165. print_circular_lock_scenario(struct held_lock *src,
  1166. struct held_lock *tgt,
  1167. struct lock_list *prt)
  1168. {
  1169. struct lock_class *source = hlock_class(src);
  1170. struct lock_class *target = hlock_class(tgt);
  1171. struct lock_class *parent = prt->class;
  1172. /*
  1173. * A direct locking problem where unsafe_class lock is taken
  1174. * directly by safe_class lock, then all we need to show
  1175. * is the deadlock scenario, as it is obvious that the
  1176. * unsafe lock is taken under the safe lock.
  1177. *
  1178. * But if there is a chain instead, where the safe lock takes
  1179. * an intermediate lock (middle_class) where this lock is
  1180. * not the same as the safe lock, then the lock chain is
  1181. * used to describe the problem. Otherwise we would need
  1182. * to show a different CPU case for each link in the chain
  1183. * from the safe_class lock to the unsafe_class lock.
  1184. */
  1185. if (parent != source) {
  1186. printk("Chain exists of:\n ");
  1187. __print_lock_name(source);
  1188. printk(KERN_CONT " --> ");
  1189. __print_lock_name(parent);
  1190. printk(KERN_CONT " --> ");
  1191. __print_lock_name(target);
  1192. printk(KERN_CONT "\n\n");
  1193. }
  1194. if (cross_lock(tgt->instance)) {
  1195. printk(" Possible unsafe locking scenario by crosslock:\n\n");
  1196. printk(" CPU0 CPU1\n");
  1197. printk(" ---- ----\n");
  1198. printk(" lock(");
  1199. __print_lock_name(parent);
  1200. printk(KERN_CONT ");\n");
  1201. printk(" lock(");
  1202. __print_lock_name(target);
  1203. printk(KERN_CONT ");\n");
  1204. printk(" lock(");
  1205. __print_lock_name(source);
  1206. printk(KERN_CONT ");\n");
  1207. printk(" unlock(");
  1208. __print_lock_name(target);
  1209. printk(KERN_CONT ");\n");
  1210. printk("\n *** DEADLOCK ***\n\n");
  1211. } else {
  1212. printk(" Possible unsafe locking scenario:\n\n");
  1213. printk(" CPU0 CPU1\n");
  1214. printk(" ---- ----\n");
  1215. printk(" lock(");
  1216. __print_lock_name(target);
  1217. printk(KERN_CONT ");\n");
  1218. printk(" lock(");
  1219. __print_lock_name(parent);
  1220. printk(KERN_CONT ");\n");
  1221. printk(" lock(");
  1222. __print_lock_name(target);
  1223. printk(KERN_CONT ");\n");
  1224. printk(" lock(");
  1225. __print_lock_name(source);
  1226. printk(KERN_CONT ");\n");
  1227. printk("\n *** DEADLOCK ***\n\n");
  1228. }
  1229. }
  1230. /*
  1231. * When a circular dependency is detected, print the
  1232. * header first:
  1233. */
  1234. static noinline int
  1235. print_circular_bug_header(struct lock_list *entry, unsigned int depth,
  1236. struct held_lock *check_src,
  1237. struct held_lock *check_tgt)
  1238. {
  1239. struct task_struct *curr = current;
  1240. if (debug_locks_silent)
  1241. return 0;
  1242. pr_warn("\n");
  1243. pr_warn("======================================================\n");
  1244. pr_warn("WARNING: possible circular locking dependency detected\n");
  1245. print_kernel_ident();
  1246. pr_warn("------------------------------------------------------\n");
  1247. pr_warn("%s/%d is trying to acquire lock:\n",
  1248. curr->comm, task_pid_nr(curr));
  1249. print_lock(check_src);
  1250. if (cross_lock(check_tgt->instance))
  1251. pr_warn("\nbut now in release context of a crosslock acquired at the following:\n");
  1252. else
  1253. pr_warn("\nbut task is already holding lock:\n");
  1254. print_lock(check_tgt);
  1255. pr_warn("\nwhich lock already depends on the new lock.\n\n");
  1256. pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
  1257. print_circular_bug_entry(entry, depth);
  1258. lockdep_aee();
  1259. return 0;
  1260. }
  1261. static inline int class_equal(struct lock_list *entry, void *data)
  1262. {
  1263. return entry->class == data;
  1264. }
  1265. static noinline int print_circular_bug(struct lock_list *this,
  1266. struct lock_list *target,
  1267. struct held_lock *check_src,
  1268. struct held_lock *check_tgt,
  1269. struct stack_trace *trace)
  1270. {
  1271. struct task_struct *curr = current;
  1272. struct lock_list *parent;
  1273. struct lock_list *first_parent;
  1274. int depth;
  1275. if (is_log_lock_held(current)) {
  1276. tracing_on();
  1277. trace_circular_bug(this, target, check_src, check_tgt, trace);
  1278. return 0;
  1279. }
  1280. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1281. return 0;
  1282. if (cross_lock(check_tgt->instance))
  1283. this->trace = *trace;
  1284. else if (!save_trace(&this->trace))
  1285. return 0;
  1286. depth = get_lock_depth(target);
  1287. print_circular_bug_header(target, depth, check_src, check_tgt);
  1288. parent = get_lock_parent(target);
  1289. first_parent = parent;
  1290. while (parent) {
  1291. print_circular_bug_entry(parent, --depth);
  1292. parent = get_lock_parent(parent);
  1293. }
  1294. printk("\nother info that might help us debug this:\n\n");
  1295. print_circular_lock_scenario(check_src, check_tgt,
  1296. first_parent);
  1297. lockdep_print_held_locks(curr);
  1298. printk("\nstack backtrace:\n");
  1299. dump_stack();
  1300. return 0;
  1301. }
  1302. static noinline int print_bfs_bug(int ret)
  1303. {
  1304. if (!debug_locks_off_graph_unlock())
  1305. return 0;
  1306. /*
  1307. * Breadth-first-search failed, graph got corrupted?
  1308. */
  1309. WARN(1, "lockdep bfs error:%d\n", ret);
  1310. return 0;
  1311. }
  1312. static int noop_count(struct lock_list *entry, void *data)
  1313. {
  1314. (*(unsigned long *)data)++;
  1315. return 0;
  1316. }
  1317. static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
  1318. {
  1319. unsigned long count = 0;
  1320. struct lock_list *uninitialized_var(target_entry);
  1321. __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
  1322. return count;
  1323. }
  1324. unsigned long lockdep_count_forward_deps(struct lock_class *class)
  1325. {
  1326. unsigned long ret, flags;
  1327. struct lock_list this;
  1328. this.parent = NULL;
  1329. this.class = class;
  1330. raw_local_irq_save(flags);
  1331. current->lockdep_recursion = 1;
  1332. arch_spin_lock(&lockdep_lock);
  1333. ret = __lockdep_count_forward_deps(&this);
  1334. arch_spin_unlock(&lockdep_lock);
  1335. current->lockdep_recursion = 0;
  1336. raw_local_irq_restore(flags);
  1337. return ret;
  1338. }
  1339. static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
  1340. {
  1341. unsigned long count = 0;
  1342. struct lock_list *uninitialized_var(target_entry);
  1343. __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
  1344. return count;
  1345. }
  1346. unsigned long lockdep_count_backward_deps(struct lock_class *class)
  1347. {
  1348. unsigned long ret, flags;
  1349. struct lock_list this;
  1350. this.parent = NULL;
  1351. this.class = class;
  1352. raw_local_irq_save(flags);
  1353. current->lockdep_recursion = 1;
  1354. arch_spin_lock(&lockdep_lock);
  1355. ret = __lockdep_count_backward_deps(&this);
  1356. arch_spin_unlock(&lockdep_lock);
  1357. current->lockdep_recursion = 0;
  1358. raw_local_irq_restore(flags);
  1359. return ret;
  1360. }
  1361. /*
  1362. * Prove that the dependency graph starting at <entry> can not
  1363. * lead to <target>. Print an error and return 0 if it does.
  1364. */
  1365. static noinline int
  1366. check_noncircular(struct lock_list *root, struct lock_class *target,
  1367. struct lock_list **target_entry)
  1368. {
  1369. int result;
  1370. debug_atomic_inc(nr_cyclic_checks);
  1371. result = __bfs_forwards(root, target, class_equal, target_entry);
  1372. return result;
  1373. }
  1374. static noinline int
  1375. check_redundant(struct lock_list *root, struct lock_class *target,
  1376. struct lock_list **target_entry)
  1377. {
  1378. int result;
  1379. debug_atomic_inc(nr_redundant_checks);
  1380. result = __bfs_forwards(root, target, class_equal, target_entry);
  1381. return result;
  1382. }
  1383. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
  1384. /*
  1385. * Forwards and backwards subgraph searching, for the purposes of
  1386. * proving that two subgraphs can be connected by a new dependency
  1387. * without creating any illegal irq-safe -> irq-unsafe lock dependency.
  1388. */
  1389. static inline int usage_match(struct lock_list *entry, void *bit)
  1390. {
  1391. return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
  1392. }
  1393. /*
  1394. * Find a node in the forwards-direction dependency sub-graph starting
  1395. * at @root->class that matches @bit.
  1396. *
  1397. * Return 0 if such a node exists in the subgraph, and put that node
  1398. * into *@target_entry.
  1399. *
  1400. * Return 1 otherwise and keep *@target_entry unchanged.
  1401. * Return <0 on error.
  1402. */
  1403. static int
  1404. find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
  1405. struct lock_list **target_entry)
  1406. {
  1407. int result;
  1408. debug_atomic_inc(nr_find_usage_forwards_checks);
  1409. result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
  1410. return result;
  1411. }
  1412. /*
  1413. * Find a node in the backwards-direction dependency sub-graph starting
  1414. * at @root->class that matches @bit.
  1415. *
  1416. * Return 0 if such a node exists in the subgraph, and put that node
  1417. * into *@target_entry.
  1418. *
  1419. * Return 1 otherwise and keep *@target_entry unchanged.
  1420. * Return <0 on error.
  1421. */
  1422. static int
  1423. find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
  1424. struct lock_list **target_entry)
  1425. {
  1426. int result;
  1427. debug_atomic_inc(nr_find_usage_backwards_checks);
  1428. result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
  1429. return result;
  1430. }
  1431. static void print_lock_class_header(struct lock_class *class, int depth)
  1432. {
  1433. int bit;
  1434. printk("%*s->", depth, "");
  1435. print_lock_name(class);
  1436. printk(KERN_CONT " ops: %lu", class->ops);
  1437. printk(KERN_CONT " {\n");
  1438. for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
  1439. if (class->usage_mask & (1 << bit)) {
  1440. int len = depth;
  1441. len += printk("%*s %s", depth, "", usage_str[bit]);
  1442. len += printk(KERN_CONT " at:\n");
  1443. print_stack_trace(class->usage_traces + bit, len);
  1444. }
  1445. }
  1446. printk("%*s }\n", depth, "");
  1447. printk("%*s ... key at: [<%p>] %pS\n",
  1448. depth, "", class->key, class->key);
  1449. }
  1450. /*
  1451. * printk the shortest lock dependencies from @start to @end in reverse order:
  1452. */
  1453. static void __used
  1454. print_shortest_lock_dependencies(struct lock_list *leaf,
  1455. struct lock_list *root)
  1456. {
  1457. struct lock_list *entry = leaf;
  1458. int depth;
  1459. /*compute depth from generated tree by BFS*/
  1460. depth = get_lock_depth(leaf);
  1461. do {
  1462. print_lock_class_header(entry->class, depth);
  1463. printk("%*s ... acquired at:\n", depth, "");
  1464. print_stack_trace(&entry->trace, 2);
  1465. printk("\n");
  1466. if (depth == 0 && (entry != root)) {
  1467. printk("lockdep:%s bad path found in chain graph\n", __func__);
  1468. break;
  1469. }
  1470. entry = get_lock_parent(entry);
  1471. depth--;
  1472. } while (entry && (depth >= 0));
  1473. return;
  1474. }
  1475. static void
  1476. print_irq_lock_scenario(struct lock_list *safe_entry,
  1477. struct lock_list *unsafe_entry,
  1478. struct lock_class *prev_class,
  1479. struct lock_class *next_class)
  1480. {
  1481. struct lock_class *safe_class = safe_entry->class;
  1482. struct lock_class *unsafe_class = unsafe_entry->class;
  1483. struct lock_class *middle_class = prev_class;
  1484. if (middle_class == safe_class)
  1485. middle_class = next_class;
  1486. /*
  1487. * A direct locking problem where unsafe_class lock is taken
  1488. * directly by safe_class lock, then all we need to show
  1489. * is the deadlock scenario, as it is obvious that the
  1490. * unsafe lock is taken under the safe lock.
  1491. *
  1492. * But if there is a chain instead, where the safe lock takes
  1493. * an intermediate lock (middle_class) where this lock is
  1494. * not the same as the safe lock, then the lock chain is
  1495. * used to describe the problem. Otherwise we would need
  1496. * to show a different CPU case for each link in the chain
  1497. * from the safe_class lock to the unsafe_class lock.
  1498. */
  1499. if (middle_class != unsafe_class) {
  1500. printk("Chain exists of:\n ");
  1501. __print_lock_name(safe_class);
  1502. printk(KERN_CONT " --> ");
  1503. __print_lock_name(middle_class);
  1504. printk(KERN_CONT " --> ");
  1505. __print_lock_name(unsafe_class);
  1506. printk(KERN_CONT "\n\n");
  1507. }
  1508. printk(" Possible interrupt unsafe locking scenario:\n\n");
  1509. printk(" CPU0 CPU1\n");
  1510. printk(" ---- ----\n");
  1511. printk(" lock(");
  1512. __print_lock_name(unsafe_class);
  1513. printk(KERN_CONT ");\n");
  1514. printk(" local_irq_disable();\n");
  1515. printk(" lock(");
  1516. __print_lock_name(safe_class);
  1517. printk(KERN_CONT ");\n");
  1518. printk(" lock(");
  1519. __print_lock_name(middle_class);
  1520. printk(KERN_CONT ");\n");
  1521. printk(" <Interrupt>\n");
  1522. printk(" lock(");
  1523. __print_lock_name(safe_class);
  1524. printk(KERN_CONT ");\n");
  1525. printk("\n *** DEADLOCK ***\n\n");
  1526. }
  1527. static int
  1528. print_bad_irq_dependency(struct task_struct *curr,
  1529. struct lock_list *prev_root,
  1530. struct lock_list *next_root,
  1531. struct lock_list *backwards_entry,
  1532. struct lock_list *forwards_entry,
  1533. struct held_lock *prev,
  1534. struct held_lock *next,
  1535. enum lock_usage_bit bit1,
  1536. enum lock_usage_bit bit2,
  1537. const char *irqclass)
  1538. {
  1539. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1540. return 0;
  1541. pr_warn("\n");
  1542. pr_warn("=====================================================\n");
  1543. pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
  1544. irqclass, irqclass);
  1545. print_kernel_ident();
  1546. pr_warn("-----------------------------------------------------\n");
  1547. pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
  1548. curr->comm, task_pid_nr(curr),
  1549. curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
  1550. curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
  1551. curr->hardirqs_enabled,
  1552. curr->softirqs_enabled);
  1553. print_lock(next);
  1554. pr_warn("\nand this task is already holding:\n");
  1555. print_lock(prev);
  1556. pr_warn("which would create a new lock dependency:\n");
  1557. print_lock_name(hlock_class(prev));
  1558. pr_cont(" ->");
  1559. print_lock_name(hlock_class(next));
  1560. pr_cont("\n");
  1561. pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
  1562. irqclass);
  1563. print_lock_name(backwards_entry->class);
  1564. pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
  1565. print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
  1566. pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
  1567. print_lock_name(forwards_entry->class);
  1568. pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
  1569. pr_warn("...");
  1570. print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
  1571. pr_warn("\nother info that might help us debug this:\n\n");
  1572. print_irq_lock_scenario(backwards_entry, forwards_entry,
  1573. hlock_class(prev), hlock_class(next));
  1574. lockdep_print_held_locks(curr);
  1575. pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
  1576. if (!save_trace(&prev_root->trace))
  1577. return 0;
  1578. print_shortest_lock_dependencies(backwards_entry, prev_root);
  1579. pr_warn("\nthe dependencies between the lock to be acquired");
  1580. pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
  1581. if (!save_trace(&next_root->trace))
  1582. return 0;
  1583. print_shortest_lock_dependencies(forwards_entry, next_root);
  1584. pr_warn("\nstack backtrace:\n");
  1585. dump_stack();
  1586. lockdep_aee();
  1587. return 0;
  1588. }
  1589. static int
  1590. check_usage(struct task_struct *curr, struct held_lock *prev,
  1591. struct held_lock *next, enum lock_usage_bit bit_backwards,
  1592. enum lock_usage_bit bit_forwards, const char *irqclass)
  1593. {
  1594. int ret;
  1595. struct lock_list this, that;
  1596. struct lock_list *uninitialized_var(target_entry);
  1597. struct lock_list *uninitialized_var(target_entry1);
  1598. this.parent = NULL;
  1599. this.class = hlock_class(prev);
  1600. ret = find_usage_backwards(&this, bit_backwards, &target_entry);
  1601. if (ret < 0)
  1602. return print_bfs_bug(ret);
  1603. if (ret == 1)
  1604. return ret;
  1605. that.parent = NULL;
  1606. that.class = hlock_class(next);
  1607. ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
  1608. if (ret < 0)
  1609. return print_bfs_bug(ret);
  1610. if (ret == 1)
  1611. return ret;
  1612. return print_bad_irq_dependency(curr, &this, &that,
  1613. target_entry, target_entry1,
  1614. prev, next,
  1615. bit_backwards, bit_forwards, irqclass);
  1616. }
  1617. static const char *state_names[] = {
  1618. #define LOCKDEP_STATE(__STATE) \
  1619. __stringify(__STATE),
  1620. #include "lockdep_states.h"
  1621. #undef LOCKDEP_STATE
  1622. };
  1623. static const char *state_rnames[] = {
  1624. #define LOCKDEP_STATE(__STATE) \
  1625. __stringify(__STATE)"-READ",
  1626. #include "lockdep_states.h"
  1627. #undef LOCKDEP_STATE
  1628. };
  1629. static inline const char *state_name(enum lock_usage_bit bit)
  1630. {
  1631. return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
  1632. }
  1633. static int exclusive_bit(int new_bit)
  1634. {
  1635. /*
  1636. * USED_IN
  1637. * USED_IN_READ
  1638. * ENABLED
  1639. * ENABLED_READ
  1640. *
  1641. * bit 0 - write/read
  1642. * bit 1 - used_in/enabled
  1643. * bit 2+ state
  1644. */
  1645. int state = new_bit & ~3;
  1646. int dir = new_bit & 2;
  1647. /*
  1648. * keep state, bit flip the direction and strip read.
  1649. */
  1650. return state | (dir ^ 2);
  1651. }
  1652. static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
  1653. struct held_lock *next, enum lock_usage_bit bit)
  1654. {
  1655. /*
  1656. * Prove that the new dependency does not connect a hardirq-safe
  1657. * lock with a hardirq-unsafe lock - to achieve this we search
  1658. * the backwards-subgraph starting at <prev>, and the
  1659. * forwards-subgraph starting at <next>:
  1660. */
  1661. if (!check_usage(curr, prev, next, bit,
  1662. exclusive_bit(bit), state_name(bit)))
  1663. return 0;
  1664. bit++; /* _READ */
  1665. /*
  1666. * Prove that the new dependency does not connect a hardirq-safe-read
  1667. * lock with a hardirq-unsafe lock - to achieve this we search
  1668. * the backwards-subgraph starting at <prev>, and the
  1669. * forwards-subgraph starting at <next>:
  1670. */
  1671. if (!check_usage(curr, prev, next, bit,
  1672. exclusive_bit(bit), state_name(bit)))
  1673. return 0;
  1674. return 1;
  1675. }
  1676. static int
  1677. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1678. struct held_lock *next)
  1679. {
  1680. #define LOCKDEP_STATE(__STATE) \
  1681. if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
  1682. return 0;
  1683. #include "lockdep_states.h"
  1684. #undef LOCKDEP_STATE
  1685. return 1;
  1686. }
  1687. static void inc_chains(void)
  1688. {
  1689. if (current->hardirq_context)
  1690. nr_hardirq_chains++;
  1691. else {
  1692. if (current->softirq_context)
  1693. nr_softirq_chains++;
  1694. else
  1695. nr_process_chains++;
  1696. }
  1697. }
  1698. #else
  1699. static inline int
  1700. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1701. struct held_lock *next)
  1702. {
  1703. return 1;
  1704. }
  1705. static inline void inc_chains(void)
  1706. {
  1707. nr_process_chains++;
  1708. }
  1709. #endif
  1710. static void
  1711. print_deadlock_scenario(struct held_lock *nxt,
  1712. struct held_lock *prv)
  1713. {
  1714. struct lock_class *next = hlock_class(nxt);
  1715. struct lock_class *prev = hlock_class(prv);
  1716. printk(" Possible unsafe locking scenario:\n\n");
  1717. printk(" CPU0\n");
  1718. printk(" ----\n");
  1719. printk(" lock(");
  1720. __print_lock_name(prev);
  1721. printk(KERN_CONT ");\n");
  1722. printk(" lock(");
  1723. __print_lock_name(next);
  1724. printk(KERN_CONT ");\n");
  1725. printk("\n *** DEADLOCK ***\n\n");
  1726. printk(" May be due to missing lock nesting notation\n\n");
  1727. }
  1728. static int
  1729. print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
  1730. struct held_lock *next)
  1731. {
  1732. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1733. return 0;
  1734. pr_warn("\n");
  1735. pr_warn("============================================\n");
  1736. pr_warn("WARNING: possible recursive locking detected\n");
  1737. print_kernel_ident();
  1738. pr_warn("--------------------------------------------\n");
  1739. pr_warn("%s/%d is trying to acquire lock:\n",
  1740. curr->comm, task_pid_nr(curr));
  1741. print_lock(next);
  1742. pr_warn("\nbut task is already holding lock:\n");
  1743. print_lock(prev);
  1744. pr_warn("\nother info that might help us debug this:\n");
  1745. print_deadlock_scenario(next, prev);
  1746. lockdep_print_held_locks(curr);
  1747. pr_warn("\nstack backtrace:\n");
  1748. dump_stack();
  1749. lockdep_aee();
  1750. return 0;
  1751. }
  1752. /*
  1753. * Check whether we are holding such a class already.
  1754. *
  1755. * (Note that this has to be done separately, because the graph cannot
  1756. * detect such classes of deadlocks.)
  1757. *
  1758. * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
  1759. */
  1760. static int
  1761. check_deadlock(struct task_struct *curr, struct held_lock *next,
  1762. struct lockdep_map *next_instance, int read)
  1763. {
  1764. struct held_lock *prev;
  1765. struct held_lock *nest = NULL;
  1766. int i;
  1767. for (i = 0; i < curr->lockdep_depth; i++) {
  1768. prev = curr->held_locks + i;
  1769. if (prev->instance == next->nest_lock)
  1770. nest = prev;
  1771. if (hlock_class(prev) != hlock_class(next))
  1772. continue;
  1773. /*
  1774. * Allow read-after-read recursion of the same
  1775. * lock class (i.e. read_lock(lock)+read_lock(lock)):
  1776. */
  1777. if ((read == 2) && prev->read)
  1778. return 2;
  1779. /*
  1780. * We're holding the nest_lock, which serializes this lock's
  1781. * nesting behaviour.
  1782. */
  1783. if (nest)
  1784. return 2;
  1785. if (cross_lock(prev->instance))
  1786. continue;
  1787. return print_deadlock_bug(curr, prev, next);
  1788. }
  1789. return 1;
  1790. }
  1791. /*
  1792. * There was a chain-cache miss, and we are about to add a new dependency
  1793. * to a previous lock. We recursively validate the following rules:
  1794. *
  1795. * - would the adding of the <prev> -> <next> dependency create a
  1796. * circular dependency in the graph? [== circular deadlock]
  1797. *
  1798. * - does the new prev->next dependency connect any hardirq-safe lock
  1799. * (in the full backwards-subgraph starting at <prev>) with any
  1800. * hardirq-unsafe lock (in the full forwards-subgraph starting at
  1801. * <next>)? [== illegal lock inversion with hardirq contexts]
  1802. *
  1803. * - does the new prev->next dependency connect any softirq-safe lock
  1804. * (in the full backwards-subgraph starting at <prev>) with any
  1805. * softirq-unsafe lock (in the full forwards-subgraph starting at
  1806. * <next>)? [== illegal lock inversion with softirq contexts]
  1807. *
  1808. * any of these scenarios could lead to a deadlock.
  1809. *
  1810. * Then if all the validations pass, we add the forwards and backwards
  1811. * dependency.
  1812. */
  1813. static int
  1814. check_prev_add(struct task_struct *curr, struct held_lock *prev,
  1815. struct held_lock *next, int distance, struct stack_trace *trace,
  1816. int (*save)(struct stack_trace *trace))
  1817. {
  1818. struct lock_list *uninitialized_var(target_entry);
  1819. struct lock_list *entry;
  1820. struct lock_list this;
  1821. int ret;
  1822. /*
  1823. * Prove that the new <prev> -> <next> dependency would not
  1824. * create a circular dependency in the graph. (We do this by
  1825. * forward-recursing into the graph starting at <next>, and
  1826. * checking whether we can reach <prev>.)
  1827. *
  1828. * We are using global variables to control the recursion, to
  1829. * keep the stackframe size of the recursive functions low:
  1830. */
  1831. this.class = hlock_class(next);
  1832. this.parent = NULL;
  1833. ret = check_noncircular(&this, hlock_class(prev), &target_entry);
  1834. if (unlikely(!ret)) {
  1835. if (!trace->entries) {
  1836. /*
  1837. * If @save fails here, the printing might trigger
  1838. * a WARN but because of the !nr_entries it should
  1839. * not do bad things.
  1840. */
  1841. save(trace);
  1842. }
  1843. return print_circular_bug(&this, target_entry, next, prev, trace);
  1844. }
  1845. else if (unlikely(ret < 0))
  1846. return print_bfs_bug(ret);
  1847. if (!check_prev_add_irq(curr, prev, next))
  1848. return 0;
  1849. /*
  1850. * For recursive read-locks we do all the dependency checks,
  1851. * but we dont store read-triggered dependencies (only
  1852. * write-triggered dependencies). This ensures that only the
  1853. * write-side dependencies matter, and that if for example a
  1854. * write-lock never takes any other locks, then the reads are
  1855. * equivalent to a NOP.
  1856. */
  1857. if (next->read == 2 || prev->read == 2)
  1858. return 1;
  1859. /*
  1860. * Is the <prev> -> <next> dependency already present?
  1861. *
  1862. * (this may occur even though this is a new chain: consider
  1863. * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
  1864. * chains - the second one will be new, but L1 already has
  1865. * L2 added to its dependency list, due to the first chain.)
  1866. */
  1867. list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
  1868. if (entry->class == hlock_class(next)) {
  1869. if (distance == 1)
  1870. entry->distance = 1;
  1871. return 1;
  1872. }
  1873. }
  1874. /*
  1875. * Is the <prev> -> <next> link redundant?
  1876. */
  1877. this.class = hlock_class(prev);
  1878. this.parent = NULL;
  1879. ret = check_redundant(&this, hlock_class(next), &target_entry);
  1880. if (!ret) {
  1881. debug_atomic_inc(nr_redundant);
  1882. return 2;
  1883. }
  1884. if (ret < 0)
  1885. return print_bfs_bug(ret);
  1886. if (!trace->entries && !save(trace))
  1887. return 0;
  1888. /*
  1889. * Ok, all validations passed, add the new lock
  1890. * to the previous lock's dependency list:
  1891. */
  1892. ret = add_lock_to_list(hlock_class(next),
  1893. &hlock_class(prev)->locks_after,
  1894. next->acquire_ip, distance, trace);
  1895. if (!ret)
  1896. return 0;
  1897. ret = add_lock_to_list(hlock_class(prev),
  1898. &hlock_class(next)->locks_before,
  1899. next->acquire_ip, distance, trace);
  1900. if (!ret)
  1901. return 0;
  1902. #ifdef MTK_LOCK_DEBUG_NEW_DEPENDENCY
  1903. if (check_lock_name(hlock_class(prev), PREV_LOCK_NAME) &&
  1904. check_lock_name(hlock_class(next), NEXT_LOCK_NAME)) {
  1905. if (current->lockdep_depth != LOCK_DEPTH && LOCK_DEPTH != 0)
  1906. return 2;
  1907. /* We drop graph lock, so another thread can overwrite trace. */
  1908. graph_unlock();
  1909. pr_cont("\n new dependency:");
  1910. print_lock_name(hlock_class(prev));
  1911. pr_cont(" =>");
  1912. print_lock_name(hlock_class(next));
  1913. pr_cont("\n");
  1914. dump_stack();
  1915. lockdep_print_held_locks(current);
  1916. #if defined(CONFIG_MTK_AEE_FEATURE) && \
  1917. defined(MTK_LOCK_DEBUG_NEW_DEPENDENCY_AEE)
  1918. aee_kernel_warning_api(__FILE__, __LINE__,
  1919. DB_OPT_DUMMY_DUMP, "[Lockdep] new dependency",
  1920. "[%s => %s]\n", PREV_LOCK_NAME, NEXT_LOCK_NAME);
  1921. #endif
  1922. graph_lock();
  1923. }
  1924. #endif
  1925. return 2;
  1926. }
  1927. /*
  1928. * Add the dependency to all directly-previous locks that are 'relevant'.
  1929. * The ones that are relevant are (in increasing distance from curr):
  1930. * all consecutive trylock entries and the final non-trylock entry - or
  1931. * the end of this context's lock-chain - whichever comes first.
  1932. */
  1933. static int
  1934. check_prevs_add(struct task_struct *curr, struct held_lock *next)
  1935. {
  1936. int depth = curr->lockdep_depth;
  1937. struct held_lock *hlock;
  1938. struct stack_trace trace = {
  1939. .nr_entries = 0,
  1940. .max_entries = 0,
  1941. .entries = NULL,
  1942. .skip = 0,
  1943. };
  1944. /*
  1945. * Debugging checks.
  1946. *
  1947. * Depth must not be zero for a non-head lock:
  1948. */
  1949. if (!depth)
  1950. goto out_bug;
  1951. /*
  1952. * At least two relevant locks must exist for this
  1953. * to be a head:
  1954. */
  1955. if (curr->held_locks[depth].irq_context !=
  1956. curr->held_locks[depth-1].irq_context)
  1957. goto out_bug;
  1958. for (;;) {
  1959. int distance = curr->lockdep_depth - depth + 1;
  1960. hlock = curr->held_locks + depth - 1;
  1961. /*
  1962. * Only non-crosslock entries get new dependencies added.
  1963. * Crosslock entries will be added by commit later:
  1964. */
  1965. if (!cross_lock(hlock->instance)) {
  1966. /*
  1967. * Only non-recursive-read entries get new dependencies
  1968. * added:
  1969. */
  1970. if (hlock->read != 2 && hlock->check) {
  1971. int ret = check_prev_add(curr, hlock, next,
  1972. distance, &trace, save_trace);
  1973. if (!ret)
  1974. return 0;
  1975. /*
  1976. * Stop after the first non-trylock entry,
  1977. * as non-trylock entries have added their
  1978. * own direct dependencies already, so this
  1979. * lock is connected to them indirectly:
  1980. */
  1981. if (!hlock->trylock)
  1982. break;
  1983. }
  1984. }
  1985. depth--;
  1986. /*
  1987. * End of lock-stack?
  1988. */
  1989. if (!depth)
  1990. break;
  1991. /*
  1992. * Stop the search if we cross into another context:
  1993. */
  1994. if (curr->held_locks[depth].irq_context !=
  1995. curr->held_locks[depth-1].irq_context)
  1996. break;
  1997. }
  1998. return 1;
  1999. out_bug:
  2000. if (!debug_locks_off_graph_unlock())
  2001. return 0;
  2002. /*
  2003. * Clearly we all shouldn't be here, but since we made it we
  2004. * can reliable say we messed up our state. See the above two
  2005. * gotos for reasons why we could possibly end up here.
  2006. */
  2007. WARN_ON(1);
  2008. return 0;
  2009. }
  2010. unsigned long nr_lock_chains;
  2011. struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
  2012. int nr_chain_hlocks;
  2013. static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
  2014. struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
  2015. {
  2016. return lock_classes + chain_hlocks[chain->base + i];
  2017. }
  2018. /*
  2019. * Returns the index of the first held_lock of the current chain
  2020. */
  2021. static inline int get_first_held_lock(struct task_struct *curr,
  2022. struct held_lock *hlock)
  2023. {
  2024. int i;
  2025. struct held_lock *hlock_curr;
  2026. for (i = curr->lockdep_depth - 1; i >= 0; i--) {
  2027. hlock_curr = curr->held_locks + i;
  2028. if (hlock_curr->irq_context != hlock->irq_context)
  2029. break;
  2030. }
  2031. return ++i;
  2032. }
  2033. #ifdef CONFIG_DEBUG_LOCKDEP
  2034. /*
  2035. * Returns the next chain_key iteration
  2036. */
  2037. static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
  2038. {
  2039. u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
  2040. printk(" class_idx:%d -> chain_key:%016Lx",
  2041. class_idx,
  2042. (unsigned long long)new_chain_key);
  2043. return new_chain_key;
  2044. }
  2045. static void
  2046. print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
  2047. {
  2048. struct held_lock *hlock;
  2049. u64 chain_key = 0;
  2050. int depth = curr->lockdep_depth;
  2051. int i;
  2052. printk("depth: %u\n", depth + 1);
  2053. for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
  2054. hlock = curr->held_locks + i;
  2055. chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
  2056. print_lock(hlock);
  2057. }
  2058. print_chain_key_iteration(hlock_next->class_idx, chain_key);
  2059. print_lock(hlock_next);
  2060. }
  2061. static void print_chain_keys_chain(struct lock_chain *chain)
  2062. {
  2063. int i;
  2064. u64 chain_key = 0;
  2065. int class_id;
  2066. printk("depth: %u\n", chain->depth);
  2067. for (i = 0; i < chain->depth; i++) {
  2068. class_id = chain_hlocks[chain->base + i];
  2069. chain_key = print_chain_key_iteration(class_id + 1, chain_key);
  2070. print_lock_name(lock_classes + class_id);
  2071. printk("\n");
  2072. }
  2073. }
  2074. static void print_collision(struct task_struct *curr,
  2075. struct held_lock *hlock_next,
  2076. struct lock_chain *chain)
  2077. {
  2078. pr_warn("\n");
  2079. pr_warn("============================\n");
  2080. pr_warn("WARNING: chain_key collision\n");
  2081. print_kernel_ident();
  2082. pr_warn("----------------------------\n");
  2083. pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
  2084. pr_warn("Hash chain already cached but the contents don't match!\n");
  2085. pr_warn("Held locks:");
  2086. print_chain_keys_held_locks(curr, hlock_next);
  2087. pr_warn("Locks in cached chain:");
  2088. print_chain_keys_chain(chain);
  2089. pr_warn("\nstack backtrace:\n");
  2090. dump_stack();
  2091. }
  2092. #endif
  2093. /*
  2094. * Checks whether the chain and the current held locks are consistent
  2095. * in depth and also in content. If they are not it most likely means
  2096. * that there was a collision during the calculation of the chain_key.
  2097. * Returns: 0 not passed, 1 passed
  2098. */
  2099. static int check_no_collision(struct task_struct *curr,
  2100. struct held_lock *hlock,
  2101. struct lock_chain *chain)
  2102. {
  2103. #ifdef CONFIG_DEBUG_LOCKDEP
  2104. int i, j, id;
  2105. i = get_first_held_lock(curr, hlock);
  2106. if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
  2107. print_collision(curr, hlock, chain);
  2108. return 0;
  2109. }
  2110. for (j = 0; j < chain->depth - 1; j++, i++) {
  2111. id = curr->held_locks[i].class_idx - 1;
  2112. if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
  2113. print_collision(curr, hlock, chain);
  2114. return 0;
  2115. }
  2116. }
  2117. #endif
  2118. return 1;
  2119. }
  2120. /*
  2121. * This is for building a chain between just two different classes,
  2122. * instead of adding a new hlock upon current, which is done by
  2123. * add_chain_cache().
  2124. *
  2125. * This can be called in any context with two classes, while
  2126. * add_chain_cache() must be done within the lock owener's context
  2127. * since it uses hlock which might be racy in another context.
  2128. */
  2129. static inline int add_chain_cache_classes(unsigned int prev,
  2130. unsigned int next,
  2131. unsigned int irq_context,
  2132. u64 chain_key)
  2133. {
  2134. struct hlist_head *hash_head = chainhashentry(chain_key);
  2135. struct lock_chain *chain;
  2136. /*
  2137. * Allocate a new chain entry from the static array, and add
  2138. * it to the hash:
  2139. */
  2140. /*
  2141. * We might need to take the graph lock, ensure we've got IRQs
  2142. * disabled to make this an IRQ-safe lock.. for recursion reasons
  2143. * lockdep won't complain about its own locking errors.
  2144. */
  2145. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2146. return 0;
  2147. if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
  2148. if (!debug_locks_off_graph_unlock())
  2149. return 0;
  2150. print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
  2151. dump_stack();
  2152. return 0;
  2153. }
  2154. chain = lock_chains + nr_lock_chains++;
  2155. chain->chain_key = chain_key;
  2156. chain->irq_context = irq_context;
  2157. chain->depth = 2;
  2158. if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
  2159. chain->base = nr_chain_hlocks;
  2160. nr_chain_hlocks += chain->depth;
  2161. chain_hlocks[chain->base] = prev - 1;
  2162. chain_hlocks[chain->base + 1] = next -1;
  2163. }
  2164. #ifdef CONFIG_DEBUG_LOCKDEP
  2165. /*
  2166. * Important for check_no_collision().
  2167. */
  2168. else {
  2169. if (!debug_locks_off_graph_unlock())
  2170. return 0;
  2171. print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
  2172. dump_stack();
  2173. return 0;
  2174. }
  2175. #endif
  2176. hlist_add_head_rcu(&chain->entry, hash_head);
  2177. debug_atomic_inc(chain_lookup_misses);
  2178. inc_chains();
  2179. return 1;
  2180. }
  2181. /*
  2182. * Adds a dependency chain into chain hashtable. And must be called with
  2183. * graph_lock held.
  2184. *
  2185. * Return 0 if fail, and graph_lock is released.
  2186. * Return 1 if succeed, with graph_lock held.
  2187. */
  2188. static inline int add_chain_cache(struct task_struct *curr,
  2189. struct held_lock *hlock,
  2190. u64 chain_key)
  2191. {
  2192. struct lock_class *class = hlock_class(hlock);
  2193. struct hlist_head *hash_head = chainhashentry(chain_key);
  2194. struct lock_chain *chain;
  2195. int i, j;
  2196. /*
  2197. * Allocate a new chain entry from the static array, and add
  2198. * it to the hash:
  2199. */
  2200. /*
  2201. * We might need to take the graph lock, ensure we've got IRQs
  2202. * disabled to make this an IRQ-safe lock.. for recursion reasons
  2203. * lockdep won't complain about its own locking errors.
  2204. */
  2205. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2206. return 0;
  2207. if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
  2208. if (!debug_locks_off_graph_unlock())
  2209. return 0;
  2210. print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
  2211. dump_stack();
  2212. return 0;
  2213. }
  2214. chain = lock_chains + nr_lock_chains++;
  2215. chain->chain_key = chain_key;
  2216. chain->irq_context = hlock->irq_context;
  2217. i = get_first_held_lock(curr, hlock);
  2218. chain->depth = curr->lockdep_depth + 1 - i;
  2219. BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
  2220. BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
  2221. BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
  2222. if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
  2223. chain->base = nr_chain_hlocks;
  2224. for (j = 0; j < chain->depth - 1; j++, i++) {
  2225. int lock_id = curr->held_locks[i].class_idx - 1;
  2226. chain_hlocks[chain->base + j] = lock_id;
  2227. }
  2228. chain_hlocks[chain->base + j] = class - lock_classes;
  2229. }
  2230. if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
  2231. nr_chain_hlocks += chain->depth;
  2232. #ifdef CONFIG_DEBUG_LOCKDEP
  2233. /*
  2234. * Important for check_no_collision().
  2235. */
  2236. if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
  2237. if (!debug_locks_off_graph_unlock())
  2238. return 0;
  2239. print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
  2240. dump_stack();
  2241. return 0;
  2242. }
  2243. #endif
  2244. hlist_add_head_rcu(&chain->entry, hash_head);
  2245. debug_atomic_inc(chain_lookup_misses);
  2246. inc_chains();
  2247. return 1;
  2248. }
  2249. /*
  2250. * Look up a dependency chain.
  2251. */
  2252. static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
  2253. {
  2254. struct hlist_head *hash_head = chainhashentry(chain_key);
  2255. struct lock_chain *chain;
  2256. /*
  2257. * We can walk it lock-free, because entries only get added
  2258. * to the hash:
  2259. */
  2260. hlist_for_each_entry_rcu(chain, hash_head, entry) {
  2261. if (chain->chain_key == chain_key) {
  2262. debug_atomic_inc(chain_lookup_hits);
  2263. return chain;
  2264. }
  2265. }
  2266. return NULL;
  2267. }
  2268. /*
  2269. * If the key is not present yet in dependency chain cache then
  2270. * add it and return 1 - in this case the new dependency chain is
  2271. * validated. If the key is already hashed, return 0.
  2272. * (On return with 1 graph_lock is held.)
  2273. */
  2274. static inline int lookup_chain_cache_add(struct task_struct *curr,
  2275. struct held_lock *hlock,
  2276. u64 chain_key)
  2277. {
  2278. struct lock_class *class = hlock_class(hlock);
  2279. struct lock_chain *chain = lookup_chain_cache(chain_key);
  2280. if (chain) {
  2281. cache_hit:
  2282. if (!check_no_collision(curr, hlock, chain))
  2283. return 0;
  2284. if (very_verbose(class)) {
  2285. printk("\nhash chain already cached, key: "
  2286. "%016Lx tail class: [%p] %s\n",
  2287. (unsigned long long)chain_key,
  2288. class->key, class->name);
  2289. }
  2290. return 0;
  2291. }
  2292. if (very_verbose(class)) {
  2293. printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
  2294. (unsigned long long)chain_key, class->key, class->name);
  2295. }
  2296. if (!graph_lock())
  2297. return 0;
  2298. /*
  2299. * We have to walk the chain again locked - to avoid duplicates:
  2300. */
  2301. chain = lookup_chain_cache(chain_key);
  2302. if (chain) {
  2303. graph_unlock();
  2304. goto cache_hit;
  2305. }
  2306. if (!add_chain_cache(curr, hlock, chain_key))
  2307. return 0;
  2308. return 1;
  2309. }
  2310. static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
  2311. struct held_lock *hlock, int chain_head, u64 chain_key)
  2312. {
  2313. /*
  2314. * Trylock needs to maintain the stack of held locks, but it
  2315. * does not add new dependencies, because trylock can be done
  2316. * in any order.
  2317. *
  2318. * We look up the chain_key and do the O(N^2) check and update of
  2319. * the dependencies only if this is a new dependency chain.
  2320. * (If lookup_chain_cache_add() return with 1 it acquires
  2321. * graph_lock for us)
  2322. */
  2323. if (!hlock->trylock && hlock->check &&
  2324. lookup_chain_cache_add(curr, hlock, chain_key)) {
  2325. /*
  2326. * Check whether last held lock:
  2327. *
  2328. * - is irq-safe, if this lock is irq-unsafe
  2329. * - is softirq-safe, if this lock is hardirq-unsafe
  2330. *
  2331. * And check whether the new lock's dependency graph
  2332. * could lead back to the previous lock.
  2333. *
  2334. * any of these scenarios could lead to a deadlock. If
  2335. * All validations
  2336. */
  2337. int ret = check_deadlock(curr, hlock, lock, hlock->read);
  2338. if (!ret)
  2339. return 0;
  2340. /*
  2341. * Mark recursive read, as we jump over it when
  2342. * building dependencies (just like we jump over
  2343. * trylock entries):
  2344. */
  2345. if (ret == 2)
  2346. hlock->read = 2;
  2347. /*
  2348. * Add dependency only if this lock is not the head
  2349. * of the chain, and if it's not a secondary read-lock:
  2350. */
  2351. if (!chain_head && ret != 2) {
  2352. if (!check_prevs_add(curr, hlock))
  2353. return 0;
  2354. }
  2355. graph_unlock();
  2356. } else {
  2357. /* after lookup_chain_cache_add(): */
  2358. if (unlikely(!debug_locks))
  2359. return 0;
  2360. }
  2361. return 1;
  2362. }
  2363. #else
  2364. static inline int validate_chain(struct task_struct *curr,
  2365. struct lockdep_map *lock, struct held_lock *hlock,
  2366. int chain_head, u64 chain_key)
  2367. {
  2368. return 1;
  2369. }
  2370. #endif
  2371. /*
  2372. * We are building curr_chain_key incrementally, so double-check
  2373. * it from scratch, to make sure that it's done correctly:
  2374. */
  2375. static void check_chain_key(struct task_struct *curr)
  2376. {
  2377. #ifdef CONFIG_DEBUG_LOCKDEP
  2378. struct held_lock *hlock, *prev_hlock = NULL;
  2379. unsigned int i;
  2380. u64 chain_key = 0;
  2381. for (i = 0; i < curr->lockdep_depth; i++) {
  2382. hlock = curr->held_locks + i;
  2383. if (chain_key != hlock->prev_chain_key) {
  2384. debug_locks_off();
  2385. /*
  2386. * We got mighty confused, our chain keys don't match
  2387. * with what we expect, someone trample on our task state?
  2388. */
  2389. WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
  2390. curr->lockdep_depth, i,
  2391. (unsigned long long)chain_key,
  2392. (unsigned long long)hlock->prev_chain_key);
  2393. return;
  2394. }
  2395. /*
  2396. * Whoops ran out of static storage again?
  2397. */
  2398. if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
  2399. return;
  2400. if (prev_hlock && (prev_hlock->irq_context !=
  2401. hlock->irq_context))
  2402. chain_key = 0;
  2403. chain_key = iterate_chain_key(chain_key, hlock->class_idx);
  2404. prev_hlock = hlock;
  2405. }
  2406. if (chain_key != curr->curr_chain_key) {
  2407. debug_locks_off();
  2408. /*
  2409. * More smoking hash instead of calculating it, damn see these
  2410. * numbers float.. I bet that a pink elephant stepped on my memory.
  2411. */
  2412. WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
  2413. curr->lockdep_depth, i,
  2414. (unsigned long long)chain_key,
  2415. (unsigned long long)curr->curr_chain_key);
  2416. }
  2417. #endif
  2418. }
  2419. static void
  2420. print_usage_bug_scenario(struct held_lock *lock)
  2421. {
  2422. struct lock_class *class = hlock_class(lock);
  2423. printk(" Possible unsafe locking scenario:\n\n");
  2424. printk(" CPU0\n");
  2425. printk(" ----\n");
  2426. printk(" lock(");
  2427. __print_lock_name(class);
  2428. printk(KERN_CONT ");\n");
  2429. printk(" <Interrupt>\n");
  2430. printk(" lock(");
  2431. __print_lock_name(class);
  2432. printk(KERN_CONT ");\n");
  2433. printk("\n *** DEADLOCK ***\n\n");
  2434. }
  2435. static int
  2436. print_usage_bug(struct task_struct *curr, struct held_lock *this,
  2437. enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
  2438. {
  2439. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  2440. return 0;
  2441. pr_warn("\n");
  2442. pr_warn("================================\n");
  2443. pr_warn("WARNING: inconsistent lock state\n");
  2444. print_kernel_ident();
  2445. pr_warn("--------------------------------\n");
  2446. pr_warn("inconsistent {%s} -> {%s} usage.\n",
  2447. usage_str[prev_bit], usage_str[new_bit]);
  2448. pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
  2449. curr->comm, task_pid_nr(curr),
  2450. trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
  2451. trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
  2452. trace_hardirqs_enabled(curr),
  2453. trace_softirqs_enabled(curr));
  2454. print_lock(this);
  2455. pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
  2456. print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
  2457. print_irqtrace_events(curr);
  2458. pr_warn("\nother info that might help us debug this:\n");
  2459. print_usage_bug_scenario(this);
  2460. lockdep_print_held_locks(curr);
  2461. pr_warn("\nstack backtrace:\n");
  2462. dump_stack();
  2463. lockdep_aee();
  2464. return 0;
  2465. }
  2466. /*
  2467. * Print out an error if an invalid bit is set:
  2468. */
  2469. static inline int
  2470. valid_state(struct task_struct *curr, struct held_lock *this,
  2471. enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
  2472. {
  2473. if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
  2474. return print_usage_bug(curr, this, bad_bit, new_bit);
  2475. return 1;
  2476. }
  2477. static int mark_lock(struct task_struct *curr, struct held_lock *this,
  2478. enum lock_usage_bit new_bit);
  2479. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
  2480. /*
  2481. * print irq inversion bug:
  2482. */
  2483. static int
  2484. print_irq_inversion_bug(struct task_struct *curr,
  2485. struct lock_list *root, struct lock_list *other,
  2486. struct held_lock *this, int forwards,
  2487. const char *irqclass)
  2488. {
  2489. struct lock_list *entry = other;
  2490. struct lock_list *middle = NULL;
  2491. int depth;
  2492. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  2493. return 0;
  2494. pr_warn("\n");
  2495. pr_warn("========================================================\n");
  2496. pr_warn("WARNING: possible irq lock inversion dependency detected\n");
  2497. print_kernel_ident();
  2498. pr_warn("--------------------------------------------------------\n");
  2499. pr_warn("%s/%d just changed the state of lock:\n",
  2500. curr->comm, task_pid_nr(curr));
  2501. print_lock(this);
  2502. if (forwards)
  2503. pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
  2504. else
  2505. pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
  2506. print_lock_name(other->class);
  2507. pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
  2508. pr_warn("\nother info that might help us debug this:\n");
  2509. /* Find a middle lock (if one exists) */
  2510. depth = get_lock_depth(other);
  2511. do {
  2512. if (depth == 0 && (entry != root)) {
  2513. pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
  2514. break;
  2515. }
  2516. middle = entry;
  2517. entry = get_lock_parent(entry);
  2518. depth--;
  2519. } while (entry && entry != root && (depth >= 0));
  2520. if (forwards)
  2521. print_irq_lock_scenario(root, other,
  2522. middle ? middle->class : root->class, other->class);
  2523. else
  2524. print_irq_lock_scenario(other, root,
  2525. middle ? middle->class : other->class, root->class);
  2526. lockdep_print_held_locks(curr);
  2527. pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
  2528. if (!save_trace(&root->trace))
  2529. return 0;
  2530. print_shortest_lock_dependencies(other, root);
  2531. pr_warn("\nstack backtrace:\n");
  2532. dump_stack();
  2533. lockdep_aee();
  2534. return 0;
  2535. }
  2536. /*
  2537. * Prove that in the forwards-direction subgraph starting at <this>
  2538. * there is no lock matching <mask>:
  2539. */
  2540. static int
  2541. check_usage_forwards(struct task_struct *curr, struct held_lock *this,
  2542. enum lock_usage_bit bit, const char *irqclass)
  2543. {
  2544. int ret;
  2545. struct lock_list root;
  2546. struct lock_list *uninitialized_var(target_entry);
  2547. root.parent = NULL;
  2548. root.class = hlock_class(this);
  2549. ret = find_usage_forwards(&root, bit, &target_entry);
  2550. if (ret < 0)
  2551. return print_bfs_bug(ret);
  2552. if (ret == 1)
  2553. return ret;
  2554. return print_irq_inversion_bug(curr, &root, target_entry,
  2555. this, 1, irqclass);
  2556. }
  2557. /*
  2558. * Prove that in the backwards-direction subgraph starting at <this>
  2559. * there is no lock matching <mask>:
  2560. */
  2561. static int
  2562. check_usage_backwards(struct task_struct *curr, struct held_lock *this,
  2563. enum lock_usage_bit bit, const char *irqclass)
  2564. {
  2565. int ret;
  2566. struct lock_list root;
  2567. struct lock_list *uninitialized_var(target_entry);
  2568. root.parent = NULL;
  2569. root.class = hlock_class(this);
  2570. ret = find_usage_backwards(&root, bit, &target_entry);
  2571. if (ret < 0)
  2572. return print_bfs_bug(ret);
  2573. if (ret == 1)
  2574. return ret;
  2575. return print_irq_inversion_bug(curr, &root, target_entry,
  2576. this, 0, irqclass);
  2577. }
  2578. void print_irqtrace_events(struct task_struct *curr)
  2579. {
  2580. printk("irq event stamp: %u\n", curr->irq_events);
  2581. printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
  2582. curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
  2583. (void *)curr->hardirq_enable_ip);
  2584. printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
  2585. curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
  2586. (void *)curr->hardirq_disable_ip);
  2587. printk("softirqs last enabled at (%u): [<%p>] %pS\n",
  2588. curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
  2589. (void *)curr->softirq_enable_ip);
  2590. printk("softirqs last disabled at (%u): [<%p>] %pS\n",
  2591. curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
  2592. (void *)curr->softirq_disable_ip);
  2593. }
  2594. static int HARDIRQ_verbose(struct lock_class *class)
  2595. {
  2596. #if HARDIRQ_VERBOSE
  2597. return class_filter(class);
  2598. #endif
  2599. return 0;
  2600. }
  2601. static int SOFTIRQ_verbose(struct lock_class *class)
  2602. {
  2603. #if SOFTIRQ_VERBOSE
  2604. return class_filter(class);
  2605. #endif
  2606. return 0;
  2607. }
  2608. #define STRICT_READ_CHECKS 1
  2609. static int (*state_verbose_f[])(struct lock_class *class) = {
  2610. #define LOCKDEP_STATE(__STATE) \
  2611. __STATE##_verbose,
  2612. #include "lockdep_states.h"
  2613. #undef LOCKDEP_STATE
  2614. };
  2615. static inline int state_verbose(enum lock_usage_bit bit,
  2616. struct lock_class *class)
  2617. {
  2618. return state_verbose_f[bit >> 2](class);
  2619. }
  2620. typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
  2621. enum lock_usage_bit bit, const char *name);
  2622. static int
  2623. mark_lock_irq(struct task_struct *curr, struct held_lock *this,
  2624. enum lock_usage_bit new_bit)
  2625. {
  2626. int excl_bit = exclusive_bit(new_bit);
  2627. int read = new_bit & 1;
  2628. int dir = new_bit & 2;
  2629. /*
  2630. * mark USED_IN has to look forwards -- to ensure no dependency
  2631. * has ENABLED state, which would allow recursion deadlocks.
  2632. *
  2633. * mark ENABLED has to look backwards -- to ensure no dependee
  2634. * has USED_IN state, which, again, would allow recursion deadlocks.
  2635. */
  2636. check_usage_f usage = dir ?
  2637. check_usage_backwards : check_usage_forwards;
  2638. /*
  2639. * Validate that this particular lock does not have conflicting
  2640. * usage states.
  2641. */
  2642. if (!valid_state(curr, this, new_bit, excl_bit))
  2643. return 0;
  2644. /*
  2645. * Validate that the lock dependencies don't have conflicting usage
  2646. * states.
  2647. */
  2648. if ((!read || !dir || STRICT_READ_CHECKS) &&
  2649. !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
  2650. return 0;
  2651. /*
  2652. * Check for read in write conflicts
  2653. */
  2654. if (!read) {
  2655. if (!valid_state(curr, this, new_bit, excl_bit + 1))
  2656. return 0;
  2657. if (STRICT_READ_CHECKS &&
  2658. !usage(curr, this, excl_bit + 1,
  2659. state_name(new_bit + 1)))
  2660. return 0;
  2661. }
  2662. if (state_verbose(new_bit, hlock_class(this)))
  2663. return 2;
  2664. return 1;
  2665. }
  2666. enum mark_type {
  2667. #define LOCKDEP_STATE(__STATE) __STATE,
  2668. #include "lockdep_states.h"
  2669. #undef LOCKDEP_STATE
  2670. };
  2671. /*
  2672. * Mark all held locks with a usage bit:
  2673. */
  2674. static int
  2675. mark_held_locks(struct task_struct *curr, enum mark_type mark)
  2676. {
  2677. enum lock_usage_bit usage_bit;
  2678. struct held_lock *hlock;
  2679. int i;
  2680. for (i = 0; i < curr->lockdep_depth; i++) {
  2681. hlock = curr->held_locks + i;
  2682. usage_bit = 2 + (mark << 2); /* ENABLED */
  2683. if (hlock->read)
  2684. usage_bit += 1; /* READ */
  2685. BUG_ON(usage_bit >= LOCK_USAGE_STATES);
  2686. if (!hlock->check)
  2687. continue;
  2688. if (!mark_lock(curr, hlock, usage_bit))
  2689. return 0;
  2690. }
  2691. return 1;
  2692. }
  2693. /*
  2694. * Hardirqs will be enabled:
  2695. */
  2696. static void __trace_hardirqs_on_caller(unsigned long ip)
  2697. {
  2698. struct task_struct *curr = current;
  2699. /* we'll do an OFF -> ON transition: */
  2700. curr->hardirqs_enabled = 1;
  2701. /*
  2702. * We are going to turn hardirqs on, so set the
  2703. * usage bit for all held locks:
  2704. */
  2705. if (!mark_held_locks(curr, HARDIRQ))
  2706. return;
  2707. /*
  2708. * If we have softirqs enabled, then set the usage
  2709. * bit for all held locks. (disabled hardirqs prevented
  2710. * this bit from being set before)
  2711. */
  2712. if (curr->softirqs_enabled)
  2713. if (!mark_held_locks(curr, SOFTIRQ))
  2714. return;
  2715. curr->hardirq_enable_ip = ip;
  2716. curr->hardirq_enable_event = ++curr->irq_events;
  2717. debug_atomic_inc(hardirqs_on_events);
  2718. }
  2719. __visible void trace_hardirqs_on_caller(unsigned long ip)
  2720. {
  2721. time_hardirqs_on(CALLER_ADDR0, ip);
  2722. if (unlikely(!debug_locks || current->lockdep_recursion))
  2723. return;
  2724. if (unlikely(current->hardirqs_enabled)) {
  2725. /*
  2726. * Neither irq nor preemption are disabled here
  2727. * so this is racy by nature but losing one hit
  2728. * in a stat is not a big deal.
  2729. */
  2730. __debug_atomic_inc(redundant_hardirqs_on);
  2731. return;
  2732. }
  2733. /*
  2734. * We're enabling irqs and according to our state above irqs weren't
  2735. * already enabled, yet we find the hardware thinks they are in fact
  2736. * enabled.. someone messed up their IRQ state tracing.
  2737. */
  2738. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2739. return;
  2740. /*
  2741. * See the fine text that goes along with this variable definition.
  2742. */
  2743. if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
  2744. return;
  2745. /*
  2746. * Can't allow enabling interrupts while in an interrupt handler,
  2747. * that's general bad form and such. Recursion, limited stack etc..
  2748. */
  2749. if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
  2750. return;
  2751. current->lockdep_recursion = 1;
  2752. __trace_hardirqs_on_caller(ip);
  2753. current->lockdep_recursion = 0;
  2754. }
  2755. EXPORT_SYMBOL(trace_hardirqs_on_caller);
  2756. void trace_hardirqs_on(void)
  2757. {
  2758. trace_hardirqs_on_time();
  2759. trace_hardirqs_on_caller(CALLER_ADDR0);
  2760. }
  2761. EXPORT_SYMBOL(trace_hardirqs_on);
  2762. /*
  2763. * Hardirqs were disabled:
  2764. */
  2765. __visible void trace_hardirqs_off_caller(unsigned long ip)
  2766. {
  2767. struct task_struct *curr = current;
  2768. time_hardirqs_off(CALLER_ADDR0, ip);
  2769. if (unlikely(!debug_locks || current->lockdep_recursion))
  2770. return;
  2771. /*
  2772. * So we're supposed to get called after you mask local IRQs, but for
  2773. * some reason the hardware doesn't quite think you did a proper job.
  2774. */
  2775. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2776. return;
  2777. if (curr->hardirqs_enabled) {
  2778. /*
  2779. * We have done an ON -> OFF transition:
  2780. */
  2781. curr->hardirqs_enabled = 0;
  2782. curr->hardirq_disable_ip = ip;
  2783. curr->hardirq_disable_event = ++curr->irq_events;
  2784. debug_atomic_inc(hardirqs_off_events);
  2785. } else
  2786. debug_atomic_inc(redundant_hardirqs_off);
  2787. }
  2788. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  2789. void trace_hardirqs_off(void)
  2790. {
  2791. trace_hardirqs_off_caller(CALLER_ADDR0);
  2792. trace_hardirqs_off_time();
  2793. }
  2794. EXPORT_SYMBOL(trace_hardirqs_off);
  2795. /*
  2796. * Softirqs will be enabled:
  2797. */
  2798. void trace_softirqs_on(unsigned long ip)
  2799. {
  2800. struct task_struct *curr = current;
  2801. if (unlikely(!debug_locks || current->lockdep_recursion))
  2802. return;
  2803. /*
  2804. * We fancy IRQs being disabled here, see softirq.c, avoids
  2805. * funny state and nesting things.
  2806. */
  2807. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2808. return;
  2809. if (curr->softirqs_enabled) {
  2810. debug_atomic_inc(redundant_softirqs_on);
  2811. return;
  2812. }
  2813. current->lockdep_recursion = 1;
  2814. /*
  2815. * We'll do an OFF -> ON transition:
  2816. */
  2817. curr->softirqs_enabled = 1;
  2818. curr->softirq_enable_ip = ip;
  2819. curr->softirq_enable_event = ++curr->irq_events;
  2820. debug_atomic_inc(softirqs_on_events);
  2821. /*
  2822. * We are going to turn softirqs on, so set the
  2823. * usage bit for all held locks, if hardirqs are
  2824. * enabled too:
  2825. */
  2826. if (curr->hardirqs_enabled)
  2827. mark_held_locks(curr, SOFTIRQ);
  2828. current->lockdep_recursion = 0;
  2829. }
  2830. /*
  2831. * Softirqs were disabled:
  2832. */
  2833. void trace_softirqs_off(unsigned long ip)
  2834. {
  2835. struct task_struct *curr = current;
  2836. if (unlikely(!debug_locks || current->lockdep_recursion))
  2837. return;
  2838. /*
  2839. * We fancy IRQs being disabled here, see softirq.c
  2840. */
  2841. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2842. return;
  2843. if (curr->softirqs_enabled) {
  2844. /*
  2845. * We have done an ON -> OFF transition:
  2846. */
  2847. curr->softirqs_enabled = 0;
  2848. curr->softirq_disable_ip = ip;
  2849. curr->softirq_disable_event = ++curr->irq_events;
  2850. debug_atomic_inc(softirqs_off_events);
  2851. /*
  2852. * Whoops, we wanted softirqs off, so why aren't they?
  2853. */
  2854. DEBUG_LOCKS_WARN_ON(!softirq_count());
  2855. } else
  2856. debug_atomic_inc(redundant_softirqs_off);
  2857. }
  2858. static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
  2859. {
  2860. /*
  2861. * If non-trylock use in a hardirq or softirq context, then
  2862. * mark the lock as used in these contexts:
  2863. */
  2864. if (!hlock->trylock) {
  2865. if (hlock->read) {
  2866. if (curr->hardirq_context)
  2867. if (!mark_lock(curr, hlock,
  2868. LOCK_USED_IN_HARDIRQ_READ))
  2869. return 0;
  2870. if (curr->softirq_context)
  2871. if (!mark_lock(curr, hlock,
  2872. LOCK_USED_IN_SOFTIRQ_READ))
  2873. return 0;
  2874. } else {
  2875. if (curr->hardirq_context)
  2876. if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
  2877. return 0;
  2878. if (curr->softirq_context)
  2879. if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
  2880. return 0;
  2881. }
  2882. }
  2883. if (!hlock->hardirqs_off) {
  2884. if (hlock->read) {
  2885. if (!mark_lock(curr, hlock,
  2886. LOCK_ENABLED_HARDIRQ_READ))
  2887. return 0;
  2888. if (curr->softirqs_enabled)
  2889. if (!mark_lock(curr, hlock,
  2890. LOCK_ENABLED_SOFTIRQ_READ))
  2891. return 0;
  2892. } else {
  2893. if (!mark_lock(curr, hlock,
  2894. LOCK_ENABLED_HARDIRQ))
  2895. return 0;
  2896. if (curr->softirqs_enabled)
  2897. if (!mark_lock(curr, hlock,
  2898. LOCK_ENABLED_SOFTIRQ))
  2899. return 0;
  2900. }
  2901. }
  2902. return 1;
  2903. }
  2904. static inline unsigned int task_irq_context(struct task_struct *task)
  2905. {
  2906. return 2 * !!task->hardirq_context + !!task->softirq_context;
  2907. }
  2908. static int separate_irq_context(struct task_struct *curr,
  2909. struct held_lock *hlock)
  2910. {
  2911. unsigned int depth = curr->lockdep_depth;
  2912. /*
  2913. * Keep track of points where we cross into an interrupt context:
  2914. */
  2915. if (depth) {
  2916. struct held_lock *prev_hlock;
  2917. prev_hlock = curr->held_locks + depth-1;
  2918. /*
  2919. * If we cross into another context, reset the
  2920. * hash key (this also prevents the checking and the
  2921. * adding of the dependency to 'prev'):
  2922. */
  2923. if (prev_hlock->irq_context != hlock->irq_context)
  2924. return 1;
  2925. }
  2926. return 0;
  2927. }
  2928. #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
  2929. static inline
  2930. int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
  2931. enum lock_usage_bit new_bit)
  2932. {
  2933. WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
  2934. return 1;
  2935. }
  2936. static inline int mark_irqflags(struct task_struct *curr,
  2937. struct held_lock *hlock)
  2938. {
  2939. return 1;
  2940. }
  2941. static inline unsigned int task_irq_context(struct task_struct *task)
  2942. {
  2943. return 0;
  2944. }
  2945. static inline int separate_irq_context(struct task_struct *curr,
  2946. struct held_lock *hlock)
  2947. {
  2948. return 0;
  2949. }
  2950. #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
  2951. /*
  2952. * Mark a lock with a usage bit, and validate the state transition:
  2953. */
  2954. static int mark_lock(struct task_struct *curr, struct held_lock *this,
  2955. enum lock_usage_bit new_bit)
  2956. {
  2957. unsigned int new_mask = 1 << new_bit, ret = 1;
  2958. /*
  2959. * If already set then do not dirty the cacheline,
  2960. * nor do any checks:
  2961. */
  2962. if (likely(hlock_class(this)->usage_mask & new_mask))
  2963. return 1;
  2964. if (!graph_lock())
  2965. return 0;
  2966. /*
  2967. * Make sure we didn't race:
  2968. */
  2969. if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
  2970. graph_unlock();
  2971. return 1;
  2972. }
  2973. hlock_class(this)->usage_mask |= new_mask;
  2974. if (!save_trace(hlock_class(this)->usage_traces + new_bit))
  2975. return 0;
  2976. switch (new_bit) {
  2977. #define LOCKDEP_STATE(__STATE) \
  2978. case LOCK_USED_IN_##__STATE: \
  2979. case LOCK_USED_IN_##__STATE##_READ: \
  2980. case LOCK_ENABLED_##__STATE: \
  2981. case LOCK_ENABLED_##__STATE##_READ:
  2982. #include "lockdep_states.h"
  2983. #undef LOCKDEP_STATE
  2984. ret = mark_lock_irq(curr, this, new_bit);
  2985. if (!ret)
  2986. return 0;
  2987. break;
  2988. case LOCK_USED:
  2989. debug_atomic_dec(nr_unused_locks);
  2990. break;
  2991. default:
  2992. if (!debug_locks_off_graph_unlock())
  2993. return 0;
  2994. WARN_ON(1);
  2995. return 0;
  2996. }
  2997. graph_unlock();
  2998. /*
  2999. * We must printk outside of the graph_lock:
  3000. */
  3001. if (ret == 2) {
  3002. printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
  3003. print_lock(this);
  3004. print_irqtrace_events(curr);
  3005. dump_stack();
  3006. }
  3007. return ret;
  3008. }
  3009. static int
  3010. print_lock_invalid_wait_context(struct task_struct *curr,
  3011. struct held_lock *hlock)
  3012. {
  3013. if (!debug_locks_off())
  3014. return 0;
  3015. if (debug_locks_silent)
  3016. return 0;
  3017. pr_warn("\n");
  3018. pr_warn("=============================\n");
  3019. pr_warn("[ BUG: Invalid wait context ]\n");
  3020. print_kernel_ident();
  3021. pr_warn("-----------------------------\n");
  3022. pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
  3023. print_lock(hlock);
  3024. pr_warn("other info that might help us debug this:\n");
  3025. lockdep_print_held_locks(curr);
  3026. pr_warn("stack backtrace:\n");
  3027. dump_stack();
  3028. return 0;
  3029. }
  3030. /*
  3031. * Verify the wait_type context.
  3032. *
  3033. * This check validates we takes locks in the right wait-type order; that is it
  3034. * ensures that we do not take mutexes inside spinlocks and do not attempt to
  3035. * acquire spinlocks inside raw_spinlocks and the sort.
  3036. *
  3037. * The entire thing is slightly more complex because of RCU, RCU is a lock that
  3038. * can be taken from (pretty much) any context but also has constraints.
  3039. * However when taken in a stricter environment the RCU lock does not loosen
  3040. * the constraints.
  3041. *
  3042. * Therefore we must look for the strictest environment in the lock stack and
  3043. * compare that to the lock we're trying to acquire.
  3044. */
  3045. static int check_wait_context(struct task_struct *curr, struct held_lock *next)
  3046. {
  3047. short next_inner = hlock_class(next)->wait_type_inner;
  3048. short next_outer = hlock_class(next)->wait_type_outer;
  3049. short curr_inner;
  3050. int depth;
  3051. if (!curr->lockdep_depth || !next_inner || next->trylock)
  3052. return 0;
  3053. if (!next_outer)
  3054. next_outer = next_inner;
  3055. /*
  3056. * Find start of current irq_context..
  3057. */
  3058. for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
  3059. struct held_lock *prev = curr->held_locks + depth;
  3060. if (prev->irq_context != next->irq_context)
  3061. break;
  3062. }
  3063. depth++;
  3064. /*
  3065. * Set appropriate wait type for the context; for IRQs we have to take
  3066. * into account force_irqthread as that is implied by PREEMPT_RT.
  3067. */
  3068. if (curr->hardirq_context) {
  3069. /*
  3070. * Check if force_irqthreads will run us threaded.
  3071. */
  3072. if (curr->hardirq_threaded)
  3073. curr_inner = LD_WAIT_CONFIG;
  3074. else
  3075. curr_inner = LD_WAIT_SPIN;
  3076. } else if (curr->softirq_context) {
  3077. /*
  3078. * Softirqs are always threaded.
  3079. */
  3080. curr_inner = LD_WAIT_CONFIG;
  3081. } else {
  3082. curr_inner = LD_WAIT_MAX;
  3083. }
  3084. for (; depth < curr->lockdep_depth; depth++) {
  3085. struct held_lock *prev = curr->held_locks + depth;
  3086. short prev_inner = hlock_class(prev)->wait_type_inner;
  3087. if (prev_inner) {
  3088. /*
  3089. * We can have a bigger inner than a previous one
  3090. * when outer is smaller than inner, as with RCU.
  3091. *
  3092. * Also due to trylocks.
  3093. */
  3094. curr_inner = min(curr_inner, prev_inner);
  3095. }
  3096. }
  3097. if (next_outer > curr_inner)
  3098. return print_lock_invalid_wait_context(curr, next);
  3099. return 0;
  3100. }
  3101. /*
  3102. * Initialize a lock instance's lock-class mapping info:
  3103. */
  3104. static void __lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
  3105. struct lock_class_key *key, int subclass,
  3106. short inner, short outer)
  3107. {
  3108. int i;
  3109. for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
  3110. lock->class_cache[i] = NULL;
  3111. #ifdef CONFIG_LOCK_STAT
  3112. lock->cpu = raw_smp_processor_id();
  3113. #endif
  3114. /*
  3115. * Can't be having no nameless bastards around this place!
  3116. */
  3117. if (DEBUG_LOCKS_WARN_ON(!name)) {
  3118. lock->name = "NULL";
  3119. return;
  3120. }
  3121. lock->name = name;
  3122. lock->wait_type_outer = outer;
  3123. lock->wait_type_inner = inner;
  3124. /*
  3125. * No key, no joy, we need to hash something.
  3126. */
  3127. if (DEBUG_LOCKS_WARN_ON(!key))
  3128. return;
  3129. /*
  3130. * Sanity check, the lock-class key must be persistent:
  3131. */
  3132. if (!static_obj(key)) {
  3133. printk("BUG: key %p not in .data!\n", key);
  3134. /*
  3135. * What it says above ^^^^^, I suggest you read it.
  3136. */
  3137. DEBUG_LOCKS_WARN_ON(1);
  3138. lockdep_aee();
  3139. return;
  3140. }
  3141. lock->key = key;
  3142. if (unlikely(!debug_locks))
  3143. return;
  3144. if (subclass) {
  3145. unsigned long flags;
  3146. if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
  3147. return;
  3148. raw_local_irq_save(flags);
  3149. current->lockdep_recursion = 1;
  3150. register_lock_class(lock, subclass, 1);
  3151. current->lockdep_recursion = 0;
  3152. raw_local_irq_restore(flags);
  3153. }
  3154. }
  3155. void lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
  3156. struct lock_class_key *key, int subclass)
  3157. {
  3158. cross_init(lock, 0);
  3159. __lockdep_init_map_waits(lock, name, key, subclass);
  3160. }
  3161. EXPORT_SYMBOL_GPL(lockdep_init_map_waits);
  3162. #ifdef CONFIG_LOCKDEP_CROSSRELEASE
  3163. void lockdep_init_map_crosslock(struct lockdep_map *lock, const char *name,
  3164. struct lock_class_key *key, int subclass)
  3165. {
  3166. cross_init(lock, 1);
  3167. __lockdep_init_map(lock, name, key, subclass);
  3168. }
  3169. EXPORT_SYMBOL_GPL(lockdep_init_map_crosslock);
  3170. #endif
  3171. struct lock_class_key __lockdep_no_validate__;
  3172. EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
  3173. static int
  3174. print_lock_nested_lock_not_held(struct task_struct *curr,
  3175. struct held_lock *hlock,
  3176. unsigned long ip)
  3177. {
  3178. if (!debug_locks_off())
  3179. return 0;
  3180. if (debug_locks_silent)
  3181. return 0;
  3182. pr_warn("\n");
  3183. pr_warn("==================================\n");
  3184. pr_warn("WARNING: Nested lock was not taken\n");
  3185. print_kernel_ident();
  3186. pr_warn("----------------------------------\n");
  3187. pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
  3188. print_lock(hlock);
  3189. pr_warn("\nbut this task is not holding:\n");
  3190. pr_warn("%s\n", hlock->nest_lock->name);
  3191. pr_warn("\nstack backtrace:\n");
  3192. dump_stack();
  3193. pr_warn("\nother info that might help us debug this:\n");
  3194. lockdep_print_held_locks(curr);
  3195. pr_warn("\nstack backtrace:\n");
  3196. dump_stack();
  3197. lockdep_aee();
  3198. return 0;
  3199. }
  3200. static int __lock_is_held(struct lockdep_map *lock, int read);
  3201. /*
  3202. * This gets called for every mutex_lock*()/spin_lock*() operation.
  3203. * We maintain the dependency maps and validate the locking attempt:
  3204. */
  3205. static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  3206. int trylock, int read, int check, int hardirqs_off,
  3207. struct lockdep_map *nest_lock, unsigned long ip,
  3208. int references, int pin_count)
  3209. {
  3210. struct task_struct *curr = current;
  3211. struct lock_class *class = NULL;
  3212. struct held_lock *hlock;
  3213. unsigned int depth;
  3214. int chain_head = 0;
  3215. int class_idx;
  3216. u64 chain_key;
  3217. int ret;
  3218. if (unlikely(!debug_locks)) {
  3219. __add_held_lock(lock, subclass, trylock, read, check,
  3220. hardirqs_off, nest_lock, ip,
  3221. references, pin_count);
  3222. return 0;
  3223. }
  3224. /*
  3225. * Lockdep should run with IRQs disabled, otherwise we could
  3226. * get an interrupt which would want to take locks, which would
  3227. * end up in lockdep and have you got a head-ache already?
  3228. */
  3229. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  3230. return 0;
  3231. if (!prove_locking || lock->key == &__lockdep_no_validate__)
  3232. check = 0;
  3233. if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  3234. class = lock->class_cache[subclass];
  3235. /*
  3236. * Not cached?
  3237. */
  3238. if (unlikely(!class)) {
  3239. class = register_lock_class(lock, subclass, 0);
  3240. if (!class)
  3241. return 0;
  3242. }
  3243. atomic_inc((atomic_t *)&class->ops);
  3244. if (very_verbose(class)) {
  3245. printk("\nacquire class [%p] %s", class->key, class->name);
  3246. if (class->name_version > 1)
  3247. printk(KERN_CONT "#%d", class->name_version);
  3248. printk(KERN_CONT "\n");
  3249. dump_stack();
  3250. }
  3251. /*
  3252. * Add the lock to the list of currently held locks.
  3253. * (we dont increase the depth just yet, up until the
  3254. * dependency checks are done)
  3255. */
  3256. depth = curr->lockdep_depth;
  3257. /*
  3258. * Ran out of static storage for our per-task lock stack again have we?
  3259. */
  3260. if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
  3261. return 0;
  3262. class_idx = class - lock_classes + 1;
  3263. /* TODO: nest_lock is not implemented for crosslock yet. */
  3264. if (depth && !cross_lock(lock)) {
  3265. hlock = curr->held_locks + depth - 1;
  3266. if (hlock->class_idx == class_idx && nest_lock) {
  3267. if (!references)
  3268. references++;
  3269. if (!hlock->references)
  3270. hlock->references++;
  3271. hlock->references += references;
  3272. /* Overflow */
  3273. if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
  3274. return 0;
  3275. return 1;
  3276. }
  3277. }
  3278. hlock = curr->held_locks + depth;
  3279. /*
  3280. * Plain impossible, we just registered it and checked it weren't no
  3281. * NULL like.. I bet this mushroom I ate was good!
  3282. */
  3283. if (DEBUG_LOCKS_WARN_ON(!class))
  3284. return 0;
  3285. hlock->class_idx = class_idx;
  3286. hlock->acquire_ip = ip;
  3287. hlock->instance = lock;
  3288. hlock->nest_lock = nest_lock;
  3289. hlock->irq_context = task_irq_context(curr);
  3290. hlock->trylock = trylock;
  3291. hlock->read = read;
  3292. hlock->check = check;
  3293. hlock->hardirqs_off = !!hardirqs_off;
  3294. hlock->references = references;
  3295. #ifdef CONFIG_LOCK_STAT
  3296. hlock->waittime_stamp = 0;
  3297. hlock->holdtime_stamp = lockstat_clock();
  3298. #endif
  3299. hlock->pin_count = pin_count;
  3300. /* MTK_LOCK_DEBUG_HELD_LOCK */
  3301. held_lock_save_trace(&hlock->trace, hlock->entries);
  3302. /* MTK_LOCK_MONITOR */
  3303. hlock->timestamp = sched_clock();
  3304. hlock->acquired = true;
  3305. if (check && !mark_irqflags(curr, hlock))
  3306. return 0;
  3307. /* mark it as used: */
  3308. if (!mark_lock(curr, hlock, LOCK_USED))
  3309. return 0;
  3310. /*
  3311. * Calculate the chain hash: it's the combined hash of all the
  3312. * lock keys along the dependency chain. We save the hash value
  3313. * at every step so that we can get the current hash easily
  3314. * after unlock. The chain hash is then used to cache dependency
  3315. * results.
  3316. *
  3317. * The 'key ID' is what is the most compact key value to drive
  3318. * the hash, not class->key.
  3319. */
  3320. /*
  3321. * Whoops, we did it again.. ran straight out of our static allocation.
  3322. */
  3323. if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
  3324. return 0;
  3325. chain_key = curr->curr_chain_key;
  3326. if (!depth) {
  3327. /*
  3328. * How can we have a chain hash when we ain't got no keys?!
  3329. */
  3330. if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
  3331. return 0;
  3332. chain_head = 1;
  3333. }
  3334. hlock->prev_chain_key = chain_key;
  3335. if (separate_irq_context(curr, hlock)) {
  3336. chain_key = 0;
  3337. chain_head = 1;
  3338. }
  3339. chain_key = iterate_chain_key(chain_key, class_idx);
  3340. if (nest_lock && !__lock_is_held(nest_lock, -1))
  3341. return print_lock_nested_lock_not_held(curr, hlock, ip);
  3342. if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
  3343. return 0;
  3344. ret = lock_acquire_crosslock(hlock);
  3345. /*
  3346. * 2 means normal acquire operations are needed. Otherwise, it's
  3347. * ok just to return with '0:fail, 1:success'.
  3348. */
  3349. if (ret != 2)
  3350. return ret;
  3351. curr->curr_chain_key = chain_key;
  3352. curr->lockdep_depth++;
  3353. check_chain_key(curr);
  3354. #ifdef CONFIG_DEBUG_LOCKDEP
  3355. if (unlikely(!debug_locks))
  3356. return 0;
  3357. #endif
  3358. if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
  3359. debug_locks_off();
  3360. print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
  3361. printk(KERN_DEBUG "depth: %i max: %lu!\n",
  3362. curr->lockdep_depth, MAX_LOCK_DEPTH);
  3363. lockdep_print_held_locks(current);
  3364. debug_show_all_locks();
  3365. dump_stack();
  3366. return 0;
  3367. }
  3368. if (unlikely(curr->lockdep_depth > max_lockdep_depth))
  3369. max_lockdep_depth = curr->lockdep_depth;
  3370. return 1;
  3371. }
  3372. static int
  3373. print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
  3374. unsigned long ip)
  3375. {
  3376. if (!debug_locks_off())
  3377. return 0;
  3378. if (debug_locks_silent)
  3379. return 0;
  3380. pr_warn("\n");
  3381. pr_warn("=====================================\n");
  3382. pr_warn("WARNING: bad unlock balance detected!\n");
  3383. print_kernel_ident();
  3384. pr_warn("-------------------------------------\n");
  3385. pr_warn("%s/%d is trying to release lock (",
  3386. curr->comm, task_pid_nr(curr));
  3387. print_lockdep_cache(lock);
  3388. pr_cont(") at:\n");
  3389. print_ip_sym(ip);
  3390. pr_warn("but there are no more locks to release!\n");
  3391. pr_warn("\nother info that might help us debug this:\n");
  3392. lockdep_print_held_locks(curr);
  3393. pr_warn("\nstack backtrace:\n");
  3394. dump_stack();
  3395. lockdep_aee();
  3396. return 0;
  3397. }
  3398. static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
  3399. {
  3400. if (hlock->instance == lock)
  3401. return 1;
  3402. if (hlock->references) {
  3403. struct lock_class *class = lock->class_cache[0];
  3404. if (!class)
  3405. class = look_up_lock_class(lock, 0);
  3406. /*
  3407. * If look_up_lock_class() failed to find a class, we're trying
  3408. * to test if we hold a lock that has never yet been acquired.
  3409. * Clearly if the lock hasn't been acquired _ever_, we're not
  3410. * holding it either, so report failure.
  3411. */
  3412. if (IS_ERR_OR_NULL(class))
  3413. return 0;
  3414. /*
  3415. * References, but not a lock we're actually ref-counting?
  3416. * State got messed up, follow the sites that change ->references
  3417. * and try to make sense of it.
  3418. */
  3419. if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
  3420. return 0;
  3421. if (hlock->class_idx == class - lock_classes + 1)
  3422. return 1;
  3423. }
  3424. return 0;
  3425. }
  3426. /* @depth must not be zero */
  3427. static struct held_lock *find_held_lock(struct task_struct *curr,
  3428. struct lockdep_map *lock,
  3429. unsigned int depth, int *idx)
  3430. {
  3431. struct held_lock *ret, *hlock, *prev_hlock;
  3432. int i;
  3433. i = depth - 1;
  3434. hlock = curr->held_locks + i;
  3435. ret = hlock;
  3436. if (match_held_lock(hlock, lock))
  3437. goto out;
  3438. ret = NULL;
  3439. for (i--, prev_hlock = hlock--;
  3440. i >= 0;
  3441. i--, prev_hlock = hlock--) {
  3442. /*
  3443. * We must not cross into another context:
  3444. */
  3445. if (prev_hlock->irq_context != hlock->irq_context) {
  3446. ret = NULL;
  3447. break;
  3448. }
  3449. if (match_held_lock(hlock, lock)) {
  3450. ret = hlock;
  3451. break;
  3452. }
  3453. }
  3454. out:
  3455. *idx = i;
  3456. return ret;
  3457. }
  3458. static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
  3459. int idx)
  3460. {
  3461. struct held_lock *hlock;
  3462. for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
  3463. if (!__lock_acquire(hlock->instance,
  3464. hlock_class(hlock)->subclass,
  3465. hlock->trylock,
  3466. hlock->read, hlock->check,
  3467. hlock->hardirqs_off,
  3468. hlock->nest_lock, hlock->acquire_ip,
  3469. hlock->references, hlock->pin_count))
  3470. return 1;
  3471. }
  3472. return 0;
  3473. }
  3474. static int
  3475. __lock_set_class(struct lockdep_map *lock, const char *name,
  3476. struct lock_class_key *key, unsigned int subclass,
  3477. unsigned long ip)
  3478. {
  3479. struct task_struct *curr = current;
  3480. struct held_lock *hlock;
  3481. struct lock_class *class;
  3482. unsigned int depth;
  3483. int i;
  3484. depth = curr->lockdep_depth;
  3485. /*
  3486. * This function is about (re)setting the class of a held lock,
  3487. * yet we're not actually holding any locks. Naughty user!
  3488. */
  3489. if (DEBUG_LOCKS_WARN_ON(!depth))
  3490. return 0;
  3491. hlock = find_held_lock(curr, lock, depth, &i);
  3492. if (!hlock)
  3493. return print_unlock_imbalance_bug(curr, lock, ip);
  3494. lockdep_init_map_waits(lock, name, key, 0,
  3495. lock->wait_type_inner,
  3496. lock->wait_type_outer);
  3497. class = register_lock_class(lock, subclass, 0);
  3498. hlock->class_idx = class - lock_classes + 1;
  3499. curr->lockdep_depth = i;
  3500. curr->curr_chain_key = hlock->prev_chain_key;
  3501. if (reacquire_held_locks(curr, depth, i))
  3502. return 0;
  3503. /*
  3504. * I took it apart and put it back together again, except now I have
  3505. * these 'spare' parts.. where shall I put them.
  3506. */
  3507. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
  3508. return 0;
  3509. return 1;
  3510. }
  3511. static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
  3512. {
  3513. struct task_struct *curr = current;
  3514. struct held_lock *hlock;
  3515. unsigned int depth;
  3516. int i;
  3517. if (unlikely(!debug_locks))
  3518. return 0;
  3519. depth = curr->lockdep_depth;
  3520. /*
  3521. * This function is about (re)setting the class of a held lock,
  3522. * yet we're not actually holding any locks. Naughty user!
  3523. */
  3524. if (DEBUG_LOCKS_WARN_ON(!depth))
  3525. return 0;
  3526. hlock = find_held_lock(curr, lock, depth, &i);
  3527. if (!hlock)
  3528. return print_unlock_imbalance_bug(curr, lock, ip);
  3529. curr->lockdep_depth = i;
  3530. curr->curr_chain_key = hlock->prev_chain_key;
  3531. WARN(hlock->read, "downgrading a read lock");
  3532. hlock->read = 1;
  3533. hlock->acquire_ip = ip;
  3534. if (reacquire_held_locks(curr, depth, i))
  3535. return 0;
  3536. /*
  3537. * I took it apart and put it back together again, except now I have
  3538. * these 'spare' parts.. where shall I put them.
  3539. */
  3540. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
  3541. return 0;
  3542. return 1;
  3543. }
  3544. /*
  3545. * Remove the lock to the list of currently held locks - this gets
  3546. * called on mutex_unlock()/spin_unlock*() (or on a failed
  3547. * mutex_lock_interruptible()).
  3548. *
  3549. * @nested is an hysterical artifact, needs a tree wide cleanup.
  3550. */
  3551. static int
  3552. __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
  3553. {
  3554. struct task_struct *curr = current;
  3555. struct held_lock *hlock;
  3556. unsigned int depth;
  3557. int ret, i;
  3558. if (unlikely(!debug_locks)) {
  3559. __remove_held_lock(lock, nested, ip);
  3560. return 0;
  3561. }
  3562. ret = lock_release_crosslock(lock);
  3563. /*
  3564. * 2 means normal release operations are needed. Otherwise, it's
  3565. * ok just to return with '0:fail, 1:success'.
  3566. */
  3567. if (ret != 2)
  3568. return ret;
  3569. depth = curr->lockdep_depth;
  3570. /*
  3571. * So we're all set to release this lock.. wait what lock? We don't
  3572. * own any locks, you've been drinking again?
  3573. */
  3574. if (depth <= 0)
  3575. return print_unlock_imbalance_bug(curr, lock, ip);
  3576. /*
  3577. * Check whether the lock exists in the current stack
  3578. * of held locks:
  3579. */
  3580. hlock = find_held_lock(curr, lock, depth, &i);
  3581. if (!hlock)
  3582. return print_unlock_imbalance_bug(curr, lock, ip);
  3583. if (hlock->instance == lock)
  3584. lock_release_holdtime(hlock);
  3585. /* MTK_LOCK_MONITOR */
  3586. hlock->timestamp = 0;
  3587. WARN(hlock->pin_count, "releasing a pinned lock\n");
  3588. if (hlock->references) {
  3589. hlock->references--;
  3590. if (hlock->references) {
  3591. /*
  3592. * We had, and after removing one, still have
  3593. * references, the current lock stack is still
  3594. * valid. We're done!
  3595. */
  3596. return 1;
  3597. }
  3598. }
  3599. /*
  3600. * We have the right lock to unlock, 'hlock' points to it.
  3601. * Now we remove it from the stack, and add back the other
  3602. * entries (if any), recalculating the hash along the way:
  3603. */
  3604. curr->lockdep_depth = i;
  3605. curr->curr_chain_key = hlock->prev_chain_key;
  3606. if (reacquire_held_locks(curr, depth, i + 1))
  3607. return 0;
  3608. /*
  3609. * We had N bottles of beer on the wall, we drank one, but now
  3610. * there's not N-1 bottles of beer left on the wall...
  3611. */
  3612. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
  3613. return 0;
  3614. return 1;
  3615. }
  3616. static int __lock_is_held(struct lockdep_map *lock, int read)
  3617. {
  3618. struct task_struct *curr = current;
  3619. int i;
  3620. for (i = 0; i < curr->lockdep_depth; i++) {
  3621. struct held_lock *hlock = curr->held_locks + i;
  3622. if (match_held_lock(hlock, lock)) {
  3623. if (read == -1 || hlock->read == read)
  3624. return 1;
  3625. return 0;
  3626. }
  3627. }
  3628. return 0;
  3629. }
  3630. static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
  3631. {
  3632. struct pin_cookie cookie = NIL_COOKIE;
  3633. struct task_struct *curr = current;
  3634. int i;
  3635. if (unlikely(!debug_locks))
  3636. return cookie;
  3637. for (i = 0; i < curr->lockdep_depth; i++) {
  3638. struct held_lock *hlock = curr->held_locks + i;
  3639. if (match_held_lock(hlock, lock)) {
  3640. /*
  3641. * Grab 16bits of randomness; this is sufficient to not
  3642. * be guessable and still allows some pin nesting in
  3643. * our u32 pin_count.
  3644. */
  3645. cookie.val = 1 + (prandom_u32() >> 16);
  3646. hlock->pin_count += cookie.val;
  3647. return cookie;
  3648. }
  3649. }
  3650. WARN(1, "pinning an unheld lock\n");
  3651. return cookie;
  3652. }
  3653. static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3654. {
  3655. struct task_struct *curr = current;
  3656. int i;
  3657. if (unlikely(!debug_locks))
  3658. return;
  3659. for (i = 0; i < curr->lockdep_depth; i++) {
  3660. struct held_lock *hlock = curr->held_locks + i;
  3661. if (match_held_lock(hlock, lock)) {
  3662. hlock->pin_count += cookie.val;
  3663. return;
  3664. }
  3665. }
  3666. WARN(1, "pinning an unheld lock\n");
  3667. }
  3668. static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3669. {
  3670. struct task_struct *curr = current;
  3671. int i;
  3672. if (unlikely(!debug_locks))
  3673. return;
  3674. for (i = 0; i < curr->lockdep_depth; i++) {
  3675. struct held_lock *hlock = curr->held_locks + i;
  3676. if (match_held_lock(hlock, lock)) {
  3677. if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
  3678. return;
  3679. hlock->pin_count -= cookie.val;
  3680. if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
  3681. hlock->pin_count = 0;
  3682. return;
  3683. }
  3684. }
  3685. WARN(1, "unpinning an unheld lock\n");
  3686. }
  3687. /*
  3688. * Check whether we follow the irq-flags state precisely:
  3689. */
  3690. static void check_flags(unsigned long flags)
  3691. {
  3692. #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
  3693. defined(CONFIG_TRACE_IRQFLAGS)
  3694. if (!debug_locks)
  3695. return;
  3696. if (irqs_disabled_flags(flags)) {
  3697. if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
  3698. printk("possible reason: unannotated irqs-off.\n");
  3699. }
  3700. } else {
  3701. if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
  3702. printk("possible reason: unannotated irqs-on.\n");
  3703. }
  3704. }
  3705. /*
  3706. * We dont accurately track softirq state in e.g.
  3707. * hardirq contexts (such as on 4KSTACKS), so only
  3708. * check if not in hardirq contexts:
  3709. */
  3710. if (!hardirq_count()) {
  3711. if (softirq_count()) {
  3712. /* like the above, but with softirqs */
  3713. DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
  3714. } else {
  3715. /* lick the above, does it taste good? */
  3716. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  3717. }
  3718. }
  3719. if (!debug_locks)
  3720. print_irqtrace_events(current);
  3721. #endif
  3722. }
  3723. void lock_set_class(struct lockdep_map *lock, const char *name,
  3724. struct lock_class_key *key, unsigned int subclass,
  3725. unsigned long ip)
  3726. {
  3727. unsigned long flags;
  3728. if (unlikely(current->lockdep_recursion))
  3729. return;
  3730. raw_local_irq_save(flags);
  3731. current->lockdep_recursion = 1;
  3732. check_flags(flags);
  3733. if (__lock_set_class(lock, name, key, subclass, ip))
  3734. check_chain_key(current);
  3735. current->lockdep_recursion = 0;
  3736. raw_local_irq_restore(flags);
  3737. }
  3738. EXPORT_SYMBOL_GPL(lock_set_class);
  3739. void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
  3740. {
  3741. unsigned long flags;
  3742. if (unlikely(current->lockdep_recursion))
  3743. return;
  3744. raw_local_irq_save(flags);
  3745. current->lockdep_recursion = 1;
  3746. check_flags(flags);
  3747. if (__lock_downgrade(lock, ip))
  3748. check_chain_key(current);
  3749. current->lockdep_recursion = 0;
  3750. raw_local_irq_restore(flags);
  3751. }
  3752. EXPORT_SYMBOL_GPL(lock_downgrade);
  3753. /*
  3754. * We are not always called with irqs disabled - do that here,
  3755. * and also avoid lockdep recursion:
  3756. */
  3757. void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  3758. int trylock, int read, int check,
  3759. struct lockdep_map *nest_lock, unsigned long ip)
  3760. {
  3761. unsigned long flags;
  3762. if (unlikely(current->lockdep_recursion))
  3763. return;
  3764. raw_local_irq_save(flags);
  3765. check_flags(flags);
  3766. current->lockdep_recursion = 1;
  3767. trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
  3768. __lock_acquire(lock, subclass, trylock, read, check,
  3769. irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
  3770. current->lockdep_recursion = 0;
  3771. raw_local_irq_restore(flags);
  3772. }
  3773. EXPORT_SYMBOL_GPL(lock_acquire);
  3774. void lock_release(struct lockdep_map *lock, int nested,
  3775. unsigned long ip)
  3776. {
  3777. unsigned long flags;
  3778. if (unlikely(current->lockdep_recursion))
  3779. return;
  3780. raw_local_irq_save(flags);
  3781. check_flags(flags);
  3782. current->lockdep_recursion = 1;
  3783. trace_lock_release(lock, ip);
  3784. if (__lock_release(lock, nested, ip))
  3785. check_chain_key(current);
  3786. current->lockdep_recursion = 0;
  3787. raw_local_irq_restore(flags);
  3788. }
  3789. EXPORT_SYMBOL_GPL(lock_release);
  3790. int lock_is_held_type(struct lockdep_map *lock, int read)
  3791. {
  3792. unsigned long flags;
  3793. int ret = 0;
  3794. if (unlikely(current->lockdep_recursion))
  3795. return 1; /* avoid false negative lockdep_assert_held() */
  3796. raw_local_irq_save(flags);
  3797. check_flags(flags);
  3798. current->lockdep_recursion = 1;
  3799. ret = __lock_is_held(lock, read);
  3800. current->lockdep_recursion = 0;
  3801. raw_local_irq_restore(flags);
  3802. return ret;
  3803. }
  3804. EXPORT_SYMBOL_GPL(lock_is_held_type);
  3805. struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
  3806. {
  3807. struct pin_cookie cookie = NIL_COOKIE;
  3808. unsigned long flags;
  3809. if (unlikely(current->lockdep_recursion))
  3810. return cookie;
  3811. raw_local_irq_save(flags);
  3812. check_flags(flags);
  3813. current->lockdep_recursion = 1;
  3814. cookie = __lock_pin_lock(lock);
  3815. current->lockdep_recursion = 0;
  3816. raw_local_irq_restore(flags);
  3817. return cookie;
  3818. }
  3819. EXPORT_SYMBOL_GPL(lock_pin_lock);
  3820. void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3821. {
  3822. unsigned long flags;
  3823. if (unlikely(current->lockdep_recursion))
  3824. return;
  3825. raw_local_irq_save(flags);
  3826. check_flags(flags);
  3827. current->lockdep_recursion = 1;
  3828. __lock_repin_lock(lock, cookie);
  3829. current->lockdep_recursion = 0;
  3830. raw_local_irq_restore(flags);
  3831. }
  3832. EXPORT_SYMBOL_GPL(lock_repin_lock);
  3833. void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3834. {
  3835. unsigned long flags;
  3836. if (unlikely(current->lockdep_recursion))
  3837. return;
  3838. raw_local_irq_save(flags);
  3839. check_flags(flags);
  3840. current->lockdep_recursion = 1;
  3841. __lock_unpin_lock(lock, cookie);
  3842. current->lockdep_recursion = 0;
  3843. raw_local_irq_restore(flags);
  3844. }
  3845. EXPORT_SYMBOL_GPL(lock_unpin_lock);
  3846. #ifdef CONFIG_LOCK_STAT
  3847. static int
  3848. print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
  3849. unsigned long ip)
  3850. {
  3851. if (!debug_locks_off())
  3852. return 0;
  3853. if (debug_locks_silent)
  3854. return 0;
  3855. pr_warn("\n");
  3856. pr_warn("=================================\n");
  3857. pr_warn("WARNING: bad contention detected!\n");
  3858. print_kernel_ident();
  3859. pr_warn("---------------------------------\n");
  3860. pr_warn("%s/%d is trying to contend lock (",
  3861. curr->comm, task_pid_nr(curr));
  3862. print_lockdep_cache(lock);
  3863. pr_cont(") at:\n");
  3864. print_ip_sym(ip);
  3865. pr_warn("but there are no locks held!\n");
  3866. pr_warn("\nother info that might help us debug this:\n");
  3867. lockdep_print_held_locks(curr);
  3868. pr_warn("\nstack backtrace:\n");
  3869. dump_stack();
  3870. lockdep_aee();
  3871. return 0;
  3872. }
  3873. static void
  3874. __lock_contended(struct lockdep_map *lock, unsigned long ip)
  3875. {
  3876. struct task_struct *curr = current;
  3877. struct held_lock *hlock;
  3878. struct lock_class_stats *stats;
  3879. unsigned int depth;
  3880. int i, contention_point, contending_point;
  3881. depth = curr->lockdep_depth;
  3882. /*
  3883. * Whee, we contended on this lock, except it seems we're not
  3884. * actually trying to acquire anything much at all..
  3885. */
  3886. if (DEBUG_LOCKS_WARN_ON(!depth))
  3887. return;
  3888. hlock = find_held_lock(curr, lock, depth, &i);
  3889. if (!hlock) {
  3890. print_lock_contention_bug(curr, lock, ip);
  3891. return;
  3892. }
  3893. if (hlock->instance != lock)
  3894. return;
  3895. hlock->waittime_stamp = lockstat_clock();
  3896. contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
  3897. contending_point = lock_point(hlock_class(hlock)->contending_point,
  3898. lock->ip);
  3899. stats = get_lock_stats(hlock_class(hlock));
  3900. if (contention_point < LOCKSTAT_POINTS)
  3901. stats->contention_point[contention_point]++;
  3902. if (contending_point < LOCKSTAT_POINTS)
  3903. stats->contending_point[contending_point]++;
  3904. if (lock->cpu != smp_processor_id())
  3905. stats->bounces[bounce_contended + !!hlock->read]++;
  3906. put_lock_stats(stats);
  3907. #ifdef MTK_LOCK_MONITOR
  3908. hlock->acquired = false;
  3909. #endif
  3910. }
  3911. static void
  3912. __lock_acquired(struct lockdep_map *lock, unsigned long ip)
  3913. {
  3914. struct task_struct *curr = current;
  3915. struct held_lock *hlock;
  3916. struct lock_class_stats *stats;
  3917. unsigned int depth;
  3918. u64 now, waittime = 0;
  3919. int i, cpu;
  3920. depth = curr->lockdep_depth;
  3921. /*
  3922. * Yay, we acquired ownership of this lock we didn't try to
  3923. * acquire, how the heck did that happen?
  3924. */
  3925. if (DEBUG_LOCKS_WARN_ON(!depth))
  3926. return;
  3927. hlock = find_held_lock(curr, lock, depth, &i);
  3928. if (!hlock) {
  3929. print_lock_contention_bug(curr, lock, _RET_IP_);
  3930. return;
  3931. }
  3932. if (hlock->instance != lock)
  3933. return;
  3934. cpu = smp_processor_id();
  3935. if (hlock->waittime_stamp) {
  3936. now = lockstat_clock();
  3937. waittime = now - hlock->waittime_stamp;
  3938. hlock->holdtime_stamp = now;
  3939. }
  3940. trace_lock_acquired(lock, ip);
  3941. stats = get_lock_stats(hlock_class(hlock));
  3942. if (waittime) {
  3943. if (hlock->read)
  3944. lock_time_inc(&stats->read_waittime, waittime);
  3945. else
  3946. lock_time_inc(&stats->write_waittime, waittime);
  3947. }
  3948. if (lock->cpu != cpu)
  3949. stats->bounces[bounce_acquired + !!hlock->read]++;
  3950. put_lock_stats(stats);
  3951. lock->cpu = cpu;
  3952. lock->ip = ip;
  3953. #ifdef MTK_LOCK_MONITOR
  3954. hlock->acquired = true;
  3955. #endif
  3956. }
  3957. void lock_contended(struct lockdep_map *lock, unsigned long ip)
  3958. {
  3959. unsigned long flags;
  3960. if (unlikely(!lock_stat || !debug_locks) && !lock_mon_enabled())
  3961. return;
  3962. if (unlikely(current->lockdep_recursion))
  3963. return;
  3964. raw_local_irq_save(flags);
  3965. check_flags(flags);
  3966. current->lockdep_recursion = 1;
  3967. trace_lock_contended(lock, ip);
  3968. __lock_contended(lock, ip);
  3969. current->lockdep_recursion = 0;
  3970. raw_local_irq_restore(flags);
  3971. }
  3972. EXPORT_SYMBOL_GPL(lock_contended);
  3973. void lock_acquired(struct lockdep_map *lock, unsigned long ip)
  3974. {
  3975. unsigned long flags;
  3976. if (unlikely(!lock_stat || !debug_locks) && !lock_mon_enabled())
  3977. return;
  3978. if (unlikely(current->lockdep_recursion))
  3979. return;
  3980. raw_local_irq_save(flags);
  3981. check_flags(flags);
  3982. current->lockdep_recursion = 1;
  3983. __lock_acquired(lock, ip);
  3984. current->lockdep_recursion = 0;
  3985. raw_local_irq_restore(flags);
  3986. }
  3987. EXPORT_SYMBOL_GPL(lock_acquired);
  3988. #elif defined(MTK_LOCK_MONITOR)
  3989. static void
  3990. set_acquired(struct lockdep_map *lock, unsigned long ip, bool acquired)
  3991. {
  3992. struct task_struct *curr = current;
  3993. struct held_lock *hlock;
  3994. unsigned int depth = curr->lockdep_depth;
  3995. int i;
  3996. if (DEBUG_LOCKS_WARN_ON(!depth))
  3997. return;
  3998. hlock = find_held_lock(curr, lock, depth, &i);
  3999. if (!hlock)
  4000. return;
  4001. if (hlock->instance != lock)
  4002. return;
  4003. hlock->acquired = acquired;
  4004. }
  4005. static void
  4006. set_lock_acquired(struct lockdep_map *lock, unsigned long ip, bool acquired)
  4007. {
  4008. unsigned long flags;
  4009. if (unlikely(current->lockdep_recursion))
  4010. return;
  4011. raw_local_irq_save(flags);
  4012. check_flags(flags);
  4013. current->lockdep_recursion = 1;
  4014. set_acquired(lock, ip, acquired);
  4015. current->lockdep_recursion = 0;
  4016. raw_local_irq_restore(flags);
  4017. }
  4018. void lock_contended(struct lockdep_map *lock, unsigned long ip)
  4019. {
  4020. set_lock_acquired(lock, ip, false);
  4021. }
  4022. void lock_acquired(struct lockdep_map *lock, unsigned long ip)
  4023. {
  4024. set_lock_acquired(lock, ip, true);
  4025. }
  4026. #else
  4027. void lock_contended(struct lockdep_map *lock, unsigned long ip) {}
  4028. void lock_acquired(struct lockdep_map *lock, unsigned long ip) {}
  4029. #endif
  4030. /*
  4031. * Used by the testsuite, sanitize the validator state
  4032. * after a simulated failure:
  4033. */
  4034. void lockdep_reset(void)
  4035. {
  4036. unsigned long flags;
  4037. int i;
  4038. raw_local_irq_save(flags);
  4039. current->curr_chain_key = 0;
  4040. current->lockdep_depth = 0;
  4041. current->lockdep_recursion = 0;
  4042. memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
  4043. nr_hardirq_chains = 0;
  4044. nr_softirq_chains = 0;
  4045. nr_process_chains = 0;
  4046. debug_locks = 1;
  4047. for (i = 0; i < CHAINHASH_SIZE; i++)
  4048. INIT_HLIST_HEAD(chainhash_table + i);
  4049. raw_local_irq_restore(flags);
  4050. }
  4051. static void zap_class(struct lock_class *class)
  4052. {
  4053. int i;
  4054. /*
  4055. * Remove all dependencies this lock is
  4056. * involved in:
  4057. */
  4058. for (i = 0; i < nr_list_entries; i++) {
  4059. if (list_entries[i].class == class)
  4060. list_del_rcu(&list_entries[i].entry);
  4061. }
  4062. /*
  4063. * Unhash the class and remove it from the all_lock_classes list:
  4064. */
  4065. hlist_del_rcu(&class->hash_entry);
  4066. list_del_rcu(&class->lock_entry);
  4067. RCU_INIT_POINTER(class->key, NULL);
  4068. RCU_INIT_POINTER(class->name, NULL);
  4069. }
  4070. static inline int within(const void *addr, void *start, unsigned long size)
  4071. {
  4072. return addr >= start && addr < start + size;
  4073. }
  4074. /*
  4075. * Used in module.c to remove lock classes from memory that is going to be
  4076. * freed; and possibly re-used by other modules.
  4077. *
  4078. * We will have had one sync_sched() before getting here, so we're guaranteed
  4079. * nobody will look up these exact classes -- they're properly dead but still
  4080. * allocated.
  4081. */
  4082. void lockdep_free_key_range(void *start, unsigned long size)
  4083. {
  4084. struct lock_class *class;
  4085. struct hlist_head *head;
  4086. unsigned long flags;
  4087. int i;
  4088. int locked;
  4089. raw_local_irq_save(flags);
  4090. locked = graph_lock();
  4091. /*
  4092. * Unhash all classes that were created by this module:
  4093. */
  4094. for (i = 0; i < CLASSHASH_SIZE; i++) {
  4095. head = classhash_table + i;
  4096. hlist_for_each_entry_rcu(class, head, hash_entry) {
  4097. if (within(class->key, start, size))
  4098. zap_class(class);
  4099. else if (within(class->name, start, size))
  4100. zap_class(class);
  4101. }
  4102. }
  4103. if (locked)
  4104. graph_unlock();
  4105. raw_local_irq_restore(flags);
  4106. /*
  4107. * Wait for any possible iterators from look_up_lock_class() to pass
  4108. * before continuing to free the memory they refer to.
  4109. *
  4110. * sync_sched() is sufficient because the read-side is IRQ disable.
  4111. */
  4112. synchronize_sched();
  4113. /*
  4114. * XXX at this point we could return the resources to the pool;
  4115. * instead we leak them. We would need to change to bitmap allocators
  4116. * instead of the linear allocators we have now.
  4117. */
  4118. }
  4119. void lockdep_reset_lock(struct lockdep_map *lock)
  4120. {
  4121. struct lock_class *class;
  4122. struct hlist_head *head;
  4123. unsigned long flags;
  4124. int i, j;
  4125. int locked;
  4126. raw_local_irq_save(flags);
  4127. /*
  4128. * Remove all classes this lock might have:
  4129. */
  4130. for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
  4131. /*
  4132. * If the class exists we look it up and zap it:
  4133. */
  4134. class = look_up_lock_class(lock, j);
  4135. if (!IS_ERR_OR_NULL(class))
  4136. zap_class(class);
  4137. }
  4138. /*
  4139. * Debug check: in the end all mapped classes should
  4140. * be gone.
  4141. */
  4142. locked = graph_lock();
  4143. for (i = 0; i < CLASSHASH_SIZE; i++) {
  4144. head = classhash_table + i;
  4145. hlist_for_each_entry_rcu(class, head, hash_entry) {
  4146. int match = 0;
  4147. for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
  4148. match |= class == lock->class_cache[j];
  4149. if (unlikely(match)) {
  4150. if (debug_locks_off_graph_unlock()) {
  4151. /*
  4152. * We all just reset everything, how did it match?
  4153. */
  4154. WARN_ON(1);
  4155. }
  4156. goto out_restore;
  4157. }
  4158. }
  4159. }
  4160. if (locked)
  4161. graph_unlock();
  4162. out_restore:
  4163. raw_local_irq_restore(flags);
  4164. }
  4165. void __init lockdep_info(void)
  4166. {
  4167. printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
  4168. printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
  4169. printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
  4170. printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
  4171. printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
  4172. printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
  4173. printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
  4174. printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
  4175. printk(" memory used by lock dependency info: %lu kB\n",
  4176. (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
  4177. sizeof(struct list_head) * CLASSHASH_SIZE +
  4178. sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
  4179. sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
  4180. sizeof(struct list_head) * CHAINHASH_SIZE
  4181. #ifdef CONFIG_PROVE_LOCKING
  4182. + sizeof(struct circular_queue)
  4183. #endif
  4184. ) / 1024
  4185. );
  4186. printk(" per task-struct memory footprint: %lu bytes\n",
  4187. sizeof(struct held_lock) * MAX_LOCK_DEPTH);
  4188. }
  4189. static void
  4190. print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
  4191. const void *mem_to, struct held_lock *hlock)
  4192. {
  4193. if (!debug_locks_off())
  4194. return;
  4195. if (debug_locks_silent)
  4196. return;
  4197. pr_warn("\n");
  4198. pr_warn("=========================\n");
  4199. pr_warn("WARNING: held lock freed!\n");
  4200. print_kernel_ident();
  4201. pr_warn("-------------------------\n");
  4202. pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
  4203. curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
  4204. print_lock(hlock);
  4205. lockdep_print_held_locks(curr);
  4206. pr_warn("\nstack backtrace:\n");
  4207. dump_stack();
  4208. lockdep_aee();
  4209. }
  4210. static inline int not_in_range(const void* mem_from, unsigned long mem_len,
  4211. const void* lock_from, unsigned long lock_len)
  4212. {
  4213. return lock_from + lock_len <= mem_from ||
  4214. mem_from + mem_len <= lock_from;
  4215. }
  4216. /*
  4217. * Called when kernel memory is freed (or unmapped), or if a lock
  4218. * is destroyed or reinitialized - this code checks whether there is
  4219. * any held lock in the memory range of <from> to <to>:
  4220. */
  4221. void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
  4222. {
  4223. struct task_struct *curr = current;
  4224. struct held_lock *hlock;
  4225. unsigned long flags;
  4226. int i;
  4227. if (unlikely(!debug_locks))
  4228. return;
  4229. raw_local_irq_save(flags);
  4230. for (i = 0; i < curr->lockdep_depth; i++) {
  4231. hlock = curr->held_locks + i;
  4232. if (not_in_range(mem_from, mem_len, hlock->instance,
  4233. sizeof(*hlock->instance)))
  4234. continue;
  4235. print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
  4236. break;
  4237. }
  4238. raw_local_irq_restore(flags);
  4239. }
  4240. EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
  4241. static void print_held_locks_bug(void)
  4242. {
  4243. if (!debug_locks_off())
  4244. return;
  4245. if (debug_locks_silent)
  4246. return;
  4247. pr_warn("\n");
  4248. pr_warn("====================================\n");
  4249. pr_warn("WARNING: %s/%d still has locks held!\n",
  4250. current->comm, task_pid_nr(current));
  4251. print_kernel_ident();
  4252. pr_warn("------------------------------------\n");
  4253. lockdep_print_held_locks(current);
  4254. pr_warn("\nstack backtrace:\n");
  4255. dump_stack();
  4256. lockdep_aee();
  4257. }
  4258. void debug_check_no_locks_held(void)
  4259. {
  4260. if (unlikely(current->lockdep_depth > 0))
  4261. print_held_locks_bug();
  4262. }
  4263. EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
  4264. #ifdef __KERNEL__
  4265. void debug_show_all_locks(void)
  4266. {
  4267. struct task_struct *g, *p;
  4268. int count = 10;
  4269. int unlock = 1;
  4270. if (unlikely(!debug_locks) && !lock_mon_enabled()) {
  4271. pr_warn("INFO: lockdep is turned off.\n");
  4272. return;
  4273. }
  4274. pr_warn("\nShowing all locks held in the system:\n");
  4275. /*
  4276. * Here we try to get the tasklist_lock as hard as possible,
  4277. * if not successful after 2 seconds we ignore it (but keep
  4278. * trying). This is to enable a debug printout even if a
  4279. * tasklist_lock-holding task deadlocks or crashes.
  4280. */
  4281. retry:
  4282. if (!read_trylock(&tasklist_lock)) {
  4283. if (count == 10)
  4284. pr_warn("hm, tasklist_lock locked, retrying... ");
  4285. if (count) {
  4286. count--;
  4287. pr_cont(" #%d", 10-count);
  4288. mdelay(200);
  4289. goto retry;
  4290. }
  4291. pr_cont(" ignoring it.\n");
  4292. unlock = 0;
  4293. } else {
  4294. if (count != 10)
  4295. pr_cont(" locked it.\n");
  4296. }
  4297. do_each_thread(g, p) {
  4298. /*
  4299. * It's not reliable to print a task's held locks
  4300. * if it's not sleeping (or if it's not the current
  4301. * task). But we still think the information from
  4302. * non-sleeping tasks are valuable.
  4303. */
  4304. #if 0
  4305. if (p->state == TASK_RUNNING && p != current)
  4306. continue;
  4307. #endif
  4308. if (p->lockdep_depth)
  4309. lockdep_print_held_locks(p);
  4310. if (!unlock)
  4311. if (read_trylock(&tasklist_lock))
  4312. unlock = 1;
  4313. } while_each_thread(g, p);
  4314. pr_warn("\n");
  4315. pr_warn("=============================================\n\n");
  4316. if (unlock)
  4317. read_unlock(&tasklist_lock);
  4318. }
  4319. EXPORT_SYMBOL_GPL(debug_show_all_locks);
  4320. #endif
  4321. /*
  4322. * Careful: only use this function if you are sure that
  4323. * the task cannot run in parallel!
  4324. */
  4325. void debug_show_held_locks(struct task_struct *task)
  4326. {
  4327. if (unlikely(!debug_locks) && !lock_mon_enabled()) {
  4328. printk("INFO: lockdep is turned off.\n");
  4329. return;
  4330. }
  4331. lockdep_print_held_locks(task);
  4332. }
  4333. EXPORT_SYMBOL_GPL(debug_show_held_locks);
  4334. asmlinkage __visible void lockdep_sys_exit(void)
  4335. {
  4336. struct task_struct *curr = current;
  4337. if (unlikely(curr->lockdep_depth)) {
  4338. if (!debug_locks_off())
  4339. return;
  4340. pr_warn("\n");
  4341. pr_warn("================================================\n");
  4342. pr_warn("WARNING: lock held when returning to user space!\n");
  4343. print_kernel_ident();
  4344. pr_warn("------------------------------------------------\n");
  4345. pr_warn("%s/%d is leaving the kernel with locks still held!\n",
  4346. curr->comm, curr->pid);
  4347. lockdep_print_held_locks(curr);
  4348. lockdep_aee();
  4349. }
  4350. /*
  4351. * The lock history for each syscall should be independent. So wipe the
  4352. * slate clean on return to userspace.
  4353. */
  4354. lockdep_invariant_state(false);
  4355. }
  4356. void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
  4357. {
  4358. struct task_struct *curr = current;
  4359. if (!debug_locks_off())
  4360. return;
  4361. /* Note: the following can be executed concurrently, so be careful. */
  4362. pr_warn("\n");
  4363. pr_warn("=============================\n");
  4364. pr_warn("WARNING: suspicious RCU usage\n");
  4365. print_kernel_ident();
  4366. pr_warn("-----------------------------\n");
  4367. pr_warn("%s:%d %s!\n", file, line, s);
  4368. pr_warn("\nother info that might help us debug this:\n\n");
  4369. pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
  4370. !rcu_lockdep_current_cpu_online()
  4371. ? "RCU used illegally from offline CPU!\n"
  4372. : !rcu_is_watching()
  4373. ? "RCU used illegally from idle CPU!\n"
  4374. : "",
  4375. rcu_scheduler_active, debug_locks);
  4376. pr_info("cpu_id = %d, cpu_is_offline = %ld\n",
  4377. raw_smp_processor_id(), cpu_is_offline(raw_smp_processor_id()));
  4378. /*
  4379. * If a CPU is in the RCU-free window in idle (ie: in the section
  4380. * between rcu_idle_enter() and rcu_idle_exit(), then RCU
  4381. * considers that CPU to be in an "extended quiescent state",
  4382. * which means that RCU will be completely ignoring that CPU.
  4383. * Therefore, rcu_read_lock() and friends have absolutely no
  4384. * effect on a CPU running in that state. In other words, even if
  4385. * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
  4386. * delete data structures out from under it. RCU really has no
  4387. * choice here: we need to keep an RCU-free window in idle where
  4388. * the CPU may possibly enter into low power mode. This way we can
  4389. * notice an extended quiescent state to other CPUs that started a grace
  4390. * period. Otherwise we would delay any grace period as long as we run
  4391. * in the idle task.
  4392. *
  4393. * So complain bitterly if someone does call rcu_read_lock(),
  4394. * rcu_read_lock_bh() and so on from extended quiescent states.
  4395. */
  4396. if (!rcu_is_watching())
  4397. pr_warn("RCU used illegally from extended quiescent state!\n");
  4398. lockdep_print_held_locks(curr);
  4399. pr_warn("\nstack backtrace:\n");
  4400. dump_stack();
  4401. lockdep_aee();
  4402. }
  4403. EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
  4404. #ifdef CONFIG_LOCKDEP_CROSSRELEASE
  4405. /*
  4406. * Crossrelease works by recording a lock history for each thread and
  4407. * connecting those historic locks that were taken after the
  4408. * wait_for_completion() in the complete() context.
  4409. *
  4410. * Task-A Task-B
  4411. *
  4412. * mutex_lock(&A);
  4413. * mutex_unlock(&A);
  4414. *
  4415. * wait_for_completion(&C);
  4416. * lock_acquire_crosslock();
  4417. * atomic_inc_return(&cross_gen_id);
  4418. * |
  4419. * | mutex_lock(&B);
  4420. * | mutex_unlock(&B);
  4421. * |
  4422. * | complete(&C);
  4423. * `-- lock_commit_crosslock();
  4424. *
  4425. * Which will then add a dependency between B and C.
  4426. */
  4427. #define xhlock(i) (current->xhlocks[(i) % MAX_XHLOCKS_NR])
  4428. /*
  4429. * Whenever a crosslock is held, cross_gen_id will be increased.
  4430. */
  4431. static atomic_t cross_gen_id; /* Can be wrapped */
  4432. /*
  4433. * Make an entry of the ring buffer invalid.
  4434. */
  4435. static inline void invalidate_xhlock(struct hist_lock *xhlock)
  4436. {
  4437. /*
  4438. * Normally, xhlock->hlock.instance must be !NULL.
  4439. */
  4440. xhlock->hlock.instance = NULL;
  4441. }
  4442. /*
  4443. * Lock history stacks; we have 2 nested lock history stacks:
  4444. *
  4445. * HARD(IRQ)
  4446. * SOFT(IRQ)
  4447. *
  4448. * The thing is that once we complete a HARD/SOFT IRQ the future task locks
  4449. * should not depend on any of the locks observed while running the IRQ. So
  4450. * what we do is rewind the history buffer and erase all our knowledge of that
  4451. * temporal event.
  4452. */
  4453. void crossrelease_hist_start(enum xhlock_context_t c)
  4454. {
  4455. struct task_struct *cur = current;
  4456. if (!cur->xhlocks)
  4457. return;
  4458. cur->xhlock_idx_hist[c] = cur->xhlock_idx;
  4459. cur->hist_id_save[c] = cur->hist_id;
  4460. }
  4461. void crossrelease_hist_end(enum xhlock_context_t c)
  4462. {
  4463. struct task_struct *cur = current;
  4464. if (cur->xhlocks) {
  4465. unsigned int idx = cur->xhlock_idx_hist[c];
  4466. struct hist_lock *h = &xhlock(idx);
  4467. cur->xhlock_idx = idx;
  4468. /* Check if the ring was overwritten. */
  4469. if (h->hist_id != cur->hist_id_save[c])
  4470. invalidate_xhlock(h);
  4471. }
  4472. }
  4473. /*
  4474. * lockdep_invariant_state() is used to annotate independence inside a task, to
  4475. * make one task look like multiple independent 'tasks'.
  4476. *
  4477. * Take for instance workqueues; each work is independent of the last. The
  4478. * completion of a future work does not depend on the completion of a past work
  4479. * (in general). Therefore we must not carry that (lock) dependency across
  4480. * works.
  4481. *
  4482. * This is true for many things; pretty much all kthreads fall into this
  4483. * pattern, where they have an invariant state and future completions do not
  4484. * depend on past completions. Its just that since they all have the 'same'
  4485. * form -- the kthread does the same over and over -- it doesn't typically
  4486. * matter.
  4487. *
  4488. * The same is true for system-calls, once a system call is completed (we've
  4489. * returned to userspace) the next system call does not depend on the lock
  4490. * history of the previous system call.
  4491. *
  4492. * They key property for independence, this invariant state, is that it must be
  4493. * a point where we hold no locks and have no history. Because if we were to
  4494. * hold locks, the restore at _end() would not necessarily recover it's history
  4495. * entry. Similarly, independence per-definition means it does not depend on
  4496. * prior state.
  4497. */
  4498. void lockdep_invariant_state(bool force)
  4499. {
  4500. /*
  4501. * We call this at an invariant point, no current state, no history.
  4502. * Verify the former, enforce the latter.
  4503. */
  4504. WARN_ON_ONCE(!force && current->lockdep_depth);
  4505. if (current->xhlocks)
  4506. invalidate_xhlock(&xhlock(current->xhlock_idx));
  4507. }
  4508. static int cross_lock(struct lockdep_map *lock)
  4509. {
  4510. return lock ? lock->cross : 0;
  4511. }
  4512. /*
  4513. * This is needed to decide the relationship between wrapable variables.
  4514. */
  4515. static inline int before(unsigned int a, unsigned int b)
  4516. {
  4517. return (int)(a - b) < 0;
  4518. }
  4519. static inline struct lock_class *xhlock_class(struct hist_lock *xhlock)
  4520. {
  4521. return hlock_class(&xhlock->hlock);
  4522. }
  4523. static inline struct lock_class *xlock_class(struct cross_lock *xlock)
  4524. {
  4525. return hlock_class(&xlock->hlock);
  4526. }
  4527. /*
  4528. * Should we check a dependency with previous one?
  4529. */
  4530. static inline int depend_before(struct held_lock *hlock)
  4531. {
  4532. return hlock->read != 2 && hlock->check && !hlock->trylock;
  4533. }
  4534. /*
  4535. * Should we check a dependency with next one?
  4536. */
  4537. static inline int depend_after(struct held_lock *hlock)
  4538. {
  4539. return hlock->read != 2 && hlock->check;
  4540. }
  4541. /*
  4542. * Check if the xhlock is valid, which would be false if,
  4543. *
  4544. * 1. Has not used after initializaion yet.
  4545. * 2. Got invalidated.
  4546. *
  4547. * Remind hist_lock is implemented as a ring buffer.
  4548. */
  4549. static inline int xhlock_valid(struct hist_lock *xhlock)
  4550. {
  4551. /*
  4552. * xhlock->hlock.instance must be !NULL.
  4553. */
  4554. return !!xhlock->hlock.instance;
  4555. }
  4556. /*
  4557. * Record a hist_lock entry.
  4558. *
  4559. * Irq disable is only required.
  4560. */
  4561. static void add_xhlock(struct held_lock *hlock)
  4562. {
  4563. unsigned int idx = ++current->xhlock_idx;
  4564. struct hist_lock *xhlock = &xhlock(idx);
  4565. #ifdef CONFIG_DEBUG_LOCKDEP
  4566. /*
  4567. * This can be done locklessly because they are all task-local
  4568. * state, we must however ensure IRQs are disabled.
  4569. */
  4570. WARN_ON_ONCE(!irqs_disabled());
  4571. #endif
  4572. /* Initialize hist_lock's members */
  4573. xhlock->hlock = *hlock;
  4574. xhlock->hist_id = ++current->hist_id;
  4575. xhlock->trace.nr_entries = 0;
  4576. xhlock->trace.max_entries = MAX_XHLOCK_TRACE_ENTRIES;
  4577. xhlock->trace.entries = xhlock->trace_entries;
  4578. xhlock->trace.skip = 3;
  4579. save_stack_trace(&xhlock->trace);
  4580. }
  4581. static inline int same_context_xhlock(struct hist_lock *xhlock)
  4582. {
  4583. return xhlock->hlock.irq_context == task_irq_context(current);
  4584. }
  4585. /*
  4586. * This should be lockless as far as possible because this would be
  4587. * called very frequently.
  4588. */
  4589. static void check_add_xhlock(struct held_lock *hlock)
  4590. {
  4591. /*
  4592. * Record a hist_lock, only in case that acquisitions ahead
  4593. * could depend on the held_lock. For example, if the held_lock
  4594. * is trylock then acquisitions ahead never depends on that.
  4595. * In that case, we don't need to record it. Just return.
  4596. */
  4597. if (!current->xhlocks || !depend_before(hlock))
  4598. return;
  4599. add_xhlock(hlock);
  4600. }
  4601. /*
  4602. * For crosslock.
  4603. */
  4604. static int add_xlock(struct held_lock *hlock)
  4605. {
  4606. struct cross_lock *xlock;
  4607. unsigned int gen_id;
  4608. if (!graph_lock())
  4609. return 0;
  4610. xlock = &((struct lockdep_map_cross *)hlock->instance)->xlock;
  4611. /*
  4612. * When acquisitions for a crosslock are overlapped, we use
  4613. * nr_acquire to perform commit for them, based on cross_gen_id
  4614. * of the first acquisition, which allows to add additional
  4615. * dependencies.
  4616. *
  4617. * Moreover, when no acquisition of a crosslock is in progress,
  4618. * we should not perform commit because the lock might not exist
  4619. * any more, which might cause incorrect memory access. So we
  4620. * have to track the number of acquisitions of a crosslock.
  4621. *
  4622. * depend_after() is necessary to initialize only the first
  4623. * valid xlock so that the xlock can be used on its commit.
  4624. */
  4625. if (xlock->nr_acquire++ && depend_after(&xlock->hlock))
  4626. goto unlock;
  4627. gen_id = (unsigned int)atomic_inc_return(&cross_gen_id);
  4628. xlock->hlock = *hlock;
  4629. xlock->hlock.gen_id = gen_id;
  4630. unlock:
  4631. graph_unlock();
  4632. return 1;
  4633. }
  4634. /*
  4635. * Called for both normal and crosslock acquires. Normal locks will be
  4636. * pushed on the hist_lock queue. Cross locks will record state and
  4637. * stop regular lock_acquire() to avoid being placed on the held_lock
  4638. * stack.
  4639. *
  4640. * Return: 0 - failure;
  4641. * 1 - crosslock, done;
  4642. * 2 - normal lock, continue to held_lock[] ops.
  4643. */
  4644. static int lock_acquire_crosslock(struct held_lock *hlock)
  4645. {
  4646. /*
  4647. * CONTEXT 1 CONTEXT 2
  4648. * --------- ---------
  4649. * lock A (cross)
  4650. * X = atomic_inc_return(&cross_gen_id)
  4651. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4652. * Y = atomic_read_acquire(&cross_gen_id)
  4653. * lock B
  4654. *
  4655. * atomic_read_acquire() is for ordering between A and B,
  4656. * IOW, A happens before B, when CONTEXT 2 see Y >= X.
  4657. *
  4658. * Pairs with atomic_inc_return() in add_xlock().
  4659. */
  4660. hlock->gen_id = (unsigned int)atomic_read_acquire(&cross_gen_id);
  4661. if (cross_lock(hlock->instance))
  4662. return add_xlock(hlock);
  4663. check_add_xhlock(hlock);
  4664. return 2;
  4665. }
  4666. static int copy_trace(struct stack_trace *trace)
  4667. {
  4668. unsigned long *buf = stack_trace + nr_stack_trace_entries;
  4669. unsigned int max_nr = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
  4670. unsigned int nr = min(max_nr, trace->nr_entries);
  4671. trace->nr_entries = nr;
  4672. memcpy(buf, trace->entries, nr * sizeof(trace->entries[0]));
  4673. trace->entries = buf;
  4674. nr_stack_trace_entries += nr;
  4675. if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
  4676. if (!debug_locks_off_graph_unlock())
  4677. return 0;
  4678. print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
  4679. dump_stack();
  4680. return 0;
  4681. }
  4682. return 1;
  4683. }
  4684. static int commit_xhlock(struct cross_lock *xlock, struct hist_lock *xhlock)
  4685. {
  4686. unsigned int xid, pid;
  4687. u64 chain_key;
  4688. xid = xlock_class(xlock) - lock_classes;
  4689. chain_key = iterate_chain_key((u64)0, xid);
  4690. pid = xhlock_class(xhlock) - lock_classes;
  4691. chain_key = iterate_chain_key(chain_key, pid);
  4692. if (lookup_chain_cache(chain_key))
  4693. return 1;
  4694. if (!add_chain_cache_classes(xid, pid, xhlock->hlock.irq_context,
  4695. chain_key))
  4696. return 0;
  4697. if (!check_prev_add(current, &xlock->hlock, &xhlock->hlock, 1,
  4698. &xhlock->trace, copy_trace))
  4699. return 0;
  4700. return 1;
  4701. }
  4702. static void commit_xhlocks(struct cross_lock *xlock)
  4703. {
  4704. unsigned int cur = current->xhlock_idx;
  4705. unsigned int prev_hist_id = xhlock(cur).hist_id;
  4706. unsigned int i;
  4707. if (!graph_lock())
  4708. return;
  4709. if (xlock->nr_acquire) {
  4710. for (i = 0; i < MAX_XHLOCKS_NR; i++) {
  4711. struct hist_lock *xhlock = &xhlock(cur - i);
  4712. if (!xhlock_valid(xhlock))
  4713. break;
  4714. if (before(xhlock->hlock.gen_id, xlock->hlock.gen_id))
  4715. break;
  4716. if (!same_context_xhlock(xhlock))
  4717. break;
  4718. /*
  4719. * Filter out the cases where the ring buffer was
  4720. * overwritten and the current entry has a bigger
  4721. * hist_id than the previous one, which is impossible
  4722. * otherwise:
  4723. */
  4724. if (unlikely(before(prev_hist_id, xhlock->hist_id)))
  4725. break;
  4726. prev_hist_id = xhlock->hist_id;
  4727. /*
  4728. * commit_xhlock() returns 0 with graph_lock already
  4729. * released if fail.
  4730. */
  4731. if (!commit_xhlock(xlock, xhlock))
  4732. return;
  4733. }
  4734. }
  4735. graph_unlock();
  4736. }
  4737. void lock_commit_crosslock(struct lockdep_map *lock)
  4738. {
  4739. struct cross_lock *xlock;
  4740. unsigned long flags;
  4741. if (unlikely(!debug_locks || current->lockdep_recursion))
  4742. return;
  4743. if (!current->xhlocks)
  4744. return;
  4745. /*
  4746. * Do commit hist_locks with the cross_lock, only in case that
  4747. * the cross_lock could depend on acquisitions after that.
  4748. *
  4749. * For example, if the cross_lock does not have the 'check' flag
  4750. * then we don't need to check dependencies and commit for that.
  4751. * Just skip it. In that case, of course, the cross_lock does
  4752. * not depend on acquisitions ahead, either.
  4753. *
  4754. * WARNING: Don't do that in add_xlock() in advance. When an
  4755. * acquisition context is different from the commit context,
  4756. * invalid(skipped) cross_lock might be accessed.
  4757. */
  4758. if (!depend_after(&((struct lockdep_map_cross *)lock)->xlock.hlock))
  4759. return;
  4760. raw_local_irq_save(flags);
  4761. check_flags(flags);
  4762. current->lockdep_recursion = 1;
  4763. xlock = &((struct lockdep_map_cross *)lock)->xlock;
  4764. commit_xhlocks(xlock);
  4765. current->lockdep_recursion = 0;
  4766. raw_local_irq_restore(flags);
  4767. }
  4768. EXPORT_SYMBOL_GPL(lock_commit_crosslock);
  4769. /*
  4770. * Return: 0 - failure;
  4771. * 1 - crosslock, done;
  4772. * 2 - normal lock, continue to held_lock[] ops.
  4773. */
  4774. static int lock_release_crosslock(struct lockdep_map *lock)
  4775. {
  4776. if (cross_lock(lock)) {
  4777. if (!graph_lock())
  4778. return 0;
  4779. ((struct lockdep_map_cross *)lock)->xlock.nr_acquire--;
  4780. graph_unlock();
  4781. return 1;
  4782. }
  4783. return 2;
  4784. }
  4785. static void cross_init(struct lockdep_map *lock, int cross)
  4786. {
  4787. if (cross)
  4788. ((struct lockdep_map_cross *)lock)->xlock.nr_acquire = 0;
  4789. lock->cross = cross;
  4790. /*
  4791. * Crossrelease assumes that the ring buffer size of xhlocks
  4792. * is aligned with power of 2. So force it on build.
  4793. */
  4794. BUILD_BUG_ON(MAX_XHLOCKS_NR & (MAX_XHLOCKS_NR - 1));
  4795. }
  4796. void lockdep_init_task(struct task_struct *task)
  4797. {
  4798. int i;
  4799. task->xhlock_idx = UINT_MAX;
  4800. task->hist_id = 0;
  4801. for (i = 0; i < XHLOCK_CTX_NR; i++) {
  4802. task->xhlock_idx_hist[i] = UINT_MAX;
  4803. task->hist_id_save[i] = 0;
  4804. }
  4805. task->xhlocks = kzalloc(sizeof(struct hist_lock) * MAX_XHLOCKS_NR,
  4806. GFP_KERNEL);
  4807. }
  4808. void lockdep_free_task(struct task_struct *task)
  4809. {
  4810. if (task->xhlocks) {
  4811. void *tmp = task->xhlocks;
  4812. /* Diable crossrelease for current */
  4813. task->xhlocks = NULL;
  4814. kfree(tmp);
  4815. }
  4816. }
  4817. #endif
  4818. #if defined(CONFIG_PROVE_LOCKING) || defined(MTK_LOCK_MONITOR)
  4819. static void get_lock_name(struct lock_class *class, char name[MAX_LOCK_NAME])
  4820. {
  4821. char str[KSYM_NAME_LEN];
  4822. const char *lock_name;
  4823. char name_version[8] = { 0 };
  4824. char subclass[8] = { 0 };
  4825. lock_name = class->name;
  4826. if (!lock_name) {
  4827. lock_name = __get_key_name(class->key, str);
  4828. snprintf(name, MAX_LOCK_NAME, "%s", lock_name);
  4829. } else {
  4830. if (class->name_version > 1)
  4831. snprintf(name_version, 8, "#%d", class->name_version);
  4832. if (class->subclass)
  4833. snprintf(subclass, 8, "/%d", class->subclass);
  4834. snprintf(name, MAX_LOCK_NAME, "%s%s%s",
  4835. lock_name, name_version, subclass);
  4836. }
  4837. }
  4838. #endif
  4839. #ifdef CONFIG_PROVE_LOCKING
  4840. /*
  4841. * Duplicate debug functions and print log to trace.
  4842. * This prevents deadlock from some special locks.
  4843. */
  4844. static void get_lock_name_usage(struct lock_class *class, char *lk_name)
  4845. {
  4846. char buf[MAX_LOCK_NAME];
  4847. char usage[LOCK_USAGE_CHARS];
  4848. get_lock_name(class, buf);
  4849. get_usage_chars(class, usage);
  4850. snprintf(lk_name, MAX_LOCK_NAME, "(%s){%s}", buf, usage);
  4851. }
  4852. static void trace_lock(struct held_lock *hlock)
  4853. {
  4854. char name[MAX_LOCK_NAME];
  4855. /*
  4856. * We can be called locklessly through debug_show_all_locks() so be
  4857. * extra careful, the hlock might have been released and cleared.
  4858. */
  4859. unsigned int class_idx = hlock->class_idx;
  4860. /* Don't re-read hlock->class_idx,
  4861. * can't use READ_ONCE() on bitfields:
  4862. */
  4863. barrier();
  4864. if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
  4865. lock_dbg("<RELEASED>");
  4866. return;
  4867. }
  4868. get_lock_name_usage(lock_classes + class_idx - 1, name);
  4869. lock_dbg("%s, at: [<%p>] %pS from [%lld.%06lu]",
  4870. name, (void *)hlock->acquire_ip, (void *)hlock->acquire_ip,
  4871. sec_high(hlock->timestamp), sec_low(hlock->timestamp));
  4872. }
  4873. static void lockdep_trace_held_locks(struct task_struct *curr)
  4874. {
  4875. int i, j, depth = curr->lockdep_depth;
  4876. struct held_lock *hlock;
  4877. if (!depth) {
  4878. lock_dbg("no locks held by %s/%d.",
  4879. curr->comm, task_pid_nr(curr));
  4880. return;
  4881. }
  4882. lock_dbg("%d lock%s held by %s/%d/[%ld] on CPU#%d:",
  4883. depth, depth > 1 ? "s" : "", curr->comm,
  4884. curr->pid, curr->state, task_cpu(curr));
  4885. hlock = curr->held_locks;
  4886. for (i = 0; i < depth; i++) {
  4887. char buf[256];
  4888. struct stack_trace *trace = &(hlock + i)->trace;
  4889. lock_dbg(" #%d: ", i);
  4890. trace_lock(hlock + i);
  4891. /* show backtrace */
  4892. if (!trace || !trace->entries)
  4893. continue;
  4894. for (j = 0; j < trace->nr_entries; j++) {
  4895. snprintf(buf, sizeof(buf), "%*c%pS", 6, ' ',
  4896. (void *)trace->entries[j]);
  4897. lock_dbg("%s", buf);
  4898. }
  4899. }
  4900. }
  4901. static void dump_stack_to_trace(struct stack_trace *trace)
  4902. {
  4903. int i;
  4904. if (!trace || !trace->entries)
  4905. return;
  4906. for (i = 0; i < trace->nr_entries; i++)
  4907. lock_dbg("%*c%pS", 7, ' ', (void *)trace->entries[i]);
  4908. }
  4909. static void dump_curr_stack(void)
  4910. {
  4911. struct stack_trace trace;
  4912. unsigned long entries[32];
  4913. trace.nr_entries = 0;
  4914. trace.max_entries = ARRAY_SIZE(entries);
  4915. trace.entries = entries;
  4916. trace.skip = 0;
  4917. save_stack_trace_tsk(current, &trace);
  4918. dump_stack_to_trace(&trace);
  4919. }
  4920. static noinline int
  4921. trace_circular_bug_entry(struct lock_list *target, int depth)
  4922. {
  4923. char name[MAX_LOCK_NAME];
  4924. get_lock_name_usage(target->class, name);
  4925. lock_dbg("-> #%u: %s", depth, name);
  4926. dump_stack_to_trace(&target->trace);
  4927. return 0;
  4928. }
  4929. static noinline int
  4930. trace_circular_bug_header(struct lock_list *entry, unsigned int depth,
  4931. struct held_lock *check_src,
  4932. struct held_lock *check_tgt)
  4933. {
  4934. char name[MAX_LOCK_NAME];
  4935. struct task_struct *curr = current;
  4936. if (debug_locks_silent)
  4937. return 0;
  4938. lock_dbg("======================================================");
  4939. lock_dbg("WARNING: possible circular locking dependency detected");
  4940. lock_dbg("%s %.*s %s", init_utsname()->release,
  4941. (int)strcspn(init_utsname()->version, " "),
  4942. init_utsname()->version,
  4943. print_tainted());
  4944. lock_dbg("------------------------------------------------------");
  4945. lock_dbg("%s/%d is trying to acquire lock:",
  4946. curr->comm, task_pid_nr(curr));
  4947. trace_lock(check_src);
  4948. if (cross_lock(check_tgt->instance))
  4949. lock_dbg("but now in release context of a crosslock acquired at the following:");
  4950. else
  4951. lock_dbg("but task is already holding lock:");
  4952. trace_lock(check_tgt);
  4953. lock_dbg("which lock already depends on the new lock.");
  4954. lock_dbg("the existing dependency chain (in reverse order) is:");
  4955. get_lock_name_usage(entry->class, name);
  4956. lock_dbg("-> #%u: %s", depth, name);
  4957. dump_stack_to_trace(&entry->trace);
  4958. lockdep_aee();
  4959. return 0;
  4960. }
  4961. static noinline int
  4962. trace_circular_bug(struct lock_list *this,
  4963. struct lock_list *target,
  4964. struct held_lock *check_src,
  4965. struct held_lock *check_tgt,
  4966. struct stack_trace *trace)
  4967. {
  4968. struct task_struct *curr = current;
  4969. struct lock_list *parent;
  4970. struct lock_list *first_parent;
  4971. int depth;
  4972. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  4973. return 0;
  4974. if (cross_lock(check_tgt->instance))
  4975. this->trace = *trace;
  4976. else if (!save_trace(&this->trace))
  4977. return 0;
  4978. depth = get_lock_depth(target);
  4979. trace_circular_bug_header(target, depth, check_src, check_tgt);
  4980. parent = get_lock_parent(target);
  4981. first_parent = parent;
  4982. while (parent) {
  4983. trace_circular_bug_entry(parent, --depth);
  4984. parent = get_lock_parent(parent);
  4985. }
  4986. lock_dbg("other info that might help us debug this:");
  4987. lockdep_trace_held_locks(curr);
  4988. lock_dbg("current backtrace:");
  4989. dump_curr_stack();
  4990. return 0;
  4991. }
  4992. #endif /* CONFIG_PROVE_LOCKING */
  4993. #ifdef MTK_LOCK_MONITOR
  4994. static inline struct lock_class *
  4995. __look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  4996. {
  4997. struct lockdep_subclass_key *key;
  4998. struct hlist_head *hash_head;
  4999. struct lock_class *class;
  5000. bool is_static = false;
  5001. if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES))
  5002. return NULL;
  5003. /*
  5004. * Static locks do not have their class-keys yet - for them the key
  5005. * is the lock object itself. If the lock is in the per cpu area,
  5006. * the canonical address of the lock (per cpu offset removed) is
  5007. * used.
  5008. */
  5009. if (unlikely(!lock->key)) {
  5010. unsigned long can_addr, addr = (unsigned long)lock;
  5011. if (__is_kernel_percpu_address(addr, &can_addr))
  5012. lock->key = (void *)can_addr;
  5013. else if (__is_module_percpu_address(addr, &can_addr))
  5014. lock->key = (void *)can_addr;
  5015. else if (static_obj(lock))
  5016. lock->key = (void *)lock;
  5017. else
  5018. return ERR_PTR(-EINVAL);
  5019. is_static = true;
  5020. }
  5021. /*
  5022. * NOTE: the class-key must be unique. For dynamic locks, a static
  5023. * lock_class_key variable is passed in through the mutex_init()
  5024. * (or spin_lock_init()) call - which acts as the key. For static
  5025. * locks we use the lock object itself as the key.
  5026. */
  5027. BUILD_BUG_ON(sizeof(struct lock_class_key) >
  5028. sizeof(struct lockdep_map));
  5029. key = lock->key->subkeys + subclass;
  5030. hash_head = classhashentry(key);
  5031. /*
  5032. * We do an RCU walk of the hash, see lockdep_free_key_range().
  5033. */
  5034. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  5035. return NULL;
  5036. hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
  5037. if (class->key == key) {
  5038. /* Don't check class->name and lock->name. */
  5039. return class;
  5040. }
  5041. }
  5042. return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL);
  5043. }
  5044. static struct lock_class *
  5045. __register_lock_class(struct lockdep_map *lock, unsigned int subclass,
  5046. int force)
  5047. {
  5048. struct lockdep_subclass_key *key;
  5049. struct hlist_head *hash_head;
  5050. struct lock_class *class;
  5051. DEBUG_LOCKS_WARN_ON(!irqs_disabled());
  5052. class = __look_up_lock_class(lock, subclass);
  5053. if (likely(!IS_ERR_OR_NULL(class)))
  5054. goto out_set_class_cache;
  5055. /*
  5056. * Debug-check: all keys must be persistent!
  5057. */
  5058. if (IS_ERR(class))
  5059. return NULL;
  5060. key = lock->key->subkeys + subclass;
  5061. hash_head = classhashentry(key);
  5062. arch_spin_lock(&lockdep_lock);
  5063. current->lockdep_recursion++;
  5064. /*
  5065. * We have to do the hash-walk again, to avoid races
  5066. * with another CPU:
  5067. */
  5068. hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
  5069. if (class->key == key)
  5070. goto out_unlock_set;
  5071. }
  5072. /*
  5073. * Allocate a new key from the static array, and add it to
  5074. * the hash:
  5075. */
  5076. if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
  5077. arch_spin_unlock(&lockdep_lock);
  5078. return NULL;
  5079. }
  5080. class = lock_classes + nr_lock_classes++;
  5081. debug_atomic_inc(nr_unused_locks);
  5082. class->key = key;
  5083. class->name = lock->name;
  5084. class->subclass = subclass;
  5085. INIT_LIST_HEAD(&class->lock_entry);
  5086. INIT_LIST_HEAD(&class->locks_before);
  5087. INIT_LIST_HEAD(&class->locks_after);
  5088. class->name_version = count_matching_names(class);
  5089. /*
  5090. * We use RCU's safe list-add method to make
  5091. * parallel walking of the hash-list safe:
  5092. */
  5093. hlist_add_head_rcu(&class->hash_entry, hash_head);
  5094. /*
  5095. * Add it to the global list of classes:
  5096. */
  5097. list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
  5098. out_unlock_set:
  5099. current->lockdep_recursion--;
  5100. arch_spin_unlock(&lockdep_lock);
  5101. out_set_class_cache:
  5102. if (!subclass || force)
  5103. lock->class_cache[0] = class;
  5104. else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  5105. lock->class_cache[subclass] = class;
  5106. /*
  5107. * Hash collision, did we smoke some? We found a class with a matching
  5108. * hash but the subclass -- which is hashed in -- didn't match.
  5109. */
  5110. if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
  5111. return NULL;
  5112. return class;
  5113. }
  5114. static void __add_held_lock(struct lockdep_map *lock, unsigned int subclass,
  5115. int trylock, int read, int check, int hardirqs_off,
  5116. struct lockdep_map *nest_lock, unsigned long ip,
  5117. int references, int pin_count)
  5118. {
  5119. struct task_struct *curr = current;
  5120. struct lock_class *class = NULL;
  5121. struct held_lock *hlock;
  5122. unsigned int depth;
  5123. int class_idx;
  5124. if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  5125. class = lock->class_cache[subclass];
  5126. if (unlikely(!class)) {
  5127. class = __register_lock_class(lock, subclass, 0);
  5128. if (!class)
  5129. return;
  5130. }
  5131. class_idx = class - lock_classes + 1;
  5132. depth = curr->lockdep_depth;
  5133. if (depth >= MAX_LOCK_DEPTH)
  5134. return;
  5135. hlock = curr->held_locks + depth;
  5136. hlock->class_idx = class_idx;
  5137. hlock->acquire_ip = ip;
  5138. hlock->instance = lock;
  5139. hlock->nest_lock = nest_lock;
  5140. hlock->irq_context = task_irq_context(curr);
  5141. hlock->trylock = trylock;
  5142. hlock->read = read;
  5143. hlock->check = check;
  5144. hlock->hardirqs_off = !!hardirqs_off;
  5145. hlock->references = references;
  5146. #ifdef CONFIG_LOCK_STAT
  5147. hlock->waittime_stamp = 0;
  5148. hlock->holdtime_stamp = lockstat_clock();
  5149. #endif
  5150. hlock->pin_count = pin_count;
  5151. /* MTK_LOCK_DEBUG_HELD_LOCK */
  5152. held_lock_save_trace(&hlock->trace, hlock->entries);
  5153. /* MTK_LOCK_MONITOR */
  5154. hlock->timestamp = sched_clock();
  5155. curr->lockdep_depth++;
  5156. }
  5157. static void
  5158. __remove_held_lock(struct lockdep_map *lock, int nested, unsigned long ip)
  5159. {
  5160. struct task_struct *curr = current;
  5161. struct held_lock *hlock;
  5162. unsigned int depth;
  5163. int i;
  5164. depth = curr->lockdep_depth;
  5165. if (depth <= 0)
  5166. return;
  5167. hlock = find_held_lock(curr, lock, depth, &i);
  5168. if (!hlock)
  5169. return;
  5170. /* MTK_LOCK_MONITOR */
  5171. hlock->timestamp = 0;
  5172. curr->lockdep_depth = i;
  5173. }
  5174. #ifndef GO_UNWIND_FRAME
  5175. static void dump_task_stack(struct task_struct *tsk, int output)
  5176. {
  5177. struct stack_trace trace;
  5178. unsigned long entries[32];
  5179. char buf[256];
  5180. int i;
  5181. if (!mutex_trylock(&tsk->signal->cred_guard_mutex))
  5182. return;
  5183. trace.nr_entries = 0;
  5184. trace.max_entries = ARRAY_SIZE(entries);
  5185. trace.entries = entries;
  5186. trace.skip = 1;
  5187. save_stack_trace_tsk(tsk, &trace);
  5188. if (trace.nr_entries != 0 &&
  5189. trace.entries[trace.nr_entries - 1] == ULONG_MAX)
  5190. trace.nr_entries--;
  5191. if (trace.nr_entries < 3) {
  5192. mutex_unlock(&tsk->signal->cred_guard_mutex);
  5193. return;
  5194. }
  5195. snprintf(buf, sizeof(buf), " stack of %s/%d:%s",
  5196. tsk->comm, tsk->pid, (output == TO_SRAM) ? "\n" : "");
  5197. lock_mon_msg(buf, output);
  5198. for (i = 0; i < trace.nr_entries; i++) {
  5199. snprintf(buf, sizeof(buf), "%*c%pS%s", 6, ' ',
  5200. (void *)entries[i], (output == TO_SRAM) ? "\n" : "");
  5201. lock_mon_msg(buf, output);
  5202. }
  5203. mutex_unlock(&tsk->signal->cred_guard_mutex);
  5204. }
  5205. #else
  5206. #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
  5207. static void dump_task_stack(struct task_struct *tsk, int output)
  5208. {
  5209. struct stackframe frame;
  5210. char buf[256];
  5211. int i = 0;
  5212. if (!tsk || !try_get_task_stack(tsk))
  5213. return;
  5214. #ifdef CONFIG_ARM64
  5215. if (tsk == current) {
  5216. frame.fp = (unsigned long)__builtin_frame_address(0);
  5217. frame.pc = (unsigned long)dump_task_stack;
  5218. } else {
  5219. frame.fp = thread_saved_fp(tsk);
  5220. frame.pc = thread_saved_pc(tsk);
  5221. }
  5222. #else
  5223. if (tsk == current) {
  5224. frame.fp = (unsigned long)__builtin_frame_address(0);
  5225. frame.sp = current_stack_pointer;
  5226. frame.lr = (unsigned long)__builtin_return_address(0);
  5227. frame.pc = (unsigned long)dump_task_stack;
  5228. } else {
  5229. frame.fp = thread_saved_fp(tsk);
  5230. frame.sp = thread_saved_sp(tsk);
  5231. frame.lr = 0;
  5232. frame.pc = thread_saved_pc(tsk);
  5233. }
  5234. #endif
  5235. snprintf(buf, sizeof(buf), " stack of %s/%d:%s",
  5236. tsk->comm, tsk->pid, add_suffix(output));
  5237. lock_mon_msg(buf, output);
  5238. while (1) {
  5239. if (i++ >= 2)
  5240. snprintf(buf, sizeof(buf), "%*c%pS%s", 6, ' ',
  5241. (void *)frame.pc, add_suffix(output));
  5242. #ifdef CONFIG_ARM64
  5243. if (unwind_frame(tsk, &frame) < 0)
  5244. break;
  5245. #else
  5246. if (unwind_frame(&frame) < 0)
  5247. break;
  5248. #endif
  5249. else if (i > 2)
  5250. lock_mon_msg(buf, output);
  5251. }
  5252. put_task_stack(tsk);
  5253. }
  5254. #else /* CONFIG_ARM || CONFIG_ARM64 */
  5255. static void dump_task_stack(struct task_struct *tsk, int output) {}
  5256. #endif
  5257. #endif
  5258. static bool lock_mon_aee = 1;
  5259. #ifdef CONFIG_PROVE_LOCKING
  5260. static bool is_critical_lock_held(void);
  5261. static void lock_monitor_aee(void)
  5262. {
  5263. char aee_str[40];
  5264. if (!is_critical_lock_held()) {
  5265. snprintf(aee_str, 40, "[%s]held locks too much", current->comm);
  5266. #ifdef CONFIG_MTK_AEE_FEATURE
  5267. aee_kernel_warning_api(__FILE__, __LINE__,
  5268. DB_OPT_DUMMY_DUMP | DB_OPT_FTRACE,
  5269. aee_str, "Lock Monitor Warning\n");
  5270. #endif
  5271. }
  5272. }
  5273. #else
  5274. static void lock_monitor_aee(void) {};
  5275. #endif
  5276. static void lockdep_check_held_locks(
  5277. struct task_struct *curr, bool en, bool aee)
  5278. {
  5279. int i, show_task_stack = 1;
  5280. struct held_lock *hlock;
  5281. unsigned long long timestamp_bak;
  5282. if (!curr->lockdep_depth)
  5283. return;
  5284. for (i = 0; i < curr->lockdep_depth; i++) {
  5285. unsigned long long t_diff;
  5286. hlock = curr->held_locks + i;
  5287. if (hlock->timestamp == 0)
  5288. continue;
  5289. timestamp_bak = hlock->timestamp;
  5290. t_diff = sched_clock() - timestamp_bak;
  5291. do_div(t_diff, 1000000);
  5292. if (hlock->timestamp == 0 || hlock->timestamp != timestamp_bak)
  5293. continue;
  5294. if (t_diff > lock_mon_1st_th_ms || aee) {
  5295. struct lock_class *class;
  5296. char name[MAX_LOCK_NAME];
  5297. unsigned int class_idx = hlock->class_idx;
  5298. bool skip_this = 0;
  5299. int j, output1, output2 = TO_FTRACE;
  5300. int list_num = ARRAY_SIZE(held_lock_white_list);
  5301. /* Don't re-read hlock->class_idx */
  5302. barrier();
  5303. if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS)
  5304. continue;
  5305. class = lock_classes + class_idx - 1;
  5306. get_lock_name(class, name);
  5307. /* check white list */
  5308. for (j = 0; j < list_num; j++) {
  5309. if (!strcmp(name, held_lock_white_list[j]))
  5310. skip_this = 1;
  5311. }
  5312. /* ignore special cases */
  5313. if (!strncmp(name, "s_active", 8))
  5314. skip_this = 1;
  5315. if (!strncmp(name, "kn->count", 9))
  5316. skip_this = 1;
  5317. /* skip this warning */
  5318. if (skip_this)
  5319. continue;
  5320. output1 = aee ? TO_SRAM : TO_BOTH;
  5321. /* locks might be released in runtime */
  5322. if (hlock->timestamp == 0 ||
  5323. hlock->timestamp != timestamp_bak)
  5324. continue;
  5325. warn_locks++;
  5326. snprintf(buf_lock, sizeof(buf_lock),
  5327. "[%p] (%s) %s by %s/%d[%c] on CPU#%d from [%lld.%06lu]%s",
  5328. hlock->instance, name,
  5329. hlock->acquired ? "held" : "needed",
  5330. curr->comm, curr->pid,
  5331. task_state_to_char(curr), task_cpu(curr),
  5332. sec_high(hlock->timestamp),
  5333. sec_low(hlock->timestamp),
  5334. add_suffix(output1));
  5335. lock_mon_msg(buf_lock, output1);
  5336. if (t_diff > lock_mon_2nd_th_ms && en)
  5337. output2 = TO_BOTH;
  5338. output2 = aee ? TO_SRAM : output2;
  5339. /* Catch the condition that too many locks are held */
  5340. if (lock_mon_aee && warn_locks >= MAX_WARN_LOCKS) {
  5341. lock_mon_aee = 0;
  5342. lock_monitor_aee();
  5343. }
  5344. /* locks might be released in runtime */
  5345. if (hlock->timestamp == 0 ||
  5346. hlock->timestamp != timestamp_bak)
  5347. continue;
  5348. held_lock_show_trace(&hlock->trace, output2);
  5349. /* locks might be released in runtime */
  5350. if (hlock->timestamp == 0 ||
  5351. hlock->timestamp != timestamp_bak)
  5352. continue;
  5353. if (show_task_stack && curr->state != TASK_RUNNING) {
  5354. dump_task_stack(curr, output2);
  5355. show_task_stack = 0;
  5356. }
  5357. }
  5358. }
  5359. }
  5360. void mt_aee_dump_held_locks(void)
  5361. {
  5362. struct task_struct *g, *p;
  5363. int count = 10;
  5364. int unlock = 1;
  5365. char buf[32];
  5366. retry:
  5367. if (!read_trylock(&tasklist_lock)) {
  5368. if (count) {
  5369. count--;
  5370. mdelay(200);
  5371. goto retry;
  5372. }
  5373. unlock = 0;
  5374. }
  5375. snprintf(buf, sizeof(buf), "===== Lock Monitor =====\n");
  5376. lock_mon_msg(buf, TO_SRAM);
  5377. do_each_thread(g, p) {
  5378. if (p->lockdep_depth)
  5379. lockdep_check_held_locks(p, 0, 1);
  5380. if (!unlock)
  5381. if (read_trylock(&tasklist_lock))
  5382. unlock = 1;
  5383. } while_each_thread(g, p);
  5384. if (unlock)
  5385. read_unlock(&tasklist_lock);
  5386. snprintf(buf, sizeof(buf), "\n");
  5387. lock_mon_msg(buf, TO_SRAM);
  5388. }
  5389. static DEFINE_SPINLOCK(lock_mon_lock);
  5390. void check_held_locks(int force)
  5391. {
  5392. struct task_struct *g, *p;
  5393. int count = 10;
  5394. int unlock = 1;
  5395. if (!spin_trylock(&lock_mon_lock))
  5396. return;
  5397. /*
  5398. * Here we try to get the tasklist_lock as hard as possible,
  5399. * if not successful after 2 seconds we ignore it (but keep
  5400. * trying). This is to enable a debug printout even if a
  5401. * tasklist_lock-holding task deadlocks or crashes.
  5402. */
  5403. retry:
  5404. if (!read_trylock(&tasklist_lock)) {
  5405. if (count) {
  5406. count--;
  5407. mdelay(200);
  5408. goto retry;
  5409. }
  5410. unlock = 0;
  5411. }
  5412. warn_locks = 0;
  5413. warn_msgs = 0;
  5414. do_each_thread(g, p) {
  5415. if (p->lockdep_depth)
  5416. lockdep_check_held_locks(p, force, 0);
  5417. if (!unlock)
  5418. if (read_trylock(&tasklist_lock))
  5419. unlock = 1;
  5420. } while_each_thread(g, p);
  5421. if (unlock)
  5422. read_unlock(&tasklist_lock);
  5423. spin_unlock(&lock_mon_lock);
  5424. }
  5425. EXPORT_SYMBOL_GPL(check_held_locks);
  5426. static void show_debug_locks_state(void)
  5427. {
  5428. static unsigned long long pre_time_sec;
  5429. unsigned long long time_sec = sec_high(sched_clock());
  5430. /* check debug_locks per 15 seconds */
  5431. if (debug_locks || time_sec - pre_time_sec < 15)
  5432. return;
  5433. pre_time_sec = time_sec;
  5434. pr_info("debug_locks is off at [%lld.%06lu]\n",
  5435. sec_high(debug_locks_off_ts),
  5436. sec_low(debug_locks_off_ts));
  5437. if (nr_lock_classes >= MAX_LOCKDEP_KEYS ||
  5438. nr_list_entries >= MAX_LOCKDEP_ENTRIES ||
  5439. nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES - 1)
  5440. pr_info("lock_classes[%lu] list_entries[%lu] stack_trace_entries[%lu]\n",
  5441. nr_lock_classes, nr_list_entries,
  5442. nr_stack_trace_entries);
  5443. #ifdef CONFIG_PROVE_LOCKING
  5444. if (nr_lock_chains >= MAX_LOCKDEP_CHAINS ||
  5445. nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)
  5446. pr_info("lock_chains[%lu] chain_hlocks[%d]\n",
  5447. nr_lock_chains, nr_chain_hlocks);
  5448. #endif
  5449. }
  5450. static int lock_monitor_work(void *data)
  5451. {
  5452. static int count;
  5453. int force;
  5454. while (lock_mon_enable) {
  5455. force = 0;
  5456. /* print backtrace or not */
  5457. if (++count >= lock_mon_period_cnt) {
  5458. count = 0;
  5459. force = 1;
  5460. }
  5461. if (!debug_locks)
  5462. show_debug_locks_state();
  5463. check_held_locks(force);
  5464. msleep(lock_mon_period_ms);
  5465. }
  5466. return 0;
  5467. }
  5468. static ssize_t
  5469. lock_mon_enable_write(struct file *filp, const char *ubuf,
  5470. size_t cnt, loff_t *data)
  5471. {
  5472. char buf[64];
  5473. int ret, lock_mon_disabled = !lock_mon_enable;
  5474. if (!lock_mon_door)
  5475. return cnt;
  5476. if (cnt >= sizeof(buf))
  5477. return -EINVAL;
  5478. if (copy_from_user(&buf, ubuf, cnt))
  5479. return -EFAULT;
  5480. buf[cnt] = 0;
  5481. ret = kstrtouint(buf, 10, &lock_mon_enable);
  5482. if (ret)
  5483. return ret;
  5484. lock_mon_enable = !!lock_mon_enable;
  5485. if (lock_mon_disabled && lock_mon_enable)
  5486. kthread_run(lock_monitor_work, NULL, "lock_monitor");
  5487. return cnt;
  5488. }
  5489. static ssize_t
  5490. lock_mon_enable_read(struct file *file, char __user *user_buf,
  5491. size_t count, loff_t *ppos)
  5492. {
  5493. char buf[32];
  5494. int len;
  5495. len = snprintf(buf, sizeof(buf), "%d\n", lock_mon_enable);
  5496. len = len > sizeof(buf) ? sizeof(buf) : len;
  5497. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  5498. }
  5499. static const struct file_operations lock_mon_enable_fops = {
  5500. .open = simple_open,
  5501. .read = lock_mon_enable_read,
  5502. .write = lock_mon_enable_write,
  5503. };
  5504. #define DECLARE_LOCK_MONITOR_MATCH(name, param) \
  5505. static ssize_t lock_mon_##name##_write(struct file *filp, \
  5506. const char *ubuf, size_t cnt, loff_t *data) \
  5507. { \
  5508. char buf[64]; \
  5509. int ret; \
  5510. \
  5511. if (!lock_mon_door) \
  5512. return cnt; \
  5513. \
  5514. if (cnt >= sizeof(buf)) \
  5515. return -EINVAL; \
  5516. \
  5517. if (copy_from_user(&buf, ubuf, cnt)) \
  5518. return -EFAULT; \
  5519. \
  5520. buf[cnt] = 0; \
  5521. \
  5522. ret = kstrtouint(buf, 10, &param); \
  5523. if (ret) \
  5524. return ret; \
  5525. \
  5526. return cnt; \
  5527. } \
  5528. \
  5529. static ssize_t lock_mon_##name##_read(struct file *file, \
  5530. char __user *user_buf, size_t count, loff_t *ppos) \
  5531. { \
  5532. char buf[32]; \
  5533. int len; \
  5534. \
  5535. len = snprintf(buf, sizeof(buf), "%d\n", param); \
  5536. len = len > sizeof(buf) ? sizeof(buf) : len; \
  5537. return simple_read_from_buffer(user_buf, count, ppos, buf, len); \
  5538. } \
  5539. \
  5540. static const struct file_operations lock_mon_##name##_fops = { \
  5541. .open = simple_open, \
  5542. .read = lock_mon_##name##_read, \
  5543. .write = lock_mon_##name##_write, \
  5544. }
  5545. DECLARE_LOCK_MONITOR_MATCH(period, lock_mon_period_ms);
  5546. DECLARE_LOCK_MONITOR_MATCH(1st_th, lock_mon_1st_th_ms);
  5547. DECLARE_LOCK_MONITOR_MATCH(2nd_th, lock_mon_2nd_th_ms);
  5548. DECLARE_LOCK_MONITOR_MATCH(3rd_th, lock_mon_3rd_th_ms);
  5549. DECLARE_LOCK_MONITOR_MATCH(period_cnt, lock_mon_period_cnt);
  5550. static ssize_t lock_monitor_door_write(struct file *filp,
  5551. const char *ubuf, size_t cnt, loff_t *data)
  5552. {
  5553. char buf[8];
  5554. if (cnt >= sizeof(buf) || cnt <= 1UL)
  5555. return cnt;
  5556. if (copy_from_user(&buf, ubuf, cnt))
  5557. return -EFAULT;
  5558. buf[cnt-1UL] = 0;
  5559. if (strncmp("open", buf, 4) == 0)
  5560. lock_mon_door = 1;
  5561. if (strncmp("close", buf, 5) == 0)
  5562. lock_mon_door = 0;
  5563. return cnt;
  5564. }
  5565. static const struct file_operations lock_mon_door_fops = {
  5566. .open = simple_open,
  5567. .write = lock_monitor_door_write,
  5568. };
  5569. /*
  5570. * Lock monitor checks the hold time of all held locks periodly.
  5571. * Type of spinlock, mutex, rw_semaphore, and RCU are supported.
  5572. * Type of semaphore and rt_mutex are not supported.
  5573. */
  5574. void lock_monitor_init(void)
  5575. {
  5576. struct proc_dir_entry *root;
  5577. struct proc_dir_entry *pe;
  5578. root = proc_mkdir("lockmon", NULL);
  5579. if (!root)
  5580. return;
  5581. pe = proc_create("door", 0220, root, &lock_mon_door_fops);
  5582. if (!pe)
  5583. return;
  5584. pe = proc_create("enable", 0664, root, &lock_mon_enable_fops);
  5585. if (!pe)
  5586. return;
  5587. pe = proc_create("period_ms", 0664, root, &lock_mon_period_fops);
  5588. if (!pe)
  5589. return;
  5590. pe = proc_create("1st_th_ms", 0664, root, &lock_mon_1st_th_fops);
  5591. if (!pe)
  5592. return;
  5593. pe = proc_create("2nd_th_ms", 0664, root, &lock_mon_2nd_th_fops);
  5594. if (!pe)
  5595. return;
  5596. pe = proc_create("3rd_th_ms", 0664, root, &lock_mon_3rd_th_fops);
  5597. if (!pe)
  5598. return;
  5599. pe = proc_create("period_cnt", 0664, root, &lock_mon_period_cnt_fops);
  5600. if (!pe)
  5601. return;
  5602. kthread_run(lock_monitor_work, NULL, "lock_monitor");
  5603. }
  5604. #else
  5605. #ifdef CONFIG_LOCKDEP
  5606. void check_held_locks(int force) {}
  5607. void mt_aee_dump_held_locks(void) {}
  5608. #endif
  5609. #endif /* MTK_LOCK_MONITOR */
  5610. #ifdef CONFIG_PROVE_LOCKING
  5611. static const char * const critical_lock_list[] = {
  5612. /* workqueue */
  5613. "&(&pool->lock)->rlock",
  5614. "works_lock",
  5615. /* kmalloc */
  5616. "&(&n->list_lock)->rlock",
  5617. "&(&zone->lock)->rlock",
  5618. /* stacktrace */
  5619. "depot_lock",
  5620. /* console */
  5621. "&(&port->lock)->rlock"
  5622. };
  5623. static bool is_critical_lock_held(void)
  5624. {
  5625. int cpu;
  5626. int i, j;
  5627. struct rq *rq;
  5628. struct held_lock *hlock;
  5629. struct lock_class *class;
  5630. char name[MAX_LOCK_NAME];
  5631. unsigned int class_idx;
  5632. /* check if current rq->lock is held by someone */
  5633. cpu = raw_smp_processor_id();
  5634. rq = cpu_rq(cpu);
  5635. if (raw_spin_is_locked(&rq->lock))
  5636. return true;
  5637. /* check locks held by current task */
  5638. if (!current->lockdep_depth)
  5639. return false;
  5640. for (i = 0; i < current->lockdep_depth; i++) {
  5641. hlock = current->held_locks + i;
  5642. class_idx = hlock->class_idx;
  5643. /* skip check if lock is released */
  5644. if (hlock->timestamp == 0)
  5645. continue;
  5646. /* Don't re-read hlock->class_idx */
  5647. barrier();
  5648. if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS)
  5649. continue;
  5650. class = lock_classes + class_idx - 1;
  5651. get_lock_name(class, name);
  5652. /* check critical lock list */
  5653. for (j = 0; j < ARRAY_SIZE(critical_lock_list); j++) {
  5654. if (!strcmp(name, critical_lock_list[j]))
  5655. return true;
  5656. }
  5657. }
  5658. return false;
  5659. }
  5660. static void lockdep_aee(void)
  5661. {
  5662. char aee_str[40];
  5663. if (!is_critical_lock_held()) {
  5664. snprintf(aee_str, 40, "[%s]LockProve Warning", current->comm);
  5665. #ifdef CONFIG_MTK_AEE_FEATURE
  5666. aee_kernel_warning_api(__FILE__, __LINE__,
  5667. DB_OPT_DUMMY_DUMP | DB_OPT_FTRACE,
  5668. aee_str, "LockProve Debug\n");
  5669. #endif
  5670. }
  5671. }
  5672. #else
  5673. static void lockdep_aee(void) {};
  5674. #endif