1002-cleanup-remove-rusty-meat.patch 442 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462
  1. # HG changeset patch
  2. # Parent 8464bb6dfd0210f5e1a72a938f3dd16b1aad78ca
  3. [Clean-up] Remove most of the rust meat
  4. diff --git a/Kbuild b/Kbuild
  5. --- a/Kbuild
  6. +++ b/Kbuild
  7. @@ -89,7 +89,6 @@ obj-y += security/
  8. obj-y += crypto/
  9. obj-$(CONFIG_BLOCK) += block/
  10. obj-$(CONFIG_IO_URING) += io_uring/
  11. -obj-$(CONFIG_RUST) += rust/
  12. obj-y += $(ARCH_LIB)
  13. obj-y += drivers/
  14. obj-y += sound/
  15. diff --git a/rust/.gitignore b/rust/.gitignore
  16. deleted file mode 100644
  17. --- a/rust/.gitignore
  18. +++ /dev/null
  19. @@ -1,10 +0,0 @@
  20. -# SPDX-License-Identifier: GPL-2.0
  21. -
  22. -bindings_generated.rs
  23. -bindings_helpers_generated.rs
  24. -doctests_kernel_generated.rs
  25. -doctests_kernel_generated_kunit.c
  26. -uapi_generated.rs
  27. -exports_*_generated.h
  28. -doc/
  29. -test/
  30. diff --git a/rust/Makefile b/rust/Makefile
  31. deleted file mode 100644
  32. --- a/rust/Makefile
  33. +++ /dev/null
  34. @@ -1,463 +0,0 @@
  35. -# SPDX-License-Identifier: GPL-2.0
  36. -
  37. -# Where to place rustdoc generated documentation
  38. -rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
  39. -
  40. -obj-$(CONFIG_RUST) += core.o compiler_builtins.o
  41. -always-$(CONFIG_RUST) += exports_core_generated.h
  42. -
  43. -# Missing prototypes are expected in the helpers since these are exported
  44. -# for Rust only, thus there is no header nor prototypes.
  45. -obj-$(CONFIG_RUST) += helpers.o
  46. -CFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations
  47. -
  48. -always-$(CONFIG_RUST) += libmacros.so
  49. -no-clean-files += libmacros.so
  50. -
  51. -always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
  52. -obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
  53. -always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
  54. - exports_kernel_generated.h
  55. -
  56. -always-$(CONFIG_RUST) += uapi/uapi_generated.rs
  57. -obj-$(CONFIG_RUST) += uapi.o
  58. -
  59. -ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
  60. -obj-$(CONFIG_RUST) += build_error.o
  61. -else
  62. -always-$(CONFIG_RUST) += build_error.o
  63. -endif
  64. -
  65. -obj-$(CONFIG_RUST) += exports.o
  66. -
  67. -always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
  68. -always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
  69. -
  70. -obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
  71. -obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
  72. -
  73. -# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
  74. -ifdef CONFIG_RUST
  75. -
  76. -# `$(rust_flags)` is passed in case the user added `--sysroot`.
  77. -rustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
  78. -rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
  79. -RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
  80. -
  81. -ifeq ($(quiet),silent_)
  82. -cargo_quiet=-q
  83. -rust_test_quiet=-q
  84. -rustdoc_test_quiet=--test-args -q
  85. -rustdoc_test_kernel_quiet=>/dev/null
  86. -else ifeq ($(quiet),quiet_)
  87. -rust_test_quiet=-q
  88. -rustdoc_test_quiet=--test-args -q
  89. -rustdoc_test_kernel_quiet=>/dev/null
  90. -else
  91. -cargo_quiet=--verbose
  92. -endif
  93. -
  94. -core-cfgs = \
  95. - --cfg no_fp_fmt_parse
  96. -
  97. -alloc-cfgs = \
  98. - --cfg no_global_oom_handling \
  99. - --cfg no_rc \
  100. - --cfg no_sync
  101. -
  102. -quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
  103. - cmd_rustdoc = \
  104. - OBJTREE=$(abspath $(objtree)) \
  105. - $(RUSTDOC) $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \
  106. - $(rustc_target_flags) -L$(objtree)/$(obj) \
  107. - --output $(rustdoc_output) \
  108. - --crate-name $(subst rustdoc-,,$@) \
  109. - $(if $(rustdoc_host),,--sysroot=/dev/null) \
  110. - @$(objtree)/include/generated/rustc_cfg $<
  111. -
  112. -# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
  113. -# can be used to specify a custom logo. However:
  114. -# - The given value is used as-is, thus it cannot be relative or a local file
  115. -# (unlike the non-custom case) since the generated docs have subfolders.
  116. -# - It requires adding it to every crate.
  117. -# - It requires changing `core` which comes from the sysroot.
  118. -#
  119. -# Using `-Zcrate-attr` would solve the last two points, but not the first.
  120. -# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
  121. -# command-like flags to solve the issue. Meanwhile, we use the non-custom case
  122. -# and then retouch the generated files.
  123. -rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
  124. - rustdoc-alloc rustdoc-kernel
  125. - $(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
  126. - $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
  127. - $(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
  128. - -e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
  129. - -e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
  130. - -e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
  131. - -e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
  132. - $(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
  133. - echo ".logo-container > img { object-fit: contain; }" >> $$f; done
  134. -
  135. -rustdoc-macros: private rustdoc_host = yes
  136. -rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
  137. - --extern proc_macro
  138. -rustdoc-macros: $(src)/macros/lib.rs FORCE
  139. - +$(call if_changed,rustdoc)
  140. -
  141. -rustdoc-core: private rustc_target_flags = $(core-cfgs)
  142. -rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
  143. - +$(call if_changed,rustdoc)
  144. -
  145. -rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
  146. - +$(call if_changed,rustdoc)
  147. -
  148. -# We need to allow `rustdoc::broken_intra_doc_links` because some
  149. -# `no_global_oom_handling` functions refer to non-`no_global_oom_handling`
  150. -# functions. Ideally `rustdoc` would have a way to distinguish broken links
  151. -# due to things that are "configured out" vs. entirely non-existing ones.
  152. -rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \
  153. - -Arustdoc::broken_intra_doc_links
  154. -rustdoc-alloc: $(RUST_LIB_SRC)/alloc/src/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
  155. - +$(call if_changed,rustdoc)
  156. -
  157. -rustdoc-kernel: private rustc_target_flags = --extern alloc \
  158. - --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
  159. - --extern bindings --extern uapi
  160. -rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
  161. - rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
  162. - $(obj)/bindings.o FORCE
  163. - +$(call if_changed,rustdoc)
  164. -
  165. -quiet_cmd_rustc_test_library = RUSTC TL $<
  166. - cmd_rustc_test_library = \
  167. - OBJTREE=$(abspath $(objtree)) \
  168. - $(RUSTC) $(rust_common_flags) \
  169. - @$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \
  170. - --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
  171. - --out-dir $(objtree)/$(obj)/test --cfg testlib \
  172. - --sysroot $(objtree)/$(obj)/test/sysroot \
  173. - -L$(objtree)/$(obj)/test \
  174. - --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
  175. -
  176. -rusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
  177. - +$(call if_changed,rustc_test_library)
  178. -
  179. -rusttestlib-macros: private rustc_target_flags = --extern proc_macro
  180. -rusttestlib-macros: private rustc_test_library_proc = yes
  181. -rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
  182. - +$(call if_changed,rustc_test_library)
  183. -
  184. -rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
  185. - +$(call if_changed,rustc_test_library)
  186. -
  187. -rusttestlib-uapi: $(src)/uapi/lib.rs rusttest-prepare FORCE
  188. - +$(call if_changed,rustc_test_library)
  189. -
  190. -quiet_cmd_rustdoc_test = RUSTDOC T $<
  191. - cmd_rustdoc_test = \
  192. - OBJTREE=$(abspath $(objtree)) \
  193. - $(RUSTDOC) --test $(rust_common_flags) \
  194. - @$(objtree)/include/generated/rustc_cfg \
  195. - $(rustc_target_flags) $(rustdoc_test_target_flags) \
  196. - --sysroot $(objtree)/$(obj)/test/sysroot $(rustdoc_test_quiet) \
  197. - -L$(objtree)/$(obj)/test --output $(rustdoc_output) \
  198. - --crate-name $(subst rusttest-,,$@) $<
  199. -
  200. -quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
  201. - cmd_rustdoc_test_kernel = \
  202. - rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
  203. - mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
  204. - OBJTREE=$(abspath $(objtree)) \
  205. - $(RUSTDOC) --test $(rust_flags) \
  206. - -L$(objtree)/$(obj) --extern alloc --extern kernel \
  207. - --extern build_error --extern macros \
  208. - --extern bindings --extern uapi \
  209. - --no-run --crate-name kernel -Zunstable-options \
  210. - --sysroot=/dev/null \
  211. - --test-builder $(objtree)/scripts/rustdoc_test_builder \
  212. - $< $(rustdoc_test_kernel_quiet); \
  213. - $(objtree)/scripts/rustdoc_test_gen
  214. -
  215. -%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
  216. - $(src)/kernel/lib.rs $(obj)/kernel.o \
  217. - $(objtree)/scripts/rustdoc_test_builder \
  218. - $(objtree)/scripts/rustdoc_test_gen FORCE
  219. - +$(call if_changed,rustdoc_test_kernel)
  220. -
  221. -# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
  222. -# so for the moment we skip `-Cpanic=abort`.
  223. -quiet_cmd_rustc_test = RUSTC T $<
  224. - cmd_rustc_test = \
  225. - OBJTREE=$(abspath $(objtree)) \
  226. - $(RUSTC) --test $(rust_common_flags) \
  227. - @$(objtree)/include/generated/rustc_cfg \
  228. - $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
  229. - --sysroot $(objtree)/$(obj)/test/sysroot \
  230. - -L$(objtree)/$(obj)/test \
  231. - --crate-name $(subst rusttest-,,$@) $<; \
  232. - $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
  233. - $(rustc_test_run_flags)
  234. -
  235. -rusttest: rusttest-macros rusttest-kernel
  236. -
  237. -# This prepares a custom sysroot with our custom `alloc` instead of
  238. -# the standard one.
  239. -#
  240. -# This requires several hacks:
  241. -# - Unlike `core` and `alloc`, `std` depends on more than a dozen crates,
  242. -# including third-party crates that need to be downloaded, plus custom
  243. -# `build.rs` steps. Thus hardcoding things here is not maintainable.
  244. -# - `cargo` knows how to build the standard library, but it is an unstable
  245. -# feature so far (`-Zbuild-std`).
  246. -# - `cargo` only considers the use case of building the standard library
  247. -# to use it in a given package. Thus we need to create a dummy package
  248. -# and pick the generated libraries from there.
  249. -# - The usual ways of modifying the dependency graph in `cargo` do not seem
  250. -# to apply for the `-Zbuild-std` steps, thus we have to mislead it
  251. -# by modifying the sources in the sysroot.
  252. -# - To avoid messing with the user's Rust installation, we create a clone
  253. -# of the sysroot. However, `cargo` ignores `RUSTFLAGS` in the `-Zbuild-std`
  254. -# steps, thus we use a wrapper binary passed via `RUSTC` to pass the flag.
  255. -#
  256. -# In the future, we hope to avoid the whole ordeal by either:
  257. -# - Making the `test` crate not depend on `std` (either improving upstream
  258. -# or having our own custom crate).
  259. -# - Making the tests run in kernel space (requires the previous point).
  260. -# - Making `std` and friends be more like a "normal" crate, so that
  261. -# `-Zbuild-std` and related hacks are not needed.
  262. -quiet_cmd_rustsysroot = RUSTSYSROOT
  263. - cmd_rustsysroot = \
  264. - rm -rf $(objtree)/$(obj)/test; \
  265. - mkdir -p $(objtree)/$(obj)/test; \
  266. - cp -a $(rustc_sysroot) $(objtree)/$(obj)/test/sysroot; \
  267. - echo '\#!/bin/sh' > $(objtree)/$(obj)/test/rustc_sysroot; \
  268. - echo "$(RUSTC) --sysroot=$(abspath $(objtree)/$(obj)/test/sysroot) \"\$$@\"" \
  269. - >> $(objtree)/$(obj)/test/rustc_sysroot; \
  270. - chmod u+x $(objtree)/$(obj)/test/rustc_sysroot; \
  271. - $(CARGO) -q new $(objtree)/$(obj)/test/dummy; \
  272. - RUSTC=$(objtree)/$(obj)/test/rustc_sysroot $(CARGO) $(cargo_quiet) \
  273. - test -Zbuild-std --target $(rustc_host_target) \
  274. - --manifest-path $(objtree)/$(obj)/test/dummy/Cargo.toml; \
  275. - rm $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib/*; \
  276. - cp $(objtree)/$(obj)/test/dummy/target/$(rustc_host_target)/debug/deps/* \
  277. - $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib
  278. -
  279. -rusttest-prepare: FORCE
  280. - +$(call if_changed,rustsysroot)
  281. -
  282. -rusttest-macros: private rustc_target_flags = --extern proc_macro
  283. -rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
  284. -rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
  285. - +$(call if_changed,rustc_test)
  286. - +$(call if_changed,rustdoc_test)
  287. -
  288. -rusttest-kernel: private rustc_target_flags = --extern alloc \
  289. - --extern build_error --extern macros --extern bindings --extern uapi
  290. -rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
  291. - rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
  292. - rusttestlib-uapi FORCE
  293. - +$(call if_changed,rustc_test)
  294. - +$(call if_changed,rustc_test_library)
  295. -
  296. -ifdef CONFIG_CC_IS_CLANG
  297. -bindgen_c_flags = $(c_flags)
  298. -else
  299. -# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
  300. -# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
  301. -#
  302. -# For the moment, here we are tweaking the flags on the fly. This is a hack,
  303. -# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
  304. -# if we end up using one of those structs).
  305. -bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
  306. - -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
  307. - -mindirect-branch=thunk-extern -mindirect-branch-register \
  308. - -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
  309. - -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
  310. - -mno-pointers-to-nested-functions -mno-string \
  311. - -mno-strict-align -mstrict-align \
  312. - -fconserve-stack -falign-jumps=% -falign-loops=% \
  313. - -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
  314. - -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
  315. - -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
  316. - -fzero-call-used-regs=% -fno-stack-clash-protection \
  317. - -fno-inline-functions-called-once -fsanitize=bounds-strict \
  318. - -fstrict-flex-arrays=% \
  319. - --param=% --param asan-%
  320. -
  321. -# Derived from `scripts/Makefile.clang`.
  322. -BINDGEN_TARGET_x86 := x86_64-linux-gnu
  323. -BINDGEN_TARGET_arm64 := aarch64-linux-gnu
  324. -BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH))
  325. -
  326. -# All warnings are inhibited since GCC builds are very experimental,
  327. -# many GCC warnings are not supported by Clang, they may only appear in
  328. -# some configurations, with new GCC versions, etc.
  329. -bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
  330. -
  331. -# Auto variable zero-initialization requires an additional special option with
  332. -# clang that is going to be removed sometime in the future (likely in
  333. -# clang-18), so make sure to pass this option only if clang supports it
  334. -# (libclang major version < 16).
  335. -#
  336. -# https://github.com/llvm/llvm-project/issues/44842
  337. -# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
  338. -ifdef CONFIG_INIT_STACK_ALL_ZERO
  339. -libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
  340. -ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
  341. -bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
  342. -endif
  343. -endif
  344. -
  345. -bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
  346. - $(bindgen_extra_c_flags)
  347. -endif
  348. -
  349. -ifdef CONFIG_LTO
  350. -bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
  351. -else
  352. -bindgen_c_flags_lto = $(bindgen_c_flags)
  353. -endif
  354. -
  355. -bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__
  356. -
  357. -quiet_cmd_bindgen = BINDGEN $@
  358. - cmd_bindgen = \
  359. - $(BINDGEN) $< $(bindgen_target_flags) \
  360. - --use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
  361. - --no-debug '.*' \
  362. - -o $@ -- $(bindgen_c_flags_final) -DMODULE \
  363. - $(bindgen_target_cflags) $(bindgen_target_extra)
  364. -
  365. -$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
  366. - $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
  367. -$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
  368. - sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
  369. -$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
  370. - $(src)/bindgen_parameters FORCE
  371. - $(call if_changed_dep,bindgen)
  372. -
  373. -$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
  374. - $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
  375. -$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
  376. - $(src)/bindgen_parameters FORCE
  377. - $(call if_changed_dep,bindgen)
  378. -
  379. -# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
  380. -# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
  381. -# given it is `libclang`; but for consistency, future Clang changes and/or
  382. -# a potential future GCC backend for `bindgen`, we disable it too.
  383. -$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
  384. - --blocklist-type '.*' --allowlist-var '' \
  385. - --allowlist-function 'rust_helper_.*'
  386. -$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
  387. - -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
  388. -$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
  389. - sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@
  390. -$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
  391. - $(call if_changed_dep,bindgen)
  392. -
  393. -quiet_cmd_exports = EXPORTS $@
  394. - cmd_exports = \
  395. - $(NM) -p --defined-only $< \
  396. - | awk '/ (T|R|D|B) / {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
  397. -
  398. -$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
  399. - $(call if_changed,exports)
  400. -
  401. -$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
  402. - $(call if_changed,exports)
  403. -
  404. -$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
  405. - $(call if_changed,exports)
  406. -
  407. -$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
  408. - $(call if_changed,exports)
  409. -
  410. -quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
  411. - cmd_rustc_procmacro = \
  412. - $(RUSTC_OR_CLIPPY) $(rust_common_flags) \
  413. - -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
  414. - -Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
  415. - --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
  416. - --crate-type proc-macro \
  417. - --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
  418. -
  419. -# Procedural macros can only be used with the `rustc` that compiled it.
  420. -# Therefore, to get `libmacros.so` automatically recompiled when the compiler
  421. -# version changes, we add `core.o` as a dependency (even if it is not needed).
  422. -$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE
  423. - +$(call if_changed_dep,rustc_procmacro)
  424. -
  425. -quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
  426. - cmd_rustc_library = \
  427. - OBJTREE=$(abspath $(objtree)) \
  428. - $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
  429. - $(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
  430. - --emit=dep-info=$(depfile) --emit=obj=$@ \
  431. - --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
  432. - --crate-type rlib -L$(objtree)/$(obj) \
  433. - --crate-name $(patsubst %.o,%,$(notdir $@)) $< \
  434. - --sysroot=/dev/null \
  435. - $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
  436. -
  437. -rust-analyzer:
  438. - $(Q)$(srctree)/scripts/generate_rust_analyzer.py \
  439. - --cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
  440. - $(realpath $(srctree)) $(realpath $(objtree)) \
  441. - $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
  442. - $(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
  443. -
  444. -redirect-intrinsics = \
  445. - __addsf3 __eqsf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __unordsf2 \
  446. - __adddf3 __ledf2 __ltdf2 __muldf3 __unorddf2 \
  447. - __muloti4 __multi3 \
  448. - __udivmodti4 __udivti3 __umodti3
  449. -
  450. -ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
  451. - # These intrinsics are defined for ARM64 and RISCV64
  452. - redirect-intrinsics += \
  453. - __ashrti3 \
  454. - __ashlti3 __lshrti3
  455. -endif
  456. -
  457. -$(obj)/core.o: private skip_clippy = 1
  458. -$(obj)/core.o: private skip_flags = -Dunreachable_pub
  459. -$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
  460. -$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
  461. -$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
  462. - +$(call if_changed_dep,rustc_library)
  463. -ifdef CONFIG_X86_64
  464. -$(obj)/core.o: scripts/target.json
  465. -endif
  466. -
  467. -$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
  468. -$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
  469. - +$(call if_changed_dep,rustc_library)
  470. -
  471. -$(obj)/alloc.o: private skip_clippy = 1
  472. -$(obj)/alloc.o: private skip_flags = -Dunreachable_pub
  473. -$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
  474. -$(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE
  475. - +$(call if_changed_dep,rustc_library)
  476. -
  477. -$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
  478. - +$(call if_changed_dep,rustc_library)
  479. -
  480. -$(obj)/bindings.o: $(src)/bindings/lib.rs \
  481. - $(obj)/compiler_builtins.o \
  482. - $(obj)/bindings/bindings_generated.rs \
  483. - $(obj)/bindings/bindings_helpers_generated.rs FORCE
  484. - +$(call if_changed_dep,rustc_library)
  485. -
  486. -$(obj)/uapi.o: $(src)/uapi/lib.rs \
  487. - $(obj)/compiler_builtins.o \
  488. - $(obj)/uapi/uapi_generated.rs FORCE
  489. - +$(call if_changed_dep,rustc_library)
  490. -
  491. -$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
  492. - --extern build_error --extern macros --extern bindings --extern uapi
  493. -$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
  494. - $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
  495. - +$(call if_changed_dep,rustc_library)
  496. -
  497. -endif # CONFIG_RUST
  498. diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters
  499. deleted file mode 100644
  500. --- a/rust/bindgen_parameters
  501. +++ /dev/null
  502. @@ -1,26 +0,0 @@
  503. -# SPDX-License-Identifier: GPL-2.0
  504. -
  505. ---opaque-type xregs_state
  506. ---opaque-type desc_struct
  507. ---opaque-type arch_lbr_state
  508. ---opaque-type local_apic
  509. -
  510. -# Packed type cannot transitively contain a `#[repr(align)]` type.
  511. ---opaque-type alt_instr
  512. ---opaque-type x86_msi_data
  513. ---opaque-type x86_msi_addr_lo
  514. -
  515. -# `try` is a reserved keyword since Rust 2018; solved in `bindgen` v0.59.2,
  516. -# commit 2aed6b021680 ("context: Escape the try keyword properly").
  517. ---opaque-type kunit_try_catch
  518. -
  519. -# If SMP is disabled, `arch_spinlock_t` is defined as a ZST which triggers a Rust
  520. -# warning. We don't need to peek into it anyway.
  521. ---opaque-type spinlock
  522. -
  523. -# `seccomp`'s comment gets understood as a doctest
  524. ---no-doc-comments
  525. -
  526. -# These functions use the `__preserve_most` calling convention, which neither bindgen
  527. -# nor Rust currently understand, and which Clang currently declares to be unstable.
  528. ---blocklist-function __list_.*_report
  529. diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
  530. deleted file mode 100644
  531. --- a/rust/bindings/bindings_helper.h
  532. +++ /dev/null
  533. @@ -1,27 +0,0 @@
  534. -/* SPDX-License-Identifier: GPL-2.0 */
  535. -/*
  536. - * Header that contains the code (mostly headers) for which Rust bindings
  537. - * will be automatically generated by `bindgen`.
  538. - *
  539. - * Sorted alphabetically.
  540. - */
  541. -
  542. -#include <kunit/test.h>
  543. -#include <linux/errname.h>
  544. -#include <linux/ethtool.h>
  545. -#include <linux/jiffies.h>
  546. -#include <linux/mdio.h>
  547. -#include <linux/phy.h>
  548. -#include <linux/refcount.h>
  549. -#include <linux/sched.h>
  550. -#include <linux/slab.h>
  551. -#include <linux/wait.h>
  552. -#include <linux/workqueue.h>
  553. -
  554. -/* `bindgen` gets confused at certain things. */
  555. -const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
  556. -const gfp_t RUST_CONST_HELPER_GFP_ATOMIC = GFP_ATOMIC;
  557. -const gfp_t RUST_CONST_HELPER_GFP_KERNEL = GFP_KERNEL;
  558. -const gfp_t RUST_CONST_HELPER_GFP_KERNEL_ACCOUNT = GFP_KERNEL_ACCOUNT;
  559. -const gfp_t RUST_CONST_HELPER_GFP_NOWAIT = GFP_NOWAIT;
  560. -const gfp_t RUST_CONST_HELPER___GFP_ZERO = __GFP_ZERO;
  561. diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs
  562. deleted file mode 100644
  563. --- a/rust/bindings/lib.rs
  564. +++ /dev/null
  565. @@ -1,50 +0,0 @@
  566. -// SPDX-License-Identifier: GPL-2.0
  567. -
  568. -//! Bindings.
  569. -//!
  570. -//! Imports the generated bindings by `bindgen`.
  571. -//!
  572. -//! This crate may not be directly used. If you need a kernel C API that is
  573. -//! not ported or wrapped in the `kernel` crate, then do so first instead of
  574. -//! using this crate.
  575. -
  576. -#![no_std]
  577. -// See <https://github.com/rust-lang/rust-bindgen/issues/1651>.
  578. -#![cfg_attr(test, allow(deref_nullptr))]
  579. -#![cfg_attr(test, allow(unaligned_references))]
  580. -#![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))]
  581. -#![allow(
  582. - clippy::all,
  583. - missing_docs,
  584. - non_camel_case_types,
  585. - non_upper_case_globals,
  586. - non_snake_case,
  587. - improper_ctypes,
  588. - unreachable_pub,
  589. - unsafe_op_in_unsafe_fn
  590. -)]
  591. -
  592. -mod bindings_raw {
  593. - // Use glob import here to expose all helpers.
  594. - // Symbols defined within the module will take precedence to the glob import.
  595. - pub use super::bindings_helper::*;
  596. - include!(concat!(
  597. - env!("OBJTREE"),
  598. - "/rust/bindings/bindings_generated.rs"
  599. - ));
  600. -}
  601. -
  602. -// When both a directly exposed symbol and a helper exists for the same function,
  603. -// the directly exposed symbol is preferred and the helper becomes dead code, so
  604. -// ignore the warning here.
  605. -#[allow(dead_code)]
  606. -mod bindings_helper {
  607. - // Import the generated bindings for types.
  608. - use super::bindings_raw::*;
  609. - include!(concat!(
  610. - env!("OBJTREE"),
  611. - "/rust/bindings/bindings_helpers_generated.rs"
  612. - ));
  613. -}
  614. -
  615. -pub use bindings_raw::*;
  616. diff --git a/rust/build_error.rs b/rust/build_error.rs
  617. deleted file mode 100644
  618. --- a/rust/build_error.rs
  619. +++ /dev/null
  620. @@ -1,31 +0,0 @@
  621. -// SPDX-License-Identifier: GPL-2.0
  622. -
  623. -//! Build-time error.
  624. -//!
  625. -//! This crate provides a [const function][const-functions] `build_error`, which will panic in
  626. -//! compile-time if executed in [const context][const-context], and will cause a build error
  627. -//! if not executed at compile time and the optimizer does not optimise away the call.
  628. -//!
  629. -//! It is used by `build_assert!` in the kernel crate, allowing checking of
  630. -//! conditions that could be checked statically, but could not be enforced in
  631. -//! Rust yet (e.g. perform some checks in [const functions][const-functions], but those
  632. -//! functions could still be called in the runtime).
  633. -//!
  634. -//! For details on constant evaluation in Rust, please see the [Reference][const-eval].
  635. -//!
  636. -//! [const-eval]: https://doc.rust-lang.org/reference/const_eval.html
  637. -//! [const-functions]: https://doc.rust-lang.org/reference/const_eval.html#const-functions
  638. -//! [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
  639. -
  640. -#![no_std]
  641. -
  642. -/// Panics if executed in [const context][const-context], or triggers a build error if not.
  643. -///
  644. -/// [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
  645. -#[inline(never)]
  646. -#[cold]
  647. -#[export_name = "rust_build_error"]
  648. -#[track_caller]
  649. -pub const fn build_error(msg: &'static str) -> ! {
  650. - panic!("{}", msg);
  651. -}
  652. diff --git a/rust/compiler_builtins.rs b/rust/compiler_builtins.rs
  653. deleted file mode 100644
  654. --- a/rust/compiler_builtins.rs
  655. +++ /dev/null
  656. @@ -1,74 +0,0 @@
  657. -// SPDX-License-Identifier: GPL-2.0
  658. -
  659. -//! Our own `compiler_builtins`.
  660. -//!
  661. -//! Rust provides [`compiler_builtins`] as a port of LLVM's [`compiler-rt`].
  662. -//! Since we do not need the vast majority of them, we avoid the dependency
  663. -//! by providing this file.
  664. -//!
  665. -//! At the moment, some builtins are required that should not be. For instance,
  666. -//! [`core`] has 128-bit integers functionality which we should not be compiling
  667. -//! in. We will work with upstream [`core`] to provide feature flags to disable
  668. -//! the parts we do not need. For the moment, we define them to [`panic!`] at
  669. -//! runtime for simplicity to catch mistakes, instead of performing surgery
  670. -//! on `core.o`.
  671. -//!
  672. -//! In any case, all these symbols are weakened to ensure we do not override
  673. -//! those that may be provided by the rest of the kernel.
  674. -//!
  675. -//! [`compiler_builtins`]: https://github.com/rust-lang/compiler-builtins
  676. -//! [`compiler-rt`]: https://compiler-rt.llvm.org/
  677. -
  678. -#![allow(internal_features)]
  679. -#![feature(compiler_builtins)]
  680. -#![compiler_builtins]
  681. -#![no_builtins]
  682. -#![no_std]
  683. -
  684. -macro_rules! define_panicking_intrinsics(
  685. - ($reason: tt, { $($ident: ident, )* }) => {
  686. - $(
  687. - #[doc(hidden)]
  688. - #[export_name = concat!("__rust", stringify!($ident))]
  689. - pub extern "C" fn $ident() {
  690. - panic!($reason);
  691. - }
  692. - )*
  693. - }
  694. -);
  695. -
  696. -define_panicking_intrinsics!("`f32` should not be used", {
  697. - __addsf3,
  698. - __eqsf2,
  699. - __gesf2,
  700. - __lesf2,
  701. - __ltsf2,
  702. - __mulsf3,
  703. - __nesf2,
  704. - __unordsf2,
  705. -});
  706. -
  707. -define_panicking_intrinsics!("`f64` should not be used", {
  708. - __adddf3,
  709. - __ledf2,
  710. - __ltdf2,
  711. - __muldf3,
  712. - __unorddf2,
  713. -});
  714. -
  715. -define_panicking_intrinsics!("`i128` should not be used", {
  716. - __ashrti3,
  717. - __muloti4,
  718. - __multi3,
  719. -});
  720. -
  721. -define_panicking_intrinsics!("`u128` should not be used", {
  722. - __ashlti3,
  723. - __lshrti3,
  724. - __udivmodti4,
  725. - __udivti3,
  726. - __umodti3,
  727. -});
  728. -
  729. -// NOTE: if you are adding a new intrinsic here, you should also add it to
  730. -// `redirect-intrinsics` in `rust/Makefile`.
  731. diff --git a/rust/exports.c b/rust/exports.c
  732. deleted file mode 100644
  733. --- a/rust/exports.c
  734. +++ /dev/null
  735. @@ -1,26 +0,0 @@
  736. -// SPDX-License-Identifier: GPL-2.0
  737. -/*
  738. - * A hack to export Rust symbols for loadable modules without having to redo
  739. - * the entire `include/linux/export.h` logic in Rust.
  740. - *
  741. - * This requires the Rust's new/future `v0` mangling scheme because the default
  742. - * one ("legacy") uses invalid characters for C identifiers (thus we cannot use
  743. - * the `EXPORT_SYMBOL_*` macros).
  744. - *
  745. - * All symbols are exported as GPL-only to guarantee no GPL-only feature is
  746. - * accidentally exposed.
  747. - */
  748. -
  749. -#include <linux/export.h>
  750. -
  751. -#define EXPORT_SYMBOL_RUST_GPL(sym) extern int sym; EXPORT_SYMBOL_GPL(sym)
  752. -
  753. -#include "exports_core_generated.h"
  754. -#include "exports_alloc_generated.h"
  755. -#include "exports_bindings_generated.h"
  756. -#include "exports_kernel_generated.h"
  757. -
  758. -// For modules using `rust/build_error.rs`.
  759. -#ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
  760. -EXPORT_SYMBOL_RUST_GPL(rust_build_error);
  761. -#endif
  762. diff --git a/rust/helpers.c b/rust/helpers.c
  763. deleted file mode 100644
  764. --- a/rust/helpers.c
  765. +++ /dev/null
  766. @@ -1,188 +0,0 @@
  767. -// SPDX-License-Identifier: GPL-2.0
  768. -/*
  769. - * Non-trivial C macros cannot be used in Rust. Similarly, inlined C functions
  770. - * cannot be called either. This file explicitly creates functions ("helpers")
  771. - * that wrap those so that they can be called from Rust.
  772. - *
  773. - * Even though Rust kernel modules should never use the bindings directly, some
  774. - * of these helpers need to be exported because Rust generics and inlined
  775. - * functions may not get their code generated in the crate where they are
  776. - * defined. Other helpers, called from non-inline functions, may not be
  777. - * exported, in principle. However, in general, the Rust compiler does not
  778. - * guarantee codegen will be performed for a non-inline function either.
  779. - * Therefore, this file exports all the helpers. In the future, this may be
  780. - * revisited to reduce the number of exports after the compiler is informed
  781. - * about the places codegen is required.
  782. - *
  783. - * All symbols are exported as GPL-only to guarantee no GPL-only feature is
  784. - * accidentally exposed.
  785. - *
  786. - * Sorted alphabetically.
  787. - */
  788. -
  789. -#include <kunit/test-bug.h>
  790. -#include <linux/bug.h>
  791. -#include <linux/build_bug.h>
  792. -#include <linux/err.h>
  793. -#include <linux/errname.h>
  794. -#include <linux/mutex.h>
  795. -#include <linux/refcount.h>
  796. -#include <linux/sched/signal.h>
  797. -#include <linux/slab.h>
  798. -#include <linux/spinlock.h>
  799. -#include <linux/wait.h>
  800. -#include <linux/workqueue.h>
  801. -
  802. -__noreturn void rust_helper_BUG(void)
  803. -{
  804. - BUG();
  805. -}
  806. -EXPORT_SYMBOL_GPL(rust_helper_BUG);
  807. -
  808. -void rust_helper_mutex_lock(struct mutex *lock)
  809. -{
  810. - mutex_lock(lock);
  811. -}
  812. -EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
  813. -
  814. -void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
  815. - struct lock_class_key *key)
  816. -{
  817. -#ifdef CONFIG_DEBUG_SPINLOCK
  818. - __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
  819. -#else
  820. - spin_lock_init(lock);
  821. -#endif
  822. -}
  823. -EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
  824. -
  825. -void rust_helper_spin_lock(spinlock_t *lock)
  826. -{
  827. - spin_lock(lock);
  828. -}
  829. -EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
  830. -
  831. -void rust_helper_spin_unlock(spinlock_t *lock)
  832. -{
  833. - spin_unlock(lock);
  834. -}
  835. -EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
  836. -
  837. -void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
  838. -{
  839. - init_wait(wq_entry);
  840. -}
  841. -EXPORT_SYMBOL_GPL(rust_helper_init_wait);
  842. -
  843. -int rust_helper_signal_pending(struct task_struct *t)
  844. -{
  845. - return signal_pending(t);
  846. -}
  847. -EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
  848. -
  849. -refcount_t rust_helper_REFCOUNT_INIT(int n)
  850. -{
  851. - return (refcount_t)REFCOUNT_INIT(n);
  852. -}
  853. -EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
  854. -
  855. -void rust_helper_refcount_inc(refcount_t *r)
  856. -{
  857. - refcount_inc(r);
  858. -}
  859. -EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
  860. -
  861. -bool rust_helper_refcount_dec_and_test(refcount_t *r)
  862. -{
  863. - return refcount_dec_and_test(r);
  864. -}
  865. -EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
  866. -
  867. -__force void *rust_helper_ERR_PTR(long err)
  868. -{
  869. - return ERR_PTR(err);
  870. -}
  871. -EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
  872. -
  873. -bool rust_helper_IS_ERR(__force const void *ptr)
  874. -{
  875. - return IS_ERR(ptr);
  876. -}
  877. -EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
  878. -
  879. -long rust_helper_PTR_ERR(__force const void *ptr)
  880. -{
  881. - return PTR_ERR(ptr);
  882. -}
  883. -EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
  884. -
  885. -const char *rust_helper_errname(int err)
  886. -{
  887. - return errname(err);
  888. -}
  889. -EXPORT_SYMBOL_GPL(rust_helper_errname);
  890. -
  891. -struct task_struct *rust_helper_get_current(void)
  892. -{
  893. - return current;
  894. -}
  895. -EXPORT_SYMBOL_GPL(rust_helper_get_current);
  896. -
  897. -void rust_helper_get_task_struct(struct task_struct *t)
  898. -{
  899. - get_task_struct(t);
  900. -}
  901. -EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
  902. -
  903. -void rust_helper_put_task_struct(struct task_struct *t)
  904. -{
  905. - put_task_struct(t);
  906. -}
  907. -EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
  908. -
  909. -struct kunit *rust_helper_kunit_get_current_test(void)
  910. -{
  911. - return kunit_get_current_test();
  912. -}
  913. -EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);
  914. -
  915. -void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
  916. - bool onstack, const char *name,
  917. - struct lock_class_key *key)
  918. -{
  919. - __init_work(work, onstack);
  920. - work->data = (atomic_long_t)WORK_DATA_INIT();
  921. - lockdep_init_map(&work->lockdep_map, name, key, 0);
  922. - INIT_LIST_HEAD(&work->entry);
  923. - work->func = func;
  924. -}
  925. -EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);
  926. -
  927. -void * __must_check __realloc_size(2)
  928. -rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
  929. -{
  930. - return krealloc(objp, new_size, flags);
  931. -}
  932. -EXPORT_SYMBOL_GPL(rust_helper_krealloc);
  933. -
  934. -/*
  935. - * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
  936. - * use it in contexts where Rust expects a `usize` like slice (array) indices.
  937. - * `usize` is defined to be the same as C's `uintptr_t` type (can hold any
  938. - * pointer) but not necessarily the same as `size_t` (can hold the size of any
  939. - * single object). Most modern platforms use the same concrete integer type for
  940. - * both of them, but in case we find ourselves on a platform where
  941. - * that's not true, fail early instead of risking ABI or
  942. - * integer-overflow issues.
  943. - *
  944. - * If your platform fails this assertion, it means that you are in
  945. - * danger of integer-overflow bugs (even if you attempt to add
  946. - * `--no-size_t-is-usize`). It may be easiest to change the kernel ABI on
  947. - * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
  948. - * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
  949. - */
  950. -static_assert(
  951. - sizeof(size_t) == sizeof(uintptr_t) &&
  952. - __alignof__(size_t) == __alignof__(uintptr_t),
  953. - "Rust code expects C `size_t` to match Rust `usize`"
  954. -);
  955. diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
  956. deleted file mode 100644
  957. --- a/rust/kernel/alloc.rs
  958. +++ /dev/null
  959. @@ -1,73 +0,0 @@
  960. -// SPDX-License-Identifier: GPL-2.0
  961. -
  962. -//! Extensions to the [`alloc`] crate.
  963. -
  964. -#[cfg(not(test))]
  965. -#[cfg(not(testlib))]
  966. -mod allocator;
  967. -pub mod box_ext;
  968. -pub mod vec_ext;
  969. -
  970. -/// Indicates an allocation error.
  971. -#[derive(Copy, Clone, PartialEq, Eq, Debug)]
  972. -pub struct AllocError;
  973. -
  974. -/// Flags to be used when allocating memory.
  975. -///
  976. -/// They can be combined with the operators `|`, `&`, and `!`.
  977. -///
  978. -/// Values can be used from the [`flags`] module.
  979. -#[derive(Clone, Copy)]
  980. -pub struct Flags(u32);
  981. -
  982. -impl core::ops::BitOr for Flags {
  983. - type Output = Self;
  984. - fn bitor(self, rhs: Self) -> Self::Output {
  985. - Self(self.0 | rhs.0)
  986. - }
  987. -}
  988. -
  989. -impl core::ops::BitAnd for Flags {
  990. - type Output = Self;
  991. - fn bitand(self, rhs: Self) -> Self::Output {
  992. - Self(self.0 & rhs.0)
  993. - }
  994. -}
  995. -
  996. -impl core::ops::Not for Flags {
  997. - type Output = Self;
  998. - fn not(self) -> Self::Output {
  999. - Self(!self.0)
  1000. - }
  1001. -}
  1002. -
  1003. -/// Allocation flags.
  1004. -///
  1005. -/// These are meant to be used in functions that can allocate memory.
  1006. -pub mod flags {
  1007. - use super::Flags;
  1008. -
  1009. - /// Zeroes out the allocated memory.
  1010. - ///
  1011. - /// This is normally or'd with other flags.
  1012. - pub const __GFP_ZERO: Flags = Flags(bindings::__GFP_ZERO);
  1013. -
  1014. - /// Users can not sleep and need the allocation to succeed.
  1015. - ///
  1016. - /// A lower watermark is applied to allow access to "atomic reserves". The current
  1017. - /// implementation doesn't support NMI and few other strict non-preemptive contexts (e.g.
  1018. - /// raw_spin_lock). The same applies to [`GFP_NOWAIT`].
  1019. - pub const GFP_ATOMIC: Flags = Flags(bindings::GFP_ATOMIC);
  1020. -
  1021. - /// Typical for kernel-internal allocations. The caller requires ZONE_NORMAL or a lower zone
  1022. - /// for direct access but can direct reclaim.
  1023. - pub const GFP_KERNEL: Flags = Flags(bindings::GFP_KERNEL);
  1024. -
  1025. - /// The same as [`GFP_KERNEL`], except the allocation is accounted to kmemcg.
  1026. - pub const GFP_KERNEL_ACCOUNT: Flags = Flags(bindings::GFP_KERNEL_ACCOUNT);
  1027. -
  1028. - /// Ror kernel allocations that should not stall for direct reclaim, start physical IO or
  1029. - /// use any filesystem callback. It is very likely to fail to allocate memory, even for very
  1030. - /// small allocations.
  1031. - pub const GFP_NOWAIT: Flags = Flags(bindings::GFP_NOWAIT);
  1032. -}
  1033. diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
  1034. deleted file mode 100644
  1035. --- a/rust/kernel/alloc/allocator.rs
  1036. +++ /dev/null
  1037. @@ -1,81 +0,0 @@
  1038. -// SPDX-License-Identifier: GPL-2.0
  1039. -
  1040. -//! Allocator support.
  1041. -
  1042. -use super::{flags::*, Flags};
  1043. -use core::alloc::{GlobalAlloc, Layout};
  1044. -use core::ptr;
  1045. -
  1046. -struct KernelAllocator;
  1047. -
  1048. -/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
  1049. -///
  1050. -/// # Safety
  1051. -///
  1052. -/// - `ptr` can be either null or a pointer which has been allocated by this allocator.
  1053. -/// - `new_layout` must have a non-zero size.
  1054. -pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 {
  1055. - // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
  1056. - let layout = new_layout.pad_to_align();
  1057. -
  1058. - let mut size = layout.size();
  1059. -
  1060. - if layout.align() > bindings::ARCH_SLAB_MINALIGN {
  1061. - // The alignment requirement exceeds the slab guarantee, thus try to enlarge the size
  1062. - // to use the "power-of-two" size/alignment guarantee (see comments in `kmalloc()` for
  1063. - // more information).
  1064. - //
  1065. - // Note that `layout.size()` (after padding) is guaranteed to be a multiple of
  1066. - // `layout.align()`, so `next_power_of_two` gives enough alignment guarantee.
  1067. - size = size.next_power_of_two();
  1068. - }
  1069. -
  1070. - // SAFETY:
  1071. - // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the
  1072. - // function safety requirement.
  1073. - // - `size` is greater than 0 since it's either a `layout.size()` (which cannot be zero
  1074. - // according to the function safety requirement) or a result from `next_power_of_two()`.
  1075. - unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 }
  1076. -}
  1077. -
  1078. -unsafe impl GlobalAlloc for KernelAllocator {
  1079. - unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
  1080. - // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
  1081. - // requirement.
  1082. - unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) }
  1083. - }
  1084. -
  1085. - unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
  1086. - unsafe {
  1087. - bindings::kfree(ptr as *const core::ffi::c_void);
  1088. - }
  1089. - }
  1090. -
  1091. - unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
  1092. - // SAFETY:
  1093. - // - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
  1094. - // overflow `isize` by the function safety requirement.
  1095. - // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two).
  1096. - let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
  1097. -
  1098. - // SAFETY:
  1099. - // - `ptr` is either null or a pointer allocated by this allocator by the function safety
  1100. - // requirement.
  1101. - // - the size of `layout` is not zero because `new_size` is not zero by the function safety
  1102. - // requirement.
  1103. - unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) }
  1104. - }
  1105. -
  1106. - unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
  1107. - // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
  1108. - // requirement.
  1109. - unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) }
  1110. - }
  1111. -}
  1112. -
  1113. -#[global_allocator]
  1114. -static ALLOCATOR: KernelAllocator = KernelAllocator;
  1115. -
  1116. -// See <https://github.com/rust-lang/rust/pull/86844>.
  1117. -#[no_mangle]
  1118. -static __rust_no_alloc_shim_is_unstable: u8 = 0;
  1119. diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs
  1120. deleted file mode 100644
  1121. --- a/rust/kernel/alloc/box_ext.rs
  1122. +++ /dev/null
  1123. @@ -1,56 +0,0 @@
  1124. -// SPDX-License-Identifier: GPL-2.0
  1125. -
  1126. -//! Extensions to [`Box`] for fallible allocations.
  1127. -
  1128. -use super::{AllocError, Flags};
  1129. -use alloc::boxed::Box;
  1130. -use core::mem::MaybeUninit;
  1131. -
  1132. -/// Extensions to [`Box`].
  1133. -pub trait BoxExt<T>: Sized {
  1134. - /// Allocates a new box.
  1135. - ///
  1136. - /// The allocation may fail, in which case an error is returned.
  1137. - fn new(x: T, flags: Flags) -> Result<Self, AllocError>;
  1138. -
  1139. - /// Allocates a new uninitialised box.
  1140. - ///
  1141. - /// The allocation may fail, in which case an error is returned.
  1142. - fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError>;
  1143. -}
  1144. -
  1145. -impl<T> BoxExt<T> for Box<T> {
  1146. - fn new(x: T, flags: Flags) -> Result<Self, AllocError> {
  1147. - let b = <Self as BoxExt<_>>::new_uninit(flags)?;
  1148. - Ok(Box::write(b, x))
  1149. - }
  1150. -
  1151. - #[cfg(any(test, testlib))]
  1152. - fn new_uninit(_flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError> {
  1153. - Ok(Box::new_uninit())
  1154. - }
  1155. -
  1156. - #[cfg(not(any(test, testlib)))]
  1157. - fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError> {
  1158. - let ptr = if core::mem::size_of::<MaybeUninit<T>>() == 0 {
  1159. - core::ptr::NonNull::<_>::dangling().as_ptr()
  1160. - } else {
  1161. - let layout = core::alloc::Layout::new::<MaybeUninit<T>>();
  1162. -
  1163. - // SAFETY: Memory is being allocated (first arg is null). The only other source of
  1164. - // safety issues is sleeping on atomic context, which is addressed by klint. Lastly,
  1165. - // the type is not a SZT (checked above).
  1166. - let ptr =
  1167. - unsafe { super::allocator::krealloc_aligned(core::ptr::null_mut(), layout, flags) };
  1168. - if ptr.is_null() {
  1169. - return Err(AllocError);
  1170. - }
  1171. -
  1172. - ptr.cast::<MaybeUninit<T>>()
  1173. - };
  1174. -
  1175. - // SAFETY: For non-zero-sized types, we allocate above using the global allocator. For
  1176. - // zero-sized types, we use `NonNull::dangling`.
  1177. - Ok(unsafe { Box::from_raw(ptr) })
  1178. - }
  1179. -}
  1180. diff --git a/rust/kernel/alloc/vec_ext.rs b/rust/kernel/alloc/vec_ext.rs
  1181. deleted file mode 100644
  1182. --- a/rust/kernel/alloc/vec_ext.rs
  1183. +++ /dev/null
  1184. @@ -1,185 +0,0 @@
  1185. -// SPDX-License-Identifier: GPL-2.0
  1186. -
  1187. -//! Extensions to [`Vec`] for fallible allocations.
  1188. -
  1189. -use super::{AllocError, Flags};
  1190. -use alloc::vec::Vec;
  1191. -
  1192. -/// Extensions to [`Vec`].
  1193. -pub trait VecExt<T>: Sized {
  1194. - /// Creates a new [`Vec`] instance with at least the given capacity.
  1195. - ///
  1196. - /// # Examples
  1197. - ///
  1198. - /// ```
  1199. - /// let v = Vec::<u32>::with_capacity(20, GFP_KERNEL)?;
  1200. - ///
  1201. - /// assert!(v.capacity() >= 20);
  1202. - /// # Ok::<(), Error>(())
  1203. - /// ```
  1204. - fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError>;
  1205. -
  1206. - /// Appends an element to the back of the [`Vec`] instance.
  1207. - ///
  1208. - /// # Examples
  1209. - ///
  1210. - /// ```
  1211. - /// let mut v = Vec::new();
  1212. - /// v.push(1, GFP_KERNEL)?;
  1213. - /// assert_eq!(&v, &[1]);
  1214. - ///
  1215. - /// v.push(2, GFP_KERNEL)?;
  1216. - /// assert_eq!(&v, &[1, 2]);
  1217. - /// # Ok::<(), Error>(())
  1218. - /// ```
  1219. - fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError>;
  1220. -
  1221. - /// Pushes clones of the elements of slice into the [`Vec`] instance.
  1222. - ///
  1223. - /// # Examples
  1224. - ///
  1225. - /// ```
  1226. - /// let mut v = Vec::new();
  1227. - /// v.push(1, GFP_KERNEL)?;
  1228. - ///
  1229. - /// v.extend_from_slice(&[20, 30, 40], GFP_KERNEL)?;
  1230. - /// assert_eq!(&v, &[1, 20, 30, 40]);
  1231. - ///
  1232. - /// v.extend_from_slice(&[50, 60], GFP_KERNEL)?;
  1233. - /// assert_eq!(&v, &[1, 20, 30, 40, 50, 60]);
  1234. - /// # Ok::<(), Error>(())
  1235. - /// ```
  1236. - fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError>
  1237. - where
  1238. - T: Clone;
  1239. -
  1240. - /// Ensures that the capacity exceeds the length by at least `additional` elements.
  1241. - ///
  1242. - /// # Examples
  1243. - ///
  1244. - /// ```
  1245. - /// let mut v = Vec::new();
  1246. - /// v.push(1, GFP_KERNEL)?;
  1247. - ///
  1248. - /// v.reserve(10, GFP_KERNEL)?;
  1249. - /// let cap = v.capacity();
  1250. - /// assert!(cap >= 10);
  1251. - ///
  1252. - /// v.reserve(10, GFP_KERNEL)?;
  1253. - /// let new_cap = v.capacity();
  1254. - /// assert_eq!(new_cap, cap);
  1255. - ///
  1256. - /// # Ok::<(), Error>(())
  1257. - /// ```
  1258. - fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError>;
  1259. -}
  1260. -
  1261. -impl<T> VecExt<T> for Vec<T> {
  1262. - fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError> {
  1263. - let mut v = Vec::new();
  1264. - <Self as VecExt<_>>::reserve(&mut v, capacity, flags)?;
  1265. - Ok(v)
  1266. - }
  1267. -
  1268. - fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
  1269. - <Self as VecExt<_>>::reserve(self, 1, flags)?;
  1270. - let s = self.spare_capacity_mut();
  1271. - s[0].write(v);
  1272. -
  1273. - // SAFETY: We just initialised the first spare entry, so it is safe to increase the length
  1274. - // by 1. We also know that the new length is <= capacity because of the previous call to
  1275. - // `reserve` above.
  1276. - unsafe { self.set_len(self.len() + 1) };
  1277. - Ok(())
  1278. - }
  1279. -
  1280. - fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError>
  1281. - where
  1282. - T: Clone,
  1283. - {
  1284. - <Self as VecExt<_>>::reserve(self, other.len(), flags)?;
  1285. - for (slot, item) in core::iter::zip(self.spare_capacity_mut(), other) {
  1286. - slot.write(item.clone());
  1287. - }
  1288. -
  1289. - // SAFETY: We just initialised the `other.len()` spare entries, so it is safe to increase
  1290. - // the length by the same amount. We also know that the new length is <= capacity because
  1291. - // of the previous call to `reserve` above.
  1292. - unsafe { self.set_len(self.len() + other.len()) };
  1293. - Ok(())
  1294. - }
  1295. -
  1296. - #[cfg(any(test, testlib))]
  1297. - fn reserve(&mut self, additional: usize, _flags: Flags) -> Result<(), AllocError> {
  1298. - Vec::reserve(self, additional);
  1299. - Ok(())
  1300. - }
  1301. -
  1302. - #[cfg(not(any(test, testlib)))]
  1303. - fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError> {
  1304. - let len = self.len();
  1305. - let cap = self.capacity();
  1306. -
  1307. - if cap - len >= additional {
  1308. - return Ok(());
  1309. - }
  1310. -
  1311. - if core::mem::size_of::<T>() == 0 {
  1312. - // The capacity is already `usize::MAX` for SZTs, we can't go higher.
  1313. - return Err(AllocError);
  1314. - }
  1315. -
  1316. - // We know cap is <= `isize::MAX` because `Layout::array` fails if the resulting byte size
  1317. - // is greater than `isize::MAX`. So the multiplication by two won't overflow.
  1318. - let new_cap = core::cmp::max(cap * 2, len.checked_add(additional).ok_or(AllocError)?);
  1319. - let layout = core::alloc::Layout::array::<T>(new_cap).map_err(|_| AllocError)?;
  1320. -
  1321. - let (old_ptr, len, cap) = destructure(self);
  1322. -
  1323. - // We need to make sure that `ptr` is either NULL or comes from a previous call to
  1324. - // `krealloc_aligned`. A `Vec<T>`'s `ptr` value is not guaranteed to be NULL and might be
  1325. - // dangling after being created with `Vec::new`. Instead, we can rely on `Vec<T>`'s capacity
  1326. - // to be zero if no memory has been allocated yet.
  1327. - let ptr = if cap == 0 {
  1328. - core::ptr::null_mut()
  1329. - } else {
  1330. - old_ptr
  1331. - };
  1332. -
  1333. - // SAFETY: `ptr` is valid because it's either NULL or comes from a previous call to
  1334. - // `krealloc_aligned`. We also verified that the type is not a ZST.
  1335. - let new_ptr = unsafe { super::allocator::krealloc_aligned(ptr.cast(), layout, flags) };
  1336. - if new_ptr.is_null() {
  1337. - // SAFETY: We are just rebuilding the existing `Vec` with no changes.
  1338. - unsafe { rebuild(self, old_ptr, len, cap) };
  1339. - Err(AllocError)
  1340. - } else {
  1341. - // SAFETY: `ptr` has been reallocated with the layout for `new_cap` elements. New cap
  1342. - // is greater than `cap`, so it continues to be >= `len`.
  1343. - unsafe { rebuild(self, new_ptr.cast::<T>(), len, new_cap) };
  1344. - Ok(())
  1345. - }
  1346. - }
  1347. -}
  1348. -
  1349. -#[cfg(not(any(test, testlib)))]
  1350. -fn destructure<T>(v: &mut Vec<T>) -> (*mut T, usize, usize) {
  1351. - let mut tmp = Vec::new();
  1352. - core::mem::swap(&mut tmp, v);
  1353. - let mut tmp = core::mem::ManuallyDrop::new(tmp);
  1354. - let len = tmp.len();
  1355. - let cap = tmp.capacity();
  1356. - (tmp.as_mut_ptr(), len, cap)
  1357. -}
  1358. -
  1359. -/// Rebuilds a `Vec` from a pointer, length, and capacity.
  1360. -///
  1361. -/// # Safety
  1362. -///
  1363. -/// The same as [`Vec::from_raw_parts`].
  1364. -#[cfg(not(any(test, testlib)))]
  1365. -unsafe fn rebuild<T>(v: &mut Vec<T>, ptr: *mut T, len: usize, cap: usize) {
  1366. - // SAFETY: The safety requirements from this function satisfy those of `from_raw_parts`.
  1367. - let mut tmp = unsafe { Vec::from_raw_parts(ptr, len, cap) };
  1368. - core::mem::swap(&mut tmp, v);
  1369. -}
  1370. diff --git a/rust/kernel/build_assert.rs b/rust/kernel/build_assert.rs
  1371. deleted file mode 100644
  1372. --- a/rust/kernel/build_assert.rs
  1373. +++ /dev/null
  1374. @@ -1,84 +0,0 @@
  1375. -// SPDX-License-Identifier: GPL-2.0
  1376. -
  1377. -//! Build-time assert.
  1378. -
  1379. -/// Fails the build if the code path calling `build_error!` can possibly be executed.
  1380. -///
  1381. -/// If the macro is executed in const context, `build_error!` will panic.
  1382. -/// If the compiler or optimizer cannot guarantee that `build_error!` can never
  1383. -/// be called, a build error will be triggered.
  1384. -///
  1385. -/// # Examples
  1386. -///
  1387. -/// ```
  1388. -/// # use kernel::build_error;
  1389. -/// #[inline]
  1390. -/// fn foo(a: usize) -> usize {
  1391. -/// a.checked_add(1).unwrap_or_else(|| build_error!("overflow"))
  1392. -/// }
  1393. -///
  1394. -/// assert_eq!(foo(usize::MAX - 1), usize::MAX); // OK.
  1395. -/// // foo(usize::MAX); // Fails to compile.
  1396. -/// ```
  1397. -#[macro_export]
  1398. -macro_rules! build_error {
  1399. - () => {{
  1400. - $crate::build_error("")
  1401. - }};
  1402. - ($msg:expr) => {{
  1403. - $crate::build_error($msg)
  1404. - }};
  1405. -}
  1406. -
  1407. -/// Asserts that a boolean expression is `true` at compile time.
  1408. -///
  1409. -/// If the condition is evaluated to `false` in const context, `build_assert!`
  1410. -/// will panic. If the compiler or optimizer cannot guarantee the condition will
  1411. -/// be evaluated to `true`, a build error will be triggered.
  1412. -///
  1413. -/// [`static_assert!`] should be preferred to `build_assert!` whenever possible.
  1414. -///
  1415. -/// # Examples
  1416. -///
  1417. -/// These examples show that different types of [`assert!`] will trigger errors
  1418. -/// at different stage of compilation. It is preferred to err as early as
  1419. -/// possible, so [`static_assert!`] should be used whenever possible.
  1420. -/// ```ignore
  1421. -/// fn foo() {
  1422. -/// static_assert!(1 > 1); // Compile-time error
  1423. -/// build_assert!(1 > 1); // Build-time error
  1424. -/// assert!(1 > 1); // Run-time error
  1425. -/// }
  1426. -/// ```
  1427. -///
  1428. -/// When the condition refers to generic parameters or parameters of an inline function,
  1429. -/// [`static_assert!`] cannot be used. Use `build_assert!` in this scenario.
  1430. -/// ```
  1431. -/// fn foo<const N: usize>() {
  1432. -/// // `static_assert!(N > 1);` is not allowed
  1433. -/// build_assert!(N > 1); // Build-time check
  1434. -/// assert!(N > 1); // Run-time check
  1435. -/// }
  1436. -///
  1437. -/// #[inline]
  1438. -/// fn bar(n: usize) {
  1439. -/// // `static_assert!(n > 1);` is not allowed
  1440. -/// build_assert!(n > 1); // Build-time check
  1441. -/// assert!(n > 1); // Run-time check
  1442. -/// }
  1443. -/// ```
  1444. -///
  1445. -/// [`static_assert!`]: crate::static_assert!
  1446. -#[macro_export]
  1447. -macro_rules! build_assert {
  1448. - ($cond:expr $(,)?) => {{
  1449. - if !$cond {
  1450. - $crate::build_error(concat!("assertion failed: ", stringify!($cond)));
  1451. - }
  1452. - }};
  1453. - ($cond:expr, $msg:expr) => {{
  1454. - if !$cond {
  1455. - $crate::build_error($msg);
  1456. - }
  1457. - }};
  1458. -}
  1459. diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
  1460. deleted file mode 100644
  1461. --- a/rust/kernel/error.rs
  1462. +++ /dev/null
  1463. @@ -1,327 +0,0 @@
  1464. -// SPDX-License-Identifier: GPL-2.0
  1465. -
  1466. -//! Kernel errors.
  1467. -//!
  1468. -//! C header: [`include/uapi/asm-generic/errno-base.h`](srctree/include/uapi/asm-generic/errno-base.h)
  1469. -
  1470. -use crate::{alloc::AllocError, str::CStr};
  1471. -
  1472. -use alloc::alloc::LayoutError;
  1473. -
  1474. -use core::fmt;
  1475. -use core::num::TryFromIntError;
  1476. -use core::str::Utf8Error;
  1477. -
  1478. -/// Contains the C-compatible error codes.
  1479. -#[rustfmt::skip]
  1480. -pub mod code {
  1481. - macro_rules! declare_err {
  1482. - ($err:tt $(,)? $($doc:expr),+) => {
  1483. - $(
  1484. - #[doc = $doc]
  1485. - )*
  1486. - pub const $err: super::Error = super::Error(-(crate::bindings::$err as i32));
  1487. - };
  1488. - }
  1489. -
  1490. - declare_err!(EPERM, "Operation not permitted.");
  1491. - declare_err!(ENOENT, "No such file or directory.");
  1492. - declare_err!(ESRCH, "No such process.");
  1493. - declare_err!(EINTR, "Interrupted system call.");
  1494. - declare_err!(EIO, "I/O error.");
  1495. - declare_err!(ENXIO, "No such device or address.");
  1496. - declare_err!(E2BIG, "Argument list too long.");
  1497. - declare_err!(ENOEXEC, "Exec format error.");
  1498. - declare_err!(EBADF, "Bad file number.");
  1499. - declare_err!(ECHILD, "No child processes.");
  1500. - declare_err!(EAGAIN, "Try again.");
  1501. - declare_err!(ENOMEM, "Out of memory.");
  1502. - declare_err!(EACCES, "Permission denied.");
  1503. - declare_err!(EFAULT, "Bad address.");
  1504. - declare_err!(ENOTBLK, "Block device required.");
  1505. - declare_err!(EBUSY, "Device or resource busy.");
  1506. - declare_err!(EEXIST, "File exists.");
  1507. - declare_err!(EXDEV, "Cross-device link.");
  1508. - declare_err!(ENODEV, "No such device.");
  1509. - declare_err!(ENOTDIR, "Not a directory.");
  1510. - declare_err!(EISDIR, "Is a directory.");
  1511. - declare_err!(EINVAL, "Invalid argument.");
  1512. - declare_err!(ENFILE, "File table overflow.");
  1513. - declare_err!(EMFILE, "Too many open files.");
  1514. - declare_err!(ENOTTY, "Not a typewriter.");
  1515. - declare_err!(ETXTBSY, "Text file busy.");
  1516. - declare_err!(EFBIG, "File too large.");
  1517. - declare_err!(ENOSPC, "No space left on device.");
  1518. - declare_err!(ESPIPE, "Illegal seek.");
  1519. - declare_err!(EROFS, "Read-only file system.");
  1520. - declare_err!(EMLINK, "Too many links.");
  1521. - declare_err!(EPIPE, "Broken pipe.");
  1522. - declare_err!(EDOM, "Math argument out of domain of func.");
  1523. - declare_err!(ERANGE, "Math result not representable.");
  1524. - declare_err!(ERESTARTSYS, "Restart the system call.");
  1525. - declare_err!(ERESTARTNOINTR, "System call was interrupted by a signal and will be restarted.");
  1526. - declare_err!(ERESTARTNOHAND, "Restart if no handler.");
  1527. - declare_err!(ENOIOCTLCMD, "No ioctl command.");
  1528. - declare_err!(ERESTART_RESTARTBLOCK, "Restart by calling sys_restart_syscall.");
  1529. - declare_err!(EPROBE_DEFER, "Driver requests probe retry.");
  1530. - declare_err!(EOPENSTALE, "Open found a stale dentry.");
  1531. - declare_err!(ENOPARAM, "Parameter not supported.");
  1532. - declare_err!(EBADHANDLE, "Illegal NFS file handle.");
  1533. - declare_err!(ENOTSYNC, "Update synchronization mismatch.");
  1534. - declare_err!(EBADCOOKIE, "Cookie is stale.");
  1535. - declare_err!(ENOTSUPP, "Operation is not supported.");
  1536. - declare_err!(ETOOSMALL, "Buffer or request is too small.");
  1537. - declare_err!(ESERVERFAULT, "An untranslatable error occurred.");
  1538. - declare_err!(EBADTYPE, "Type not supported by server.");
  1539. - declare_err!(EJUKEBOX, "Request initiated, but will not complete before timeout.");
  1540. - declare_err!(EIOCBQUEUED, "iocb queued, will get completion event.");
  1541. - declare_err!(ERECALLCONFLICT, "Conflict with recalled state.");
  1542. - declare_err!(ENOGRACE, "NFS file lock reclaim refused.");
  1543. -}
  1544. -
  1545. -/// Generic integer kernel error.
  1546. -///
  1547. -/// The kernel defines a set of integer generic error codes based on C and
  1548. -/// POSIX ones. These codes may have a more specific meaning in some contexts.
  1549. -///
  1550. -/// # Invariants
  1551. -///
  1552. -/// The value is a valid `errno` (i.e. `>= -MAX_ERRNO && < 0`).
  1553. -#[derive(Clone, Copy, PartialEq, Eq)]
  1554. -pub struct Error(core::ffi::c_int);
  1555. -
  1556. -impl Error {
  1557. - /// Creates an [`Error`] from a kernel error code.
  1558. - ///
  1559. - /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
  1560. - /// be returned in such a case.
  1561. - pub(crate) fn from_errno(errno: core::ffi::c_int) -> Error {
  1562. - if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
  1563. - // TODO: Make it a `WARN_ONCE` once available.
  1564. - crate::pr_warn!(
  1565. - "attempted to create `Error` with out of range `errno`: {}",
  1566. - errno
  1567. - );
  1568. - return code::EINVAL;
  1569. - }
  1570. -
  1571. - // INVARIANT: The check above ensures the type invariant
  1572. - // will hold.
  1573. - Error(errno)
  1574. - }
  1575. -
  1576. - /// Creates an [`Error`] from a kernel error code.
  1577. - ///
  1578. - /// # Safety
  1579. - ///
  1580. - /// `errno` must be within error code range (i.e. `>= -MAX_ERRNO && < 0`).
  1581. - unsafe fn from_errno_unchecked(errno: core::ffi::c_int) -> Error {
  1582. - // INVARIANT: The contract ensures the type invariant
  1583. - // will hold.
  1584. - Error(errno)
  1585. - }
  1586. -
  1587. - /// Returns the kernel error code.
  1588. - pub fn to_errno(self) -> core::ffi::c_int {
  1589. - self.0
  1590. - }
  1591. -
  1592. - /// Returns the error encoded as a pointer.
  1593. - #[allow(dead_code)]
  1594. - pub(crate) fn to_ptr<T>(self) -> *mut T {
  1595. - // SAFETY: `self.0` is a valid error due to its invariant.
  1596. - unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ }
  1597. - }
  1598. -
  1599. - /// Returns a string representing the error, if one exists.
  1600. - #[cfg(not(testlib))]
  1601. - pub fn name(&self) -> Option<&'static CStr> {
  1602. - // SAFETY: Just an FFI call, there are no extra safety requirements.
  1603. - let ptr = unsafe { bindings::errname(-self.0) };
  1604. - if ptr.is_null() {
  1605. - None
  1606. - } else {
  1607. - // SAFETY: The string returned by `errname` is static and `NUL`-terminated.
  1608. - Some(unsafe { CStr::from_char_ptr(ptr) })
  1609. - }
  1610. - }
  1611. -
  1612. - /// Returns a string representing the error, if one exists.
  1613. - ///
  1614. - /// When `testlib` is configured, this always returns `None` to avoid the dependency on a
  1615. - /// kernel function so that tests that use this (e.g., by calling [`Result::unwrap`]) can still
  1616. - /// run in userspace.
  1617. - #[cfg(testlib)]
  1618. - pub fn name(&self) -> Option<&'static CStr> {
  1619. - None
  1620. - }
  1621. -}
  1622. -
  1623. -impl fmt::Debug for Error {
  1624. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  1625. - match self.name() {
  1626. - // Print out number if no name can be found.
  1627. - None => f.debug_tuple("Error").field(&-self.0).finish(),
  1628. - // SAFETY: These strings are ASCII-only.
  1629. - Some(name) => f
  1630. - .debug_tuple(unsafe { core::str::from_utf8_unchecked(name) })
  1631. - .finish(),
  1632. - }
  1633. - }
  1634. -}
  1635. -
  1636. -impl From<AllocError> for Error {
  1637. - fn from(_: AllocError) -> Error {
  1638. - code::ENOMEM
  1639. - }
  1640. -}
  1641. -
  1642. -impl From<TryFromIntError> for Error {
  1643. - fn from(_: TryFromIntError) -> Error {
  1644. - code::EINVAL
  1645. - }
  1646. -}
  1647. -
  1648. -impl From<Utf8Error> for Error {
  1649. - fn from(_: Utf8Error) -> Error {
  1650. - code::EINVAL
  1651. - }
  1652. -}
  1653. -
  1654. -impl From<LayoutError> for Error {
  1655. - fn from(_: LayoutError) -> Error {
  1656. - code::ENOMEM
  1657. - }
  1658. -}
  1659. -
  1660. -impl From<core::fmt::Error> for Error {
  1661. - fn from(_: core::fmt::Error) -> Error {
  1662. - code::EINVAL
  1663. - }
  1664. -}
  1665. -
  1666. -impl From<core::convert::Infallible> for Error {
  1667. - fn from(e: core::convert::Infallible) -> Error {
  1668. - match e {}
  1669. - }
  1670. -}
  1671. -
  1672. -/// A [`Result`] with an [`Error`] error type.
  1673. -///
  1674. -/// To be used as the return type for functions that may fail.
  1675. -///
  1676. -/// # Error codes in C and Rust
  1677. -///
  1678. -/// In C, it is common that functions indicate success or failure through
  1679. -/// their return value; modifying or returning extra data through non-`const`
  1680. -/// pointer parameters. In particular, in the kernel, functions that may fail
  1681. -/// typically return an `int` that represents a generic error code. We model
  1682. -/// those as [`Error`].
  1683. -///
  1684. -/// In Rust, it is idiomatic to model functions that may fail as returning
  1685. -/// a [`Result`]. Since in the kernel many functions return an error code,
  1686. -/// [`Result`] is a type alias for a [`core::result::Result`] that uses
  1687. -/// [`Error`] as its error type.
  1688. -///
  1689. -/// Note that even if a function does not return anything when it succeeds,
  1690. -/// it should still be modeled as returning a `Result` rather than
  1691. -/// just an [`Error`].
  1692. -pub type Result<T = (), E = Error> = core::result::Result<T, E>;
  1693. -
  1694. -/// Converts an integer as returned by a C kernel function to an error if it's negative, and
  1695. -/// `Ok(())` otherwise.
  1696. -pub fn to_result(err: core::ffi::c_int) -> Result {
  1697. - if err < 0 {
  1698. - Err(Error::from_errno(err))
  1699. - } else {
  1700. - Ok(())
  1701. - }
  1702. -}
  1703. -
  1704. -/// Transform a kernel "error pointer" to a normal pointer.
  1705. -///
  1706. -/// Some kernel C API functions return an "error pointer" which optionally
  1707. -/// embeds an `errno`. Callers are supposed to check the returned pointer
  1708. -/// for errors. This function performs the check and converts the "error pointer"
  1709. -/// to a normal pointer in an idiomatic fashion.
  1710. -///
  1711. -/// # Examples
  1712. -///
  1713. -/// ```ignore
  1714. -/// # use kernel::from_err_ptr;
  1715. -/// # use kernel::bindings;
  1716. -/// fn devm_platform_ioremap_resource(
  1717. -/// pdev: &mut PlatformDevice,
  1718. -/// index: u32,
  1719. -/// ) -> Result<*mut core::ffi::c_void> {
  1720. -/// // SAFETY: `pdev` points to a valid platform device. There are no safety requirements
  1721. -/// // on `index`.
  1722. -/// from_err_ptr(unsafe { bindings::devm_platform_ioremap_resource(pdev.to_ptr(), index) })
  1723. -/// }
  1724. -/// ```
  1725. -// TODO: Remove `dead_code` marker once an in-kernel client is available.
  1726. -#[allow(dead_code)]
  1727. -pub(crate) fn from_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
  1728. - // CAST: Casting a pointer to `*const core::ffi::c_void` is always valid.
  1729. - let const_ptr: *const core::ffi::c_void = ptr.cast();
  1730. - // SAFETY: The FFI function does not deref the pointer.
  1731. - if unsafe { bindings::IS_ERR(const_ptr) } {
  1732. - // SAFETY: The FFI function does not deref the pointer.
  1733. - let err = unsafe { bindings::PTR_ERR(const_ptr) };
  1734. - // CAST: If `IS_ERR()` returns `true`,
  1735. - // then `PTR_ERR()` is guaranteed to return a
  1736. - // negative value greater-or-equal to `-bindings::MAX_ERRNO`,
  1737. - // which always fits in an `i16`, as per the invariant above.
  1738. - // And an `i16` always fits in an `i32`. So casting `err` to
  1739. - // an `i32` can never overflow, and is always valid.
  1740. - //
  1741. - // SAFETY: `IS_ERR()` ensures `err` is a
  1742. - // negative value greater-or-equal to `-bindings::MAX_ERRNO`.
  1743. - #[allow(clippy::unnecessary_cast)]
  1744. - return Err(unsafe { Error::from_errno_unchecked(err as core::ffi::c_int) });
  1745. - }
  1746. - Ok(ptr)
  1747. -}
  1748. -
  1749. -/// Calls a closure returning a [`crate::error::Result<T>`] and converts the result to
  1750. -/// a C integer result.
  1751. -///
  1752. -/// This is useful when calling Rust functions that return [`crate::error::Result<T>`]
  1753. -/// from inside `extern "C"` functions that need to return an integer error result.
  1754. -///
  1755. -/// `T` should be convertible from an `i16` via `From<i16>`.
  1756. -///
  1757. -/// # Examples
  1758. -///
  1759. -/// ```ignore
  1760. -/// # use kernel::from_result;
  1761. -/// # use kernel::bindings;
  1762. -/// unsafe extern "C" fn probe_callback(
  1763. -/// pdev: *mut bindings::platform_device,
  1764. -/// ) -> core::ffi::c_int {
  1765. -/// from_result(|| {
  1766. -/// let ptr = devm_alloc(pdev)?;
  1767. -/// bindings::platform_set_drvdata(pdev, ptr);
  1768. -/// Ok(0)
  1769. -/// })
  1770. -/// }
  1771. -/// ```
  1772. -// TODO: Remove `dead_code` marker once an in-kernel client is available.
  1773. -#[allow(dead_code)]
  1774. -pub(crate) fn from_result<T, F>(f: F) -> T
  1775. -where
  1776. - T: From<i16>,
  1777. - F: FnOnce() -> Result<T>,
  1778. -{
  1779. - match f() {
  1780. - Ok(v) => v,
  1781. - // NO-OVERFLOW: negative `errno`s are no smaller than `-bindings::MAX_ERRNO`,
  1782. - // `-bindings::MAX_ERRNO` fits in an `i16` as per invariant above,
  1783. - // therefore a negative `errno` always fits in an `i16` and will not overflow.
  1784. - Err(e) => T::from(e.to_errno() as i16),
  1785. - }
  1786. -}
  1787. -
  1788. -/// Error message for calling a default function of a [`#[vtable]`](macros::vtable) trait.
  1789. -pub const VTABLE_DEFAULT_ERROR: &str =
  1790. - "This function must not be called, see the #[vtable] documentation.";
  1791. diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs
  1792. deleted file mode 100644
  1793. --- a/rust/kernel/init.rs
  1794. +++ /dev/null
  1795. @@ -1,1352 +0,0 @@
  1796. -// SPDX-License-Identifier: Apache-2.0 OR MIT
  1797. -
  1798. -//! API to safely and fallibly initialize pinned `struct`s using in-place constructors.
  1799. -//!
  1800. -//! It also allows in-place initialization of big `struct`s that would otherwise produce a stack
  1801. -//! overflow.
  1802. -//!
  1803. -//! Most `struct`s from the [`sync`] module need to be pinned, because they contain self-referential
  1804. -//! `struct`s from C. [Pinning][pinning] is Rust's way of ensuring data does not move.
  1805. -//!
  1806. -//! # Overview
  1807. -//!
  1808. -//! To initialize a `struct` with an in-place constructor you will need two things:
  1809. -//! - an in-place constructor,
  1810. -//! - a memory location that can hold your `struct` (this can be the [stack], an [`Arc<T>`],
  1811. -//! [`UniqueArc<T>`], [`Box<T>`] or any other smart pointer that implements [`InPlaceInit`]).
  1812. -//!
  1813. -//! To get an in-place constructor there are generally three options:
  1814. -//! - directly creating an in-place constructor using the [`pin_init!`] macro,
  1815. -//! - a custom function/macro returning an in-place constructor provided by someone else,
  1816. -//! - using the unsafe function [`pin_init_from_closure()`] to manually create an initializer.
  1817. -//!
  1818. -//! Aside from pinned initialization, this API also supports in-place construction without pinning,
  1819. -//! the macros/types/functions are generally named like the pinned variants without the `pin`
  1820. -//! prefix.
  1821. -//!
  1822. -//! # Examples
  1823. -//!
  1824. -//! ## Using the [`pin_init!`] macro
  1825. -//!
  1826. -//! If you want to use [`PinInit`], then you will have to annotate your `struct` with
  1827. -//! `#[`[`pin_data`]`]`. It is a macro that uses `#[pin]` as a marker for
  1828. -//! [structurally pinned fields]. After doing this, you can then create an in-place constructor via
  1829. -//! [`pin_init!`]. The syntax is almost the same as normal `struct` initializers. The difference is
  1830. -//! that you need to write `<-` instead of `:` for fields that you want to initialize in-place.
  1831. -//!
  1832. -//! ```rust
  1833. -//! # #![allow(clippy::disallowed_names)]
  1834. -//! use kernel::sync::{new_mutex, Mutex};
  1835. -//! # use core::pin::Pin;
  1836. -//! #[pin_data]
  1837. -//! struct Foo {
  1838. -//! #[pin]
  1839. -//! a: Mutex<usize>,
  1840. -//! b: u32,
  1841. -//! }
  1842. -//!
  1843. -//! let foo = pin_init!(Foo {
  1844. -//! a <- new_mutex!(42, "Foo::a"),
  1845. -//! b: 24,
  1846. -//! });
  1847. -//! ```
  1848. -//!
  1849. -//! `foo` now is of the type [`impl PinInit<Foo>`]. We can now use any smart pointer that we like
  1850. -//! (or just the stack) to actually initialize a `Foo`:
  1851. -//!
  1852. -//! ```rust
  1853. -//! # #![allow(clippy::disallowed_names)]
  1854. -//! # use kernel::sync::{new_mutex, Mutex};
  1855. -//! # use core::pin::Pin;
  1856. -//! # #[pin_data]
  1857. -//! # struct Foo {
  1858. -//! # #[pin]
  1859. -//! # a: Mutex<usize>,
  1860. -//! # b: u32,
  1861. -//! # }
  1862. -//! # let foo = pin_init!(Foo {
  1863. -//! # a <- new_mutex!(42, "Foo::a"),
  1864. -//! # b: 24,
  1865. -//! # });
  1866. -//! let foo: Result<Pin<Box<Foo>>> = Box::pin_init(foo, GFP_KERNEL);
  1867. -//! ```
  1868. -//!
  1869. -//! For more information see the [`pin_init!`] macro.
  1870. -//!
  1871. -//! ## Using a custom function/macro that returns an initializer
  1872. -//!
  1873. -//! Many types from the kernel supply a function/macro that returns an initializer, because the
  1874. -//! above method only works for types where you can access the fields.
  1875. -//!
  1876. -//! ```rust
  1877. -//! # use kernel::sync::{new_mutex, Arc, Mutex};
  1878. -//! let mtx: Result<Arc<Mutex<usize>>> =
  1879. -//! Arc::pin_init(new_mutex!(42, "example::mtx"), GFP_KERNEL);
  1880. -//! ```
  1881. -//!
  1882. -//! To declare an init macro/function you just return an [`impl PinInit<T, E>`]:
  1883. -//!
  1884. -//! ```rust
  1885. -//! # #![allow(clippy::disallowed_names)]
  1886. -//! # use kernel::{sync::Mutex, new_mutex, init::PinInit, try_pin_init};
  1887. -//! #[pin_data]
  1888. -//! struct DriverData {
  1889. -//! #[pin]
  1890. -//! status: Mutex<i32>,
  1891. -//! buffer: Box<[u8; 1_000_000]>,
  1892. -//! }
  1893. -//!
  1894. -//! impl DriverData {
  1895. -//! fn new() -> impl PinInit<Self, Error> {
  1896. -//! try_pin_init!(Self {
  1897. -//! status <- new_mutex!(0, "DriverData::status"),
  1898. -//! buffer: Box::init(kernel::init::zeroed(), GFP_KERNEL)?,
  1899. -//! })
  1900. -//! }
  1901. -//! }
  1902. -//! ```
  1903. -//!
  1904. -//! ## Manual creation of an initializer
  1905. -//!
  1906. -//! Often when working with primitives the previous approaches are not sufficient. That is where
  1907. -//! [`pin_init_from_closure()`] comes in. This `unsafe` function allows you to create a
  1908. -//! [`impl PinInit<T, E>`] directly from a closure. Of course you have to ensure that the closure
  1909. -//! actually does the initialization in the correct way. Here are the things to look out for
  1910. -//! (we are calling the parameter to the closure `slot`):
  1911. -//! - when the closure returns `Ok(())`, then it has completed the initialization successfully, so
  1912. -//! `slot` now contains a valid bit pattern for the type `T`,
  1913. -//! - when the closure returns `Err(e)`, then the caller may deallocate the memory at `slot`, so
  1914. -//! you need to take care to clean up anything if your initialization fails mid-way,
  1915. -//! - you may assume that `slot` will stay pinned even after the closure returns until `drop` of
  1916. -//! `slot` gets called.
  1917. -//!
  1918. -//! ```rust
  1919. -//! # #![allow(unreachable_pub, clippy::disallowed_names)]
  1920. -//! use kernel::{init, types::Opaque};
  1921. -//! use core::{ptr::addr_of_mut, marker::PhantomPinned, pin::Pin};
  1922. -//! # mod bindings {
  1923. -//! # #![allow(non_camel_case_types)]
  1924. -//! # pub struct foo;
  1925. -//! # pub unsafe fn init_foo(_ptr: *mut foo) {}
  1926. -//! # pub unsafe fn destroy_foo(_ptr: *mut foo) {}
  1927. -//! # pub unsafe fn enable_foo(_ptr: *mut foo, _flags: u32) -> i32 { 0 }
  1928. -//! # }
  1929. -//! # // `Error::from_errno` is `pub(crate)` in the `kernel` crate, thus provide a workaround.
  1930. -//! # trait FromErrno {
  1931. -//! # fn from_errno(errno: core::ffi::c_int) -> Error {
  1932. -//! # // Dummy error that can be constructed outside the `kernel` crate.
  1933. -//! # Error::from(core::fmt::Error)
  1934. -//! # }
  1935. -//! # }
  1936. -//! # impl FromErrno for Error {}
  1937. -//! /// # Invariants
  1938. -//! ///
  1939. -//! /// `foo` is always initialized
  1940. -//! #[pin_data(PinnedDrop)]
  1941. -//! pub struct RawFoo {
  1942. -//! #[pin]
  1943. -//! foo: Opaque<bindings::foo>,
  1944. -//! #[pin]
  1945. -//! _p: PhantomPinned,
  1946. -//! }
  1947. -//!
  1948. -//! impl RawFoo {
  1949. -//! pub fn new(flags: u32) -> impl PinInit<Self, Error> {
  1950. -//! // SAFETY:
  1951. -//! // - when the closure returns `Ok(())`, then it has successfully initialized and
  1952. -//! // enabled `foo`,
  1953. -//! // - when it returns `Err(e)`, then it has cleaned up before
  1954. -//! unsafe {
  1955. -//! init::pin_init_from_closure(move |slot: *mut Self| {
  1956. -//! // `slot` contains uninit memory, avoid creating a reference.
  1957. -//! let foo = addr_of_mut!((*slot).foo);
  1958. -//!
  1959. -//! // Initialize the `foo`
  1960. -//! bindings::init_foo(Opaque::raw_get(foo));
  1961. -//!
  1962. -//! // Try to enable it.
  1963. -//! let err = bindings::enable_foo(Opaque::raw_get(foo), flags);
  1964. -//! if err != 0 {
  1965. -//! // Enabling has failed, first clean up the foo and then return the error.
  1966. -//! bindings::destroy_foo(Opaque::raw_get(foo));
  1967. -//! return Err(Error::from_errno(err));
  1968. -//! }
  1969. -//!
  1970. -//! // All fields of `RawFoo` have been initialized, since `_p` is a ZST.
  1971. -//! Ok(())
  1972. -//! })
  1973. -//! }
  1974. -//! }
  1975. -//! }
  1976. -//!
  1977. -//! #[pinned_drop]
  1978. -//! impl PinnedDrop for RawFoo {
  1979. -//! fn drop(self: Pin<&mut Self>) {
  1980. -//! // SAFETY: Since `foo` is initialized, destroying is safe.
  1981. -//! unsafe { bindings::destroy_foo(self.foo.get()) };
  1982. -//! }
  1983. -//! }
  1984. -//! ```
  1985. -//!
  1986. -//! For the special case where initializing a field is a single FFI-function call that cannot fail,
  1987. -//! there exist the helper function [`Opaque::ffi_init`]. This function initialize a single
  1988. -//! [`Opaque`] field by just delegating to the supplied closure. You can use these in combination
  1989. -//! with [`pin_init!`].
  1990. -//!
  1991. -//! For more information on how to use [`pin_init_from_closure()`], take a look at the uses inside
  1992. -//! the `kernel` crate. The [`sync`] module is a good starting point.
  1993. -//!
  1994. -//! [`sync`]: kernel::sync
  1995. -//! [pinning]: https://doc.rust-lang.org/std/pin/index.html
  1996. -//! [structurally pinned fields]:
  1997. -//! https://doc.rust-lang.org/std/pin/index.html#pinning-is-structural-for-field
  1998. -//! [stack]: crate::stack_pin_init
  1999. -//! [`Arc<T>`]: crate::sync::Arc
  2000. -//! [`impl PinInit<Foo>`]: PinInit
  2001. -//! [`impl PinInit<T, E>`]: PinInit
  2002. -//! [`impl Init<T, E>`]: Init
  2003. -//! [`Opaque`]: kernel::types::Opaque
  2004. -//! [`Opaque::ffi_init`]: kernel::types::Opaque::ffi_init
  2005. -//! [`pin_data`]: ::macros::pin_data
  2006. -//! [`pin_init!`]: crate::pin_init!
  2007. -
  2008. -use crate::{
  2009. - alloc::{box_ext::BoxExt, AllocError, Flags},
  2010. - error::{self, Error},
  2011. - sync::UniqueArc,
  2012. - types::{Opaque, ScopeGuard},
  2013. -};
  2014. -use alloc::boxed::Box;
  2015. -use core::{
  2016. - cell::UnsafeCell,
  2017. - convert::Infallible,
  2018. - marker::PhantomData,
  2019. - mem::MaybeUninit,
  2020. - num::*,
  2021. - pin::Pin,
  2022. - ptr::{self, NonNull},
  2023. -};
  2024. -
  2025. -#[doc(hidden)]
  2026. -pub mod __internal;
  2027. -#[doc(hidden)]
  2028. -pub mod macros;
  2029. -
  2030. -/// Initialize and pin a type directly on the stack.
  2031. -///
  2032. -/// # Examples
  2033. -///
  2034. -/// ```rust
  2035. -/// # #![allow(clippy::disallowed_names)]
  2036. -/// # use kernel::{init, macros::pin_data, pin_init, stack_pin_init, init::*, sync::Mutex, new_mutex};
  2037. -/// # use core::pin::Pin;
  2038. -/// #[pin_data]
  2039. -/// struct Foo {
  2040. -/// #[pin]
  2041. -/// a: Mutex<usize>,
  2042. -/// b: Bar,
  2043. -/// }
  2044. -///
  2045. -/// #[pin_data]
  2046. -/// struct Bar {
  2047. -/// x: u32,
  2048. -/// }
  2049. -///
  2050. -/// stack_pin_init!(let foo = pin_init!(Foo {
  2051. -/// a <- new_mutex!(42),
  2052. -/// b: Bar {
  2053. -/// x: 64,
  2054. -/// },
  2055. -/// }));
  2056. -/// let foo: Pin<&mut Foo> = foo;
  2057. -/// pr_info!("a: {}", &*foo.a.lock());
  2058. -/// ```
  2059. -///
  2060. -/// # Syntax
  2061. -///
  2062. -/// A normal `let` binding with optional type annotation. The expression is expected to implement
  2063. -/// [`PinInit`]/[`Init`] with the error type [`Infallible`]. If you want to use a different error
  2064. -/// type, then use [`stack_try_pin_init!`].
  2065. -///
  2066. -/// [`stack_try_pin_init!`]: crate::stack_try_pin_init!
  2067. -#[macro_export]
  2068. -macro_rules! stack_pin_init {
  2069. - (let $var:ident $(: $t:ty)? = $val:expr) => {
  2070. - let val = $val;
  2071. - let mut $var = ::core::pin::pin!($crate::init::__internal::StackInit$(::<$t>)?::uninit());
  2072. - let mut $var = match $crate::init::__internal::StackInit::init($var, val) {
  2073. - Ok(res) => res,
  2074. - Err(x) => {
  2075. - let x: ::core::convert::Infallible = x;
  2076. - match x {}
  2077. - }
  2078. - };
  2079. - };
  2080. -}
  2081. -
  2082. -/// Initialize and pin a type directly on the stack.
  2083. -///
  2084. -/// # Examples
  2085. -///
  2086. -/// ```rust,ignore
  2087. -/// # #![allow(clippy::disallowed_names)]
  2088. -/// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex};
  2089. -/// # use macros::pin_data;
  2090. -/// # use core::{alloc::AllocError, pin::Pin};
  2091. -/// #[pin_data]
  2092. -/// struct Foo {
  2093. -/// #[pin]
  2094. -/// a: Mutex<usize>,
  2095. -/// b: Box<Bar>,
  2096. -/// }
  2097. -///
  2098. -/// struct Bar {
  2099. -/// x: u32,
  2100. -/// }
  2101. -///
  2102. -/// stack_try_pin_init!(let foo: Result<Pin<&mut Foo>, AllocError> = pin_init!(Foo {
  2103. -/// a <- new_mutex!(42),
  2104. -/// b: Box::new(Bar {
  2105. -/// x: 64,
  2106. -/// }, GFP_KERNEL)?,
  2107. -/// }));
  2108. -/// let foo = foo.unwrap();
  2109. -/// pr_info!("a: {}", &*foo.a.lock());
  2110. -/// ```
  2111. -///
  2112. -/// ```rust,ignore
  2113. -/// # #![allow(clippy::disallowed_names)]
  2114. -/// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex};
  2115. -/// # use macros::pin_data;
  2116. -/// # use core::{alloc::AllocError, pin::Pin};
  2117. -/// #[pin_data]
  2118. -/// struct Foo {
  2119. -/// #[pin]
  2120. -/// a: Mutex<usize>,
  2121. -/// b: Box<Bar>,
  2122. -/// }
  2123. -///
  2124. -/// struct Bar {
  2125. -/// x: u32,
  2126. -/// }
  2127. -///
  2128. -/// stack_try_pin_init!(let foo: Pin<&mut Foo> =? pin_init!(Foo {
  2129. -/// a <- new_mutex!(42),
  2130. -/// b: Box::new(Bar {
  2131. -/// x: 64,
  2132. -/// }, GFP_KERNEL)?,
  2133. -/// }));
  2134. -/// pr_info!("a: {}", &*foo.a.lock());
  2135. -/// # Ok::<_, AllocError>(())
  2136. -/// ```
  2137. -///
  2138. -/// # Syntax
  2139. -///
  2140. -/// A normal `let` binding with optional type annotation. The expression is expected to implement
  2141. -/// [`PinInit`]/[`Init`]. This macro assigns a result to the given variable, adding a `?` after the
  2142. -/// `=` will propagate this error.
  2143. -#[macro_export]
  2144. -macro_rules! stack_try_pin_init {
  2145. - (let $var:ident $(: $t:ty)? = $val:expr) => {
  2146. - let val = $val;
  2147. - let mut $var = ::core::pin::pin!($crate::init::__internal::StackInit$(::<$t>)?::uninit());
  2148. - let mut $var = $crate::init::__internal::StackInit::init($var, val);
  2149. - };
  2150. - (let $var:ident $(: $t:ty)? =? $val:expr) => {
  2151. - let val = $val;
  2152. - let mut $var = ::core::pin::pin!($crate::init::__internal::StackInit$(::<$t>)?::uninit());
  2153. - let mut $var = $crate::init::__internal::StackInit::init($var, val)?;
  2154. - };
  2155. -}
  2156. -
  2157. -/// Construct an in-place, pinned initializer for `struct`s.
  2158. -///
  2159. -/// This macro defaults the error to [`Infallible`]. If you need [`Error`], then use
  2160. -/// [`try_pin_init!`].
  2161. -///
  2162. -/// The syntax is almost identical to that of a normal `struct` initializer:
  2163. -///
  2164. -/// ```rust
  2165. -/// # #![allow(clippy::disallowed_names)]
  2166. -/// # use kernel::{init, pin_init, macros::pin_data, init::*};
  2167. -/// # use core::pin::Pin;
  2168. -/// #[pin_data]
  2169. -/// struct Foo {
  2170. -/// a: usize,
  2171. -/// b: Bar,
  2172. -/// }
  2173. -///
  2174. -/// #[pin_data]
  2175. -/// struct Bar {
  2176. -/// x: u32,
  2177. -/// }
  2178. -///
  2179. -/// # fn demo() -> impl PinInit<Foo> {
  2180. -/// let a = 42;
  2181. -///
  2182. -/// let initializer = pin_init!(Foo {
  2183. -/// a,
  2184. -/// b: Bar {
  2185. -/// x: 64,
  2186. -/// },
  2187. -/// });
  2188. -/// # initializer }
  2189. -/// # Box::pin_init(demo(), GFP_KERNEL).unwrap();
  2190. -/// ```
  2191. -///
  2192. -/// Arbitrary Rust expressions can be used to set the value of a variable.
  2193. -///
  2194. -/// The fields are initialized in the order that they appear in the initializer. So it is possible
  2195. -/// to read already initialized fields using raw pointers.
  2196. -///
  2197. -/// IMPORTANT: You are not allowed to create references to fields of the struct inside of the
  2198. -/// initializer.
  2199. -///
  2200. -/// # Init-functions
  2201. -///
  2202. -/// When working with this API it is often desired to let others construct your types without
  2203. -/// giving access to all fields. This is where you would normally write a plain function `new`
  2204. -/// that would return a new instance of your type. With this API that is also possible.
  2205. -/// However, there are a few extra things to keep in mind.
  2206. -///
  2207. -/// To create an initializer function, simply declare it like this:
  2208. -///
  2209. -/// ```rust
  2210. -/// # #![allow(clippy::disallowed_names)]
  2211. -/// # use kernel::{init, pin_init, init::*};
  2212. -/// # use core::pin::Pin;
  2213. -/// # #[pin_data]
  2214. -/// # struct Foo {
  2215. -/// # a: usize,
  2216. -/// # b: Bar,
  2217. -/// # }
  2218. -/// # #[pin_data]
  2219. -/// # struct Bar {
  2220. -/// # x: u32,
  2221. -/// # }
  2222. -/// impl Foo {
  2223. -/// fn new() -> impl PinInit<Self> {
  2224. -/// pin_init!(Self {
  2225. -/// a: 42,
  2226. -/// b: Bar {
  2227. -/// x: 64,
  2228. -/// },
  2229. -/// })
  2230. -/// }
  2231. -/// }
  2232. -/// ```
  2233. -///
  2234. -/// Users of `Foo` can now create it like this:
  2235. -///
  2236. -/// ```rust
  2237. -/// # #![allow(clippy::disallowed_names)]
  2238. -/// # use kernel::{init, pin_init, macros::pin_data, init::*};
  2239. -/// # use core::pin::Pin;
  2240. -/// # #[pin_data]
  2241. -/// # struct Foo {
  2242. -/// # a: usize,
  2243. -/// # b: Bar,
  2244. -/// # }
  2245. -/// # #[pin_data]
  2246. -/// # struct Bar {
  2247. -/// # x: u32,
  2248. -/// # }
  2249. -/// # impl Foo {
  2250. -/// # fn new() -> impl PinInit<Self> {
  2251. -/// # pin_init!(Self {
  2252. -/// # a: 42,
  2253. -/// # b: Bar {
  2254. -/// # x: 64,
  2255. -/// # },
  2256. -/// # })
  2257. -/// # }
  2258. -/// # }
  2259. -/// let foo = Box::pin_init(Foo::new(), GFP_KERNEL);
  2260. -/// ```
  2261. -///
  2262. -/// They can also easily embed it into their own `struct`s:
  2263. -///
  2264. -/// ```rust
  2265. -/// # #![allow(clippy::disallowed_names)]
  2266. -/// # use kernel::{init, pin_init, macros::pin_data, init::*};
  2267. -/// # use core::pin::Pin;
  2268. -/// # #[pin_data]
  2269. -/// # struct Foo {
  2270. -/// # a: usize,
  2271. -/// # b: Bar,
  2272. -/// # }
  2273. -/// # #[pin_data]
  2274. -/// # struct Bar {
  2275. -/// # x: u32,
  2276. -/// # }
  2277. -/// # impl Foo {
  2278. -/// # fn new() -> impl PinInit<Self> {
  2279. -/// # pin_init!(Self {
  2280. -/// # a: 42,
  2281. -/// # b: Bar {
  2282. -/// # x: 64,
  2283. -/// # },
  2284. -/// # })
  2285. -/// # }
  2286. -/// # }
  2287. -/// #[pin_data]
  2288. -/// struct FooContainer {
  2289. -/// #[pin]
  2290. -/// foo1: Foo,
  2291. -/// #[pin]
  2292. -/// foo2: Foo,
  2293. -/// other: u32,
  2294. -/// }
  2295. -///
  2296. -/// impl FooContainer {
  2297. -/// fn new(other: u32) -> impl PinInit<Self> {
  2298. -/// pin_init!(Self {
  2299. -/// foo1 <- Foo::new(),
  2300. -/// foo2 <- Foo::new(),
  2301. -/// other,
  2302. -/// })
  2303. -/// }
  2304. -/// }
  2305. -/// ```
  2306. -///
  2307. -/// Here we see that when using `pin_init!` with `PinInit`, one needs to write `<-` instead of `:`.
  2308. -/// This signifies that the given field is initialized in-place. As with `struct` initializers, just
  2309. -/// writing the field (in this case `other`) without `:` or `<-` means `other: other,`.
  2310. -///
  2311. -/// # Syntax
  2312. -///
  2313. -/// As already mentioned in the examples above, inside of `pin_init!` a `struct` initializer with
  2314. -/// the following modifications is expected:
  2315. -/// - Fields that you want to initialize in-place have to use `<-` instead of `:`.
  2316. -/// - In front of the initializer you can write `&this in` to have access to a [`NonNull<Self>`]
  2317. -/// pointer named `this` inside of the initializer.
  2318. -/// - Using struct update syntax one can place `..Zeroable::zeroed()` at the very end of the
  2319. -/// struct, this initializes every field with 0 and then runs all initializers specified in the
  2320. -/// body. This can only be done if [`Zeroable`] is implemented for the struct.
  2321. -///
  2322. -/// For instance:
  2323. -///
  2324. -/// ```rust
  2325. -/// # use kernel::{macros::{Zeroable, pin_data}, pin_init};
  2326. -/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
  2327. -/// #[pin_data]
  2328. -/// #[derive(Zeroable)]
  2329. -/// struct Buf {
  2330. -/// // `ptr` points into `buf`.
  2331. -/// ptr: *mut u8,
  2332. -/// buf: [u8; 64],
  2333. -/// #[pin]
  2334. -/// pin: PhantomPinned,
  2335. -/// }
  2336. -/// pin_init!(&this in Buf {
  2337. -/// buf: [0; 64],
  2338. -/// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
  2339. -/// pin: PhantomPinned,
  2340. -/// });
  2341. -/// pin_init!(Buf {
  2342. -/// buf: [1; 64],
  2343. -/// ..Zeroable::zeroed()
  2344. -/// });
  2345. -/// ```
  2346. -///
  2347. -/// [`try_pin_init!`]: kernel::try_pin_init
  2348. -/// [`NonNull<Self>`]: core::ptr::NonNull
  2349. -// For a detailed example of how this macro works, see the module documentation of the hidden
  2350. -// module `__internal` inside of `init/__internal.rs`.
  2351. -#[macro_export]
  2352. -macro_rules! pin_init {
  2353. - ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
  2354. - $($fields:tt)*
  2355. - }) => {
  2356. - $crate::__init_internal!(
  2357. - @this($($this)?),
  2358. - @typ($t $(::<$($generics),*>)?),
  2359. - @fields($($fields)*),
  2360. - @error(::core::convert::Infallible),
  2361. - @data(PinData, use_data),
  2362. - @has_data(HasPinData, __pin_data),
  2363. - @construct_closure(pin_init_from_closure),
  2364. - @munch_fields($($fields)*),
  2365. - )
  2366. - };
  2367. -}
  2368. -
  2369. -/// Construct an in-place, fallible pinned initializer for `struct`s.
  2370. -///
  2371. -/// If the initialization can complete without error (or [`Infallible`]), then use [`pin_init!`].
  2372. -///
  2373. -/// You can use the `?` operator or use `return Err(err)` inside the initializer to stop
  2374. -/// initialization and return the error.
  2375. -///
  2376. -/// IMPORTANT: if you have `unsafe` code inside of the initializer you have to ensure that when
  2377. -/// initialization fails, the memory can be safely deallocated without any further modifications.
  2378. -///
  2379. -/// This macro defaults the error to [`Error`].
  2380. -///
  2381. -/// The syntax is identical to [`pin_init!`] with the following exception: you can append `? $type`
  2382. -/// after the `struct` initializer to specify the error type you want to use.
  2383. -///
  2384. -/// # Examples
  2385. -///
  2386. -/// ```rust
  2387. -/// # #![feature(new_uninit)]
  2388. -/// use kernel::{init::{self, PinInit}, error::Error};
  2389. -/// #[pin_data]
  2390. -/// struct BigBuf {
  2391. -/// big: Box<[u8; 1024 * 1024 * 1024]>,
  2392. -/// small: [u8; 1024 * 1024],
  2393. -/// ptr: *mut u8,
  2394. -/// }
  2395. -///
  2396. -/// impl BigBuf {
  2397. -/// fn new() -> impl PinInit<Self, Error> {
  2398. -/// try_pin_init!(Self {
  2399. -/// big: Box::init(init::zeroed(), GFP_KERNEL)?,
  2400. -/// small: [0; 1024 * 1024],
  2401. -/// ptr: core::ptr::null_mut(),
  2402. -/// }? Error)
  2403. -/// }
  2404. -/// }
  2405. -/// ```
  2406. -// For a detailed example of how this macro works, see the module documentation of the hidden
  2407. -// module `__internal` inside of `init/__internal.rs`.
  2408. -#[macro_export]
  2409. -macro_rules! try_pin_init {
  2410. - ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
  2411. - $($fields:tt)*
  2412. - }) => {
  2413. - $crate::__init_internal!(
  2414. - @this($($this)?),
  2415. - @typ($t $(::<$($generics),*>)? ),
  2416. - @fields($($fields)*),
  2417. - @error($crate::error::Error),
  2418. - @data(PinData, use_data),
  2419. - @has_data(HasPinData, __pin_data),
  2420. - @construct_closure(pin_init_from_closure),
  2421. - @munch_fields($($fields)*),
  2422. - )
  2423. - };
  2424. - ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
  2425. - $($fields:tt)*
  2426. - }? $err:ty) => {
  2427. - $crate::__init_internal!(
  2428. - @this($($this)?),
  2429. - @typ($t $(::<$($generics),*>)? ),
  2430. - @fields($($fields)*),
  2431. - @error($err),
  2432. - @data(PinData, use_data),
  2433. - @has_data(HasPinData, __pin_data),
  2434. - @construct_closure(pin_init_from_closure),
  2435. - @munch_fields($($fields)*),
  2436. - )
  2437. - };
  2438. -}
  2439. -
  2440. -/// Construct an in-place initializer for `struct`s.
  2441. -///
  2442. -/// This macro defaults the error to [`Infallible`]. If you need [`Error`], then use
  2443. -/// [`try_init!`].
  2444. -///
  2445. -/// The syntax is identical to [`pin_init!`] and its safety caveats also apply:
  2446. -/// - `unsafe` code must guarantee either full initialization or return an error and allow
  2447. -/// deallocation of the memory.
  2448. -/// - the fields are initialized in the order given in the initializer.
  2449. -/// - no references to fields are allowed to be created inside of the initializer.
  2450. -///
  2451. -/// This initializer is for initializing data in-place that might later be moved. If you want to
  2452. -/// pin-initialize, use [`pin_init!`].
  2453. -///
  2454. -/// [`try_init!`]: crate::try_init!
  2455. -// For a detailed example of how this macro works, see the module documentation of the hidden
  2456. -// module `__internal` inside of `init/__internal.rs`.
  2457. -#[macro_export]
  2458. -macro_rules! init {
  2459. - ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
  2460. - $($fields:tt)*
  2461. - }) => {
  2462. - $crate::__init_internal!(
  2463. - @this($($this)?),
  2464. - @typ($t $(::<$($generics),*>)?),
  2465. - @fields($($fields)*),
  2466. - @error(::core::convert::Infallible),
  2467. - @data(InitData, /*no use_data*/),
  2468. - @has_data(HasInitData, __init_data),
  2469. - @construct_closure(init_from_closure),
  2470. - @munch_fields($($fields)*),
  2471. - )
  2472. - }
  2473. -}
  2474. -
  2475. -/// Construct an in-place fallible initializer for `struct`s.
  2476. -///
  2477. -/// This macro defaults the error to [`Error`]. If you need [`Infallible`], then use
  2478. -/// [`init!`].
  2479. -///
  2480. -/// The syntax is identical to [`try_pin_init!`]. If you want to specify a custom error,
  2481. -/// append `? $type` after the `struct` initializer.
  2482. -/// The safety caveats from [`try_pin_init!`] also apply:
  2483. -/// - `unsafe` code must guarantee either full initialization or return an error and allow
  2484. -/// deallocation of the memory.
  2485. -/// - the fields are initialized in the order given in the initializer.
  2486. -/// - no references to fields are allowed to be created inside of the initializer.
  2487. -///
  2488. -/// # Examples
  2489. -///
  2490. -/// ```rust
  2491. -/// use kernel::{init::{PinInit, zeroed}, error::Error};
  2492. -/// struct BigBuf {
  2493. -/// big: Box<[u8; 1024 * 1024 * 1024]>,
  2494. -/// small: [u8; 1024 * 1024],
  2495. -/// }
  2496. -///
  2497. -/// impl BigBuf {
  2498. -/// fn new() -> impl Init<Self, Error> {
  2499. -/// try_init!(Self {
  2500. -/// big: Box::init(zeroed(), GFP_KERNEL)?,
  2501. -/// small: [0; 1024 * 1024],
  2502. -/// }? Error)
  2503. -/// }
  2504. -/// }
  2505. -/// ```
  2506. -// For a detailed example of how this macro works, see the module documentation of the hidden
  2507. -// module `__internal` inside of `init/__internal.rs`.
  2508. -#[macro_export]
  2509. -macro_rules! try_init {
  2510. - ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
  2511. - $($fields:tt)*
  2512. - }) => {
  2513. - $crate::__init_internal!(
  2514. - @this($($this)?),
  2515. - @typ($t $(::<$($generics),*>)?),
  2516. - @fields($($fields)*),
  2517. - @error($crate::error::Error),
  2518. - @data(InitData, /*no use_data*/),
  2519. - @has_data(HasInitData, __init_data),
  2520. - @construct_closure(init_from_closure),
  2521. - @munch_fields($($fields)*),
  2522. - )
  2523. - };
  2524. - ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
  2525. - $($fields:tt)*
  2526. - }? $err:ty) => {
  2527. - $crate::__init_internal!(
  2528. - @this($($this)?),
  2529. - @typ($t $(::<$($generics),*>)?),
  2530. - @fields($($fields)*),
  2531. - @error($err),
  2532. - @data(InitData, /*no use_data*/),
  2533. - @has_data(HasInitData, __init_data),
  2534. - @construct_closure(init_from_closure),
  2535. - @munch_fields($($fields)*),
  2536. - )
  2537. - };
  2538. -}
  2539. -
  2540. -/// A pin-initializer for the type `T`.
  2541. -///
  2542. -/// To use this initializer, you will need a suitable memory location that can hold a `T`. This can
  2543. -/// be [`Box<T>`], [`Arc<T>`], [`UniqueArc<T>`] or even the stack (see [`stack_pin_init!`]). Use the
  2544. -/// [`InPlaceInit::pin_init`] function of a smart pointer like [`Arc<T>`] on this.
  2545. -///
  2546. -/// Also see the [module description](self).
  2547. -///
  2548. -/// # Safety
  2549. -///
  2550. -/// When implementing this trait you will need to take great care. Also there are probably very few
  2551. -/// cases where a manual implementation is necessary. Use [`pin_init_from_closure`] where possible.
  2552. -///
  2553. -/// The [`PinInit::__pinned_init`] function:
  2554. -/// - returns `Ok(())` if it initialized every field of `slot`,
  2555. -/// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
  2556. -/// - `slot` can be deallocated without UB occurring,
  2557. -/// - `slot` does not need to be dropped,
  2558. -/// - `slot` is not partially initialized.
  2559. -/// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
  2560. -///
  2561. -/// [`Arc<T>`]: crate::sync::Arc
  2562. -/// [`Arc::pin_init`]: crate::sync::Arc::pin_init
  2563. -#[must_use = "An initializer must be used in order to create its value."]
  2564. -pub unsafe trait PinInit<T: ?Sized, E = Infallible>: Sized {
  2565. - /// Initializes `slot`.
  2566. - ///
  2567. - /// # Safety
  2568. - ///
  2569. - /// - `slot` is a valid pointer to uninitialized memory.
  2570. - /// - the caller does not touch `slot` when `Err` is returned, they are only permitted to
  2571. - /// deallocate.
  2572. - /// - `slot` will not move until it is dropped, i.e. it will be pinned.
  2573. - unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>;
  2574. -
  2575. - /// First initializes the value using `self` then calls the function `f` with the initialized
  2576. - /// value.
  2577. - ///
  2578. - /// If `f` returns an error the value is dropped and the initializer will forward the error.
  2579. - ///
  2580. - /// # Examples
  2581. - ///
  2582. - /// ```rust
  2583. - /// # #![allow(clippy::disallowed_names)]
  2584. - /// use kernel::{types::Opaque, init::pin_init_from_closure};
  2585. - /// #[repr(C)]
  2586. - /// struct RawFoo([u8; 16]);
  2587. - /// extern {
  2588. - /// fn init_foo(_: *mut RawFoo);
  2589. - /// }
  2590. - ///
  2591. - /// #[pin_data]
  2592. - /// struct Foo {
  2593. - /// #[pin]
  2594. - /// raw: Opaque<RawFoo>,
  2595. - /// }
  2596. - ///
  2597. - /// impl Foo {
  2598. - /// fn setup(self: Pin<&mut Self>) {
  2599. - /// pr_info!("Setting up foo");
  2600. - /// }
  2601. - /// }
  2602. - ///
  2603. - /// let foo = pin_init!(Foo {
  2604. - /// raw <- unsafe {
  2605. - /// Opaque::ffi_init(|s| {
  2606. - /// init_foo(s);
  2607. - /// })
  2608. - /// },
  2609. - /// }).pin_chain(|foo| {
  2610. - /// foo.setup();
  2611. - /// Ok(())
  2612. - /// });
  2613. - /// ```
  2614. - fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
  2615. - where
  2616. - F: FnOnce(Pin<&mut T>) -> Result<(), E>,
  2617. - {
  2618. - ChainPinInit(self, f, PhantomData)
  2619. - }
  2620. -}
  2621. -
  2622. -/// An initializer returned by [`PinInit::pin_chain`].
  2623. -pub struct ChainPinInit<I, F, T: ?Sized, E>(I, F, __internal::Invariant<(E, Box<T>)>);
  2624. -
  2625. -// SAFETY: The `__pinned_init` function is implemented such that it
  2626. -// - returns `Ok(())` on successful initialization,
  2627. -// - returns `Err(err)` on error and in this case `slot` will be dropped.
  2628. -// - considers `slot` pinned.
  2629. -unsafe impl<T: ?Sized, E, I, F> PinInit<T, E> for ChainPinInit<I, F, T, E>
  2630. -where
  2631. - I: PinInit<T, E>,
  2632. - F: FnOnce(Pin<&mut T>) -> Result<(), E>,
  2633. -{
  2634. - unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
  2635. - // SAFETY: All requirements fulfilled since this function is `__pinned_init`.
  2636. - unsafe { self.0.__pinned_init(slot)? };
  2637. - // SAFETY: The above call initialized `slot` and we still have unique access.
  2638. - let val = unsafe { &mut *slot };
  2639. - // SAFETY: `slot` is considered pinned.
  2640. - let val = unsafe { Pin::new_unchecked(val) };
  2641. - (self.1)(val).map_err(|e| {
  2642. - // SAFETY: `slot` was initialized above.
  2643. - unsafe { core::ptr::drop_in_place(slot) };
  2644. - e
  2645. - })
  2646. - }
  2647. -}
  2648. -
  2649. -/// An initializer for `T`.
  2650. -///
  2651. -/// To use this initializer, you will need a suitable memory location that can hold a `T`. This can
  2652. -/// be [`Box<T>`], [`Arc<T>`], [`UniqueArc<T>`] or even the stack (see [`stack_pin_init!`]). Use the
  2653. -/// [`InPlaceInit::init`] function of a smart pointer like [`Arc<T>`] on this. Because
  2654. -/// [`PinInit<T, E>`] is a super trait, you can use every function that takes it as well.
  2655. -///
  2656. -/// Also see the [module description](self).
  2657. -///
  2658. -/// # Safety
  2659. -///
  2660. -/// When implementing this trait you will need to take great care. Also there are probably very few
  2661. -/// cases where a manual implementation is necessary. Use [`init_from_closure`] where possible.
  2662. -///
  2663. -/// The [`Init::__init`] function:
  2664. -/// - returns `Ok(())` if it initialized every field of `slot`,
  2665. -/// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
  2666. -/// - `slot` can be deallocated without UB occurring,
  2667. -/// - `slot` does not need to be dropped,
  2668. -/// - `slot` is not partially initialized.
  2669. -/// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
  2670. -///
  2671. -/// The `__pinned_init` function from the supertrait [`PinInit`] needs to execute the exact same
  2672. -/// code as `__init`.
  2673. -///
  2674. -/// Contrary to its supertype [`PinInit<T, E>`] the caller is allowed to
  2675. -/// move the pointee after initialization.
  2676. -///
  2677. -/// [`Arc<T>`]: crate::sync::Arc
  2678. -#[must_use = "An initializer must be used in order to create its value."]
  2679. -pub unsafe trait Init<T: ?Sized, E = Infallible>: PinInit<T, E> {
  2680. - /// Initializes `slot`.
  2681. - ///
  2682. - /// # Safety
  2683. - ///
  2684. - /// - `slot` is a valid pointer to uninitialized memory.
  2685. - /// - the caller does not touch `slot` when `Err` is returned, they are only permitted to
  2686. - /// deallocate.
  2687. - unsafe fn __init(self, slot: *mut T) -> Result<(), E>;
  2688. -
  2689. - /// First initializes the value using `self` then calls the function `f` with the initialized
  2690. - /// value.
  2691. - ///
  2692. - /// If `f` returns an error the value is dropped and the initializer will forward the error.
  2693. - ///
  2694. - /// # Examples
  2695. - ///
  2696. - /// ```rust
  2697. - /// # #![allow(clippy::disallowed_names)]
  2698. - /// use kernel::{types::Opaque, init::{self, init_from_closure}};
  2699. - /// struct Foo {
  2700. - /// buf: [u8; 1_000_000],
  2701. - /// }
  2702. - ///
  2703. - /// impl Foo {
  2704. - /// fn setup(&mut self) {
  2705. - /// pr_info!("Setting up foo");
  2706. - /// }
  2707. - /// }
  2708. - ///
  2709. - /// let foo = init!(Foo {
  2710. - /// buf <- init::zeroed()
  2711. - /// }).chain(|foo| {
  2712. - /// foo.setup();
  2713. - /// Ok(())
  2714. - /// });
  2715. - /// ```
  2716. - fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
  2717. - where
  2718. - F: FnOnce(&mut T) -> Result<(), E>,
  2719. - {
  2720. - ChainInit(self, f, PhantomData)
  2721. - }
  2722. -}
  2723. -
  2724. -/// An initializer returned by [`Init::chain`].
  2725. -pub struct ChainInit<I, F, T: ?Sized, E>(I, F, __internal::Invariant<(E, Box<T>)>);
  2726. -
  2727. -// SAFETY: The `__init` function is implemented such that it
  2728. -// - returns `Ok(())` on successful initialization,
  2729. -// - returns `Err(err)` on error and in this case `slot` will be dropped.
  2730. -unsafe impl<T: ?Sized, E, I, F> Init<T, E> for ChainInit<I, F, T, E>
  2731. -where
  2732. - I: Init<T, E>,
  2733. - F: FnOnce(&mut T) -> Result<(), E>,
  2734. -{
  2735. - unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
  2736. - // SAFETY: All requirements fulfilled since this function is `__init`.
  2737. - unsafe { self.0.__pinned_init(slot)? };
  2738. - // SAFETY: The above call initialized `slot` and we still have unique access.
  2739. - (self.1)(unsafe { &mut *slot }).map_err(|e| {
  2740. - // SAFETY: `slot` was initialized above.
  2741. - unsafe { core::ptr::drop_in_place(slot) };
  2742. - e
  2743. - })
  2744. - }
  2745. -}
  2746. -
  2747. -// SAFETY: `__pinned_init` behaves exactly the same as `__init`.
  2748. -unsafe impl<T: ?Sized, E, I, F> PinInit<T, E> for ChainInit<I, F, T, E>
  2749. -where
  2750. - I: Init<T, E>,
  2751. - F: FnOnce(&mut T) -> Result<(), E>,
  2752. -{
  2753. - unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
  2754. - // SAFETY: `__init` has less strict requirements compared to `__pinned_init`.
  2755. - unsafe { self.__init(slot) }
  2756. - }
  2757. -}
  2758. -
  2759. -/// Creates a new [`PinInit<T, E>`] from the given closure.
  2760. -///
  2761. -/// # Safety
  2762. -///
  2763. -/// The closure:
  2764. -/// - returns `Ok(())` if it initialized every field of `slot`,
  2765. -/// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
  2766. -/// - `slot` can be deallocated without UB occurring,
  2767. -/// - `slot` does not need to be dropped,
  2768. -/// - `slot` is not partially initialized.
  2769. -/// - may assume that the `slot` does not move if `T: !Unpin`,
  2770. -/// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
  2771. -#[inline]
  2772. -pub const unsafe fn pin_init_from_closure<T: ?Sized, E>(
  2773. - f: impl FnOnce(*mut T) -> Result<(), E>,
  2774. -) -> impl PinInit<T, E> {
  2775. - __internal::InitClosure(f, PhantomData)
  2776. -}
  2777. -
  2778. -/// Creates a new [`Init<T, E>`] from the given closure.
  2779. -///
  2780. -/// # Safety
  2781. -///
  2782. -/// The closure:
  2783. -/// - returns `Ok(())` if it initialized every field of `slot`,
  2784. -/// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
  2785. -/// - `slot` can be deallocated without UB occurring,
  2786. -/// - `slot` does not need to be dropped,
  2787. -/// - `slot` is not partially initialized.
  2788. -/// - the `slot` may move after initialization.
  2789. -/// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
  2790. -#[inline]
  2791. -pub const unsafe fn init_from_closure<T: ?Sized, E>(
  2792. - f: impl FnOnce(*mut T) -> Result<(), E>,
  2793. -) -> impl Init<T, E> {
  2794. - __internal::InitClosure(f, PhantomData)
  2795. -}
  2796. -
  2797. -/// An initializer that leaves the memory uninitialized.
  2798. -///
  2799. -/// The initializer is a no-op. The `slot` memory is not changed.
  2800. -#[inline]
  2801. -pub fn uninit<T, E>() -> impl Init<MaybeUninit<T>, E> {
  2802. - // SAFETY: The memory is allowed to be uninitialized.
  2803. - unsafe { init_from_closure(|_| Ok(())) }
  2804. -}
  2805. -
  2806. -/// Initializes an array by initializing each element via the provided initializer.
  2807. -///
  2808. -/// # Examples
  2809. -///
  2810. -/// ```rust
  2811. -/// use kernel::{error::Error, init::init_array_from_fn};
  2812. -/// let array: Box<[usize; 1_000]> = Box::init::<Error>(init_array_from_fn(|i| i), GFP_KERNEL).unwrap();
  2813. -/// assert_eq!(array.len(), 1_000);
  2814. -/// ```
  2815. -pub fn init_array_from_fn<I, const N: usize, T, E>(
  2816. - mut make_init: impl FnMut(usize) -> I,
  2817. -) -> impl Init<[T; N], E>
  2818. -where
  2819. - I: Init<T, E>,
  2820. -{
  2821. - let init = move |slot: *mut [T; N]| {
  2822. - let slot = slot.cast::<T>();
  2823. - // Counts the number of initialized elements and when dropped drops that many elements from
  2824. - // `slot`.
  2825. - let mut init_count = ScopeGuard::new_with_data(0, |i| {
  2826. - // We now free every element that has been initialized before.
  2827. - // SAFETY: The loop initialized exactly the values from 0..i and since we
  2828. - // return `Err` below, the caller will consider the memory at `slot` as
  2829. - // uninitialized.
  2830. - unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(slot, i)) };
  2831. - });
  2832. - for i in 0..N {
  2833. - let init = make_init(i);
  2834. - // SAFETY: Since 0 <= `i` < N, it is still in bounds of `[T; N]`.
  2835. - let ptr = unsafe { slot.add(i) };
  2836. - // SAFETY: The pointer is derived from `slot` and thus satisfies the `__init`
  2837. - // requirements.
  2838. - unsafe { init.__init(ptr) }?;
  2839. - *init_count += 1;
  2840. - }
  2841. - init_count.dismiss();
  2842. - Ok(())
  2843. - };
  2844. - // SAFETY: The initializer above initializes every element of the array. On failure it drops
  2845. - // any initialized elements and returns `Err`.
  2846. - unsafe { init_from_closure(init) }
  2847. -}
  2848. -
  2849. -/// Initializes an array by initializing each element via the provided initializer.
  2850. -///
  2851. -/// # Examples
  2852. -///
  2853. -/// ```rust
  2854. -/// use kernel::{sync::{Arc, Mutex}, init::pin_init_array_from_fn, new_mutex};
  2855. -/// let array: Arc<[Mutex<usize>; 1_000]> =
  2856. -/// Arc::pin_init(pin_init_array_from_fn(|i| new_mutex!(i)), GFP_KERNEL).unwrap();
  2857. -/// assert_eq!(array.len(), 1_000);
  2858. -/// ```
  2859. -pub fn pin_init_array_from_fn<I, const N: usize, T, E>(
  2860. - mut make_init: impl FnMut(usize) -> I,
  2861. -) -> impl PinInit<[T; N], E>
  2862. -where
  2863. - I: PinInit<T, E>,
  2864. -{
  2865. - let init = move |slot: *mut [T; N]| {
  2866. - let slot = slot.cast::<T>();
  2867. - // Counts the number of initialized elements and when dropped drops that many elements from
  2868. - // `slot`.
  2869. - let mut init_count = ScopeGuard::new_with_data(0, |i| {
  2870. - // We now free every element that has been initialized before.
  2871. - // SAFETY: The loop initialized exactly the values from 0..i and since we
  2872. - // return `Err` below, the caller will consider the memory at `slot` as
  2873. - // uninitialized.
  2874. - unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(slot, i)) };
  2875. - });
  2876. - for i in 0..N {
  2877. - let init = make_init(i);
  2878. - // SAFETY: Since 0 <= `i` < N, it is still in bounds of `[T; N]`.
  2879. - let ptr = unsafe { slot.add(i) };
  2880. - // SAFETY: The pointer is derived from `slot` and thus satisfies the `__init`
  2881. - // requirements.
  2882. - unsafe { init.__pinned_init(ptr) }?;
  2883. - *init_count += 1;
  2884. - }
  2885. - init_count.dismiss();
  2886. - Ok(())
  2887. - };
  2888. - // SAFETY: The initializer above initializes every element of the array. On failure it drops
  2889. - // any initialized elements and returns `Err`.
  2890. - unsafe { pin_init_from_closure(init) }
  2891. -}
  2892. -
  2893. -// SAFETY: Every type can be initialized by-value.
  2894. -unsafe impl<T, E> Init<T, E> for T {
  2895. - unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
  2896. - unsafe { slot.write(self) };
  2897. - Ok(())
  2898. - }
  2899. -}
  2900. -
  2901. -// SAFETY: Every type can be initialized by-value. `__pinned_init` calls `__init`.
  2902. -unsafe impl<T, E> PinInit<T, E> for T {
  2903. - unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
  2904. - unsafe { self.__init(slot) }
  2905. - }
  2906. -}
  2907. -
  2908. -/// Smart pointer that can initialize memory in-place.
  2909. -pub trait InPlaceInit<T>: Sized {
  2910. - /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this
  2911. - /// type.
  2912. - ///
  2913. - /// If `T: !Unpin` it will not be able to move afterwards.
  2914. - fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
  2915. - where
  2916. - E: From<AllocError>;
  2917. -
  2918. - /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this
  2919. - /// type.
  2920. - ///
  2921. - /// If `T: !Unpin` it will not be able to move afterwards.
  2922. - fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> error::Result<Pin<Self>>
  2923. - where
  2924. - Error: From<E>,
  2925. - {
  2926. - // SAFETY: We delegate to `init` and only change the error type.
  2927. - let init = unsafe {
  2928. - pin_init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e)))
  2929. - };
  2930. - Self::try_pin_init(init, flags)
  2931. - }
  2932. -
  2933. - /// Use the given initializer to in-place initialize a `T`.
  2934. - fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
  2935. - where
  2936. - E: From<AllocError>;
  2937. -
  2938. - /// Use the given initializer to in-place initialize a `T`.
  2939. - fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
  2940. - where
  2941. - Error: From<E>,
  2942. - {
  2943. - // SAFETY: We delegate to `init` and only change the error type.
  2944. - let init = unsafe {
  2945. - init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e)))
  2946. - };
  2947. - Self::try_init(init, flags)
  2948. - }
  2949. -}
  2950. -
  2951. -impl<T> InPlaceInit<T> for Box<T> {
  2952. - #[inline]
  2953. - fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
  2954. - where
  2955. - E: From<AllocError>,
  2956. - {
  2957. - let mut this = <Box<_> as BoxExt<_>>::new_uninit(flags)?;
  2958. - let slot = this.as_mut_ptr();
  2959. - // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
  2960. - // slot is valid and will not be moved, because we pin it later.
  2961. - unsafe { init.__pinned_init(slot)? };
  2962. - // SAFETY: All fields have been initialized.
  2963. - Ok(unsafe { this.assume_init() }.into())
  2964. - }
  2965. -
  2966. - #[inline]
  2967. - fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
  2968. - where
  2969. - E: From<AllocError>,
  2970. - {
  2971. - let mut this = <Box<_> as BoxExt<_>>::new_uninit(flags)?;
  2972. - let slot = this.as_mut_ptr();
  2973. - // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
  2974. - // slot is valid.
  2975. - unsafe { init.__init(slot)? };
  2976. - // SAFETY: All fields have been initialized.
  2977. - Ok(unsafe { this.assume_init() })
  2978. - }
  2979. -}
  2980. -
  2981. -impl<T> InPlaceInit<T> for UniqueArc<T> {
  2982. - #[inline]
  2983. - fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
  2984. - where
  2985. - E: From<AllocError>,
  2986. - {
  2987. - let mut this = UniqueArc::new_uninit(flags)?;
  2988. - let slot = this.as_mut_ptr();
  2989. - // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
  2990. - // slot is valid and will not be moved, because we pin it later.
  2991. - unsafe { init.__pinned_init(slot)? };
  2992. - // SAFETY: All fields have been initialized.
  2993. - Ok(unsafe { this.assume_init() }.into())
  2994. - }
  2995. -
  2996. - #[inline]
  2997. - fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
  2998. - where
  2999. - E: From<AllocError>,
  3000. - {
  3001. - let mut this = UniqueArc::new_uninit(flags)?;
  3002. - let slot = this.as_mut_ptr();
  3003. - // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
  3004. - // slot is valid.
  3005. - unsafe { init.__init(slot)? };
  3006. - // SAFETY: All fields have been initialized.
  3007. - Ok(unsafe { this.assume_init() })
  3008. - }
  3009. -}
  3010. -
  3011. -/// Trait facilitating pinned destruction.
  3012. -///
  3013. -/// Use [`pinned_drop`] to implement this trait safely:
  3014. -///
  3015. -/// ```rust
  3016. -/// # use kernel::sync::Mutex;
  3017. -/// use kernel::macros::pinned_drop;
  3018. -/// use core::pin::Pin;
  3019. -/// #[pin_data(PinnedDrop)]
  3020. -/// struct Foo {
  3021. -/// #[pin]
  3022. -/// mtx: Mutex<usize>,
  3023. -/// }
  3024. -///
  3025. -/// #[pinned_drop]
  3026. -/// impl PinnedDrop for Foo {
  3027. -/// fn drop(self: Pin<&mut Self>) {
  3028. -/// pr_info!("Foo is being dropped!");
  3029. -/// }
  3030. -/// }
  3031. -/// ```
  3032. -///
  3033. -/// # Safety
  3034. -///
  3035. -/// This trait must be implemented via the [`pinned_drop`] proc-macro attribute on the impl.
  3036. -///
  3037. -/// [`pinned_drop`]: kernel::macros::pinned_drop
  3038. -pub unsafe trait PinnedDrop: __internal::HasPinData {
  3039. - /// Executes the pinned destructor of this type.
  3040. - ///
  3041. - /// While this function is marked safe, it is actually unsafe to call it manually. For this
  3042. - /// reason it takes an additional parameter. This type can only be constructed by `unsafe` code
  3043. - /// and thus prevents this function from being called where it should not.
  3044. - ///
  3045. - /// This extra parameter will be generated by the `#[pinned_drop]` proc-macro attribute
  3046. - /// automatically.
  3047. - fn drop(self: Pin<&mut Self>, only_call_from_drop: __internal::OnlyCallFromDrop);
  3048. -}
  3049. -
  3050. -/// Marker trait for types that can be initialized by writing just zeroes.
  3051. -///
  3052. -/// # Safety
  3053. -///
  3054. -/// The bit pattern consisting of only zeroes is a valid bit pattern for this type. In other words,
  3055. -/// this is not UB:
  3056. -///
  3057. -/// ```rust,ignore
  3058. -/// let val: Self = unsafe { core::mem::zeroed() };
  3059. -/// ```
  3060. -pub unsafe trait Zeroable {}
  3061. -
  3062. -/// Create a new zeroed T.
  3063. -///
  3064. -/// The returned initializer will write `0x00` to every byte of the given `slot`.
  3065. -#[inline]
  3066. -pub fn zeroed<T: Zeroable>() -> impl Init<T> {
  3067. - // SAFETY: Because `T: Zeroable`, all bytes zero is a valid bit pattern for `T`
  3068. - // and because we write all zeroes, the memory is initialized.
  3069. - unsafe {
  3070. - init_from_closure(|slot: *mut T| {
  3071. - slot.write_bytes(0, 1);
  3072. - Ok(())
  3073. - })
  3074. - }
  3075. -}
  3076. -
  3077. -macro_rules! impl_zeroable {
  3078. - ($($({$($generics:tt)*})? $t:ty, )*) => {
  3079. - $(unsafe impl$($($generics)*)? Zeroable for $t {})*
  3080. - };
  3081. -}
  3082. -
  3083. -impl_zeroable! {
  3084. - // SAFETY: All primitives that are allowed to be zero.
  3085. - bool,
  3086. - char,
  3087. - u8, u16, u32, u64, u128, usize,
  3088. - i8, i16, i32, i64, i128, isize,
  3089. - f32, f64,
  3090. -
  3091. - // Note: do not add uninhabited types (such as `!` or `core::convert::Infallible`) to this list;
  3092. - // creating an instance of an uninhabited type is immediate undefined behavior. For more on
  3093. - // uninhabited/empty types, consult The Rustonomicon:
  3094. - // <https://doc.rust-lang.org/stable/nomicon/exotic-sizes.html#empty-types>. The Rust Reference
  3095. - // also has information on undefined behavior:
  3096. - // <https://doc.rust-lang.org/stable/reference/behavior-considered-undefined.html>.
  3097. - //
  3098. - // SAFETY: These are inhabited ZSTs; there is nothing to zero and a valid value exists.
  3099. - {<T: ?Sized>} PhantomData<T>, core::marker::PhantomPinned, (),
  3100. -
  3101. - // SAFETY: Type is allowed to take any value, including all zeros.
  3102. - {<T>} MaybeUninit<T>,
  3103. - // SAFETY: Type is allowed to take any value, including all zeros.
  3104. - {<T>} Opaque<T>,
  3105. -
  3106. - // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
  3107. - {<T: ?Sized + Zeroable>} UnsafeCell<T>,
  3108. -
  3109. - // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
  3110. - Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,
  3111. - Option<NonZeroU128>, Option<NonZeroUsize>,
  3112. - Option<NonZeroI8>, Option<NonZeroI16>, Option<NonZeroI32>, Option<NonZeroI64>,
  3113. - Option<NonZeroI128>, Option<NonZeroIsize>,
  3114. -
  3115. - // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
  3116. - //
  3117. - // In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant.
  3118. - {<T: ?Sized>} Option<NonNull<T>>,
  3119. - {<T: ?Sized>} Option<Box<T>>,
  3120. -
  3121. - // SAFETY: `null` pointer is valid.
  3122. - //
  3123. - // We cannot use `T: ?Sized`, since the VTABLE pointer part of fat pointers is not allowed to be
  3124. - // null.
  3125. - //
  3126. - // When `Pointee` gets stabilized, we could use
  3127. - // `T: ?Sized where <T as Pointee>::Metadata: Zeroable`
  3128. - {<T>} *mut T, {<T>} *const T,
  3129. -
  3130. - // SAFETY: `null` pointer is valid and the metadata part of these fat pointers is allowed to be
  3131. - // zero.
  3132. - {<T>} *mut [T], {<T>} *const [T], *mut str, *const str,
  3133. -
  3134. - // SAFETY: `T` is `Zeroable`.
  3135. - {<const N: usize, T: Zeroable>} [T; N], {<T: Zeroable>} Wrapping<T>,
  3136. -}
  3137. -
  3138. -macro_rules! impl_tuple_zeroable {
  3139. - ($(,)?) => {};
  3140. - ($first:ident, $($t:ident),* $(,)?) => {
  3141. - // SAFETY: All elements are zeroable and padding can be zero.
  3142. - unsafe impl<$first: Zeroable, $($t: Zeroable),*> Zeroable for ($first, $($t),*) {}
  3143. - impl_tuple_zeroable!($($t),* ,);
  3144. - }
  3145. -}
  3146. -
  3147. -impl_tuple_zeroable!(A, B, C, D, E, F, G, H, I, J);
  3148. diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs
  3149. deleted file mode 100644
  3150. --- a/rust/kernel/init/__internal.rs
  3151. +++ /dev/null
  3152. @@ -1,230 +0,0 @@
  3153. -// SPDX-License-Identifier: Apache-2.0 OR MIT
  3154. -
  3155. -//! This module contains API-internal items for pin-init.
  3156. -//!
  3157. -//! These items must not be used outside of
  3158. -//! - `kernel/init.rs`
  3159. -//! - `macros/pin_data.rs`
  3160. -//! - `macros/pinned_drop.rs`
  3161. -
  3162. -use super::*;
  3163. -
  3164. -/// See the [nomicon] for what subtyping is. See also [this table].
  3165. -///
  3166. -/// [nomicon]: https://doc.rust-lang.org/nomicon/subtyping.html
  3167. -/// [this table]: https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns
  3168. -pub(super) type Invariant<T> = PhantomData<fn(*mut T) -> *mut T>;
  3169. -
  3170. -/// This is the module-internal type implementing `PinInit` and `Init`. It is unsafe to create this
  3171. -/// type, since the closure needs to fulfill the same safety requirement as the
  3172. -/// `__pinned_init`/`__init` functions.
  3173. -pub(crate) struct InitClosure<F, T: ?Sized, E>(pub(crate) F, pub(crate) Invariant<(E, T)>);
  3174. -
  3175. -// SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
  3176. -// `__init` invariants.
  3177. -unsafe impl<T: ?Sized, F, E> Init<T, E> for InitClosure<F, T, E>
  3178. -where
  3179. - F: FnOnce(*mut T) -> Result<(), E>,
  3180. -{
  3181. - #[inline]
  3182. - unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
  3183. - (self.0)(slot)
  3184. - }
  3185. -}
  3186. -
  3187. -// SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
  3188. -// `__pinned_init` invariants.
  3189. -unsafe impl<T: ?Sized, F, E> PinInit<T, E> for InitClosure<F, T, E>
  3190. -where
  3191. - F: FnOnce(*mut T) -> Result<(), E>,
  3192. -{
  3193. - #[inline]
  3194. - unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
  3195. - (self.0)(slot)
  3196. - }
  3197. -}
  3198. -
  3199. -/// This trait is only implemented via the `#[pin_data]` proc-macro. It is used to facilitate
  3200. -/// the pin projections within the initializers.
  3201. -///
  3202. -/// # Safety
  3203. -///
  3204. -/// Only the `init` module is allowed to use this trait.
  3205. -pub unsafe trait HasPinData {
  3206. - type PinData: PinData;
  3207. -
  3208. - unsafe fn __pin_data() -> Self::PinData;
  3209. -}
  3210. -
  3211. -/// Marker trait for pinning data of structs.
  3212. -///
  3213. -/// # Safety
  3214. -///
  3215. -/// Only the `init` module is allowed to use this trait.
  3216. -pub unsafe trait PinData: Copy {
  3217. - type Datee: ?Sized + HasPinData;
  3218. -
  3219. - /// Type inference helper function.
  3220. - fn make_closure<F, O, E>(self, f: F) -> F
  3221. - where
  3222. - F: FnOnce(*mut Self::Datee) -> Result<O, E>,
  3223. - {
  3224. - f
  3225. - }
  3226. -}
  3227. -
  3228. -/// This trait is automatically implemented for every type. It aims to provide the same type
  3229. -/// inference help as `HasPinData`.
  3230. -///
  3231. -/// # Safety
  3232. -///
  3233. -/// Only the `init` module is allowed to use this trait.
  3234. -pub unsafe trait HasInitData {
  3235. - type InitData: InitData;
  3236. -
  3237. - unsafe fn __init_data() -> Self::InitData;
  3238. -}
  3239. -
  3240. -/// Same function as `PinData`, but for arbitrary data.
  3241. -///
  3242. -/// # Safety
  3243. -///
  3244. -/// Only the `init` module is allowed to use this trait.
  3245. -pub unsafe trait InitData: Copy {
  3246. - type Datee: ?Sized + HasInitData;
  3247. -
  3248. - /// Type inference helper function.
  3249. - fn make_closure<F, O, E>(self, f: F) -> F
  3250. - where
  3251. - F: FnOnce(*mut Self::Datee) -> Result<O, E>,
  3252. - {
  3253. - f
  3254. - }
  3255. -}
  3256. -
  3257. -pub struct AllData<T: ?Sized>(PhantomData<fn(Box<T>) -> Box<T>>);
  3258. -
  3259. -impl<T: ?Sized> Clone for AllData<T> {
  3260. - fn clone(&self) -> Self {
  3261. - *self
  3262. - }
  3263. -}
  3264. -
  3265. -impl<T: ?Sized> Copy for AllData<T> {}
  3266. -
  3267. -unsafe impl<T: ?Sized> InitData for AllData<T> {
  3268. - type Datee = T;
  3269. -}
  3270. -
  3271. -unsafe impl<T: ?Sized> HasInitData for T {
  3272. - type InitData = AllData<T>;
  3273. -
  3274. - unsafe fn __init_data() -> Self::InitData {
  3275. - AllData(PhantomData)
  3276. - }
  3277. -}
  3278. -
  3279. -/// Stack initializer helper type. Use [`stack_pin_init`] instead of this primitive.
  3280. -///
  3281. -/// # Invariants
  3282. -///
  3283. -/// If `self.is_init` is true, then `self.value` is initialized.
  3284. -///
  3285. -/// [`stack_pin_init`]: kernel::stack_pin_init
  3286. -pub struct StackInit<T> {
  3287. - value: MaybeUninit<T>,
  3288. - is_init: bool,
  3289. -}
  3290. -
  3291. -impl<T> Drop for StackInit<T> {
  3292. - #[inline]
  3293. - fn drop(&mut self) {
  3294. - if self.is_init {
  3295. - // SAFETY: As we are being dropped, we only call this once. And since `self.is_init` is
  3296. - // true, `self.value` is initialized.
  3297. - unsafe { self.value.assume_init_drop() };
  3298. - }
  3299. - }
  3300. -}
  3301. -
  3302. -impl<T> StackInit<T> {
  3303. - /// Creates a new [`StackInit<T>`] that is uninitialized. Use [`stack_pin_init`] instead of this
  3304. - /// primitive.
  3305. - ///
  3306. - /// [`stack_pin_init`]: kernel::stack_pin_init
  3307. - #[inline]
  3308. - pub fn uninit() -> Self {
  3309. - Self {
  3310. - value: MaybeUninit::uninit(),
  3311. - is_init: false,
  3312. - }
  3313. - }
  3314. -
  3315. - /// Initializes the contents and returns the result.
  3316. - #[inline]
  3317. - pub fn init<E>(self: Pin<&mut Self>, init: impl PinInit<T, E>) -> Result<Pin<&mut T>, E> {
  3318. - // SAFETY: We never move out of `this`.
  3319. - let this = unsafe { Pin::into_inner_unchecked(self) };
  3320. - // The value is currently initialized, so it needs to be dropped before we can reuse
  3321. - // the memory (this is a safety guarantee of `Pin`).
  3322. - if this.is_init {
  3323. - this.is_init = false;
  3324. - // SAFETY: `this.is_init` was true and therefore `this.value` is initialized.
  3325. - unsafe { this.value.assume_init_drop() };
  3326. - }
  3327. - // SAFETY: The memory slot is valid and this type ensures that it will stay pinned.
  3328. - unsafe { init.__pinned_init(this.value.as_mut_ptr())? };
  3329. - // INVARIANT: `this.value` is initialized above.
  3330. - this.is_init = true;
  3331. - // SAFETY: The slot is now pinned, since we will never give access to `&mut T`.
  3332. - Ok(unsafe { Pin::new_unchecked(this.value.assume_init_mut()) })
  3333. - }
  3334. -}
  3335. -
  3336. -/// When a value of this type is dropped, it drops a `T`.
  3337. -///
  3338. -/// Can be forgotten to prevent the drop.
  3339. -pub struct DropGuard<T: ?Sized> {
  3340. - ptr: *mut T,
  3341. -}
  3342. -
  3343. -impl<T: ?Sized> DropGuard<T> {
  3344. - /// Creates a new [`DropGuard<T>`]. It will [`ptr::drop_in_place`] `ptr` when it gets dropped.
  3345. - ///
  3346. - /// # Safety
  3347. - ///
  3348. - /// `ptr` must be a valid pointer.
  3349. - ///
  3350. - /// It is the callers responsibility that `self` will only get dropped if the pointee of `ptr`:
  3351. - /// - has not been dropped,
  3352. - /// - is not accessible by any other means,
  3353. - /// - will not be dropped by any other means.
  3354. - #[inline]
  3355. - pub unsafe fn new(ptr: *mut T) -> Self {
  3356. - Self { ptr }
  3357. - }
  3358. -}
  3359. -
  3360. -impl<T: ?Sized> Drop for DropGuard<T> {
  3361. - #[inline]
  3362. - fn drop(&mut self) {
  3363. - // SAFETY: A `DropGuard` can only be constructed using the unsafe `new` function
  3364. - // ensuring that this operation is safe.
  3365. - unsafe { ptr::drop_in_place(self.ptr) }
  3366. - }
  3367. -}
  3368. -
  3369. -/// Token used by `PinnedDrop` to prevent calling the function without creating this unsafely
  3370. -/// created struct. This is needed, because the `drop` function is safe, but should not be called
  3371. -/// manually.
  3372. -pub struct OnlyCallFromDrop(());
  3373. -
  3374. -impl OnlyCallFromDrop {
  3375. - /// # Safety
  3376. - ///
  3377. - /// This function should only be called from the [`Drop::drop`] function and only be used to
  3378. - /// delegate the destruction to the pinned destructor [`PinnedDrop::drop`] of the same type.
  3379. - pub unsafe fn new() -> Self {
  3380. - Self(())
  3381. - }
  3382. -}
  3383. diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs
  3384. deleted file mode 100644
  3385. --- a/rust/kernel/init/macros.rs
  3386. +++ /dev/null
  3387. @@ -1,1400 +0,0 @@
  3388. -// SPDX-License-Identifier: Apache-2.0 OR MIT
  3389. -
  3390. -//! This module provides the macros that actually implement the proc-macros `pin_data` and
  3391. -//! `pinned_drop`. It also contains `__init_internal` the implementation of the `{try_}{pin_}init!`
  3392. -//! macros.
  3393. -//!
  3394. -//! These macros should never be called directly, since they expect their input to be
  3395. -//! in a certain format which is internal. If used incorrectly, these macros can lead to UB even in
  3396. -//! safe code! Use the public facing macros instead.
  3397. -//!
  3398. -//! This architecture has been chosen because the kernel does not yet have access to `syn` which
  3399. -//! would make matters a lot easier for implementing these as proc-macros.
  3400. -//!
  3401. -//! # Macro expansion example
  3402. -//!
  3403. -//! This section is intended for readers trying to understand the macros in this module and the
  3404. -//! `pin_init!` macros from `init.rs`.
  3405. -//!
  3406. -//! We will look at the following example:
  3407. -//!
  3408. -//! ```rust,ignore
  3409. -//! # use kernel::init::*;
  3410. -//! # use core::pin::Pin;
  3411. -//! #[pin_data]
  3412. -//! #[repr(C)]
  3413. -//! struct Bar<T> {
  3414. -//! #[pin]
  3415. -//! t: T,
  3416. -//! pub x: usize,
  3417. -//! }
  3418. -//!
  3419. -//! impl<T> Bar<T> {
  3420. -//! fn new(t: T) -> impl PinInit<Self> {
  3421. -//! pin_init!(Self { t, x: 0 })
  3422. -//! }
  3423. -//! }
  3424. -//!
  3425. -//! #[pin_data(PinnedDrop)]
  3426. -//! struct Foo {
  3427. -//! a: usize,
  3428. -//! #[pin]
  3429. -//! b: Bar<u32>,
  3430. -//! }
  3431. -//!
  3432. -//! #[pinned_drop]
  3433. -//! impl PinnedDrop for Foo {
  3434. -//! fn drop(self: Pin<&mut Self>) {
  3435. -//! pr_info!("{self:p} is getting dropped.");
  3436. -//! }
  3437. -//! }
  3438. -//!
  3439. -//! let a = 42;
  3440. -//! let initializer = pin_init!(Foo {
  3441. -//! a,
  3442. -//! b <- Bar::new(36),
  3443. -//! });
  3444. -//! ```
  3445. -//!
  3446. -//! This example includes the most common and important features of the pin-init API.
  3447. -//!
  3448. -//! Below you can find individual section about the different macro invocations. Here are some
  3449. -//! general things we need to take into account when designing macros:
  3450. -//! - use global paths, similarly to file paths, these start with the separator: `::core::panic!()`
  3451. -//! this ensures that the correct item is used, since users could define their own `mod core {}`
  3452. -//! and then their own `panic!` inside to execute arbitrary code inside of our macro.
  3453. -//! - macro `unsafe` hygiene: we need to ensure that we do not expand arbitrary, user-supplied
  3454. -//! expressions inside of an `unsafe` block in the macro, because this would allow users to do
  3455. -//! `unsafe` operations without an associated `unsafe` block.
  3456. -//!
  3457. -//! ## `#[pin_data]` on `Bar`
  3458. -//!
  3459. -//! This macro is used to specify which fields are structurally pinned and which fields are not. It
  3460. -//! is placed on the struct definition and allows `#[pin]` to be placed on the fields.
  3461. -//!
  3462. -//! Here is the definition of `Bar` from our example:
  3463. -//!
  3464. -//! ```rust,ignore
  3465. -//! # use kernel::init::*;
  3466. -//! #[pin_data]
  3467. -//! #[repr(C)]
  3468. -//! struct Bar<T> {
  3469. -//! #[pin]
  3470. -//! t: T,
  3471. -//! pub x: usize,
  3472. -//! }
  3473. -//! ```
  3474. -//!
  3475. -//! This expands to the following code:
  3476. -//!
  3477. -//! ```rust,ignore
  3478. -//! // Firstly the normal definition of the struct, attributes are preserved:
  3479. -//! #[repr(C)]
  3480. -//! struct Bar<T> {
  3481. -//! t: T,
  3482. -//! pub x: usize,
  3483. -//! }
  3484. -//! // Then an anonymous constant is defined, this is because we do not want any code to access the
  3485. -//! // types that we define inside:
  3486. -//! const _: () = {
  3487. -//! // We define the pin-data carrying struct, it is a ZST and needs to have the same generics,
  3488. -//! // since we need to implement access functions for each field and thus need to know its
  3489. -//! // type.
  3490. -//! struct __ThePinData<T> {
  3491. -//! __phantom: ::core::marker::PhantomData<fn(Bar<T>) -> Bar<T>>,
  3492. -//! }
  3493. -//! // We implement `Copy` for the pin-data struct, since all functions it defines will take
  3494. -//! // `self` by value.
  3495. -//! impl<T> ::core::clone::Clone for __ThePinData<T> {
  3496. -//! fn clone(&self) -> Self {
  3497. -//! *self
  3498. -//! }
  3499. -//! }
  3500. -//! impl<T> ::core::marker::Copy for __ThePinData<T> {}
  3501. -//! // For every field of `Bar`, the pin-data struct will define a function with the same name
  3502. -//! // and accessor (`pub` or `pub(crate)` etc.). This function will take a pointer to the
  3503. -//! // field (`slot`) and a `PinInit` or `Init` depending on the projection kind of the field
  3504. -//! // (if pinning is structural for the field, then `PinInit` otherwise `Init`).
  3505. -//! #[allow(dead_code)]
  3506. -//! impl<T> __ThePinData<T> {
  3507. -//! unsafe fn t<E>(
  3508. -//! self,
  3509. -//! slot: *mut T,
  3510. -//! // Since `t` is `#[pin]`, this is `PinInit`.
  3511. -//! init: impl ::kernel::init::PinInit<T, E>,
  3512. -//! ) -> ::core::result::Result<(), E> {
  3513. -//! unsafe { ::kernel::init::PinInit::__pinned_init(init, slot) }
  3514. -//! }
  3515. -//! pub unsafe fn x<E>(
  3516. -//! self,
  3517. -//! slot: *mut usize,
  3518. -//! // Since `x` is not `#[pin]`, this is `Init`.
  3519. -//! init: impl ::kernel::init::Init<usize, E>,
  3520. -//! ) -> ::core::result::Result<(), E> {
  3521. -//! unsafe { ::kernel::init::Init::__init(init, slot) }
  3522. -//! }
  3523. -//! }
  3524. -//! // Implement the internal `HasPinData` trait that associates `Bar` with the pin-data struct
  3525. -//! // that we constructed above.
  3526. -//! unsafe impl<T> ::kernel::init::__internal::HasPinData for Bar<T> {
  3527. -//! type PinData = __ThePinData<T>;
  3528. -//! unsafe fn __pin_data() -> Self::PinData {
  3529. -//! __ThePinData {
  3530. -//! __phantom: ::core::marker::PhantomData,
  3531. -//! }
  3532. -//! }
  3533. -//! }
  3534. -//! // Implement the internal `PinData` trait that marks the pin-data struct as a pin-data
  3535. -//! // struct. This is important to ensure that no user can implement a rouge `__pin_data`
  3536. -//! // function without using `unsafe`.
  3537. -//! unsafe impl<T> ::kernel::init::__internal::PinData for __ThePinData<T> {
  3538. -//! type Datee = Bar<T>;
  3539. -//! }
  3540. -//! // Now we only want to implement `Unpin` for `Bar` when every structurally pinned field is
  3541. -//! // `Unpin`. In other words, whether `Bar` is `Unpin` only depends on structurally pinned
  3542. -//! // fields (those marked with `#[pin]`). These fields will be listed in this struct, in our
  3543. -//! // case no such fields exist, hence this is almost empty. The two phantomdata fields exist
  3544. -//! // for two reasons:
  3545. -//! // - `__phantom`: every generic must be used, since we cannot really know which generics
  3546. -//! // are used, we declere all and then use everything here once.
  3547. -//! // - `__phantom_pin`: uses the `'__pin` lifetime and ensures that this struct is invariant
  3548. -//! // over it. The lifetime is needed to work around the limitation that trait bounds must
  3549. -//! // not be trivial, e.g. the user has a `#[pin] PhantomPinned` field -- this is
  3550. -//! // unconditionally `!Unpin` and results in an error. The lifetime tricks the compiler
  3551. -//! // into accepting these bounds regardless.
  3552. -//! #[allow(dead_code)]
  3553. -//! struct __Unpin<'__pin, T> {
  3554. -//! __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
  3555. -//! __phantom: ::core::marker::PhantomData<fn(Bar<T>) -> Bar<T>>,
  3556. -//! // Our only `#[pin]` field is `t`.
  3557. -//! t: T,
  3558. -//! }
  3559. -//! #[doc(hidden)]
  3560. -//! impl<'__pin, T> ::core::marker::Unpin for Bar<T>
  3561. -//! where
  3562. -//! __Unpin<'__pin, T>: ::core::marker::Unpin,
  3563. -//! {}
  3564. -//! // Now we need to ensure that `Bar` does not implement `Drop`, since that would give users
  3565. -//! // access to `&mut self` inside of `drop` even if the struct was pinned. This could lead to
  3566. -//! // UB with only safe code, so we disallow this by giving a trait implementation error using
  3567. -//! // a direct impl and a blanket implementation.
  3568. -//! trait MustNotImplDrop {}
  3569. -//! // Normally `Drop` bounds do not have the correct semantics, but for this purpose they do
  3570. -//! // (normally people want to know if a type has any kind of drop glue at all, here we want
  3571. -//! // to know if it has any kind of custom drop glue, which is exactly what this bound does).
  3572. -//! #[allow(drop_bounds)]
  3573. -//! impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
  3574. -//! impl<T> MustNotImplDrop for Bar<T> {}
  3575. -//! // Here comes a convenience check, if one implemented `PinnedDrop`, but forgot to add it to
  3576. -//! // `#[pin_data]`, then this will error with the same mechanic as above, this is not needed
  3577. -//! // for safety, but a good sanity check, since no normal code calls `PinnedDrop::drop`.
  3578. -//! #[allow(non_camel_case_types)]
  3579. -//! trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
  3580. -//! impl<
  3581. -//! T: ::kernel::init::PinnedDrop,
  3582. -//! > UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
  3583. -//! impl<T> UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for Bar<T> {}
  3584. -//! };
  3585. -//! ```
  3586. -//!
  3587. -//! ## `pin_init!` in `impl Bar`
  3588. -//!
  3589. -//! This macro creates an pin-initializer for the given struct. It requires that the struct is
  3590. -//! annotated by `#[pin_data]`.
  3591. -//!
  3592. -//! Here is the impl on `Bar` defining the new function:
  3593. -//!
  3594. -//! ```rust,ignore
  3595. -//! impl<T> Bar<T> {
  3596. -//! fn new(t: T) -> impl PinInit<Self> {
  3597. -//! pin_init!(Self { t, x: 0 })
  3598. -//! }
  3599. -//! }
  3600. -//! ```
  3601. -//!
  3602. -//! This expands to the following code:
  3603. -//!
  3604. -//! ```rust,ignore
  3605. -//! impl<T> Bar<T> {
  3606. -//! fn new(t: T) -> impl PinInit<Self> {
  3607. -//! {
  3608. -//! // We do not want to allow arbitrary returns, so we declare this type as the `Ok`
  3609. -//! // return type and shadow it later when we insert the arbitrary user code. That way
  3610. -//! // there will be no possibility of returning without `unsafe`.
  3611. -//! struct __InitOk;
  3612. -//! // Get the data about fields from the supplied type.
  3613. -//! // - the function is unsafe, hence the unsafe block
  3614. -//! // - we `use` the `HasPinData` trait in the block, it is only available in that
  3615. -//! // scope.
  3616. -//! let data = unsafe {
  3617. -//! use ::kernel::init::__internal::HasPinData;
  3618. -//! Self::__pin_data()
  3619. -//! };
  3620. -//! // Ensure that `data` really is of type `PinData` and help with type inference:
  3621. -//! let init = ::kernel::init::__internal::PinData::make_closure::<
  3622. -//! _,
  3623. -//! __InitOk,
  3624. -//! ::core::convert::Infallible,
  3625. -//! >(data, move |slot| {
  3626. -//! {
  3627. -//! // Shadow the structure so it cannot be used to return early. If a user
  3628. -//! // tries to write `return Ok(__InitOk)`, then they get a type error,
  3629. -//! // since that will refer to this struct instead of the one defined
  3630. -//! // above.
  3631. -//! struct __InitOk;
  3632. -//! // This is the expansion of `t,`, which is syntactic sugar for `t: t,`.
  3633. -//! {
  3634. -//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).t), t) };
  3635. -//! }
  3636. -//! // Since initialization could fail later (not in this case, since the
  3637. -//! // error type is `Infallible`) we will need to drop this field if there
  3638. -//! // is an error later. This `DropGuard` will drop the field when it gets
  3639. -//! // dropped and has not yet been forgotten.
  3640. -//! let __t_guard = unsafe {
  3641. -//! ::pinned_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).t))
  3642. -//! };
  3643. -//! // Expansion of `x: 0,`:
  3644. -//! // Since this can be an arbitrary expression we cannot place it inside
  3645. -//! // of the `unsafe` block, so we bind it here.
  3646. -//! {
  3647. -//! let x = 0;
  3648. -//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).x), x) };
  3649. -//! }
  3650. -//! // We again create a `DropGuard`.
  3651. -//! let __x_guard = unsafe {
  3652. -//! ::kernel::init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).x))
  3653. -//! };
  3654. -//! // Since initialization has successfully completed, we can now forget
  3655. -//! // the guards. This is not `mem::forget`, since we only have
  3656. -//! // `&DropGuard`.
  3657. -//! ::core::mem::forget(__x_guard);
  3658. -//! ::core::mem::forget(__t_guard);
  3659. -//! // Here we use the type checker to ensure that every field has been
  3660. -//! // initialized exactly once, since this is `if false` it will never get
  3661. -//! // executed, but still type-checked.
  3662. -//! // Additionally we abuse `slot` to automatically infer the correct type
  3663. -//! // for the struct. This is also another check that every field is
  3664. -//! // accessible from this scope.
  3665. -//! #[allow(unreachable_code, clippy::diverging_sub_expression)]
  3666. -//! let _ = || {
  3667. -//! unsafe {
  3668. -//! ::core::ptr::write(
  3669. -//! slot,
  3670. -//! Self {
  3671. -//! // We only care about typecheck finding every field
  3672. -//! // here, the expression does not matter, just conjure
  3673. -//! // one using `panic!()`:
  3674. -//! t: ::core::panic!(),
  3675. -//! x: ::core::panic!(),
  3676. -//! },
  3677. -//! );
  3678. -//! };
  3679. -//! };
  3680. -//! }
  3681. -//! // We leave the scope above and gain access to the previously shadowed
  3682. -//! // `__InitOk` that we need to return.
  3683. -//! Ok(__InitOk)
  3684. -//! });
  3685. -//! // Change the return type from `__InitOk` to `()`.
  3686. -//! let init = move |
  3687. -//! slot,
  3688. -//! | -> ::core::result::Result<(), ::core::convert::Infallible> {
  3689. -//! init(slot).map(|__InitOk| ())
  3690. -//! };
  3691. -//! // Construct the initializer.
  3692. -//! let init = unsafe {
  3693. -//! ::kernel::init::pin_init_from_closure::<
  3694. -//! _,
  3695. -//! ::core::convert::Infallible,
  3696. -//! >(init)
  3697. -//! };
  3698. -//! init
  3699. -//! }
  3700. -//! }
  3701. -//! }
  3702. -//! ```
  3703. -//!
  3704. -//! ## `#[pin_data]` on `Foo`
  3705. -//!
  3706. -//! Since we already took a look at `#[pin_data]` on `Bar`, this section will only explain the
  3707. -//! differences/new things in the expansion of the `Foo` definition:
  3708. -//!
  3709. -//! ```rust,ignore
  3710. -//! #[pin_data(PinnedDrop)]
  3711. -//! struct Foo {
  3712. -//! a: usize,
  3713. -//! #[pin]
  3714. -//! b: Bar<u32>,
  3715. -//! }
  3716. -//! ```
  3717. -//!
  3718. -//! This expands to the following code:
  3719. -//!
  3720. -//! ```rust,ignore
  3721. -//! struct Foo {
  3722. -//! a: usize,
  3723. -//! b: Bar<u32>,
  3724. -//! }
  3725. -//! const _: () = {
  3726. -//! struct __ThePinData {
  3727. -//! __phantom: ::core::marker::PhantomData<fn(Foo) -> Foo>,
  3728. -//! }
  3729. -//! impl ::core::clone::Clone for __ThePinData {
  3730. -//! fn clone(&self) -> Self {
  3731. -//! *self
  3732. -//! }
  3733. -//! }
  3734. -//! impl ::core::marker::Copy for __ThePinData {}
  3735. -//! #[allow(dead_code)]
  3736. -//! impl __ThePinData {
  3737. -//! unsafe fn b<E>(
  3738. -//! self,
  3739. -//! slot: *mut Bar<u32>,
  3740. -//! init: impl ::kernel::init::PinInit<Bar<u32>, E>,
  3741. -//! ) -> ::core::result::Result<(), E> {
  3742. -//! unsafe { ::kernel::init::PinInit::__pinned_init(init, slot) }
  3743. -//! }
  3744. -//! unsafe fn a<E>(
  3745. -//! self,
  3746. -//! slot: *mut usize,
  3747. -//! init: impl ::kernel::init::Init<usize, E>,
  3748. -//! ) -> ::core::result::Result<(), E> {
  3749. -//! unsafe { ::kernel::init::Init::__init(init, slot) }
  3750. -//! }
  3751. -//! }
  3752. -//! unsafe impl ::kernel::init::__internal::HasPinData for Foo {
  3753. -//! type PinData = __ThePinData;
  3754. -//! unsafe fn __pin_data() -> Self::PinData {
  3755. -//! __ThePinData {
  3756. -//! __phantom: ::core::marker::PhantomData,
  3757. -//! }
  3758. -//! }
  3759. -//! }
  3760. -//! unsafe impl ::kernel::init::__internal::PinData for __ThePinData {
  3761. -//! type Datee = Foo;
  3762. -//! }
  3763. -//! #[allow(dead_code)]
  3764. -//! struct __Unpin<'__pin> {
  3765. -//! __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
  3766. -//! __phantom: ::core::marker::PhantomData<fn(Foo) -> Foo>,
  3767. -//! b: Bar<u32>,
  3768. -//! }
  3769. -//! #[doc(hidden)]
  3770. -//! impl<'__pin> ::core::marker::Unpin for Foo
  3771. -//! where
  3772. -//! __Unpin<'__pin>: ::core::marker::Unpin,
  3773. -//! {}
  3774. -//! // Since we specified `PinnedDrop` as the argument to `#[pin_data]`, we expect `Foo` to
  3775. -//! // implement `PinnedDrop`. Thus we do not need to prevent `Drop` implementations like
  3776. -//! // before, instead we implement `Drop` here and delegate to `PinnedDrop`.
  3777. -//! impl ::core::ops::Drop for Foo {
  3778. -//! fn drop(&mut self) {
  3779. -//! // Since we are getting dropped, no one else has a reference to `self` and thus we
  3780. -//! // can assume that we never move.
  3781. -//! let pinned = unsafe { ::core::pin::Pin::new_unchecked(self) };
  3782. -//! // Create the unsafe token that proves that we are inside of a destructor, this
  3783. -//! // type is only allowed to be created in a destructor.
  3784. -//! let token = unsafe { ::kernel::init::__internal::OnlyCallFromDrop::new() };
  3785. -//! ::kernel::init::PinnedDrop::drop(pinned, token);
  3786. -//! }
  3787. -//! }
  3788. -//! };
  3789. -//! ```
  3790. -//!
  3791. -//! ## `#[pinned_drop]` on `impl PinnedDrop for Foo`
  3792. -//!
  3793. -//! This macro is used to implement the `PinnedDrop` trait, since that trait is `unsafe` and has an
  3794. -//! extra parameter that should not be used at all. The macro hides that parameter.
  3795. -//!
  3796. -//! Here is the `PinnedDrop` impl for `Foo`:
  3797. -//!
  3798. -//! ```rust,ignore
  3799. -//! #[pinned_drop]
  3800. -//! impl PinnedDrop for Foo {
  3801. -//! fn drop(self: Pin<&mut Self>) {
  3802. -//! pr_info!("{self:p} is getting dropped.");
  3803. -//! }
  3804. -//! }
  3805. -//! ```
  3806. -//!
  3807. -//! This expands to the following code:
  3808. -//!
  3809. -//! ```rust,ignore
  3810. -//! // `unsafe`, full path and the token parameter are added, everything else stays the same.
  3811. -//! unsafe impl ::kernel::init::PinnedDrop for Foo {
  3812. -//! fn drop(self: Pin<&mut Self>, _: ::kernel::init::__internal::OnlyCallFromDrop) {
  3813. -//! pr_info!("{self:p} is getting dropped.");
  3814. -//! }
  3815. -//! }
  3816. -//! ```
  3817. -//!
  3818. -//! ## `pin_init!` on `Foo`
  3819. -//!
  3820. -//! Since we already took a look at `pin_init!` on `Bar`, this section will only show the expansion
  3821. -//! of `pin_init!` on `Foo`:
  3822. -//!
  3823. -//! ```rust,ignore
  3824. -//! let a = 42;
  3825. -//! let initializer = pin_init!(Foo {
  3826. -//! a,
  3827. -//! b <- Bar::new(36),
  3828. -//! });
  3829. -//! ```
  3830. -//!
  3831. -//! This expands to the following code:
  3832. -//!
  3833. -//! ```rust,ignore
  3834. -//! let a = 42;
  3835. -//! let initializer = {
  3836. -//! struct __InitOk;
  3837. -//! let data = unsafe {
  3838. -//! use ::kernel::init::__internal::HasPinData;
  3839. -//! Foo::__pin_data()
  3840. -//! };
  3841. -//! let init = ::kernel::init::__internal::PinData::make_closure::<
  3842. -//! _,
  3843. -//! __InitOk,
  3844. -//! ::core::convert::Infallible,
  3845. -//! >(data, move |slot| {
  3846. -//! {
  3847. -//! struct __InitOk;
  3848. -//! {
  3849. -//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).a), a) };
  3850. -//! }
  3851. -//! let __a_guard = unsafe {
  3852. -//! ::kernel::init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).a))
  3853. -//! };
  3854. -//! let init = Bar::new(36);
  3855. -//! unsafe { data.b(::core::addr_of_mut!((*slot).b), b)? };
  3856. -//! let __b_guard = unsafe {
  3857. -//! ::kernel::init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).b))
  3858. -//! };
  3859. -//! ::core::mem::forget(__b_guard);
  3860. -//! ::core::mem::forget(__a_guard);
  3861. -//! #[allow(unreachable_code, clippy::diverging_sub_expression)]
  3862. -//! let _ = || {
  3863. -//! unsafe {
  3864. -//! ::core::ptr::write(
  3865. -//! slot,
  3866. -//! Foo {
  3867. -//! a: ::core::panic!(),
  3868. -//! b: ::core::panic!(),
  3869. -//! },
  3870. -//! );
  3871. -//! };
  3872. -//! };
  3873. -//! }
  3874. -//! Ok(__InitOk)
  3875. -//! });
  3876. -//! let init = move |
  3877. -//! slot,
  3878. -//! | -> ::core::result::Result<(), ::core::convert::Infallible> {
  3879. -//! init(slot).map(|__InitOk| ())
  3880. -//! };
  3881. -//! let init = unsafe {
  3882. -//! ::kernel::init::pin_init_from_closure::<_, ::core::convert::Infallible>(init)
  3883. -//! };
  3884. -//! init
  3885. -//! };
  3886. -//! ```
  3887. -
  3888. -/// Creates a `unsafe impl<...> PinnedDrop for $type` block.
  3889. -///
  3890. -/// See [`PinnedDrop`] for more information.
  3891. -#[doc(hidden)]
  3892. -#[macro_export]
  3893. -macro_rules! __pinned_drop {
  3894. - (
  3895. - @impl_sig($($impl_sig:tt)*),
  3896. - @impl_body(
  3897. - $(#[$($attr:tt)*])*
  3898. - fn drop($($sig:tt)*) {
  3899. - $($inner:tt)*
  3900. - }
  3901. - ),
  3902. - ) => {
  3903. - unsafe $($impl_sig)* {
  3904. - // Inherit all attributes and the type/ident tokens for the signature.
  3905. - $(#[$($attr)*])*
  3906. - fn drop($($sig)*, _: $crate::init::__internal::OnlyCallFromDrop) {
  3907. - $($inner)*
  3908. - }
  3909. - }
  3910. - }
  3911. -}
  3912. -
  3913. -/// This macro first parses the struct definition such that it separates pinned and not pinned
  3914. -/// fields. Afterwards it declares the struct and implement the `PinData` trait safely.
  3915. -#[doc(hidden)]
  3916. -#[macro_export]
  3917. -macro_rules! __pin_data {
  3918. - // Proc-macro entry point, this is supplied by the proc-macro pre-parsing.
  3919. - (parse_input:
  3920. - @args($($pinned_drop:ident)?),
  3921. - @sig(
  3922. - $(#[$($struct_attr:tt)*])*
  3923. - $vis:vis struct $name:ident
  3924. - $(where $($whr:tt)*)?
  3925. - ),
  3926. - @impl_generics($($impl_generics:tt)*),
  3927. - @ty_generics($($ty_generics:tt)*),
  3928. - @decl_generics($($decl_generics:tt)*),
  3929. - @body({ $($fields:tt)* }),
  3930. - ) => {
  3931. - // We now use token munching to iterate through all of the fields. While doing this we
  3932. - // identify fields marked with `#[pin]`, these fields are the 'pinned fields'. The user
  3933. - // wants these to be structurally pinned. The rest of the fields are the
  3934. - // 'not pinned fields'. Additionally we collect all fields, since we need them in the right
  3935. - // order to declare the struct.
  3936. - //
  3937. - // In this call we also put some explaining comments for the parameters.
  3938. - $crate::__pin_data!(find_pinned_fields:
  3939. - // Attributes on the struct itself, these will just be propagated to be put onto the
  3940. - // struct definition.
  3941. - @struct_attrs($(#[$($struct_attr)*])*),
  3942. - // The visibility of the struct.
  3943. - @vis($vis),
  3944. - // The name of the struct.
  3945. - @name($name),
  3946. - // The 'impl generics', the generics that will need to be specified on the struct inside
  3947. - // of an `impl<$ty_generics>` block.
  3948. - @impl_generics($($impl_generics)*),
  3949. - // The 'ty generics', the generics that will need to be specified on the impl blocks.
  3950. - @ty_generics($($ty_generics)*),
  3951. - // The 'decl generics', the generics that need to be specified on the struct
  3952. - // definition.
  3953. - @decl_generics($($decl_generics)*),
  3954. - // The where clause of any impl block and the declaration.
  3955. - @where($($($whr)*)?),
  3956. - // The remaining fields tokens that need to be processed.
  3957. - // We add a `,` at the end to ensure correct parsing.
  3958. - @fields_munch($($fields)* ,),
  3959. - // The pinned fields.
  3960. - @pinned(),
  3961. - // The not pinned fields.
  3962. - @not_pinned(),
  3963. - // All fields.
  3964. - @fields(),
  3965. - // The accumulator containing all attributes already parsed.
  3966. - @accum(),
  3967. - // Contains `yes` or `` to indicate if `#[pin]` was found on the current field.
  3968. - @is_pinned(),
  3969. - // The proc-macro argument, this should be `PinnedDrop` or ``.
  3970. - @pinned_drop($($pinned_drop)?),
  3971. - );
  3972. - };
  3973. - (find_pinned_fields:
  3974. - @struct_attrs($($struct_attrs:tt)*),
  3975. - @vis($vis:vis),
  3976. - @name($name:ident),
  3977. - @impl_generics($($impl_generics:tt)*),
  3978. - @ty_generics($($ty_generics:tt)*),
  3979. - @decl_generics($($decl_generics:tt)*),
  3980. - @where($($whr:tt)*),
  3981. - // We found a PhantomPinned field, this should generally be pinned!
  3982. - @fields_munch($field:ident : $($($(::)?core::)?marker::)?PhantomPinned, $($rest:tt)*),
  3983. - @pinned($($pinned:tt)*),
  3984. - @not_pinned($($not_pinned:tt)*),
  3985. - @fields($($fields:tt)*),
  3986. - @accum($($accum:tt)*),
  3987. - // This field is not pinned.
  3988. - @is_pinned(),
  3989. - @pinned_drop($($pinned_drop:ident)?),
  3990. - ) => {
  3991. - ::core::compile_error!(concat!(
  3992. - "The field `",
  3993. - stringify!($field),
  3994. - "` of type `PhantomPinned` only has an effect, if it has the `#[pin]` attribute.",
  3995. - ));
  3996. - $crate::__pin_data!(find_pinned_fields:
  3997. - @struct_attrs($($struct_attrs)*),
  3998. - @vis($vis),
  3999. - @name($name),
  4000. - @impl_generics($($impl_generics)*),
  4001. - @ty_generics($($ty_generics)*),
  4002. - @decl_generics($($decl_generics)*),
  4003. - @where($($whr)*),
  4004. - @fields_munch($($rest)*),
  4005. - @pinned($($pinned)* $($accum)* $field: ::core::marker::PhantomPinned,),
  4006. - @not_pinned($($not_pinned)*),
  4007. - @fields($($fields)* $($accum)* $field: ::core::marker::PhantomPinned,),
  4008. - @accum(),
  4009. - @is_pinned(),
  4010. - @pinned_drop($($pinned_drop)?),
  4011. - );
  4012. - };
  4013. - (find_pinned_fields:
  4014. - @struct_attrs($($struct_attrs:tt)*),
  4015. - @vis($vis:vis),
  4016. - @name($name:ident),
  4017. - @impl_generics($($impl_generics:tt)*),
  4018. - @ty_generics($($ty_generics:tt)*),
  4019. - @decl_generics($($decl_generics:tt)*),
  4020. - @where($($whr:tt)*),
  4021. - // We reached the field declaration.
  4022. - @fields_munch($field:ident : $type:ty, $($rest:tt)*),
  4023. - @pinned($($pinned:tt)*),
  4024. - @not_pinned($($not_pinned:tt)*),
  4025. - @fields($($fields:tt)*),
  4026. - @accum($($accum:tt)*),
  4027. - // This field is pinned.
  4028. - @is_pinned(yes),
  4029. - @pinned_drop($($pinned_drop:ident)?),
  4030. - ) => {
  4031. - $crate::__pin_data!(find_pinned_fields:
  4032. - @struct_attrs($($struct_attrs)*),
  4033. - @vis($vis),
  4034. - @name($name),
  4035. - @impl_generics($($impl_generics)*),
  4036. - @ty_generics($($ty_generics)*),
  4037. - @decl_generics($($decl_generics)*),
  4038. - @where($($whr)*),
  4039. - @fields_munch($($rest)*),
  4040. - @pinned($($pinned)* $($accum)* $field: $type,),
  4041. - @not_pinned($($not_pinned)*),
  4042. - @fields($($fields)* $($accum)* $field: $type,),
  4043. - @accum(),
  4044. - @is_pinned(),
  4045. - @pinned_drop($($pinned_drop)?),
  4046. - );
  4047. - };
  4048. - (find_pinned_fields:
  4049. - @struct_attrs($($struct_attrs:tt)*),
  4050. - @vis($vis:vis),
  4051. - @name($name:ident),
  4052. - @impl_generics($($impl_generics:tt)*),
  4053. - @ty_generics($($ty_generics:tt)*),
  4054. - @decl_generics($($decl_generics:tt)*),
  4055. - @where($($whr:tt)*),
  4056. - // We reached the field declaration.
  4057. - @fields_munch($field:ident : $type:ty, $($rest:tt)*),
  4058. - @pinned($($pinned:tt)*),
  4059. - @not_pinned($($not_pinned:tt)*),
  4060. - @fields($($fields:tt)*),
  4061. - @accum($($accum:tt)*),
  4062. - // This field is not pinned.
  4063. - @is_pinned(),
  4064. - @pinned_drop($($pinned_drop:ident)?),
  4065. - ) => {
  4066. - $crate::__pin_data!(find_pinned_fields:
  4067. - @struct_attrs($($struct_attrs)*),
  4068. - @vis($vis),
  4069. - @name($name),
  4070. - @impl_generics($($impl_generics)*),
  4071. - @ty_generics($($ty_generics)*),
  4072. - @decl_generics($($decl_generics)*),
  4073. - @where($($whr)*),
  4074. - @fields_munch($($rest)*),
  4075. - @pinned($($pinned)*),
  4076. - @not_pinned($($not_pinned)* $($accum)* $field: $type,),
  4077. - @fields($($fields)* $($accum)* $field: $type,),
  4078. - @accum(),
  4079. - @is_pinned(),
  4080. - @pinned_drop($($pinned_drop)?),
  4081. - );
  4082. - };
  4083. - (find_pinned_fields:
  4084. - @struct_attrs($($struct_attrs:tt)*),
  4085. - @vis($vis:vis),
  4086. - @name($name:ident),
  4087. - @impl_generics($($impl_generics:tt)*),
  4088. - @ty_generics($($ty_generics:tt)*),
  4089. - @decl_generics($($decl_generics:tt)*),
  4090. - @where($($whr:tt)*),
  4091. - // We found the `#[pin]` attr.
  4092. - @fields_munch(#[pin] $($rest:tt)*),
  4093. - @pinned($($pinned:tt)*),
  4094. - @not_pinned($($not_pinned:tt)*),
  4095. - @fields($($fields:tt)*),
  4096. - @accum($($accum:tt)*),
  4097. - @is_pinned($($is_pinned:ident)?),
  4098. - @pinned_drop($($pinned_drop:ident)?),
  4099. - ) => {
  4100. - $crate::__pin_data!(find_pinned_fields:
  4101. - @struct_attrs($($struct_attrs)*),
  4102. - @vis($vis),
  4103. - @name($name),
  4104. - @impl_generics($($impl_generics)*),
  4105. - @ty_generics($($ty_generics)*),
  4106. - @decl_generics($($decl_generics)*),
  4107. - @where($($whr)*),
  4108. - @fields_munch($($rest)*),
  4109. - // We do not include `#[pin]` in the list of attributes, since it is not actually an
  4110. - // attribute that is defined somewhere.
  4111. - @pinned($($pinned)*),
  4112. - @not_pinned($($not_pinned)*),
  4113. - @fields($($fields)*),
  4114. - @accum($($accum)*),
  4115. - // Set this to `yes`.
  4116. - @is_pinned(yes),
  4117. - @pinned_drop($($pinned_drop)?),
  4118. - );
  4119. - };
  4120. - (find_pinned_fields:
  4121. - @struct_attrs($($struct_attrs:tt)*),
  4122. - @vis($vis:vis),
  4123. - @name($name:ident),
  4124. - @impl_generics($($impl_generics:tt)*),
  4125. - @ty_generics($($ty_generics:tt)*),
  4126. - @decl_generics($($decl_generics:tt)*),
  4127. - @where($($whr:tt)*),
  4128. - // We reached the field declaration with visibility, for simplicity we only munch the
  4129. - // visibility and put it into `$accum`.
  4130. - @fields_munch($fvis:vis $field:ident $($rest:tt)*),
  4131. - @pinned($($pinned:tt)*),
  4132. - @not_pinned($($not_pinned:tt)*),
  4133. - @fields($($fields:tt)*),
  4134. - @accum($($accum:tt)*),
  4135. - @is_pinned($($is_pinned:ident)?),
  4136. - @pinned_drop($($pinned_drop:ident)?),
  4137. - ) => {
  4138. - $crate::__pin_data!(find_pinned_fields:
  4139. - @struct_attrs($($struct_attrs)*),
  4140. - @vis($vis),
  4141. - @name($name),
  4142. - @impl_generics($($impl_generics)*),
  4143. - @ty_generics($($ty_generics)*),
  4144. - @decl_generics($($decl_generics)*),
  4145. - @where($($whr)*),
  4146. - @fields_munch($field $($rest)*),
  4147. - @pinned($($pinned)*),
  4148. - @not_pinned($($not_pinned)*),
  4149. - @fields($($fields)*),
  4150. - @accum($($accum)* $fvis),
  4151. - @is_pinned($($is_pinned)?),
  4152. - @pinned_drop($($pinned_drop)?),
  4153. - );
  4154. - };
  4155. - (find_pinned_fields:
  4156. - @struct_attrs($($struct_attrs:tt)*),
  4157. - @vis($vis:vis),
  4158. - @name($name:ident),
  4159. - @impl_generics($($impl_generics:tt)*),
  4160. - @ty_generics($($ty_generics:tt)*),
  4161. - @decl_generics($($decl_generics:tt)*),
  4162. - @where($($whr:tt)*),
  4163. - // Some other attribute, just put it into `$accum`.
  4164. - @fields_munch(#[$($attr:tt)*] $($rest:tt)*),
  4165. - @pinned($($pinned:tt)*),
  4166. - @not_pinned($($not_pinned:tt)*),
  4167. - @fields($($fields:tt)*),
  4168. - @accum($($accum:tt)*),
  4169. - @is_pinned($($is_pinned:ident)?),
  4170. - @pinned_drop($($pinned_drop:ident)?),
  4171. - ) => {
  4172. - $crate::__pin_data!(find_pinned_fields:
  4173. - @struct_attrs($($struct_attrs)*),
  4174. - @vis($vis),
  4175. - @name($name),
  4176. - @impl_generics($($impl_generics)*),
  4177. - @ty_generics($($ty_generics)*),
  4178. - @decl_generics($($decl_generics)*),
  4179. - @where($($whr)*),
  4180. - @fields_munch($($rest)*),
  4181. - @pinned($($pinned)*),
  4182. - @not_pinned($($not_pinned)*),
  4183. - @fields($($fields)*),
  4184. - @accum($($accum)* #[$($attr)*]),
  4185. - @is_pinned($($is_pinned)?),
  4186. - @pinned_drop($($pinned_drop)?),
  4187. - );
  4188. - };
  4189. - (find_pinned_fields:
  4190. - @struct_attrs($($struct_attrs:tt)*),
  4191. - @vis($vis:vis),
  4192. - @name($name:ident),
  4193. - @impl_generics($($impl_generics:tt)*),
  4194. - @ty_generics($($ty_generics:tt)*),
  4195. - @decl_generics($($decl_generics:tt)*),
  4196. - @where($($whr:tt)*),
  4197. - // We reached the end of the fields, plus an optional additional comma, since we added one
  4198. - // before and the user is also allowed to put a trailing comma.
  4199. - @fields_munch($(,)?),
  4200. - @pinned($($pinned:tt)*),
  4201. - @not_pinned($($not_pinned:tt)*),
  4202. - @fields($($fields:tt)*),
  4203. - @accum(),
  4204. - @is_pinned(),
  4205. - @pinned_drop($($pinned_drop:ident)?),
  4206. - ) => {
  4207. - // Declare the struct with all fields in the correct order.
  4208. - $($struct_attrs)*
  4209. - $vis struct $name <$($decl_generics)*>
  4210. - where $($whr)*
  4211. - {
  4212. - $($fields)*
  4213. - }
  4214. -
  4215. - // We put the rest into this const item, because it then will not be accessible to anything
  4216. - // outside.
  4217. - const _: () = {
  4218. - // We declare this struct which will host all of the projection function for our type.
  4219. - // it will be invariant over all generic parameters which are inherited from the
  4220. - // struct.
  4221. - $vis struct __ThePinData<$($impl_generics)*>
  4222. - where $($whr)*
  4223. - {
  4224. - __phantom: ::core::marker::PhantomData<
  4225. - fn($name<$($ty_generics)*>) -> $name<$($ty_generics)*>
  4226. - >,
  4227. - }
  4228. -
  4229. - impl<$($impl_generics)*> ::core::clone::Clone for __ThePinData<$($ty_generics)*>
  4230. - where $($whr)*
  4231. - {
  4232. - fn clone(&self) -> Self { *self }
  4233. - }
  4234. -
  4235. - impl<$($impl_generics)*> ::core::marker::Copy for __ThePinData<$($ty_generics)*>
  4236. - where $($whr)*
  4237. - {}
  4238. -
  4239. - // Make all projection functions.
  4240. - $crate::__pin_data!(make_pin_data:
  4241. - @pin_data(__ThePinData),
  4242. - @impl_generics($($impl_generics)*),
  4243. - @ty_generics($($ty_generics)*),
  4244. - @where($($whr)*),
  4245. - @pinned($($pinned)*),
  4246. - @not_pinned($($not_pinned)*),
  4247. - );
  4248. -
  4249. - // SAFETY: We have added the correct projection functions above to `__ThePinData` and
  4250. - // we also use the least restrictive generics possible.
  4251. - unsafe impl<$($impl_generics)*>
  4252. - $crate::init::__internal::HasPinData for $name<$($ty_generics)*>
  4253. - where $($whr)*
  4254. - {
  4255. - type PinData = __ThePinData<$($ty_generics)*>;
  4256. -
  4257. - unsafe fn __pin_data() -> Self::PinData {
  4258. - __ThePinData { __phantom: ::core::marker::PhantomData }
  4259. - }
  4260. - }
  4261. -
  4262. - unsafe impl<$($impl_generics)*>
  4263. - $crate::init::__internal::PinData for __ThePinData<$($ty_generics)*>
  4264. - where $($whr)*
  4265. - {
  4266. - type Datee = $name<$($ty_generics)*>;
  4267. - }
  4268. -
  4269. - // This struct will be used for the unpin analysis. Since only structurally pinned
  4270. - // fields are relevant whether the struct should implement `Unpin`.
  4271. - #[allow(dead_code)]
  4272. - struct __Unpin <'__pin, $($impl_generics)*>
  4273. - where $($whr)*
  4274. - {
  4275. - __phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
  4276. - __phantom: ::core::marker::PhantomData<
  4277. - fn($name<$($ty_generics)*>) -> $name<$($ty_generics)*>
  4278. - >,
  4279. - // Only the pinned fields.
  4280. - $($pinned)*
  4281. - }
  4282. -
  4283. - #[doc(hidden)]
  4284. - impl<'__pin, $($impl_generics)*> ::core::marker::Unpin for $name<$($ty_generics)*>
  4285. - where
  4286. - __Unpin<'__pin, $($ty_generics)*>: ::core::marker::Unpin,
  4287. - $($whr)*
  4288. - {}
  4289. -
  4290. - // We need to disallow normal `Drop` implementation, the exact behavior depends on
  4291. - // whether `PinnedDrop` was specified as the parameter.
  4292. - $crate::__pin_data!(drop_prevention:
  4293. - @name($name),
  4294. - @impl_generics($($impl_generics)*),
  4295. - @ty_generics($($ty_generics)*),
  4296. - @where($($whr)*),
  4297. - @pinned_drop($($pinned_drop)?),
  4298. - );
  4299. - };
  4300. - };
  4301. - // When no `PinnedDrop` was specified, then we have to prevent implementing drop.
  4302. - (drop_prevention:
  4303. - @name($name:ident),
  4304. - @impl_generics($($impl_generics:tt)*),
  4305. - @ty_generics($($ty_generics:tt)*),
  4306. - @where($($whr:tt)*),
  4307. - @pinned_drop(),
  4308. - ) => {
  4309. - // We prevent this by creating a trait that will be implemented for all types implementing
  4310. - // `Drop`. Additionally we will implement this trait for the struct leading to a conflict,
  4311. - // if it also implements `Drop`
  4312. - trait MustNotImplDrop {}
  4313. - #[allow(drop_bounds)]
  4314. - impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
  4315. - impl<$($impl_generics)*> MustNotImplDrop for $name<$($ty_generics)*>
  4316. - where $($whr)* {}
  4317. - // We also take care to prevent users from writing a useless `PinnedDrop` implementation.
  4318. - // They might implement `PinnedDrop` correctly for the struct, but forget to give
  4319. - // `PinnedDrop` as the parameter to `#[pin_data]`.
  4320. - #[allow(non_camel_case_types)]
  4321. - trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
  4322. - impl<T: $crate::init::PinnedDrop>
  4323. - UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
  4324. - impl<$($impl_generics)*>
  4325. - UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for $name<$($ty_generics)*>
  4326. - where $($whr)* {}
  4327. - };
  4328. - // When `PinnedDrop` was specified we just implement `Drop` and delegate.
  4329. - (drop_prevention:
  4330. - @name($name:ident),
  4331. - @impl_generics($($impl_generics:tt)*),
  4332. - @ty_generics($($ty_generics:tt)*),
  4333. - @where($($whr:tt)*),
  4334. - @pinned_drop(PinnedDrop),
  4335. - ) => {
  4336. - impl<$($impl_generics)*> ::core::ops::Drop for $name<$($ty_generics)*>
  4337. - where $($whr)*
  4338. - {
  4339. - fn drop(&mut self) {
  4340. - // SAFETY: Since this is a destructor, `self` will not move after this function
  4341. - // terminates, since it is inaccessible.
  4342. - let pinned = unsafe { ::core::pin::Pin::new_unchecked(self) };
  4343. - // SAFETY: Since this is a drop function, we can create this token to call the
  4344. - // pinned destructor of this type.
  4345. - let token = unsafe { $crate::init::__internal::OnlyCallFromDrop::new() };
  4346. - $crate::init::PinnedDrop::drop(pinned, token);
  4347. - }
  4348. - }
  4349. - };
  4350. - // If some other parameter was specified, we emit a readable error.
  4351. - (drop_prevention:
  4352. - @name($name:ident),
  4353. - @impl_generics($($impl_generics:tt)*),
  4354. - @ty_generics($($ty_generics:tt)*),
  4355. - @where($($whr:tt)*),
  4356. - @pinned_drop($($rest:tt)*),
  4357. - ) => {
  4358. - compile_error!(
  4359. - "Wrong parameters to `#[pin_data]`, expected nothing or `PinnedDrop`, got '{}'.",
  4360. - stringify!($($rest)*),
  4361. - );
  4362. - };
  4363. - (make_pin_data:
  4364. - @pin_data($pin_data:ident),
  4365. - @impl_generics($($impl_generics:tt)*),
  4366. - @ty_generics($($ty_generics:tt)*),
  4367. - @where($($whr:tt)*),
  4368. - @pinned($($(#[$($p_attr:tt)*])* $pvis:vis $p_field:ident : $p_type:ty),* $(,)?),
  4369. - @not_pinned($($(#[$($attr:tt)*])* $fvis:vis $field:ident : $type:ty),* $(,)?),
  4370. - ) => {
  4371. - // For every field, we create a projection function according to its projection type. If a
  4372. - // field is structurally pinned, then it must be initialized via `PinInit`, if it is not
  4373. - // structurally pinned, then it can be initialized via `Init`.
  4374. - //
  4375. - // The functions are `unsafe` to prevent accidentally calling them.
  4376. - #[allow(dead_code)]
  4377. - impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
  4378. - where $($whr)*
  4379. - {
  4380. - $(
  4381. - $(#[$($p_attr)*])*
  4382. - $pvis unsafe fn $p_field<E>(
  4383. - self,
  4384. - slot: *mut $p_type,
  4385. - init: impl $crate::init::PinInit<$p_type, E>,
  4386. - ) -> ::core::result::Result<(), E> {
  4387. - unsafe { $crate::init::PinInit::__pinned_init(init, slot) }
  4388. - }
  4389. - )*
  4390. - $(
  4391. - $(#[$($attr)*])*
  4392. - $fvis unsafe fn $field<E>(
  4393. - self,
  4394. - slot: *mut $type,
  4395. - init: impl $crate::init::Init<$type, E>,
  4396. - ) -> ::core::result::Result<(), E> {
  4397. - unsafe { $crate::init::Init::__init(init, slot) }
  4398. - }
  4399. - )*
  4400. - }
  4401. - };
  4402. -}
  4403. -
  4404. -/// The internal init macro. Do not call manually!
  4405. -///
  4406. -/// This is called by the `{try_}{pin_}init!` macros with various inputs.
  4407. -///
  4408. -/// This macro has multiple internal call configurations, these are always the very first ident:
  4409. -/// - nothing: this is the base case and called by the `{try_}{pin_}init!` macros.
  4410. -/// - `with_update_parsed`: when the `..Zeroable::zeroed()` syntax has been handled.
  4411. -/// - `init_slot`: recursively creates the code that initializes all fields in `slot`.
  4412. -/// - `make_initializer`: recursively create the struct initializer that guarantees that every
  4413. -/// field has been initialized exactly once.
  4414. -#[doc(hidden)]
  4415. -#[macro_export]
  4416. -macro_rules! __init_internal {
  4417. - (
  4418. - @this($($this:ident)?),
  4419. - @typ($t:path),
  4420. - @fields($($fields:tt)*),
  4421. - @error($err:ty),
  4422. - // Either `PinData` or `InitData`, `$use_data` should only be present in the `PinData`
  4423. - // case.
  4424. - @data($data:ident, $($use_data:ident)?),
  4425. - // `HasPinData` or `HasInitData`.
  4426. - @has_data($has_data:ident, $get_data:ident),
  4427. - // `pin_init_from_closure` or `init_from_closure`.
  4428. - @construct_closure($construct_closure:ident),
  4429. - @munch_fields(),
  4430. - ) => {
  4431. - $crate::__init_internal!(with_update_parsed:
  4432. - @this($($this)?),
  4433. - @typ($t),
  4434. - @fields($($fields)*),
  4435. - @error($err),
  4436. - @data($data, $($use_data)?),
  4437. - @has_data($has_data, $get_data),
  4438. - @construct_closure($construct_closure),
  4439. - @zeroed(), // Nothing means default behavior.
  4440. - )
  4441. - };
  4442. - (
  4443. - @this($($this:ident)?),
  4444. - @typ($t:path),
  4445. - @fields($($fields:tt)*),
  4446. - @error($err:ty),
  4447. - // Either `PinData` or `InitData`, `$use_data` should only be present in the `PinData`
  4448. - // case.
  4449. - @data($data:ident, $($use_data:ident)?),
  4450. - // `HasPinData` or `HasInitData`.
  4451. - @has_data($has_data:ident, $get_data:ident),
  4452. - // `pin_init_from_closure` or `init_from_closure`.
  4453. - @construct_closure($construct_closure:ident),
  4454. - @munch_fields(..Zeroable::zeroed()),
  4455. - ) => {
  4456. - $crate::__init_internal!(with_update_parsed:
  4457. - @this($($this)?),
  4458. - @typ($t),
  4459. - @fields($($fields)*),
  4460. - @error($err),
  4461. - @data($data, $($use_data)?),
  4462. - @has_data($has_data, $get_data),
  4463. - @construct_closure($construct_closure),
  4464. - @zeroed(()), // `()` means zero all fields not mentioned.
  4465. - )
  4466. - };
  4467. - (
  4468. - @this($($this:ident)?),
  4469. - @typ($t:path),
  4470. - @fields($($fields:tt)*),
  4471. - @error($err:ty),
  4472. - // Either `PinData` or `InitData`, `$use_data` should only be present in the `PinData`
  4473. - // case.
  4474. - @data($data:ident, $($use_data:ident)?),
  4475. - // `HasPinData` or `HasInitData`.
  4476. - @has_data($has_data:ident, $get_data:ident),
  4477. - // `pin_init_from_closure` or `init_from_closure`.
  4478. - @construct_closure($construct_closure:ident),
  4479. - @munch_fields($ignore:tt $($rest:tt)*),
  4480. - ) => {
  4481. - $crate::__init_internal!(
  4482. - @this($($this)?),
  4483. - @typ($t),
  4484. - @fields($($fields)*),
  4485. - @error($err),
  4486. - @data($data, $($use_data)?),
  4487. - @has_data($has_data, $get_data),
  4488. - @construct_closure($construct_closure),
  4489. - @munch_fields($($rest)*),
  4490. - )
  4491. - };
  4492. - (with_update_parsed:
  4493. - @this($($this:ident)?),
  4494. - @typ($t:path),
  4495. - @fields($($fields:tt)*),
  4496. - @error($err:ty),
  4497. - // Either `PinData` or `InitData`, `$use_data` should only be present in the `PinData`
  4498. - // case.
  4499. - @data($data:ident, $($use_data:ident)?),
  4500. - // `HasPinData` or `HasInitData`.
  4501. - @has_data($has_data:ident, $get_data:ident),
  4502. - // `pin_init_from_closure` or `init_from_closure`.
  4503. - @construct_closure($construct_closure:ident),
  4504. - @zeroed($($init_zeroed:expr)?),
  4505. - ) => {{
  4506. - // We do not want to allow arbitrary returns, so we declare this type as the `Ok` return
  4507. - // type and shadow it later when we insert the arbitrary user code. That way there will be
  4508. - // no possibility of returning without `unsafe`.
  4509. - struct __InitOk;
  4510. - // Get the data about fields from the supplied type.
  4511. - let data = unsafe {
  4512. - use $crate::init::__internal::$has_data;
  4513. - // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
  4514. - // information that is associated to already parsed fragments, so a path fragment
  4515. - // cannot be used in this position. Doing the retokenization results in valid rust
  4516. - // code.
  4517. - ::kernel::macros::paste!($t::$get_data())
  4518. - };
  4519. - // Ensure that `data` really is of type `$data` and help with type inference:
  4520. - let init = $crate::init::__internal::$data::make_closure::<_, __InitOk, $err>(
  4521. - data,
  4522. - move |slot| {
  4523. - {
  4524. - // Shadow the structure so it cannot be used to return early.
  4525. - struct __InitOk;
  4526. - // If `$init_zeroed` is present we should zero the slot now and not emit an
  4527. - // error when fields are missing (since they will be zeroed). We also have to
  4528. - // check that the type actually implements `Zeroable`.
  4529. - $({
  4530. - fn assert_zeroable<T: $crate::init::Zeroable>(_: *mut T) {}
  4531. - // Ensure that the struct is indeed `Zeroable`.
  4532. - assert_zeroable(slot);
  4533. - // SAFETY: The type implements `Zeroable` by the check above.
  4534. - unsafe { ::core::ptr::write_bytes(slot, 0, 1) };
  4535. - $init_zeroed // This will be `()` if set.
  4536. - })?
  4537. - // Create the `this` so it can be referenced by the user inside of the
  4538. - // expressions creating the individual fields.
  4539. - $(let $this = unsafe { ::core::ptr::NonNull::new_unchecked(slot) };)?
  4540. - // Initialize every field.
  4541. - $crate::__init_internal!(init_slot($($use_data)?):
  4542. - @data(data),
  4543. - @slot(slot),
  4544. - @guards(),
  4545. - @munch_fields($($fields)*,),
  4546. - );
  4547. - // We use unreachable code to ensure that all fields have been mentioned exactly
  4548. - // once, this struct initializer will still be type-checked and complain with a
  4549. - // very natural error message if a field is forgotten/mentioned more than once.
  4550. - #[allow(unreachable_code, clippy::diverging_sub_expression)]
  4551. - let _ = || {
  4552. - $crate::__init_internal!(make_initializer:
  4553. - @slot(slot),
  4554. - @type_name($t),
  4555. - @munch_fields($($fields)*,),
  4556. - @acc(),
  4557. - );
  4558. - };
  4559. - }
  4560. - Ok(__InitOk)
  4561. - }
  4562. - );
  4563. - let init = move |slot| -> ::core::result::Result<(), $err> {
  4564. - init(slot).map(|__InitOk| ())
  4565. - };
  4566. - let init = unsafe { $crate::init::$construct_closure::<_, $err>(init) };
  4567. - init
  4568. - }};
  4569. - (init_slot($($use_data:ident)?):
  4570. - @data($data:ident),
  4571. - @slot($slot:ident),
  4572. - @guards($($guards:ident,)*),
  4573. - @munch_fields($(..Zeroable::zeroed())? $(,)?),
  4574. - ) => {
  4575. - // Endpoint of munching, no fields are left. If execution reaches this point, all fields
  4576. - // have been initialized. Therefore we can now dismiss the guards by forgetting them.
  4577. - $(::core::mem::forget($guards);)*
  4578. - };
  4579. - (init_slot($use_data:ident): // `use_data` is present, so we use the `data` to init fields.
  4580. - @data($data:ident),
  4581. - @slot($slot:ident),
  4582. - @guards($($guards:ident,)*),
  4583. - // In-place initialization syntax.
  4584. - @munch_fields($field:ident <- $val:expr, $($rest:tt)*),
  4585. - ) => {
  4586. - let init = $val;
  4587. - // Call the initializer.
  4588. - //
  4589. - // SAFETY: `slot` is valid, because we are inside of an initializer closure, we
  4590. - // return when an error/panic occurs.
  4591. - // We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`.
  4592. - unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), init)? };
  4593. - // Create the drop guard:
  4594. - //
  4595. - // We rely on macro hygiene to make it impossible for users to access this local variable.
  4596. - // We use `paste!` to create new hygiene for `$field`.
  4597. - ::kernel::macros::paste! {
  4598. - // SAFETY: We forget the guard later when initialization has succeeded.
  4599. - let [< __ $field _guard >] = unsafe {
  4600. - $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
  4601. - };
  4602. -
  4603. - $crate::__init_internal!(init_slot($use_data):
  4604. - @data($data),
  4605. - @slot($slot),
  4606. - @guards([< __ $field _guard >], $($guards,)*),
  4607. - @munch_fields($($rest)*),
  4608. - );
  4609. - }
  4610. - };
  4611. - (init_slot(): // No `use_data`, so we use `Init::__init` directly.
  4612. - @data($data:ident),
  4613. - @slot($slot:ident),
  4614. - @guards($($guards:ident,)*),
  4615. - // In-place initialization syntax.
  4616. - @munch_fields($field:ident <- $val:expr, $($rest:tt)*),
  4617. - ) => {
  4618. - let init = $val;
  4619. - // Call the initializer.
  4620. - //
  4621. - // SAFETY: `slot` is valid, because we are inside of an initializer closure, we
  4622. - // return when an error/panic occurs.
  4623. - unsafe { $crate::init::Init::__init(init, ::core::ptr::addr_of_mut!((*$slot).$field))? };
  4624. - // Create the drop guard:
  4625. - //
  4626. - // We rely on macro hygiene to make it impossible for users to access this local variable.
  4627. - // We use `paste!` to create new hygiene for `$field`.
  4628. - ::kernel::macros::paste! {
  4629. - // SAFETY: We forget the guard later when initialization has succeeded.
  4630. - let [< __ $field _guard >] = unsafe {
  4631. - $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
  4632. - };
  4633. -
  4634. - $crate::__init_internal!(init_slot():
  4635. - @data($data),
  4636. - @slot($slot),
  4637. - @guards([< __ $field _guard >], $($guards,)*),
  4638. - @munch_fields($($rest)*),
  4639. - );
  4640. - }
  4641. - };
  4642. - (init_slot($($use_data:ident)?):
  4643. - @data($data:ident),
  4644. - @slot($slot:ident),
  4645. - @guards($($guards:ident,)*),
  4646. - // Init by-value.
  4647. - @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
  4648. - ) => {
  4649. - {
  4650. - $(let $field = $val;)?
  4651. - // Initialize the field.
  4652. - //
  4653. - // SAFETY: The memory at `slot` is uninitialized.
  4654. - unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
  4655. - }
  4656. - // Create the drop guard:
  4657. - //
  4658. - // We rely on macro hygiene to make it impossible for users to access this local variable.
  4659. - // We use `paste!` to create new hygiene for `$field`.
  4660. - ::kernel::macros::paste! {
  4661. - // SAFETY: We forget the guard later when initialization has succeeded.
  4662. - let [< __ $field _guard >] = unsafe {
  4663. - $crate::init::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
  4664. - };
  4665. -
  4666. - $crate::__init_internal!(init_slot($($use_data)?):
  4667. - @data($data),
  4668. - @slot($slot),
  4669. - @guards([< __ $field _guard >], $($guards,)*),
  4670. - @munch_fields($($rest)*),
  4671. - );
  4672. - }
  4673. - };
  4674. - (make_initializer:
  4675. - @slot($slot:ident),
  4676. - @type_name($t:path),
  4677. - @munch_fields(..Zeroable::zeroed() $(,)?),
  4678. - @acc($($acc:tt)*),
  4679. - ) => {
  4680. - // Endpoint, nothing more to munch, create the initializer. Since the users specified
  4681. - // `..Zeroable::zeroed()`, the slot will already have been zeroed and all field that have
  4682. - // not been overwritten are thus zero and initialized. We still check that all fields are
  4683. - // actually accessible by using the struct update syntax ourselves.
  4684. - // We are inside of a closure that is never executed and thus we can abuse `slot` to
  4685. - // get the correct type inference here:
  4686. - #[allow(unused_assignments)]
  4687. - unsafe {
  4688. - let mut zeroed = ::core::mem::zeroed();
  4689. - // We have to use type inference here to make zeroed have the correct type. This does
  4690. - // not get executed, so it has no effect.
  4691. - ::core::ptr::write($slot, zeroed);
  4692. - zeroed = ::core::mem::zeroed();
  4693. - // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
  4694. - // information that is associated to already parsed fragments, so a path fragment
  4695. - // cannot be used in this position. Doing the retokenization results in valid rust
  4696. - // code.
  4697. - ::kernel::macros::paste!(
  4698. - ::core::ptr::write($slot, $t {
  4699. - $($acc)*
  4700. - ..zeroed
  4701. - });
  4702. - );
  4703. - }
  4704. - };
  4705. - (make_initializer:
  4706. - @slot($slot:ident),
  4707. - @type_name($t:path),
  4708. - @munch_fields($(,)?),
  4709. - @acc($($acc:tt)*),
  4710. - ) => {
  4711. - // Endpoint, nothing more to munch, create the initializer.
  4712. - // Since we are in the closure that is never called, this will never get executed.
  4713. - // We abuse `slot` to get the correct type inference here:
  4714. - unsafe {
  4715. - // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
  4716. - // information that is associated to already parsed fragments, so a path fragment
  4717. - // cannot be used in this position. Doing the retokenization results in valid rust
  4718. - // code.
  4719. - ::kernel::macros::paste!(
  4720. - ::core::ptr::write($slot, $t {
  4721. - $($acc)*
  4722. - });
  4723. - );
  4724. - }
  4725. - };
  4726. - (make_initializer:
  4727. - @slot($slot:ident),
  4728. - @type_name($t:path),
  4729. - @munch_fields($field:ident <- $val:expr, $($rest:tt)*),
  4730. - @acc($($acc:tt)*),
  4731. - ) => {
  4732. - $crate::__init_internal!(make_initializer:
  4733. - @slot($slot),
  4734. - @type_name($t),
  4735. - @munch_fields($($rest)*),
  4736. - @acc($($acc)* $field: ::core::panic!(),),
  4737. - );
  4738. - };
  4739. - (make_initializer:
  4740. - @slot($slot:ident),
  4741. - @type_name($t:path),
  4742. - @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
  4743. - @acc($($acc:tt)*),
  4744. - ) => {
  4745. - $crate::__init_internal!(make_initializer:
  4746. - @slot($slot),
  4747. - @type_name($t),
  4748. - @munch_fields($($rest)*),
  4749. - @acc($($acc)* $field: ::core::panic!(),),
  4750. - );
  4751. - };
  4752. -}
  4753. -
  4754. -#[doc(hidden)]
  4755. -#[macro_export]
  4756. -macro_rules! __derive_zeroable {
  4757. - (parse_input:
  4758. - @sig(
  4759. - $(#[$($struct_attr:tt)*])*
  4760. - $vis:vis struct $name:ident
  4761. - $(where $($whr:tt)*)?
  4762. - ),
  4763. - @impl_generics($($impl_generics:tt)*),
  4764. - @ty_generics($($ty_generics:tt)*),
  4765. - @body({
  4766. - $(
  4767. - $(#[$($field_attr:tt)*])*
  4768. - $field:ident : $field_ty:ty
  4769. - ),* $(,)?
  4770. - }),
  4771. - ) => {
  4772. - // SAFETY: Every field type implements `Zeroable` and padding bytes may be zero.
  4773. - #[automatically_derived]
  4774. - unsafe impl<$($impl_generics)*> $crate::init::Zeroable for $name<$($ty_generics)*>
  4775. - where
  4776. - $($($whr)*)?
  4777. - {}
  4778. - const _: () = {
  4779. - fn assert_zeroable<T: ?::core::marker::Sized + $crate::init::Zeroable>() {}
  4780. - fn ensure_zeroable<$($impl_generics)*>()
  4781. - where $($($whr)*)?
  4782. - {
  4783. - $(assert_zeroable::<$field_ty>();)*
  4784. - }
  4785. - };
  4786. - };
  4787. -}
  4788. diff --git a/rust/kernel/ioctl.rs b/rust/kernel/ioctl.rs
  4789. deleted file mode 100644
  4790. --- a/rust/kernel/ioctl.rs
  4791. +++ /dev/null
  4792. @@ -1,72 +0,0 @@
  4793. -// SPDX-License-Identifier: GPL-2.0
  4794. -
  4795. -//! `ioctl()` number definitions.
  4796. -//!
  4797. -//! C header: [`include/asm-generic/ioctl.h`](srctree/include/asm-generic/ioctl.h)
  4798. -
  4799. -#![allow(non_snake_case)]
  4800. -
  4801. -use crate::build_assert;
  4802. -
  4803. -/// Build an ioctl number, analogous to the C macro of the same name.
  4804. -#[inline(always)]
  4805. -const fn _IOC(dir: u32, ty: u32, nr: u32, size: usize) -> u32 {
  4806. - build_assert!(dir <= uapi::_IOC_DIRMASK);
  4807. - build_assert!(ty <= uapi::_IOC_TYPEMASK);
  4808. - build_assert!(nr <= uapi::_IOC_NRMASK);
  4809. - build_assert!(size <= (uapi::_IOC_SIZEMASK as usize));
  4810. -
  4811. - (dir << uapi::_IOC_DIRSHIFT)
  4812. - | (ty << uapi::_IOC_TYPESHIFT)
  4813. - | (nr << uapi::_IOC_NRSHIFT)
  4814. - | ((size as u32) << uapi::_IOC_SIZESHIFT)
  4815. -}
  4816. -
  4817. -/// Build an ioctl number for an argumentless ioctl.
  4818. -#[inline(always)]
  4819. -pub const fn _IO(ty: u32, nr: u32) -> u32 {
  4820. - _IOC(uapi::_IOC_NONE, ty, nr, 0)
  4821. -}
  4822. -
  4823. -/// Build an ioctl number for a read-only ioctl.
  4824. -#[inline(always)]
  4825. -pub const fn _IOR<T>(ty: u32, nr: u32) -> u32 {
  4826. - _IOC(uapi::_IOC_READ, ty, nr, core::mem::size_of::<T>())
  4827. -}
  4828. -
  4829. -/// Build an ioctl number for a write-only ioctl.
  4830. -#[inline(always)]
  4831. -pub const fn _IOW<T>(ty: u32, nr: u32) -> u32 {
  4832. - _IOC(uapi::_IOC_WRITE, ty, nr, core::mem::size_of::<T>())
  4833. -}
  4834. -
  4835. -/// Build an ioctl number for a read-write ioctl.
  4836. -#[inline(always)]
  4837. -pub const fn _IOWR<T>(ty: u32, nr: u32) -> u32 {
  4838. - _IOC(
  4839. - uapi::_IOC_READ | uapi::_IOC_WRITE,
  4840. - ty,
  4841. - nr,
  4842. - core::mem::size_of::<T>(),
  4843. - )
  4844. -}
  4845. -
  4846. -/// Get the ioctl direction from an ioctl number.
  4847. -pub const fn _IOC_DIR(nr: u32) -> u32 {
  4848. - (nr >> uapi::_IOC_DIRSHIFT) & uapi::_IOC_DIRMASK
  4849. -}
  4850. -
  4851. -/// Get the ioctl type from an ioctl number.
  4852. -pub const fn _IOC_TYPE(nr: u32) -> u32 {
  4853. - (nr >> uapi::_IOC_TYPESHIFT) & uapi::_IOC_TYPEMASK
  4854. -}
  4855. -
  4856. -/// Get the ioctl number from an ioctl number.
  4857. -pub const fn _IOC_NR(nr: u32) -> u32 {
  4858. - (nr >> uapi::_IOC_NRSHIFT) & uapi::_IOC_NRMASK
  4859. -}
  4860. -
  4861. -/// Get the ioctl size from an ioctl number.
  4862. -pub const fn _IOC_SIZE(nr: u32) -> usize {
  4863. - ((nr >> uapi::_IOC_SIZESHIFT) & uapi::_IOC_SIZEMASK) as usize
  4864. -}
  4865. diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
  4866. deleted file mode 100644
  4867. --- a/rust/kernel/kunit.rs
  4868. +++ /dev/null
  4869. @@ -1,163 +0,0 @@
  4870. -// SPDX-License-Identifier: GPL-2.0
  4871. -
  4872. -//! KUnit-based macros for Rust unit tests.
  4873. -//!
  4874. -//! C header: [`include/kunit/test.h`](srctree/include/kunit/test.h)
  4875. -//!
  4876. -//! Reference: <https://docs.kernel.org/dev-tools/kunit/index.html>
  4877. -
  4878. -use core::{ffi::c_void, fmt};
  4879. -
  4880. -/// Prints a KUnit error-level message.
  4881. -///
  4882. -/// Public but hidden since it should only be used from KUnit generated code.
  4883. -#[doc(hidden)]
  4884. -pub fn err(args: fmt::Arguments<'_>) {
  4885. - // SAFETY: The format string is null-terminated and the `%pA` specifier matches the argument we
  4886. - // are passing.
  4887. - #[cfg(CONFIG_PRINTK)]
  4888. - unsafe {
  4889. - bindings::_printk(
  4890. - b"\x013%pA\0".as_ptr() as _,
  4891. - &args as *const _ as *const c_void,
  4892. - );
  4893. - }
  4894. -}
  4895. -
  4896. -/// Prints a KUnit info-level message.
  4897. -///
  4898. -/// Public but hidden since it should only be used from KUnit generated code.
  4899. -#[doc(hidden)]
  4900. -pub fn info(args: fmt::Arguments<'_>) {
  4901. - // SAFETY: The format string is null-terminated and the `%pA` specifier matches the argument we
  4902. - // are passing.
  4903. - #[cfg(CONFIG_PRINTK)]
  4904. - unsafe {
  4905. - bindings::_printk(
  4906. - b"\x016%pA\0".as_ptr() as _,
  4907. - &args as *const _ as *const c_void,
  4908. - );
  4909. - }
  4910. -}
  4911. -
  4912. -/// Asserts that a boolean expression is `true` at runtime.
  4913. -///
  4914. -/// Public but hidden since it should only be used from generated tests.
  4915. -///
  4916. -/// Unlike the one in `core`, this one does not panic; instead, it is mapped to the KUnit
  4917. -/// facilities. See [`assert!`] for more details.
  4918. -#[doc(hidden)]
  4919. -#[macro_export]
  4920. -macro_rules! kunit_assert {
  4921. - ($name:literal, $file:literal, $diff:expr, $condition:expr $(,)?) => {
  4922. - 'out: {
  4923. - // Do nothing if the condition is `true`.
  4924. - if $condition {
  4925. - break 'out;
  4926. - }
  4927. -
  4928. - static FILE: &'static $crate::str::CStr = $crate::c_str!($file);
  4929. - static LINE: i32 = core::line!() as i32 - $diff;
  4930. - static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition));
  4931. -
  4932. - // SAFETY: FFI call without safety requirements.
  4933. - let kunit_test = unsafe { $crate::bindings::kunit_get_current_test() };
  4934. - if kunit_test.is_null() {
  4935. - // The assertion failed but this task is not running a KUnit test, so we cannot call
  4936. - // KUnit, but at least print an error to the kernel log. This may happen if this
  4937. - // macro is called from an spawned thread in a test (see
  4938. - // `scripts/rustdoc_test_gen.rs`) or if some non-test code calls this macro by
  4939. - // mistake (it is hidden to prevent that).
  4940. - //
  4941. - // This mimics KUnit's failed assertion format.
  4942. - $crate::kunit::err(format_args!(
  4943. - " # {}: ASSERTION FAILED at {FILE}:{LINE}\n",
  4944. - $name
  4945. - ));
  4946. - $crate::kunit::err(format_args!(
  4947. - " Expected {CONDITION} to be true, but is false\n"
  4948. - ));
  4949. - $crate::kunit::err(format_args!(
  4950. - " Failure not reported to KUnit since this is a non-KUnit task\n"
  4951. - ));
  4952. - break 'out;
  4953. - }
  4954. -
  4955. - #[repr(transparent)]
  4956. - struct Location($crate::bindings::kunit_loc);
  4957. -
  4958. - #[repr(transparent)]
  4959. - struct UnaryAssert($crate::bindings::kunit_unary_assert);
  4960. -
  4961. - // SAFETY: There is only a static instance and in that one the pointer field points to
  4962. - // an immutable C string.
  4963. - unsafe impl Sync for Location {}
  4964. -
  4965. - // SAFETY: There is only a static instance and in that one the pointer field points to
  4966. - // an immutable C string.
  4967. - unsafe impl Sync for UnaryAssert {}
  4968. -
  4969. - static LOCATION: Location = Location($crate::bindings::kunit_loc {
  4970. - file: FILE.as_char_ptr(),
  4971. - line: LINE,
  4972. - });
  4973. - static ASSERTION: UnaryAssert = UnaryAssert($crate::bindings::kunit_unary_assert {
  4974. - assert: $crate::bindings::kunit_assert {},
  4975. - condition: CONDITION.as_char_ptr(),
  4976. - expected_true: true,
  4977. - });
  4978. -
  4979. - // SAFETY:
  4980. - // - FFI call.
  4981. - // - The `kunit_test` pointer is valid because we got it from
  4982. - // `kunit_get_current_test()` and it was not null. This means we are in a KUnit
  4983. - // test, and that the pointer can be passed to KUnit functions and assertions.
  4984. - // - The string pointers (`file` and `condition` above) point to null-terminated
  4985. - // strings since they are `CStr`s.
  4986. - // - The function pointer (`format`) points to the proper function.
  4987. - // - The pointers passed will remain valid since they point to `static`s.
  4988. - // - The format string is allowed to be null.
  4989. - // - There are, however, problems with this: first of all, this will end up stopping
  4990. - // the thread, without running destructors. While that is problematic in itself,
  4991. - // it is considered UB to have what is effectively a forced foreign unwind
  4992. - // with `extern "C"` ABI. One could observe the stack that is now gone from
  4993. - // another thread. We should avoid pinning stack variables to prevent library UB,
  4994. - // too. For the moment, given that test failures are reported immediately before the
  4995. - // next test runs, that test failures should be fixed and that KUnit is explicitly
  4996. - // documented as not suitable for production environments, we feel it is reasonable.
  4997. - unsafe {
  4998. - $crate::bindings::__kunit_do_failed_assertion(
  4999. - kunit_test,
  5000. - core::ptr::addr_of!(LOCATION.0),
  5001. - $crate::bindings::kunit_assert_type_KUNIT_ASSERTION,
  5002. - core::ptr::addr_of!(ASSERTION.0.assert),
  5003. - Some($crate::bindings::kunit_unary_assert_format),
  5004. - core::ptr::null(),
  5005. - );
  5006. - }
  5007. -
  5008. - // SAFETY: FFI call; the `test` pointer is valid because this hidden macro should only
  5009. - // be called by the generated documentation tests which forward the test pointer given
  5010. - // by KUnit.
  5011. - unsafe {
  5012. - $crate::bindings::__kunit_abort(kunit_test);
  5013. - }
  5014. - }
  5015. - };
  5016. -}
  5017. -
  5018. -/// Asserts that two expressions are equal to each other (using [`PartialEq`]).
  5019. -///
  5020. -/// Public but hidden since it should only be used from generated tests.
  5021. -///
  5022. -/// Unlike the one in `core`, this one does not panic; instead, it is mapped to the KUnit
  5023. -/// facilities. See [`assert!`] for more details.
  5024. -#[doc(hidden)]
  5025. -#[macro_export]
  5026. -macro_rules! kunit_assert_eq {
  5027. - ($name:literal, $file:literal, $diff:expr, $left:expr, $right:expr $(,)?) => {{
  5028. - // For the moment, we just forward to the expression assert because, for binary asserts,
  5029. - // KUnit supports only a few types (e.g. integers).
  5030. - $crate::kunit_assert!($name, $file, $diff, $left == $right);
  5031. - }};
  5032. -}
  5033. diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
  5034. deleted file mode 100644
  5035. --- a/rust/kernel/lib.rs
  5036. +++ /dev/null
  5037. @@ -1,138 +0,0 @@
  5038. -// SPDX-License-Identifier: GPL-2.0
  5039. -
  5040. -//! The `kernel` crate.
  5041. -//!
  5042. -//! This crate contains the kernel APIs that have been ported or wrapped for
  5043. -//! usage by Rust code in the kernel and is shared by all of them.
  5044. -//!
  5045. -//! In other words, all the rest of the Rust code in the kernel (e.g. kernel
  5046. -//! modules written in Rust) depends on [`core`], [`alloc`] and this crate.
  5047. -//!
  5048. -//! If you need a kernel C API that is not ported or wrapped yet here, then
  5049. -//! do so first instead of bypassing this crate.
  5050. -
  5051. -#![no_std]
  5052. -#![feature(coerce_unsized)]
  5053. -#![feature(dispatch_from_dyn)]
  5054. -#![feature(new_uninit)]
  5055. -#![feature(receiver_trait)]
  5056. -#![feature(unsize)]
  5057. -
  5058. -// Ensure conditional compilation based on the kernel configuration works;
  5059. -// otherwise we may silently break things like initcall handling.
  5060. -#[cfg(not(CONFIG_RUST))]
  5061. -compile_error!("Missing kernel configuration for conditional compilation");
  5062. -
  5063. -// Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
  5064. -extern crate self as kernel;
  5065. -
  5066. -pub mod alloc;
  5067. -mod build_assert;
  5068. -pub mod error;
  5069. -pub mod init;
  5070. -pub mod ioctl;
  5071. -#[cfg(CONFIG_KUNIT)]
  5072. -pub mod kunit;
  5073. -#[cfg(CONFIG_NET)]
  5074. -pub mod net;
  5075. -pub mod prelude;
  5076. -pub mod print;
  5077. -mod static_assert;
  5078. -#[doc(hidden)]
  5079. -pub mod std_vendor;
  5080. -pub mod str;
  5081. -pub mod sync;
  5082. -pub mod task;
  5083. -pub mod time;
  5084. -pub mod types;
  5085. -pub mod workqueue;
  5086. -
  5087. -#[doc(hidden)]
  5088. -pub use bindings;
  5089. -pub use macros;
  5090. -pub use uapi;
  5091. -
  5092. -#[doc(hidden)]
  5093. -pub use build_error::build_error;
  5094. -
  5095. -/// Prefix to appear before log messages printed from within the `kernel` crate.
  5096. -const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
  5097. -
  5098. -/// The top level entrypoint to implementing a kernel module.
  5099. -///
  5100. -/// For any teardown or cleanup operations, your type may implement [`Drop`].
  5101. -pub trait Module: Sized + Sync + Send {
  5102. - /// Called at module initialization time.
  5103. - ///
  5104. - /// Use this method to perform whatever setup or registration your module
  5105. - /// should do.
  5106. - ///
  5107. - /// Equivalent to the `module_init` macro in the C API.
  5108. - fn init(module: &'static ThisModule) -> error::Result<Self>;
  5109. -}
  5110. -
  5111. -/// Equivalent to `THIS_MODULE` in the C API.
  5112. -///
  5113. -/// C header: [`include/linux/export.h`](srctree/include/linux/export.h)
  5114. -pub struct ThisModule(*mut bindings::module);
  5115. -
  5116. -// SAFETY: `THIS_MODULE` may be used from all threads within a module.
  5117. -unsafe impl Sync for ThisModule {}
  5118. -
  5119. -impl ThisModule {
  5120. - /// Creates a [`ThisModule`] given the `THIS_MODULE` pointer.
  5121. - ///
  5122. - /// # Safety
  5123. - ///
  5124. - /// The pointer must be equal to the right `THIS_MODULE`.
  5125. - pub const unsafe fn from_ptr(ptr: *mut bindings::module) -> ThisModule {
  5126. - ThisModule(ptr)
  5127. - }
  5128. -
  5129. - /// Access the raw pointer for this module.
  5130. - ///
  5131. - /// It is up to the user to use it correctly.
  5132. - pub const fn as_ptr(&self) -> *mut bindings::module {
  5133. - self.0
  5134. - }
  5135. -}
  5136. -
  5137. -#[cfg(not(any(testlib, test)))]
  5138. -#[panic_handler]
  5139. -fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
  5140. - pr_emerg!("{}\n", info);
  5141. - // SAFETY: FFI call.
  5142. - unsafe { bindings::BUG() };
  5143. -}
  5144. -
  5145. -/// Produces a pointer to an object from a pointer to one of its fields.
  5146. -///
  5147. -/// # Safety
  5148. -///
  5149. -/// The pointer passed to this macro, and the pointer returned by this macro, must both be in
  5150. -/// bounds of the same allocation.
  5151. -///
  5152. -/// # Examples
  5153. -///
  5154. -/// ```
  5155. -/// # use kernel::container_of;
  5156. -/// struct Test {
  5157. -/// a: u64,
  5158. -/// b: u32,
  5159. -/// }
  5160. -///
  5161. -/// let test = Test { a: 10, b: 20 };
  5162. -/// let b_ptr = &test.b;
  5163. -/// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be
  5164. -/// // in-bounds of the same allocation as `b_ptr`.
  5165. -/// let test_alias = unsafe { container_of!(b_ptr, Test, b) };
  5166. -/// assert!(core::ptr::eq(&test, test_alias));
  5167. -/// ```
  5168. -#[macro_export]
  5169. -macro_rules! container_of {
  5170. - ($ptr:expr, $type:ty, $($f:tt)*) => {{
  5171. - let ptr = $ptr as *const _ as *const u8;
  5172. - let offset: usize = ::core::mem::offset_of!($type, $($f)*);
  5173. - ptr.sub(offset) as *const $type
  5174. - }}
  5175. -}
  5176. diff --git a/rust/kernel/net.rs b/rust/kernel/net.rs
  5177. deleted file mode 100644
  5178. --- a/rust/kernel/net.rs
  5179. +++ /dev/null
  5180. @@ -1,6 +0,0 @@
  5181. -// SPDX-License-Identifier: GPL-2.0
  5182. -
  5183. -//! Networking.
  5184. -
  5185. -#[cfg(CONFIG_RUST_PHYLIB_ABSTRACTIONS)]
  5186. -pub mod phy;
  5187. diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
  5188. deleted file mode 100644
  5189. --- a/rust/kernel/net/phy.rs
  5190. +++ /dev/null
  5191. @@ -1,905 +0,0 @@
  5192. -// SPDX-License-Identifier: GPL-2.0
  5193. -
  5194. -// Copyright (C) 2023 FUJITA Tomonori <fujita.tomonori@gmail.com>
  5195. -
  5196. -//! Network PHY device.
  5197. -//!
  5198. -//! C headers: [`include/linux/phy.h`](srctree/include/linux/phy.h).
  5199. -
  5200. -use crate::{error::*, prelude::*, types::Opaque};
  5201. -
  5202. -use core::marker::PhantomData;
  5203. -
  5204. -/// PHY state machine states.
  5205. -///
  5206. -/// Corresponds to the kernel's [`enum phy_state`].
  5207. -///
  5208. -/// Some of PHY drivers access to the state of PHY's software state machine.
  5209. -///
  5210. -/// [`enum phy_state`]: srctree/include/linux/phy.h
  5211. -#[derive(PartialEq, Eq)]
  5212. -pub enum DeviceState {
  5213. - /// PHY device and driver are not ready for anything.
  5214. - Down,
  5215. - /// PHY is ready to send and receive packets.
  5216. - Ready,
  5217. - /// PHY is up, but no polling or interrupts are done.
  5218. - Halted,
  5219. - /// PHY is up, but is in an error state.
  5220. - Error,
  5221. - /// PHY and attached device are ready to do work.
  5222. - Up,
  5223. - /// PHY is currently running.
  5224. - Running,
  5225. - /// PHY is up, but not currently plugged in.
  5226. - NoLink,
  5227. - /// PHY is performing a cable test.
  5228. - CableTest,
  5229. -}
  5230. -
  5231. -/// A mode of Ethernet communication.
  5232. -///
  5233. -/// PHY drivers get duplex information from hardware and update the current state.
  5234. -pub enum DuplexMode {
  5235. - /// PHY is in full-duplex mode.
  5236. - Full,
  5237. - /// PHY is in half-duplex mode.
  5238. - Half,
  5239. - /// PHY is in unknown duplex mode.
  5240. - Unknown,
  5241. -}
  5242. -
  5243. -/// An instance of a PHY device.
  5244. -///
  5245. -/// Wraps the kernel's [`struct phy_device`].
  5246. -///
  5247. -/// A [`Device`] instance is created when a callback in [`Driver`] is executed. A PHY driver
  5248. -/// executes [`Driver`]'s methods during the callback.
  5249. -///
  5250. -/// # Invariants
  5251. -///
  5252. -/// Referencing a `phy_device` using this struct asserts that you are in
  5253. -/// a context where all methods defined on this struct are safe to call.
  5254. -///
  5255. -/// [`struct phy_device`]: srctree/include/linux/phy.h
  5256. -// During the calls to most functions in [`Driver`], the C side (`PHYLIB`) holds a lock that is
  5257. -// unique for every instance of [`Device`]. `PHYLIB` uses a different serialization technique for
  5258. -// [`Driver::resume`] and [`Driver::suspend`]: `PHYLIB` updates `phy_device`'s state with
  5259. -// the lock held, thus guaranteeing that [`Driver::resume`] has exclusive access to the instance.
  5260. -// [`Driver::resume`] and [`Driver::suspend`] also are called where only one thread can access
  5261. -// to the instance.
  5262. -#[repr(transparent)]
  5263. -pub struct Device(Opaque<bindings::phy_device>);
  5264. -
  5265. -impl Device {
  5266. - /// Creates a new [`Device`] instance from a raw pointer.
  5267. - ///
  5268. - /// # Safety
  5269. - ///
  5270. - /// For the duration of 'a, the pointer must point at a valid `phy_device`,
  5271. - /// and the caller must be in a context where all methods defined on this struct
  5272. - /// are safe to call.
  5273. - unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
  5274. - // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::phy_device`.
  5275. - let ptr = ptr.cast::<Self>();
  5276. - // SAFETY: by the function requirements the pointer is valid and we have unique access for
  5277. - // the duration of `'a`.
  5278. - unsafe { &mut *ptr }
  5279. - }
  5280. -
  5281. - /// Gets the id of the PHY.
  5282. - pub fn phy_id(&self) -> u32 {
  5283. - let phydev = self.0.get();
  5284. - // SAFETY: The struct invariant ensures that we may access
  5285. - // this field without additional synchronization.
  5286. - unsafe { (*phydev).phy_id }
  5287. - }
  5288. -
  5289. - /// Gets the state of PHY state machine states.
  5290. - pub fn state(&self) -> DeviceState {
  5291. - let phydev = self.0.get();
  5292. - // SAFETY: The struct invariant ensures that we may access
  5293. - // this field without additional synchronization.
  5294. - let state = unsafe { (*phydev).state };
  5295. - // TODO: this conversion code will be replaced with automatically generated code by bindgen
  5296. - // when it becomes possible.
  5297. - match state {
  5298. - bindings::phy_state_PHY_DOWN => DeviceState::Down,
  5299. - bindings::phy_state_PHY_READY => DeviceState::Ready,
  5300. - bindings::phy_state_PHY_HALTED => DeviceState::Halted,
  5301. - bindings::phy_state_PHY_ERROR => DeviceState::Error,
  5302. - bindings::phy_state_PHY_UP => DeviceState::Up,
  5303. - bindings::phy_state_PHY_RUNNING => DeviceState::Running,
  5304. - bindings::phy_state_PHY_NOLINK => DeviceState::NoLink,
  5305. - bindings::phy_state_PHY_CABLETEST => DeviceState::CableTest,
  5306. - _ => DeviceState::Error,
  5307. - }
  5308. - }
  5309. -
  5310. - /// Gets the current link state.
  5311. - ///
  5312. - /// It returns true if the link is up.
  5313. - pub fn is_link_up(&self) -> bool {
  5314. - const LINK_IS_UP: u64 = 1;
  5315. - // TODO: the code to access to the bit field will be replaced with automatically
  5316. - // generated code by bindgen when it becomes possible.
  5317. - // SAFETY: The struct invariant ensures that we may access
  5318. - // this field without additional synchronization.
  5319. - let bit_field = unsafe { &(*self.0.get())._bitfield_1 };
  5320. - bit_field.get(14, 1) == LINK_IS_UP
  5321. - }
  5322. -
  5323. - /// Gets the current auto-negotiation configuration.
  5324. - ///
  5325. - /// It returns true if auto-negotiation is enabled.
  5326. - pub fn is_autoneg_enabled(&self) -> bool {
  5327. - // TODO: the code to access to the bit field will be replaced with automatically
  5328. - // generated code by bindgen when it becomes possible.
  5329. - // SAFETY: The struct invariant ensures that we may access
  5330. - // this field without additional synchronization.
  5331. - let bit_field = unsafe { &(*self.0.get())._bitfield_1 };
  5332. - bit_field.get(13, 1) == bindings::AUTONEG_ENABLE as u64
  5333. - }
  5334. -
  5335. - /// Gets the current auto-negotiation state.
  5336. - ///
  5337. - /// It returns true if auto-negotiation is completed.
  5338. - pub fn is_autoneg_completed(&self) -> bool {
  5339. - const AUTONEG_COMPLETED: u64 = 1;
  5340. - // TODO: the code to access to the bit field will be replaced with automatically
  5341. - // generated code by bindgen when it becomes possible.
  5342. - // SAFETY: The struct invariant ensures that we may access
  5343. - // this field without additional synchronization.
  5344. - let bit_field = unsafe { &(*self.0.get())._bitfield_1 };
  5345. - bit_field.get(15, 1) == AUTONEG_COMPLETED
  5346. - }
  5347. -
  5348. - /// Sets the speed of the PHY.
  5349. - pub fn set_speed(&mut self, speed: u32) {
  5350. - let phydev = self.0.get();
  5351. - // SAFETY: The struct invariant ensures that we may access
  5352. - // this field without additional synchronization.
  5353. - unsafe { (*phydev).speed = speed as i32 };
  5354. - }
  5355. -
  5356. - /// Sets duplex mode.
  5357. - pub fn set_duplex(&mut self, mode: DuplexMode) {
  5358. - let phydev = self.0.get();
  5359. - let v = match mode {
  5360. - DuplexMode::Full => bindings::DUPLEX_FULL as i32,
  5361. - DuplexMode::Half => bindings::DUPLEX_HALF as i32,
  5362. - DuplexMode::Unknown => bindings::DUPLEX_UNKNOWN as i32,
  5363. - };
  5364. - // SAFETY: The struct invariant ensures that we may access
  5365. - // this field without additional synchronization.
  5366. - unsafe { (*phydev).duplex = v };
  5367. - }
  5368. -
  5369. - /// Reads a given C22 PHY register.
  5370. - // This function reads a hardware register and updates the stats so takes `&mut self`.
  5371. - pub fn read(&mut self, regnum: u16) -> Result<u16> {
  5372. - let phydev = self.0.get();
  5373. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5374. - // So it's just an FFI call, open code of `phy_read()` with a valid `phy_device` pointer
  5375. - // `phydev`.
  5376. - let ret = unsafe {
  5377. - bindings::mdiobus_read((*phydev).mdio.bus, (*phydev).mdio.addr, regnum.into())
  5378. - };
  5379. - if ret < 0 {
  5380. - Err(Error::from_errno(ret))
  5381. - } else {
  5382. - Ok(ret as u16)
  5383. - }
  5384. - }
  5385. -
  5386. - /// Writes a given C22 PHY register.
  5387. - pub fn write(&mut self, regnum: u16, val: u16) -> Result {
  5388. - let phydev = self.0.get();
  5389. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5390. - // So it's just an FFI call, open code of `phy_write()` with a valid `phy_device` pointer
  5391. - // `phydev`.
  5392. - to_result(unsafe {
  5393. - bindings::mdiobus_write((*phydev).mdio.bus, (*phydev).mdio.addr, regnum.into(), val)
  5394. - })
  5395. - }
  5396. -
  5397. - /// Reads a paged register.
  5398. - pub fn read_paged(&mut self, page: u16, regnum: u16) -> Result<u16> {
  5399. - let phydev = self.0.get();
  5400. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5401. - // So it's just an FFI call.
  5402. - let ret = unsafe { bindings::phy_read_paged(phydev, page.into(), regnum.into()) };
  5403. - if ret < 0 {
  5404. - Err(Error::from_errno(ret))
  5405. - } else {
  5406. - Ok(ret as u16)
  5407. - }
  5408. - }
  5409. -
  5410. - /// Resolves the advertisements into PHY settings.
  5411. - pub fn resolve_aneg_linkmode(&mut self) {
  5412. - let phydev = self.0.get();
  5413. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5414. - // So it's just an FFI call.
  5415. - unsafe { bindings::phy_resolve_aneg_linkmode(phydev) };
  5416. - }
  5417. -
  5418. - /// Executes software reset the PHY via `BMCR_RESET` bit.
  5419. - pub fn genphy_soft_reset(&mut self) -> Result {
  5420. - let phydev = self.0.get();
  5421. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5422. - // So it's just an FFI call.
  5423. - to_result(unsafe { bindings::genphy_soft_reset(phydev) })
  5424. - }
  5425. -
  5426. - /// Initializes the PHY.
  5427. - pub fn init_hw(&mut self) -> Result {
  5428. - let phydev = self.0.get();
  5429. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5430. - // So it's just an FFI call.
  5431. - to_result(unsafe { bindings::phy_init_hw(phydev) })
  5432. - }
  5433. -
  5434. - /// Starts auto-negotiation.
  5435. - pub fn start_aneg(&mut self) -> Result {
  5436. - let phydev = self.0.get();
  5437. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5438. - // So it's just an FFI call.
  5439. - to_result(unsafe { bindings::_phy_start_aneg(phydev) })
  5440. - }
  5441. -
  5442. - /// Resumes the PHY via `BMCR_PDOWN` bit.
  5443. - pub fn genphy_resume(&mut self) -> Result {
  5444. - let phydev = self.0.get();
  5445. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5446. - // So it's just an FFI call.
  5447. - to_result(unsafe { bindings::genphy_resume(phydev) })
  5448. - }
  5449. -
  5450. - /// Suspends the PHY via `BMCR_PDOWN` bit.
  5451. - pub fn genphy_suspend(&mut self) -> Result {
  5452. - let phydev = self.0.get();
  5453. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5454. - // So it's just an FFI call.
  5455. - to_result(unsafe { bindings::genphy_suspend(phydev) })
  5456. - }
  5457. -
  5458. - /// Checks the link status and updates current link state.
  5459. - pub fn genphy_read_status(&mut self) -> Result<u16> {
  5460. - let phydev = self.0.get();
  5461. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5462. - // So it's just an FFI call.
  5463. - let ret = unsafe { bindings::genphy_read_status(phydev) };
  5464. - if ret < 0 {
  5465. - Err(Error::from_errno(ret))
  5466. - } else {
  5467. - Ok(ret as u16)
  5468. - }
  5469. - }
  5470. -
  5471. - /// Updates the link status.
  5472. - pub fn genphy_update_link(&mut self) -> Result {
  5473. - let phydev = self.0.get();
  5474. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5475. - // So it's just an FFI call.
  5476. - to_result(unsafe { bindings::genphy_update_link(phydev) })
  5477. - }
  5478. -
  5479. - /// Reads link partner ability.
  5480. - pub fn genphy_read_lpa(&mut self) -> Result {
  5481. - let phydev = self.0.get();
  5482. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5483. - // So it's just an FFI call.
  5484. - to_result(unsafe { bindings::genphy_read_lpa(phydev) })
  5485. - }
  5486. -
  5487. - /// Reads PHY abilities.
  5488. - pub fn genphy_read_abilities(&mut self) -> Result {
  5489. - let phydev = self.0.get();
  5490. - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  5491. - // So it's just an FFI call.
  5492. - to_result(unsafe { bindings::genphy_read_abilities(phydev) })
  5493. - }
  5494. -}
  5495. -
  5496. -/// Defines certain other features this PHY supports (like interrupts).
  5497. -///
  5498. -/// These flag values are used in [`Driver::FLAGS`].
  5499. -pub mod flags {
  5500. - /// PHY is internal.
  5501. - pub const IS_INTERNAL: u32 = bindings::PHY_IS_INTERNAL;
  5502. - /// PHY needs to be reset after the refclk is enabled.
  5503. - pub const RST_AFTER_CLK_EN: u32 = bindings::PHY_RST_AFTER_CLK_EN;
  5504. - /// Polling is used to detect PHY status changes.
  5505. - pub const POLL_CABLE_TEST: u32 = bindings::PHY_POLL_CABLE_TEST;
  5506. - /// Don't suspend.
  5507. - pub const ALWAYS_CALL_SUSPEND: u32 = bindings::PHY_ALWAYS_CALL_SUSPEND;
  5508. -}
  5509. -
  5510. -/// An adapter for the registration of a PHY driver.
  5511. -struct Adapter<T: Driver> {
  5512. - _p: PhantomData<T>,
  5513. -}
  5514. -
  5515. -impl<T: Driver> Adapter<T> {
  5516. - /// # Safety
  5517. - ///
  5518. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5519. - unsafe extern "C" fn soft_reset_callback(
  5520. - phydev: *mut bindings::phy_device,
  5521. - ) -> core::ffi::c_int {
  5522. - from_result(|| {
  5523. - // SAFETY: This callback is called only in contexts
  5524. - // where we hold `phy_device->lock`, so the accessors on
  5525. - // `Device` are okay to call.
  5526. - let dev = unsafe { Device::from_raw(phydev) };
  5527. - T::soft_reset(dev)?;
  5528. - Ok(0)
  5529. - })
  5530. - }
  5531. -
  5532. - /// # Safety
  5533. - ///
  5534. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5535. - unsafe extern "C" fn get_features_callback(
  5536. - phydev: *mut bindings::phy_device,
  5537. - ) -> core::ffi::c_int {
  5538. - from_result(|| {
  5539. - // SAFETY: This callback is called only in contexts
  5540. - // where we hold `phy_device->lock`, so the accessors on
  5541. - // `Device` are okay to call.
  5542. - let dev = unsafe { Device::from_raw(phydev) };
  5543. - T::get_features(dev)?;
  5544. - Ok(0)
  5545. - })
  5546. - }
  5547. -
  5548. - /// # Safety
  5549. - ///
  5550. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5551. - unsafe extern "C" fn suspend_callback(phydev: *mut bindings::phy_device) -> core::ffi::c_int {
  5552. - from_result(|| {
  5553. - // SAFETY: The C core code ensures that the accessors on
  5554. - // `Device` are okay to call even though `phy_device->lock`
  5555. - // might not be held.
  5556. - let dev = unsafe { Device::from_raw(phydev) };
  5557. - T::suspend(dev)?;
  5558. - Ok(0)
  5559. - })
  5560. - }
  5561. -
  5562. - /// # Safety
  5563. - ///
  5564. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5565. - unsafe extern "C" fn resume_callback(phydev: *mut bindings::phy_device) -> core::ffi::c_int {
  5566. - from_result(|| {
  5567. - // SAFETY: The C core code ensures that the accessors on
  5568. - // `Device` are okay to call even though `phy_device->lock`
  5569. - // might not be held.
  5570. - let dev = unsafe { Device::from_raw(phydev) };
  5571. - T::resume(dev)?;
  5572. - Ok(0)
  5573. - })
  5574. - }
  5575. -
  5576. - /// # Safety
  5577. - ///
  5578. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5579. - unsafe extern "C" fn config_aneg_callback(
  5580. - phydev: *mut bindings::phy_device,
  5581. - ) -> core::ffi::c_int {
  5582. - from_result(|| {
  5583. - // SAFETY: This callback is called only in contexts
  5584. - // where we hold `phy_device->lock`, so the accessors on
  5585. - // `Device` are okay to call.
  5586. - let dev = unsafe { Device::from_raw(phydev) };
  5587. - T::config_aneg(dev)?;
  5588. - Ok(0)
  5589. - })
  5590. - }
  5591. -
  5592. - /// # Safety
  5593. - ///
  5594. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5595. - unsafe extern "C" fn read_status_callback(
  5596. - phydev: *mut bindings::phy_device,
  5597. - ) -> core::ffi::c_int {
  5598. - from_result(|| {
  5599. - // SAFETY: This callback is called only in contexts
  5600. - // where we hold `phy_device->lock`, so the accessors on
  5601. - // `Device` are okay to call.
  5602. - let dev = unsafe { Device::from_raw(phydev) };
  5603. - T::read_status(dev)?;
  5604. - Ok(0)
  5605. - })
  5606. - }
  5607. -
  5608. - /// # Safety
  5609. - ///
  5610. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5611. - unsafe extern "C" fn match_phy_device_callback(
  5612. - phydev: *mut bindings::phy_device,
  5613. - ) -> core::ffi::c_int {
  5614. - // SAFETY: This callback is called only in contexts
  5615. - // where we hold `phy_device->lock`, so the accessors on
  5616. - // `Device` are okay to call.
  5617. - let dev = unsafe { Device::from_raw(phydev) };
  5618. - T::match_phy_device(dev) as i32
  5619. - }
  5620. -
  5621. - /// # Safety
  5622. - ///
  5623. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5624. - unsafe extern "C" fn read_mmd_callback(
  5625. - phydev: *mut bindings::phy_device,
  5626. - devnum: i32,
  5627. - regnum: u16,
  5628. - ) -> i32 {
  5629. - from_result(|| {
  5630. - // SAFETY: This callback is called only in contexts
  5631. - // where we hold `phy_device->lock`, so the accessors on
  5632. - // `Device` are okay to call.
  5633. - let dev = unsafe { Device::from_raw(phydev) };
  5634. - // CAST: the C side verifies devnum < 32.
  5635. - let ret = T::read_mmd(dev, devnum as u8, regnum)?;
  5636. - Ok(ret.into())
  5637. - })
  5638. - }
  5639. -
  5640. - /// # Safety
  5641. - ///
  5642. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5643. - unsafe extern "C" fn write_mmd_callback(
  5644. - phydev: *mut bindings::phy_device,
  5645. - devnum: i32,
  5646. - regnum: u16,
  5647. - val: u16,
  5648. - ) -> i32 {
  5649. - from_result(|| {
  5650. - // SAFETY: This callback is called only in contexts
  5651. - // where we hold `phy_device->lock`, so the accessors on
  5652. - // `Device` are okay to call.
  5653. - let dev = unsafe { Device::from_raw(phydev) };
  5654. - T::write_mmd(dev, devnum as u8, regnum, val)?;
  5655. - Ok(0)
  5656. - })
  5657. - }
  5658. -
  5659. - /// # Safety
  5660. - ///
  5661. - /// `phydev` must be passed by the corresponding callback in `phy_driver`.
  5662. - unsafe extern "C" fn link_change_notify_callback(phydev: *mut bindings::phy_device) {
  5663. - // SAFETY: This callback is called only in contexts
  5664. - // where we hold `phy_device->lock`, so the accessors on
  5665. - // `Device` are okay to call.
  5666. - let dev = unsafe { Device::from_raw(phydev) };
  5667. - T::link_change_notify(dev);
  5668. - }
  5669. -}
  5670. -
  5671. -/// Driver structure for a particular PHY type.
  5672. -///
  5673. -/// Wraps the kernel's [`struct phy_driver`].
  5674. -/// This is used to register a driver for a particular PHY type with the kernel.
  5675. -///
  5676. -/// # Invariants
  5677. -///
  5678. -/// `self.0` is always in a valid state.
  5679. -///
  5680. -/// [`struct phy_driver`]: srctree/include/linux/phy.h
  5681. -#[repr(transparent)]
  5682. -pub struct DriverVTable(Opaque<bindings::phy_driver>);
  5683. -
  5684. -// SAFETY: `DriverVTable` doesn't expose any &self method to access internal data, so it's safe to
  5685. -// share `&DriverVTable` across execution context boundries.
  5686. -unsafe impl Sync for DriverVTable {}
  5687. -
  5688. -/// Creates a [`DriverVTable`] instance from [`Driver`].
  5689. -///
  5690. -/// This is used by [`module_phy_driver`] macro to create a static array of `phy_driver`.
  5691. -///
  5692. -/// [`module_phy_driver`]: crate::module_phy_driver
  5693. -pub const fn create_phy_driver<T: Driver>() -> DriverVTable {
  5694. - // INVARIANT: All the fields of `struct phy_driver` are initialized properly.
  5695. - DriverVTable(Opaque::new(bindings::phy_driver {
  5696. - name: T::NAME.as_char_ptr().cast_mut(),
  5697. - flags: T::FLAGS,
  5698. - phy_id: T::PHY_DEVICE_ID.id,
  5699. - phy_id_mask: T::PHY_DEVICE_ID.mask_as_int(),
  5700. - soft_reset: if T::HAS_SOFT_RESET {
  5701. - Some(Adapter::<T>::soft_reset_callback)
  5702. - } else {
  5703. - None
  5704. - },
  5705. - get_features: if T::HAS_GET_FEATURES {
  5706. - Some(Adapter::<T>::get_features_callback)
  5707. - } else {
  5708. - None
  5709. - },
  5710. - match_phy_device: if T::HAS_MATCH_PHY_DEVICE {
  5711. - Some(Adapter::<T>::match_phy_device_callback)
  5712. - } else {
  5713. - None
  5714. - },
  5715. - suspend: if T::HAS_SUSPEND {
  5716. - Some(Adapter::<T>::suspend_callback)
  5717. - } else {
  5718. - None
  5719. - },
  5720. - resume: if T::HAS_RESUME {
  5721. - Some(Adapter::<T>::resume_callback)
  5722. - } else {
  5723. - None
  5724. - },
  5725. - config_aneg: if T::HAS_CONFIG_ANEG {
  5726. - Some(Adapter::<T>::config_aneg_callback)
  5727. - } else {
  5728. - None
  5729. - },
  5730. - read_status: if T::HAS_READ_STATUS {
  5731. - Some(Adapter::<T>::read_status_callback)
  5732. - } else {
  5733. - None
  5734. - },
  5735. - read_mmd: if T::HAS_READ_MMD {
  5736. - Some(Adapter::<T>::read_mmd_callback)
  5737. - } else {
  5738. - None
  5739. - },
  5740. - write_mmd: if T::HAS_WRITE_MMD {
  5741. - Some(Adapter::<T>::write_mmd_callback)
  5742. - } else {
  5743. - None
  5744. - },
  5745. - link_change_notify: if T::HAS_LINK_CHANGE_NOTIFY {
  5746. - Some(Adapter::<T>::link_change_notify_callback)
  5747. - } else {
  5748. - None
  5749. - },
  5750. - // SAFETY: The rest is zeroed out to initialize `struct phy_driver`,
  5751. - // sets `Option<&F>` to be `None`.
  5752. - ..unsafe { core::mem::MaybeUninit::<bindings::phy_driver>::zeroed().assume_init() }
  5753. - }))
  5754. -}
  5755. -
  5756. -/// Driver implementation for a particular PHY type.
  5757. -///
  5758. -/// This trait is used to create a [`DriverVTable`].
  5759. -#[vtable]
  5760. -pub trait Driver {
  5761. - /// Defines certain other features this PHY supports.
  5762. - /// It is a combination of the flags in the [`flags`] module.
  5763. - const FLAGS: u32 = 0;
  5764. -
  5765. - /// The friendly name of this PHY type.
  5766. - const NAME: &'static CStr;
  5767. -
  5768. - /// This driver only works for PHYs with IDs which match this field.
  5769. - /// The default id and mask are zero.
  5770. - const PHY_DEVICE_ID: DeviceId = DeviceId::new_with_custom_mask(0, 0);
  5771. -
  5772. - /// Issues a PHY software reset.
  5773. - fn soft_reset(_dev: &mut Device) -> Result {
  5774. - kernel::build_error(VTABLE_DEFAULT_ERROR)
  5775. - }
  5776. -
  5777. - /// Probes the hardware to determine what abilities it has.
  5778. - fn get_features(_dev: &mut Device) -> Result {
  5779. - kernel::build_error(VTABLE_DEFAULT_ERROR)
  5780. - }
  5781. -
  5782. - /// Returns true if this is a suitable driver for the given phydev.
  5783. - /// If not implemented, matching is based on [`Driver::PHY_DEVICE_ID`].
  5784. - fn match_phy_device(_dev: &Device) -> bool {
  5785. - false
  5786. - }
  5787. -
  5788. - /// Configures the advertisement and resets auto-negotiation
  5789. - /// if auto-negotiation is enabled.
  5790. - fn config_aneg(_dev: &mut Device) -> Result {
  5791. - kernel::build_error(VTABLE_DEFAULT_ERROR)
  5792. - }
  5793. -
  5794. - /// Determines the negotiated speed and duplex.
  5795. - fn read_status(_dev: &mut Device) -> Result<u16> {
  5796. - kernel::build_error(VTABLE_DEFAULT_ERROR)
  5797. - }
  5798. -
  5799. - /// Suspends the hardware, saving state if needed.
  5800. - fn suspend(_dev: &mut Device) -> Result {
  5801. - kernel::build_error(VTABLE_DEFAULT_ERROR)
  5802. - }
  5803. -
  5804. - /// Resumes the hardware, restoring state if needed.
  5805. - fn resume(_dev: &mut Device) -> Result {
  5806. - kernel::build_error(VTABLE_DEFAULT_ERROR)
  5807. - }
  5808. -
  5809. - /// Overrides the default MMD read function for reading a MMD register.
  5810. - fn read_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16) -> Result<u16> {
  5811. - kernel::build_error(VTABLE_DEFAULT_ERROR)
  5812. - }
  5813. -
  5814. - /// Overrides the default MMD write function for writing a MMD register.
  5815. - fn write_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16, _val: u16) -> Result {
  5816. - kernel::build_error(VTABLE_DEFAULT_ERROR)
  5817. - }
  5818. -
  5819. - /// Callback for notification of link change.
  5820. - fn link_change_notify(_dev: &mut Device) {}
  5821. -}
  5822. -
  5823. -/// Registration structure for PHY drivers.
  5824. -///
  5825. -/// Registers [`DriverVTable`] instances with the kernel. They will be unregistered when dropped.
  5826. -///
  5827. -/// # Invariants
  5828. -///
  5829. -/// The `drivers` slice are currently registered to the kernel via `phy_drivers_register`.
  5830. -pub struct Registration {
  5831. - drivers: Pin<&'static mut [DriverVTable]>,
  5832. -}
  5833. -
  5834. -// SAFETY: The only action allowed in a `Registration` instance is dropping it, which is safe to do
  5835. -// from any thread because `phy_drivers_unregister` can be called from any thread context.
  5836. -unsafe impl Send for Registration {}
  5837. -
  5838. -impl Registration {
  5839. - /// Registers a PHY driver.
  5840. - pub fn register(
  5841. - module: &'static crate::ThisModule,
  5842. - drivers: Pin<&'static mut [DriverVTable]>,
  5843. - ) -> Result<Self> {
  5844. - if drivers.is_empty() {
  5845. - return Err(code::EINVAL);
  5846. - }
  5847. - // SAFETY: The type invariants of [`DriverVTable`] ensure that all elements of
  5848. - // the `drivers` slice are initialized properly. `drivers` will not be moved.
  5849. - // So it's just an FFI call.
  5850. - to_result(unsafe {
  5851. - bindings::phy_drivers_register(drivers[0].0.get(), drivers.len().try_into()?, module.0)
  5852. - })?;
  5853. - // INVARIANT: The `drivers` slice is successfully registered to the kernel via `phy_drivers_register`.
  5854. - Ok(Registration { drivers })
  5855. - }
  5856. -}
  5857. -
  5858. -impl Drop for Registration {
  5859. - fn drop(&mut self) {
  5860. - // SAFETY: The type invariants guarantee that `self.drivers` is valid.
  5861. - // So it's just an FFI call.
  5862. - unsafe {
  5863. - bindings::phy_drivers_unregister(self.drivers[0].0.get(), self.drivers.len() as i32)
  5864. - };
  5865. - }
  5866. -}
  5867. -
  5868. -/// An identifier for PHY devices on an MDIO/MII bus.
  5869. -///
  5870. -/// Represents the kernel's `struct mdio_device_id`. This is used to find an appropriate
  5871. -/// PHY driver.
  5872. -pub struct DeviceId {
  5873. - id: u32,
  5874. - mask: DeviceMask,
  5875. -}
  5876. -
  5877. -impl DeviceId {
  5878. - /// Creates a new instance with the exact match mask.
  5879. - pub const fn new_with_exact_mask(id: u32) -> Self {
  5880. - DeviceId {
  5881. - id,
  5882. - mask: DeviceMask::Exact,
  5883. - }
  5884. - }
  5885. -
  5886. - /// Creates a new instance with the model match mask.
  5887. - pub const fn new_with_model_mask(id: u32) -> Self {
  5888. - DeviceId {
  5889. - id,
  5890. - mask: DeviceMask::Model,
  5891. - }
  5892. - }
  5893. -
  5894. - /// Creates a new instance with the vendor match mask.
  5895. - pub const fn new_with_vendor_mask(id: u32) -> Self {
  5896. - DeviceId {
  5897. - id,
  5898. - mask: DeviceMask::Vendor,
  5899. - }
  5900. - }
  5901. -
  5902. - /// Creates a new instance with a custom match mask.
  5903. - pub const fn new_with_custom_mask(id: u32, mask: u32) -> Self {
  5904. - DeviceId {
  5905. - id,
  5906. - mask: DeviceMask::Custom(mask),
  5907. - }
  5908. - }
  5909. -
  5910. - /// Creates a new instance from [`Driver`].
  5911. - pub const fn new_with_driver<T: Driver>() -> Self {
  5912. - T::PHY_DEVICE_ID
  5913. - }
  5914. -
  5915. - /// Get a `mask` as u32.
  5916. - pub const fn mask_as_int(&self) -> u32 {
  5917. - self.mask.as_int()
  5918. - }
  5919. -
  5920. - // macro use only
  5921. - #[doc(hidden)]
  5922. - pub const fn mdio_device_id(&self) -> bindings::mdio_device_id {
  5923. - bindings::mdio_device_id {
  5924. - phy_id: self.id,
  5925. - phy_id_mask: self.mask.as_int(),
  5926. - }
  5927. - }
  5928. -}
  5929. -
  5930. -enum DeviceMask {
  5931. - Exact,
  5932. - Model,
  5933. - Vendor,
  5934. - Custom(u32),
  5935. -}
  5936. -
  5937. -impl DeviceMask {
  5938. - const MASK_EXACT: u32 = !0;
  5939. - const MASK_MODEL: u32 = !0 << 4;
  5940. - const MASK_VENDOR: u32 = !0 << 10;
  5941. -
  5942. - const fn as_int(&self) -> u32 {
  5943. - match self {
  5944. - DeviceMask::Exact => Self::MASK_EXACT,
  5945. - DeviceMask::Model => Self::MASK_MODEL,
  5946. - DeviceMask::Vendor => Self::MASK_VENDOR,
  5947. - DeviceMask::Custom(mask) => *mask,
  5948. - }
  5949. - }
  5950. -}
  5951. -
  5952. -/// Declares a kernel module for PHYs drivers.
  5953. -///
  5954. -/// This creates a static array of kernel's `struct phy_driver` and registers it.
  5955. -/// This also corresponds to the kernel's `MODULE_DEVICE_TABLE` macro, which embeds the information
  5956. -/// for module loading into the module binary file. Every driver needs an entry in `device_table`.
  5957. -///
  5958. -/// # Examples
  5959. -///
  5960. -/// ```
  5961. -/// # mod module_phy_driver_sample {
  5962. -/// use kernel::c_str;
  5963. -/// use kernel::net::phy::{self, DeviceId};
  5964. -/// use kernel::prelude::*;
  5965. -///
  5966. -/// kernel::module_phy_driver! {
  5967. -/// drivers: [PhySample],
  5968. -/// device_table: [
  5969. -/// DeviceId::new_with_driver::<PhySample>()
  5970. -/// ],
  5971. -/// name: "rust_sample_phy",
  5972. -/// author: "Rust for Linux Contributors",
  5973. -/// description: "Rust sample PHYs driver",
  5974. -/// license: "GPL",
  5975. -/// }
  5976. -///
  5977. -/// struct PhySample;
  5978. -///
  5979. -/// #[vtable]
  5980. -/// impl phy::Driver for PhySample {
  5981. -/// const NAME: &'static CStr = c_str!("PhySample");
  5982. -/// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001);
  5983. -/// }
  5984. -/// # }
  5985. -/// ```
  5986. -///
  5987. -/// This expands to the following code:
  5988. -///
  5989. -/// ```ignore
  5990. -/// use kernel::c_str;
  5991. -/// use kernel::net::phy::{self, DeviceId};
  5992. -/// use kernel::prelude::*;
  5993. -///
  5994. -/// struct Module {
  5995. -/// _reg: ::kernel::net::phy::Registration,
  5996. -/// }
  5997. -///
  5998. -/// module! {
  5999. -/// type: Module,
  6000. -/// name: "rust_sample_phy",
  6001. -/// author: "Rust for Linux Contributors",
  6002. -/// description: "Rust sample PHYs driver",
  6003. -/// license: "GPL",
  6004. -/// }
  6005. -///
  6006. -/// struct PhySample;
  6007. -///
  6008. -/// #[vtable]
  6009. -/// impl phy::Driver for PhySample {
  6010. -/// const NAME: &'static CStr = c_str!("PhySample");
  6011. -/// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001);
  6012. -/// }
  6013. -///
  6014. -/// const _: () = {
  6015. -/// static mut DRIVERS: [::kernel::net::phy::DriverVTable; 1] =
  6016. -/// [::kernel::net::phy::create_phy_driver::<PhySample>()];
  6017. -///
  6018. -/// impl ::kernel::Module for Module {
  6019. -/// fn init(module: &'static ThisModule) -> Result<Self> {
  6020. -/// let drivers = unsafe { &mut DRIVERS };
  6021. -/// let mut reg = ::kernel::net::phy::Registration::register(
  6022. -/// module,
  6023. -/// ::core::pin::Pin::static_mut(drivers),
  6024. -/// )?;
  6025. -/// Ok(Module { _reg: reg })
  6026. -/// }
  6027. -/// }
  6028. -/// };
  6029. -///
  6030. -/// #[cfg(MODULE)]
  6031. -/// #[no_mangle]
  6032. -/// static __mod_mdio__phydev_device_table: [::kernel::bindings::mdio_device_id; 2] = [
  6033. -/// ::kernel::bindings::mdio_device_id {
  6034. -/// phy_id: 0x00000001,
  6035. -/// phy_id_mask: 0xffffffff,
  6036. -/// },
  6037. -/// ::kernel::bindings::mdio_device_id {
  6038. -/// phy_id: 0,
  6039. -/// phy_id_mask: 0,
  6040. -/// },
  6041. -/// ];
  6042. -/// ```
  6043. -#[macro_export]
  6044. -macro_rules! module_phy_driver {
  6045. - (@replace_expr $_t:tt $sub:expr) => {$sub};
  6046. -
  6047. - (@count_devices $($x:expr),*) => {
  6048. - 0usize $(+ $crate::module_phy_driver!(@replace_expr $x 1usize))*
  6049. - };
  6050. -
  6051. - (@device_table [$($dev:expr),+]) => {
  6052. - // SAFETY: C will not read off the end of this constant since the last element is zero.
  6053. - #[cfg(MODULE)]
  6054. - #[no_mangle]
  6055. - static __mod_mdio__phydev_device_table: [$crate::bindings::mdio_device_id;
  6056. - $crate::module_phy_driver!(@count_devices $($dev),+) + 1] = [
  6057. - $($dev.mdio_device_id()),+,
  6058. - $crate::bindings::mdio_device_id {
  6059. - phy_id: 0,
  6060. - phy_id_mask: 0
  6061. - }
  6062. - ];
  6063. - };
  6064. -
  6065. - (drivers: [$($driver:ident),+ $(,)?], device_table: [$($dev:expr),+ $(,)?], $($f:tt)*) => {
  6066. - struct Module {
  6067. - _reg: $crate::net::phy::Registration,
  6068. - }
  6069. -
  6070. - $crate::prelude::module! {
  6071. - type: Module,
  6072. - $($f)*
  6073. - }
  6074. -
  6075. - const _: () = {
  6076. - static mut DRIVERS: [$crate::net::phy::DriverVTable;
  6077. - $crate::module_phy_driver!(@count_devices $($driver),+)] =
  6078. - [$($crate::net::phy::create_phy_driver::<$driver>()),+];
  6079. -
  6080. - impl $crate::Module for Module {
  6081. - fn init(module: &'static ThisModule) -> Result<Self> {
  6082. - // SAFETY: The anonymous constant guarantees that nobody else can access
  6083. - // the `DRIVERS` static. The array is used only in the C side.
  6084. - let drivers = unsafe { &mut DRIVERS };
  6085. - let mut reg = $crate::net::phy::Registration::register(
  6086. - module,
  6087. - ::core::pin::Pin::static_mut(drivers),
  6088. - )?;
  6089. - Ok(Module { _reg: reg })
  6090. - }
  6091. - }
  6092. - };
  6093. -
  6094. - $crate::module_phy_driver!(@device_table [$($dev),+]);
  6095. - }
  6096. -}
  6097. diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs
  6098. deleted file mode 100644
  6099. --- a/rust/kernel/prelude.rs
  6100. +++ /dev/null
  6101. @@ -1,42 +0,0 @@
  6102. -// SPDX-License-Identifier: GPL-2.0
  6103. -
  6104. -//! The `kernel` prelude.
  6105. -//!
  6106. -//! These are the most common items used by Rust code in the kernel,
  6107. -//! intended to be imported by all Rust code, for convenience.
  6108. -//!
  6109. -//! # Examples
  6110. -//!
  6111. -//! ```
  6112. -//! use kernel::prelude::*;
  6113. -//! ```
  6114. -
  6115. -#[doc(no_inline)]
  6116. -pub use core::pin::Pin;
  6117. -
  6118. -pub use crate::alloc::{box_ext::BoxExt, flags::*, vec_ext::VecExt};
  6119. -
  6120. -#[doc(no_inline)]
  6121. -pub use alloc::{boxed::Box, vec::Vec};
  6122. -
  6123. -#[doc(no_inline)]
  6124. -pub use macros::{module, pin_data, pinned_drop, vtable, Zeroable};
  6125. -
  6126. -pub use super::build_assert;
  6127. -
  6128. -// `super::std_vendor` is hidden, which makes the macro inline for some reason.
  6129. -#[doc(no_inline)]
  6130. -pub use super::dbg;
  6131. -pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
  6132. -
  6133. -pub use super::{init, pin_init, try_init, try_pin_init};
  6134. -
  6135. -pub use super::static_assert;
  6136. -
  6137. -pub use super::error::{code::*, Error, Result};
  6138. -
  6139. -pub use super::{str::CStr, ThisModule};
  6140. -
  6141. -pub use super::init::{InPlaceInit, Init, PinInit};
  6142. -
  6143. -pub use super::current;
  6144. diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
  6145. deleted file mode 100644
  6146. --- a/rust/kernel/print.rs
  6147. +++ /dev/null
  6148. @@ -1,413 +0,0 @@
  6149. -// SPDX-License-Identifier: GPL-2.0
  6150. -
  6151. -//! Printing facilities.
  6152. -//!
  6153. -//! C header: [`include/linux/printk.h`](srctree/include/linux/printk.h)
  6154. -//!
  6155. -//! Reference: <https://www.kernel.org/doc/html/latest/core-api/printk-basics.html>
  6156. -
  6157. -use core::{
  6158. - ffi::{c_char, c_void},
  6159. - fmt,
  6160. -};
  6161. -
  6162. -use crate::str::RawFormatter;
  6163. -
  6164. -// Called from `vsprintf` with format specifier `%pA`.
  6165. -#[no_mangle]
  6166. -unsafe extern "C" fn rust_fmt_argument(
  6167. - buf: *mut c_char,
  6168. - end: *mut c_char,
  6169. - ptr: *const c_void,
  6170. -) -> *mut c_char {
  6171. - use fmt::Write;
  6172. - // SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`.
  6173. - let mut w = unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast()) };
  6174. - let _ = w.write_fmt(unsafe { *(ptr as *const fmt::Arguments<'_>) });
  6175. - w.pos().cast()
  6176. -}
  6177. -
  6178. -/// Format strings.
  6179. -///
  6180. -/// Public but hidden since it should only be used from public macros.
  6181. -#[doc(hidden)]
  6182. -pub mod format_strings {
  6183. - /// The length we copy from the `KERN_*` kernel prefixes.
  6184. - const LENGTH_PREFIX: usize = 2;
  6185. -
  6186. - /// The length of the fixed format strings.
  6187. - pub const LENGTH: usize = 10;
  6188. -
  6189. - /// Generates a fixed format string for the kernel's [`_printk`].
  6190. - ///
  6191. - /// The format string is always the same for a given level, i.e. for a
  6192. - /// given `prefix`, which are the kernel's `KERN_*` constants.
  6193. - ///
  6194. - /// [`_printk`]: srctree/include/linux/printk.h
  6195. - const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] {
  6196. - // Ensure the `KERN_*` macros are what we expect.
  6197. - assert!(prefix[0] == b'\x01');
  6198. - if is_cont {
  6199. - assert!(prefix[1] == b'c');
  6200. - } else {
  6201. - assert!(prefix[1] >= b'0' && prefix[1] <= b'7');
  6202. - }
  6203. - assert!(prefix[2] == b'\x00');
  6204. -
  6205. - let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont {
  6206. - b"%pA\0\0\0\0\0"
  6207. - } else {
  6208. - b"%s: %pA\0"
  6209. - };
  6210. -
  6211. - [
  6212. - prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5],
  6213. - suffix[6], suffix[7],
  6214. - ]
  6215. - }
  6216. -
  6217. - // Generate the format strings at compile-time.
  6218. - //
  6219. - // This avoids the compiler generating the contents on the fly in the stack.
  6220. - //
  6221. - // Furthermore, `static` instead of `const` is used to share the strings
  6222. - // for all the kernel.
  6223. - pub static EMERG: [u8; LENGTH] = generate(false, bindings::KERN_EMERG);
  6224. - pub static ALERT: [u8; LENGTH] = generate(false, bindings::KERN_ALERT);
  6225. - pub static CRIT: [u8; LENGTH] = generate(false, bindings::KERN_CRIT);
  6226. - pub static ERR: [u8; LENGTH] = generate(false, bindings::KERN_ERR);
  6227. - pub static WARNING: [u8; LENGTH] = generate(false, bindings::KERN_WARNING);
  6228. - pub static NOTICE: [u8; LENGTH] = generate(false, bindings::KERN_NOTICE);
  6229. - pub static INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO);
  6230. - pub static DEBUG: [u8; LENGTH] = generate(false, bindings::KERN_DEBUG);
  6231. - pub static CONT: [u8; LENGTH] = generate(true, bindings::KERN_CONT);
  6232. -}
  6233. -
  6234. -/// Prints a message via the kernel's [`_printk`].
  6235. -///
  6236. -/// Public but hidden since it should only be used from public macros.
  6237. -///
  6238. -/// # Safety
  6239. -///
  6240. -/// The format string must be one of the ones in [`format_strings`], and
  6241. -/// the module name must be null-terminated.
  6242. -///
  6243. -/// [`_printk`]: srctree/include/linux/_printk.h
  6244. -#[doc(hidden)]
  6245. -#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
  6246. -pub unsafe fn call_printk(
  6247. - format_string: &[u8; format_strings::LENGTH],
  6248. - module_name: &[u8],
  6249. - args: fmt::Arguments<'_>,
  6250. -) {
  6251. - // `_printk` does not seem to fail in any path.
  6252. - #[cfg(CONFIG_PRINTK)]
  6253. - unsafe {
  6254. - bindings::_printk(
  6255. - format_string.as_ptr() as _,
  6256. - module_name.as_ptr(),
  6257. - &args as *const _ as *const c_void,
  6258. - );
  6259. - }
  6260. -}
  6261. -
  6262. -/// Prints a message via the kernel's [`_printk`] for the `CONT` level.
  6263. -///
  6264. -/// Public but hidden since it should only be used from public macros.
  6265. -///
  6266. -/// [`_printk`]: srctree/include/linux/printk.h
  6267. -#[doc(hidden)]
  6268. -#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
  6269. -pub fn call_printk_cont(args: fmt::Arguments<'_>) {
  6270. - // `_printk` does not seem to fail in any path.
  6271. - //
  6272. - // SAFETY: The format string is fixed.
  6273. - #[cfg(CONFIG_PRINTK)]
  6274. - unsafe {
  6275. - bindings::_printk(
  6276. - format_strings::CONT.as_ptr() as _,
  6277. - &args as *const _ as *const c_void,
  6278. - );
  6279. - }
  6280. -}
  6281. -
  6282. -/// Performs formatting and forwards the string to [`call_printk`].
  6283. -///
  6284. -/// Public but hidden since it should only be used from public macros.
  6285. -#[doc(hidden)]
  6286. -#[cfg(not(testlib))]
  6287. -#[macro_export]
  6288. -#[allow(clippy::crate_in_macro_def)]
  6289. -macro_rules! print_macro (
  6290. - // The non-continuation cases (most of them, e.g. `INFO`).
  6291. - ($format_string:path, false, $($arg:tt)+) => (
  6292. - // To remain sound, `arg`s must be expanded outside the `unsafe` block.
  6293. - // Typically one would use a `let` binding for that; however, `format_args!`
  6294. - // takes borrows on the arguments, but does not extend the scope of temporaries.
  6295. - // Therefore, a `match` expression is used to keep them around, since
  6296. - // the scrutinee is kept until the end of the `match`.
  6297. - match format_args!($($arg)+) {
  6298. - // SAFETY: This hidden macro should only be called by the documented
  6299. - // printing macros which ensure the format string is one of the fixed
  6300. - // ones. All `__LOG_PREFIX`s are null-terminated as they are generated
  6301. - // by the `module!` proc macro or fixed values defined in a kernel
  6302. - // crate.
  6303. - args => unsafe {
  6304. - $crate::print::call_printk(
  6305. - &$format_string,
  6306. - crate::__LOG_PREFIX,
  6307. - args,
  6308. - );
  6309. - }
  6310. - }
  6311. - );
  6312. -
  6313. - // The `CONT` case.
  6314. - ($format_string:path, true, $($arg:tt)+) => (
  6315. - $crate::print::call_printk_cont(
  6316. - format_args!($($arg)+),
  6317. - );
  6318. - );
  6319. -);
  6320. -
  6321. -/// Stub for doctests
  6322. -#[cfg(testlib)]
  6323. -#[macro_export]
  6324. -macro_rules! print_macro (
  6325. - ($format_string:path, $e:expr, $($arg:tt)+) => (
  6326. - ()
  6327. - );
  6328. -);
  6329. -
  6330. -// We could use a macro to generate these macros. However, doing so ends
  6331. -// up being a bit ugly: it requires the dollar token trick to escape `$` as
  6332. -// well as playing with the `doc` attribute. Furthermore, they cannot be easily
  6333. -// imported in the prelude due to [1]. So, for the moment, we just write them
  6334. -// manually, like in the C side; while keeping most of the logic in another
  6335. -// macro, i.e. [`print_macro`].
  6336. -//
  6337. -// [1]: https://github.com/rust-lang/rust/issues/52234
  6338. -
  6339. -/// Prints an emergency-level message (level 0).
  6340. -///
  6341. -/// Use this level if the system is unusable.
  6342. -///
  6343. -/// Equivalent to the kernel's [`pr_emerg`] macro.
  6344. -///
  6345. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6346. -/// `alloc::format!` for information about the formatting syntax.
  6347. -///
  6348. -/// [`pr_emerg`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_emerg
  6349. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6350. -///
  6351. -/// # Examples
  6352. -///
  6353. -/// ```
  6354. -/// pr_emerg!("hello {}\n", "there");
  6355. -/// ```
  6356. -#[macro_export]
  6357. -macro_rules! pr_emerg (
  6358. - ($($arg:tt)*) => (
  6359. - $crate::print_macro!($crate::print::format_strings::EMERG, false, $($arg)*)
  6360. - )
  6361. -);
  6362. -
  6363. -/// Prints an alert-level message (level 1).
  6364. -///
  6365. -/// Use this level if action must be taken immediately.
  6366. -///
  6367. -/// Equivalent to the kernel's [`pr_alert`] macro.
  6368. -///
  6369. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6370. -/// `alloc::format!` for information about the formatting syntax.
  6371. -///
  6372. -/// [`pr_alert`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_alert
  6373. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6374. -///
  6375. -/// # Examples
  6376. -///
  6377. -/// ```
  6378. -/// pr_alert!("hello {}\n", "there");
  6379. -/// ```
  6380. -#[macro_export]
  6381. -macro_rules! pr_alert (
  6382. - ($($arg:tt)*) => (
  6383. - $crate::print_macro!($crate::print::format_strings::ALERT, false, $($arg)*)
  6384. - )
  6385. -);
  6386. -
  6387. -/// Prints a critical-level message (level 2).
  6388. -///
  6389. -/// Use this level for critical conditions.
  6390. -///
  6391. -/// Equivalent to the kernel's [`pr_crit`] macro.
  6392. -///
  6393. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6394. -/// `alloc::format!` for information about the formatting syntax.
  6395. -///
  6396. -/// [`pr_crit`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_crit
  6397. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6398. -///
  6399. -/// # Examples
  6400. -///
  6401. -/// ```
  6402. -/// pr_crit!("hello {}\n", "there");
  6403. -/// ```
  6404. -#[macro_export]
  6405. -macro_rules! pr_crit (
  6406. - ($($arg:tt)*) => (
  6407. - $crate::print_macro!($crate::print::format_strings::CRIT, false, $($arg)*)
  6408. - )
  6409. -);
  6410. -
  6411. -/// Prints an error-level message (level 3).
  6412. -///
  6413. -/// Use this level for error conditions.
  6414. -///
  6415. -/// Equivalent to the kernel's [`pr_err`] macro.
  6416. -///
  6417. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6418. -/// `alloc::format!` for information about the formatting syntax.
  6419. -///
  6420. -/// [`pr_err`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_err
  6421. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6422. -///
  6423. -/// # Examples
  6424. -///
  6425. -/// ```
  6426. -/// pr_err!("hello {}\n", "there");
  6427. -/// ```
  6428. -#[macro_export]
  6429. -macro_rules! pr_err (
  6430. - ($($arg:tt)*) => (
  6431. - $crate::print_macro!($crate::print::format_strings::ERR, false, $($arg)*)
  6432. - )
  6433. -);
  6434. -
  6435. -/// Prints a warning-level message (level 4).
  6436. -///
  6437. -/// Use this level for warning conditions.
  6438. -///
  6439. -/// Equivalent to the kernel's [`pr_warn`] macro.
  6440. -///
  6441. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6442. -/// `alloc::format!` for information about the formatting syntax.
  6443. -///
  6444. -/// [`pr_warn`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_warn
  6445. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6446. -///
  6447. -/// # Examples
  6448. -///
  6449. -/// ```
  6450. -/// pr_warn!("hello {}\n", "there");
  6451. -/// ```
  6452. -#[macro_export]
  6453. -macro_rules! pr_warn (
  6454. - ($($arg:tt)*) => (
  6455. - $crate::print_macro!($crate::print::format_strings::WARNING, false, $($arg)*)
  6456. - )
  6457. -);
  6458. -
  6459. -/// Prints a notice-level message (level 5).
  6460. -///
  6461. -/// Use this level for normal but significant conditions.
  6462. -///
  6463. -/// Equivalent to the kernel's [`pr_notice`] macro.
  6464. -///
  6465. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6466. -/// `alloc::format!` for information about the formatting syntax.
  6467. -///
  6468. -/// [`pr_notice`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_notice
  6469. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6470. -///
  6471. -/// # Examples
  6472. -///
  6473. -/// ```
  6474. -/// pr_notice!("hello {}\n", "there");
  6475. -/// ```
  6476. -#[macro_export]
  6477. -macro_rules! pr_notice (
  6478. - ($($arg:tt)*) => (
  6479. - $crate::print_macro!($crate::print::format_strings::NOTICE, false, $($arg)*)
  6480. - )
  6481. -);
  6482. -
  6483. -/// Prints an info-level message (level 6).
  6484. -///
  6485. -/// Use this level for informational messages.
  6486. -///
  6487. -/// Equivalent to the kernel's [`pr_info`] macro.
  6488. -///
  6489. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6490. -/// `alloc::format!` for information about the formatting syntax.
  6491. -///
  6492. -/// [`pr_info`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_info
  6493. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6494. -///
  6495. -/// # Examples
  6496. -///
  6497. -/// ```
  6498. -/// pr_info!("hello {}\n", "there");
  6499. -/// ```
  6500. -#[macro_export]
  6501. -#[doc(alias = "print")]
  6502. -macro_rules! pr_info (
  6503. - ($($arg:tt)*) => (
  6504. - $crate::print_macro!($crate::print::format_strings::INFO, false, $($arg)*)
  6505. - )
  6506. -);
  6507. -
  6508. -/// Prints a debug-level message (level 7).
  6509. -///
  6510. -/// Use this level for debug messages.
  6511. -///
  6512. -/// Equivalent to the kernel's [`pr_debug`] macro, except that it doesn't support dynamic debug
  6513. -/// yet.
  6514. -///
  6515. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6516. -/// `alloc::format!` for information about the formatting syntax.
  6517. -///
  6518. -/// [`pr_debug`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_debug
  6519. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6520. -///
  6521. -/// # Examples
  6522. -///
  6523. -/// ```
  6524. -/// pr_debug!("hello {}\n", "there");
  6525. -/// ```
  6526. -#[macro_export]
  6527. -#[doc(alias = "print")]
  6528. -macro_rules! pr_debug (
  6529. - ($($arg:tt)*) => (
  6530. - if cfg!(debug_assertions) {
  6531. - $crate::print_macro!($crate::print::format_strings::DEBUG, false, $($arg)*)
  6532. - }
  6533. - )
  6534. -);
  6535. -
  6536. -/// Continues a previous log message in the same line.
  6537. -///
  6538. -/// Use only when continuing a previous `pr_*!` macro (e.g. [`pr_info!`]).
  6539. -///
  6540. -/// Equivalent to the kernel's [`pr_cont`] macro.
  6541. -///
  6542. -/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
  6543. -/// `alloc::format!` for information about the formatting syntax.
  6544. -///
  6545. -/// [`pr_info!`]: crate::pr_info!
  6546. -/// [`pr_cont`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_cont
  6547. -/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
  6548. -///
  6549. -/// # Examples
  6550. -///
  6551. -/// ```
  6552. -/// # use kernel::pr_cont;
  6553. -/// pr_info!("hello");
  6554. -/// pr_cont!(" {}\n", "there");
  6555. -/// ```
  6556. -#[macro_export]
  6557. -macro_rules! pr_cont (
  6558. - ($($arg:tt)*) => (
  6559. - $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)
  6560. - )
  6561. -);
  6562. diff --git a/rust/kernel/static_assert.rs b/rust/kernel/static_assert.rs
  6563. deleted file mode 100644
  6564. --- a/rust/kernel/static_assert.rs
  6565. +++ /dev/null
  6566. @@ -1,34 +0,0 @@
  6567. -// SPDX-License-Identifier: GPL-2.0
  6568. -
  6569. -//! Static assert.
  6570. -
  6571. -/// Static assert (i.e. compile-time assert).
  6572. -///
  6573. -/// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`].
  6574. -///
  6575. -/// The feature may be added to Rust in the future: see [RFC 2790].
  6576. -///
  6577. -/// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert
  6578. -/// [`static_assert`]: https://en.cppreference.com/w/cpp/language/static_assert
  6579. -/// [RFC 2790]: https://github.com/rust-lang/rfcs/issues/2790
  6580. -///
  6581. -/// # Examples
  6582. -///
  6583. -/// ```
  6584. -/// static_assert!(42 > 24);
  6585. -/// static_assert!(core::mem::size_of::<u8>() == 1);
  6586. -///
  6587. -/// const X: &[u8] = b"bar";
  6588. -/// static_assert!(X[1] == b'a');
  6589. -///
  6590. -/// const fn f(x: i32) -> i32 {
  6591. -/// x + 2
  6592. -/// }
  6593. -/// static_assert!(f(40) == 42);
  6594. -/// ```
  6595. -#[macro_export]
  6596. -macro_rules! static_assert {
  6597. - ($condition:expr) => {
  6598. - const _: () = core::assert!($condition);
  6599. - };
  6600. -}
  6601. diff --git a/rust/kernel/std_vendor.rs b/rust/kernel/std_vendor.rs
  6602. deleted file mode 100644
  6603. --- a/rust/kernel/std_vendor.rs
  6604. +++ /dev/null
  6605. @@ -1,166 +0,0 @@
  6606. -// SPDX-License-Identifier: Apache-2.0 OR MIT
  6607. -
  6608. -//! The contents of this file come from the Rust standard library, hosted in
  6609. -//! the <https://github.com/rust-lang/rust> repository, licensed under
  6610. -//! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details,
  6611. -//! see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>.
  6612. -
  6613. -/// [`std::dbg`], but using [`pr_info`] instead of [`eprintln`].
  6614. -///
  6615. -/// Prints and returns the value of a given expression for quick and dirty
  6616. -/// debugging.
  6617. -///
  6618. -/// An example:
  6619. -///
  6620. -/// ```rust
  6621. -/// let a = 2;
  6622. -/// # #[allow(clippy::dbg_macro)]
  6623. -/// let b = dbg!(a * 2) + 1;
  6624. -/// // ^-- prints: [src/main.rs:2] a * 2 = 4
  6625. -/// assert_eq!(b, 5);
  6626. -/// ```
  6627. -///
  6628. -/// The macro works by using the `Debug` implementation of the type of
  6629. -/// the given expression to print the value with [`printk`] along with the
  6630. -/// source location of the macro invocation as well as the source code
  6631. -/// of the expression.
  6632. -///
  6633. -/// Invoking the macro on an expression moves and takes ownership of it
  6634. -/// before returning the evaluated expression unchanged. If the type
  6635. -/// of the expression does not implement `Copy` and you don't want
  6636. -/// to give up ownership, you can instead borrow with `dbg!(&expr)`
  6637. -/// for some expression `expr`.
  6638. -///
  6639. -/// The `dbg!` macro works exactly the same in release builds.
  6640. -/// This is useful when debugging issues that only occur in release
  6641. -/// builds or when debugging in release mode is significantly faster.
  6642. -///
  6643. -/// Note that the macro is intended as a temporary debugging tool to be
  6644. -/// used during development. Therefore, avoid committing `dbg!` macro
  6645. -/// invocations into the kernel tree.
  6646. -///
  6647. -/// For debug output that is intended to be kept in the kernel tree,
  6648. -/// use [`pr_debug`] and similar facilities instead.
  6649. -///
  6650. -/// # Stability
  6651. -///
  6652. -/// The exact output printed by this macro should not be relied upon
  6653. -/// and is subject to future changes.
  6654. -///
  6655. -/// # Further examples
  6656. -///
  6657. -/// With a method call:
  6658. -///
  6659. -/// ```rust
  6660. -/// # #[allow(clippy::dbg_macro)]
  6661. -/// fn foo(n: usize) {
  6662. -/// if dbg!(n.checked_sub(4)).is_some() {
  6663. -/// // ...
  6664. -/// }
  6665. -/// }
  6666. -///
  6667. -/// foo(3)
  6668. -/// ```
  6669. -///
  6670. -/// This prints to the kernel log:
  6671. -///
  6672. -/// ```text,ignore
  6673. -/// [src/main.rs:4] n.checked_sub(4) = None
  6674. -/// ```
  6675. -///
  6676. -/// Naive factorial implementation:
  6677. -///
  6678. -/// ```rust
  6679. -/// # #[allow(clippy::dbg_macro)]
  6680. -/// # {
  6681. -/// fn factorial(n: u32) -> u32 {
  6682. -/// if dbg!(n <= 1) {
  6683. -/// dbg!(1)
  6684. -/// } else {
  6685. -/// dbg!(n * factorial(n - 1))
  6686. -/// }
  6687. -/// }
  6688. -///
  6689. -/// dbg!(factorial(4));
  6690. -/// # }
  6691. -/// ```
  6692. -///
  6693. -/// This prints to the kernel log:
  6694. -///
  6695. -/// ```text,ignore
  6696. -/// [src/main.rs:3] n <= 1 = false
  6697. -/// [src/main.rs:3] n <= 1 = false
  6698. -/// [src/main.rs:3] n <= 1 = false
  6699. -/// [src/main.rs:3] n <= 1 = true
  6700. -/// [src/main.rs:4] 1 = 1
  6701. -/// [src/main.rs:5] n * factorial(n - 1) = 2
  6702. -/// [src/main.rs:5] n * factorial(n - 1) = 6
  6703. -/// [src/main.rs:5] n * factorial(n - 1) = 24
  6704. -/// [src/main.rs:11] factorial(4) = 24
  6705. -/// ```
  6706. -///
  6707. -/// The `dbg!(..)` macro moves the input:
  6708. -///
  6709. -/// ```ignore
  6710. -/// /// A wrapper around `usize` which importantly is not Copyable.
  6711. -/// #[derive(Debug)]
  6712. -/// struct NoCopy(usize);
  6713. -///
  6714. -/// let a = NoCopy(42);
  6715. -/// let _ = dbg!(a); // <-- `a` is moved here.
  6716. -/// let _ = dbg!(a); // <-- `a` is moved again; error!
  6717. -/// ```
  6718. -///
  6719. -/// You can also use `dbg!()` without a value to just print the
  6720. -/// file and line whenever it's reached.
  6721. -///
  6722. -/// Finally, if you want to `dbg!(..)` multiple values, it will treat them as
  6723. -/// a tuple (and return it, too):
  6724. -///
  6725. -/// ```
  6726. -/// # #[allow(clippy::dbg_macro)]
  6727. -/// assert_eq!(dbg!(1usize, 2u32), (1, 2));
  6728. -/// ```
  6729. -///
  6730. -/// However, a single argument with a trailing comma will still not be treated
  6731. -/// as a tuple, following the convention of ignoring trailing commas in macro
  6732. -/// invocations. You can use a 1-tuple directly if you need one:
  6733. -///
  6734. -/// ```
  6735. -/// # #[allow(clippy::dbg_macro)]
  6736. -/// # {
  6737. -/// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored
  6738. -/// assert_eq!((1,), dbg!((1u32,))); // 1-tuple
  6739. -/// # }
  6740. -/// ```
  6741. -///
  6742. -/// [`std::dbg`]: https://doc.rust-lang.org/std/macro.dbg.html
  6743. -/// [`eprintln`]: https://doc.rust-lang.org/std/macro.eprintln.html
  6744. -/// [`printk`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html
  6745. -/// [`pr_info`]: crate::pr_info!
  6746. -/// [`pr_debug`]: crate::pr_debug!
  6747. -#[macro_export]
  6748. -macro_rules! dbg {
  6749. - // NOTE: We cannot use `concat!` to make a static string as a format argument
  6750. - // of `pr_info!` because `file!` could contain a `{` or
  6751. - // `$val` expression could be a block (`{ .. }`), in which case the `pr_info!`
  6752. - // will be malformed.
  6753. - () => {
  6754. - $crate::pr_info!("[{}:{}:{}]\n", ::core::file!(), ::core::line!(), ::core::column!())
  6755. - };
  6756. - ($val:expr $(,)?) => {
  6757. - // Use of `match` here is intentional because it affects the lifetimes
  6758. - // of temporaries - https://stackoverflow.com/a/48732525/1063961
  6759. - match $val {
  6760. - tmp => {
  6761. - $crate::pr_info!("[{}:{}:{}] {} = {:#?}\n",
  6762. - ::core::file!(), ::core::line!(), ::core::column!(),
  6763. - ::core::stringify!($val), &tmp);
  6764. - tmp
  6765. - }
  6766. - }
  6767. - };
  6768. - ($($val:expr),+ $(,)?) => {
  6769. - ($($crate::dbg!($val)),+,)
  6770. - };
  6771. -}
  6772. diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
  6773. deleted file mode 100644
  6774. --- a/rust/kernel/str.rs
  6775. +++ /dev/null
  6776. @@ -1,874 +0,0 @@
  6777. -// SPDX-License-Identifier: GPL-2.0
  6778. -
  6779. -//! String representations.
  6780. -
  6781. -use crate::alloc::{flags::*, vec_ext::VecExt, AllocError};
  6782. -use alloc::vec::Vec;
  6783. -use core::fmt::{self, Write};
  6784. -use core::ops::{self, Deref, DerefMut, Index};
  6785. -
  6786. -use crate::error::{code::*, Error};
  6787. -
  6788. -/// Byte string without UTF-8 validity guarantee.
  6789. -#[repr(transparent)]
  6790. -pub struct BStr([u8]);
  6791. -
  6792. -impl BStr {
  6793. - /// Returns the length of this string.
  6794. - #[inline]
  6795. - pub const fn len(&self) -> usize {
  6796. - self.0.len()
  6797. - }
  6798. -
  6799. - /// Returns `true` if the string is empty.
  6800. - #[inline]
  6801. - pub const fn is_empty(&self) -> bool {
  6802. - self.len() == 0
  6803. - }
  6804. -
  6805. - /// Creates a [`BStr`] from a `[u8]`.
  6806. - #[inline]
  6807. - pub const fn from_bytes(bytes: &[u8]) -> &Self {
  6808. - // SAFETY: `BStr` is transparent to `[u8]`.
  6809. - unsafe { &*(bytes as *const [u8] as *const BStr) }
  6810. - }
  6811. -}
  6812. -
  6813. -impl fmt::Display for BStr {
  6814. - /// Formats printable ASCII characters, escaping the rest.
  6815. - ///
  6816. - /// ```
  6817. - /// # use kernel::{fmt, b_str, str::{BStr, CString}};
  6818. - /// let ascii = b_str!("Hello, BStr!");
  6819. - /// let s = CString::try_from_fmt(fmt!("{}", ascii)).unwrap();
  6820. - /// assert_eq!(s.as_bytes(), "Hello, BStr!".as_bytes());
  6821. - ///
  6822. - /// let non_ascii = b_str!("🦀");
  6823. - /// let s = CString::try_from_fmt(fmt!("{}", non_ascii)).unwrap();
  6824. - /// assert_eq!(s.as_bytes(), "\\xf0\\x9f\\xa6\\x80".as_bytes());
  6825. - /// ```
  6826. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  6827. - for &b in &self.0 {
  6828. - match b {
  6829. - // Common escape codes.
  6830. - b'\t' => f.write_str("\\t")?,
  6831. - b'\n' => f.write_str("\\n")?,
  6832. - b'\r' => f.write_str("\\r")?,
  6833. - // Printable characters.
  6834. - 0x20..=0x7e => f.write_char(b as char)?,
  6835. - _ => write!(f, "\\x{:02x}", b)?,
  6836. - }
  6837. - }
  6838. - Ok(())
  6839. - }
  6840. -}
  6841. -
  6842. -impl fmt::Debug for BStr {
  6843. - /// Formats printable ASCII characters with a double quote on either end,
  6844. - /// escaping the rest.
  6845. - ///
  6846. - /// ```
  6847. - /// # use kernel::{fmt, b_str, str::{BStr, CString}};
  6848. - /// // Embedded double quotes are escaped.
  6849. - /// let ascii = b_str!("Hello, \"BStr\"!");
  6850. - /// let s = CString::try_from_fmt(fmt!("{:?}", ascii)).unwrap();
  6851. - /// assert_eq!(s.as_bytes(), "\"Hello, \\\"BStr\\\"!\"".as_bytes());
  6852. - ///
  6853. - /// let non_ascii = b_str!("😺");
  6854. - /// let s = CString::try_from_fmt(fmt!("{:?}", non_ascii)).unwrap();
  6855. - /// assert_eq!(s.as_bytes(), "\"\\xf0\\x9f\\x98\\xba\"".as_bytes());
  6856. - /// ```
  6857. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  6858. - f.write_char('"')?;
  6859. - for &b in &self.0 {
  6860. - match b {
  6861. - // Common escape codes.
  6862. - b'\t' => f.write_str("\\t")?,
  6863. - b'\n' => f.write_str("\\n")?,
  6864. - b'\r' => f.write_str("\\r")?,
  6865. - // String escape characters.
  6866. - b'\"' => f.write_str("\\\"")?,
  6867. - b'\\' => f.write_str("\\\\")?,
  6868. - // Printable characters.
  6869. - 0x20..=0x7e => f.write_char(b as char)?,
  6870. - _ => write!(f, "\\x{:02x}", b)?,
  6871. - }
  6872. - }
  6873. - f.write_char('"')
  6874. - }
  6875. -}
  6876. -
  6877. -impl Deref for BStr {
  6878. - type Target = [u8];
  6879. -
  6880. - #[inline]
  6881. - fn deref(&self) -> &Self::Target {
  6882. - &self.0
  6883. - }
  6884. -}
  6885. -
  6886. -/// Creates a new [`BStr`] from a string literal.
  6887. -///
  6888. -/// `b_str!` converts the supplied string literal to byte string, so non-ASCII
  6889. -/// characters can be included.
  6890. -///
  6891. -/// # Examples
  6892. -///
  6893. -/// ```
  6894. -/// # use kernel::b_str;
  6895. -/// # use kernel::str::BStr;
  6896. -/// const MY_BSTR: &BStr = b_str!("My awesome BStr!");
  6897. -/// ```
  6898. -#[macro_export]
  6899. -macro_rules! b_str {
  6900. - ($str:literal) => {{
  6901. - const S: &'static str = $str;
  6902. - const C: &'static $crate::str::BStr = $crate::str::BStr::from_bytes(S.as_bytes());
  6903. - C
  6904. - }};
  6905. -}
  6906. -
  6907. -/// Possible errors when using conversion functions in [`CStr`].
  6908. -#[derive(Debug, Clone, Copy)]
  6909. -pub enum CStrConvertError {
  6910. - /// Supplied bytes contain an interior `NUL`.
  6911. - InteriorNul,
  6912. -
  6913. - /// Supplied bytes are not terminated by `NUL`.
  6914. - NotNulTerminated,
  6915. -}
  6916. -
  6917. -impl From<CStrConvertError> for Error {
  6918. - #[inline]
  6919. - fn from(_: CStrConvertError) -> Error {
  6920. - EINVAL
  6921. - }
  6922. -}
  6923. -
  6924. -/// A string that is guaranteed to have exactly one `NUL` byte, which is at the
  6925. -/// end.
  6926. -///
  6927. -/// Used for interoperability with kernel APIs that take C strings.
  6928. -#[repr(transparent)]
  6929. -pub struct CStr([u8]);
  6930. -
  6931. -impl CStr {
  6932. - /// Returns the length of this string excluding `NUL`.
  6933. - #[inline]
  6934. - pub const fn len(&self) -> usize {
  6935. - self.len_with_nul() - 1
  6936. - }
  6937. -
  6938. - /// Returns the length of this string with `NUL`.
  6939. - #[inline]
  6940. - pub const fn len_with_nul(&self) -> usize {
  6941. - // SAFETY: This is one of the invariant of `CStr`.
  6942. - // We add a `unreachable_unchecked` here to hint the optimizer that
  6943. - // the value returned from this function is non-zero.
  6944. - if self.0.is_empty() {
  6945. - unsafe { core::hint::unreachable_unchecked() };
  6946. - }
  6947. - self.0.len()
  6948. - }
  6949. -
  6950. - /// Returns `true` if the string only includes `NUL`.
  6951. - #[inline]
  6952. - pub const fn is_empty(&self) -> bool {
  6953. - self.len() == 0
  6954. - }
  6955. -
  6956. - /// Wraps a raw C string pointer.
  6957. - ///
  6958. - /// # Safety
  6959. - ///
  6960. - /// `ptr` must be a valid pointer to a `NUL`-terminated C string, and it must
  6961. - /// last at least `'a`. When `CStr` is alive, the memory pointed by `ptr`
  6962. - /// must not be mutated.
  6963. - #[inline]
  6964. - pub unsafe fn from_char_ptr<'a>(ptr: *const core::ffi::c_char) -> &'a Self {
  6965. - // SAFETY: The safety precondition guarantees `ptr` is a valid pointer
  6966. - // to a `NUL`-terminated C string.
  6967. - let len = unsafe { bindings::strlen(ptr) } + 1;
  6968. - // SAFETY: Lifetime guaranteed by the safety precondition.
  6969. - let bytes = unsafe { core::slice::from_raw_parts(ptr as _, len as _) };
  6970. - // SAFETY: As `len` is returned by `strlen`, `bytes` does not contain interior `NUL`.
  6971. - // As we have added 1 to `len`, the last byte is known to be `NUL`.
  6972. - unsafe { Self::from_bytes_with_nul_unchecked(bytes) }
  6973. - }
  6974. -
  6975. - /// Creates a [`CStr`] from a `[u8]`.
  6976. - ///
  6977. - /// The provided slice must be `NUL`-terminated, does not contain any
  6978. - /// interior `NUL` bytes.
  6979. - pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, CStrConvertError> {
  6980. - if bytes.is_empty() {
  6981. - return Err(CStrConvertError::NotNulTerminated);
  6982. - }
  6983. - if bytes[bytes.len() - 1] != 0 {
  6984. - return Err(CStrConvertError::NotNulTerminated);
  6985. - }
  6986. - let mut i = 0;
  6987. - // `i + 1 < bytes.len()` allows LLVM to optimize away bounds checking,
  6988. - // while it couldn't optimize away bounds checks for `i < bytes.len() - 1`.
  6989. - while i + 1 < bytes.len() {
  6990. - if bytes[i] == 0 {
  6991. - return Err(CStrConvertError::InteriorNul);
  6992. - }
  6993. - i += 1;
  6994. - }
  6995. - // SAFETY: We just checked that all properties hold.
  6996. - Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
  6997. - }
  6998. -
  6999. - /// Creates a [`CStr`] from a `[u8]` without performing any additional
  7000. - /// checks.
  7001. - ///
  7002. - /// # Safety
  7003. - ///
  7004. - /// `bytes` *must* end with a `NUL` byte, and should only have a single
  7005. - /// `NUL` byte (or the string will be truncated).
  7006. - #[inline]
  7007. - pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
  7008. - // SAFETY: Properties of `bytes` guaranteed by the safety precondition.
  7009. - unsafe { core::mem::transmute(bytes) }
  7010. - }
  7011. -
  7012. - /// Creates a mutable [`CStr`] from a `[u8]` without performing any
  7013. - /// additional checks.
  7014. - ///
  7015. - /// # Safety
  7016. - ///
  7017. - /// `bytes` *must* end with a `NUL` byte, and should only have a single
  7018. - /// `NUL` byte (or the string will be truncated).
  7019. - #[inline]
  7020. - pub unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut CStr {
  7021. - // SAFETY: Properties of `bytes` guaranteed by the safety precondition.
  7022. - unsafe { &mut *(bytes as *mut [u8] as *mut CStr) }
  7023. - }
  7024. -
  7025. - /// Returns a C pointer to the string.
  7026. - #[inline]
  7027. - pub const fn as_char_ptr(&self) -> *const core::ffi::c_char {
  7028. - self.0.as_ptr() as _
  7029. - }
  7030. -
  7031. - /// Convert the string to a byte slice without the trailing `NUL` byte.
  7032. - #[inline]
  7033. - pub fn as_bytes(&self) -> &[u8] {
  7034. - &self.0[..self.len()]
  7035. - }
  7036. -
  7037. - /// Convert the string to a byte slice containing the trailing `NUL` byte.
  7038. - #[inline]
  7039. - pub const fn as_bytes_with_nul(&self) -> &[u8] {
  7040. - &self.0
  7041. - }
  7042. -
  7043. - /// Yields a [`&str`] slice if the [`CStr`] contains valid UTF-8.
  7044. - ///
  7045. - /// If the contents of the [`CStr`] are valid UTF-8 data, this
  7046. - /// function will return the corresponding [`&str`] slice. Otherwise,
  7047. - /// it will return an error with details of where UTF-8 validation failed.
  7048. - ///
  7049. - /// # Examples
  7050. - ///
  7051. - /// ```
  7052. - /// # use kernel::str::CStr;
  7053. - /// let cstr = CStr::from_bytes_with_nul(b"foo\0").unwrap();
  7054. - /// assert_eq!(cstr.to_str(), Ok("foo"));
  7055. - /// ```
  7056. - #[inline]
  7057. - pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
  7058. - core::str::from_utf8(self.as_bytes())
  7059. - }
  7060. -
  7061. - /// Unsafely convert this [`CStr`] into a [`&str`], without checking for
  7062. - /// valid UTF-8.
  7063. - ///
  7064. - /// # Safety
  7065. - ///
  7066. - /// The contents must be valid UTF-8.
  7067. - ///
  7068. - /// # Examples
  7069. - ///
  7070. - /// ```
  7071. - /// # use kernel::c_str;
  7072. - /// # use kernel::str::CStr;
  7073. - /// let bar = c_str!("ツ");
  7074. - /// // SAFETY: String literals are guaranteed to be valid UTF-8
  7075. - /// // by the Rust compiler.
  7076. - /// assert_eq!(unsafe { bar.as_str_unchecked() }, "ツ");
  7077. - /// ```
  7078. - #[inline]
  7079. - pub unsafe fn as_str_unchecked(&self) -> &str {
  7080. - unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
  7081. - }
  7082. -
  7083. - /// Convert this [`CStr`] into a [`CString`] by allocating memory and
  7084. - /// copying over the string data.
  7085. - pub fn to_cstring(&self) -> Result<CString, AllocError> {
  7086. - CString::try_from(self)
  7087. - }
  7088. -
  7089. - /// Converts this [`CStr`] to its ASCII lower case equivalent in-place.
  7090. - ///
  7091. - /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
  7092. - /// but non-ASCII letters are unchanged.
  7093. - ///
  7094. - /// To return a new lowercased value without modifying the existing one, use
  7095. - /// [`to_ascii_lowercase()`].
  7096. - ///
  7097. - /// [`to_ascii_lowercase()`]: #method.to_ascii_lowercase
  7098. - pub fn make_ascii_lowercase(&mut self) {
  7099. - // INVARIANT: This doesn't introduce or remove NUL bytes in the C
  7100. - // string.
  7101. - self.0.make_ascii_lowercase();
  7102. - }
  7103. -
  7104. - /// Converts this [`CStr`] to its ASCII upper case equivalent in-place.
  7105. - ///
  7106. - /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
  7107. - /// but non-ASCII letters are unchanged.
  7108. - ///
  7109. - /// To return a new uppercased value without modifying the existing one, use
  7110. - /// [`to_ascii_uppercase()`].
  7111. - ///
  7112. - /// [`to_ascii_uppercase()`]: #method.to_ascii_uppercase
  7113. - pub fn make_ascii_uppercase(&mut self) {
  7114. - // INVARIANT: This doesn't introduce or remove NUL bytes in the C
  7115. - // string.
  7116. - self.0.make_ascii_uppercase();
  7117. - }
  7118. -
  7119. - /// Returns a copy of this [`CString`] where each character is mapped to its
  7120. - /// ASCII lower case equivalent.
  7121. - ///
  7122. - /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
  7123. - /// but non-ASCII letters are unchanged.
  7124. - ///
  7125. - /// To lowercase the value in-place, use [`make_ascii_lowercase`].
  7126. - ///
  7127. - /// [`make_ascii_lowercase`]: str::make_ascii_lowercase
  7128. - pub fn to_ascii_lowercase(&self) -> Result<CString, AllocError> {
  7129. - let mut s = self.to_cstring()?;
  7130. -
  7131. - s.make_ascii_lowercase();
  7132. -
  7133. - Ok(s)
  7134. - }
  7135. -
  7136. - /// Returns a copy of this [`CString`] where each character is mapped to its
  7137. - /// ASCII upper case equivalent.
  7138. - ///
  7139. - /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
  7140. - /// but non-ASCII letters are unchanged.
  7141. - ///
  7142. - /// To uppercase the value in-place, use [`make_ascii_uppercase`].
  7143. - ///
  7144. - /// [`make_ascii_uppercase`]: str::make_ascii_uppercase
  7145. - pub fn to_ascii_uppercase(&self) -> Result<CString, AllocError> {
  7146. - let mut s = self.to_cstring()?;
  7147. -
  7148. - s.make_ascii_uppercase();
  7149. -
  7150. - Ok(s)
  7151. - }
  7152. -}
  7153. -
  7154. -impl fmt::Display for CStr {
  7155. - /// Formats printable ASCII characters, escaping the rest.
  7156. - ///
  7157. - /// ```
  7158. - /// # use kernel::c_str;
  7159. - /// # use kernel::fmt;
  7160. - /// # use kernel::str::CStr;
  7161. - /// # use kernel::str::CString;
  7162. - /// let penguin = c_str!("🐧");
  7163. - /// let s = CString::try_from_fmt(fmt!("{}", penguin)).unwrap();
  7164. - /// assert_eq!(s.as_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
  7165. - ///
  7166. - /// let ascii = c_str!("so \"cool\"");
  7167. - /// let s = CString::try_from_fmt(fmt!("{}", ascii)).unwrap();
  7168. - /// assert_eq!(s.as_bytes_with_nul(), "so \"cool\"\0".as_bytes());
  7169. - /// ```
  7170. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  7171. - for &c in self.as_bytes() {
  7172. - if (0x20..0x7f).contains(&c) {
  7173. - // Printable character.
  7174. - f.write_char(c as char)?;
  7175. - } else {
  7176. - write!(f, "\\x{:02x}", c)?;
  7177. - }
  7178. - }
  7179. - Ok(())
  7180. - }
  7181. -}
  7182. -
  7183. -impl fmt::Debug for CStr {
  7184. - /// Formats printable ASCII characters with a double quote on either end, escaping the rest.
  7185. - ///
  7186. - /// ```
  7187. - /// # use kernel::c_str;
  7188. - /// # use kernel::fmt;
  7189. - /// # use kernel::str::CStr;
  7190. - /// # use kernel::str::CString;
  7191. - /// let penguin = c_str!("🐧");
  7192. - /// let s = CString::try_from_fmt(fmt!("{:?}", penguin)).unwrap();
  7193. - /// assert_eq!(s.as_bytes_with_nul(), "\"\\xf0\\x9f\\x90\\xa7\"\0".as_bytes());
  7194. - ///
  7195. - /// // Embedded double quotes are escaped.
  7196. - /// let ascii = c_str!("so \"cool\"");
  7197. - /// let s = CString::try_from_fmt(fmt!("{:?}", ascii)).unwrap();
  7198. - /// assert_eq!(s.as_bytes_with_nul(), "\"so \\\"cool\\\"\"\0".as_bytes());
  7199. - /// ```
  7200. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  7201. - f.write_str("\"")?;
  7202. - for &c in self.as_bytes() {
  7203. - match c {
  7204. - // Printable characters.
  7205. - b'\"' => f.write_str("\\\"")?,
  7206. - 0x20..=0x7e => f.write_char(c as char)?,
  7207. - _ => write!(f, "\\x{:02x}", c)?,
  7208. - }
  7209. - }
  7210. - f.write_str("\"")
  7211. - }
  7212. -}
  7213. -
  7214. -impl AsRef<BStr> for CStr {
  7215. - #[inline]
  7216. - fn as_ref(&self) -> &BStr {
  7217. - BStr::from_bytes(self.as_bytes())
  7218. - }
  7219. -}
  7220. -
  7221. -impl Deref for CStr {
  7222. - type Target = BStr;
  7223. -
  7224. - #[inline]
  7225. - fn deref(&self) -> &Self::Target {
  7226. - self.as_ref()
  7227. - }
  7228. -}
  7229. -
  7230. -impl Index<ops::RangeFrom<usize>> for CStr {
  7231. - type Output = CStr;
  7232. -
  7233. - #[inline]
  7234. - fn index(&self, index: ops::RangeFrom<usize>) -> &Self::Output {
  7235. - // Delegate bounds checking to slice.
  7236. - // Assign to _ to mute clippy's unnecessary operation warning.
  7237. - let _ = &self.as_bytes()[index.start..];
  7238. - // SAFETY: We just checked the bounds.
  7239. - unsafe { Self::from_bytes_with_nul_unchecked(&self.0[index.start..]) }
  7240. - }
  7241. -}
  7242. -
  7243. -impl Index<ops::RangeFull> for CStr {
  7244. - type Output = CStr;
  7245. -
  7246. - #[inline]
  7247. - fn index(&self, _index: ops::RangeFull) -> &Self::Output {
  7248. - self
  7249. - }
  7250. -}
  7251. -
  7252. -mod private {
  7253. - use core::ops;
  7254. -
  7255. - // Marker trait for index types that can be forward to `BStr`.
  7256. - pub trait CStrIndex {}
  7257. -
  7258. - impl CStrIndex for usize {}
  7259. - impl CStrIndex for ops::Range<usize> {}
  7260. - impl CStrIndex for ops::RangeInclusive<usize> {}
  7261. - impl CStrIndex for ops::RangeToInclusive<usize> {}
  7262. -}
  7263. -
  7264. -impl<Idx> Index<Idx> for CStr
  7265. -where
  7266. - Idx: private::CStrIndex,
  7267. - BStr: Index<Idx>,
  7268. -{
  7269. - type Output = <BStr as Index<Idx>>::Output;
  7270. -
  7271. - #[inline]
  7272. - fn index(&self, index: Idx) -> &Self::Output {
  7273. - &self.as_ref()[index]
  7274. - }
  7275. -}
  7276. -
  7277. -/// Creates a new [`CStr`] from a string literal.
  7278. -///
  7279. -/// The string literal should not contain any `NUL` bytes.
  7280. -///
  7281. -/// # Examples
  7282. -///
  7283. -/// ```
  7284. -/// # use kernel::c_str;
  7285. -/// # use kernel::str::CStr;
  7286. -/// const MY_CSTR: &CStr = c_str!("My awesome CStr!");
  7287. -/// ```
  7288. -#[macro_export]
  7289. -macro_rules! c_str {
  7290. - ($str:expr) => {{
  7291. - const S: &str = concat!($str, "\0");
  7292. - const C: &$crate::str::CStr = match $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) {
  7293. - Ok(v) => v,
  7294. - Err(_) => panic!("string contains interior NUL"),
  7295. - };
  7296. - C
  7297. - }};
  7298. -}
  7299. -
  7300. -#[cfg(test)]
  7301. -mod tests {
  7302. - use super::*;
  7303. - use alloc::format;
  7304. -
  7305. - const ALL_ASCII_CHARS: &'static str =
  7306. - "\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f\
  7307. - \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f \
  7308. - !\"#$%&'()*+,-./0123456789:;<=>?@\
  7309. - ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\
  7310. - \\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\
  7311. - \\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\
  7312. - \\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\
  7313. - \\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\
  7314. - \\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\
  7315. - \\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\
  7316. - \\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\
  7317. - \\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff";
  7318. -
  7319. - #[test]
  7320. - fn test_cstr_to_str() {
  7321. - let good_bytes = b"\xf0\x9f\xa6\x80\0";
  7322. - let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap();
  7323. - let checked_str = checked_cstr.to_str().unwrap();
  7324. - assert_eq!(checked_str, "🦀");
  7325. - }
  7326. -
  7327. - #[test]
  7328. - #[should_panic]
  7329. - fn test_cstr_to_str_panic() {
  7330. - let bad_bytes = b"\xc3\x28\0";
  7331. - let checked_cstr = CStr::from_bytes_with_nul(bad_bytes).unwrap();
  7332. - checked_cstr.to_str().unwrap();
  7333. - }
  7334. -
  7335. - #[test]
  7336. - fn test_cstr_as_str_unchecked() {
  7337. - let good_bytes = b"\xf0\x9f\x90\xA7\0";
  7338. - let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap();
  7339. - let unchecked_str = unsafe { checked_cstr.as_str_unchecked() };
  7340. - assert_eq!(unchecked_str, "🐧");
  7341. - }
  7342. -
  7343. - #[test]
  7344. - fn test_cstr_display() {
  7345. - let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0").unwrap();
  7346. - assert_eq!(format!("{}", hello_world), "hello, world!");
  7347. - let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0").unwrap();
  7348. - assert_eq!(format!("{}", non_printables), "\\x01\\x09\\x0a");
  7349. - let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0").unwrap();
  7350. - assert_eq!(format!("{}", non_ascii), "d\\xe9j\\xe0 vu");
  7351. - let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0").unwrap();
  7352. - assert_eq!(format!("{}", good_bytes), "\\xf0\\x9f\\xa6\\x80");
  7353. - }
  7354. -
  7355. - #[test]
  7356. - fn test_cstr_display_all_bytes() {
  7357. - let mut bytes: [u8; 256] = [0; 256];
  7358. - // fill `bytes` with [1..=255] + [0]
  7359. - for i in u8::MIN..=u8::MAX {
  7360. - bytes[i as usize] = i.wrapping_add(1);
  7361. - }
  7362. - let cstr = CStr::from_bytes_with_nul(&bytes).unwrap();
  7363. - assert_eq!(format!("{}", cstr), ALL_ASCII_CHARS);
  7364. - }
  7365. -
  7366. - #[test]
  7367. - fn test_cstr_debug() {
  7368. - let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0").unwrap();
  7369. - assert_eq!(format!("{:?}", hello_world), "\"hello, world!\"");
  7370. - let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0").unwrap();
  7371. - assert_eq!(format!("{:?}", non_printables), "\"\\x01\\x09\\x0a\"");
  7372. - let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0").unwrap();
  7373. - assert_eq!(format!("{:?}", non_ascii), "\"d\\xe9j\\xe0 vu\"");
  7374. - let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0").unwrap();
  7375. - assert_eq!(format!("{:?}", good_bytes), "\"\\xf0\\x9f\\xa6\\x80\"");
  7376. - }
  7377. -
  7378. - #[test]
  7379. - fn test_bstr_display() {
  7380. - let hello_world = BStr::from_bytes(b"hello, world!");
  7381. - assert_eq!(format!("{}", hello_world), "hello, world!");
  7382. - let escapes = BStr::from_bytes(b"_\t_\n_\r_\\_\'_\"_");
  7383. - assert_eq!(format!("{}", escapes), "_\\t_\\n_\\r_\\_'_\"_");
  7384. - let others = BStr::from_bytes(b"\x01");
  7385. - assert_eq!(format!("{}", others), "\\x01");
  7386. - let non_ascii = BStr::from_bytes(b"d\xe9j\xe0 vu");
  7387. - assert_eq!(format!("{}", non_ascii), "d\\xe9j\\xe0 vu");
  7388. - let good_bytes = BStr::from_bytes(b"\xf0\x9f\xa6\x80");
  7389. - assert_eq!(format!("{}", good_bytes), "\\xf0\\x9f\\xa6\\x80");
  7390. - }
  7391. -
  7392. - #[test]
  7393. - fn test_bstr_debug() {
  7394. - let hello_world = BStr::from_bytes(b"hello, world!");
  7395. - assert_eq!(format!("{:?}", hello_world), "\"hello, world!\"");
  7396. - let escapes = BStr::from_bytes(b"_\t_\n_\r_\\_\'_\"_");
  7397. - assert_eq!(format!("{:?}", escapes), "\"_\\t_\\n_\\r_\\\\_'_\\\"_\"");
  7398. - let others = BStr::from_bytes(b"\x01");
  7399. - assert_eq!(format!("{:?}", others), "\"\\x01\"");
  7400. - let non_ascii = BStr::from_bytes(b"d\xe9j\xe0 vu");
  7401. - assert_eq!(format!("{:?}", non_ascii), "\"d\\xe9j\\xe0 vu\"");
  7402. - let good_bytes = BStr::from_bytes(b"\xf0\x9f\xa6\x80");
  7403. - assert_eq!(format!("{:?}", good_bytes), "\"\\xf0\\x9f\\xa6\\x80\"");
  7404. - }
  7405. -}
  7406. -
  7407. -/// Allows formatting of [`fmt::Arguments`] into a raw buffer.
  7408. -///
  7409. -/// It does not fail if callers write past the end of the buffer so that they can calculate the
  7410. -/// size required to fit everything.
  7411. -///
  7412. -/// # Invariants
  7413. -///
  7414. -/// The memory region between `pos` (inclusive) and `end` (exclusive) is valid for writes if `pos`
  7415. -/// is less than `end`.
  7416. -pub(crate) struct RawFormatter {
  7417. - // Use `usize` to use `saturating_*` functions.
  7418. - beg: usize,
  7419. - pos: usize,
  7420. - end: usize,
  7421. -}
  7422. -
  7423. -impl RawFormatter {
  7424. - /// Creates a new instance of [`RawFormatter`] with an empty buffer.
  7425. - fn new() -> Self {
  7426. - // INVARIANT: The buffer is empty, so the region that needs to be writable is empty.
  7427. - Self {
  7428. - beg: 0,
  7429. - pos: 0,
  7430. - end: 0,
  7431. - }
  7432. - }
  7433. -
  7434. - /// Creates a new instance of [`RawFormatter`] with the given buffer pointers.
  7435. - ///
  7436. - /// # Safety
  7437. - ///
  7438. - /// If `pos` is less than `end`, then the region between `pos` (inclusive) and `end`
  7439. - /// (exclusive) must be valid for writes for the lifetime of the returned [`RawFormatter`].
  7440. - pub(crate) unsafe fn from_ptrs(pos: *mut u8, end: *mut u8) -> Self {
  7441. - // INVARIANT: The safety requirements guarantee the type invariants.
  7442. - Self {
  7443. - beg: pos as _,
  7444. - pos: pos as _,
  7445. - end: end as _,
  7446. - }
  7447. - }
  7448. -
  7449. - /// Creates a new instance of [`RawFormatter`] with the given buffer.
  7450. - ///
  7451. - /// # Safety
  7452. - ///
  7453. - /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes
  7454. - /// for the lifetime of the returned [`RawFormatter`].
  7455. - pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
  7456. - let pos = buf as usize;
  7457. - // INVARIANT: We ensure that `end` is never less then `buf`, and the safety requirements
  7458. - // guarantees that the memory region is valid for writes.
  7459. - Self {
  7460. - pos,
  7461. - beg: pos,
  7462. - end: pos.saturating_add(len),
  7463. - }
  7464. - }
  7465. -
  7466. - /// Returns the current insert position.
  7467. - ///
  7468. - /// N.B. It may point to invalid memory.
  7469. - pub(crate) fn pos(&self) -> *mut u8 {
  7470. - self.pos as _
  7471. - }
  7472. -
  7473. - /// Returns the number of bytes written to the formatter.
  7474. - pub(crate) fn bytes_written(&self) -> usize {
  7475. - self.pos - self.beg
  7476. - }
  7477. -}
  7478. -
  7479. -impl fmt::Write for RawFormatter {
  7480. - fn write_str(&mut self, s: &str) -> fmt::Result {
  7481. - // `pos` value after writing `len` bytes. This does not have to be bounded by `end`, but we
  7482. - // don't want it to wrap around to 0.
  7483. - let pos_new = self.pos.saturating_add(s.len());
  7484. -
  7485. - // Amount that we can copy. `saturating_sub` ensures we get 0 if `pos` goes past `end`.
  7486. - let len_to_copy = core::cmp::min(pos_new, self.end).saturating_sub(self.pos);
  7487. -
  7488. - if len_to_copy > 0 {
  7489. - // SAFETY: If `len_to_copy` is non-zero, then we know `pos` has not gone past `end`
  7490. - // yet, so it is valid for write per the type invariants.
  7491. - unsafe {
  7492. - core::ptr::copy_nonoverlapping(
  7493. - s.as_bytes().as_ptr(),
  7494. - self.pos as *mut u8,
  7495. - len_to_copy,
  7496. - )
  7497. - };
  7498. - }
  7499. -
  7500. - self.pos = pos_new;
  7501. - Ok(())
  7502. - }
  7503. -}
  7504. -
  7505. -/// Allows formatting of [`fmt::Arguments`] into a raw buffer.
  7506. -///
  7507. -/// Fails if callers attempt to write more than will fit in the buffer.
  7508. -pub(crate) struct Formatter(RawFormatter);
  7509. -
  7510. -impl Formatter {
  7511. - /// Creates a new instance of [`Formatter`] with the given buffer.
  7512. - ///
  7513. - /// # Safety
  7514. - ///
  7515. - /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes
  7516. - /// for the lifetime of the returned [`Formatter`].
  7517. - pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
  7518. - // SAFETY: The safety requirements of this function satisfy those of the callee.
  7519. - Self(unsafe { RawFormatter::from_buffer(buf, len) })
  7520. - }
  7521. -}
  7522. -
  7523. -impl Deref for Formatter {
  7524. - type Target = RawFormatter;
  7525. -
  7526. - fn deref(&self) -> &Self::Target {
  7527. - &self.0
  7528. - }
  7529. -}
  7530. -
  7531. -impl fmt::Write for Formatter {
  7532. - fn write_str(&mut self, s: &str) -> fmt::Result {
  7533. - self.0.write_str(s)?;
  7534. -
  7535. - // Fail the request if we go past the end of the buffer.
  7536. - if self.0.pos > self.0.end {
  7537. - Err(fmt::Error)
  7538. - } else {
  7539. - Ok(())
  7540. - }
  7541. - }
  7542. -}
  7543. -
  7544. -/// An owned string that is guaranteed to have exactly one `NUL` byte, which is at the end.
  7545. -///
  7546. -/// Used for interoperability with kernel APIs that take C strings.
  7547. -///
  7548. -/// # Invariants
  7549. -///
  7550. -/// The string is always `NUL`-terminated and contains no other `NUL` bytes.
  7551. -///
  7552. -/// # Examples
  7553. -///
  7554. -/// ```
  7555. -/// use kernel::{str::CString, fmt};
  7556. -///
  7557. -/// let s = CString::try_from_fmt(fmt!("{}{}{}", "abc", 10, 20)).unwrap();
  7558. -/// assert_eq!(s.as_bytes_with_nul(), "abc1020\0".as_bytes());
  7559. -///
  7560. -/// let tmp = "testing";
  7561. -/// let s = CString::try_from_fmt(fmt!("{tmp}{}", 123)).unwrap();
  7562. -/// assert_eq!(s.as_bytes_with_nul(), "testing123\0".as_bytes());
  7563. -///
  7564. -/// // This fails because it has an embedded `NUL` byte.
  7565. -/// let s = CString::try_from_fmt(fmt!("a\0b{}", 123));
  7566. -/// assert_eq!(s.is_ok(), false);
  7567. -/// ```
  7568. -pub struct CString {
  7569. - buf: Vec<u8>,
  7570. -}
  7571. -
  7572. -impl CString {
  7573. - /// Creates an instance of [`CString`] from the given formatted arguments.
  7574. - pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result<Self, Error> {
  7575. - // Calculate the size needed (formatted string plus `NUL` terminator).
  7576. - let mut f = RawFormatter::new();
  7577. - f.write_fmt(args)?;
  7578. - f.write_str("\0")?;
  7579. - let size = f.bytes_written();
  7580. -
  7581. - // Allocate a vector with the required number of bytes, and write to it.
  7582. - let mut buf = <Vec<_> as VecExt<_>>::with_capacity(size, GFP_KERNEL)?;
  7583. - // SAFETY: The buffer stored in `buf` is at least of size `size` and is valid for writes.
  7584. - let mut f = unsafe { Formatter::from_buffer(buf.as_mut_ptr(), size) };
  7585. - f.write_fmt(args)?;
  7586. - f.write_str("\0")?;
  7587. -
  7588. - // SAFETY: The number of bytes that can be written to `f` is bounded by `size`, which is
  7589. - // `buf`'s capacity. The contents of the buffer have been initialised by writes to `f`.
  7590. - unsafe { buf.set_len(f.bytes_written()) };
  7591. -
  7592. - // Check that there are no `NUL` bytes before the end.
  7593. - // SAFETY: The buffer is valid for read because `f.bytes_written()` is bounded by `size`
  7594. - // (which the minimum buffer size) and is non-zero (we wrote at least the `NUL` terminator)
  7595. - // so `f.bytes_written() - 1` doesn't underflow.
  7596. - let ptr = unsafe { bindings::memchr(buf.as_ptr().cast(), 0, (f.bytes_written() - 1) as _) };
  7597. - if !ptr.is_null() {
  7598. - return Err(EINVAL);
  7599. - }
  7600. -
  7601. - // INVARIANT: We wrote the `NUL` terminator and checked above that no other `NUL` bytes
  7602. - // exist in the buffer.
  7603. - Ok(Self { buf })
  7604. - }
  7605. -}
  7606. -
  7607. -impl Deref for CString {
  7608. - type Target = CStr;
  7609. -
  7610. - fn deref(&self) -> &Self::Target {
  7611. - // SAFETY: The type invariants guarantee that the string is `NUL`-terminated and that no
  7612. - // other `NUL` bytes exist.
  7613. - unsafe { CStr::from_bytes_with_nul_unchecked(self.buf.as_slice()) }
  7614. - }
  7615. -}
  7616. -
  7617. -impl DerefMut for CString {
  7618. - fn deref_mut(&mut self) -> &mut Self::Target {
  7619. - // SAFETY: A `CString` is always NUL-terminated and contains no other
  7620. - // NUL bytes.
  7621. - unsafe { CStr::from_bytes_with_nul_unchecked_mut(self.buf.as_mut_slice()) }
  7622. - }
  7623. -}
  7624. -
  7625. -impl<'a> TryFrom<&'a CStr> for CString {
  7626. - type Error = AllocError;
  7627. -
  7628. - fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
  7629. - let mut buf = Vec::new();
  7630. -
  7631. - <Vec<_> as VecExt<_>>::extend_from_slice(&mut buf, cstr.as_bytes_with_nul(), GFP_KERNEL)
  7632. - .map_err(|_| AllocError)?;
  7633. -
  7634. - // INVARIANT: The `CStr` and `CString` types have the same invariants for
  7635. - // the string data, and we copied it over without changes.
  7636. - Ok(CString { buf })
  7637. - }
  7638. -}
  7639. -
  7640. -impl fmt::Debug for CString {
  7641. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  7642. - fmt::Debug::fmt(&**self, f)
  7643. - }
  7644. -}
  7645. -
  7646. -/// A convenience alias for [`core::format_args`].
  7647. -#[macro_export]
  7648. -macro_rules! fmt {
  7649. - ($($f:tt)*) => ( core::format_args!($($f)*) )
  7650. -}
  7651. diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
  7652. deleted file mode 100644
  7653. --- a/rust/kernel/sync.rs
  7654. +++ /dev/null
  7655. @@ -1,67 +0,0 @@
  7656. -// SPDX-License-Identifier: GPL-2.0
  7657. -
  7658. -//! Synchronisation primitives.
  7659. -//!
  7660. -//! This module contains the kernel APIs related to synchronisation that have been ported or
  7661. -//! wrapped for usage by Rust code in the kernel.
  7662. -
  7663. -use crate::types::Opaque;
  7664. -
  7665. -mod arc;
  7666. -mod condvar;
  7667. -pub mod lock;
  7668. -mod locked_by;
  7669. -
  7670. -pub use arc::{Arc, ArcBorrow, UniqueArc};
  7671. -pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
  7672. -pub use lock::mutex::{new_mutex, Mutex};
  7673. -pub use lock::spinlock::{new_spinlock, SpinLock};
  7674. -pub use locked_by::LockedBy;
  7675. -
  7676. -/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
  7677. -#[repr(transparent)]
  7678. -pub struct LockClassKey(Opaque<bindings::lock_class_key>);
  7679. -
  7680. -// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
  7681. -// provides its own synchronization.
  7682. -unsafe impl Sync for LockClassKey {}
  7683. -
  7684. -impl LockClassKey {
  7685. - /// Creates a new lock class key.
  7686. - pub const fn new() -> Self {
  7687. - Self(Opaque::uninit())
  7688. - }
  7689. -
  7690. - pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
  7691. - self.0.get()
  7692. - }
  7693. -}
  7694. -
  7695. -impl Default for LockClassKey {
  7696. - fn default() -> Self {
  7697. - Self::new()
  7698. - }
  7699. -}
  7700. -
  7701. -/// Defines a new static lock class and returns a pointer to it.
  7702. -#[doc(hidden)]
  7703. -#[macro_export]
  7704. -macro_rules! static_lock_class {
  7705. - () => {{
  7706. - static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new();
  7707. - &CLASS
  7708. - }};
  7709. -}
  7710. -
  7711. -/// Returns the given string, if one is provided, otherwise generates one based on the source code
  7712. -/// location.
  7713. -#[doc(hidden)]
  7714. -#[macro_export]
  7715. -macro_rules! optional_name {
  7716. - () => {
  7717. - $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
  7718. - };
  7719. - ($name:literal) => {
  7720. - $crate::c_str!($name)
  7721. - };
  7722. -}
  7723. diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
  7724. deleted file mode 100644
  7725. --- a/rust/kernel/sync/arc.rs
  7726. +++ /dev/null
  7727. @@ -1,782 +0,0 @@
  7728. -// SPDX-License-Identifier: GPL-2.0
  7729. -
  7730. -//! A reference-counted pointer.
  7731. -//!
  7732. -//! This module implements a way for users to create reference-counted objects and pointers to
  7733. -//! them. Such a pointer automatically increments and decrements the count, and drops the
  7734. -//! underlying object when it reaches zero. It is also safe to use concurrently from multiple
  7735. -//! threads.
  7736. -//!
  7737. -//! It is different from the standard library's [`Arc`] in a few ways:
  7738. -//! 1. It is backed by the kernel's `refcount_t` type.
  7739. -//! 2. It does not support weak references, which allows it to be half the size.
  7740. -//! 3. It saturates the reference count instead of aborting when it goes over a threshold.
  7741. -//! 4. It does not provide a `get_mut` method, so the ref counted object is pinned.
  7742. -//!
  7743. -//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html
  7744. -
  7745. -use crate::{
  7746. - alloc::{box_ext::BoxExt, AllocError, Flags},
  7747. - error::{self, Error},
  7748. - init::{self, InPlaceInit, Init, PinInit},
  7749. - try_init,
  7750. - types::{ForeignOwnable, Opaque},
  7751. -};
  7752. -use alloc::boxed::Box;
  7753. -use core::{
  7754. - alloc::Layout,
  7755. - fmt,
  7756. - marker::{PhantomData, Unsize},
  7757. - mem::{ManuallyDrop, MaybeUninit},
  7758. - ops::{Deref, DerefMut},
  7759. - pin::Pin,
  7760. - ptr::NonNull,
  7761. -};
  7762. -use macros::pin_data;
  7763. -
  7764. -mod std_vendor;
  7765. -
  7766. -/// A reference-counted pointer to an instance of `T`.
  7767. -///
  7768. -/// The reference count is incremented when new instances of [`Arc`] are created, and decremented
  7769. -/// when they are dropped. When the count reaches zero, the underlying `T` is also dropped.
  7770. -///
  7771. -/// # Invariants
  7772. -///
  7773. -/// The reference count on an instance of [`Arc`] is always non-zero.
  7774. -/// The object pointed to by [`Arc`] is always pinned.
  7775. -///
  7776. -/// # Examples
  7777. -///
  7778. -/// ```
  7779. -/// use kernel::sync::Arc;
  7780. -///
  7781. -/// struct Example {
  7782. -/// a: u32,
  7783. -/// b: u32,
  7784. -/// }
  7785. -///
  7786. -/// // Create a refcounted instance of `Example`.
  7787. -/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
  7788. -///
  7789. -/// // Get a new pointer to `obj` and increment the refcount.
  7790. -/// let cloned = obj.clone();
  7791. -///
  7792. -/// // Assert that both `obj` and `cloned` point to the same underlying object.
  7793. -/// assert!(core::ptr::eq(&*obj, &*cloned));
  7794. -///
  7795. -/// // Destroy `obj` and decrement its refcount.
  7796. -/// drop(obj);
  7797. -///
  7798. -/// // Check that the values are still accessible through `cloned`.
  7799. -/// assert_eq!(cloned.a, 10);
  7800. -/// assert_eq!(cloned.b, 20);
  7801. -///
  7802. -/// // The refcount drops to zero when `cloned` goes out of scope, and the memory is freed.
  7803. -/// # Ok::<(), Error>(())
  7804. -/// ```
  7805. -///
  7806. -/// Using `Arc<T>` as the type of `self`:
  7807. -///
  7808. -/// ```
  7809. -/// use kernel::sync::Arc;
  7810. -///
  7811. -/// struct Example {
  7812. -/// a: u32,
  7813. -/// b: u32,
  7814. -/// }
  7815. -///
  7816. -/// impl Example {
  7817. -/// fn take_over(self: Arc<Self>) {
  7818. -/// // ...
  7819. -/// }
  7820. -///
  7821. -/// fn use_reference(self: &Arc<Self>) {
  7822. -/// // ...
  7823. -/// }
  7824. -/// }
  7825. -///
  7826. -/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
  7827. -/// obj.use_reference();
  7828. -/// obj.take_over();
  7829. -/// # Ok::<(), Error>(())
  7830. -/// ```
  7831. -///
  7832. -/// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`:
  7833. -///
  7834. -/// ```
  7835. -/// use kernel::sync::{Arc, ArcBorrow};
  7836. -///
  7837. -/// trait MyTrait {
  7838. -/// // Trait has a function whose `self` type is `Arc<Self>`.
  7839. -/// fn example1(self: Arc<Self>) {}
  7840. -///
  7841. -/// // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`.
  7842. -/// fn example2(self: ArcBorrow<'_, Self>) {}
  7843. -/// }
  7844. -///
  7845. -/// struct Example;
  7846. -/// impl MyTrait for Example {}
  7847. -///
  7848. -/// // `obj` has type `Arc<Example>`.
  7849. -/// let obj: Arc<Example> = Arc::new(Example, GFP_KERNEL)?;
  7850. -///
  7851. -/// // `coerced` has type `Arc<dyn MyTrait>`.
  7852. -/// let coerced: Arc<dyn MyTrait> = obj;
  7853. -/// # Ok::<(), Error>(())
  7854. -/// ```
  7855. -pub struct Arc<T: ?Sized> {
  7856. - ptr: NonNull<ArcInner<T>>,
  7857. - _p: PhantomData<ArcInner<T>>,
  7858. -}
  7859. -
  7860. -#[pin_data]
  7861. -#[repr(C)]
  7862. -struct ArcInner<T: ?Sized> {
  7863. - refcount: Opaque<bindings::refcount_t>,
  7864. - data: T,
  7865. -}
  7866. -
  7867. -impl<T: ?Sized> ArcInner<T> {
  7868. - /// Converts a pointer to the contents of an [`Arc`] into a pointer to the [`ArcInner`].
  7869. - ///
  7870. - /// # Safety
  7871. - ///
  7872. - /// `ptr` must have been returned by a previous call to [`Arc::into_raw`], and the `Arc` must
  7873. - /// not yet have been destroyed.
  7874. - unsafe fn container_of(ptr: *const T) -> NonNull<ArcInner<T>> {
  7875. - let refcount_layout = Layout::new::<bindings::refcount_t>();
  7876. - // SAFETY: The caller guarantees that the pointer is valid.
  7877. - let val_layout = Layout::for_value(unsafe { &*ptr });
  7878. - // SAFETY: We're computing the layout of a real struct that existed when compiling this
  7879. - // binary, so its layout is not so large that it can trigger arithmetic overflow.
  7880. - let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 };
  7881. -
  7882. - // Pointer casts leave the metadata unchanged. This is okay because the metadata of `T` and
  7883. - // `ArcInner<T>` is the same since `ArcInner` is a struct with `T` as its last field.
  7884. - //
  7885. - // This is documented at:
  7886. - // <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
  7887. - let ptr = ptr as *const ArcInner<T>;
  7888. -
  7889. - // SAFETY: The pointer is in-bounds of an allocation both before and after offsetting the
  7890. - // pointer, since it originates from a previous call to `Arc::into_raw` on an `Arc` that is
  7891. - // still valid.
  7892. - let ptr = unsafe { ptr.byte_sub(val_offset) };
  7893. -
  7894. - // SAFETY: The pointer can't be null since you can't have an `ArcInner<T>` value at the null
  7895. - // address.
  7896. - unsafe { NonNull::new_unchecked(ptr.cast_mut()) }
  7897. - }
  7898. -}
  7899. -
  7900. -// This is to allow [`Arc`] (and variants) to be used as the type of `self`.
  7901. -impl<T: ?Sized> core::ops::Receiver for Arc<T> {}
  7902. -
  7903. -// This is to allow coercion from `Arc<T>` to `Arc<U>` if `T` can be converted to the
  7904. -// dynamically-sized type (DST) `U`.
  7905. -impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Arc<U>> for Arc<T> {}
  7906. -
  7907. -// This is to allow `Arc<U>` to be dispatched on when `Arc<T>` can be coerced into `Arc<U>`.
  7908. -impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Arc<U>> for Arc<T> {}
  7909. -
  7910. -// SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
  7911. -// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
  7912. -// `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` using a
  7913. -// mutable reference when the reference count reaches zero and `T` is dropped.
  7914. -unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
  7915. -
  7916. -// SAFETY: It is safe to send `&Arc<T>` to another thread when the underlying `T` is `Sync`
  7917. -// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
  7918. -// it needs `T` to be `Send` because any thread that has a `&Arc<T>` may clone it and get an
  7919. -// `Arc<T>` on that thread, so the thread may ultimately access `T` using a mutable reference when
  7920. -// the reference count reaches zero and `T` is dropped.
  7921. -unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
  7922. -
  7923. -impl<T> Arc<T> {
  7924. - /// Constructs a new reference counted instance of `T`.
  7925. - pub fn new(contents: T, flags: Flags) -> Result<Self, AllocError> {
  7926. - // INVARIANT: The refcount is initialised to a non-zero value.
  7927. - let value = ArcInner {
  7928. - // SAFETY: There are no safety requirements for this FFI call.
  7929. - refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
  7930. - data: contents,
  7931. - };
  7932. -
  7933. - let inner = <Box<_> as BoxExt<_>>::new(value, flags)?;
  7934. -
  7935. - // SAFETY: We just created `inner` with a reference count of 1, which is owned by the new
  7936. - // `Arc` object.
  7937. - Ok(unsafe { Self::from_inner(Box::leak(inner).into()) })
  7938. - }
  7939. -
  7940. - /// Use the given initializer to in-place initialize a `T`.
  7941. - ///
  7942. - /// If `T: !Unpin` it will not be able to move afterwards.
  7943. - #[inline]
  7944. - pub fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> error::Result<Self>
  7945. - where
  7946. - Error: From<E>,
  7947. - {
  7948. - UniqueArc::pin_init(init, flags).map(|u| u.into())
  7949. - }
  7950. -
  7951. - /// Use the given initializer to in-place initialize a `T`.
  7952. - ///
  7953. - /// This is equivalent to [`Arc<T>::pin_init`], since an [`Arc`] is always pinned.
  7954. - #[inline]
  7955. - pub fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
  7956. - where
  7957. - Error: From<E>,
  7958. - {
  7959. - UniqueArc::init(init, flags).map(|u| u.into())
  7960. - }
  7961. -}
  7962. -
  7963. -impl<T: ?Sized> Arc<T> {
  7964. - /// Constructs a new [`Arc`] from an existing [`ArcInner`].
  7965. - ///
  7966. - /// # Safety
  7967. - ///
  7968. - /// The caller must ensure that `inner` points to a valid location and has a non-zero reference
  7969. - /// count, one of which will be owned by the new [`Arc`] instance.
  7970. - unsafe fn from_inner(inner: NonNull<ArcInner<T>>) -> Self {
  7971. - // INVARIANT: By the safety requirements, the invariants hold.
  7972. - Arc {
  7973. - ptr: inner,
  7974. - _p: PhantomData,
  7975. - }
  7976. - }
  7977. -
  7978. - /// Convert the [`Arc`] into a raw pointer.
  7979. - ///
  7980. - /// The raw pointer has ownership of the refcount that this Arc object owned.
  7981. - pub fn into_raw(self) -> *const T {
  7982. - let ptr = self.ptr.as_ptr();
  7983. - core::mem::forget(self);
  7984. - // SAFETY: The pointer is valid.
  7985. - unsafe { core::ptr::addr_of!((*ptr).data) }
  7986. - }
  7987. -
  7988. - /// Recreates an [`Arc`] instance previously deconstructed via [`Arc::into_raw`].
  7989. - ///
  7990. - /// # Safety
  7991. - ///
  7992. - /// `ptr` must have been returned by a previous call to [`Arc::into_raw`]. Additionally, it
  7993. - /// must not be called more than once for each previous call to [`Arc::into_raw`].
  7994. - pub unsafe fn from_raw(ptr: *const T) -> Self {
  7995. - // SAFETY: The caller promises that this pointer originates from a call to `into_raw` on an
  7996. - // `Arc` that is still valid.
  7997. - let ptr = unsafe { ArcInner::container_of(ptr) };
  7998. -
  7999. - // SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
  8000. - // reference count held then will be owned by the new `Arc` object.
  8001. - unsafe { Self::from_inner(ptr) }
  8002. - }
  8003. -
  8004. - /// Returns an [`ArcBorrow`] from the given [`Arc`].
  8005. - ///
  8006. - /// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method
  8007. - /// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised.
  8008. - #[inline]
  8009. - pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
  8010. - // SAFETY: The constraint that the lifetime of the shared reference must outlive that of
  8011. - // the returned `ArcBorrow` ensures that the object remains alive and that no mutable
  8012. - // reference can be created.
  8013. - unsafe { ArcBorrow::new(self.ptr) }
  8014. - }
  8015. -
  8016. - /// Compare whether two [`Arc`] pointers reference the same underlying object.
  8017. - pub fn ptr_eq(this: &Self, other: &Self) -> bool {
  8018. - core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
  8019. - }
  8020. -
  8021. - /// Converts this [`Arc`] into a [`UniqueArc`], or destroys it if it is not unique.
  8022. - ///
  8023. - /// When this destroys the `Arc`, it does so while properly avoiding races. This means that
  8024. - /// this method will never call the destructor of the value.
  8025. - ///
  8026. - /// # Examples
  8027. - ///
  8028. - /// ```
  8029. - /// use kernel::sync::{Arc, UniqueArc};
  8030. - ///
  8031. - /// let arc = Arc::new(42, GFP_KERNEL)?;
  8032. - /// let unique_arc = arc.into_unique_or_drop();
  8033. - ///
  8034. - /// // The above conversion should succeed since refcount of `arc` is 1.
  8035. - /// assert!(unique_arc.is_some());
  8036. - ///
  8037. - /// assert_eq!(*(unique_arc.unwrap()), 42);
  8038. - ///
  8039. - /// # Ok::<(), Error>(())
  8040. - /// ```
  8041. - ///
  8042. - /// ```
  8043. - /// use kernel::sync::{Arc, UniqueArc};
  8044. - ///
  8045. - /// let arc = Arc::new(42, GFP_KERNEL)?;
  8046. - /// let another = arc.clone();
  8047. - ///
  8048. - /// let unique_arc = arc.into_unique_or_drop();
  8049. - ///
  8050. - /// // The above conversion should fail since refcount of `arc` is >1.
  8051. - /// assert!(unique_arc.is_none());
  8052. - ///
  8053. - /// # Ok::<(), Error>(())
  8054. - /// ```
  8055. - pub fn into_unique_or_drop(self) -> Option<Pin<UniqueArc<T>>> {
  8056. - // We will manually manage the refcount in this method, so we disable the destructor.
  8057. - let me = ManuallyDrop::new(self);
  8058. - // SAFETY: We own a refcount, so the pointer is still valid.
  8059. - let refcount = unsafe { me.ptr.as_ref() }.refcount.get();
  8060. -
  8061. - // If the refcount reaches a non-zero value, then we have destroyed this `Arc` and will
  8062. - // return without further touching the `Arc`. If the refcount reaches zero, then there are
  8063. - // no other arcs, and we can create a `UniqueArc`.
  8064. - //
  8065. - // SAFETY: We own a refcount, so the pointer is not dangling.
  8066. - let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };
  8067. - if is_zero {
  8068. - // SAFETY: We have exclusive access to the arc, so we can perform unsynchronized
  8069. - // accesses to the refcount.
  8070. - unsafe { core::ptr::write(refcount, bindings::REFCOUNT_INIT(1)) };
  8071. -
  8072. - // INVARIANT: We own the only refcount to this arc, so we may create a `UniqueArc`. We
  8073. - // must pin the `UniqueArc` because the values was previously in an `Arc`, and they pin
  8074. - // their values.
  8075. - Some(Pin::from(UniqueArc {
  8076. - inner: ManuallyDrop::into_inner(me),
  8077. - }))
  8078. - } else {
  8079. - None
  8080. - }
  8081. - }
  8082. -}
  8083. -
  8084. -impl<T: 'static> ForeignOwnable for Arc<T> {
  8085. - type Borrowed<'a> = ArcBorrow<'a, T>;
  8086. -
  8087. - fn into_foreign(self) -> *const core::ffi::c_void {
  8088. - ManuallyDrop::new(self).ptr.as_ptr() as _
  8089. - }
  8090. -
  8091. - unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
  8092. - // SAFETY: By the safety requirement of this function, we know that `ptr` came from
  8093. - // a previous call to `Arc::into_foreign`.
  8094. - let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
  8095. -
  8096. - // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
  8097. - // for the lifetime of the returned value.
  8098. - unsafe { ArcBorrow::new(inner) }
  8099. - }
  8100. -
  8101. - unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
  8102. - // SAFETY: By the safety requirement of this function, we know that `ptr` came from
  8103. - // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
  8104. - // holds a reference count increment that is transferrable to us.
  8105. - unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) }
  8106. - }
  8107. -}
  8108. -
  8109. -impl<T: ?Sized> Deref for Arc<T> {
  8110. - type Target = T;
  8111. -
  8112. - fn deref(&self) -> &Self::Target {
  8113. - // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
  8114. - // safe to dereference it.
  8115. - unsafe { &self.ptr.as_ref().data }
  8116. - }
  8117. -}
  8118. -
  8119. -impl<T: ?Sized> AsRef<T> for Arc<T> {
  8120. - fn as_ref(&self) -> &T {
  8121. - self.deref()
  8122. - }
  8123. -}
  8124. -
  8125. -impl<T: ?Sized> Clone for Arc<T> {
  8126. - fn clone(&self) -> Self {
  8127. - // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
  8128. - // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
  8129. - // safe to increment the refcount.
  8130. - unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) };
  8131. -
  8132. - // SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`.
  8133. - unsafe { Self::from_inner(self.ptr) }
  8134. - }
  8135. -}
  8136. -
  8137. -impl<T: ?Sized> Drop for Arc<T> {
  8138. - fn drop(&mut self) {
  8139. - // SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot
  8140. - // touch `refcount` after it's decremented to a non-zero value because another thread/CPU
  8141. - // may concurrently decrement it to zero and free it. It is ok to have a raw pointer to
  8142. - // freed/invalid memory as long as it is never dereferenced.
  8143. - let refcount = unsafe { self.ptr.as_ref() }.refcount.get();
  8144. -
  8145. - // INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and
  8146. - // this instance is being dropped, so the broken invariant is not observable.
  8147. - // SAFETY: Also by the type invariant, we are allowed to decrement the refcount.
  8148. - let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };
  8149. - if is_zero {
  8150. - // The count reached zero, we must free the memory.
  8151. - //
  8152. - // SAFETY: The pointer was initialised from the result of `Box::leak`.
  8153. - unsafe { drop(Box::from_raw(self.ptr.as_ptr())) };
  8154. - }
  8155. - }
  8156. -}
  8157. -
  8158. -impl<T: ?Sized> From<UniqueArc<T>> for Arc<T> {
  8159. - fn from(item: UniqueArc<T>) -> Self {
  8160. - item.inner
  8161. - }
  8162. -}
  8163. -
  8164. -impl<T: ?Sized> From<Pin<UniqueArc<T>>> for Arc<T> {
  8165. - fn from(item: Pin<UniqueArc<T>>) -> Self {
  8166. - // SAFETY: The type invariants of `Arc` guarantee that the data is pinned.
  8167. - unsafe { Pin::into_inner_unchecked(item).inner }
  8168. - }
  8169. -}
  8170. -
  8171. -/// A borrowed reference to an [`Arc`] instance.
  8172. -///
  8173. -/// For cases when one doesn't ever need to increment the refcount on the allocation, it is simpler
  8174. -/// to use just `&T`, which we can trivially get from an [`Arc<T>`] instance.
  8175. -///
  8176. -/// However, when one may need to increment the refcount, it is preferable to use an `ArcBorrow<T>`
  8177. -/// over `&Arc<T>` because the latter results in a double-indirection: a pointer (shared reference)
  8178. -/// to a pointer ([`Arc<T>`]) to the object (`T`). An [`ArcBorrow`] eliminates this double
  8179. -/// indirection while still allowing one to increment the refcount and getting an [`Arc<T>`] when/if
  8180. -/// needed.
  8181. -///
  8182. -/// # Invariants
  8183. -///
  8184. -/// There are no mutable references to the underlying [`Arc`], and it remains valid for the
  8185. -/// lifetime of the [`ArcBorrow`] instance.
  8186. -///
  8187. -/// # Example
  8188. -///
  8189. -/// ```
  8190. -/// use kernel::sync::{Arc, ArcBorrow};
  8191. -///
  8192. -/// struct Example;
  8193. -///
  8194. -/// fn do_something(e: ArcBorrow<'_, Example>) -> Arc<Example> {
  8195. -/// e.into()
  8196. -/// }
  8197. -///
  8198. -/// let obj = Arc::new(Example, GFP_KERNEL)?;
  8199. -/// let cloned = do_something(obj.as_arc_borrow());
  8200. -///
  8201. -/// // Assert that both `obj` and `cloned` point to the same underlying object.
  8202. -/// assert!(core::ptr::eq(&*obj, &*cloned));
  8203. -/// # Ok::<(), Error>(())
  8204. -/// ```
  8205. -///
  8206. -/// Using `ArcBorrow<T>` as the type of `self`:
  8207. -///
  8208. -/// ```
  8209. -/// use kernel::sync::{Arc, ArcBorrow};
  8210. -///
  8211. -/// struct Example {
  8212. -/// a: u32,
  8213. -/// b: u32,
  8214. -/// }
  8215. -///
  8216. -/// impl Example {
  8217. -/// fn use_reference(self: ArcBorrow<'_, Self>) {
  8218. -/// // ...
  8219. -/// }
  8220. -/// }
  8221. -///
  8222. -/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
  8223. -/// obj.as_arc_borrow().use_reference();
  8224. -/// # Ok::<(), Error>(())
  8225. -/// ```
  8226. -pub struct ArcBorrow<'a, T: ?Sized + 'a> {
  8227. - inner: NonNull<ArcInner<T>>,
  8228. - _p: PhantomData<&'a ()>,
  8229. -}
  8230. -
  8231. -// This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`.
  8232. -impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
  8233. -
  8234. -// This is to allow `ArcBorrow<U>` to be dispatched on when `ArcBorrow<T>` can be coerced into
  8235. -// `ArcBorrow<U>`.
  8236. -impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<ArcBorrow<'_, U>>
  8237. - for ArcBorrow<'_, T>
  8238. -{
  8239. -}
  8240. -
  8241. -impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
  8242. - fn clone(&self) -> Self {
  8243. - *self
  8244. - }
  8245. -}
  8246. -
  8247. -impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}
  8248. -
  8249. -impl<T: ?Sized> ArcBorrow<'_, T> {
  8250. - /// Creates a new [`ArcBorrow`] instance.
  8251. - ///
  8252. - /// # Safety
  8253. - ///
  8254. - /// Callers must ensure the following for the lifetime of the returned [`ArcBorrow`] instance:
  8255. - /// 1. That `inner` remains valid;
  8256. - /// 2. That no mutable references to `inner` are created.
  8257. - unsafe fn new(inner: NonNull<ArcInner<T>>) -> Self {
  8258. - // INVARIANT: The safety requirements guarantee the invariants.
  8259. - Self {
  8260. - inner,
  8261. - _p: PhantomData,
  8262. - }
  8263. - }
  8264. -
  8265. - /// Creates an [`ArcBorrow`] to an [`Arc`] that has previously been deconstructed with
  8266. - /// [`Arc::into_raw`].
  8267. - ///
  8268. - /// # Safety
  8269. - ///
  8270. - /// * The provided pointer must originate from a call to [`Arc::into_raw`].
  8271. - /// * For the duration of the lifetime annotated on this `ArcBorrow`, the reference count must
  8272. - /// not hit zero.
  8273. - /// * For the duration of the lifetime annotated on this `ArcBorrow`, there must not be a
  8274. - /// [`UniqueArc`] reference to this value.
  8275. - pub unsafe fn from_raw(ptr: *const T) -> Self {
  8276. - // SAFETY: The caller promises that this pointer originates from a call to `into_raw` on an
  8277. - // `Arc` that is still valid.
  8278. - let ptr = unsafe { ArcInner::container_of(ptr) };
  8279. -
  8280. - // SAFETY: The caller promises that the value remains valid since the reference count must
  8281. - // not hit zero, and no mutable reference will be created since that would involve a
  8282. - // `UniqueArc`.
  8283. - unsafe { Self::new(ptr) }
  8284. - }
  8285. -}
  8286. -
  8287. -impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T> {
  8288. - fn from(b: ArcBorrow<'_, T>) -> Self {
  8289. - // SAFETY: The existence of `b` guarantees that the refcount is non-zero. `ManuallyDrop`
  8290. - // guarantees that `drop` isn't called, so it's ok that the temporary `Arc` doesn't own the
  8291. - // increment.
  8292. - ManuallyDrop::new(unsafe { Arc::from_inner(b.inner) })
  8293. - .deref()
  8294. - .clone()
  8295. - }
  8296. -}
  8297. -
  8298. -impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
  8299. - type Target = T;
  8300. -
  8301. - fn deref(&self) -> &Self::Target {
  8302. - // SAFETY: By the type invariant, the underlying object is still alive with no mutable
  8303. - // references to it, so it is safe to create a shared reference.
  8304. - unsafe { &self.inner.as_ref().data }
  8305. - }
  8306. -}
  8307. -
  8308. -/// A refcounted object that is known to have a refcount of 1.
  8309. -///
  8310. -/// It is mutable and can be converted to an [`Arc`] so that it can be shared.
  8311. -///
  8312. -/// # Invariants
  8313. -///
  8314. -/// `inner` always has a reference count of 1.
  8315. -///
  8316. -/// # Examples
  8317. -///
  8318. -/// In the following example, we make changes to the inner object before turning it into an
  8319. -/// `Arc<Test>` object (after which point, it cannot be mutated directly). Note that `x.into()`
  8320. -/// cannot fail.
  8321. -///
  8322. -/// ```
  8323. -/// use kernel::sync::{Arc, UniqueArc};
  8324. -///
  8325. -/// struct Example {
  8326. -/// a: u32,
  8327. -/// b: u32,
  8328. -/// }
  8329. -///
  8330. -/// fn test() -> Result<Arc<Example>> {
  8331. -/// let mut x = UniqueArc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
  8332. -/// x.a += 1;
  8333. -/// x.b += 1;
  8334. -/// Ok(x.into())
  8335. -/// }
  8336. -///
  8337. -/// # test().unwrap();
  8338. -/// ```
  8339. -///
  8340. -/// In the following example we first allocate memory for a refcounted `Example` but we don't
  8341. -/// initialise it on allocation. We do initialise it later with a call to [`UniqueArc::write`],
  8342. -/// followed by a conversion to `Arc<Example>`. This is particularly useful when allocation happens
  8343. -/// in one context (e.g., sleepable) and initialisation in another (e.g., atomic):
  8344. -///
  8345. -/// ```
  8346. -/// use kernel::sync::{Arc, UniqueArc};
  8347. -///
  8348. -/// struct Example {
  8349. -/// a: u32,
  8350. -/// b: u32,
  8351. -/// }
  8352. -///
  8353. -/// fn test() -> Result<Arc<Example>> {
  8354. -/// let x = UniqueArc::new_uninit(GFP_KERNEL)?;
  8355. -/// Ok(x.write(Example { a: 10, b: 20 }).into())
  8356. -/// }
  8357. -///
  8358. -/// # test().unwrap();
  8359. -/// ```
  8360. -///
  8361. -/// In the last example below, the caller gets a pinned instance of `Example` while converting to
  8362. -/// `Arc<Example>`; this is useful in scenarios where one needs a pinned reference during
  8363. -/// initialisation, for example, when initialising fields that are wrapped in locks.
  8364. -///
  8365. -/// ```
  8366. -/// use kernel::sync::{Arc, UniqueArc};
  8367. -///
  8368. -/// struct Example {
  8369. -/// a: u32,
  8370. -/// b: u32,
  8371. -/// }
  8372. -///
  8373. -/// fn test() -> Result<Arc<Example>> {
  8374. -/// let mut pinned = Pin::from(UniqueArc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?);
  8375. -/// // We can modify `pinned` because it is `Unpin`.
  8376. -/// pinned.as_mut().a += 1;
  8377. -/// Ok(pinned.into())
  8378. -/// }
  8379. -///
  8380. -/// # test().unwrap();
  8381. -/// ```
  8382. -pub struct UniqueArc<T: ?Sized> {
  8383. - inner: Arc<T>,
  8384. -}
  8385. -
  8386. -impl<T> UniqueArc<T> {
  8387. - /// Tries to allocate a new [`UniqueArc`] instance.
  8388. - pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> {
  8389. - Ok(Self {
  8390. - // INVARIANT: The newly-created object has a refcount of 1.
  8391. - inner: Arc::new(value, flags)?,
  8392. - })
  8393. - }
  8394. -
  8395. - /// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet.
  8396. - pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
  8397. - // INVARIANT: The refcount is initialised to a non-zero value.
  8398. - let inner = Box::try_init::<AllocError>(
  8399. - try_init!(ArcInner {
  8400. - // SAFETY: There are no safety requirements for this FFI call.
  8401. - refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
  8402. - data <- init::uninit::<T, AllocError>(),
  8403. - }? AllocError),
  8404. - flags,
  8405. - )?;
  8406. - Ok(UniqueArc {
  8407. - // INVARIANT: The newly-created object has a refcount of 1.
  8408. - // SAFETY: The pointer from the `Box` is valid.
  8409. - inner: unsafe { Arc::from_inner(Box::leak(inner).into()) },
  8410. - })
  8411. - }
  8412. -}
  8413. -
  8414. -impl<T> UniqueArc<MaybeUninit<T>> {
  8415. - /// Converts a `UniqueArc<MaybeUninit<T>>` into a `UniqueArc<T>` by writing a value into it.
  8416. - pub fn write(mut self, value: T) -> UniqueArc<T> {
  8417. - self.deref_mut().write(value);
  8418. - // SAFETY: We just wrote the value to be initialized.
  8419. - unsafe { self.assume_init() }
  8420. - }
  8421. -
  8422. - /// Unsafely assume that `self` is initialized.
  8423. - ///
  8424. - /// # Safety
  8425. - ///
  8426. - /// The caller guarantees that the value behind this pointer has been initialized. It is
  8427. - /// *immediate* UB to call this when the value is not initialized.
  8428. - pub unsafe fn assume_init(self) -> UniqueArc<T> {
  8429. - let inner = ManuallyDrop::new(self).inner.ptr;
  8430. - UniqueArc {
  8431. - // SAFETY: The new `Arc` is taking over `ptr` from `self.inner` (which won't be
  8432. - // dropped). The types are compatible because `MaybeUninit<T>` is compatible with `T`.
  8433. - inner: unsafe { Arc::from_inner(inner.cast()) },
  8434. - }
  8435. - }
  8436. -
  8437. - /// Initialize `self` using the given initializer.
  8438. - pub fn init_with<E>(mut self, init: impl Init<T, E>) -> core::result::Result<UniqueArc<T>, E> {
  8439. - // SAFETY: The supplied pointer is valid for initialization.
  8440. - match unsafe { init.__init(self.as_mut_ptr()) } {
  8441. - // SAFETY: Initialization completed successfully.
  8442. - Ok(()) => Ok(unsafe { self.assume_init() }),
  8443. - Err(err) => Err(err),
  8444. - }
  8445. - }
  8446. -
  8447. - /// Pin-initialize `self` using the given pin-initializer.
  8448. - pub fn pin_init_with<E>(
  8449. - mut self,
  8450. - init: impl PinInit<T, E>,
  8451. - ) -> core::result::Result<Pin<UniqueArc<T>>, E> {
  8452. - // SAFETY: The supplied pointer is valid for initialization and we will later pin the value
  8453. - // to ensure it does not move.
  8454. - match unsafe { init.__pinned_init(self.as_mut_ptr()) } {
  8455. - // SAFETY: Initialization completed successfully.
  8456. - Ok(()) => Ok(unsafe { self.assume_init() }.into()),
  8457. - Err(err) => Err(err),
  8458. - }
  8459. - }
  8460. -}
  8461. -
  8462. -impl<T: ?Sized> From<UniqueArc<T>> for Pin<UniqueArc<T>> {
  8463. - fn from(obj: UniqueArc<T>) -> Self {
  8464. - // SAFETY: It is not possible to move/replace `T` inside a `Pin<UniqueArc<T>>` (unless `T`
  8465. - // is `Unpin`), so it is ok to convert it to `Pin<UniqueArc<T>>`.
  8466. - unsafe { Pin::new_unchecked(obj) }
  8467. - }
  8468. -}
  8469. -
  8470. -impl<T: ?Sized> Deref for UniqueArc<T> {
  8471. - type Target = T;
  8472. -
  8473. - fn deref(&self) -> &Self::Target {
  8474. - self.inner.deref()
  8475. - }
  8476. -}
  8477. -
  8478. -impl<T: ?Sized> DerefMut for UniqueArc<T> {
  8479. - fn deref_mut(&mut self) -> &mut Self::Target {
  8480. - // SAFETY: By the `Arc` type invariant, there is necessarily a reference to the object, so
  8481. - // it is safe to dereference it. Additionally, we know there is only one reference when
  8482. - // it's inside a `UniqueArc`, so it is safe to get a mutable reference.
  8483. - unsafe { &mut self.inner.ptr.as_mut().data }
  8484. - }
  8485. -}
  8486. -
  8487. -impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
  8488. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  8489. - fmt::Display::fmt(self.deref(), f)
  8490. - }
  8491. -}
  8492. -
  8493. -impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
  8494. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  8495. - fmt::Display::fmt(self.deref(), f)
  8496. - }
  8497. -}
  8498. -
  8499. -impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
  8500. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  8501. - fmt::Debug::fmt(self.deref(), f)
  8502. - }
  8503. -}
  8504. -
  8505. -impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
  8506. - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  8507. - fmt::Debug::fmt(self.deref(), f)
  8508. - }
  8509. -}
  8510. diff --git a/rust/kernel/sync/arc/std_vendor.rs b/rust/kernel/sync/arc/std_vendor.rs
  8511. deleted file mode 100644
  8512. --- a/rust/kernel/sync/arc/std_vendor.rs
  8513. +++ /dev/null
  8514. @@ -1,28 +0,0 @@
  8515. -// SPDX-License-Identifier: Apache-2.0 OR MIT
  8516. -
  8517. -//! The contents of this file come from the Rust standard library, hosted in
  8518. -//! the <https://github.com/rust-lang/rust> repository, licensed under
  8519. -//! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details,
  8520. -//! see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>.
  8521. -
  8522. -use crate::sync::{arc::ArcInner, Arc};
  8523. -use core::any::Any;
  8524. -
  8525. -impl Arc<dyn Any + Send + Sync> {
  8526. - /// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
  8527. - pub fn downcast<T>(self) -> core::result::Result<Arc<T>, Self>
  8528. - where
  8529. - T: Any + Send + Sync,
  8530. - {
  8531. - if (*self).is::<T>() {
  8532. - // SAFETY: We have just checked that the type is correct, so we can cast the pointer.
  8533. - unsafe {
  8534. - let ptr = self.ptr.cast::<ArcInner<T>>();
  8535. - core::mem::forget(self);
  8536. - Ok(Arc::from_inner(ptr))
  8537. - }
  8538. - } else {
  8539. - Err(self)
  8540. - }
  8541. - }
  8542. -}
  8543. diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs
  8544. deleted file mode 100644
  8545. --- a/rust/kernel/sync/condvar.rs
  8546. +++ /dev/null
  8547. @@ -1,238 +0,0 @@
  8548. -// SPDX-License-Identifier: GPL-2.0
  8549. -
  8550. -//! A condition variable.
  8551. -//!
  8552. -//! This module allows Rust code to use the kernel's [`struct wait_queue_head`] as a condition
  8553. -//! variable.
  8554. -
  8555. -use super::{lock::Backend, lock::Guard, LockClassKey};
  8556. -use crate::{
  8557. - init::PinInit,
  8558. - pin_init,
  8559. - str::CStr,
  8560. - task::{MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE, TASK_NORMAL, TASK_UNINTERRUPTIBLE},
  8561. - time::Jiffies,
  8562. - types::Opaque,
  8563. -};
  8564. -use core::ffi::{c_int, c_long};
  8565. -use core::marker::PhantomPinned;
  8566. -use core::ptr;
  8567. -use macros::pin_data;
  8568. -
  8569. -/// Creates a [`CondVar`] initialiser with the given name and a newly-created lock class.
  8570. -#[macro_export]
  8571. -macro_rules! new_condvar {
  8572. - ($($name:literal)?) => {
  8573. - $crate::sync::CondVar::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
  8574. - };
  8575. -}
  8576. -pub use new_condvar;
  8577. -
  8578. -/// A conditional variable.
  8579. -///
  8580. -/// Exposes the kernel's [`struct wait_queue_head`] as a condition variable. It allows the caller to
  8581. -/// atomically release the given lock and go to sleep. It reacquires the lock when it wakes up. And
  8582. -/// it wakes up when notified by another thread (via [`CondVar::notify_one`] or
  8583. -/// [`CondVar::notify_all`]) or because the thread received a signal. It may also wake up
  8584. -/// spuriously.
  8585. -///
  8586. -/// Instances of [`CondVar`] need a lock class and to be pinned. The recommended way to create such
  8587. -/// instances is with the [`pin_init`](crate::pin_init) and [`new_condvar`] macros.
  8588. -///
  8589. -/// # Examples
  8590. -///
  8591. -/// The following is an example of using a condvar with a mutex:
  8592. -///
  8593. -/// ```
  8594. -/// use kernel::sync::{new_condvar, new_mutex, CondVar, Mutex};
  8595. -///
  8596. -/// #[pin_data]
  8597. -/// pub struct Example {
  8598. -/// #[pin]
  8599. -/// value: Mutex<u32>,
  8600. -///
  8601. -/// #[pin]
  8602. -/// value_changed: CondVar,
  8603. -/// }
  8604. -///
  8605. -/// /// Waits for `e.value` to become `v`.
  8606. -/// fn wait_for_value(e: &Example, v: u32) {
  8607. -/// let mut guard = e.value.lock();
  8608. -/// while *guard != v {
  8609. -/// e.value_changed.wait(&mut guard);
  8610. -/// }
  8611. -/// }
  8612. -///
  8613. -/// /// Increments `e.value` and notifies all potential waiters.
  8614. -/// fn increment(e: &Example) {
  8615. -/// *e.value.lock() += 1;
  8616. -/// e.value_changed.notify_all();
  8617. -/// }
  8618. -///
  8619. -/// /// Allocates a new boxed `Example`.
  8620. -/// fn new_example() -> Result<Pin<Box<Example>>> {
  8621. -/// Box::pin_init(pin_init!(Example {
  8622. -/// value <- new_mutex!(0),
  8623. -/// value_changed <- new_condvar!(),
  8624. -/// }), GFP_KERNEL)
  8625. -/// }
  8626. -/// ```
  8627. -///
  8628. -/// [`struct wait_queue_head`]: srctree/include/linux/wait.h
  8629. -#[pin_data]
  8630. -pub struct CondVar {
  8631. - #[pin]
  8632. - pub(crate) wait_queue_head: Opaque<bindings::wait_queue_head>,
  8633. -
  8634. - /// A condvar needs to be pinned because it contains a [`struct list_head`] that is
  8635. - /// self-referential, so it cannot be safely moved once it is initialised.
  8636. - ///
  8637. - /// [`struct list_head`]: srctree/include/linux/types.h
  8638. - #[pin]
  8639. - _pin: PhantomPinned,
  8640. -}
  8641. -
  8642. -// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on any thread.
  8643. -#[allow(clippy::non_send_fields_in_send_ty)]
  8644. -unsafe impl Send for CondVar {}
  8645. -
  8646. -// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on multiple threads
  8647. -// concurrently.
  8648. -unsafe impl Sync for CondVar {}
  8649. -
  8650. -impl CondVar {
  8651. - /// Constructs a new condvar initialiser.
  8652. - pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> {
  8653. - pin_init!(Self {
  8654. - _pin: PhantomPinned,
  8655. - // SAFETY: `slot` is valid while the closure is called and both `name` and `key` have
  8656. - // static lifetimes so they live indefinitely.
  8657. - wait_queue_head <- Opaque::ffi_init(|slot| unsafe {
  8658. - bindings::__init_waitqueue_head(slot, name.as_char_ptr(), key.as_ptr())
  8659. - }),
  8660. - })
  8661. - }
  8662. -
  8663. - fn wait_internal<T: ?Sized, B: Backend>(
  8664. - &self,
  8665. - wait_state: c_int,
  8666. - guard: &mut Guard<'_, T, B>,
  8667. - timeout_in_jiffies: c_long,
  8668. - ) -> c_long {
  8669. - let wait = Opaque::<bindings::wait_queue_entry>::uninit();
  8670. -
  8671. - // SAFETY: `wait` points to valid memory.
  8672. - unsafe { bindings::init_wait(wait.get()) };
  8673. -
  8674. - // SAFETY: Both `wait` and `wait_queue_head` point to valid memory.
  8675. - unsafe {
  8676. - bindings::prepare_to_wait_exclusive(self.wait_queue_head.get(), wait.get(), wait_state)
  8677. - };
  8678. -
  8679. - // SAFETY: Switches to another thread. The timeout can be any number.
  8680. - let ret = guard.do_unlocked(|| unsafe { bindings::schedule_timeout(timeout_in_jiffies) });
  8681. -
  8682. - // SAFETY: Both `wait` and `wait_queue_head` point to valid memory.
  8683. - unsafe { bindings::finish_wait(self.wait_queue_head.get(), wait.get()) };
  8684. -
  8685. - ret
  8686. - }
  8687. -
  8688. - /// Releases the lock and waits for a notification in uninterruptible mode.
  8689. - ///
  8690. - /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the
  8691. - /// thread to sleep, reacquiring the lock on wake up. It wakes up when notified by
  8692. - /// [`CondVar::notify_one`] or [`CondVar::notify_all`]. Note that it may also wake up
  8693. - /// spuriously.
  8694. - pub fn wait<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>) {
  8695. - self.wait_internal(TASK_UNINTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT);
  8696. - }
  8697. -
  8698. - /// Releases the lock and waits for a notification in interruptible mode.
  8699. - ///
  8700. - /// Similar to [`CondVar::wait`], except that the wait is interruptible. That is, the thread may
  8701. - /// wake up due to signals. It may also wake up spuriously.
  8702. - ///
  8703. - /// Returns whether there is a signal pending.
  8704. - #[must_use = "wait_interruptible returns if a signal is pending, so the caller must check the return value"]
  8705. - pub fn wait_interruptible<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>) -> bool {
  8706. - self.wait_internal(TASK_INTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT);
  8707. - crate::current!().signal_pending()
  8708. - }
  8709. -
  8710. - /// Releases the lock and waits for a notification in interruptible mode.
  8711. - ///
  8712. - /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the
  8713. - /// thread to sleep. It wakes up when notified by [`CondVar::notify_one`] or
  8714. - /// [`CondVar::notify_all`], or when a timeout occurs, or when the thread receives a signal.
  8715. - #[must_use = "wait_interruptible_timeout returns if a signal is pending, so the caller must check the return value"]
  8716. - pub fn wait_interruptible_timeout<T: ?Sized, B: Backend>(
  8717. - &self,
  8718. - guard: &mut Guard<'_, T, B>,
  8719. - jiffies: Jiffies,
  8720. - ) -> CondVarTimeoutResult {
  8721. - let jiffies = jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT);
  8722. - let res = self.wait_internal(TASK_INTERRUPTIBLE, guard, jiffies);
  8723. -
  8724. - match (res as Jiffies, crate::current!().signal_pending()) {
  8725. - (jiffies, true) => CondVarTimeoutResult::Signal { jiffies },
  8726. - (0, false) => CondVarTimeoutResult::Timeout,
  8727. - (jiffies, false) => CondVarTimeoutResult::Woken { jiffies },
  8728. - }
  8729. - }
  8730. -
  8731. - /// Calls the kernel function to notify the appropriate number of threads.
  8732. - fn notify(&self, count: c_int) {
  8733. - // SAFETY: `wait_queue_head` points to valid memory.
  8734. - unsafe {
  8735. - bindings::__wake_up(
  8736. - self.wait_queue_head.get(),
  8737. - TASK_NORMAL,
  8738. - count,
  8739. - ptr::null_mut(),
  8740. - )
  8741. - };
  8742. - }
  8743. -
  8744. - /// Calls the kernel function to notify one thread synchronously.
  8745. - ///
  8746. - /// This method behaves like `notify_one`, except that it hints to the scheduler that the
  8747. - /// current thread is about to go to sleep, so it should schedule the target thread on the same
  8748. - /// CPU.
  8749. - pub fn notify_sync(&self) {
  8750. - // SAFETY: `wait_queue_head` points to valid memory.
  8751. - unsafe { bindings::__wake_up_sync(self.wait_queue_head.get(), TASK_NORMAL) };
  8752. - }
  8753. -
  8754. - /// Wakes a single waiter up, if any.
  8755. - ///
  8756. - /// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost
  8757. - /// completely (as opposed to automatically waking up the next waiter).
  8758. - pub fn notify_one(&self) {
  8759. - self.notify(1);
  8760. - }
  8761. -
  8762. - /// Wakes all waiters up, if any.
  8763. - ///
  8764. - /// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost
  8765. - /// completely (as opposed to automatically waking up the next waiter).
  8766. - pub fn notify_all(&self) {
  8767. - self.notify(0);
  8768. - }
  8769. -}
  8770. -
  8771. -/// The return type of `wait_timeout`.
  8772. -pub enum CondVarTimeoutResult {
  8773. - /// The timeout was reached.
  8774. - Timeout,
  8775. - /// Somebody woke us up.
  8776. - Woken {
  8777. - /// Remaining sleep duration.
  8778. - jiffies: Jiffies,
  8779. - },
  8780. - /// A signal occurred.
  8781. - Signal {
  8782. - /// Remaining sleep duration.
  8783. - jiffies: Jiffies,
  8784. - },
  8785. -}
  8786. diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
  8787. deleted file mode 100644
  8788. --- a/rust/kernel/sync/lock.rs
  8789. +++ /dev/null
  8790. @@ -1,197 +0,0 @@
  8791. -// SPDX-License-Identifier: GPL-2.0
  8792. -
  8793. -//! Generic kernel lock and guard.
  8794. -//!
  8795. -//! It contains a generic Rust lock and guard that allow for different backends (e.g., mutexes,
  8796. -//! spinlocks, raw spinlocks) to be provided with minimal effort.
  8797. -
  8798. -use super::LockClassKey;
  8799. -use crate::{init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard};
  8800. -use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned};
  8801. -use macros::pin_data;
  8802. -
  8803. -pub mod mutex;
  8804. -pub mod spinlock;
  8805. -
  8806. -/// The "backend" of a lock.
  8807. -///
  8808. -/// It is the actual implementation of the lock, without the need to repeat patterns used in all
  8809. -/// locks.
  8810. -///
  8811. -/// # Safety
  8812. -///
  8813. -/// - Implementers must ensure that only one thread/CPU may access the protected data once the lock
  8814. -/// is owned, that is, between calls to [`lock`] and [`unlock`].
  8815. -/// - Implementers must also ensure that [`relock`] uses the same locking method as the original
  8816. -/// lock operation.
  8817. -///
  8818. -/// [`lock`]: Backend::lock
  8819. -/// [`unlock`]: Backend::unlock
  8820. -/// [`relock`]: Backend::relock
  8821. -pub unsafe trait Backend {
  8822. - /// The state required by the lock.
  8823. - type State;
  8824. -
  8825. - /// The state required to be kept between [`lock`] and [`unlock`].
  8826. - ///
  8827. - /// [`lock`]: Backend::lock
  8828. - /// [`unlock`]: Backend::unlock
  8829. - type GuardState;
  8830. -
  8831. - /// Initialises the lock.
  8832. - ///
  8833. - /// # Safety
  8834. - ///
  8835. - /// `ptr` must be valid for write for the duration of the call, while `name` and `key` must
  8836. - /// remain valid for read indefinitely.
  8837. - unsafe fn init(
  8838. - ptr: *mut Self::State,
  8839. - name: *const core::ffi::c_char,
  8840. - key: *mut bindings::lock_class_key,
  8841. - );
  8842. -
  8843. - /// Acquires the lock, making the caller its owner.
  8844. - ///
  8845. - /// # Safety
  8846. - ///
  8847. - /// Callers must ensure that [`Backend::init`] has been previously called.
  8848. - #[must_use]
  8849. - unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState;
  8850. -
  8851. - /// Releases the lock, giving up its ownership.
  8852. - ///
  8853. - /// # Safety
  8854. - ///
  8855. - /// It must only be called by the current owner of the lock.
  8856. - unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState);
  8857. -
  8858. - /// Reacquires the lock, making the caller its owner.
  8859. - ///
  8860. - /// # Safety
  8861. - ///
  8862. - /// Callers must ensure that `guard_state` comes from a previous call to [`Backend::lock`] (or
  8863. - /// variant) that has been unlocked with [`Backend::unlock`] and will be relocked now.
  8864. - unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) {
  8865. - // SAFETY: The safety requirements ensure that the lock is initialised.
  8866. - *guard_state = unsafe { Self::lock(ptr) };
  8867. - }
  8868. -}
  8869. -
  8870. -/// A mutual exclusion primitive.
  8871. -///
  8872. -/// Exposes one of the kernel locking primitives. Which one is exposed depends on the lock
  8873. -/// [`Backend`] specified as the generic parameter `B`.
  8874. -#[pin_data]
  8875. -pub struct Lock<T: ?Sized, B: Backend> {
  8876. - /// The kernel lock object.
  8877. - #[pin]
  8878. - state: Opaque<B::State>,
  8879. -
  8880. - /// Some locks are known to be self-referential (e.g., mutexes), while others are architecture
  8881. - /// or config defined (e.g., spinlocks). So we conservatively require them to be pinned in case
  8882. - /// some architecture uses self-references now or in the future.
  8883. - #[pin]
  8884. - _pin: PhantomPinned,
  8885. -
  8886. - /// The data protected by the lock.
  8887. - pub(crate) data: UnsafeCell<T>,
  8888. -}
  8889. -
  8890. -// SAFETY: `Lock` can be transferred across thread boundaries iff the data it protects can.
  8891. -unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {}
  8892. -
  8893. -// SAFETY: `Lock` serialises the interior mutability it provides, so it is `Sync` as long as the
  8894. -// data it protects is `Send`.
  8895. -unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
  8896. -
  8897. -impl<T, B: Backend> Lock<T, B> {
  8898. - /// Constructs a new lock initialiser.
  8899. - pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> {
  8900. - pin_init!(Self {
  8901. - data: UnsafeCell::new(t),
  8902. - _pin: PhantomPinned,
  8903. - // SAFETY: `slot` is valid while the closure is called and both `name` and `key` have
  8904. - // static lifetimes so they live indefinitely.
  8905. - state <- Opaque::ffi_init(|slot| unsafe {
  8906. - B::init(slot, name.as_char_ptr(), key.as_ptr())
  8907. - }),
  8908. - })
  8909. - }
  8910. -}
  8911. -
  8912. -impl<T: ?Sized, B: Backend> Lock<T, B> {
  8913. - /// Acquires the lock and gives the caller access to the data protected by it.
  8914. - pub fn lock(&self) -> Guard<'_, T, B> {
  8915. - // SAFETY: The constructor of the type calls `init`, so the existence of the object proves
  8916. - // that `init` was called.
  8917. - let state = unsafe { B::lock(self.state.get()) };
  8918. - // SAFETY: The lock was just acquired.
  8919. - unsafe { Guard::new(self, state) }
  8920. - }
  8921. -}
  8922. -
  8923. -/// A lock guard.
  8924. -///
  8925. -/// Allows mutual exclusion primitives that implement the [`Backend`] trait to automatically unlock
  8926. -/// when a guard goes out of scope. It also provides a safe and convenient way to access the data
  8927. -/// protected by the lock.
  8928. -#[must_use = "the lock unlocks immediately when the guard is unused"]
  8929. -pub struct Guard<'a, T: ?Sized, B: Backend> {
  8930. - pub(crate) lock: &'a Lock<T, B>,
  8931. - pub(crate) state: B::GuardState,
  8932. - _not_send: PhantomData<*mut ()>,
  8933. -}
  8934. -
  8935. -// SAFETY: `Guard` is sync when the data protected by the lock is also sync.
  8936. -unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
  8937. -
  8938. -impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
  8939. - pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
  8940. - // SAFETY: The caller owns the lock, so it is safe to unlock it.
  8941. - unsafe { B::unlock(self.lock.state.get(), &self.state) };
  8942. -
  8943. - // SAFETY: The lock was just unlocked above and is being relocked now.
  8944. - let _relock =
  8945. - ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &mut self.state) });
  8946. -
  8947. - cb()
  8948. - }
  8949. -}
  8950. -
  8951. -impl<T: ?Sized, B: Backend> core::ops::Deref for Guard<'_, T, B> {
  8952. - type Target = T;
  8953. -
  8954. - fn deref(&self) -> &Self::Target {
  8955. - // SAFETY: The caller owns the lock, so it is safe to deref the protected data.
  8956. - unsafe { &*self.lock.data.get() }
  8957. - }
  8958. -}
  8959. -
  8960. -impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B> {
  8961. - fn deref_mut(&mut self) -> &mut Self::Target {
  8962. - // SAFETY: The caller owns the lock, so it is safe to deref the protected data.
  8963. - unsafe { &mut *self.lock.data.get() }
  8964. - }
  8965. -}
  8966. -
  8967. -impl<T: ?Sized, B: Backend> Drop for Guard<'_, T, B> {
  8968. - fn drop(&mut self) {
  8969. - // SAFETY: The caller owns the lock, so it is safe to unlock it.
  8970. - unsafe { B::unlock(self.lock.state.get(), &self.state) };
  8971. - }
  8972. -}
  8973. -
  8974. -impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
  8975. - /// Constructs a new immutable lock guard.
  8976. - ///
  8977. - /// # Safety
  8978. - ///
  8979. - /// The caller must ensure that it owns the lock.
  8980. - pub(crate) unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
  8981. - Self {
  8982. - lock,
  8983. - state,
  8984. - _not_send: PhantomData,
  8985. - }
  8986. - }
  8987. -}
  8988. diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs
  8989. deleted file mode 100644
  8990. --- a/rust/kernel/sync/lock/mutex.rs
  8991. +++ /dev/null
  8992. @@ -1,118 +0,0 @@
  8993. -// SPDX-License-Identifier: GPL-2.0
  8994. -
  8995. -//! A kernel mutex.
  8996. -//!
  8997. -//! This module allows Rust code to use the kernel's `struct mutex`.
  8998. -
  8999. -/// Creates a [`Mutex`] initialiser with the given name and a newly-created lock class.
  9000. -///
  9001. -/// It uses the name if one is given, otherwise it generates one based on the file name and line
  9002. -/// number.
  9003. -#[macro_export]
  9004. -macro_rules! new_mutex {
  9005. - ($inner:expr $(, $name:literal)? $(,)?) => {
  9006. - $crate::sync::Mutex::new(
  9007. - $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
  9008. - };
  9009. -}
  9010. -pub use new_mutex;
  9011. -
  9012. -/// A mutual exclusion primitive.
  9013. -///
  9014. -/// Exposes the kernel's [`struct mutex`]. When multiple threads attempt to lock the same mutex,
  9015. -/// only one at a time is allowed to progress, the others will block (sleep) until the mutex is
  9016. -/// unlocked, at which point another thread will be allowed to wake up and make progress.
  9017. -///
  9018. -/// Since it may block, [`Mutex`] needs to be used with care in atomic contexts.
  9019. -///
  9020. -/// Instances of [`Mutex`] need a lock class and to be pinned. The recommended way to create such
  9021. -/// instances is with the [`pin_init`](crate::pin_init) and [`new_mutex`] macros.
  9022. -///
  9023. -/// # Examples
  9024. -///
  9025. -/// The following example shows how to declare, allocate and initialise a struct (`Example`) that
  9026. -/// contains an inner struct (`Inner`) that is protected by a mutex.
  9027. -///
  9028. -/// ```
  9029. -/// use kernel::sync::{new_mutex, Mutex};
  9030. -///
  9031. -/// struct Inner {
  9032. -/// a: u32,
  9033. -/// b: u32,
  9034. -/// }
  9035. -///
  9036. -/// #[pin_data]
  9037. -/// struct Example {
  9038. -/// c: u32,
  9039. -/// #[pin]
  9040. -/// d: Mutex<Inner>,
  9041. -/// }
  9042. -///
  9043. -/// impl Example {
  9044. -/// fn new() -> impl PinInit<Self> {
  9045. -/// pin_init!(Self {
  9046. -/// c: 10,
  9047. -/// d <- new_mutex!(Inner { a: 20, b: 30 }),
  9048. -/// })
  9049. -/// }
  9050. -/// }
  9051. -///
  9052. -/// // Allocate a boxed `Example`.
  9053. -/// let e = Box::pin_init(Example::new(), GFP_KERNEL)?;
  9054. -/// assert_eq!(e.c, 10);
  9055. -/// assert_eq!(e.d.lock().a, 20);
  9056. -/// assert_eq!(e.d.lock().b, 30);
  9057. -/// # Ok::<(), Error>(())
  9058. -/// ```
  9059. -///
  9060. -/// The following example shows how to use interior mutability to modify the contents of a struct
  9061. -/// protected by a mutex despite only having a shared reference:
  9062. -///
  9063. -/// ```
  9064. -/// use kernel::sync::Mutex;
  9065. -///
  9066. -/// struct Example {
  9067. -/// a: u32,
  9068. -/// b: u32,
  9069. -/// }
  9070. -///
  9071. -/// fn example(m: &Mutex<Example>) {
  9072. -/// let mut guard = m.lock();
  9073. -/// guard.a += 10;
  9074. -/// guard.b += 20;
  9075. -/// }
  9076. -/// ```
  9077. -///
  9078. -/// [`struct mutex`]: srctree/include/linux/mutex.h
  9079. -pub type Mutex<T> = super::Lock<T, MutexBackend>;
  9080. -
  9081. -/// A kernel `struct mutex` lock backend.
  9082. -pub struct MutexBackend;
  9083. -
  9084. -// SAFETY: The underlying kernel `struct mutex` object ensures mutual exclusion.
  9085. -unsafe impl super::Backend for MutexBackend {
  9086. - type State = bindings::mutex;
  9087. - type GuardState = ();
  9088. -
  9089. - unsafe fn init(
  9090. - ptr: *mut Self::State,
  9091. - name: *const core::ffi::c_char,
  9092. - key: *mut bindings::lock_class_key,
  9093. - ) {
  9094. - // SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and
  9095. - // `key` are valid for read indefinitely.
  9096. - unsafe { bindings::__mutex_init(ptr, name, key) }
  9097. - }
  9098. -
  9099. - unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
  9100. - // SAFETY: The safety requirements of this function ensure that `ptr` points to valid
  9101. - // memory, and that it has been initialised before.
  9102. - unsafe { bindings::mutex_lock(ptr) };
  9103. - }
  9104. -
  9105. - unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
  9106. - // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
  9107. - // caller is the owner of the mutex.
  9108. - unsafe { bindings::mutex_unlock(ptr) };
  9109. - }
  9110. -}
  9111. diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs
  9112. deleted file mode 100644
  9113. --- a/rust/kernel/sync/lock/spinlock.rs
  9114. +++ /dev/null
  9115. @@ -1,117 +0,0 @@
  9116. -// SPDX-License-Identifier: GPL-2.0
  9117. -
  9118. -//! A kernel spinlock.
  9119. -//!
  9120. -//! This module allows Rust code to use the kernel's `spinlock_t`.
  9121. -
  9122. -/// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class.
  9123. -///
  9124. -/// It uses the name if one is given, otherwise it generates one based on the file name and line
  9125. -/// number.
  9126. -#[macro_export]
  9127. -macro_rules! new_spinlock {
  9128. - ($inner:expr $(, $name:literal)? $(,)?) => {
  9129. - $crate::sync::SpinLock::new(
  9130. - $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
  9131. - };
  9132. -}
  9133. -pub use new_spinlock;
  9134. -
  9135. -/// A spinlock.
  9136. -///
  9137. -/// Exposes the kernel's [`spinlock_t`]. When multiple CPUs attempt to lock the same spinlock, only
  9138. -/// one at a time is allowed to progress, the others will block (spinning) until the spinlock is
  9139. -/// unlocked, at which point another CPU will be allowed to make progress.
  9140. -///
  9141. -/// Instances of [`SpinLock`] need a lock class and to be pinned. The recommended way to create such
  9142. -/// instances is with the [`pin_init`](crate::pin_init) and [`new_spinlock`] macros.
  9143. -///
  9144. -/// # Examples
  9145. -///
  9146. -/// The following example shows how to declare, allocate and initialise a struct (`Example`) that
  9147. -/// contains an inner struct (`Inner`) that is protected by a spinlock.
  9148. -///
  9149. -/// ```
  9150. -/// use kernel::sync::{new_spinlock, SpinLock};
  9151. -///
  9152. -/// struct Inner {
  9153. -/// a: u32,
  9154. -/// b: u32,
  9155. -/// }
  9156. -///
  9157. -/// #[pin_data]
  9158. -/// struct Example {
  9159. -/// c: u32,
  9160. -/// #[pin]
  9161. -/// d: SpinLock<Inner>,
  9162. -/// }
  9163. -///
  9164. -/// impl Example {
  9165. -/// fn new() -> impl PinInit<Self> {
  9166. -/// pin_init!(Self {
  9167. -/// c: 10,
  9168. -/// d <- new_spinlock!(Inner { a: 20, b: 30 }),
  9169. -/// })
  9170. -/// }
  9171. -/// }
  9172. -///
  9173. -/// // Allocate a boxed `Example`.
  9174. -/// let e = Box::pin_init(Example::new(), GFP_KERNEL)?;
  9175. -/// assert_eq!(e.c, 10);
  9176. -/// assert_eq!(e.d.lock().a, 20);
  9177. -/// assert_eq!(e.d.lock().b, 30);
  9178. -/// # Ok::<(), Error>(())
  9179. -/// ```
  9180. -///
  9181. -/// The following example shows how to use interior mutability to modify the contents of a struct
  9182. -/// protected by a spinlock despite only having a shared reference:
  9183. -///
  9184. -/// ```
  9185. -/// use kernel::sync::SpinLock;
  9186. -///
  9187. -/// struct Example {
  9188. -/// a: u32,
  9189. -/// b: u32,
  9190. -/// }
  9191. -///
  9192. -/// fn example(m: &SpinLock<Example>) {
  9193. -/// let mut guard = m.lock();
  9194. -/// guard.a += 10;
  9195. -/// guard.b += 20;
  9196. -/// }
  9197. -/// ```
  9198. -///
  9199. -/// [`spinlock_t`]: srctree/include/linux/spinlock.h
  9200. -pub type SpinLock<T> = super::Lock<T, SpinLockBackend>;
  9201. -
  9202. -/// A kernel `spinlock_t` lock backend.
  9203. -pub struct SpinLockBackend;
  9204. -
  9205. -// SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion. `relock` uses the
  9206. -// default implementation that always calls the same locking method.
  9207. -unsafe impl super::Backend for SpinLockBackend {
  9208. - type State = bindings::spinlock_t;
  9209. - type GuardState = ();
  9210. -
  9211. - unsafe fn init(
  9212. - ptr: *mut Self::State,
  9213. - name: *const core::ffi::c_char,
  9214. - key: *mut bindings::lock_class_key,
  9215. - ) {
  9216. - // SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and
  9217. - // `key` are valid for read indefinitely.
  9218. - unsafe { bindings::__spin_lock_init(ptr, name, key) }
  9219. - }
  9220. -
  9221. - unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
  9222. - // SAFETY: The safety requirements of this function ensure that `ptr` points to valid
  9223. - // memory, and that it has been initialised before.
  9224. - unsafe { bindings::spin_lock(ptr) }
  9225. - }
  9226. -
  9227. - unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
  9228. - // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
  9229. - // caller is the owner of the spinlock.
  9230. - unsafe { bindings::spin_unlock(ptr) }
  9231. - }
  9232. -}
  9233. diff --git a/rust/kernel/sync/locked_by.rs b/rust/kernel/sync/locked_by.rs
  9234. deleted file mode 100644
  9235. --- a/rust/kernel/sync/locked_by.rs
  9236. +++ /dev/null
  9237. @@ -1,159 +0,0 @@
  9238. -// SPDX-License-Identifier: GPL-2.0
  9239. -
  9240. -//! A wrapper for data protected by a lock that does not wrap it.
  9241. -
  9242. -use super::{lock::Backend, lock::Lock};
  9243. -use crate::build_assert;
  9244. -use core::{cell::UnsafeCell, mem::size_of, ptr};
  9245. -
  9246. -/// Allows access to some data to be serialised by a lock that does not wrap it.
  9247. -///
  9248. -/// In most cases, data protected by a lock is wrapped by the appropriate lock type, e.g.,
  9249. -/// [`Mutex`] or [`SpinLock`]. [`LockedBy`] is meant for cases when this is not possible.
  9250. -/// For example, if a container has a lock and some data in the contained elements needs
  9251. -/// to be protected by the same lock.
  9252. -///
  9253. -/// [`LockedBy`] wraps the data in lieu of another locking primitive, and only allows access to it
  9254. -/// when the caller shows evidence that the 'external' lock is locked. It panics if the evidence
  9255. -/// refers to the wrong instance of the lock.
  9256. -///
  9257. -/// [`Mutex`]: super::Mutex
  9258. -/// [`SpinLock`]: super::SpinLock
  9259. -///
  9260. -/// # Examples
  9261. -///
  9262. -/// The following is an example for illustrative purposes: `InnerDirectory::bytes_used` is an
  9263. -/// aggregate of all `InnerFile::bytes_used` and must be kept consistent; so we wrap `InnerFile` in
  9264. -/// a `LockedBy` so that it shares a lock with `InnerDirectory`. This allows us to enforce at
  9265. -/// compile-time that access to `InnerFile` is only granted when an `InnerDirectory` is also
  9266. -/// locked; we enforce at run time that the right `InnerDirectory` is locked.
  9267. -///
  9268. -/// ```
  9269. -/// use kernel::sync::{LockedBy, Mutex};
  9270. -///
  9271. -/// struct InnerFile {
  9272. -/// bytes_used: u64,
  9273. -/// }
  9274. -///
  9275. -/// struct File {
  9276. -/// _ino: u32,
  9277. -/// inner: LockedBy<InnerFile, InnerDirectory>,
  9278. -/// }
  9279. -///
  9280. -/// struct InnerDirectory {
  9281. -/// /// The sum of the bytes used by all files.
  9282. -/// bytes_used: u64,
  9283. -/// _files: Vec<File>,
  9284. -/// }
  9285. -///
  9286. -/// struct Directory {
  9287. -/// _ino: u32,
  9288. -/// inner: Mutex<InnerDirectory>,
  9289. -/// }
  9290. -///
  9291. -/// /// Prints `bytes_used` from both the directory and file.
  9292. -/// fn print_bytes_used(dir: &Directory, file: &File) {
  9293. -/// let guard = dir.inner.lock();
  9294. -/// let inner_file = file.inner.access(&guard);
  9295. -/// pr_info!("{} {}", guard.bytes_used, inner_file.bytes_used);
  9296. -/// }
  9297. -///
  9298. -/// /// Increments `bytes_used` for both the directory and file.
  9299. -/// fn inc_bytes_used(dir: &Directory, file: &File) {
  9300. -/// let mut guard = dir.inner.lock();
  9301. -/// guard.bytes_used += 10;
  9302. -///
  9303. -/// let file_inner = file.inner.access_mut(&mut guard);
  9304. -/// file_inner.bytes_used += 10;
  9305. -/// }
  9306. -///
  9307. -/// /// Creates a new file.
  9308. -/// fn new_file(ino: u32, dir: &Directory) -> File {
  9309. -/// File {
  9310. -/// _ino: ino,
  9311. -/// inner: LockedBy::new(&dir.inner, InnerFile { bytes_used: 0 }),
  9312. -/// }
  9313. -/// }
  9314. -/// ```
  9315. -pub struct LockedBy<T: ?Sized, U: ?Sized> {
  9316. - owner: *const U,
  9317. - data: UnsafeCell<T>,
  9318. -}
  9319. -
  9320. -// SAFETY: `LockedBy` can be transferred across thread boundaries iff the data it protects can.
  9321. -unsafe impl<T: ?Sized + Send, U: ?Sized> Send for LockedBy<T, U> {}
  9322. -
  9323. -// SAFETY: `LockedBy` serialises the interior mutability it provides, so it is `Sync` as long as the
  9324. -// data it protects is `Send`.
  9325. -unsafe impl<T: ?Sized + Send, U: ?Sized> Sync for LockedBy<T, U> {}
  9326. -
  9327. -impl<T, U> LockedBy<T, U> {
  9328. - /// Constructs a new instance of [`LockedBy`].
  9329. - ///
  9330. - /// It stores a raw pointer to the owner that is never dereferenced. It is only used to ensure
  9331. - /// that the right owner is being used to access the protected data. If the owner is freed, the
  9332. - /// data becomes inaccessible; if another instance of the owner is allocated *on the same
  9333. - /// memory location*, the data becomes accessible again: none of this affects memory safety
  9334. - /// because in any case at most one thread (or CPU) can access the protected data at a time.
  9335. - pub fn new<B: Backend>(owner: &Lock<U, B>, data: T) -> Self {
  9336. - build_assert!(
  9337. - size_of::<Lock<U, B>>() > 0,
  9338. - "The lock type cannot be a ZST because it may be impossible to distinguish instances"
  9339. - );
  9340. - Self {
  9341. - owner: owner.data.get(),
  9342. - data: UnsafeCell::new(data),
  9343. - }
  9344. - }
  9345. -}
  9346. -
  9347. -impl<T: ?Sized, U> LockedBy<T, U> {
  9348. - /// Returns a reference to the protected data when the caller provides evidence (via a
  9349. - /// reference) that the owner is locked.
  9350. - ///
  9351. - /// `U` cannot be a zero-sized type (ZST) because there are ways to get an `&U` that matches
  9352. - /// the data protected by the lock without actually holding it.
  9353. - ///
  9354. - /// # Panics
  9355. - ///
  9356. - /// Panics if `owner` is different from the data protected by the lock used in
  9357. - /// [`new`](LockedBy::new).
  9358. - pub fn access<'a>(&'a self, owner: &'a U) -> &'a T {
  9359. - build_assert!(
  9360. - size_of::<U>() > 0,
  9361. - "`U` cannot be a ZST because `owner` wouldn't be unique"
  9362. - );
  9363. - if !ptr::eq(owner, self.owner) {
  9364. - panic!("mismatched owners");
  9365. - }
  9366. -
  9367. - // SAFETY: `owner` is evidence that the owner is locked.
  9368. - unsafe { &*self.data.get() }
  9369. - }
  9370. -
  9371. - /// Returns a mutable reference to the protected data when the caller provides evidence (via a
  9372. - /// mutable owner) that the owner is locked mutably.
  9373. - ///
  9374. - /// `U` cannot be a zero-sized type (ZST) because there are ways to get an `&mut U` that
  9375. - /// matches the data protected by the lock without actually holding it.
  9376. - ///
  9377. - /// Showing a mutable reference to the owner is sufficient because we know no other references
  9378. - /// can exist to it.
  9379. - ///
  9380. - /// # Panics
  9381. - ///
  9382. - /// Panics if `owner` is different from the data protected by the lock used in
  9383. - /// [`new`](LockedBy::new).
  9384. - pub fn access_mut<'a>(&'a self, owner: &'a mut U) -> &'a mut T {
  9385. - build_assert!(
  9386. - size_of::<U>() > 0,
  9387. - "`U` cannot be a ZST because `owner` wouldn't be unique"
  9388. - );
  9389. - if !ptr::eq(owner, self.owner) {
  9390. - panic!("mismatched owners");
  9391. - }
  9392. -
  9393. - // SAFETY: `owner` is evidence that there is only one reference to the owner.
  9394. - unsafe { &mut *self.data.get() }
  9395. - }
  9396. -}
  9397. diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
  9398. deleted file mode 100644
  9399. --- a/rust/kernel/task.rs
  9400. +++ /dev/null
  9401. @@ -1,177 +0,0 @@
  9402. -// SPDX-License-Identifier: GPL-2.0
  9403. -
  9404. -//! Tasks (threads and processes).
  9405. -//!
  9406. -//! C header: [`include/linux/sched.h`](srctree/include/linux/sched.h).
  9407. -
  9408. -use crate::types::Opaque;
  9409. -use core::{
  9410. - ffi::{c_int, c_long, c_uint},
  9411. - marker::PhantomData,
  9412. - ops::Deref,
  9413. - ptr,
  9414. -};
  9415. -
  9416. -/// A sentinel value used for infinite timeouts.
  9417. -pub const MAX_SCHEDULE_TIMEOUT: c_long = c_long::MAX;
  9418. -
  9419. -/// Bitmask for tasks that are sleeping in an interruptible state.
  9420. -pub const TASK_INTERRUPTIBLE: c_int = bindings::TASK_INTERRUPTIBLE as c_int;
  9421. -/// Bitmask for tasks that are sleeping in an uninterruptible state.
  9422. -pub const TASK_UNINTERRUPTIBLE: c_int = bindings::TASK_UNINTERRUPTIBLE as c_int;
  9423. -/// Convenience constant for waking up tasks regardless of whether they are in interruptible or
  9424. -/// uninterruptible sleep.
  9425. -pub const TASK_NORMAL: c_uint = bindings::TASK_NORMAL as c_uint;
  9426. -
  9427. -/// Returns the currently running task.
  9428. -#[macro_export]
  9429. -macro_rules! current {
  9430. - () => {
  9431. - // SAFETY: Deref + addr-of below create a temporary `TaskRef` that cannot outlive the
  9432. - // caller.
  9433. - unsafe { &*$crate::task::Task::current() }
  9434. - };
  9435. -}
  9436. -
  9437. -/// Wraps the kernel's `struct task_struct`.
  9438. -///
  9439. -/// # Invariants
  9440. -///
  9441. -/// All instances are valid tasks created by the C portion of the kernel.
  9442. -///
  9443. -/// Instances of this type are always refcounted, that is, a call to `get_task_struct` ensures
  9444. -/// that the allocation remains valid at least until the matching call to `put_task_struct`.
  9445. -///
  9446. -/// # Examples
  9447. -///
  9448. -/// The following is an example of getting the PID of the current thread with zero additional cost
  9449. -/// when compared to the C version:
  9450. -///
  9451. -/// ```
  9452. -/// let pid = current!().pid();
  9453. -/// ```
  9454. -///
  9455. -/// Getting the PID of the current process, also zero additional cost:
  9456. -///
  9457. -/// ```
  9458. -/// let pid = current!().group_leader().pid();
  9459. -/// ```
  9460. -///
  9461. -/// Getting the current task and storing it in some struct. The reference count is automatically
  9462. -/// incremented when creating `State` and decremented when it is dropped:
  9463. -///
  9464. -/// ```
  9465. -/// use kernel::{task::Task, types::ARef};
  9466. -///
  9467. -/// struct State {
  9468. -/// creator: ARef<Task>,
  9469. -/// index: u32,
  9470. -/// }
  9471. -///
  9472. -/// impl State {
  9473. -/// fn new() -> Self {
  9474. -/// Self {
  9475. -/// creator: current!().into(),
  9476. -/// index: 0,
  9477. -/// }
  9478. -/// }
  9479. -/// }
  9480. -/// ```
  9481. -#[repr(transparent)]
  9482. -pub struct Task(pub(crate) Opaque<bindings::task_struct>);
  9483. -
  9484. -// SAFETY: By design, the only way to access a `Task` is via the `current` function or via an
  9485. -// `ARef<Task>` obtained through the `AlwaysRefCounted` impl. This means that the only situation in
  9486. -// which a `Task` can be accessed mutably is when the refcount drops to zero and the destructor
  9487. -// runs. It is safe for that to happen on any thread, so it is ok for this type to be `Send`.
  9488. -unsafe impl Send for Task {}
  9489. -
  9490. -// SAFETY: It's OK to access `Task` through shared references from other threads because we're
  9491. -// either accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly
  9492. -// synchronised by C code (e.g., `signal_pending`).
  9493. -unsafe impl Sync for Task {}
  9494. -
  9495. -/// The type of process identifiers (PIDs).
  9496. -type Pid = bindings::pid_t;
  9497. -
  9498. -impl Task {
  9499. - /// Returns a task reference for the currently executing task/thread.
  9500. - ///
  9501. - /// The recommended way to get the current task/thread is to use the
  9502. - /// [`current`] macro because it is safe.
  9503. - ///
  9504. - /// # Safety
  9505. - ///
  9506. - /// Callers must ensure that the returned object doesn't outlive the current task/thread.
  9507. - pub unsafe fn current() -> impl Deref<Target = Task> {
  9508. - struct TaskRef<'a> {
  9509. - task: &'a Task,
  9510. - _not_send: PhantomData<*mut ()>,
  9511. - }
  9512. -
  9513. - impl Deref for TaskRef<'_> {
  9514. - type Target = Task;
  9515. -
  9516. - fn deref(&self) -> &Self::Target {
  9517. - self.task
  9518. - }
  9519. - }
  9520. -
  9521. - // SAFETY: Just an FFI call with no additional safety requirements.
  9522. - let ptr = unsafe { bindings::get_current() };
  9523. -
  9524. - TaskRef {
  9525. - // SAFETY: If the current thread is still running, the current task is valid. Given
  9526. - // that `TaskRef` is not `Send`, we know it cannot be transferred to another thread
  9527. - // (where it could potentially outlive the caller).
  9528. - task: unsafe { &*ptr.cast() },
  9529. - _not_send: PhantomData,
  9530. - }
  9531. - }
  9532. -
  9533. - /// Returns the group leader of the given task.
  9534. - pub fn group_leader(&self) -> &Task {
  9535. - // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
  9536. - // have a valid `group_leader`.
  9537. - let ptr = unsafe { *ptr::addr_of!((*self.0.get()).group_leader) };
  9538. -
  9539. - // SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
  9540. - // and given that a task has a reference to its group leader, we know it must be valid for
  9541. - // the lifetime of the returned task reference.
  9542. - unsafe { &*ptr.cast() }
  9543. - }
  9544. -
  9545. - /// Returns the PID of the given task.
  9546. - pub fn pid(&self) -> Pid {
  9547. - // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
  9548. - // have a valid pid.
  9549. - unsafe { *ptr::addr_of!((*self.0.get()).pid) }
  9550. - }
  9551. -
  9552. - /// Determines whether the given task has pending signals.
  9553. - pub fn signal_pending(&self) -> bool {
  9554. - // SAFETY: By the type invariant, we know that `self.0` is valid.
  9555. - unsafe { bindings::signal_pending(self.0.get()) != 0 }
  9556. - }
  9557. -
  9558. - /// Wakes up the task.
  9559. - pub fn wake_up(&self) {
  9560. - // SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
  9561. - // And `wake_up_process` is safe to be called for any valid task, even if the task is
  9562. - // running.
  9563. - unsafe { bindings::wake_up_process(self.0.get()) };
  9564. - }
  9565. -}
  9566. -
  9567. -// SAFETY: The type invariants guarantee that `Task` is always refcounted.
  9568. -unsafe impl crate::types::AlwaysRefCounted for Task {
  9569. - fn inc_ref(&self) {
  9570. - // SAFETY: The existence of a shared reference means that the refcount is nonzero.
  9571. - unsafe { bindings::get_task_struct(self.0.get()) };
  9572. - }
  9573. -
  9574. - unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
  9575. - // SAFETY: The safety requirements guarantee that the refcount is nonzero.
  9576. - unsafe { bindings::put_task_struct(obj.cast().as_ptr()) }
  9577. - }
  9578. -}
  9579. diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
  9580. deleted file mode 100644
  9581. --- a/rust/kernel/time.rs
  9582. +++ /dev/null
  9583. @@ -1,83 +0,0 @@
  9584. -// SPDX-License-Identifier: GPL-2.0
  9585. -
  9586. -//! Time related primitives.
  9587. -//!
  9588. -//! This module contains the kernel APIs related to time and timers that
  9589. -//! have been ported or wrapped for usage by Rust code in the kernel.
  9590. -//!
  9591. -//! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
  9592. -//! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h).
  9593. -
  9594. -/// The number of nanoseconds per millisecond.
  9595. -pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
  9596. -
  9597. -/// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
  9598. -pub type Jiffies = core::ffi::c_ulong;
  9599. -
  9600. -/// The millisecond time unit.
  9601. -pub type Msecs = core::ffi::c_uint;
  9602. -
  9603. -/// Converts milliseconds to jiffies.
  9604. -#[inline]
  9605. -pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
  9606. - // SAFETY: The `__msecs_to_jiffies` function is always safe to call no
  9607. - // matter what the argument is.
  9608. - unsafe { bindings::__msecs_to_jiffies(msecs) }
  9609. -}
  9610. -
  9611. -/// A Rust wrapper around a `ktime_t`.
  9612. -#[repr(transparent)]
  9613. -#[derive(Copy, Clone)]
  9614. -pub struct Ktime {
  9615. - inner: bindings::ktime_t,
  9616. -}
  9617. -
  9618. -impl Ktime {
  9619. - /// Create a `Ktime` from a raw `ktime_t`.
  9620. - #[inline]
  9621. - pub fn from_raw(inner: bindings::ktime_t) -> Self {
  9622. - Self { inner }
  9623. - }
  9624. -
  9625. - /// Get the current time using `CLOCK_MONOTONIC`.
  9626. - #[inline]
  9627. - pub fn ktime_get() -> Self {
  9628. - // SAFETY: It is always safe to call `ktime_get` outside of NMI context.
  9629. - Self::from_raw(unsafe { bindings::ktime_get() })
  9630. - }
  9631. -
  9632. - /// Divide the number of nanoseconds by a compile-time constant.
  9633. - #[inline]
  9634. - fn divns_constant<const DIV: i64>(self) -> i64 {
  9635. - self.to_ns() / DIV
  9636. - }
  9637. -
  9638. - /// Returns the number of nanoseconds.
  9639. - #[inline]
  9640. - pub fn to_ns(self) -> i64 {
  9641. - self.inner
  9642. - }
  9643. -
  9644. - /// Returns the number of milliseconds.
  9645. - #[inline]
  9646. - pub fn to_ms(self) -> i64 {
  9647. - self.divns_constant::<NSEC_PER_MSEC>()
  9648. - }
  9649. -}
  9650. -
  9651. -/// Returns the number of milliseconds between two ktimes.
  9652. -#[inline]
  9653. -pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 {
  9654. - (later - earlier).to_ms()
  9655. -}
  9656. -
  9657. -impl core::ops::Sub for Ktime {
  9658. - type Output = Ktime;
  9659. -
  9660. - #[inline]
  9661. - fn sub(self, other: Ktime) -> Ktime {
  9662. - Self {
  9663. - inner: self.inner - other.inner,
  9664. - }
  9665. - }
  9666. -}
  9667. diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
  9668. deleted file mode 100644
  9669. --- a/rust/kernel/types.rs
  9670. +++ /dev/null
  9671. @@ -1,411 +0,0 @@
  9672. -// SPDX-License-Identifier: GPL-2.0
  9673. -
  9674. -//! Kernel types.
  9675. -
  9676. -use crate::init::{self, PinInit};
  9677. -use alloc::boxed::Box;
  9678. -use core::{
  9679. - cell::UnsafeCell,
  9680. - marker::{PhantomData, PhantomPinned},
  9681. - mem::MaybeUninit,
  9682. - ops::{Deref, DerefMut},
  9683. - ptr::NonNull,
  9684. -};
  9685. -
  9686. -/// Used to transfer ownership to and from foreign (non-Rust) languages.
  9687. -///
  9688. -/// Ownership is transferred from Rust to a foreign language by calling [`Self::into_foreign`] and
  9689. -/// later may be transferred back to Rust by calling [`Self::from_foreign`].
  9690. -///
  9691. -/// This trait is meant to be used in cases when Rust objects are stored in C objects and
  9692. -/// eventually "freed" back to Rust.
  9693. -pub trait ForeignOwnable: Sized {
  9694. - /// Type of values borrowed between calls to [`ForeignOwnable::into_foreign`] and
  9695. - /// [`ForeignOwnable::from_foreign`].
  9696. - type Borrowed<'a>;
  9697. -
  9698. - /// Converts a Rust-owned object to a foreign-owned one.
  9699. - ///
  9700. - /// The foreign representation is a pointer to void.
  9701. - fn into_foreign(self) -> *const core::ffi::c_void;
  9702. -
  9703. - /// Borrows a foreign-owned object.
  9704. - ///
  9705. - /// # Safety
  9706. - ///
  9707. - /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
  9708. - /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
  9709. - unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>;
  9710. -
  9711. - /// Converts a foreign-owned object back to a Rust-owned one.
  9712. - ///
  9713. - /// # Safety
  9714. - ///
  9715. - /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
  9716. - /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
  9717. - /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
  9718. - /// this object must have been dropped.
  9719. - unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
  9720. -
  9721. - /// Tries to convert a foreign-owned object back to a Rust-owned one.
  9722. - ///
  9723. - /// A convenience wrapper over [`ForeignOwnable::from_foreign`] that returns [`None`] if `ptr`
  9724. - /// is null.
  9725. - ///
  9726. - /// # Safety
  9727. - ///
  9728. - /// `ptr` must either be null or satisfy the safety requirements for
  9729. - /// [`ForeignOwnable::from_foreign`].
  9730. - unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option<Self> {
  9731. - if ptr.is_null() {
  9732. - None
  9733. - } else {
  9734. - // SAFETY: Since `ptr` is not null here, then `ptr` satisfies the safety requirements
  9735. - // of `from_foreign` given the safety requirements of this function.
  9736. - unsafe { Some(Self::from_foreign(ptr)) }
  9737. - }
  9738. - }
  9739. -}
  9740. -
  9741. -impl<T: 'static> ForeignOwnable for Box<T> {
  9742. - type Borrowed<'a> = &'a T;
  9743. -
  9744. - fn into_foreign(self) -> *const core::ffi::c_void {
  9745. - Box::into_raw(self) as _
  9746. - }
  9747. -
  9748. - unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T {
  9749. - // SAFETY: The safety requirements for this function ensure that the object is still alive,
  9750. - // so it is safe to dereference the raw pointer.
  9751. - // The safety requirements of `from_foreign` also ensure that the object remains alive for
  9752. - // the lifetime of the returned value.
  9753. - unsafe { &*ptr.cast() }
  9754. - }
  9755. -
  9756. - unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
  9757. - // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
  9758. - // call to `Self::into_foreign`.
  9759. - unsafe { Box::from_raw(ptr as _) }
  9760. - }
  9761. -}
  9762. -
  9763. -impl ForeignOwnable for () {
  9764. - type Borrowed<'a> = ();
  9765. -
  9766. - fn into_foreign(self) -> *const core::ffi::c_void {
  9767. - core::ptr::NonNull::dangling().as_ptr()
  9768. - }
  9769. -
  9770. - unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {}
  9771. -
  9772. - unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {}
  9773. -}
  9774. -
  9775. -/// Runs a cleanup function/closure when dropped.
  9776. -///
  9777. -/// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
  9778. -///
  9779. -/// # Examples
  9780. -///
  9781. -/// In the example below, we have multiple exit paths and we want to log regardless of which one is
  9782. -/// taken:
  9783. -///
  9784. -/// ```
  9785. -/// # use kernel::types::ScopeGuard;
  9786. -/// fn example1(arg: bool) {
  9787. -/// let _log = ScopeGuard::new(|| pr_info!("example1 completed\n"));
  9788. -///
  9789. -/// if arg {
  9790. -/// return;
  9791. -/// }
  9792. -///
  9793. -/// pr_info!("Do something...\n");
  9794. -/// }
  9795. -///
  9796. -/// # example1(false);
  9797. -/// # example1(true);
  9798. -/// ```
  9799. -///
  9800. -/// In the example below, we want to log the same message on all early exits but a different one on
  9801. -/// the main exit path:
  9802. -///
  9803. -/// ```
  9804. -/// # use kernel::types::ScopeGuard;
  9805. -/// fn example2(arg: bool) {
  9806. -/// let log = ScopeGuard::new(|| pr_info!("example2 returned early\n"));
  9807. -///
  9808. -/// if arg {
  9809. -/// return;
  9810. -/// }
  9811. -///
  9812. -/// // (Other early returns...)
  9813. -///
  9814. -/// log.dismiss();
  9815. -/// pr_info!("example2 no early return\n");
  9816. -/// }
  9817. -///
  9818. -/// # example2(false);
  9819. -/// # example2(true);
  9820. -/// ```
  9821. -///
  9822. -/// In the example below, we need a mutable object (the vector) to be accessible within the log
  9823. -/// function, so we wrap it in the [`ScopeGuard`]:
  9824. -///
  9825. -/// ```
  9826. -/// # use kernel::types::ScopeGuard;
  9827. -/// fn example3(arg: bool) -> Result {
  9828. -/// let mut vec =
  9829. -/// ScopeGuard::new_with_data(Vec::new(), |v| pr_info!("vec had {} elements\n", v.len()));
  9830. -///
  9831. -/// vec.push(10u8, GFP_KERNEL)?;
  9832. -/// if arg {
  9833. -/// return Ok(());
  9834. -/// }
  9835. -/// vec.push(20u8, GFP_KERNEL)?;
  9836. -/// Ok(())
  9837. -/// }
  9838. -///
  9839. -/// # assert_eq!(example3(false), Ok(()));
  9840. -/// # assert_eq!(example3(true), Ok(()));
  9841. -/// ```
  9842. -///
  9843. -/// # Invariants
  9844. -///
  9845. -/// The value stored in the struct is nearly always `Some(_)`, except between
  9846. -/// [`ScopeGuard::dismiss`] and [`ScopeGuard::drop`]: in this case, it will be `None` as the value
  9847. -/// will have been returned to the caller. Since [`ScopeGuard::dismiss`] consumes the guard,
  9848. -/// callers won't be able to use it anymore.
  9849. -pub struct ScopeGuard<T, F: FnOnce(T)>(Option<(T, F)>);
  9850. -
  9851. -impl<T, F: FnOnce(T)> ScopeGuard<T, F> {
  9852. - /// Creates a new guarded object wrapping the given data and with the given cleanup function.
  9853. - pub fn new_with_data(data: T, cleanup_func: F) -> Self {
  9854. - // INVARIANT: The struct is being initialised with `Some(_)`.
  9855. - Self(Some((data, cleanup_func)))
  9856. - }
  9857. -
  9858. - /// Prevents the cleanup function from running and returns the guarded data.
  9859. - pub fn dismiss(mut self) -> T {
  9860. - // INVARIANT: This is the exception case in the invariant; it is not visible to callers
  9861. - // because this function consumes `self`.
  9862. - self.0.take().unwrap().0
  9863. - }
  9864. -}
  9865. -
  9866. -impl ScopeGuard<(), fn(())> {
  9867. - /// Creates a new guarded object with the given cleanup function.
  9868. - pub fn new(cleanup: impl FnOnce()) -> ScopeGuard<(), impl FnOnce(())> {
  9869. - ScopeGuard::new_with_data((), move |_| cleanup())
  9870. - }
  9871. -}
  9872. -
  9873. -impl<T, F: FnOnce(T)> Deref for ScopeGuard<T, F> {
  9874. - type Target = T;
  9875. -
  9876. - fn deref(&self) -> &T {
  9877. - // The type invariants guarantee that `unwrap` will succeed.
  9878. - &self.0.as_ref().unwrap().0
  9879. - }
  9880. -}
  9881. -
  9882. -impl<T, F: FnOnce(T)> DerefMut for ScopeGuard<T, F> {
  9883. - fn deref_mut(&mut self) -> &mut T {
  9884. - // The type invariants guarantee that `unwrap` will succeed.
  9885. - &mut self.0.as_mut().unwrap().0
  9886. - }
  9887. -}
  9888. -
  9889. -impl<T, F: FnOnce(T)> Drop for ScopeGuard<T, F> {
  9890. - fn drop(&mut self) {
  9891. - // Run the cleanup function if one is still present.
  9892. - if let Some((data, cleanup)) = self.0.take() {
  9893. - cleanup(data)
  9894. - }
  9895. - }
  9896. -}
  9897. -
  9898. -/// Stores an opaque value.
  9899. -///
  9900. -/// This is meant to be used with FFI objects that are never interpreted by Rust code.
  9901. -#[repr(transparent)]
  9902. -pub struct Opaque<T> {
  9903. - value: UnsafeCell<MaybeUninit<T>>,
  9904. - _pin: PhantomPinned,
  9905. -}
  9906. -
  9907. -impl<T> Opaque<T> {
  9908. - /// Creates a new opaque value.
  9909. - pub const fn new(value: T) -> Self {
  9910. - Self {
  9911. - value: UnsafeCell::new(MaybeUninit::new(value)),
  9912. - _pin: PhantomPinned,
  9913. - }
  9914. - }
  9915. -
  9916. - /// Creates an uninitialised value.
  9917. - pub const fn uninit() -> Self {
  9918. - Self {
  9919. - value: UnsafeCell::new(MaybeUninit::uninit()),
  9920. - _pin: PhantomPinned,
  9921. - }
  9922. - }
  9923. -
  9924. - /// Creates a pin-initializer from the given initializer closure.
  9925. - ///
  9926. - /// The returned initializer calls the given closure with the pointer to the inner `T` of this
  9927. - /// `Opaque`. Since this memory is uninitialized, the closure is not allowed to read from it.
  9928. - ///
  9929. - /// This function is safe, because the `T` inside of an `Opaque` is allowed to be
  9930. - /// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
  9931. - /// to verify at that point that the inner value is valid.
  9932. - pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
  9933. - // SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
  9934. - // initialize the `T`.
  9935. - unsafe {
  9936. - init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
  9937. - init_func(Self::raw_get(slot));
  9938. - Ok(())
  9939. - })
  9940. - }
  9941. - }
  9942. -
  9943. - /// Returns a raw pointer to the opaque data.
  9944. - pub const fn get(&self) -> *mut T {
  9945. - UnsafeCell::get(&self.value).cast::<T>()
  9946. - }
  9947. -
  9948. - /// Gets the value behind `this`.
  9949. - ///
  9950. - /// This function is useful to get access to the value without creating intermediate
  9951. - /// references.
  9952. - pub const fn raw_get(this: *const Self) -> *mut T {
  9953. - UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
  9954. - }
  9955. -}
  9956. -
  9957. -/// Types that are _always_ reference counted.
  9958. -///
  9959. -/// It allows such types to define their own custom ref increment and decrement functions.
  9960. -/// Additionally, it allows users to convert from a shared reference `&T` to an owned reference
  9961. -/// [`ARef<T>`].
  9962. -///
  9963. -/// This is usually implemented by wrappers to existing structures on the C side of the code. For
  9964. -/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
  9965. -/// instances of a type.
  9966. -///
  9967. -/// # Safety
  9968. -///
  9969. -/// Implementers must ensure that increments to the reference count keep the object alive in memory
  9970. -/// at least until matching decrements are performed.
  9971. -///
  9972. -/// Implementers must also ensure that all instances are reference-counted. (Otherwise they
  9973. -/// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object
  9974. -/// alive.)
  9975. -pub unsafe trait AlwaysRefCounted {
  9976. - /// Increments the reference count on the object.
  9977. - fn inc_ref(&self);
  9978. -
  9979. - /// Decrements the reference count on the object.
  9980. - ///
  9981. - /// Frees the object when the count reaches zero.
  9982. - ///
  9983. - /// # Safety
  9984. - ///
  9985. - /// Callers must ensure that there was a previous matching increment to the reference count,
  9986. - /// and that the object is no longer used after its reference count is decremented (as it may
  9987. - /// result in the object being freed), unless the caller owns another increment on the refcount
  9988. - /// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls
  9989. - /// [`AlwaysRefCounted::dec_ref`] once).
  9990. - unsafe fn dec_ref(obj: NonNull<Self>);
  9991. -}
  9992. -
  9993. -/// An owned reference to an always-reference-counted object.
  9994. -///
  9995. -/// The object's reference count is automatically decremented when an instance of [`ARef`] is
  9996. -/// dropped. It is also automatically incremented when a new instance is created via
  9997. -/// [`ARef::clone`].
  9998. -///
  9999. -/// # Invariants
  10000. -///
  10001. -/// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
  10002. -/// particular, the [`ARef`] instance owns an increment on the underlying object's reference count.
  10003. -pub struct ARef<T: AlwaysRefCounted> {
  10004. - ptr: NonNull<T>,
  10005. - _p: PhantomData<T>,
  10006. -}
  10007. -
  10008. -// SAFETY: It is safe to send `ARef<T>` to another thread when the underlying `T` is `Sync` because
  10009. -// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
  10010. -// `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
  10011. -// mutable reference, for example, when the reference count reaches zero and `T` is dropped.
  10012. -unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
  10013. -
  10014. -// SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
  10015. -// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
  10016. -// it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
  10017. -// `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
  10018. -// example, when the reference count reaches zero and `T` is dropped.
  10019. -unsafe impl<T: AlwaysRefCounted + Sync + Send> Sync for ARef<T> {}
  10020. -
  10021. -impl<T: AlwaysRefCounted> ARef<T> {
  10022. - /// Creates a new instance of [`ARef`].
  10023. - ///
  10024. - /// It takes over an increment of the reference count on the underlying object.
  10025. - ///
  10026. - /// # Safety
  10027. - ///
  10028. - /// Callers must ensure that the reference count was incremented at least once, and that they
  10029. - /// are properly relinquishing one increment. That is, if there is only one increment, callers
  10030. - /// must not use the underlying object anymore -- it is only safe to do so via the newly
  10031. - /// created [`ARef`].
  10032. - pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
  10033. - // INVARIANT: The safety requirements guarantee that the new instance now owns the
  10034. - // increment on the refcount.
  10035. - Self {
  10036. - ptr,
  10037. - _p: PhantomData,
  10038. - }
  10039. - }
  10040. -}
  10041. -
  10042. -impl<T: AlwaysRefCounted> Clone for ARef<T> {
  10043. - fn clone(&self) -> Self {
  10044. - self.inc_ref();
  10045. - // SAFETY: We just incremented the refcount above.
  10046. - unsafe { Self::from_raw(self.ptr) }
  10047. - }
  10048. -}
  10049. -
  10050. -impl<T: AlwaysRefCounted> Deref for ARef<T> {
  10051. - type Target = T;
  10052. -
  10053. - fn deref(&self) -> &Self::Target {
  10054. - // SAFETY: The type invariants guarantee that the object is valid.
  10055. - unsafe { self.ptr.as_ref() }
  10056. - }
  10057. -}
  10058. -
  10059. -impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
  10060. - fn from(b: &T) -> Self {
  10061. - b.inc_ref();
  10062. - // SAFETY: We just incremented the refcount above.
  10063. - unsafe { Self::from_raw(NonNull::from(b)) }
  10064. - }
  10065. -}
  10066. -
  10067. -impl<T: AlwaysRefCounted> Drop for ARef<T> {
  10068. - fn drop(&mut self) {
  10069. - // SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
  10070. - // decrement.
  10071. - unsafe { T::dec_ref(self.ptr) };
  10072. - }
  10073. -}
  10074. -
  10075. -/// A sum type that always holds either a value of type `L` or `R`.
  10076. -pub enum Either<L, R> {
  10077. - /// Constructs an instance of [`Either`] containing a value of type `L`.
  10078. - Left(L),
  10079. -
  10080. - /// Constructs an instance of [`Either`] containing a value of type `R`.
  10081. - Right(R),
  10082. -}
  10083. diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
  10084. deleted file mode 100644
  10085. --- a/rust/kernel/workqueue.rs
  10086. +++ /dev/null
  10087. @@ -1,683 +0,0 @@
  10088. -// SPDX-License-Identifier: GPL-2.0
  10089. -
  10090. -//! Work queues.
  10091. -//!
  10092. -//! This file has two components: The raw work item API, and the safe work item API.
  10093. -//!
  10094. -//! One pattern that is used in both APIs is the `ID` const generic, which exists to allow a single
  10095. -//! type to define multiple `work_struct` fields. This is done by choosing an id for each field,
  10096. -//! and using that id to specify which field you wish to use. (The actual value doesn't matter, as
  10097. -//! long as you use different values for different fields of the same struct.) Since these IDs are
  10098. -//! generic, they are used only at compile-time, so they shouldn't exist in the final binary.
  10099. -//!
  10100. -//! # The raw API
  10101. -//!
  10102. -//! The raw API consists of the [`RawWorkItem`] trait, where the work item needs to provide an
  10103. -//! arbitrary function that knows how to enqueue the work item. It should usually not be used
  10104. -//! directly, but if you want to, you can use it without using the pieces from the safe API.
  10105. -//!
  10106. -//! # The safe API
  10107. -//!
  10108. -//! The safe API is used via the [`Work`] struct and [`WorkItem`] traits. Furthermore, it also
  10109. -//! includes a trait called [`WorkItemPointer`], which is usually not used directly by the user.
  10110. -//!
  10111. -//! * The [`Work`] struct is the Rust wrapper for the C `work_struct` type.
  10112. -//! * The [`WorkItem`] trait is implemented for structs that can be enqueued to a workqueue.
  10113. -//! * The [`WorkItemPointer`] trait is implemented for the pointer type that points at a something
  10114. -//! that implements [`WorkItem`].
  10115. -//!
  10116. -//! ## Example
  10117. -//!
  10118. -//! This example defines a struct that holds an integer and can be scheduled on the workqueue. When
  10119. -//! the struct is executed, it will print the integer. Since there is only one `work_struct` field,
  10120. -//! we do not need to specify ids for the fields.
  10121. -//!
  10122. -//! ```
  10123. -//! use kernel::sync::Arc;
  10124. -//! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem};
  10125. -//!
  10126. -//! #[pin_data]
  10127. -//! struct MyStruct {
  10128. -//! value: i32,
  10129. -//! #[pin]
  10130. -//! work: Work<MyStruct>,
  10131. -//! }
  10132. -//!
  10133. -//! impl_has_work! {
  10134. -//! impl HasWork<Self> for MyStruct { self.work }
  10135. -//! }
  10136. -//!
  10137. -//! impl MyStruct {
  10138. -//! fn new(value: i32) -> Result<Arc<Self>> {
  10139. -//! Arc::pin_init(pin_init!(MyStruct {
  10140. -//! value,
  10141. -//! work <- new_work!("MyStruct::work"),
  10142. -//! }), GFP_KERNEL)
  10143. -//! }
  10144. -//! }
  10145. -//!
  10146. -//! impl WorkItem for MyStruct {
  10147. -//! type Pointer = Arc<MyStruct>;
  10148. -//!
  10149. -//! fn run(this: Arc<MyStruct>) {
  10150. -//! pr_info!("The value is: {}", this.value);
  10151. -//! }
  10152. -//! }
  10153. -//!
  10154. -//! /// This method will enqueue the struct for execution on the system workqueue, where its value
  10155. -//! /// will be printed.
  10156. -//! fn print_later(val: Arc<MyStruct>) {
  10157. -//! let _ = workqueue::system().enqueue(val);
  10158. -//! }
  10159. -//! ```
  10160. -//!
  10161. -//! The following example shows how multiple `work_struct` fields can be used:
  10162. -//!
  10163. -//! ```
  10164. -//! use kernel::sync::Arc;
  10165. -//! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem};
  10166. -//!
  10167. -//! #[pin_data]
  10168. -//! struct MyStruct {
  10169. -//! value_1: i32,
  10170. -//! value_2: i32,
  10171. -//! #[pin]
  10172. -//! work_1: Work<MyStruct, 1>,
  10173. -//! #[pin]
  10174. -//! work_2: Work<MyStruct, 2>,
  10175. -//! }
  10176. -//!
  10177. -//! impl_has_work! {
  10178. -//! impl HasWork<Self, 1> for MyStruct { self.work_1 }
  10179. -//! impl HasWork<Self, 2> for MyStruct { self.work_2 }
  10180. -//! }
  10181. -//!
  10182. -//! impl MyStruct {
  10183. -//! fn new(value_1: i32, value_2: i32) -> Result<Arc<Self>> {
  10184. -//! Arc::pin_init(pin_init!(MyStruct {
  10185. -//! value_1,
  10186. -//! value_2,
  10187. -//! work_1 <- new_work!("MyStruct::work_1"),
  10188. -//! work_2 <- new_work!("MyStruct::work_2"),
  10189. -//! }), GFP_KERNEL)
  10190. -//! }
  10191. -//! }
  10192. -//!
  10193. -//! impl WorkItem<1> for MyStruct {
  10194. -//! type Pointer = Arc<MyStruct>;
  10195. -//!
  10196. -//! fn run(this: Arc<MyStruct>) {
  10197. -//! pr_info!("The value is: {}", this.value_1);
  10198. -//! }
  10199. -//! }
  10200. -//!
  10201. -//! impl WorkItem<2> for MyStruct {
  10202. -//! type Pointer = Arc<MyStruct>;
  10203. -//!
  10204. -//! fn run(this: Arc<MyStruct>) {
  10205. -//! pr_info!("The second value is: {}", this.value_2);
  10206. -//! }
  10207. -//! }
  10208. -//!
  10209. -//! fn print_1_later(val: Arc<MyStruct>) {
  10210. -//! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 1>(val);
  10211. -//! }
  10212. -//!
  10213. -//! fn print_2_later(val: Arc<MyStruct>) {
  10214. -//! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 2>(val);
  10215. -//! }
  10216. -//! ```
  10217. -//!
  10218. -//! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h)
  10219. -
  10220. -use crate::alloc::{AllocError, Flags};
  10221. -use crate::{prelude::*, sync::Arc, sync::LockClassKey, types::Opaque};
  10222. -use core::marker::PhantomData;
  10223. -
  10224. -/// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
  10225. -#[macro_export]
  10226. -macro_rules! new_work {
  10227. - ($($name:literal)?) => {
  10228. - $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
  10229. - };
  10230. -}
  10231. -pub use new_work;
  10232. -
  10233. -/// A kernel work queue.
  10234. -///
  10235. -/// Wraps the kernel's C `struct workqueue_struct`.
  10236. -///
  10237. -/// It allows work items to be queued to run on thread pools managed by the kernel. Several are
  10238. -/// always available, for example, `system`, `system_highpri`, `system_long`, etc.
  10239. -#[repr(transparent)]
  10240. -pub struct Queue(Opaque<bindings::workqueue_struct>);
  10241. -
  10242. -// SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
  10243. -unsafe impl Send for Queue {}
  10244. -// SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
  10245. -unsafe impl Sync for Queue {}
  10246. -
  10247. -impl Queue {
  10248. - /// Use the provided `struct workqueue_struct` with Rust.
  10249. - ///
  10250. - /// # Safety
  10251. - ///
  10252. - /// The caller must ensure that the provided raw pointer is not dangling, that it points at a
  10253. - /// valid workqueue, and that it remains valid until the end of `'a`.
  10254. - pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue {
  10255. - // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The
  10256. - // caller promises that the pointer is not dangling.
  10257. - unsafe { &*(ptr as *const Queue) }
  10258. - }
  10259. -
  10260. - /// Enqueues a work item.
  10261. - ///
  10262. - /// This may fail if the work item is already enqueued in a workqueue.
  10263. - ///
  10264. - /// The work item will be submitted using `WORK_CPU_UNBOUND`.
  10265. - pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
  10266. - where
  10267. - W: RawWorkItem<ID> + Send + 'static,
  10268. - {
  10269. - let queue_ptr = self.0.get();
  10270. -
  10271. - // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
  10272. - // `__enqueue` requirements are not relevant since `W` is `Send` and static.
  10273. - //
  10274. - // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
  10275. - // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
  10276. - // closure.
  10277. - //
  10278. - // Furthermore, if the C workqueue code accesses the pointer after this call to
  10279. - // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
  10280. - // will have returned true. In this case, `__enqueue` promises that the raw pointer will
  10281. - // stay valid until we call the function pointer in the `work_struct`, so the access is ok.
  10282. - unsafe {
  10283. - w.__enqueue(move |work_ptr| {
  10284. - bindings::queue_work_on(
  10285. - bindings::wq_misc_consts_WORK_CPU_UNBOUND as _,
  10286. - queue_ptr,
  10287. - work_ptr,
  10288. - )
  10289. - })
  10290. - }
  10291. - }
  10292. -
  10293. - /// Tries to spawn the given function or closure as a work item.
  10294. - ///
  10295. - /// This method can fail because it allocates memory to store the work item.
  10296. - pub fn try_spawn<T: 'static + Send + FnOnce()>(
  10297. - &self,
  10298. - flags: Flags,
  10299. - func: T,
  10300. - ) -> Result<(), AllocError> {
  10301. - let init = pin_init!(ClosureWork {
  10302. - work <- new_work!("Queue::try_spawn"),
  10303. - func: Some(func),
  10304. - });
  10305. -
  10306. - self.enqueue(Box::pin_init(init, flags).map_err(|_| AllocError)?);
  10307. - Ok(())
  10308. - }
  10309. -}
  10310. -
  10311. -/// A helper type used in [`try_spawn`].
  10312. -///
  10313. -/// [`try_spawn`]: Queue::try_spawn
  10314. -#[pin_data]
  10315. -struct ClosureWork<T> {
  10316. - #[pin]
  10317. - work: Work<ClosureWork<T>>,
  10318. - func: Option<T>,
  10319. -}
  10320. -
  10321. -impl<T> ClosureWork<T> {
  10322. - fn project(self: Pin<&mut Self>) -> &mut Option<T> {
  10323. - // SAFETY: The `func` field is not structurally pinned.
  10324. - unsafe { &mut self.get_unchecked_mut().func }
  10325. - }
  10326. -}
  10327. -
  10328. -impl<T: FnOnce()> WorkItem for ClosureWork<T> {
  10329. - type Pointer = Pin<Box<Self>>;
  10330. -
  10331. - fn run(mut this: Pin<Box<Self>>) {
  10332. - if let Some(func) = this.as_mut().project().take() {
  10333. - (func)()
  10334. - }
  10335. - }
  10336. -}
  10337. -
  10338. -/// A raw work item.
  10339. -///
  10340. -/// This is the low-level trait that is designed for being as general as possible.
  10341. -///
  10342. -/// The `ID` parameter to this trait exists so that a single type can provide multiple
  10343. -/// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
  10344. -/// you will implement this trait once for each field, using a different id for each field. The
  10345. -/// actual value of the id is not important as long as you use different ids for different fields
  10346. -/// of the same struct. (Fields of different structs need not use different ids.)
  10347. -///
  10348. -/// Note that the id is used only to select the right method to call during compilation. It won't be
  10349. -/// part of the final executable.
  10350. -///
  10351. -/// # Safety
  10352. -///
  10353. -/// Implementers must ensure that any pointers passed to a `queue_work_on` closure by [`__enqueue`]
  10354. -/// remain valid for the duration specified in the guarantees section of the documentation for
  10355. -/// [`__enqueue`].
  10356. -///
  10357. -/// [`__enqueue`]: RawWorkItem::__enqueue
  10358. -pub unsafe trait RawWorkItem<const ID: u64> {
  10359. - /// The return type of [`Queue::enqueue`].
  10360. - type EnqueueOutput;
  10361. -
  10362. - /// Enqueues this work item on a queue using the provided `queue_work_on` method.
  10363. - ///
  10364. - /// # Guarantees
  10365. - ///
  10366. - /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
  10367. - /// valid `work_struct` for the duration of the call to the closure. If the closure returns
  10368. - /// true, then it is further guaranteed that the pointer remains valid until someone calls the
  10369. - /// function pointer stored in the `work_struct`.
  10370. - ///
  10371. - /// # Safety
  10372. - ///
  10373. - /// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
  10374. - ///
  10375. - /// If the work item type is annotated with any lifetimes, then you must not call the function
  10376. - /// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
  10377. - ///
  10378. - /// If the work item type is not [`Send`], then the function pointer must be called on the same
  10379. - /// thread as the call to `__enqueue`.
  10380. - unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
  10381. - where
  10382. - F: FnOnce(*mut bindings::work_struct) -> bool;
  10383. -}
  10384. -
  10385. -/// Defines the method that should be called directly when a work item is executed.
  10386. -///
  10387. -/// This trait is implemented by `Pin<Box<T>>` and [`Arc<T>`], and is mainly intended to be
  10388. -/// implemented for smart pointer types. For your own structs, you would implement [`WorkItem`]
  10389. -/// instead. The [`run`] method on this trait will usually just perform the appropriate
  10390. -/// `container_of` translation and then call into the [`run`][WorkItem::run] method from the
  10391. -/// [`WorkItem`] trait.
  10392. -///
  10393. -/// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
  10394. -///
  10395. -/// # Safety
  10396. -///
  10397. -/// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`]
  10398. -/// method of this trait as the function pointer.
  10399. -///
  10400. -/// [`__enqueue`]: RawWorkItem::__enqueue
  10401. -/// [`run`]: WorkItemPointer::run
  10402. -pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> {
  10403. - /// Run this work item.
  10404. - ///
  10405. - /// # Safety
  10406. - ///
  10407. - /// The provided `work_struct` pointer must originate from a previous call to [`__enqueue`]
  10408. - /// where the `queue_work_on` closure returned true, and the pointer must still be valid.
  10409. - ///
  10410. - /// [`__enqueue`]: RawWorkItem::__enqueue
  10411. - unsafe extern "C" fn run(ptr: *mut bindings::work_struct);
  10412. -}
  10413. -
  10414. -/// Defines the method that should be called when this work item is executed.
  10415. -///
  10416. -/// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
  10417. -pub trait WorkItem<const ID: u64 = 0> {
  10418. - /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or
  10419. - /// `Pin<Box<Self>>`.
  10420. - type Pointer: WorkItemPointer<ID>;
  10421. -
  10422. - /// The method that should be called when this work item is executed.
  10423. - fn run(this: Self::Pointer);
  10424. -}
  10425. -
  10426. -/// Links for a work item.
  10427. -///
  10428. -/// This struct contains a function pointer to the [`run`] function from the [`WorkItemPointer`]
  10429. -/// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue.
  10430. -///
  10431. -/// Wraps the kernel's C `struct work_struct`.
  10432. -///
  10433. -/// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
  10434. -///
  10435. -/// [`run`]: WorkItemPointer::run
  10436. -#[pin_data]
  10437. -#[repr(transparent)]
  10438. -pub struct Work<T: ?Sized, const ID: u64 = 0> {
  10439. - #[pin]
  10440. - work: Opaque<bindings::work_struct>,
  10441. - _inner: PhantomData<T>,
  10442. -}
  10443. -
  10444. -// SAFETY: Kernel work items are usable from any thread.
  10445. -//
  10446. -// We do not need to constrain `T` since the work item does not actually contain a `T`.
  10447. -unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {}
  10448. -// SAFETY: Kernel work items are usable from any thread.
  10449. -//
  10450. -// We do not need to constrain `T` since the work item does not actually contain a `T`.
  10451. -unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {}
  10452. -
  10453. -impl<T: ?Sized, const ID: u64> Work<T, ID> {
  10454. - /// Creates a new instance of [`Work`].
  10455. - #[inline]
  10456. - #[allow(clippy::new_ret_no_self)]
  10457. - pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self>
  10458. - where
  10459. - T: WorkItem<ID>,
  10460. - {
  10461. - pin_init!(Self {
  10462. - work <- Opaque::ffi_init(|slot| {
  10463. - // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as
  10464. - // the work item function.
  10465. - unsafe {
  10466. - bindings::init_work_with_key(
  10467. - slot,
  10468. - Some(T::Pointer::run),
  10469. - false,
  10470. - name.as_char_ptr(),
  10471. - key.as_ptr(),
  10472. - )
  10473. - }
  10474. - }),
  10475. - _inner: PhantomData,
  10476. - })
  10477. - }
  10478. -
  10479. - /// Get a pointer to the inner `work_struct`.
  10480. - ///
  10481. - /// # Safety
  10482. - ///
  10483. - /// The provided pointer must not be dangling and must be properly aligned. (But the memory
  10484. - /// need not be initialized.)
  10485. - #[inline]
  10486. - pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
  10487. - // SAFETY: The caller promises that the pointer is aligned and not dangling.
  10488. - //
  10489. - // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
  10490. - // the compiler does not complain that the `work` field is unused.
  10491. - unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) }
  10492. - }
  10493. -}
  10494. -
  10495. -/// Declares that a type has a [`Work<T, ID>`] field.
  10496. -///
  10497. -/// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro
  10498. -/// like this:
  10499. -///
  10500. -/// ```no_run
  10501. -/// use kernel::workqueue::{impl_has_work, Work};
  10502. -///
  10503. -/// struct MyWorkItem {
  10504. -/// work_field: Work<MyWorkItem, 1>,
  10505. -/// }
  10506. -///
  10507. -/// impl_has_work! {
  10508. -/// impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field }
  10509. -/// }
  10510. -/// ```
  10511. -///
  10512. -/// Note that since the [`Work`] type is annotated with an id, you can have several `work_struct`
  10513. -/// fields by using a different id for each one.
  10514. -///
  10515. -/// # Safety
  10516. -///
  10517. -/// The [`OFFSET`] constant must be the offset of a field in `Self` of type [`Work<T, ID>`]. The
  10518. -/// methods on this trait must have exactly the behavior that the definitions given below have.
  10519. -///
  10520. -/// [`impl_has_work!`]: crate::impl_has_work
  10521. -/// [`OFFSET`]: HasWork::OFFSET
  10522. -pub unsafe trait HasWork<T, const ID: u64 = 0> {
  10523. - /// The offset of the [`Work<T, ID>`] field.
  10524. - const OFFSET: usize;
  10525. -
  10526. - /// Returns the offset of the [`Work<T, ID>`] field.
  10527. - ///
  10528. - /// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not
  10529. - /// [`Sized`].
  10530. - ///
  10531. - /// [`OFFSET`]: HasWork::OFFSET
  10532. - #[inline]
  10533. - fn get_work_offset(&self) -> usize {
  10534. - Self::OFFSET
  10535. - }
  10536. -
  10537. - /// Returns a pointer to the [`Work<T, ID>`] field.
  10538. - ///
  10539. - /// # Safety
  10540. - ///
  10541. - /// The provided pointer must point at a valid struct of type `Self`.
  10542. - #[inline]
  10543. - unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> {
  10544. - // SAFETY: The caller promises that the pointer is valid.
  10545. - unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work<T, ID> }
  10546. - }
  10547. -
  10548. - /// Returns a pointer to the struct containing the [`Work<T, ID>`] field.
  10549. - ///
  10550. - /// # Safety
  10551. - ///
  10552. - /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`.
  10553. - #[inline]
  10554. - unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self
  10555. - where
  10556. - Self: Sized,
  10557. - {
  10558. - // SAFETY: The caller promises that the pointer points at a field of the right type in the
  10559. - // right kind of struct.
  10560. - unsafe { (ptr as *mut u8).sub(Self::OFFSET) as *mut Self }
  10561. - }
  10562. -}
  10563. -
  10564. -/// Used to safely implement the [`HasWork<T, ID>`] trait.
  10565. -///
  10566. -/// # Examples
  10567. -///
  10568. -/// ```
  10569. -/// use kernel::sync::Arc;
  10570. -/// use kernel::workqueue::{self, impl_has_work, Work};
  10571. -///
  10572. -/// struct MyStruct {
  10573. -/// work_field: Work<MyStruct, 17>,
  10574. -/// }
  10575. -///
  10576. -/// impl_has_work! {
  10577. -/// impl HasWork<MyStruct, 17> for MyStruct { self.work_field }
  10578. -/// }
  10579. -/// ```
  10580. -#[macro_export]
  10581. -macro_rules! impl_has_work {
  10582. - ($(impl$(<$($implarg:ident),*>)?
  10583. - HasWork<$work_type:ty $(, $id:tt)?>
  10584. - for $self:ident $(<$($selfarg:ident),*>)?
  10585. - { self.$field:ident }
  10586. - )*) => {$(
  10587. - // SAFETY: The implementation of `raw_get_work` only compiles if the field has the right
  10588. - // type.
  10589. - unsafe impl$(<$($implarg),*>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self $(<$($selfarg),*>)? {
  10590. - const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize;
  10591. -
  10592. - #[inline]
  10593. - unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> {
  10594. - // SAFETY: The caller promises that the pointer is not dangling.
  10595. - unsafe {
  10596. - ::core::ptr::addr_of_mut!((*ptr).$field)
  10597. - }
  10598. - }
  10599. - }
  10600. - )*};
  10601. -}
  10602. -pub use impl_has_work;
  10603. -
  10604. -impl_has_work! {
  10605. - impl<T> HasWork<Self> for ClosureWork<T> { self.work }
  10606. -}
  10607. -
  10608. -unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T>
  10609. -where
  10610. - T: WorkItem<ID, Pointer = Self>,
  10611. - T: HasWork<T, ID>,
  10612. -{
  10613. - unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
  10614. - // SAFETY: The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
  10615. - let ptr = ptr as *mut Work<T, ID>;
  10616. - // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
  10617. - let ptr = unsafe { T::work_container_of(ptr) };
  10618. - // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
  10619. - let arc = unsafe { Arc::from_raw(ptr) };
  10620. -
  10621. - T::run(arc)
  10622. - }
  10623. -}
  10624. -
  10625. -unsafe impl<T, const ID: u64> RawWorkItem<ID> for Arc<T>
  10626. -where
  10627. - T: WorkItem<ID, Pointer = Self>,
  10628. - T: HasWork<T, ID>,
  10629. -{
  10630. - type EnqueueOutput = Result<(), Self>;
  10631. -
  10632. - unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
  10633. - where
  10634. - F: FnOnce(*mut bindings::work_struct) -> bool,
  10635. - {
  10636. - // Casting between const and mut is not a problem as long as the pointer is a raw pointer.
  10637. - let ptr = Arc::into_raw(self).cast_mut();
  10638. -
  10639. - // SAFETY: Pointers into an `Arc` point at a valid value.
  10640. - let work_ptr = unsafe { T::raw_get_work(ptr) };
  10641. - // SAFETY: `raw_get_work` returns a pointer to a valid value.
  10642. - let work_ptr = unsafe { Work::raw_get(work_ptr) };
  10643. -
  10644. - if queue_work_on(work_ptr) {
  10645. - Ok(())
  10646. - } else {
  10647. - // SAFETY: The work queue has not taken ownership of the pointer.
  10648. - Err(unsafe { Arc::from_raw(ptr) })
  10649. - }
  10650. - }
  10651. -}
  10652. -
  10653. -unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<Box<T>>
  10654. -where
  10655. - T: WorkItem<ID, Pointer = Self>,
  10656. - T: HasWork<T, ID>,
  10657. -{
  10658. - unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
  10659. - // SAFETY: The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
  10660. - let ptr = ptr as *mut Work<T, ID>;
  10661. - // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
  10662. - let ptr = unsafe { T::work_container_of(ptr) };
  10663. - // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
  10664. - let boxed = unsafe { Box::from_raw(ptr) };
  10665. - // SAFETY: The box was already pinned when it was enqueued.
  10666. - let pinned = unsafe { Pin::new_unchecked(boxed) };
  10667. -
  10668. - T::run(pinned)
  10669. - }
  10670. -}
  10671. -
  10672. -unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<Box<T>>
  10673. -where
  10674. - T: WorkItem<ID, Pointer = Self>,
  10675. - T: HasWork<T, ID>,
  10676. -{
  10677. - type EnqueueOutput = ();
  10678. -
  10679. - unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
  10680. - where
  10681. - F: FnOnce(*mut bindings::work_struct) -> bool,
  10682. - {
  10683. - // SAFETY: We're not going to move `self` or any of its fields, so its okay to temporarily
  10684. - // remove the `Pin` wrapper.
  10685. - let boxed = unsafe { Pin::into_inner_unchecked(self) };
  10686. - let ptr = Box::into_raw(boxed);
  10687. -
  10688. - // SAFETY: Pointers into a `Box` point at a valid value.
  10689. - let work_ptr = unsafe { T::raw_get_work(ptr) };
  10690. - // SAFETY: `raw_get_work` returns a pointer to a valid value.
  10691. - let work_ptr = unsafe { Work::raw_get(work_ptr) };
  10692. -
  10693. - if !queue_work_on(work_ptr) {
  10694. - // SAFETY: This method requires exclusive ownership of the box, so it cannot be in a
  10695. - // workqueue.
  10696. - unsafe { ::core::hint::unreachable_unchecked() }
  10697. - }
  10698. - }
  10699. -}
  10700. -
  10701. -/// Returns the system work queue (`system_wq`).
  10702. -///
  10703. -/// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are
  10704. -/// users which expect relatively short queue flush time.
  10705. -///
  10706. -/// Callers shouldn't queue work items which can run for too long.
  10707. -pub fn system() -> &'static Queue {
  10708. - // SAFETY: `system_wq` is a C global, always available.
  10709. - unsafe { Queue::from_raw(bindings::system_wq) }
  10710. -}
  10711. -
  10712. -/// Returns the system high-priority work queue (`system_highpri_wq`).
  10713. -///
  10714. -/// It is similar to the one returned by [`system`] but for work items which require higher
  10715. -/// scheduling priority.
  10716. -pub fn system_highpri() -> &'static Queue {
  10717. - // SAFETY: `system_highpri_wq` is a C global, always available.
  10718. - unsafe { Queue::from_raw(bindings::system_highpri_wq) }
  10719. -}
  10720. -
  10721. -/// Returns the system work queue for potentially long-running work items (`system_long_wq`).
  10722. -///
  10723. -/// It is similar to the one returned by [`system`] but may host long running work items. Queue
  10724. -/// flushing might take relatively long.
  10725. -pub fn system_long() -> &'static Queue {
  10726. - // SAFETY: `system_long_wq` is a C global, always available.
  10727. - unsafe { Queue::from_raw(bindings::system_long_wq) }
  10728. -}
  10729. -
  10730. -/// Returns the system unbound work queue (`system_unbound_wq`).
  10731. -///
  10732. -/// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items
  10733. -/// are executed immediately as long as `max_active` limit is not reached and resources are
  10734. -/// available.
  10735. -pub fn system_unbound() -> &'static Queue {
  10736. - // SAFETY: `system_unbound_wq` is a C global, always available.
  10737. - unsafe { Queue::from_raw(bindings::system_unbound_wq) }
  10738. -}
  10739. -
  10740. -/// Returns the system freezable work queue (`system_freezable_wq`).
  10741. -///
  10742. -/// It is equivalent to the one returned by [`system`] except that it's freezable.
  10743. -///
  10744. -/// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
  10745. -/// items on the workqueue are drained and no new work item starts execution until thawed.
  10746. -pub fn system_freezable() -> &'static Queue {
  10747. - // SAFETY: `system_freezable_wq` is a C global, always available.
  10748. - unsafe { Queue::from_raw(bindings::system_freezable_wq) }
  10749. -}
  10750. -
  10751. -/// Returns the system power-efficient work queue (`system_power_efficient_wq`).
  10752. -///
  10753. -/// It is inclined towards saving power and is converted to "unbound" variants if the
  10754. -/// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one
  10755. -/// returned by [`system`].
  10756. -pub fn system_power_efficient() -> &'static Queue {
  10757. - // SAFETY: `system_power_efficient_wq` is a C global, always available.
  10758. - unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
  10759. -}
  10760. -
  10761. -/// Returns the system freezable power-efficient work queue (`system_freezable_power_efficient_wq`).
  10762. -///
  10763. -/// It is similar to the one returned by [`system_power_efficient`] except that is freezable.
  10764. -///
  10765. -/// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
  10766. -/// items on the workqueue are drained and no new work item starts execution until thawed.
  10767. -pub fn system_freezable_power_efficient() -> &'static Queue {
  10768. - // SAFETY: `system_freezable_power_efficient_wq` is a C global, always available.
  10769. - unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) }
  10770. -}
  10771. diff --git a/rust/macros/concat_idents.rs b/rust/macros/concat_idents.rs
  10772. deleted file mode 100644
  10773. --- a/rust/macros/concat_idents.rs
  10774. +++ /dev/null
  10775. @@ -1,23 +0,0 @@
  10776. -// SPDX-License-Identifier: GPL-2.0
  10777. -
  10778. -use proc_macro::{token_stream, Ident, TokenStream, TokenTree};
  10779. -
  10780. -use crate::helpers::expect_punct;
  10781. -
  10782. -fn expect_ident(it: &mut token_stream::IntoIter) -> Ident {
  10783. - if let Some(TokenTree::Ident(ident)) = it.next() {
  10784. - ident
  10785. - } else {
  10786. - panic!("Expected Ident")
  10787. - }
  10788. -}
  10789. -
  10790. -pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream {
  10791. - let mut it = ts.into_iter();
  10792. - let a = expect_ident(&mut it);
  10793. - assert_eq!(expect_punct(&mut it), ',');
  10794. - let b = expect_ident(&mut it);
  10795. - assert!(it.next().is_none(), "only two idents can be concatenated");
  10796. - let res = Ident::new(&format!("{a}{b}"), b.span());
  10797. - TokenStream::from_iter([TokenTree::Ident(res)])
  10798. -}
  10799. diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs
  10800. deleted file mode 100644
  10801. --- a/rust/macros/helpers.rs
  10802. +++ /dev/null
  10803. @@ -1,217 +0,0 @@
  10804. -// SPDX-License-Identifier: GPL-2.0
  10805. -
  10806. -use proc_macro::{token_stream, Group, TokenStream, TokenTree};
  10807. -
  10808. -pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
  10809. - if let Some(TokenTree::Ident(ident)) = it.next() {
  10810. - Some(ident.to_string())
  10811. - } else {
  10812. - None
  10813. - }
  10814. -}
  10815. -
  10816. -pub(crate) fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
  10817. - if let Some(TokenTree::Literal(literal)) = it.next() {
  10818. - Some(literal.to_string())
  10819. - } else {
  10820. - None
  10821. - }
  10822. -}
  10823. -
  10824. -pub(crate) fn try_string(it: &mut token_stream::IntoIter) -> Option<String> {
  10825. - try_literal(it).and_then(|string| {
  10826. - if string.starts_with('\"') && string.ends_with('\"') {
  10827. - let content = &string[1..string.len() - 1];
  10828. - if content.contains('\\') {
  10829. - panic!("Escape sequences in string literals not yet handled");
  10830. - }
  10831. - Some(content.to_string())
  10832. - } else if string.starts_with("r\"") {
  10833. - panic!("Raw string literals are not yet handled");
  10834. - } else {
  10835. - None
  10836. - }
  10837. - })
  10838. -}
  10839. -
  10840. -pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String {
  10841. - try_ident(it).expect("Expected Ident")
  10842. -}
  10843. -
  10844. -pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
  10845. - if let TokenTree::Punct(punct) = it.next().expect("Reached end of token stream for Punct") {
  10846. - punct.as_char()
  10847. - } else {
  10848. - panic!("Expected Punct");
  10849. - }
  10850. -}
  10851. -
  10852. -pub(crate) fn expect_string(it: &mut token_stream::IntoIter) -> String {
  10853. - try_string(it).expect("Expected string")
  10854. -}
  10855. -
  10856. -pub(crate) fn expect_string_ascii(it: &mut token_stream::IntoIter) -> String {
  10857. - let string = try_string(it).expect("Expected string");
  10858. - assert!(string.is_ascii(), "Expected ASCII string");
  10859. - string
  10860. -}
  10861. -
  10862. -pub(crate) fn expect_group(it: &mut token_stream::IntoIter) -> Group {
  10863. - if let TokenTree::Group(group) = it.next().expect("Reached end of token stream for Group") {
  10864. - group
  10865. - } else {
  10866. - panic!("Expected Group");
  10867. - }
  10868. -}
  10869. -
  10870. -pub(crate) fn expect_end(it: &mut token_stream::IntoIter) {
  10871. - if it.next().is_some() {
  10872. - panic!("Expected end");
  10873. - }
  10874. -}
  10875. -
  10876. -/// Parsed generics.
  10877. -///
  10878. -/// See the field documentation for an explanation what each of the fields represents.
  10879. -///
  10880. -/// # Examples
  10881. -///
  10882. -/// ```rust,ignore
  10883. -/// # let input = todo!();
  10884. -/// let (Generics { decl_generics, impl_generics, ty_generics }, rest) = parse_generics(input);
  10885. -/// quote! {
  10886. -/// struct Foo<$($decl_generics)*> {
  10887. -/// // ...
  10888. -/// }
  10889. -///
  10890. -/// impl<$impl_generics> Foo<$ty_generics> {
  10891. -/// fn foo() {
  10892. -/// // ...
  10893. -/// }
  10894. -/// }
  10895. -/// }
  10896. -/// ```
  10897. -pub(crate) struct Generics {
  10898. - /// The generics with bounds and default values (e.g. `T: Clone, const N: usize = 0`).
  10899. - ///
  10900. - /// Use this on type definitions e.g. `struct Foo<$decl_generics> ...` (or `union`/`enum`).
  10901. - pub(crate) decl_generics: Vec<TokenTree>,
  10902. - /// The generics with bounds (e.g. `T: Clone, const N: usize`).
  10903. - ///
  10904. - /// Use this on `impl` blocks e.g. `impl<$impl_generics> Trait for ...`.
  10905. - pub(crate) impl_generics: Vec<TokenTree>,
  10906. - /// The generics without bounds and without default values (e.g. `T, N`).
  10907. - ///
  10908. - /// Use this when you use the type that is declared with these generics e.g.
  10909. - /// `Foo<$ty_generics>`.
  10910. - pub(crate) ty_generics: Vec<TokenTree>,
  10911. -}
  10912. -
  10913. -/// Parses the given `TokenStream` into `Generics` and the rest.
  10914. -///
  10915. -/// The generics are not present in the rest, but a where clause might remain.
  10916. -pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec<TokenTree>) {
  10917. - // The generics with bounds and default values.
  10918. - let mut decl_generics = vec![];
  10919. - // `impl_generics`, the declared generics with their bounds.
  10920. - let mut impl_generics = vec![];
  10921. - // Only the names of the generics, without any bounds.
  10922. - let mut ty_generics = vec![];
  10923. - // Tokens not related to the generics e.g. the `where` token and definition.
  10924. - let mut rest = vec![];
  10925. - // The current level of `<`.
  10926. - let mut nesting = 0;
  10927. - let mut toks = input.into_iter();
  10928. - // If we are at the beginning of a generic parameter.
  10929. - let mut at_start = true;
  10930. - let mut skip_until_comma = false;
  10931. - while let Some(tt) = toks.next() {
  10932. - if nesting == 1 && matches!(&tt, TokenTree::Punct(p) if p.as_char() == '>') {
  10933. - // Found the end of the generics.
  10934. - break;
  10935. - } else if nesting >= 1 {
  10936. - decl_generics.push(tt.clone());
  10937. - }
  10938. - match tt.clone() {
  10939. - TokenTree::Punct(p) if p.as_char() == '<' => {
  10940. - if nesting >= 1 && !skip_until_comma {
  10941. - // This is inside of the generics and part of some bound.
  10942. - impl_generics.push(tt);
  10943. - }
  10944. - nesting += 1;
  10945. - }
  10946. - TokenTree::Punct(p) if p.as_char() == '>' => {
  10947. - // This is a parsing error, so we just end it here.
  10948. - if nesting == 0 {
  10949. - break;
  10950. - } else {
  10951. - nesting -= 1;
  10952. - if nesting >= 1 && !skip_until_comma {
  10953. - // We are still inside of the generics and part of some bound.
  10954. - impl_generics.push(tt);
  10955. - }
  10956. - }
  10957. - }
  10958. - TokenTree::Punct(p) if skip_until_comma && p.as_char() == ',' => {
  10959. - if nesting == 1 {
  10960. - impl_generics.push(tt.clone());
  10961. - impl_generics.push(tt);
  10962. - skip_until_comma = false;
  10963. - }
  10964. - }
  10965. - _ if !skip_until_comma => {
  10966. - match nesting {
  10967. - // If we haven't entered the generics yet, we still want to keep these tokens.
  10968. - 0 => rest.push(tt),
  10969. - 1 => {
  10970. - // Here depending on the token, it might be a generic variable name.
  10971. - match tt.clone() {
  10972. - TokenTree::Ident(i) if at_start && i.to_string() == "const" => {
  10973. - let Some(name) = toks.next() else {
  10974. - // Parsing error.
  10975. - break;
  10976. - };
  10977. - impl_generics.push(tt);
  10978. - impl_generics.push(name.clone());
  10979. - ty_generics.push(name.clone());
  10980. - decl_generics.push(name);
  10981. - at_start = false;
  10982. - }
  10983. - TokenTree::Ident(_) if at_start => {
  10984. - impl_generics.push(tt.clone());
  10985. - ty_generics.push(tt);
  10986. - at_start = false;
  10987. - }
  10988. - TokenTree::Punct(p) if p.as_char() == ',' => {
  10989. - impl_generics.push(tt.clone());
  10990. - ty_generics.push(tt);
  10991. - at_start = true;
  10992. - }
  10993. - // Lifetimes begin with `'`.
  10994. - TokenTree::Punct(p) if p.as_char() == '\'' && at_start => {
  10995. - impl_generics.push(tt.clone());
  10996. - ty_generics.push(tt);
  10997. - }
  10998. - // Generics can have default values, we skip these.
  10999. - TokenTree::Punct(p) if p.as_char() == '=' => {
  11000. - skip_until_comma = true;
  11001. - }
  11002. - _ => impl_generics.push(tt),
  11003. - }
  11004. - }
  11005. - _ => impl_generics.push(tt),
  11006. - }
  11007. - }
  11008. - _ => {}
  11009. - }
  11010. - }
  11011. - rest.extend(toks);
  11012. - (
  11013. - Generics {
  11014. - impl_generics,
  11015. - decl_generics,
  11016. - ty_generics,
  11017. - },
  11018. - rest,
  11019. - )
  11020. -}
  11021. diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
  11022. deleted file mode 100644
  11023. --- a/rust/macros/lib.rs
  11024. +++ /dev/null
  11025. @@ -1,395 +0,0 @@
  11026. -// SPDX-License-Identifier: GPL-2.0
  11027. -
  11028. -//! Crate for all kernel procedural macros.
  11029. -
  11030. -#[macro_use]
  11031. -mod quote;
  11032. -mod concat_idents;
  11033. -mod helpers;
  11034. -mod module;
  11035. -mod paste;
  11036. -mod pin_data;
  11037. -mod pinned_drop;
  11038. -mod vtable;
  11039. -mod zeroable;
  11040. -
  11041. -use proc_macro::TokenStream;
  11042. -
  11043. -/// Declares a kernel module.
  11044. -///
  11045. -/// The `type` argument should be a type which implements the [`Module`]
  11046. -/// trait. Also accepts various forms of kernel metadata.
  11047. -///
  11048. -/// C header: [`include/linux/moduleparam.h`](srctree/include/linux/moduleparam.h)
  11049. -///
  11050. -/// [`Module`]: ../kernel/trait.Module.html
  11051. -///
  11052. -/// # Examples
  11053. -///
  11054. -/// ```ignore
  11055. -/// use kernel::prelude::*;
  11056. -///
  11057. -/// module!{
  11058. -/// type: MyModule,
  11059. -/// name: "my_kernel_module",
  11060. -/// author: "Rust for Linux Contributors",
  11061. -/// description: "My very own kernel module!",
  11062. -/// license: "GPL",
  11063. -/// }
  11064. -///
  11065. -/// struct MyModule;
  11066. -///
  11067. -/// impl kernel::Module for MyModule {
  11068. -/// fn init() -> Result<Self> {
  11069. -/// // If the parameter is writeable, then the kparam lock must be
  11070. -/// // taken to read the parameter:
  11071. -/// {
  11072. -/// let lock = THIS_MODULE.kernel_param_lock();
  11073. -/// pr_info!("i32 param is: {}\n", writeable_i32.read(&lock));
  11074. -/// }
  11075. -/// // If the parameter is read only, it can be read without locking
  11076. -/// // the kernel parameters:
  11077. -/// pr_info!("i32 param is: {}\n", my_i32.read());
  11078. -/// Ok(Self)
  11079. -/// }
  11080. -/// }
  11081. -/// ```
  11082. -///
  11083. -/// # Supported argument types
  11084. -/// - `type`: type which implements the [`Module`] trait (required).
  11085. -/// - `name`: byte array of the name of the kernel module (required).
  11086. -/// - `author`: byte array of the author of the kernel module.
  11087. -/// - `description`: byte array of the description of the kernel module.
  11088. -/// - `license`: byte array of the license of the kernel module (required).
  11089. -/// - `alias`: byte array of alias name of the kernel module.
  11090. -#[proc_macro]
  11091. -pub fn module(ts: TokenStream) -> TokenStream {
  11092. - module::module(ts)
  11093. -}
  11094. -
  11095. -/// Declares or implements a vtable trait.
  11096. -///
  11097. -/// Linux's use of pure vtables is very close to Rust traits, but they differ
  11098. -/// in how unimplemented functions are represented. In Rust, traits can provide
  11099. -/// default implementation for all non-required methods (and the default
  11100. -/// implementation could just return `Error::EINVAL`); Linux typically use C
  11101. -/// `NULL` pointers to represent these functions.
  11102. -///
  11103. -/// This attribute closes that gap. A trait can be annotated with the
  11104. -/// `#[vtable]` attribute. Implementers of the trait will then also have to
  11105. -/// annotate the trait with `#[vtable]`. This attribute generates a `HAS_*`
  11106. -/// associated constant bool for each method in the trait that is set to true if
  11107. -/// the implementer has overridden the associated method.
  11108. -///
  11109. -/// For a trait method to be optional, it must have a default implementation.
  11110. -/// This is also the case for traits annotated with `#[vtable]`, but in this
  11111. -/// case the default implementation will never be executed. The reason for this
  11112. -/// is that the functions will be called through function pointers installed in
  11113. -/// C side vtables. When an optional method is not implemented on a `#[vtable]`
  11114. -/// trait, a NULL entry is installed in the vtable. Thus the default
  11115. -/// implementation is never called. Since these traits are not designed to be
  11116. -/// used on the Rust side, it should not be possible to call the default
  11117. -/// implementation. This is done to ensure that we call the vtable methods
  11118. -/// through the C vtable, and not through the Rust vtable. Therefore, the
  11119. -/// default implementation should call `kernel::build_error`, which prevents
  11120. -/// calls to this function at compile time:
  11121. -///
  11122. -/// ```compile_fail
  11123. -/// # use kernel::error::VTABLE_DEFAULT_ERROR;
  11124. -/// kernel::build_error(VTABLE_DEFAULT_ERROR)
  11125. -/// ```
  11126. -///
  11127. -/// Note that you might need to import [`kernel::error::VTABLE_DEFAULT_ERROR`].
  11128. -///
  11129. -/// This macro should not be used when all functions are required.
  11130. -///
  11131. -/// # Examples
  11132. -///
  11133. -/// ```ignore
  11134. -/// use kernel::error::VTABLE_DEFAULT_ERROR;
  11135. -/// use kernel::prelude::*;
  11136. -///
  11137. -/// // Declares a `#[vtable]` trait
  11138. -/// #[vtable]
  11139. -/// pub trait Operations: Send + Sync + Sized {
  11140. -/// fn foo(&self) -> Result<()> {
  11141. -/// kernel::build_error(VTABLE_DEFAULT_ERROR)
  11142. -/// }
  11143. -///
  11144. -/// fn bar(&self) -> Result<()> {
  11145. -/// kernel::build_error(VTABLE_DEFAULT_ERROR)
  11146. -/// }
  11147. -/// }
  11148. -///
  11149. -/// struct Foo;
  11150. -///
  11151. -/// // Implements the `#[vtable]` trait
  11152. -/// #[vtable]
  11153. -/// impl Operations for Foo {
  11154. -/// fn foo(&self) -> Result<()> {
  11155. -/// # Err(EINVAL)
  11156. -/// // ...
  11157. -/// }
  11158. -/// }
  11159. -///
  11160. -/// assert_eq!(<Foo as Operations>::HAS_FOO, true);
  11161. -/// assert_eq!(<Foo as Operations>::HAS_BAR, false);
  11162. -/// ```
  11163. -///
  11164. -/// [`kernel::error::VTABLE_DEFAULT_ERROR`]: ../kernel/error/constant.VTABLE_DEFAULT_ERROR.html
  11165. -#[proc_macro_attribute]
  11166. -pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream {
  11167. - vtable::vtable(attr, ts)
  11168. -}
  11169. -
  11170. -/// Concatenate two identifiers.
  11171. -///
  11172. -/// This is useful in macros that need to declare or reference items with names
  11173. -/// starting with a fixed prefix and ending in a user specified name. The resulting
  11174. -/// identifier has the span of the second argument.
  11175. -///
  11176. -/// # Examples
  11177. -///
  11178. -/// ```ignore
  11179. -/// use kernel::macro::concat_idents;
  11180. -///
  11181. -/// macro_rules! pub_no_prefix {
  11182. -/// ($prefix:ident, $($newname:ident),+) => {
  11183. -/// $(pub(crate) const $newname: u32 = kernel::macros::concat_idents!($prefix, $newname);)+
  11184. -/// };
  11185. -/// }
  11186. -///
  11187. -/// pub_no_prefix!(
  11188. -/// binder_driver_return_protocol_,
  11189. -/// BR_OK,
  11190. -/// BR_ERROR,
  11191. -/// BR_TRANSACTION,
  11192. -/// BR_REPLY,
  11193. -/// BR_DEAD_REPLY,
  11194. -/// BR_TRANSACTION_COMPLETE,
  11195. -/// BR_INCREFS,
  11196. -/// BR_ACQUIRE,
  11197. -/// BR_RELEASE,
  11198. -/// BR_DECREFS,
  11199. -/// BR_NOOP,
  11200. -/// BR_SPAWN_LOOPER,
  11201. -/// BR_DEAD_BINDER,
  11202. -/// BR_CLEAR_DEATH_NOTIFICATION_DONE,
  11203. -/// BR_FAILED_REPLY
  11204. -/// );
  11205. -///
  11206. -/// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK);
  11207. -/// ```
  11208. -#[proc_macro]
  11209. -pub fn concat_idents(ts: TokenStream) -> TokenStream {
  11210. - concat_idents::concat_idents(ts)
  11211. -}
  11212. -
  11213. -/// Used to specify the pinning information of the fields of a struct.
  11214. -///
  11215. -/// This is somewhat similar in purpose as
  11216. -/// [pin-project-lite](https://crates.io/crates/pin-project-lite).
  11217. -/// Place this macro on a struct definition and then `#[pin]` in front of the attributes of each
  11218. -/// field you want to structurally pin.
  11219. -///
  11220. -/// This macro enables the use of the [`pin_init!`] macro. When pin-initializing a `struct`,
  11221. -/// then `#[pin]` directs the type of initializer that is required.
  11222. -///
  11223. -/// If your `struct` implements `Drop`, then you need to add `PinnedDrop` as arguments to this
  11224. -/// macro, and change your `Drop` implementation to `PinnedDrop` annotated with
  11225. -/// `#[`[`macro@pinned_drop`]`]`, since dropping pinned values requires extra care.
  11226. -///
  11227. -/// # Examples
  11228. -///
  11229. -/// ```rust,ignore
  11230. -/// #[pin_data]
  11231. -/// struct DriverData {
  11232. -/// #[pin]
  11233. -/// queue: Mutex<Vec<Command>>,
  11234. -/// buf: Box<[u8; 1024 * 1024]>,
  11235. -/// }
  11236. -/// ```
  11237. -///
  11238. -/// ```rust,ignore
  11239. -/// #[pin_data(PinnedDrop)]
  11240. -/// struct DriverData {
  11241. -/// #[pin]
  11242. -/// queue: Mutex<Vec<Command>>,
  11243. -/// buf: Box<[u8; 1024 * 1024]>,
  11244. -/// raw_info: *mut Info,
  11245. -/// }
  11246. -///
  11247. -/// #[pinned_drop]
  11248. -/// impl PinnedDrop for DriverData {
  11249. -/// fn drop(self: Pin<&mut Self>) {
  11250. -/// unsafe { bindings::destroy_info(self.raw_info) };
  11251. -/// }
  11252. -/// }
  11253. -/// ```
  11254. -///
  11255. -/// [`pin_init!`]: ../kernel/macro.pin_init.html
  11256. -// ^ cannot use direct link, since `kernel` is not a dependency of `macros`.
  11257. -#[proc_macro_attribute]
  11258. -pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
  11259. - pin_data::pin_data(inner, item)
  11260. -}
  11261. -
  11262. -/// Used to implement `PinnedDrop` safely.
  11263. -///
  11264. -/// Only works on structs that are annotated via `#[`[`macro@pin_data`]`]`.
  11265. -///
  11266. -/// # Examples
  11267. -///
  11268. -/// ```rust,ignore
  11269. -/// #[pin_data(PinnedDrop)]
  11270. -/// struct DriverData {
  11271. -/// #[pin]
  11272. -/// queue: Mutex<Vec<Command>>,
  11273. -/// buf: Box<[u8; 1024 * 1024]>,
  11274. -/// raw_info: *mut Info,
  11275. -/// }
  11276. -///
  11277. -/// #[pinned_drop]
  11278. -/// impl PinnedDrop for DriverData {
  11279. -/// fn drop(self: Pin<&mut Self>) {
  11280. -/// unsafe { bindings::destroy_info(self.raw_info) };
  11281. -/// }
  11282. -/// }
  11283. -/// ```
  11284. -#[proc_macro_attribute]
  11285. -pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
  11286. - pinned_drop::pinned_drop(args, input)
  11287. -}
  11288. -
  11289. -/// Paste identifiers together.
  11290. -///
  11291. -/// Within the `paste!` macro, identifiers inside `[<` and `>]` are concatenated together to form a
  11292. -/// single identifier.
  11293. -///
  11294. -/// This is similar to the [`paste`] crate, but with pasting feature limited to identifiers and
  11295. -/// literals (lifetimes and documentation strings are not supported). There is a difference in
  11296. -/// supported modifiers as well.
  11297. -///
  11298. -/// # Example
  11299. -///
  11300. -/// ```ignore
  11301. -/// use kernel::macro::paste;
  11302. -///
  11303. -/// macro_rules! pub_no_prefix {
  11304. -/// ($prefix:ident, $($newname:ident),+) => {
  11305. -/// paste! {
  11306. -/// $(pub(crate) const $newname: u32 = [<$prefix $newname>];)+
  11307. -/// }
  11308. -/// };
  11309. -/// }
  11310. -///
  11311. -/// pub_no_prefix!(
  11312. -/// binder_driver_return_protocol_,
  11313. -/// BR_OK,
  11314. -/// BR_ERROR,
  11315. -/// BR_TRANSACTION,
  11316. -/// BR_REPLY,
  11317. -/// BR_DEAD_REPLY,
  11318. -/// BR_TRANSACTION_COMPLETE,
  11319. -/// BR_INCREFS,
  11320. -/// BR_ACQUIRE,
  11321. -/// BR_RELEASE,
  11322. -/// BR_DECREFS,
  11323. -/// BR_NOOP,
  11324. -/// BR_SPAWN_LOOPER,
  11325. -/// BR_DEAD_BINDER,
  11326. -/// BR_CLEAR_DEATH_NOTIFICATION_DONE,
  11327. -/// BR_FAILED_REPLY
  11328. -/// );
  11329. -///
  11330. -/// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK);
  11331. -/// ```
  11332. -///
  11333. -/// # Modifiers
  11334. -///
  11335. -/// For each identifier, it is possible to attach one or multiple modifiers to
  11336. -/// it.
  11337. -///
  11338. -/// Currently supported modifiers are:
  11339. -/// * `span`: change the span of concatenated identifier to the span of the specified token. By
  11340. -/// default the span of the `[< >]` group is used.
  11341. -/// * `lower`: change the identifier to lower case.
  11342. -/// * `upper`: change the identifier to upper case.
  11343. -///
  11344. -/// ```ignore
  11345. -/// use kernel::macro::paste;
  11346. -///
  11347. -/// macro_rules! pub_no_prefix {
  11348. -/// ($prefix:ident, $($newname:ident),+) => {
  11349. -/// kernel::macros::paste! {
  11350. -/// $(pub(crate) const fn [<$newname:lower:span>]: u32 = [<$prefix $newname:span>];)+
  11351. -/// }
  11352. -/// };
  11353. -/// }
  11354. -///
  11355. -/// pub_no_prefix!(
  11356. -/// binder_driver_return_protocol_,
  11357. -/// BR_OK,
  11358. -/// BR_ERROR,
  11359. -/// BR_TRANSACTION,
  11360. -/// BR_REPLY,
  11361. -/// BR_DEAD_REPLY,
  11362. -/// BR_TRANSACTION_COMPLETE,
  11363. -/// BR_INCREFS,
  11364. -/// BR_ACQUIRE,
  11365. -/// BR_RELEASE,
  11366. -/// BR_DECREFS,
  11367. -/// BR_NOOP,
  11368. -/// BR_SPAWN_LOOPER,
  11369. -/// BR_DEAD_BINDER,
  11370. -/// BR_CLEAR_DEATH_NOTIFICATION_DONE,
  11371. -/// BR_FAILED_REPLY
  11372. -/// );
  11373. -///
  11374. -/// assert_eq!(br_ok(), binder_driver_return_protocol_BR_OK);
  11375. -/// ```
  11376. -///
  11377. -/// # Literals
  11378. -///
  11379. -/// Literals can also be concatenated with other identifiers:
  11380. -///
  11381. -/// ```ignore
  11382. -/// macro_rules! create_numbered_fn {
  11383. -/// ($name:literal, $val:literal) => {
  11384. -/// kernel::macros::paste! {
  11385. -/// fn [<some_ $name _fn $val>]() -> u32 { $val }
  11386. -/// }
  11387. -/// };
  11388. -/// }
  11389. -///
  11390. -/// create_numbered_fn!("foo", 100);
  11391. -///
  11392. -/// assert_eq!(some_foo_fn100(), 100)
  11393. -/// ```
  11394. -///
  11395. -/// [`paste`]: https://docs.rs/paste/
  11396. -#[proc_macro]
  11397. -pub fn paste(input: TokenStream) -> TokenStream {
  11398. - let mut tokens = input.into_iter().collect();
  11399. - paste::expand(&mut tokens);
  11400. - tokens.into_iter().collect()
  11401. -}
  11402. -
  11403. -/// Derives the [`Zeroable`] trait for the given struct.
  11404. -///
  11405. -/// This can only be used for structs where every field implements the [`Zeroable`] trait.
  11406. -///
  11407. -/// # Examples
  11408. -///
  11409. -/// ```rust,ignore
  11410. -/// #[derive(Zeroable)]
  11411. -/// pub struct DriverData {
  11412. -/// id: i64,
  11413. -/// buf_ptr: *mut u8,
  11414. -/// len: usize,
  11415. -/// }
  11416. -/// ```
  11417. -#[proc_macro_derive(Zeroable)]
  11418. -pub fn derive_zeroable(input: TokenStream) -> TokenStream {
  11419. - zeroable::derive(input)
  11420. -}
  11421. diff --git a/rust/macros/module.rs b/rust/macros/module.rs
  11422. deleted file mode 100644
  11423. --- a/rust/macros/module.rs
  11424. +++ /dev/null
  11425. @@ -1,351 +0,0 @@
  11426. -// SPDX-License-Identifier: GPL-2.0
  11427. -
  11428. -use crate::helpers::*;
  11429. -use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
  11430. -use std::fmt::Write;
  11431. -
  11432. -fn expect_string_array(it: &mut token_stream::IntoIter) -> Vec<String> {
  11433. - let group = expect_group(it);
  11434. - assert_eq!(group.delimiter(), Delimiter::Bracket);
  11435. - let mut values = Vec::new();
  11436. - let mut it = group.stream().into_iter();
  11437. -
  11438. - while let Some(val) = try_string(&mut it) {
  11439. - assert!(val.is_ascii(), "Expected ASCII string");
  11440. - values.push(val);
  11441. - match it.next() {
  11442. - Some(TokenTree::Punct(punct)) => assert_eq!(punct.as_char(), ','),
  11443. - None => break,
  11444. - _ => panic!("Expected ',' or end of array"),
  11445. - }
  11446. - }
  11447. - values
  11448. -}
  11449. -
  11450. -struct ModInfoBuilder<'a> {
  11451. - module: &'a str,
  11452. - counter: usize,
  11453. - buffer: String,
  11454. -}
  11455. -
  11456. -impl<'a> ModInfoBuilder<'a> {
  11457. - fn new(module: &'a str) -> Self {
  11458. - ModInfoBuilder {
  11459. - module,
  11460. - counter: 0,
  11461. - buffer: String::new(),
  11462. - }
  11463. - }
  11464. -
  11465. - fn emit_base(&mut self, field: &str, content: &str, builtin: bool) {
  11466. - let string = if builtin {
  11467. - // Built-in modules prefix their modinfo strings by `module.`.
  11468. - format!(
  11469. - "{module}.{field}={content}\0",
  11470. - module = self.module,
  11471. - field = field,
  11472. - content = content
  11473. - )
  11474. - } else {
  11475. - // Loadable modules' modinfo strings go as-is.
  11476. - format!("{field}={content}\0", field = field, content = content)
  11477. - };
  11478. -
  11479. - write!(
  11480. - &mut self.buffer,
  11481. - "
  11482. - {cfg}
  11483. - #[doc(hidden)]
  11484. - #[link_section = \".modinfo\"]
  11485. - #[used]
  11486. - pub static __{module}_{counter}: [u8; {length}] = *{string};
  11487. - ",
  11488. - cfg = if builtin {
  11489. - "#[cfg(not(MODULE))]"
  11490. - } else {
  11491. - "#[cfg(MODULE)]"
  11492. - },
  11493. - module = self.module.to_uppercase(),
  11494. - counter = self.counter,
  11495. - length = string.len(),
  11496. - string = Literal::byte_string(string.as_bytes()),
  11497. - )
  11498. - .unwrap();
  11499. -
  11500. - self.counter += 1;
  11501. - }
  11502. -
  11503. - fn emit_only_builtin(&mut self, field: &str, content: &str) {
  11504. - self.emit_base(field, content, true)
  11505. - }
  11506. -
  11507. - fn emit_only_loadable(&mut self, field: &str, content: &str) {
  11508. - self.emit_base(field, content, false)
  11509. - }
  11510. -
  11511. - fn emit(&mut self, field: &str, content: &str) {
  11512. - self.emit_only_builtin(field, content);
  11513. - self.emit_only_loadable(field, content);
  11514. - }
  11515. -}
  11516. -
  11517. -#[derive(Debug, Default)]
  11518. -struct ModuleInfo {
  11519. - type_: String,
  11520. - license: String,
  11521. - name: String,
  11522. - author: Option<String>,
  11523. - description: Option<String>,
  11524. - alias: Option<Vec<String>>,
  11525. -}
  11526. -
  11527. -impl ModuleInfo {
  11528. - fn parse(it: &mut token_stream::IntoIter) -> Self {
  11529. - let mut info = ModuleInfo::default();
  11530. -
  11531. - const EXPECTED_KEYS: &[&str] =
  11532. - &["type", "name", "author", "description", "license", "alias"];
  11533. - const REQUIRED_KEYS: &[&str] = &["type", "name", "license"];
  11534. - let mut seen_keys = Vec::new();
  11535. -
  11536. - loop {
  11537. - let key = match it.next() {
  11538. - Some(TokenTree::Ident(ident)) => ident.to_string(),
  11539. - Some(_) => panic!("Expected Ident or end"),
  11540. - None => break,
  11541. - };
  11542. -
  11543. - if seen_keys.contains(&key) {
  11544. - panic!(
  11545. - "Duplicated key \"{}\". Keys can only be specified once.",
  11546. - key
  11547. - );
  11548. - }
  11549. -
  11550. - assert_eq!(expect_punct(it), ':');
  11551. -
  11552. - match key.as_str() {
  11553. - "type" => info.type_ = expect_ident(it),
  11554. - "name" => info.name = expect_string_ascii(it),
  11555. - "author" => info.author = Some(expect_string(it)),
  11556. - "description" => info.description = Some(expect_string(it)),
  11557. - "license" => info.license = expect_string_ascii(it),
  11558. - "alias" => info.alias = Some(expect_string_array(it)),
  11559. - _ => panic!(
  11560. - "Unknown key \"{}\". Valid keys are: {:?}.",
  11561. - key, EXPECTED_KEYS
  11562. - ),
  11563. - }
  11564. -
  11565. - assert_eq!(expect_punct(it), ',');
  11566. -
  11567. - seen_keys.push(key);
  11568. - }
  11569. -
  11570. - expect_end(it);
  11571. -
  11572. - for key in REQUIRED_KEYS {
  11573. - if !seen_keys.iter().any(|e| e == key) {
  11574. - panic!("Missing required key \"{}\".", key);
  11575. - }
  11576. - }
  11577. -
  11578. - let mut ordered_keys: Vec<&str> = Vec::new();
  11579. - for key in EXPECTED_KEYS {
  11580. - if seen_keys.iter().any(|e| e == key) {
  11581. - ordered_keys.push(key);
  11582. - }
  11583. - }
  11584. -
  11585. - if seen_keys != ordered_keys {
  11586. - panic!(
  11587. - "Keys are not ordered as expected. Order them like: {:?}.",
  11588. - ordered_keys
  11589. - );
  11590. - }
  11591. -
  11592. - info
  11593. - }
  11594. -}
  11595. -
  11596. -pub(crate) fn module(ts: TokenStream) -> TokenStream {
  11597. - let mut it = ts.into_iter();
  11598. -
  11599. - let info = ModuleInfo::parse(&mut it);
  11600. -
  11601. - let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
  11602. - if let Some(author) = info.author {
  11603. - modinfo.emit("author", &author);
  11604. - }
  11605. - if let Some(description) = info.description {
  11606. - modinfo.emit("description", &description);
  11607. - }
  11608. - modinfo.emit("license", &info.license);
  11609. - if let Some(aliases) = info.alias {
  11610. - for alias in aliases {
  11611. - modinfo.emit("alias", &alias);
  11612. - }
  11613. - }
  11614. -
  11615. - // Built-in modules also export the `file` modinfo string.
  11616. - let file =
  11617. - std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE environmental variable");
  11618. - modinfo.emit_only_builtin("file", &file);
  11619. -
  11620. - format!(
  11621. - "
  11622. - /// The module name.
  11623. - ///
  11624. - /// Used by the printing macros, e.g. [`info!`].
  11625. - const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
  11626. -
  11627. - // SAFETY: `__this_module` is constructed by the kernel at load time and will not be
  11628. - // freed until the module is unloaded.
  11629. - #[cfg(MODULE)]
  11630. - static THIS_MODULE: kernel::ThisModule = unsafe {{
  11631. - extern \"C\" {{
  11632. - static __this_module: kernel::types::Opaque<kernel::bindings::module>;
  11633. - }}
  11634. -
  11635. - kernel::ThisModule::from_ptr(__this_module.get())
  11636. - }};
  11637. - #[cfg(not(MODULE))]
  11638. - static THIS_MODULE: kernel::ThisModule = unsafe {{
  11639. - kernel::ThisModule::from_ptr(core::ptr::null_mut())
  11640. - }};
  11641. -
  11642. - // Double nested modules, since then nobody can access the public items inside.
  11643. - mod __module_init {{
  11644. - mod __module_init {{
  11645. - use super::super::{type_};
  11646. -
  11647. - /// The \"Rust loadable module\" mark.
  11648. - //
  11649. - // This may be best done another way later on, e.g. as a new modinfo
  11650. - // key or a new section. For the moment, keep it simple.
  11651. - #[cfg(MODULE)]
  11652. - #[doc(hidden)]
  11653. - #[used]
  11654. - static __IS_RUST_MODULE: () = ();
  11655. -
  11656. - static mut __MOD: Option<{type_}> = None;
  11657. -
  11658. - // Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
  11659. - /// # Safety
  11660. - ///
  11661. - /// This function must not be called after module initialization, because it may be
  11662. - /// freed after that completes.
  11663. - #[cfg(MODULE)]
  11664. - #[doc(hidden)]
  11665. - #[no_mangle]
  11666. - #[link_section = \".init.text\"]
  11667. - pub unsafe extern \"C\" fn init_module() -> core::ffi::c_int {{
  11668. - // SAFETY: This function is inaccessible to the outside due to the double
  11669. - // module wrapping it. It is called exactly once by the C side via its
  11670. - // unique name.
  11671. - unsafe {{ __init() }}
  11672. - }}
  11673. -
  11674. - #[cfg(MODULE)]
  11675. - #[doc(hidden)]
  11676. - #[no_mangle]
  11677. - pub extern \"C\" fn cleanup_module() {{
  11678. - // SAFETY:
  11679. - // - This function is inaccessible to the outside due to the double
  11680. - // module wrapping it. It is called exactly once by the C side via its
  11681. - // unique name,
  11682. - // - furthermore it is only called after `init_module` has returned `0`
  11683. - // (which delegates to `__init`).
  11684. - unsafe {{ __exit() }}
  11685. - }}
  11686. -
  11687. - // Built-in modules are initialized through an initcall pointer
  11688. - // and the identifiers need to be unique.
  11689. - #[cfg(not(MODULE))]
  11690. - #[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
  11691. - #[doc(hidden)]
  11692. - #[link_section = \"{initcall_section}\"]
  11693. - #[used]
  11694. - pub static __{name}_initcall: extern \"C\" fn() -> core::ffi::c_int = __{name}_init;
  11695. -
  11696. - #[cfg(not(MODULE))]
  11697. - #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
  11698. - core::arch::global_asm!(
  11699. - r#\".section \"{initcall_section}\", \"a\"
  11700. - __{name}_initcall:
  11701. - .long __{name}_init - .
  11702. - .previous
  11703. - \"#
  11704. - );
  11705. -
  11706. - #[cfg(not(MODULE))]
  11707. - #[doc(hidden)]
  11708. - #[no_mangle]
  11709. - pub extern \"C\" fn __{name}_init() -> core::ffi::c_int {{
  11710. - // SAFETY: This function is inaccessible to the outside due to the double
  11711. - // module wrapping it. It is called exactly once by the C side via its
  11712. - // placement above in the initcall section.
  11713. - unsafe {{ __init() }}
  11714. - }}
  11715. -
  11716. - #[cfg(not(MODULE))]
  11717. - #[doc(hidden)]
  11718. - #[no_mangle]
  11719. - pub extern \"C\" fn __{name}_exit() {{
  11720. - // SAFETY:
  11721. - // - This function is inaccessible to the outside due to the double
  11722. - // module wrapping it. It is called exactly once by the C side via its
  11723. - // unique name,
  11724. - // - furthermore it is only called after `__{name}_init` has returned `0`
  11725. - // (which delegates to `__init`).
  11726. - unsafe {{ __exit() }}
  11727. - }}
  11728. -
  11729. - /// # Safety
  11730. - ///
  11731. - /// This function must only be called once.
  11732. - unsafe fn __init() -> core::ffi::c_int {{
  11733. - match <{type_} as kernel::Module>::init(&super::super::THIS_MODULE) {{
  11734. - Ok(m) => {{
  11735. - // SAFETY: No data race, since `__MOD` can only be accessed by this
  11736. - // module and there only `__init` and `__exit` access it. These
  11737. - // functions are only called once and `__exit` cannot be called
  11738. - // before or during `__init`.
  11739. - unsafe {{
  11740. - __MOD = Some(m);
  11741. - }}
  11742. - return 0;
  11743. - }}
  11744. - Err(e) => {{
  11745. - return e.to_errno();
  11746. - }}
  11747. - }}
  11748. - }}
  11749. -
  11750. - /// # Safety
  11751. - ///
  11752. - /// This function must
  11753. - /// - only be called once,
  11754. - /// - be called after `__init` has been called and returned `0`.
  11755. - unsafe fn __exit() {{
  11756. - // SAFETY: No data race, since `__MOD` can only be accessed by this module
  11757. - // and there only `__init` and `__exit` access it. These functions are only
  11758. - // called once and `__init` was already called.
  11759. - unsafe {{
  11760. - // Invokes `drop()` on `__MOD`, which should be used for cleanup.
  11761. - __MOD = None;
  11762. - }}
  11763. - }}
  11764. -
  11765. - {modinfo}
  11766. - }}
  11767. - }}
  11768. - ",
  11769. - type_ = info.type_,
  11770. - name = info.name,
  11771. - modinfo = modinfo.buffer,
  11772. - initcall_section = ".initcall6.init"
  11773. - )
  11774. - .parse()
  11775. - .expect("Error parsing formatted string into token stream.")
  11776. -}
  11777. diff --git a/rust/macros/paste.rs b/rust/macros/paste.rs
  11778. deleted file mode 100644
  11779. --- a/rust/macros/paste.rs
  11780. +++ /dev/null
  11781. @@ -1,104 +0,0 @@
  11782. -// SPDX-License-Identifier: GPL-2.0
  11783. -
  11784. -use proc_macro::{Delimiter, Group, Ident, Spacing, Span, TokenTree};
  11785. -
  11786. -fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree {
  11787. - let mut tokens = tokens.iter();
  11788. - let mut segments = Vec::new();
  11789. - let mut span = None;
  11790. - loop {
  11791. - match tokens.next() {
  11792. - None => break,
  11793. - Some(TokenTree::Literal(lit)) => {
  11794. - // Allow us to concat string literals by stripping quotes
  11795. - let mut value = lit.to_string();
  11796. - if value.starts_with('"') && value.ends_with('"') {
  11797. - value.remove(0);
  11798. - value.pop();
  11799. - }
  11800. - segments.push((value, lit.span()));
  11801. - }
  11802. - Some(TokenTree::Ident(ident)) => {
  11803. - let mut value = ident.to_string();
  11804. - if value.starts_with("r#") {
  11805. - value.replace_range(0..2, "");
  11806. - }
  11807. - segments.push((value, ident.span()));
  11808. - }
  11809. - Some(TokenTree::Punct(p)) if p.as_char() == ':' => {
  11810. - let Some(TokenTree::Ident(ident)) = tokens.next() else {
  11811. - panic!("expected identifier as modifier");
  11812. - };
  11813. -
  11814. - let (mut value, sp) = segments.pop().expect("expected identifier before modifier");
  11815. - match ident.to_string().as_str() {
  11816. - // Set the overall span of concatenated token as current span
  11817. - "span" => {
  11818. - assert!(
  11819. - span.is_none(),
  11820. - "span modifier should only appear at most once"
  11821. - );
  11822. - span = Some(sp);
  11823. - }
  11824. - "lower" => value = value.to_lowercase(),
  11825. - "upper" => value = value.to_uppercase(),
  11826. - v => panic!("unknown modifier `{v}`"),
  11827. - };
  11828. - segments.push((value, sp));
  11829. - }
  11830. - _ => panic!("unexpected token in paste segments"),
  11831. - };
  11832. - }
  11833. -
  11834. - let pasted: String = segments.into_iter().map(|x| x.0).collect();
  11835. - TokenTree::Ident(Ident::new(&pasted, span.unwrap_or(group_span)))
  11836. -}
  11837. -
  11838. -pub(crate) fn expand(tokens: &mut Vec<TokenTree>) {
  11839. - for token in tokens.iter_mut() {
  11840. - if let TokenTree::Group(group) = token {
  11841. - let delimiter = group.delimiter();
  11842. - let span = group.span();
  11843. - let mut stream: Vec<_> = group.stream().into_iter().collect();
  11844. - // Find groups that looks like `[< A B C D >]`
  11845. - if delimiter == Delimiter::Bracket
  11846. - && stream.len() >= 3
  11847. - && matches!(&stream[0], TokenTree::Punct(p) if p.as_char() == '<')
  11848. - && matches!(&stream[stream.len() - 1], TokenTree::Punct(p) if p.as_char() == '>')
  11849. - {
  11850. - // Replace the group with concatenated token
  11851. - *token = concat(&stream[1..stream.len() - 1], span);
  11852. - } else {
  11853. - // Recursively expand tokens inside the group
  11854. - expand(&mut stream);
  11855. - let mut group = Group::new(delimiter, stream.into_iter().collect());
  11856. - group.set_span(span);
  11857. - *token = TokenTree::Group(group);
  11858. - }
  11859. - }
  11860. - }
  11861. -
  11862. - // Path segments cannot contain invisible delimiter group, so remove them if any.
  11863. - for i in (0..tokens.len().saturating_sub(3)).rev() {
  11864. - // Looking for a double colon
  11865. - if matches!(
  11866. - (&tokens[i + 1], &tokens[i + 2]),
  11867. - (TokenTree::Punct(a), TokenTree::Punct(b))
  11868. - if a.as_char() == ':' && a.spacing() == Spacing::Joint && b.as_char() == ':'
  11869. - ) {
  11870. - match &tokens[i + 3] {
  11871. - TokenTree::Group(group) if group.delimiter() == Delimiter::None => {
  11872. - tokens.splice(i + 3..i + 4, group.stream());
  11873. - }
  11874. - _ => (),
  11875. - }
  11876. -
  11877. - match &tokens[i] {
  11878. - TokenTree::Group(group) if group.delimiter() == Delimiter::None => {
  11879. - tokens.splice(i..i + 1, group.stream());
  11880. - }
  11881. - _ => (),
  11882. - }
  11883. - }
  11884. - }
  11885. -}
  11886. diff --git a/rust/macros/pin_data.rs b/rust/macros/pin_data.rs
  11887. deleted file mode 100644
  11888. --- a/rust/macros/pin_data.rs
  11889. +++ /dev/null
  11890. @@ -1,129 +0,0 @@
  11891. -// SPDX-License-Identifier: Apache-2.0 OR MIT
  11892. -
  11893. -use crate::helpers::{parse_generics, Generics};
  11894. -use proc_macro::{Group, Punct, Spacing, TokenStream, TokenTree};
  11895. -
  11896. -pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
  11897. - // This proc-macro only does some pre-parsing and then delegates the actual parsing to
  11898. - // `kernel::__pin_data!`.
  11899. -
  11900. - let (
  11901. - Generics {
  11902. - impl_generics,
  11903. - decl_generics,
  11904. - ty_generics,
  11905. - },
  11906. - rest,
  11907. - ) = parse_generics(input);
  11908. - // The struct definition might contain the `Self` type. Since `__pin_data!` will define a new
  11909. - // type with the same generics and bounds, this poses a problem, since `Self` will refer to the
  11910. - // new type as opposed to this struct definition. Therefore we have to replace `Self` with the
  11911. - // concrete name.
  11912. -
  11913. - // Errors that occur when replacing `Self` with `struct_name`.
  11914. - let mut errs = TokenStream::new();
  11915. - // The name of the struct with ty_generics.
  11916. - let struct_name = rest
  11917. - .iter()
  11918. - .skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i.to_string() == "struct"))
  11919. - .nth(1)
  11920. - .and_then(|tt| match tt {
  11921. - TokenTree::Ident(_) => {
  11922. - let tt = tt.clone();
  11923. - let mut res = vec![tt];
  11924. - if !ty_generics.is_empty() {
  11925. - // We add this, so it is maximally compatible with e.g. `Self::CONST` which
  11926. - // will be replaced by `StructName::<$generics>::CONST`.
  11927. - res.push(TokenTree::Punct(Punct::new(':', Spacing::Joint)));
  11928. - res.push(TokenTree::Punct(Punct::new(':', Spacing::Alone)));
  11929. - res.push(TokenTree::Punct(Punct::new('<', Spacing::Alone)));
  11930. - res.extend(ty_generics.iter().cloned());
  11931. - res.push(TokenTree::Punct(Punct::new('>', Spacing::Alone)));
  11932. - }
  11933. - Some(res)
  11934. - }
  11935. - _ => None,
  11936. - })
  11937. - .unwrap_or_else(|| {
  11938. - // If we did not find the name of the struct then we will use `Self` as the replacement
  11939. - // and add a compile error to ensure it does not compile.
  11940. - errs.extend(
  11941. - "::core::compile_error!(\"Could not locate type name.\");"
  11942. - .parse::<TokenStream>()
  11943. - .unwrap(),
  11944. - );
  11945. - "Self".parse::<TokenStream>().unwrap().into_iter().collect()
  11946. - });
  11947. - let impl_generics = impl_generics
  11948. - .into_iter()
  11949. - .flat_map(|tt| replace_self_and_deny_type_defs(&struct_name, tt, &mut errs))
  11950. - .collect::<Vec<_>>();
  11951. - let mut rest = rest
  11952. - .into_iter()
  11953. - .flat_map(|tt| {
  11954. - // We ignore top level `struct` tokens, since they would emit a compile error.
  11955. - if matches!(&tt, TokenTree::Ident(i) if i.to_string() == "struct") {
  11956. - vec![tt]
  11957. - } else {
  11958. - replace_self_and_deny_type_defs(&struct_name, tt, &mut errs)
  11959. - }
  11960. - })
  11961. - .collect::<Vec<_>>();
  11962. - // This should be the body of the struct `{...}`.
  11963. - let last = rest.pop();
  11964. - let mut quoted = quote!(::kernel::__pin_data! {
  11965. - parse_input:
  11966. - @args(#args),
  11967. - @sig(#(#rest)*),
  11968. - @impl_generics(#(#impl_generics)*),
  11969. - @ty_generics(#(#ty_generics)*),
  11970. - @decl_generics(#(#decl_generics)*),
  11971. - @body(#last),
  11972. - });
  11973. - quoted.extend(errs);
  11974. - quoted
  11975. -}
  11976. -
  11977. -/// Replaces `Self` with `struct_name` and errors on `enum`, `trait`, `struct` `union` and `impl`
  11978. -/// keywords.
  11979. -///
  11980. -/// The error is appended to `errs` to allow normal parsing to continue.
  11981. -fn replace_self_and_deny_type_defs(
  11982. - struct_name: &Vec<TokenTree>,
  11983. - tt: TokenTree,
  11984. - errs: &mut TokenStream,
  11985. -) -> Vec<TokenTree> {
  11986. - match tt {
  11987. - TokenTree::Ident(ref i)
  11988. - if i.to_string() == "enum"
  11989. - || i.to_string() == "trait"
  11990. - || i.to_string() == "struct"
  11991. - || i.to_string() == "union"
  11992. - || i.to_string() == "impl" =>
  11993. - {
  11994. - errs.extend(
  11995. - format!(
  11996. - "::core::compile_error!(\"Cannot use `{i}` inside of struct definition with \
  11997. - `#[pin_data]`.\");"
  11998. - )
  11999. - .parse::<TokenStream>()
  12000. - .unwrap()
  12001. - .into_iter()
  12002. - .map(|mut tok| {
  12003. - tok.set_span(tt.span());
  12004. - tok
  12005. - }),
  12006. - );
  12007. - vec![tt]
  12008. - }
  12009. - TokenTree::Ident(i) if i.to_string() == "Self" => struct_name.clone(),
  12010. - TokenTree::Literal(_) | TokenTree::Punct(_) | TokenTree::Ident(_) => vec![tt],
  12011. - TokenTree::Group(g) => vec![TokenTree::Group(Group::new(
  12012. - g.delimiter(),
  12013. - g.stream()
  12014. - .into_iter()
  12015. - .flat_map(|tt| replace_self_and_deny_type_defs(struct_name, tt, errs))
  12016. - .collect(),
  12017. - ))],
  12018. - }
  12019. -}
  12020. diff --git a/rust/macros/pinned_drop.rs b/rust/macros/pinned_drop.rs
  12021. deleted file mode 100644
  12022. --- a/rust/macros/pinned_drop.rs
  12023. +++ /dev/null
  12024. @@ -1,49 +0,0 @@
  12025. -// SPDX-License-Identifier: Apache-2.0 OR MIT
  12026. -
  12027. -use proc_macro::{TokenStream, TokenTree};
  12028. -
  12029. -pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
  12030. - let mut toks = input.into_iter().collect::<Vec<_>>();
  12031. - assert!(!toks.is_empty());
  12032. - // Ensure that we have an `impl` item.
  12033. - assert!(matches!(&toks[0], TokenTree::Ident(i) if i.to_string() == "impl"));
  12034. - // Ensure that we are implementing `PinnedDrop`.
  12035. - let mut nesting: usize = 0;
  12036. - let mut pinned_drop_idx = None;
  12037. - for (i, tt) in toks.iter().enumerate() {
  12038. - match tt {
  12039. - TokenTree::Punct(p) if p.as_char() == '<' => {
  12040. - nesting += 1;
  12041. - }
  12042. - TokenTree::Punct(p) if p.as_char() == '>' => {
  12043. - nesting = nesting.checked_sub(1).unwrap();
  12044. - continue;
  12045. - }
  12046. - _ => {}
  12047. - }
  12048. - if i >= 1 && nesting == 0 {
  12049. - // Found the end of the generics, this should be `PinnedDrop`.
  12050. - assert!(
  12051. - matches!(tt, TokenTree::Ident(i) if i.to_string() == "PinnedDrop"),
  12052. - "expected 'PinnedDrop', found: '{:?}'",
  12053. - tt
  12054. - );
  12055. - pinned_drop_idx = Some(i);
  12056. - break;
  12057. - }
  12058. - }
  12059. - let idx = pinned_drop_idx
  12060. - .unwrap_or_else(|| panic!("Expected an `impl` block implementing `PinnedDrop`."));
  12061. - // Fully qualify the `PinnedDrop`, as to avoid any tampering.
  12062. - toks.splice(idx..idx, quote!(::kernel::init::));
  12063. - // Take the `{}` body and call the declarative macro.
  12064. - if let Some(TokenTree::Group(last)) = toks.pop() {
  12065. - let last = last.stream();
  12066. - quote!(::kernel::__pinned_drop! {
  12067. - @impl_sig(#(#toks)*),
  12068. - @impl_body(#last),
  12069. - })
  12070. - } else {
  12071. - TokenStream::from_iter(toks)
  12072. - }
  12073. -}
  12074. diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs
  12075. deleted file mode 100644
  12076. --- a/rust/macros/quote.rs
  12077. +++ /dev/null
  12078. @@ -1,157 +0,0 @@
  12079. -// SPDX-License-Identifier: Apache-2.0 OR MIT
  12080. -
  12081. -use proc_macro::{TokenStream, TokenTree};
  12082. -
  12083. -pub(crate) trait ToTokens {
  12084. - fn to_tokens(&self, tokens: &mut TokenStream);
  12085. -}
  12086. -
  12087. -impl<T: ToTokens> ToTokens for Option<T> {
  12088. - fn to_tokens(&self, tokens: &mut TokenStream) {
  12089. - if let Some(v) = self {
  12090. - v.to_tokens(tokens);
  12091. - }
  12092. - }
  12093. -}
  12094. -
  12095. -impl ToTokens for proc_macro::Group {
  12096. - fn to_tokens(&self, tokens: &mut TokenStream) {
  12097. - tokens.extend([TokenTree::from(self.clone())]);
  12098. - }
  12099. -}
  12100. -
  12101. -impl ToTokens for TokenTree {
  12102. - fn to_tokens(&self, tokens: &mut TokenStream) {
  12103. - tokens.extend([self.clone()]);
  12104. - }
  12105. -}
  12106. -
  12107. -impl ToTokens for TokenStream {
  12108. - fn to_tokens(&self, tokens: &mut TokenStream) {
  12109. - tokens.extend(self.clone());
  12110. - }
  12111. -}
  12112. -
  12113. -/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
  12114. -/// the given span.
  12115. -///
  12116. -/// This is a similar to the
  12117. -/// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the
  12118. -/// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
  12119. -macro_rules! quote_spanned {
  12120. - ($span:expr => $($tt:tt)*) => {{
  12121. - let mut tokens;
  12122. - #[allow(clippy::vec_init_then_push)]
  12123. - {
  12124. - tokens = ::std::vec::Vec::new();
  12125. - let span = $span;
  12126. - quote_spanned!(@proc tokens span $($tt)*);
  12127. - }
  12128. - ::proc_macro::TokenStream::from_iter(tokens)
  12129. - }};
  12130. - (@proc $v:ident $span:ident) => {};
  12131. - (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => {
  12132. - let mut ts = ::proc_macro::TokenStream::new();
  12133. - $crate::quote::ToTokens::to_tokens(&$id, &mut ts);
  12134. - $v.extend(ts);
  12135. - quote_spanned!(@proc $v $span $($tt)*);
  12136. - };
  12137. - (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => {
  12138. - for token in $id {
  12139. - let mut ts = ::proc_macro::TokenStream::new();
  12140. - $crate::quote::ToTokens::to_tokens(&token, &mut ts);
  12141. - $v.extend(ts);
  12142. - }
  12143. - quote_spanned!(@proc $v $span $($tt)*);
  12144. - };
  12145. - (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => {
  12146. - let mut tokens = ::std::vec::Vec::new();
  12147. - quote_spanned!(@proc tokens $span $($inner)*);
  12148. - $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
  12149. - ::proc_macro::Delimiter::Parenthesis,
  12150. - ::proc_macro::TokenStream::from_iter(tokens)
  12151. - )));
  12152. - quote_spanned!(@proc $v $span $($tt)*);
  12153. - };
  12154. - (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => {
  12155. - let mut tokens = ::std::vec::Vec::new();
  12156. - quote_spanned!(@proc tokens $span $($inner)*);
  12157. - $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
  12158. - ::proc_macro::Delimiter::Bracket,
  12159. - ::proc_macro::TokenStream::from_iter(tokens)
  12160. - )));
  12161. - quote_spanned!(@proc $v $span $($tt)*);
  12162. - };
  12163. - (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => {
  12164. - let mut tokens = ::std::vec::Vec::new();
  12165. - quote_spanned!(@proc tokens $span $($inner)*);
  12166. - $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
  12167. - ::proc_macro::Delimiter::Brace,
  12168. - ::proc_macro::TokenStream::from_iter(tokens)
  12169. - )));
  12170. - quote_spanned!(@proc $v $span $($tt)*);
  12171. - };
  12172. - (@proc $v:ident $span:ident :: $($tt:tt)*) => {
  12173. - $v.push(::proc_macro::TokenTree::Punct(
  12174. - ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Joint)
  12175. - ));
  12176. - $v.push(::proc_macro::TokenTree::Punct(
  12177. - ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone)
  12178. - ));
  12179. - quote_spanned!(@proc $v $span $($tt)*);
  12180. - };
  12181. - (@proc $v:ident $span:ident : $($tt:tt)*) => {
  12182. - $v.push(::proc_macro::TokenTree::Punct(
  12183. - ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone)
  12184. - ));
  12185. - quote_spanned!(@proc $v $span $($tt)*);
  12186. - };
  12187. - (@proc $v:ident $span:ident , $($tt:tt)*) => {
  12188. - $v.push(::proc_macro::TokenTree::Punct(
  12189. - ::proc_macro::Punct::new(',', ::proc_macro::Spacing::Alone)
  12190. - ));
  12191. - quote_spanned!(@proc $v $span $($tt)*);
  12192. - };
  12193. - (@proc $v:ident $span:ident @ $($tt:tt)*) => {
  12194. - $v.push(::proc_macro::TokenTree::Punct(
  12195. - ::proc_macro::Punct::new('@', ::proc_macro::Spacing::Alone)
  12196. - ));
  12197. - quote_spanned!(@proc $v $span $($tt)*);
  12198. - };
  12199. - (@proc $v:ident $span:ident ! $($tt:tt)*) => {
  12200. - $v.push(::proc_macro::TokenTree::Punct(
  12201. - ::proc_macro::Punct::new('!', ::proc_macro::Spacing::Alone)
  12202. - ));
  12203. - quote_spanned!(@proc $v $span $($tt)*);
  12204. - };
  12205. - (@proc $v:ident $span:ident ; $($tt:tt)*) => {
  12206. - $v.push(::proc_macro::TokenTree::Punct(
  12207. - ::proc_macro::Punct::new(';', ::proc_macro::Spacing::Alone)
  12208. - ));
  12209. - quote_spanned!(@proc $v $span $($tt)*);
  12210. - };
  12211. - (@proc $v:ident $span:ident + $($tt:tt)*) => {
  12212. - $v.push(::proc_macro::TokenTree::Punct(
  12213. - ::proc_macro::Punct::new('+', ::proc_macro::Spacing::Alone)
  12214. - ));
  12215. - quote_spanned!(@proc $v $span $($tt)*);
  12216. - };
  12217. - (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => {
  12218. - $v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new(stringify!($id), $span)));
  12219. - quote_spanned!(@proc $v $span $($tt)*);
  12220. - };
  12221. -}
  12222. -
  12223. -/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
  12224. -/// mixed site span ([`Span::mixed_site()`]).
  12225. -///
  12226. -/// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro
  12227. -/// from the `quote` crate but provides only just enough functionality needed by the current
  12228. -/// `macros` crate.
  12229. -///
  12230. -/// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site
  12231. -macro_rules! quote {
  12232. - ($($tt:tt)*) => {
  12233. - quote_spanned!(::proc_macro::Span::mixed_site() => $($tt)*)
  12234. - }
  12235. -}
  12236. diff --git a/rust/macros/vtable.rs b/rust/macros/vtable.rs
  12237. deleted file mode 100644
  12238. --- a/rust/macros/vtable.rs
  12239. +++ /dev/null
  12240. @@ -1,96 +0,0 @@
  12241. -// SPDX-License-Identifier: GPL-2.0
  12242. -
  12243. -use proc_macro::{Delimiter, Group, TokenStream, TokenTree};
  12244. -use std::collections::HashSet;
  12245. -use std::fmt::Write;
  12246. -
  12247. -pub(crate) fn vtable(_attr: TokenStream, ts: TokenStream) -> TokenStream {
  12248. - let mut tokens: Vec<_> = ts.into_iter().collect();
  12249. -
  12250. - // Scan for the `trait` or `impl` keyword.
  12251. - let is_trait = tokens
  12252. - .iter()
  12253. - .find_map(|token| match token {
  12254. - TokenTree::Ident(ident) => match ident.to_string().as_str() {
  12255. - "trait" => Some(true),
  12256. - "impl" => Some(false),
  12257. - _ => None,
  12258. - },
  12259. - _ => None,
  12260. - })
  12261. - .expect("#[vtable] attribute should only be applied to trait or impl block");
  12262. -
  12263. - // Retrieve the main body. The main body should be the last token tree.
  12264. - let body = match tokens.pop() {
  12265. - Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Brace => group,
  12266. - _ => panic!("cannot locate main body of trait or impl block"),
  12267. - };
  12268. -
  12269. - let mut body_it = body.stream().into_iter();
  12270. - let mut functions = Vec::new();
  12271. - let mut consts = HashSet::new();
  12272. - while let Some(token) = body_it.next() {
  12273. - match token {
  12274. - TokenTree::Ident(ident) if ident.to_string() == "fn" => {
  12275. - let fn_name = match body_it.next() {
  12276. - Some(TokenTree::Ident(ident)) => ident.to_string(),
  12277. - // Possibly we've encountered a fn pointer type instead.
  12278. - _ => continue,
  12279. - };
  12280. - functions.push(fn_name);
  12281. - }
  12282. - TokenTree::Ident(ident) if ident.to_string() == "const" => {
  12283. - let const_name = match body_it.next() {
  12284. - Some(TokenTree::Ident(ident)) => ident.to_string(),
  12285. - // Possibly we've encountered an inline const block instead.
  12286. - _ => continue,
  12287. - };
  12288. - consts.insert(const_name);
  12289. - }
  12290. - _ => (),
  12291. - }
  12292. - }
  12293. -
  12294. - let mut const_items;
  12295. - if is_trait {
  12296. - const_items = "
  12297. - /// A marker to prevent implementors from forgetting to use [`#[vtable]`](vtable)
  12298. - /// attribute when implementing this trait.
  12299. - const USE_VTABLE_ATTR: ();
  12300. - "
  12301. - .to_owned();
  12302. -
  12303. - for f in functions {
  12304. - let gen_const_name = format!("HAS_{}", f.to_uppercase());
  12305. - // Skip if it's declared already -- this allows user override.
  12306. - if consts.contains(&gen_const_name) {
  12307. - continue;
  12308. - }
  12309. - // We don't know on the implementation-site whether a method is required or provided
  12310. - // so we have to generate a const for all methods.
  12311. - write!(
  12312. - const_items,
  12313. - "/// Indicates if the `{f}` method is overridden by the implementor.
  12314. - const {gen_const_name}: bool = false;",
  12315. - )
  12316. - .unwrap();
  12317. - consts.insert(gen_const_name);
  12318. - }
  12319. - } else {
  12320. - const_items = "const USE_VTABLE_ATTR: () = ();".to_owned();
  12321. -
  12322. - for f in functions {
  12323. - let gen_const_name = format!("HAS_{}", f.to_uppercase());
  12324. - if consts.contains(&gen_const_name) {
  12325. - continue;
  12326. - }
  12327. - write!(const_items, "const {gen_const_name}: bool = true;").unwrap();
  12328. - }
  12329. - }
  12330. -
  12331. - let new_body = vec![const_items.parse().unwrap(), body.stream()]
  12332. - .into_iter()
  12333. - .collect();
  12334. - tokens.push(TokenTree::Group(Group::new(Delimiter::Brace, new_body)));
  12335. - tokens.into_iter().collect()
  12336. -}
  12337. diff --git a/rust/macros/zeroable.rs b/rust/macros/zeroable.rs
  12338. deleted file mode 100644
  12339. --- a/rust/macros/zeroable.rs
  12340. +++ /dev/null
  12341. @@ -1,73 +0,0 @@
  12342. -// SPDX-License-Identifier: GPL-2.0
  12343. -
  12344. -use crate::helpers::{parse_generics, Generics};
  12345. -use proc_macro::{TokenStream, TokenTree};
  12346. -
  12347. -pub(crate) fn derive(input: TokenStream) -> TokenStream {
  12348. - let (
  12349. - Generics {
  12350. - impl_generics,
  12351. - decl_generics: _,
  12352. - ty_generics,
  12353. - },
  12354. - mut rest,
  12355. - ) = parse_generics(input);
  12356. - // This should be the body of the struct `{...}`.
  12357. - let last = rest.pop();
  12358. - // Now we insert `Zeroable` as a bound for every generic parameter in `impl_generics`.
  12359. - let mut new_impl_generics = Vec::with_capacity(impl_generics.len());
  12360. - // Are we inside of a generic where we want to add `Zeroable`?
  12361. - let mut in_generic = !impl_generics.is_empty();
  12362. - // Have we already inserted `Zeroable`?
  12363. - let mut inserted = false;
  12364. - // Level of `<>` nestings.
  12365. - let mut nested = 0;
  12366. - for tt in impl_generics {
  12367. - match &tt {
  12368. - // If we find a `,`, then we have finished a generic/constant/lifetime parameter.
  12369. - TokenTree::Punct(p) if nested == 0 && p.as_char() == ',' => {
  12370. - if in_generic && !inserted {
  12371. - new_impl_generics.extend(quote! { : ::kernel::init::Zeroable });
  12372. - }
  12373. - in_generic = true;
  12374. - inserted = false;
  12375. - new_impl_generics.push(tt);
  12376. - }
  12377. - // If we find `'`, then we are entering a lifetime.
  12378. - TokenTree::Punct(p) if nested == 0 && p.as_char() == '\'' => {
  12379. - in_generic = false;
  12380. - new_impl_generics.push(tt);
  12381. - }
  12382. - TokenTree::Punct(p) if nested == 0 && p.as_char() == ':' => {
  12383. - new_impl_generics.push(tt);
  12384. - if in_generic {
  12385. - new_impl_generics.extend(quote! { ::kernel::init::Zeroable + });
  12386. - inserted = true;
  12387. - }
  12388. - }
  12389. - TokenTree::Punct(p) if p.as_char() == '<' => {
  12390. - nested += 1;
  12391. - new_impl_generics.push(tt);
  12392. - }
  12393. - TokenTree::Punct(p) if p.as_char() == '>' => {
  12394. - assert!(nested > 0);
  12395. - nested -= 1;
  12396. - new_impl_generics.push(tt);
  12397. - }
  12398. - _ => new_impl_generics.push(tt),
  12399. - }
  12400. - }
  12401. - assert_eq!(nested, 0);
  12402. - if in_generic && !inserted {
  12403. - new_impl_generics.extend(quote! { : ::kernel::init::Zeroable });
  12404. - }
  12405. - quote! {
  12406. - ::kernel::__derive_zeroable!(
  12407. - parse_input:
  12408. - @sig(#(#rest)*),
  12409. - @impl_generics(#(#new_impl_generics)*),
  12410. - @ty_generics(#(#ty_generics)*),
  12411. - @body(#last),
  12412. - );
  12413. - }
  12414. -}
  12415. diff --git a/rust/uapi/lib.rs b/rust/uapi/lib.rs
  12416. deleted file mode 100644
  12417. --- a/rust/uapi/lib.rs
  12418. +++ /dev/null
  12419. @@ -1,26 +0,0 @@
  12420. -// SPDX-License-Identifier: GPL-2.0
  12421. -
  12422. -//! UAPI Bindings.
  12423. -//!
  12424. -//! Contains the bindings generated by `bindgen` for UAPI interfaces.
  12425. -//!
  12426. -//! This crate may be used directly by drivers that need to interact with
  12427. -//! userspace APIs.
  12428. -
  12429. -#![no_std]
  12430. -// See <https://github.com/rust-lang/rust-bindgen/issues/1651>.
  12431. -#![cfg_attr(test, allow(deref_nullptr))]
  12432. -#![cfg_attr(test, allow(unaligned_references))]
  12433. -#![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))]
  12434. -#![allow(
  12435. - clippy::all,
  12436. - missing_docs,
  12437. - non_camel_case_types,
  12438. - non_upper_case_globals,
  12439. - non_snake_case,
  12440. - improper_ctypes,
  12441. - unreachable_pub,
  12442. - unsafe_op_in_unsafe_fn
  12443. -)]
  12444. -
  12445. -include!(concat!(env!("OBJTREE"), "/rust/uapi/uapi_generated.rs"));
  12446. diff --git a/rust/uapi/uapi_helper.h b/rust/uapi/uapi_helper.h
  12447. deleted file mode 100644
  12448. --- a/rust/uapi/uapi_helper.h
  12449. +++ /dev/null
  12450. @@ -1,11 +0,0 @@
  12451. -/* SPDX-License-Identifier: GPL-2.0 */
  12452. -/*
  12453. - * Header that contains the headers for which Rust UAPI bindings
  12454. - * will be automatically generated by `bindgen`.
  12455. - *
  12456. - * Sorted alphabetically.
  12457. - */
  12458. -
  12459. -#include <uapi/asm-generic/ioctl.h>
  12460. -#include <uapi/linux/mii.h>
  12461. -#include <uapi/linux/ethtool.h>