lockdep.c 168 KB

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